dymokomi/luce-ui
Source of release 0.19.5
0.19.5 / src / widgets / action_popup.lucb
## The popup action list shared by menus, context menus and the command
## palette. Layout owns popup placement, clipping, modal dispatch and focus
## restoration; this module owns row selection, keyboard and pointer choice,
## the palette's search filter, and presenting each command with its shortcut.
from luce_gpu import gpu
from luce_window import input
import interop
from luce_std import math
import memory
import ownership
import layout
import result
from layout import Widget, Layout, Size, Constraints, Event
from font import Font, selected_font
from command import Command, Commands
from painter import Painter
from geometry import Point
from text.line_input import LineInput
import icons
## Room after a checked command's caption for its tick.
let tick_room: f64 = 18.0
## Room at a submenu row's end for its chevron.
let chevron_room: f64 = 16.0
## A hidden popup listing commands, with dividers as slim rules. Opening it
## selects the first enabled row; arrows, Tab, the wheel and the pointer move the
## selection, Enter (or Space, or a click released on the pressed row) triggers
## it and closes the list, and Escape or a press outside closes it. A
## `searchable` popup is the palette: a query line at the window's top that
## lists nothing until something is typed, then the commands whose text contains
## every typed word. A command with items is a submenu's row: pointing at it,
## Right, Enter or a click opens its items beside the row in a nested popup,
## and choosing there closes every menu of the chain. Commands with an icon
## draw it before their caption. `on_close` hears the outermost list close: after
## the chosen command has run, or dismissed with nothing chosen.
pub struct ActionPopup: Widget:
let box: interop.Reference[Layout]
let font: interop.Reference[Font]
var commands: Commands
var selected: i64
var first: i64
## Wheel travel not yet enough for a whole row.
var wheel: f64
## The row to scroll into the middle (-1: none) once the popup's height is
## known, at its first draw or event: it opens on its checked command,
## even when the pointer hovers another row before it is drawn.
var reveal: i64
var pressed: i64
## The command the last choice triggered, as its place in the presented
## commands; -1 before any. One handler serving many rows reads it.
var picked: i64
## Opened by a press still held (a tool group's flyout): letting go over
## an item chooses it.
var held: bool
let searchable: bool
var query: LineInput
var matches: usize[4096]
var count: usize
## The submenu popup, made when a row first opens one; inside it, the menu
## it opened from (which owns it, so outlives it).
var child: interop.Reference[ActionPopup]?
var holder: ActionPopup*?
## The display row whose submenu is open, -1 when none is.
var opened: i64
## Who hears the outermost list close.
let closing: interop.Signal[unit]
pub func init(commands: const interop.Reference[Command][], font: interop.Reference[Font], searchable: bool = false) -> !:
if commands.length == 0 or commands.length > 4096:
error(result.invalid, "a menu takes 1..4096 actions")
var copied = Commands()
errdefer copied.close()
try copied.set(commands)
let box = try layout.make_layout()
errdefer box.release()
try box.value().set_shrink(vertical = 0.0)
box.value().set_focusable(true)
box.value().set_cursor(input.Cursor.pointer)
box.value().set_popup(true)
box.value().set_visible(false)
let closing = try interop.Signal[unit]()
errdefer closing.release()
var query = try LineInput()
self.searchable = searchable
self.query = query
var matches: usize[4096]
self.count = commands.length
for index in 0..<commands.length:
matches[index] = index
self.matches = matches
if searchable:
box.value().set_text_input(true)
box.value().set_popup_placement(layout.PopupPlacement.window_top)
box.value().set_animate_open(true)
self.box = box
self.closing = closing
self.font = font.clone()
self.commands = copied
self.selected = -1
self.wheel = 0.0
self.reveal = -1
self.first = 0
self.pressed = -1
self.picked = -1
self.held = false
self.child = none
self.holder = none
self.opened = -1
self.tidy_dividers()
pub func layout() -> interop.Reference[Layout]:
return self.box.clone()
## Hear the list close: once a chosen command has run, or when it is
## dismissed (Escape, a press outside) with nothing chosen. A submenu
## closing alone is not the menu closing.
pub func on_close(callback: interop.Callback[unit, unit]) -> interop.Reference[interop.Connection]!:
return try self.closing.connect(callback)
## The outermost list of this chain closed: tell its listeners.
func tell_closed() -> interop.Outcome[unit]:
if let up = self.holder:
return up.tell_closed()
return self.closing.emit(())
## Replace the presented commands, for a context menu whose entries depend on
## what was clicked. Any open filtering and selection reset with the new set.
pub func set_commands(commands: const interop.Reference[Command][]) -> !:
if commands.length == 0 or commands.length > 4096:
error(result.invalid, "a menu takes 1..4096 actions")
self.close_submenu()
try self.commands.set(commands)
self.count = commands.length
for index in 0..<commands.length:
self.matches[index] = index
self.tidy_dividers()
self.selected = -1
self.first = 0
self.pressed = -1
self.box.value().invalidate()
pub func is_open() -> bool:
return self.box.value().is_visible()
## The place, among the presented commands, of the one the last choice
## triggered; -1 before any choice.
pub func chosen() -> i64:
return self.picked
## The last choice's place, forgetting it: a host that polls the popup
## (a parameter panel's menus) tells a new choice from none.
pub func take_chosen() -> i64:
let picked = self.picked
self.picked = -1
return picked
## Show the list with an empty query and the first enabled row selected.
pub func open(held: bool = false) -> !:
self.close_submenu()
self.held = held
if held:
self.box.value().claim_press()
try self.query.set_text("")
try self.filter()
self.selected = -1
self.first = 0
self.wheel = 0.0
self.pressed = -1
self.move(1)
# The initial popup has not been placed yet. Its previous (or zero)
# height must not scroll leading disabled entries out of the new menu.
self.first = 0
for index in 0..<(i64)self.count:
let command = self.commands.values()[self.matches[(usize)index]].value()
if command.is_checked() and command.is_enabled() and not command.is_divider():
self.selected = index
self.reveal = index
break
self.box.value().begin_open()
self.box.value().set_visible(true)
self.note_selection()
## The nested popup showing the open row's submenu, if one is open.
pub func submenu() -> interop.Reference[ActionPopup]?:
let child = self.child else return none
if self.opened < 0 or not child.value().is_open():
return none
return child.clone()
## Hide this list and any submenu it opened.
func dismiss():
self.close_submenu()
self.box.value().set_visible(false)
func close_submenu():
if let child = self.child:
child.value().dismiss()
self.opened = -1
## Open display row `index`'s submenu beside it; `select` picks its first
## row (the keyboard opened it), otherwise nothing is selected yet.
func open_submenu(index: i64, select: bool) -> !:
let command = self.commands.values()[self.matches[(usize)index]]
if not command.value().has_items() or not command.value().is_enabled():
return
if self.opened == index:
if let child = self.child:
if child.value().is_open():
if select and child.value().selected < 0:
child.value().move(1)
return
if self.child == none:
let made = try interop.Reference[ActionPopup].adopt(new ActionPopup(command.value().items(), self.font) in memory.heap, action_popup_type, memory.heap)
errdefer made.release()
made.value().holder = &self
let nested = made.value().layout()
defer nested.release()
nested.value().set_submenu(true)
let widget = made.as_interface[Widget](made.value())
defer widget.release()
try self.box.value().set_children([widget])
self.child = made
let child = self.child else return
try child.value().set_commands(command.value().items())
# Beside the row, its first item level with the row.
var y = self.top()
var row = self.first
while row < index:
y += self.extent(row)
row += 1
let nested = child.value().layout()
defer nested.release()
try nested.value().set_popup_point(0.0, y - child.value().pad_y())
nested.value().set_popup_placement(layout.PopupPlacement.beside)
try child.value().open()
if not select:
child.value().selected = -1
child.value().note_selection()
self.opened = index
self.box.value().invalidate()
## Tell the tree whether Right opens the selected row's submenu.
func note_selection():
var opens = false
if self.selected >= 0 and self.selected < (i64)self.count:
opens = self.commands.values()[self.matches[(usize)self.selected]].value().has_items()
self.box.value().set_submenu_selected(opens)
pub func query_text() -> interop.Owned[str]!:
return try self.query.text()
## How many rows the current query lists.
pub func result_count() -> i64:
return (i64)self.count
func filter() -> !:
let query = try self.query.text()
defer query.release()
self.count = 0
# A searchable palette lists nothing until a character is typed, then narrows
# to the matches; an ordinary menu always lists all of its commands.
if not self.searchable or query.value.length > 0:
for index in 0..<self.commands.values().length:
let text = self.commands.values()[index].value().text()
defer text.release()
if matches_query(text.value, query.value):
self.matches[self.count] = index
self.count += 1
self.tidy_dividers()
self.selected = -1
self.first = 0
self.pressed = -1
self.move(1)
self.first = 0
## Drop the listed dividers that separate nothing: a leading or trailing
## one, and any right after another. Menus built from optional groups
## (a context menu whose entries depend on the selection) may leave a
## group empty; every menu presents the result without a blank row.
func tidy_dividers():
var kept: usize = 0
for row in 0..<self.count:
let index = self.matches[row]
if self.commands.values()[index].value().is_divider() and (kept == 0 or self.commands.values()[self.matches[kept - 1]].value().is_divider()):
continue
self.matches[kept] = index
kept += 1
if kept > 0 and self.commands.values()[self.matches[kept - 1]].value().is_divider():
kept -= 1
self.count = kept
func header_height() -> f64:
return (self.pad_y() + self.row_height()) if self.searchable else 0.0
func row_height() -> f64:
return self.box.value().theme().row_height(self.font)
## Interior padding so captions do not crowd the popup's border. The vertical
## margin lifts the first and last rows off the frame; the horizontal margin
## insets every caption evenly on both sides.
func pad_x() -> f64:
return math.ceil(self.font.value().advance() * 1.5)
func pad_y() -> f64:
return math.ceil(self.font.value().advance() * 0.6)
## The y of the first item row, below any search header and the top margin.
func top() -> f64:
return self.header_height() + self.pad_y()
## A divider is a slim row — just a line with a little breathing room — rather than
## a full item's height, so grouped menus read as tight clusters, not a sparse list.
func separator_height() -> f64:
return math.ceil(self.row_height() * 0.5)
## The height of display row `index`: slim for a divider, a full row otherwise.
func extent(index: i64) -> f64:
return self.separator_height() if self.commands.values()[self.matches[(usize)index]].value().is_divider() else self.row_height()
## The display row whose band contains `y`, or -1 when `y` falls on no row. Walks
## the visible rows since divider rows are shorter, so a fixed stride would be wrong.
func index_at(y: f64) -> i64:
var band = self.top()
var index = self.first
while index < (i64)self.count:
let height = self.extent(index)
if y >= band and y < band + height:
return index
band += height
index += 1
return -1
## Total height of the listed rows plus their top and bottom margin.
func stack_height() -> f64:
if self.count == 0:
return 0.0
var total = self.pad_y() * 2.0
if self.searchable:
total += self.row_height() * math.clamp((f64)self.count, 0.0, 10.0)
else:
for index in 0..<(i64)self.count:
total += self.extent(index)
return total
## Room before the captions for icons, when any command has one.
func icon_room() -> f64:
for action in self.commands.values():
if action.value().icon() != "":
return math.ceil(self.font.value().height() * 1.6)
return 0.0
pub func measure(available: Constraints) -> interop.Outcome[Size]:
var width: f64 = 0.0
let icon_room = self.icon_room()
for action in self.commands.values():
if action.value().is_divider():
continue
let text = action.value().text()
defer text.release()
var measured = self.font.value().measure(text.value) catch failure:
return result.failure[Size](failure.code, failure.message)
var chord: u8[32]
let chord_len = action.value().write_shortcut(chord)
if chord_len > 0:
let chord_width = self.font.value().measure((str)chord[..<chord_len]) catch failure:
return result.failure[Size](failure.code, failure.message)
measured += self.font.value().advance() * 2.0 + chord_width
if action.value().is_checked():
measured += tick_room
if action.value().has_items():
measured += chevron_room + self.font.value().advance()
width = math.max(width, measured + icon_room)
return result.success[Size](Size(600.0 if self.searchable else width + self.pad_x() * 2.0, self.header_height() + self.stack_height()))
pub func draw(target: interop.View[gpu.RenderTarget]) -> interop.Outcome[unit]:
self.show_revealed()
self.paint(target) catch failure:
return result.failure[unit](failure.code, failure.message)
return result.success[unit](())
func paint(target: interop.View[gpu.RenderTarget]) -> !:
var painter = try Painter(target)
defer painter.close()
let size = try target.get().size()
let theme = self.box.value().theme()
try painter.rectangle(gpu.Rect(width = size.width, height = size.height), theme.menu)
if self.searchable:
let center = math.max(0.0, (self.header_height() - self.font.value().height()) * 0.5)
try self.query.paint(&painter, self.font, theme, self.pad_x(), center, size.width - self.pad_x() * 2.0)
if self.count > 0:
try painter.rectangle(gpu.Rect(y = self.header_height() - 1.0, width = size.width, height = 1.0), theme.border)
let icon_room = self.icon_room()
var y = self.top()
var index = self.first
while index < (i64)self.count:
if y >= size.height:
break
let action = self.commands.values()[self.matches[(usize)index]]
let height = self.extent(index)
if action.value().is_divider():
let divider_y = math.ceil(y + height * 0.5)
try painter.rectangle(gpu.Rect(self.pad_x(), divider_y, math.max(0.0, size.width - self.pad_x() * 2.0), 1.0), theme.border)
y += height
index += 1
continue
let lit = index == self.selected or index == self.opened
if lit:
try painter.rectangle(gpu.Rect(y = y, width = size.width, height = height), theme.menu_selection)
let caption = action.value().text()
defer caption.release()
let text_y = y + (height - self.font.value().height()) * 0.5
let shown = theme.on_menu_selection(theme.foreground) if lit else theme.foreground
let ink = shown if action.value().is_enabled() else theme.disabled(shown)
if action.value().icon() != "":
let side = self.font.value().height() * 1.15
try icons.draw_named(&painter, action.value().icon(), gpu.Rect(self.pad_x(), y + (height - side) * 0.5, side, side), ink)
try painter.text(caption.value, self.pad_x() + icon_room, text_y, ink, self.font)
if action.value().has_items():
# A chevron drawn, as the tick is.
let x = size.width - self.pad_x() - 5.0
let middle = y + height * 0.5
try painter.line(Point(x - 4.0, middle - 4.5), Point(x, middle), 1.6, ink)
try painter.line(Point(x, middle), Point(x - 4.0, middle + 4.5), 1.6, ink)
if action.value().is_checked():
# A tick drawn, not a glyph: every UI font has the lines.
let x = self.pad_x() + icon_room + (try self.font.value().measure(caption.value)) + 8.0
let middle = y + height * 0.5
try painter.line(Point(x, middle), Point(x + 3.5, middle + 3.5), 1.6, ink)
try painter.line(Point(x + 3.5, middle + 3.5), Point(x + 10.0, middle - 4.0), 1.6, ink)
var chord: u8[32]
let chord_len = action.value().write_shortcut(chord)
if chord_len > 0:
let label = (str)chord[..<chord_len]
let chord_width = try self.font.value().measure(label)
try painter.text(label, math.max(self.pad_x(), size.width - self.pad_x() - chord_width), text_y, theme.on_menu_selection(theme.muted) if lit else theme.muted, self.font)
y += height
index += 1
try painter.rectangle(gpu.Rect(width = size.width, height = 1.0), theme.border)
try painter.rectangle(gpu.Rect(y = math.max(0.0, size.height - 1.0), width = size.width, height = 1.0), theme.border)
try painter.rectangle(gpu.Rect(width = 1.0, height = size.height), theme.border)
try painter.rectangle(gpu.Rect(x = math.max(0.0, size.width - 1.0), width = 1.0, height = size.height), theme.border)
func move(direction: i64):
let count = (i64)self.count
if count == 0:
return
var index = self.selected
for visited in 0..<count:
index = (index + direction + count) % count
if self.commands.values()[self.matches[(usize)index]].value().is_enabled():
self.selected = index
self.scroll_to(index)
self.box.value().repaint()
return
## Scroll so display row `index` shows: up to it when it is above the
## first row shown, else just far enough that it ends inside the popup.
## Rows are measured by their extents, since a divider is a slim row: a
## row stride would scroll a menu that fits (dividers and all) off its
## first rows.
func scroll_to(index: i64):
if index < self.first:
self.first = index
return
let room = self.box.value().bounds().height - self.top() - self.pad_y()
var used: f64 = 0.0
var row = self.first
while row <= index:
used += self.extent(row)
row += 1
while self.first < index and used > room + 0.01:
used -= self.extent(self.first)
self.first += 1
## The wheel scrolls the rows, three to a notch (a trackpad's points by the
## row), and the row now under the pointer is the one selected.
func scroll(event: Event):
self.wheel += event.scroll_y if event.scroll_unit == input.ScrollUnit.points else event.scroll_y * 3.0 * self.row_height()
let rows = (i64)(self.wheel / self.row_height())
if rows == 0:
return
self.wheel -= (f64)rows * self.row_height()
self.first = (i64)math.clamp((f64)(self.first - rows), 0.0, (f64)self.last_first())
let index = self.index_at(event.y)
let over = event.inside and index >= 0 and not self.commands.values()[self.matches[(usize)index]].value().is_divider() and self.commands.values()[self.matches[(usize)index]].value().is_enabled()
self.selected = index if over else -1
self.box.value().repaint()
## Scroll the row to reveal into the middle, now the popup is placed.
func show_revealed():
if self.reveal < 0 or self.box.value().bounds().height <= 0.0:
return
let rows = (i64)math.max(1.0, (self.box.value().bounds().height - self.top() - self.pad_y()) / self.row_height())
self.first = (i64)math.clamp((f64)(self.reveal - rows // 2), 0.0, (f64)self.last_first())
self.selected = self.reveal
self.reveal = -1
## The furthest the list scrolls: the first row that still fills the popup to its last.
func last_first() -> i64:
let room = self.box.value().bounds().height - self.top() - self.pad_y()
var used: f64 = 0.0
var index = (i64)self.count
while index > 0:
used += self.extent(index - 1)
if used > room:
return index
index -= 1
return 0
func activate() -> interop.Outcome[unit]:
if self.selected < 0:
return result.success[unit](())
let action = self.commands.values()[self.matches[(usize)self.selected]].clone()
defer action.release()
if not action.value().is_enabled():
return result.success[unit](())
if action.value().has_items():
self.open_submenu(self.selected, true) catch failure:
return result.failure[unit](failure.code, failure.message)
return result.success[unit](())
# Close before invoking application code, including callbacks which
# replace the containing view or fail while saving a document; a
# submenu's choice closes the menus it opened from too.
self.dismiss()
var up = self.holder
while let menu = up:
menu.dismiss()
up = menu.holder
self.pressed = -1
self.picked = (i64)self.matches[(usize)self.selected]
let triggered = action.value().trigger()
if not triggered.is_success():
return triggered
triggered.release()
return self.tell_closed()
pub func event(event: Event) -> interop.Outcome[unit]:
self.show_revealed()
if self.searchable:
let inset = self.pad_x()
self.query.pointer(event, self.font, inset, 0.0, self.box.value().bounds().width - inset * 2.0, self.header_height()) catch failure:
return result.failure[unit](failure.code, failure.message)
let changed = self.query.event(event) catch failure:
return result.failure[unit](failure.code, failure.message)
# A new filter changes the rows, and so the height; the rest only draws.
if changed:
self.filter() catch failure:
return result.failure[unit](failure.code, failure.message)
self.box.value().invalidate()
else:
self.box.value().repaint()
if event.cancelled:
let shown = self.box.value().is_visible()
self.dismiss()
self.pressed = -1
if shown and self.holder == none:
return self.tell_closed()
elif event.kind == input.EventKind.key_down:
if event.key == input.Key.escape or (event.key == input.Key.left and self.holder != none):
self.dismiss()
if event.key == input.Key.escape and self.holder == none:
return self.tell_closed()
elif event.key == input.Key.right and self.selected >= 0 and self.commands.values()[self.matches[(usize)self.selected]].value().has_items():
self.open_submenu(self.selected, true) catch failure:
return result.failure[unit](failure.code, failure.message)
elif event.key == input.Key.down or (event.key == input.Key.tab and not event.shift):
self.move(1)
elif event.key == input.Key.up or (event.key == input.Key.tab and event.shift):
self.move(-1)
elif (event.key == input.Key.enter or (event.key == input.Key.space and not self.searchable)) and not event.repeated:
return self.activate()
elif event.kind == input.EventKind.scroll:
self.scroll(event)
elif event.kind == input.EventKind.pointer_moved or event.kind == input.EventKind.pointer_down:
let index = self.index_at(event.y)
let over = event.inside and index >= 0 and not self.commands.values()[self.matches[(usize)index]].value().is_divider() and self.commands.values()[self.matches[(usize)index]].value().is_enabled()
if over:
self.selected = index
if event.kind == input.EventKind.pointer_down and event.button == 0:
self.pressed = index
# Pointing at a row opens its submenu, or closes another's.
if self.commands.values()[self.matches[(usize)index]].value().has_items():
self.open_submenu(index, false) catch failure:
return result.failure[unit](failure.code, failure.message)
elif self.opened >= 0:
self.close_submenu()
elif event.kind == input.EventKind.pointer_moved:
# Hovering a divider or a gap between rows highlights nothing, so the
# last item does not linger as if still under the pointer.
self.selected = -1
self.box.value().repaint()
elif event.kind == input.EventKind.pointer_up and event.button == 0:
let index = self.index_at(event.y)
let over = event.inside and index >= 0 and not self.commands.values()[self.matches[(usize)index]].value().is_divider()
let activate = over and ((self.pressed >= 0 and index == self.pressed) or self.held)
self.pressed = -1
# Let go elsewhere, it stays open for a click, as a menu does.
self.held = false
if activate:
return self.activate()
self.note_selection()
return result.success[unit](())
pub func close():
if let child = self.child:
self.child = none
child.release()
self.holder = none
self.closing.close()
self.closing.release()
self.query.close()
self.commands.close()
self.font.release()
self.box.release()
func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
if let child = self.child:
child.trace(visit, context)
self.commands.trace(visit, context)
self.closing.trace(visit, context)
self.font.trace(visit, context)
self.box.trace(visit, context)
pub let action_popup_type: interop.Type[ActionPopup] = interop.Type[ActionPopup]("Menu popup", ActionPopup.close, ActionPopup.trace, closeable = true, main_thread = true)
## Match query words case-insensitively in the caption. ASCII case folding keeps
## UTF-8 bytes intact; literal Unicode words still match without reinterpretation.
func matches_query(text: str, query: str) -> bool:
var start: usize = 0
while start < query.length:
while start < query.length and query.bytes[start] == 32:
start += 1
var end = start
while end < query.length and query.bytes[end] != 32:
end += 1
if end == start:
break
var found = false
var position: usize = 0
while position + end - start <= text.length:
var offset: usize = 0
while offset < end - start and folded(text.bytes[position + offset]) == folded(query.bytes[start + offset]):
offset += 1
if offset == end - start:
found = true
break
position += 1
if not found:
return false
start = end
return true
func folded(byte: u8) -> u8:
return byte + 32 if byte >= 65 and byte <= 90 else byte