Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.0

0.5.0 / src / tool_rail.luc

## The tool rail, as Photoshop's: a button for each tool on its own and one
## for each group of alike tools (tools.luc's `tool_groups`), which shows the
## group's current tool with a small corner mark. A click takes that tool; a
## press held a moment opens the group beside it — slide onto another tool
## and let go to take it — and a right-click opens it for a click; the tool
## chosen then shows on the button. A key or a menu choosing a grouped tool
## brings it to the front too.
from luce_ui.ui import Widget, Button, ButtonStyle, ContextMenu, Command
from actions import Actions
from kit import Kit
from tools import tools, tool_groups, group_of, tool_by_id
from workspace import Workspace

## One button on the rail: the tool it shows, and its group, or -1.
class RailSlot:
    pub let button: Button
    pub let widget: Widget
    pub let group: int
    pub var tool: str

    pub func init(self, button: Button, widget: Widget, group: int, tool: str):
        self.button = button
        self.widget = widget
        self.group = group
        self.tool = tool

pub class ToolRail:
    ## The rail's tool widgets, top to bottom.
    pub let items: list[Widget]
    ## Quick Mask's button, which the panels put under the color swatches.
    pub let quick: Button
    let slots: list[RailSlot]
    let actions: Actions

    pub func init(self, actions: Actions, kit: Kit) -> unit!:
        self.actions = actions
        self.items = []
        self.slots = []
        self.quick = kit.tool_button("layer-mask", actions.quick_mask)
        var placed: list[int] = []
        var index = 0
        for tool in tools():
            let group = group_of(tool.id)
            if group < 0:
                let button = kit.tool_button(tool.icon, actions.tools[index])
                self.add(RailSlot(button, button, -1, tool.id))
            elif not (group in placed):
                placed.append(group)
                self.add_group(tool.id, group, kit)
            index += 1

    func add(self, slot: RailSlot):
        self.slots.append(slot)
        self.items.append(slot.widget)

    ## A group's button, showing its first tool, and the flyout of all of them.
    func add_group(self, first: str, group: int, kit: Kit) -> unit!:
        var commands: list[Command] = []
        for id in tool_groups()[group]:
            commands.append(self.command(id))
        let button = Button(icon = tool_by_id(first).icon, font = kit.font)
        button.layout().set_size(kit.tool_side(), kit.tool_side())
        button.set_grouped(true)
        let flyout = ContextMenu(button, commands, font = kit.font)
        let slot = RailSlot(button, flyout, group, first)
        kit.keep(button.on_click(func () -> unit!:
            self.command(slot.tool).trigger()))
        kit.keep(button.on_hold(func () -> unit!:
            # Beside the button, while it is still held, as Photoshop's flyout:
            # slide onto a tool and let go to take it.
            flyout.open_at(button.layout().bounds().width + 2.0, 0.0, held = true)))
        self.add(slot)

    ## Each frame: a group button held a moment opens its flyout, still held.
    pub func poll(self) -> unit!:
        for slot in self.slots:
            if slot.group >= 0:
                slot.button.poll_hold()

    func command(self, id: str) -> Command:
        for command in self.actions.tools:
            if command.id() == id:
                return command
        return self.actions.tools[0]

    ## The active tool marked, and each group showing its last-chosen tool.
    pub func show(self, workspace: Workspace) -> unit!:
        let active = workspace.tools.tool
        let active_group = group_of(active)
        self.quick.set_style(ButtonStyle.selected if workspace.canvas.quick_mask() else ButtonStyle.sunken)
        for slot in self.slots:
            if slot.group >= 0 and slot.group == active_group and slot.tool != active:
                slot.tool = active
                slot.button.set_icon(tool_by_id(active).icon)
            slot.button.set_style(ButtonStyle.selected if slot.tool == active else ButtonStyle.sunken)