Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jul 22, 2024
1 parent c751e8a commit fff4260
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
10 changes: 6 additions & 4 deletions src/textual/_dispatch_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ def _raise_duplicate_key_handlers_error(
f"Consider combining them into a single handler.",
)

screen = node.screen
try:
screen = node.screen
except Exception:
screen = None
for key_method_name in event.name_aliases:
key_method = get_key_handler(node, key_method_name)
if key_method is not None:
if (key_method := get_key_handler(node, key_method_name)) is not None:
if invoked_method:
_raise_duplicate_key_handlers_error(
key_name, invoked_method.__name__, key_method.__name__
)
# If key handlers return False, then they are not considered handled
# This allows key handlers to do some conditional logic

if not screen.is_active:
if screen is not None and not screen.is_active:
break
handled = (await invoke(key_method, event)) is not False
invoked_method = key_method
Expand Down
2 changes: 1 addition & 1 deletion src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ def is_inline(self) -> bool:
return False if self._driver is None else self._driver.is_inline

@property
def screen_stack(self) -> Sequence[Screen[Any]]:
def screen_stack(self) -> list[Screen[Any]]:
"""A snapshot of the current screen stack.
Returns:
Expand Down
9 changes: 2 additions & 7 deletions src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,20 +1237,15 @@ def dismiss(
!!! note
Only the active screen may be dismissed. If you try to dismiss a screen that isn't active,
this method will raise a `ScreenError`.
Only the active screen may be dismissed. This method will produce a warning in the logs if
called on an inactive screen (but otherwise have no effect).
If `result` is provided and a callback was set when the screen was [pushed][textual.app.App.push_screen], then
the callback will be invoked with `result`.
Args:
result: The optional result to be passed to the result callback.
Raises:
ScreenError: If the screen being dismissed is not active.
ScreenStackError: If trying to dismiss a screen that is not at the top of
the stack.
"""
if not self.is_active:
self.log.warning("Can't dismiss inactive screen")
Expand Down
7 changes: 4 additions & 3 deletions tests/test_message_pump.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from textual._dispatch_key import dispatch_key
from textual.app import App, ComposeResult
from textual.errors import DuplicateKeyHandlers
from textual.events import Key
Expand All @@ -20,7 +21,7 @@ def key_ctrl_i(self):

async def test_dispatch_key_valid_key():
widget = ValidWidget()
result = await widget.dispatch_key(Key(key="x", character="x"))
result = await dispatch_key(widget, Key(key="x", character="x"))
assert result is True
assert widget.called_by == widget.key_x

Expand All @@ -29,7 +30,7 @@ async def test_dispatch_key_valid_key_alias():
"""When you press tab or ctrl+i, it comes through as a tab key event, but handlers for
tab and ctrl+i are both considered valid."""
widget = ValidWidget()
result = await widget.dispatch_key(Key(key="tab", character="\t"))
result = await dispatch_key(widget, Key(key="tab", character="\t"))
assert result is True
assert widget.called_by == widget.key_ctrl_i

Expand All @@ -55,7 +56,7 @@ async def test_dispatch_key_raises_when_conflicting_handler_aliases():
In the terminal, they're the same thing, so we fail fast via exception here."""
widget = DuplicateHandlersWidget()
with pytest.raises(DuplicateKeyHandlers):
await widget.dispatch_key(Key(key="tab", character="\t"))
await dispatch_key(widget, Key(key="tab", character="\t"))
assert widget.called_by == widget.key_tab


Expand Down
8 changes: 5 additions & 3 deletions tests/test_screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from textual import work
from textual.app import App, ComposeResult, ScreenError, ScreenStackError
from textual.app import App, ComposeResult, ScreenStackError
from textual.events import MouseMove
from textual.geometry import Offset
from textual.screen import Screen
Expand Down Expand Up @@ -301,8 +301,10 @@ async def key_p(self) -> None:
app = MyApp()
async with app.run_test() as pilot:
await pilot.press("p")
with pytest.raises(ScreenError):
await app.bottom.dismiss()
# A noop if not the top
stack = list(app.screen_stack)
await app.bottom.dismiss()
assert app.screen_stack == stack


async def test_dismiss_action():
Expand Down

0 comments on commit fff4260

Please sign in to comment.