Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / icon_shelf.luc

## A panel's bottom toolbar, as luced-2d's panels have them: luce-ui's Shelf
## of plain icon buttons, each triggering a command, its command and
## shortcut in the button's tooltip (as the timeline's buttons show theirs).
## Used by the Network panel.
from luce_ui.ui import Shelf, Button, ButtonStyle, Font, Command, Spacer, Widget
from interop import Connection

pub class IconShelf:
    ## The shelf, the panel's footer.
    pub let body: Shelf
    pub let buttons: list[Button]
    let commands: list[Command]
    let connections: list[Connection]

    ## Buttons for `left` (icon, command) pairs at the start and `right` ones
    ## at the end.
    pub func init(self, left: list[(str, Command)], right: list[(str, Command)], font: Font) -> unit!:
        self.buttons = []
        self.commands = []
        self.connections = []
        let children: list[Widget] = []
        for (icon, command) in left + right:
            let button = Button(icon = icon, font = font)
            button.set_style(ButtonStyle.plain)
            let shortcut = command.shortcut()
            button.set_tooltip(command.text(), shortcut.text() if shortcut.is_bound() else "")
            let chosen = command
            self.connections.append(button.on_click(func () -> unit!: chosen.trigger()))
            self.buttons.append(button)
            self.commands.append(command)
        for at in 0..<left.length:
            children.append(self.buttons[at])
        children.append(Spacer())
        for at in left.length..<self.buttons.length:
            children.append(self.buttons[at])
        self.body = Shelf(children)

    ## The buttons follow their commands' enabled state (after
    ## Actions.refresh, which sets it).
    pub func sync_enabled(self):
        for at in 0..<self.buttons.length:
            self.buttons[at].set_enabled(self.commands[at].is_enabled())

    pub func close(self):
        for connection in self.connections:
            connection.disconnect()
        self.connections.clear()