Draw toolbar icons and labels separately for consistent spacing

This commit is contained in:
lm 2025-10-17 15:42:52 +02:00
parent 3212e3c64f
commit 105a93c3f9
1 changed files with 41 additions and 29 deletions

View File

@ -32,8 +32,7 @@ class UIBuilderMixin:
buttons_frame = ttk.Frame(toolbar)
buttons_frame.pack(side=tk.LEFT)
for icon, label, command in buttons:
text = f"{icon} {label}"
self._add_toolbar_button(buttons_frame, text, command)
self._add_toolbar_button(buttons_frame, icon, label, command)
status_container = ttk.Frame(toolbar)
status_container.pack(side=tk.RIGHT, expand=True, fill=tk.X)
@ -200,10 +199,13 @@ class UIBuilderMixin:
swatch.bind("<Enter>", lambda _e: swatch.configure(cursor="hand2"))
swatch.bind("<Leave>", lambda _e: swatch.configure(cursor="arrow"))
def _add_toolbar_button(self, parent, text: str, command) -> None:
def _add_toolbar_button(self, parent, icon: str, label: str, command) -> None:
font = tkfont.Font(root=self.root, family="Segoe UI", size=9)
padding_x = 14
width = font.measure(text) + padding_x * 2
padding_x = 12
gap = font.measure(" ")
icon_width = font.measure(icon)
label_width = font.measure(label)
width = padding_x * 2 + icon_width + gap + label_width
height = 28
radius = 9
bg = self.root.cget("bg") if hasattr(self.root, "cget") else "#f2f2f7"
@ -232,22 +234,31 @@ class UIBuilderMixin:
outline=palette["outline"],
width=1,
)
text_id = canvas.create_text(
width / 2,
height / 2,
text=text,
font=font,
fill=palette["text"],
)
button_data = {
"canvas": canvas,
"rect": rect_id,
"text_id": text_id,
"command": command,
"palette": palette.copy(),
"dimensions": (width, height, radius),
}
icon_id = canvas.create_text(
padding_x,
height / 2,
text=icon,
font=font,
fill=palette["text"],
anchor="w",
)
label_id = canvas.create_text(
padding_x + icon_width + gap,
height / 2,
text=label,
font=font,
fill=palette["text"],
anchor="w",
)
button_data = {
"canvas": canvas,
"rect": rect_id,
"text_ids": (icon_id, label_id),
"command": command,
"palette": palette.copy(),
"dimensions": (width, height, radius),
}
self._toolbar_buttons.append(button_data)
def set_fill(state: str) -> None:
@ -462,14 +473,15 @@ class UIBuilderMixin:
return
bg = self.root.cget("bg") if hasattr(self.root, "cget") else "#f2f2f7"
palette = self._toolbar_palette()
for data in self._toolbar_buttons:
canvas = data["canvas"] # type: ignore[index]
rect_id = data["rect"] # type: ignore[index]
text_id = data["text_id"] # type: ignore[index]
data["palette"] = palette.copy()
canvas.configure(bg=bg)
canvas.itemconfigure(rect_id, fill=palette["normal"], outline=palette["outline"])
canvas.itemconfigure(text_id, fill=palette["text"])
for data in self._toolbar_buttons:
canvas = data["canvas"] # type: ignore[index]
rect_id = data["rect"] # type: ignore[index]
text_ids = data["text_ids"] # type: ignore[index]
data["palette"] = palette.copy()
canvas.configure(bg=bg)
canvas.itemconfigure(rect_id, fill=palette["normal"], outline=palette["outline"])
for text_id in text_ids:
canvas.itemconfigure(text_id, fill=palette["text"])
def _refresh_navigation_buttons_theme(self) -> None:
if not getattr(self, "_nav_buttons", None):