Skip to content

Commit

Permalink
message_ to handle_
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Aug 27, 2021
1 parent 5ba536f commit 5590829
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 31 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.11] - Unreleased

### Changed

- Changed message handlers to use prefix handle\_
- Renamed messages to drop the Message suffix

## [0.1.10] - 2021-08-25

### Added
Expand Down
2 changes: 1 addition & 1 deletion examples/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def make_button(text: str, style: str) -> Button:
*self.buttons.values(), clear=self.ac, numbers=self.numbers, zero=self.zero
)

def message_button_pressed(self, message: ButtonPressed) -> None:
def handle_button_pressed(self, message: ButtonPressed) -> None:
"""A message sent by the button widget"""

assert isinstance(message.sender, Button)
Expand Down
2 changes: 1 addition & 1 deletion examples/code_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def on_mount(self) -> None:
)
await self.view.dock(self.body, edge="top")

async def message_file_click(self, message: FileClick) -> None:
async def handle_file_click(self, message: FileClick) -> None:
"""A message sent by the directory tree when a file is clicked."""

syntax: RenderableType
Expand Down
2 changes: 1 addition & 1 deletion src/textual/message_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ async def on_event(self, event: events.Event) -> None:

async def on_message(self, message: Message) -> None:
_rich_traceback_guard = True
method_name = f"message_{message.name}"
method_name = f"handle_{message.name}"

method = getattr(self, method_name, None)
if method is not None:
Expand Down
12 changes: 6 additions & 6 deletions src/textual/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@rich.repr.auto
class UpdateMessage(Message, verbosity=3):
class Update(Message, verbosity=3):
def __init__(self, sender: MessagePump, widget: Widget):
super().__init__(sender)
self.widget = widget
Expand All @@ -22,22 +22,22 @@ def __rich_repr__(self) -> rich.repr.Result:
yield self.widget

def __eq__(self, other: object) -> bool:
if isinstance(other, UpdateMessage):
if isinstance(other, Update):
return self.widget == other.widget
return NotImplemented

def can_replace(self, message: Message) -> bool:
return isinstance(message, UpdateMessage) and self == message
return isinstance(message, Update) and self == message


@rich.repr.auto
class LayoutMessage(Message, verbosity=3):
class Layout(Message, verbosity=3):
def can_replace(self, message: Message) -> bool:
return isinstance(message, (LayoutMessage, UpdateMessage))
return isinstance(message, (Layout, Update))


@rich.repr.auto
class CursorMoveMessage(Message):
class CursorMove(Message):
def __init__(self, sender: MessagePump, line: int) -> None:
self.line = line
super().__init__(sender)
8 changes: 4 additions & 4 deletions src/textual/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from . import log
from .layout import Layout, NoWidget
from .geometry import Size, Offset, Region
from .messages import UpdateMessage, LayoutMessage
from .messages import Update, Layout
from .reactive import Reactive, watch

from .widget import Widget, Widget
Expand Down Expand Up @@ -89,7 +89,7 @@ def get_offset(self, widget: Widget) -> Offset:
# def check_layout(self) -> bool:
# return super().check_layout() or self.layout.check_update()

async def message_update(self, message: UpdateMessage) -> None:
async def handle_update(self, message: Update) -> None:
if self.is_root_view:
message.stop()
widget = message.widget
Expand All @@ -99,7 +99,7 @@ async def message_update(self, message: UpdateMessage) -> None:
if display_update is not None:
self.app.display(display_update)

async def message_layout(self, message: LayoutMessage) -> None:
async def handle_layout(self, message: Layout) -> None:
if self.is_root_view:
message.stop()
await self.refresh_layout()
Expand Down Expand Up @@ -245,4 +245,4 @@ async def forward_event(self, event: events.Event) -> None:
async def action_toggle(self, name: str) -> None:
widget = self.named_widgets[name]
widget.visible = not widget.visible
await self.post_message(LayoutMessage(self))
await self.post_message(Layout(self))
4 changes: 2 additions & 2 deletions src/textual/views/_window_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..layouts.vertical import VerticalLayout
from ..view import View
from ..message import Message
from ..messages import UpdateMessage, LayoutMessage
from ..messages import Update, Layout
from ..widget import Widget
from ..widgets import Static

Expand Down Expand Up @@ -40,7 +40,7 @@ async def update(self, widget: Widget | RenderableType) -> None:
self.refresh(layout=True)
await self.emit(WindowChange(self))

async def message_update(self, message: UpdateMessage) -> None:
async def handle_update(self, message: Update) -> None:
message.prevent_default()
await self.emit(WindowChange(self))

Expand Down
6 changes: 3 additions & 3 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .geometry import Size
from .message import Message
from .message_pump import MessagePump
from .messages import LayoutMessage, UpdateMessage
from .messages import Layout, Update
from .reactive import Reactive, watch
from ._types import Lines

Expand Down Expand Up @@ -286,11 +286,11 @@ async def on_idle(self, event: events.Idle) -> None:
self.render_cache = None
self.reset_check_repaint()
self.reset_check_layout()
await self.emit(LayoutMessage(self))
await self.emit(Layout(self))
elif self.check_repaint():
self.render_cache = None
self.reset_check_repaint()
await self.emit(UpdateMessage(self, self))
await self.emit(Update(self, self))

async def focus(self) -> None:
await self.app.set_focus(self)
Expand Down
2 changes: 1 addition & 1 deletion src/textual/widgets/_directory_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async def load_directory(self, node: TreeNode[DirEntry]):
await node.expand()
self.refresh(layout=True)

async def message_tree_click(self, message: TreeClick[DirEntry]) -> None:
async def handle_tree_click(self, message: TreeClick[DirEntry]) -> None:
dir_entry = message.node.data
if not dir_entry.is_dir:
await self.emit(FileClick(self, dir_entry.path))
Expand Down
16 changes: 8 additions & 8 deletions src/textual/widgets/_scroll_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .. import events
from ..layouts.grid import GridLayout
from ..message import Message
from ..messages import CursorMoveMessage
from ..messages import CursorMove
from ..scrollbar import ScrollTo, ScrollBar
from ..geometry import clamp
from ..view import View
Expand Down Expand Up @@ -170,27 +170,27 @@ async def key_home(self) -> None:
self.animate("x", self.target_x, duration=1, easing="out_cubic")
self.animate("y", self.target_y, duration=1, easing="out_cubic")

async def message_scroll_up(self, message: Message) -> None:
async def handle_scroll_up(self, message: Message) -> None:
self.page_up()

async def message_scroll_down(self, message: Message) -> None:
async def handle_scroll_down(self, message: Message) -> None:
self.page_down()

async def message_scroll_left(self, message: Message) -> None:
async def handle_scroll_left(self, message: Message) -> None:
self.page_left()

async def message_scroll_right(self, message: Message) -> None:
async def handle_scroll_right(self, message: Message) -> None:
self.page_right()

async def message_scroll_to(self, message: ScrollTo) -> None:
async def handle_scroll_to(self, message: ScrollTo) -> None:
if message.x is not None:
self.target_x = message.x
if message.y is not None:
self.target_y = message.y
self.animate("x", self.target_x, speed=150, easing="out_cubic")
self.animate("y", self.target_y, speed=150, easing="out_cubic")

async def message_window_change(self, message: Message) -> None:
async def handle_window_change(self, message: Message) -> None:
virtual_size = self.window.virtual_size
self.x = self.validate_x(self.x)
self.y = self.validate_y(self.y)
Expand All @@ -204,6 +204,6 @@ async def message_window_change(self, message: Message) -> None:
if self.layout.show_row("hscroll", virtual_size.width > self.size.width):
self.refresh()

def message_cursor_move(self, message: CursorMoveMessage) -> None:
def handle_cursor_move(self, message: CursorMove) -> None:
self.scroll_to_center(message.line)
message.stop()
8 changes: 4 additions & 4 deletions src/textual/widgets/_tree_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .._types import MessageTarget
from ..widget import Widget
from ..message import Message
from ..messages import CursorMoveMessage
from ..messages import CursorMove


NodeID = NewType("NodeID", int)
Expand Down Expand Up @@ -189,11 +189,11 @@ def __init__(
show_cursor: Reactive[bool] = Reactive(False, layout=True)

def watch_show_cursor(self, value: bool) -> None:
self.emit_no_wait(CursorMoveMessage(self, self.cursor_line))
self.emit_no_wait(CursorMove(self, self.cursor_line))

def watch_cursor_line(self, value: int) -> None:
if self.show_cursor:
self.emit_no_wait(CursorMoveMessage(self, value + self.gutter.top))
self.emit_no_wait(CursorMove(self, value + self.gutter.top))

async def add(
self,
Expand Down Expand Up @@ -309,7 +309,7 @@ class TreeApp(App):
async def on_mount(self, event: events.Mount) -> None:
await self.view.dock(TreeControl("Tree Root", data="foo"))

async def message_tree_click(self, message: TreeClick) -> None:
async def handle_tree_click(self, message: TreeClick) -> None:
if message.node.empty:
await message.node.add("foo")
await message.node.add("bar")
Expand Down

0 comments on commit 5590829

Please sign in to comment.