mirror of
https://github.com/1j01/textual-paint.git
synced 2024-11-09 22:38:26 +03:00
Appease the type checker by using maps of buttons
This commit is contained in:
parent
77295bd622
commit
065c5be2cb
12
paint.py
12
paint.py
@ -316,18 +316,19 @@ class ToolsBox(Container):
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Add our buttons."""
|
||||
self.tool_by_button = {}
|
||||
for tool in Tool:
|
||||
# TODO: tooltip with tool.get_name()
|
||||
button = Button(tool.get_icon(), classes="tool_button")
|
||||
button.can_focus = False
|
||||
button.represented_tool = tool
|
||||
self.tool_by_button[button] = tool
|
||||
yield button
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Called when a button is clicked."""
|
||||
|
||||
if "tool_button" in event.button.classes:
|
||||
self.post_message(self.ToolSelected(event.button.represented_tool))
|
||||
self.post_message(self.ToolSelected(self.tool_by_button[event.button]))
|
||||
|
||||
class CharInput(Input):
|
||||
"""Widget for entering a single character."""
|
||||
@ -372,6 +373,7 @@ class ColorsBox(Container):
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Add our selected color and color well buttons."""
|
||||
self.color_by_button = {}
|
||||
with Container(id="palette_selection_box"):
|
||||
# This widget is doing double duty, showing the current color
|
||||
# and showing/editing the current character.
|
||||
@ -382,14 +384,14 @@ class ColorsBox(Container):
|
||||
button = Button("", classes="color_button color_well")
|
||||
button.styles.background = color
|
||||
button.can_focus = False
|
||||
button.represented_color = color
|
||||
self.color_by_button[button] = color
|
||||
yield button
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Called when a button is clicked."""
|
||||
|
||||
if "color_button" in event.button.classes:
|
||||
self.post_message(self.ColorSelected(event.button.represented_color))
|
||||
self.post_message(self.ColorSelected(self.color_by_button[event.button]))
|
||||
|
||||
|
||||
class Selection:
|
||||
@ -1112,7 +1114,7 @@ class PaintApp(App):
|
||||
def watch_selected_tool(self, old_selected_tool: Tool, selected_tool: Tool) -> None:
|
||||
"""Called when selected_tool changes."""
|
||||
for button in self.query(".tool_button"):
|
||||
if button.represented_tool == selected_tool:
|
||||
if selected_tool == self.query_one("ToolsBox", ToolsBox).tool_by_button[button]:
|
||||
button.add_class("selected")
|
||||
else:
|
||||
button.remove_class("selected")
|
||||
|
20
windows.py
20
windows.py
@ -203,6 +203,8 @@ class DialogWindow(Window):
|
||||
|
||||
class CharacterSelectorDialogWindow(DialogWindow):
|
||||
"""A dialog window that lets the user select a character."""
|
||||
# TODO: use a DataTable instead of a bunch of buttons,
|
||||
# for performance, compactness, and better keyboard navigation
|
||||
|
||||
# class CharacterSelected(Message):
|
||||
# """Sent when a character is selected."""
|
||||
@ -222,6 +224,7 @@ class CharacterSelectorDialogWindow(DialogWindow):
|
||||
"""Initialize the dialog window."""
|
||||
super().__init__(handle_button=self.handle_button, *args, **kwargs)
|
||||
self._char_to_highlight = selected_character
|
||||
self._char_by_button: dict[Button, str] = {}
|
||||
self.handle_selected_character = handle_selected_character
|
||||
|
||||
def handle_button(self, button: Button) -> None:
|
||||
@ -229,27 +232,16 @@ class CharacterSelectorDialogWindow(DialogWindow):
|
||||
if button.id == "cancel":
|
||||
self.request_close()
|
||||
else:
|
||||
# self.post_message(self.CharacterSelected(button.char))
|
||||
# self.post_message(self.CharacterSelected(self._char_by_button[button]))
|
||||
# self.close()
|
||||
self.handle_selected_character(button.char)
|
||||
|
||||
# def compose(self) -> ComposeResult:
|
||||
# """Add our buttons."""
|
||||
# with Container(classes="character_buttons"):
|
||||
# for char in self.char_list:
|
||||
# button = Button(char, variant="primary" if char is self._char_to_highlight else "default")
|
||||
# button.char = char
|
||||
# # if char is self._char_to_highlight:
|
||||
# # button.add_class("selected")
|
||||
# yield button
|
||||
# yield Button("Cancel", id="cancel")
|
||||
self.handle_selected_character(self._char_by_button[button])
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Called when the window is mounted."""
|
||||
container = Container(classes="character_buttons")
|
||||
for char in self.char_list:
|
||||
button = Button(char, variant="primary" if char is self._char_to_highlight else "default")
|
||||
button.char = char
|
||||
self._char_by_button[button] = char
|
||||
# if char is self._char_to_highlight:
|
||||
# button.add_class("selected")
|
||||
container.mount(button)
|
||||
|
Loading…
Reference in New Issue
Block a user