@@ -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 :
0 commit comments