Appease the type checker by using maps of buttons

This commit is contained in:
Isaiah Odhner 2023-04-22 13:17:23 -04:00
parent 77295bd622
commit 065c5be2cb
2 changed files with 13 additions and 19 deletions

View File

@ -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")

View File

@ -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)