removed debug

This commit is contained in:
Will McGugan
2022-10-18 12:49:54 +01:00
parent 3c4f0129ec
commit 417d6e2d33
2 changed files with 37 additions and 22 deletions

View File

@@ -7,6 +7,7 @@ import os
import platform import platform
import sys import sys
import warnings import warnings
from collections import defaultdict
from contextlib import redirect_stderr, redirect_stdout from contextlib import redirect_stderr, redirect_stdout
from datetime import datetime from datetime import datetime
from pathlib import Path, PurePath from pathlib import Path, PurePath
@@ -31,7 +32,7 @@ from ._callback import invoke
from ._context import active_app from ._context import active_app
from ._event_broker import NoHandler, extract_handler_actions from ._event_broker import NoHandler, extract_handler_actions
from ._filter import LineFilter, Monochrome from ._filter import LineFilter, Monochrome
from .binding import Bindings, NoBinding from .binding import Binding, Bindings, NoBinding
from .css.query import NoMatches from .css.query import NoMatches
from .css.stylesheet import Stylesheet from .css.stylesheet import Stylesheet
from .design import ColorSystem from .design import ColorSystem
@@ -320,16 +321,29 @@ class App(Generic[ReturnType], DOMNode):
return self.screen.focused return self.screen.focused
@property @property
def bindings(self) -> Bindings: def namespace_bindings(self) -> dict[str, tuple[object, Binding]]:
"""Get current bindings. If no widget is focused, then the app-level bindings """Get current bindings. If no widget is focused, then the app-level bindings
are returned. If a widget is focused, then any bindings present in the active are returned. If a widget is focused, then any bindings present in the active
screen and app are merged and returned.""" screen and app are merged and returned."""
if self.focused is None:
return Bindings.merge([self.screen._bindings, self._bindings]) focused = self.focused
namespace_bindings: list[tuple[object, Bindings]]
if focused is None:
namespace_bindings = [
(self, self._bindings),
(self.screen, self.screen._bindings),
]
else: else:
return Bindings.merge( namespace_bindings = [
node._bindings for node in reversed(self.focused.ancestors) (node, node._bindings) for node in reversed(focused.ancestors)
) ]
namespace_binding_map: dict[str, tuple[object, Binding]] = {}
for namespace, bindings in namespace_bindings:
for key, binding in bindings.keys.items():
namespace_binding_map[key] = (namespace, binding)
return namespace_binding_map
def _set_active(self) -> None: def _set_active(self) -> None:
"""Set this app to be the currently active app.""" """Set this app to be the currently active app."""
@@ -1261,7 +1275,7 @@ class App(Generic[ReturnType], DOMNode):
if not self.is_headless: if not self.is_headless:
self.console.bell() self.console.bell()
async def press(self, key: str) -> bool: async def check_bindings(self, key: str) -> bool:
"""Handle a key press. """Handle a key press.
Args: Args:
@@ -1271,11 +1285,11 @@ class App(Generic[ReturnType], DOMNode):
bool: True if the key was handled by a binding, otherwise False bool: True if the key was handled by a binding, otherwise False
""" """
try: try:
binding = self.bindings.get_key(key) namespace, binding = self.namespace_bindings[key]
except NoBinding: except KeyError:
return False return False
else: else:
await self.action(binding.action) await self.action(binding.action, default_namespace=namespace)
return True return True
async def on_event(self, event: events.Event) -> None: async def on_event(self, event: events.Event) -> None:
@@ -1291,16 +1305,12 @@ class App(Generic[ReturnType], DOMNode):
if isinstance(event, events.MouseEvent): if isinstance(event, events.MouseEvent):
# Record current mouse position on App # Record current mouse position on App
self.mouse_position = Offset(event.x, event.y) self.mouse_position = Offset(event.x, event.y)
if isinstance(event, events.Key) and self.focused is not None: if isinstance(event, events.Key):
# Key events are sent direct to focused widget of the currently active screen forward_target = self.focused or self.screen
if self.bindings.allow_forward(event.key): await forward_target._forward_event(event)
await self.focused._forward_event(event)
else:
# Key has allow_forward=False which disallows forward to focused widget
await super().on_event(event)
else: else:
# Forward the event to the currently active Screen
await self.screen._forward_event(event) await self.screen._forward_event(event)
elif isinstance(event, events.Paste): elif isinstance(event, events.Paste):
if self.focused is not None: if self.focused is not None:
await self.focused._forward_event(event) await self.focused._forward_event(event)
@@ -1404,7 +1414,7 @@ class App(Generic[ReturnType], DOMNode):
elif event.key == "shift+tab": elif event.key == "shift+tab":
self.screen.focus_previous() self.screen.focus_previous()
else: else:
if not (await self.press(event.key)): if not (await self.check_bindings(event.key)):
await self.dispatch_key(event) await self.dispatch_key(event)
async def _on_shutdown_request(self, event: events.ShutdownRequest) -> None: async def _on_shutdown_request(self, event: events.ShutdownRequest) -> None:
@@ -1430,8 +1440,8 @@ class App(Generic[ReturnType], DOMNode):
if parent is not None: if parent is not None:
parent.refresh(layout=True) parent.refresh(layout=True)
async def action_press(self, key: str) -> None: async def action_check_binding(self, key: str) -> None:
await self.press(key) await self.check_bindings(key)
async def action_quit(self) -> None: async def action_quit(self) -> None:
"""Quit the app as soon as possible.""" """Quit the app as soon as possible."""

View File

@@ -230,13 +230,16 @@ class Screen(Widget):
Args: Args:
widget (Widget): A widget that is removed. widget (Widget): A widget that is removed.
""" """
print("_reset_focus", widget)
if self.focused is widget: if self.focused is widget:
for sibling in widget.siblings: for sibling in widget.siblings:
if sibling.can_focus: if sibling.can_focus:
print("MOVED focus to", sibling)
sibling.focus() sibling.focus()
break break
else: else:
self.focused = None self.focused = None
print("RESET FOCUS in _reset_focus")
def set_focus(self, widget: Widget | None, scroll_visible: bool = True) -> None: def set_focus(self, widget: Widget | None, scroll_visible: bool = True) -> None:
"""Focus (or un-focus) a widget. A focused widget will receive key events first. """Focus (or un-focus) a widget. A focused widget will receive key events first.
@@ -255,6 +258,7 @@ class Screen(Widget):
self.focused.post_message_no_wait(events.Blur(self)) self.focused.post_message_no_wait(events.Blur(self))
self.focused.emit_no_wait(events.DescendantBlur(self)) self.focused.emit_no_wait(events.DescendantBlur(self))
self.focused = None self.focused = None
print("FOCUSED", None)
elif widget.can_focus: elif widget.can_focus:
if self.focused != widget: if self.focused != widget:
if self.focused is not None: if self.focused is not None:
@@ -263,6 +267,7 @@ class Screen(Widget):
self.focused.emit_no_wait(events.DescendantBlur(self)) self.focused.emit_no_wait(events.DescendantBlur(self))
# Change focus # Change focus
self.focused = widget self.focused = widget
print("FOCUSED", widget)
# Send focus event # Send focus event
if scroll_visible: if scroll_visible:
self.screen.scroll_to_widget(widget) self.screen.scroll_to_widget(widget)