Shape library (cookies, clover, scallop) with morphing Shape component, press ripple in StateLayer, content card shell, shaped file glyphs, card grid tiles, dialog header shapes, menu pills, wavy toast progress. Co-Authored-By: Claude Opus 5.5 <[email protected]>
386 lines
13 KiB
Text
386 lines
13 KiB
Text
import { Theme } from "../theme.slint";
|
|
import { Motion } from "../motion.slint";
|
|
import { Icon, Icons } from "icon.slint";
|
|
import { ForceState, StateLayer } from "state-layer.slint";
|
|
import { Overlay } from "overlay.slint";
|
|
|
|
// One entry of a submenu (and the fields every menu item shares). `shortcut` is shown, not
|
|
// bound: the window owns the key bindings.
|
|
export struct MenuEntry {
|
|
text: string,
|
|
// Glyph from `Icons`; empty for none.
|
|
icon: string,
|
|
shortcut: string,
|
|
disabled: bool,
|
|
separator: bool,
|
|
}
|
|
|
|
// One entry of a menu; a non-empty `submenu` opens one more level (and no deeper).
|
|
export struct MenuItem {
|
|
text: string,
|
|
icon: string,
|
|
shortcut: string,
|
|
disabled: bool,
|
|
separator: bool,
|
|
submenu: [MenuEntry],
|
|
}
|
|
|
|
component MenuRow inherits StateLayer {
|
|
in property <MenuEntry> entry;
|
|
in property <bool> leading-icon;
|
|
in property <bool> has-submenu;
|
|
// Keyboard highlight, or its submenu is open.
|
|
in property <bool> highlighted;
|
|
|
|
property <color> content: root.enabled ? Theme.on-surface : Theme.on-surface.with-alpha(Theme.disabled-content);
|
|
property <color> detail: root.enabled ? Theme.on-surface-variant : Theme.on-surface.with-alpha(Theme.disabled-content);
|
|
|
|
enabled: !root.entry.separator && !root.entry.disabled;
|
|
force-state: root.highlighted ? ForceState.hover : ForceState.none;
|
|
height: root.entry.separator ? Theme.space-2 + Theme.hairline : 36px;
|
|
border-radius: Theme.corner-menu-item;
|
|
squeeze: Motion.squeeze-large;
|
|
|
|
accessible-role: list-item;
|
|
accessible-label: root.entry.text;
|
|
accessible-enabled: root.enabled;
|
|
accessible-action-default => {
|
|
self.activate();
|
|
}
|
|
|
|
if root.entry.separator: Rectangle {
|
|
y: Theme.space-1;
|
|
height: Theme.hairline;
|
|
background: Theme.outline-variant;
|
|
}
|
|
|
|
// Always present (hidden for a separator) so the menu can size to the widest label.
|
|
preferred-width: content.preferred-width;
|
|
|
|
content := HorizontalLayout {
|
|
visible: !root.entry.separator;
|
|
padding-left: Theme.space-3;
|
|
padding-right: Theme.space-3;
|
|
spacing: Theme.space-3;
|
|
|
|
if root.leading-icon: VerticalLayout {
|
|
alignment: center;
|
|
|
|
Icon {
|
|
glyph: root.entry.icon;
|
|
size: 20px;
|
|
color: root.detail;
|
|
}
|
|
}
|
|
|
|
Text {
|
|
horizontal-stretch: 1;
|
|
text: root.entry.text;
|
|
color: root.content;
|
|
font-size: Theme.body-medium.size;
|
|
font-weight: Theme.body-medium.weight;
|
|
vertical-alignment: center;
|
|
overflow: elide;
|
|
}
|
|
|
|
if root.entry.shortcut != "": Text {
|
|
text: root.entry.shortcut;
|
|
color: root.detail;
|
|
font-size: Theme.body-small.size;
|
|
font-weight: Theme.body-small.weight;
|
|
vertical-alignment: center;
|
|
}
|
|
|
|
if root.has-submenu: VerticalLayout {
|
|
alignment: center;
|
|
|
|
Icon {
|
|
glyph: Icons.chevron-right;
|
|
size: 20px;
|
|
color: root.detail;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// The floating surface of a menu or submenu: grows from `origin` while `progress` rises.
|
|
component MenuSurface inherits Rectangle {
|
|
in property <float> progress;
|
|
in property <Point> origin;
|
|
|
|
background: Theme.surface-container;
|
|
border-radius: Theme.corner-menu;
|
|
drop-shadow-blur: Theme.float-shadow-blur;
|
|
drop-shadow-offset-y: Theme.float-shadow-offset;
|
|
drop-shadow-color: Theme.shadow.with-alpha(Theme.float-shadow-opacity);
|
|
opacity: root.progress;
|
|
transform-origin: root.origin;
|
|
transform-scale: Motion.enter-scale + (1 - Motion.enter-scale) * root.progress;
|
|
visible: root.progress > 0;
|
|
|
|
// Clicks between rows stay on the menu instead of dismissing it.
|
|
TouchArea { }
|
|
|
|
@children
|
|
}
|
|
|
|
// A popup menu: icons, shortcuts, separators, disabled items and one submenu level. Place it
|
|
// last in the window (`width: 100%; height: 100%;`) and call `open-at` with window
|
|
// coordinates, e.g. a button's `absolute-position` plus its height, or the pointer.
|
|
// It flips to stay inside the window, closes on a click outside or Escape, and reports the
|
|
// chosen item through `activated` (`sub-index` is -1 for a top-level item).
|
|
export component Menu inherits Overlay {
|
|
in property <[MenuItem]> items;
|
|
// Reserve a leading icon column so labels line up; set it when any item has an icon.
|
|
in property <bool> leading-icons;
|
|
callback activated(index: int, sub-index: int);
|
|
// After the menu was closed for any reason: give focus back to the opener here.
|
|
callback closed;
|
|
|
|
public function open-at(x: length, y: length) {
|
|
root.anchor-x = x - root.absolute-position.x;
|
|
root.anchor-y = y - root.absolute-position.y;
|
|
root.current = -1;
|
|
root.close-sub();
|
|
root.open = true;
|
|
keys.focus();
|
|
}
|
|
|
|
// Highlights `index` and opens its submenu as the Right key does (a menu bar moving in).
|
|
public function expand(index: int) {
|
|
root.current = index;
|
|
root.choose(index, true);
|
|
}
|
|
|
|
public function close() {
|
|
if !root.open {
|
|
return;
|
|
}
|
|
root.open = false;
|
|
root.close-sub();
|
|
keys.clear-focus();
|
|
root.closed();
|
|
}
|
|
|
|
property <length> anchor-x;
|
|
property <length> anchor-y;
|
|
// Highlighted rows: keyboard or pointer, -1 for none.
|
|
property <int> current: -1;
|
|
property <int> sub-current: -1;
|
|
// The item whose submenu shows; kept while the submenu plays its exit.
|
|
property <int> sub-index: -1;
|
|
property <bool> sub-open;
|
|
// Arrow keys are in the submenu.
|
|
property <bool> in-sub;
|
|
// Top of the row the submenu belongs to (that row reports it).
|
|
property <length> sub-row-y;
|
|
property <[MenuEntry]> sub-items: root.sub-index >= 0 && root.sub-index < root.items.length ? root.items[root.sub-index].submenu : [];
|
|
property <float> sub-progress: root.open && root.sub-open ? 1 : 0;
|
|
animate sub-progress {
|
|
duration: Motion.time(root.sub-open ? Motion.short4 : Motion.short3);
|
|
easing: root.sub-open ? Motion.emphasized-decelerate : Motion.standard;
|
|
}
|
|
|
|
// Arrow keys skip separators and disabled rows: a step lands, and `changed` steps again
|
|
// in the same direction until a selectable row (or a full lap) is reached.
|
|
property <int> step: 1;
|
|
property <int> hops;
|
|
|
|
pure function selectable(separator: bool, disabled: bool) -> bool {
|
|
!separator && !disabled
|
|
}
|
|
|
|
pure function wrap(from: int, dir: int, count: int) -> int {
|
|
if count == 0 {
|
|
return -1;
|
|
}
|
|
if from < 0 {
|
|
return dir > 0 ? 0 : count - 1;
|
|
}
|
|
Math.mod(from + dir + count, count)
|
|
}
|
|
|
|
function move(dir: int) {
|
|
root.step = dir;
|
|
root.hops = 0;
|
|
if root.in-sub {
|
|
root.sub-current = root.wrap(root.sub-current, dir, root.sub-items.length);
|
|
} else {
|
|
root.current = root.wrap(root.current, dir, root.items.length);
|
|
}
|
|
}
|
|
|
|
changed current => {
|
|
if root.current >= 0 && root.hops < root.items.length && !root.selectable(root.items[root.current].separator, root.items[root.current].disabled) {
|
|
root.hops += 1;
|
|
root.current = root.wrap(root.current, root.step, root.items.length);
|
|
}
|
|
}
|
|
|
|
changed sub-current => {
|
|
if root.sub-current >= 0 && root.hops < root.sub-items.length && !root.selectable(root.sub-items[root.sub-current].separator, root.sub-items[root.sub-current].disabled) {
|
|
root.hops += 1;
|
|
root.sub-current = root.wrap(root.sub-current, root.step, root.sub-items.length);
|
|
}
|
|
}
|
|
|
|
function open-sub(index: int) {
|
|
if root.sub-open && root.sub-index == index {
|
|
return;
|
|
}
|
|
root.sub-index = index;
|
|
root.sub-current = -1;
|
|
root.in-sub = false;
|
|
root.sub-open = true;
|
|
}
|
|
|
|
function close-sub() {
|
|
root.sub-open = false;
|
|
root.in-sub = false;
|
|
root.sub-current = -1;
|
|
}
|
|
|
|
// Pointer over a row (or the keyboard landing on it): submenus open on hover.
|
|
function hover(index: int) {
|
|
root.current = index;
|
|
if root.items[index].submenu.length > 0 {
|
|
root.open-sub(index);
|
|
} else {
|
|
root.close-sub();
|
|
}
|
|
}
|
|
|
|
function choose(index: int, keyboard: bool) {
|
|
if index < 0 || !root.selectable(root.items[index].separator, root.items[index].disabled) {
|
|
return;
|
|
}
|
|
if root.items[index].submenu.length > 0 {
|
|
root.open-sub(index);
|
|
if keyboard {
|
|
root.in-sub = true;
|
|
root.move(1);
|
|
}
|
|
return;
|
|
}
|
|
root.activated(index, -1);
|
|
root.close();
|
|
}
|
|
|
|
function choose-sub(index: int) {
|
|
if index < 0 || !root.selectable(root.sub-items[index].separator, root.sub-items[index].disabled) {
|
|
return;
|
|
}
|
|
root.activated(root.sub-index, index);
|
|
root.close();
|
|
}
|
|
|
|
dismissed => {
|
|
root.close();
|
|
}
|
|
|
|
keys := FocusScope {
|
|
x: 0;
|
|
width: 0;
|
|
focus-on-click: false;
|
|
focus-on-tab-navigation: false;
|
|
|
|
// Everything else is swallowed while the menu is open, Tab included.
|
|
key-pressed(event) => {
|
|
if event.text == Key.DownArrow {
|
|
root.move(1);
|
|
} else if event.text == Key.UpArrow {
|
|
root.move(-1);
|
|
} else if event.text == Key.RightArrow {
|
|
if !root.in-sub && root.current >= 0 && root.items[root.current].submenu.length > 0 {
|
|
root.choose(root.current, true);
|
|
}
|
|
} else if event.text == Key.LeftArrow {
|
|
root.close-sub();
|
|
} else if event.text == Key.Escape {
|
|
if root.sub-open {
|
|
root.close-sub();
|
|
} else {
|
|
root.close();
|
|
}
|
|
} else if event.text == Key.Return || event.text == Key.Space {
|
|
if root.in-sub {
|
|
root.choose-sub(root.sub-current);
|
|
} else {
|
|
root.choose(root.current, true);
|
|
}
|
|
}
|
|
accept
|
|
}
|
|
}
|
|
|
|
surface := MenuSurface {
|
|
property <length> w: clamp(column.preferred-width, 180px, 280px);
|
|
|
|
width: self.w;
|
|
height: column.preferred-height;
|
|
x: root.place(root.anchor-x, root.anchor-x, self.width, root.width);
|
|
y: root.place(root.anchor-y, root.anchor-y, self.height, root.height);
|
|
origin: {
|
|
x: clamp(root.anchor-x - self.x, 0, self.width),
|
|
y: clamp(root.anchor-y - self.y, 0, self.height),
|
|
};
|
|
progress: root.progress;
|
|
|
|
column := VerticalLayout {
|
|
padding: Theme.space-2;
|
|
|
|
for item[i] in root.items: MenuRow {
|
|
entry: { text: item.text, icon: item.icon, shortcut: item.shortcut, disabled: item.disabled, separator: item.separator };
|
|
leading-icon: root.leading-icons;
|
|
has-submenu: item.submenu.length > 0;
|
|
highlighted: i == root.current || (root.sub-open && i == root.sub-index);
|
|
property <bool> owns-sub: i == root.sub-index;
|
|
|
|
changed hovered => {
|
|
if self.hovered && i != root.current {
|
|
root.hover(i);
|
|
}
|
|
}
|
|
changed owns-sub => {
|
|
if self.owns-sub {
|
|
root.sub-row-y = self.y;
|
|
}
|
|
}
|
|
clicked => {
|
|
root.choose(i, false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
MenuSurface {
|
|
// The parent row's top, so the first submenu row lines up with it.
|
|
property <length> row-y: surface.y + root.sub-row-y - Theme.space-2;
|
|
|
|
width: clamp(sub-column.preferred-width, 180px, 280px);
|
|
height: sub-column.preferred-height;
|
|
x: root.place(surface.x + surface.width - Theme.space-1, surface.x + Theme.space-1, self.width, root.width);
|
|
y: root.place(self.row-y, root.height, self.height, root.height);
|
|
origin: { x: surface.x + surface.width - self.x, y: self.row-y - self.y };
|
|
progress: root.sub-progress;
|
|
|
|
sub-column := VerticalLayout {
|
|
padding: Theme.space-2;
|
|
|
|
for entry[j] in root.sub-items: MenuRow {
|
|
entry: entry;
|
|
leading-icon: root.leading-icons;
|
|
highlighted: j == root.sub-current;
|
|
changed hovered => {
|
|
if self.hovered && j != root.sub-current {
|
|
root.in-sub = true;
|
|
root.sub-current = j;
|
|
}
|
|
}
|
|
clicked => {
|
|
root.choose-sub(j);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|