Skip to content

Commit 8273f78

Browse files
committed
Fix issue with modals
1 parent 4fcf44c commit 8273f78

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

1212
- Fixed bindings persistance https://github.com/Textualize/textual/issues/1613
13-
- The `Markdown` widget now auto-increments ordered lists https://github.com/Textualize/textual/issues/2002
13+
- Fixed modal bindings https://github.com/Textualize/textual/issues/2194
14+
15+
### Changed
16+
17+
- - The `Markdown` widget now auto-increments ordered lists https://github.com/Textualize/textual/issues/2002
1418

1519
## [0.17.1] - 2023-03-30
1620

src/textual/app.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,38 +1961,34 @@ def bell(self) -> None:
19611961
@property
19621962
def _binding_chain(self) -> list[tuple[DOMNode, Bindings]]:
19631963
"""Get a chain of nodes and bindings to consider.
1964+
19641965
If no widget is focused, returns the bindings from both the screen and the app level bindings.
19651966
Otherwise, combines all the bindings from the currently focused node up the DOM to the root App.
1966-
1967-
Returns:
1968-
List of DOM nodes and their bindings.
19691967
"""
19701968
focused = self.focused
19711969
namespace_bindings: list[tuple[DOMNode, Bindings]]
1972-
screen = self.screen
19731970

19741971
if focused is None:
1975-
if screen.is_modal:
1976-
namespace_bindings = [
1977-
(self.screen, self.screen._bindings),
1978-
]
1979-
else:
1980-
namespace_bindings = [
1981-
(self.screen, self.screen._bindings),
1982-
(self, self._bindings),
1983-
]
1972+
namespace_bindings = [
1973+
(self.screen, self.screen._bindings),
1974+
(self, self._bindings),
1975+
]
19841976
else:
1985-
if screen.is_modal:
1986-
namespace_bindings = [
1987-
(node, node._bindings) for node in focused.ancestors
1988-
]
1989-
else:
1990-
namespace_bindings = [
1991-
(node, node._bindings) for node in focused.ancestors_with_self
1992-
]
1977+
namespace_bindings = [
1978+
(node, node._bindings) for node in focused.ancestors_with_self
1979+
]
19931980

19941981
return namespace_bindings
19951982

1983+
@property
1984+
def _modal_binding_chain(self) -> list[tuple[DOMNode, Bindings]]:
1985+
"""The binding chain, ignoring everything before the last modal."""
1986+
binding_chain = self._binding_chain
1987+
for index, (node, _bindings) in enumerate(binding_chain):
1988+
if node.is_modal:
1989+
return binding_chain[:index]
1990+
return binding_chain
1991+
19961992
async def check_bindings(self, key: str, priority: bool = False) -> bool:
19971993
"""Handle a key press.
19981994
@@ -2004,7 +2000,7 @@ async def check_bindings(self, key: str, priority: bool = False) -> bool:
20042000
True if the key was handled by a binding, otherwise False
20052001
"""
20062002
for namespace, bindings in (
2007-
reversed(self._binding_chain) if priority else self._binding_chain
2003+
reversed(self._binding_chain) if priority else self._modal_binding_chain
20082004
):
20092005
binding = bindings.keys.get(key)
20102006
if binding is not None and binding.priority == priority:

src/textual/dom.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ def auto_refresh(self, interval: float | None) -> None:
208208
)
209209
self._auto_refresh = interval
210210

211+
@property
212+
def is_modal(self) -> bool:
213+
"""Is the node a modal?"""
214+
return False
215+
211216
def _automatic_refresh(self) -> None:
212217
"""Perform an automatic refresh (set with auto_refresh property)."""
213218
self.refresh()

0 commit comments

Comments
 (0)