dymokomi/luce-ui
Source of release 0.19.0
0.19.0 / src / docking / state / catalog.lucb
#==============================================================================================
#
# docking/state/catalog - The add menu's panels
#
# DESCRIPTION:
# The application names the panels it can make (`DStack.set_catalog`). A group's "+"
# lists them, a tick beside each one already open, then Split Right and Split Down.
# Picking a panel docks it as a tab of that group; picking a split lists the panels
# again, and the next pick docks the chosen one beside the group. A panel already
# open moves there and is selected; one that is not is asked of the application
# through a DockRequest carrying its id, which answers by making and adding it.
#
#==============================================================================================
type CommandSlot = interop.Reference[Command]?
## At most this many panels in a catalog.
let catalog_limit: usize = 64
## The menu's entries past the catalog, in `Catalog.fixed`.
let fixed_divider: usize = 0
let fixed_right: usize = 1
let fixed_down: usize = 2
let fixed_heading: usize = 3
## The catalog's commands, the ones every menu shows after them, and where the
## next pick docks: a tab, or beside the group after a split was picked.
pub struct Catalog:
var entries: CommandSlot[]
var links: ConnectionSlot[]
var count: usize
var fixed: CommandSlot[]
var position: DockPosition
static func make() -> Catalog!:
let entries = try new CommandSlot[catalog_limit] --- in memory.heap
errdefer free(entries) in memory.heap
let links = try new ConnectionSlot[catalog_limit] --- in memory.heap
errdefer free(links) in memory.heap
let fixed = try new CommandSlot[4] --- in memory.heap
for index in 0..<catalog_limit:
entries[index] = none
links[index] = none
for index in 0..<fixed.length: fixed[index] = none
return Catalog(entries = entries, links = links, count = 0, fixed = fixed, position = DockPosition.tab)
## Drop the catalog's commands and their connections, keeping the fixed ones.
func clear():
for index in 0..<self.count:
if let link = self.links[index]: link.release()
if let command = self.entries[index]: command.release()
self.links[index] = none
self.entries[index] = none
self.count = 0
func release():
self.clear()
for slot in self.fixed:
if let command = slot: command.release()
free(self.entries) in memory.heap
free(self.links) in memory.heap
free(self.fixed) in memory.heap
func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
for index in 0..<self.count:
if let link = self.links[index]: link.trace(visit, context)
if let command = self.entries[index]: command.trace(visit, context)
for slot in self.fixed:
if let command = slot: command.trace(visit, context)
extend State:
## Keep `commands` (divider, Split Right, Split Down and the heading shown
## before a split's list), whose triggers the owner has bound to `pick_from_menu`.
pub func set_fixed(commands: const interop.Reference[Command][]) -> !:
if commands.length != 4: error(result.invalid, "the add menu takes four fixed commands")
for index in 0..<4:
if let previous = self.catalog.fixed[index]: previous.release()
self.catalog.fixed[index] = commands[index].clone()
## Offer the panels `ids` (names `titles`) in the add menu, in that order,
## replacing what it offered; `owner` is the state's own reference, which each
## entry's command calls back.
pub func set_catalog(owner: interop.Reference[State], ids: const str[], titles: const str[]) -> !:
if ids.length != titles.length or ids.length > catalog_limit:
error(result.invalid, "a catalog takes up to 64 ids with a title each")
self.catalog.clear()
for index in 0..<ids.length:
if ids[index].length == 0: error(result.invalid, "a catalog id cannot be empty")
let command = try interop.Reference[Command].adopt(new Command(titles[index], id = ids[index]) in memory.heap, command_type, memory.heap)
errdefer command.release()
let picked = try interop.Callback[unit, unit].bind_mutating(owner, State.pick_from_menu)
defer picked.release()
let link = try command.value().on_trigger(picked)
self.catalog.entries[index] = command
self.catalog.links[index] = link
self.catalog.count = index + 1
## The slot of the open panel made from catalog entry `id`, docked or
## floating; 0 when none is open.
pub func catalog_slot(id: str) -> usize:
if id.length == 0: return 0
for index in 1..<self.panels.length:
if let panel = self.panels[index]:
let name = panel.value().id()
defer name.release()
if name.value == id: return index
return 0
## Show the add menu: the catalog with ticks for open panels, then the splits;
## `beside`, after a split was picked, the heading and the catalog alone.
func show_menu(beside: bool) -> !:
let popup = self.popup else error(result.invalid, "dynamic stack has no add menu")
var shown: interop.Reference[Command][68] = ---
var count: usize = 0
if beside:
if let heading = self.catalog.fixed[fixed_heading]:
shown[count] = heading
count += 1
for index in 0..<self.catalog.count:
let command = self.catalog.entries[index] else continue
command.value().set_checked(self.catalog_slot(command.value().id()) != 0)
shown[count] = command
count += 1
if not beside and self.catalog.count > 0:
for slot in [fixed_divider, fixed_right, fixed_down]:
if let command = self.catalog.fixed[slot]:
shown[count] = command
count += 1
# With nothing to offer there is nothing to split beside either.
if count == 0 or (beside and count == 1): return
try popup.value().set_commands(shown[..<count])
try popup.value().open()
## Every add menu command lands here: read which row was chosen, then dock the
## picked panel, or list the panels again after a split was picked.
pub func pick_from_menu(argument: unit) -> interop.Outcome[unit]:
let popup = self.popup else return result.success[unit](())
let chosen = popup.value().chosen()
let count = (i64)self.catalog.count
var entry = chosen
if self.catalog.position != DockPosition.tab:
# The heading comes first in a split's list.
entry = chosen - 1
elif chosen == count + (i64)fixed_right or chosen == count + (i64)fixed_down:
self.catalog.position = DockPosition.right if chosen == count + (i64)fixed_right else DockPosition.bottom
self.show_menu(true) catch failure:
return result.failure[unit](failure.code, failure.message)
return result.success[unit](())
let position = self.catalog.position
self.catalog.position = DockPosition.tab
if entry < 0 or entry >= count: return result.success[unit](())
self.dock_entry((usize)entry, position) catch failure:
return result.failure[unit](failure.code, failure.message)
return result.success[unit](())
## Put catalog entry `entry` at `position` beside the menu's target: moved
## there and selected when open, else asked of the application.
func dock_entry(entry: usize, position: DockPosition) -> !:
let command = self.catalog.entries[entry] else return
let id = command.value().id()
let open = self.catalog_slot(id)
let target = self.menu_target
if open != 0:
let together = not self.floating(open) and not self.floating(target) and self.model.tabs[open].group == self.model.tabs[target].group
if target == 0 or open == target or (position == DockPosition.tab and together):
try self.select(open)
return
let panel = try self.panel(open)
defer panel.release()
let relative = try self.panel(target)
defer relative.release()
try self.move(panel, relative, position)
return
var relative: interop.Reference[Panel]? = none
if target != 0: relative = try self.panel(target)
defer release_panel(relative)
let name = try interop.copy_text(id)
let request = try interop.Reference[DockRequest].adopt(new DockRequest(relative, position, name) in memory.heap, dock_request_type, memory.heap)
defer request.release()
let delivered = self.added.emit(request)
defer delivered.release()
try delivered.get()
## Open the add menu below `group`'s add button (the empty stack's button when
## `group` is 0), aimed at that group's selected panel: the catalog's panels,
## then the ways to split.
pub func open_menu(group: usize) -> !:
let popup = self.popup else error(result.invalid, "dynamic stack has no add menu")
let geometry = self.geometry(self.size())
let bounds = self.strip(group, geometry.panes[group]).add if group != 0 else gpu.Rect(math.max(0.0, self.size().width - self.height()), 0.0, self.height(), self.height())
self.menu_target = self.model.nodes[group].selected if group != 0 else 0
let box = popup.value().layout()
defer box.release()
try box.value().set_popup_point(bounds.x, bounds.y + bounds.height)
self.catalog.position = DockPosition.tab
try self.show_menu(false)