Source code for simvx.core.ui.toolbar
"""Toolbar: horizontal strip of buttons and separators over a background bar."""
import logging
from ..descriptors import Property
from ..math.types import Vec2
from .button_group import ButtonGroup
from .containers import HBoxContainer
from .core import ThemeColour
from .widgets import Button, Panel
log = logging.getLogger(__name__)
__all__ = ["Toolbar"]
# ============================================================================
# Toolbar: Horizontal strip of buttons with background bar
# ============================================================================
[docs]
class Toolbar(HBoxContainer):
"""Horizontal toolbar with buttons, separators, and a background bar.
Example:
toolbar = Toolbar()
toolbar.add_button("File")
toolbar.add_separator()
tools = ButtonGroup()
toolbar.add_button("Move", group=tools)
toolbar.add_button("Rotate", group=tools)
"""
toolbar_bg = ThemeColour("toolbar_bg")
toolbar_border = ThemeColour("border")
separator_colour = ThemeColour("border_light")
separation = Property(2.0, range=(0, 100), hint="Space between toolbar items", on_change="mark_layout_dirty")
size_y = Property(36.0, range=(0, 10000), hint="Control height", on_change="_on_size_changed")
[docs]
def add_button(
self, text: str, icon=None, toggle: bool = False, group: ButtonGroup | None = None, on_press=None
) -> Button:
"""Add a toolbar button.
Args:
text: Button label text.
icon: Reserved for future icon support.
toggle: If True, the button stays checked when clicked (``toggle_mode``).
group: A ``ButtonGroup`` whose members uncheck each other; implies ``toggle``.
on_press: Callback invoked when button is clicked.
Returns:
The created Button.
"""
btn = Button(text, on_press, toggle_mode=toggle, group=group)
btn.size = Vec2(max(60, len(text) * 10 + 20), self.size.y - 4)
self.add_child(btn)
return btn
[docs]
def add_separator(self):
"""Add a thin vertical separator line between buttons."""
from .theme import StyleBox
sep = Panel()
sep.size = Vec2(2, self.size.y - 8)
sep.style = StyleBox(bg_colour=self.separator_colour, border_width=0.0)
self.add_child(sep)
[docs]
def on_draw(self, renderer):
# Draw background bar
x, y, w, h = self._local_rect()
renderer.rect((x, y), (w, h), fill=self.toolbar_bg)
renderer.line((x, y + h), (x + w, y + h), stroke=self.toolbar_border)