Skip to content

Commit

Permalink
micro oprimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jul 12, 2024
1 parent 08c4aad commit 12aa4ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/textual/message_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
26 changes: 15 additions & 11 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
)

Expand Down

0 comments on commit 12aa4ed

Please sign in to comment.