Use straightforward properties instead of storing information in IDs

Sure, I'm tacking on these properties, but it's better to tack onto
objects than to tack onto strings. I'm not using a type checker yet,
but this is a better situation for type checking. (I could extend Button
with mini classes within ToolsBox and ColorsBox, if need be, to give
clear ownership of these properties.)
This commit is contained in:
Isaiah Odhner 2023-04-20 23:27:39 -04:00
parent 24076d9af5
commit 0f4a154eda

View File

@ -311,8 +311,10 @@ class ToolsBox(Container):
with Container(id="tools_box"): with Container(id="tools_box"):
# tool buttons # tool buttons
for tool in Tool: for tool in Tool:
button = Button(tool.get_icon(), id="tool_button_" + tool.name) # TODO: tooltip with tool.get_name()
button = Button(tool.get_icon(), classes="tool_button")
button.can_focus = False button.can_focus = False
button.represented_tool = tool
yield button yield button
class CharInput(Input): class CharInput(Input):
@ -355,9 +357,10 @@ class ColorsBox(Container):
yield CharInput(id="selected_color", classes="color_well") yield CharInput(id="selected_color", classes="color_well")
with Container(id="available_colors"): with Container(id="available_colors"):
for color in palette: for color in palette:
button = Button("", id="color_button_" + color, classes="color_well") button = Button("", classes="color_button color_well")
button.styles.background = color button.styles.background = color
button.can_focus = False button.can_focus = False
button.represented_color = color
yield button yield button
@ -922,8 +925,11 @@ class PaintApp(App):
def watch_selected_tool(self, old_selected_tool: Tool, selected_tool: Tool) -> None: def watch_selected_tool(self, old_selected_tool: Tool, selected_tool: Tool) -> None:
"""Called when selected_tool changes.""" """Called when selected_tool changes."""
self.query_one("#tool_button_" + old_selected_tool.name).remove_class("selected") for button in self.query(".tool_button"):
self.query_one("#tool_button_" + selected_tool.name).add_class("selected") if button.represented_tool == selected_tool:
button.add_class("selected")
else:
button.remove_class("selected")
def watch_selected_color(self, old_selected_color: str, selected_color: str) -> None: def watch_selected_color(self, old_selected_color: str, selected_color: str) -> None:
"""Called when selected_color changes.""" """Called when selected_color changes."""
@ -1690,14 +1696,10 @@ class PaintApp(App):
def on_button_pressed(self, event: Button.Pressed) -> None: def on_button_pressed(self, event: Button.Pressed) -> None:
"""Called when a button is clicked or activated with the keyboard.""" """Called when a button is clicked or activated with the keyboard."""
button_id = event.button.id if "tool_button" in event.button.classes:
# button_classes = event.button.classes self.selected_tool = event.button.represented_tool
elif "color_button" in event.button.classes:
if button_id: self.selected_color = event.button.represented_color
if button_id.startswith("tool_button_"):
self.selected_tool = Tool[button_id[len("tool_button_") :]]
elif button_id.startswith("color_button_"):
self.selected_color = button_id[len("color_button_") :]
def on_tree_node_highlighted(self, event: Tree.NodeHighlighted) -> None: def on_tree_node_highlighted(self, event: Tree.NodeHighlighted) -> None:
""" """