From 12aa4ed7229903279c7fa576a6741477e8304b06 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 12 Jul 2024 15:51:01 +0100 Subject: [PATCH] micro oprimizations --- src/textual/message_pump.py | 3 +-- src/textual/widget.py | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/textual/message_pump.py b/src/textual/message_pump.py index f5b312b0f6..42bbe7df3f 100644 --- a/src/textual/message_pump.py +++ b/src/textual/message_pump.py @@ -298,8 +298,7 @@ def check_message_enabled(self, message: Message) -> bool: `True` if the message will be sent, or `False` if it is disabled. """ - enabled = type(message) not in self._disabled_messages - return enabled + return type(message) not in self._disabled_messages def disable_messages(self, *messages: type[Message]) -> None: """Disable message types from being processed.""" diff --git a/src/textual/widget.py b/src/textual/widget.py index fcdbc8aa60..5c00c8099a 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -107,6 +107,8 @@ _NULL_STYLE = Style() +_MOUSE_EVENTS_DISALLOW_IF_DISABLED = (events.MouseEvent, events.Enter, events.Leave) +_MOUSE_EVENTS_ALLOW_IF_DISABLED = (events.MouseScrollDown, events.MouseScrollUp) class AwaitMount: @@ -1782,11 +1784,13 @@ def virtual_region_with_margin(self) -> Region: @property def _self_or_ancestors_disabled(self) -> bool: """Is this widget or any of its ancestors disabled?""" - return any( - node.disabled - for node in self.ancestors_with_self - if isinstance(node, Widget) - ) + + node: Widget | None = self + while isinstance(node, Widget) and not node.is_dom_root: + if node.disabled: + return True + node = node._parent # type:ignore[assignment] + return False @property def focusable(self) -> bool: @@ -3714,20 +3718,20 @@ def check_message_enabled(self, message: Message) -> bool: `True` if the message will be sent, or `False` if it is disabled. """ # Do the normal checking and get out if that fails. - if not super().check_message_enabled(message): - return False - message_type = type(message) - if self._is_prevented(message_type): + if not super().check_message_enabled(message) or self._is_prevented( + type(message) + ): return False + # Mouse scroll events should always go through, this allows mouse # wheel scrolling to pass through disabled widgets. - if isinstance(message, (events.MouseScrollDown, events.MouseScrollUp)): + if isinstance(message, _MOUSE_EVENTS_ALLOW_IF_DISABLED): return True # Otherwise, if this is any other mouse event, the widget receiving # the event must not be disabled at this moment. return ( not self._self_or_ancestors_disabled - if isinstance(message, (events.MouseEvent, events.Enter, events.Leave)) + if isinstance(message, _MOUSE_EVENTS_DISALLOW_IF_DISABLED) else True )