Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / node_context_menu.luc

## The network view's right-click menu, which the Outliner opens on a node's
## row too. On a node: Rename, Cut, Copy, Paste and Duplicate, the node's
## flags, Set Output, Group and Delete. On empty canvas: Paste, and Create holding
## the Tab menu's nodes for the network shown, in its categories (the menu
## bar's Create). Every entry is one of Actions' commands, so its shortcut
## shows and it is disabled where it does not apply (Actions.refresh). The
## menu is a ContextMenu anchored on an empty label in the footer, its
## entries chosen as it opens.
from luce_ui.ui import Text, Font, Command, ContextMenu
from workspace import Workspace
from actions import Actions
from top_menus import node_categories

pub class NodeContextMenu:
    let workspace: Workspace
    let actions: Actions
    let anchor: Text
    pub let menu: ContextMenu
    ## The entries as last opened.
    pub let commands: list[Command]

    pub func init(self, workspace: Workspace, actions: Actions, font: Font) -> unit!:
        self.workspace = workspace
        self.actions = actions
        self.anchor = Text("", font = font)
        self.commands = self.node_entries()
        self.menu = ContextMenu(self.anchor, self.commands, font = font)

    ## A node's entries: naming and copying, its flags, then what changes
    ## the network around it.
    pub func node_entries(self) -> list[Command]!:
        let a = self.actions
        return [a.rename, Command("", divider = true), a.cut, a.copy, a.paste, a.duplicate, Command("", divider = true),
                a.display, a.enabled, a.freeze, Command("", divider = true), a.set_output, a.group, Command("", divider = true), a.delete]

    ## Empty canvas's entries: Paste, and Create with the node categories.
    pub func canvas_entries(self) -> list[Command]!:
        let create = Command("Create", id = "create_nodes")
        create.set_items(node_categories(self.workspace, self.actions))
        return [self.actions.paste, Command("", divider = true), create]

    ## Open at (`x`, `y`) in window points: a node's entries when `on_node`
    ## (for the node selected), else the canvas's.
    pub func open(self, x: float, y: float, on_node: bool) -> unit!:
        self.actions.refresh()
        let entries = self.node_entries() if on_node else self.canvas_entries()
        # The new entries reach the menu before the old ones are let go.
        self.menu.set_commands(entries)
        self.commands.clear()
        self.commands.extend(entries)
        let origin = self.anchor.layout().bounds()
        self.menu.open_at(x - origin.x, y - origin.y)

    pub func is_open(self) -> bool:
        return self.menu.is_open()