Merge pull request #1421 from davep/main

Allow `App.dark` to be set as early as `App.on_load`
This commit is contained in:
Will McGugan
2022-12-21 11:46:57 +00:00
committed by GitHub
3 changed files with 54 additions and 2 deletions

View File

@@ -7,10 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.8.0] - Unreleased
### Fixed
### Fixed
- Fixed issues with nested auto dimensions https://github.com/Textualize/textual/issues/1402
- Fixed watch method incorrectly running on first set when value hasn't changed and init=False https://github.com/Textualize/textual/pull/1367
- `App.dark` can now be set from `App.on_load` without an error being raised https://github.com/Textualize/textual/issues/1369
### Added

View File

@@ -480,7 +480,14 @@ class App(Generic[ReturnType], DOMNode):
"""Watches the dark bool."""
self.set_class(dark, "-dark-mode")
self.set_class(not dark, "-light-mode")
self.refresh_css()
try:
self.refresh_css()
except ScreenStackError:
# It's possible that `dark` can be set before we have a default
# screen, in an app's `on_load`, for example. So let's eat the
# ScreenStackError -- the above styles will be handled once the
# screen is spun up anyway.
pass
def get_driver_class(self) -> Type[Driver]:
"""Get a driver class for this platform.

44
tests/test_dark_toggle.py Normal file
View File

@@ -0,0 +1,44 @@
from textual.app import App
class OnLoadDarkSwitch(App[None]):
"""App for testing toggling dark mode in on_load."""
def on_load(self) -> None:
self.dark = not self.dark
async def test_toggle_dark_on_load() -> None:
"""It should be possible to toggle dark mode in on_load."""
async with OnLoadDarkSwitch().run_test() as pilot:
assert not pilot.app.dark
class OnMountDarkSwitch(App[None]):
"""App for testing toggling dark mode in on_mount."""
def on_mount(self) -> None:
self.dark = not self.dark
async def test_toggle_dark_on_mount() -> None:
"""It should be possible to toggle dark mode in on_mount."""
async with OnMountDarkSwitch().run_test() as pilot:
assert not pilot.app.dark
class ActionDarkSwitch(App[None]):
"""App for testing toggling dark mode from an action."""
BINDINGS = [("d", "toggle", "Toggle Dark Mode")]
def action_toggle(self) -> None:
self.dark = not self.dark
async def test_toggle_dark_in_action() -> None:
"""It should be possible to toggle dark mode with an action."""
async with OnMountDarkSwitch().run_test() as pilot:
await pilot.press("d")
await pilot.pause(2 / 100)
assert not pilot.app.dark