From e8baf52bddb95c37839b6653d8047bdd9743df14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Tue, 9 May 2023 16:55:43 +0100 Subject: [PATCH] Allow setting (sub)title of any type. Related issues: #2521. --- CHANGELOG.md | 8 ++++++++ src/textual/app.py | 8 ++++++++ tests/test_app.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa2923fac..542d137f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). + +## Unrealeased + +### Changed + +- App `title` and `sub_title` attributes can be set to any type https://github.com/Textualize/textual/issues/2521 + + ## [0.24.1] - 2023-05-08 ### Fixed diff --git a/src/textual/app.py b/src/textual/app.py index 113a7a220..e5987f857 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -316,6 +316,7 @@ class App(Generic[ReturnType], DOMNode): the name of the app if it doesn't. Assign a new value to this attribute to change the title. + The new value is always converted to string. """ self.sub_title = self.SUB_TITLE if self.SUB_TITLE is not None else "" @@ -328,6 +329,7 @@ class App(Generic[ReturnType], DOMNode): the file being worker on. Assign a new value to this attribute to change the sub-title. + The new value is always converted to string. """ self._logger = Logger(self._log) @@ -406,6 +408,12 @@ class App(Generic[ReturnType], DOMNode): self.set_class(self.dark, "-dark-mode") self.set_class(not self.dark, "-light-mode") + def validate_title(self, title: Any) -> str: + return str(title) + + def validate_sub_title(self, sub_title: Any) -> str: + return str(sub_title) + @property def workers(self) -> WorkerManager: """The [worker](guide/workers/) manager. diff --git a/tests/test_app.py b/tests/test_app.py index e529cfbd7..54bde8221 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -36,3 +36,33 @@ async def test_hover_update_styles(): # We've hovered, so ensure the pseudoclass is present and background changed assert button.pseudo_classes == {"enabled", "hover"} assert button.styles.background != initial_background + + +def test_setting_title(): + app = MyApp() + app.title = None + assert app.title == "None" + + app.title = "" + assert app.title == "" + + app.title = 0.125 + assert app.title == "0.125" + + app.title = [True, False, 2] + assert app.title == "[True, False, 2]" + + +def test_setting_sub_title(): + app = MyApp() + app.sub_title = None + assert app.sub_title == "None" + + app.sub_title = "" + assert app.sub_title == "" + + app.sub_title = 0.125 + assert app.sub_title == "0.125" + + app.sub_title = [True, False, 2] + assert app.sub_title == "[True, False, 2]"