S005: overlay layer, menu with submenu, modal dialog, toast stack

Menus, dialogs and toasts draw on an own full-window layer so they can
play exit motion; gallery pages show them open for screenshots.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-18 10:06:17 -06:00
commit c94b868b2e
12 changed files with 1071 additions and 40 deletions

View file

@ -5,13 +5,14 @@ use std::process::ExitCode;
slint::include_modules!();
const USAGE: &str = "usage: tunal [--gallery] [--screenshot <png> [--dark] [--size WxH]]";
const USAGE: &str =
"usage: tunal [--gallery [--page menu|dialog]] [--screenshot <png> [--dark] [--size WxH]]";
fn main() -> ExitCode {
let result = parse(std::env::args().skip(1)).and_then(|args| match args.screenshot {
None if args.gallery => Gallery::new().and_then(run).map_err(|e| e.to_string()),
None if args.gallery => gallery(args.page).and_then(run).map_err(|e| e.to_string()),
None => MainWindow::new().and_then(run).map_err(|e| e.to_string()),
Some(options) if args.gallery => screenshot::run(options, Gallery::new),
Some(options) if args.gallery => screenshot::run(options, || gallery(args.page)),
Some(options) => screenshot::run(options, MainWindow::new),
});
match result {
@ -23,6 +24,13 @@ fn main() -> ExitCode {
}
}
fn gallery(page: GalleryPage) -> Result<Gallery, slint::PlatformError> {
let ui = Gallery::new()?;
ui.set_page(page);
ui.invoke_stage();
Ok(ui)
}
fn run(ui: impl theme_bridge::Themed) -> Result<(), slint::PlatformError> {
theme_bridge::start(&ui, theme_bridge::Overrides::from_env());
ui.run()
@ -30,11 +38,13 @@ fn run(ui: impl theme_bridge::Themed) -> Result<(), slint::PlatformError> {
struct Args {
gallery: bool,
page: GalleryPage,
screenshot: Option<screenshot::Options>,
}
fn parse(mut args: impl Iterator<Item = String>) -> Result<Args, String> {
let mut gallery = false;
let mut page = GalleryPage::Controls;
let mut options = screenshot::Options {
path: String::new(),
dark: false,
@ -43,23 +53,40 @@ fn parse(mut args: impl Iterator<Item = String>) -> Result<Args, String> {
};
let mut screenshot_flags = false;
while let Some(arg) = args.next() {
screenshot_flags |= arg != "--gallery";
match arg.as_str() {
"--gallery" => gallery = true,
"--screenshot" => options.path = args.next().ok_or(USAGE)?,
"--dark" => options.dark = true,
"--page" => {
page = match args.next().as_deref() {
Some("menu") => GalleryPage::Menu,
Some("dialog") => GalleryPage::Dialog,
_ => return Err(USAGE.into()),
}
}
"--screenshot" => {
options.path = args.next().ok_or(USAGE)?;
screenshot_flags = true;
}
"--dark" => {
options.dark = true;
screenshot_flags = true;
}
"--size" => {
let size = args.next().ok_or(USAGE)?;
let (w, h) = size.split_once('x').ok_or(USAGE)?;
let parse = |n: &str| n.parse::<u32>().ok().filter(|&n| n > 0).ok_or(USAGE);
options.size = (parse(w)?, parse(h)?);
screenshot_flags = true;
}
_ => return Err(USAGE.into()),
}
}
if page != GalleryPage::Controls && !gallery {
return Err(USAGE.into());
}
if !screenshot_flags {
return Ok(Args {
gallery,
page,
screenshot: None,
});
}
@ -69,6 +96,7 @@ fn parse(mut args: impl Iterator<Item = String>) -> Result<Args, String> {
options.reduced_motion = gallery;
Ok(Args {
gallery,
page,
screenshot: Some(options),
})
}

View file

@ -4,7 +4,7 @@ import { Motion, MotionPrefs } from "motion.slint";
import { Icon, Icons } from "components/icon.slint";
export { Theme, MotionPrefs }
export { Gallery } from "gallery.slint";
export { Gallery, GalleryPage } from "gallery.slint";
// Placeholder shell (sidebar + content) until the views land.
export component MainWindow inherits Window {

View file

@ -0,0 +1,188 @@
import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Button, ButtonKind } from "button.slint";
import { Overlay } from "overlay.slint";
// One button of a dialog's action row.
export struct DialogAction {
text: string,
kind: ButtonKind,
}
// A modal dialog: the scrim fades in, the surface scales in from the centre. Title, one or
// two lines of body, the children (a field, a list) and the action row, main action last.
// Place it last in the window (`width: 100%; height: 100%;`), call `show()`, and close it
// with `close()` from `action`: the dialog never closes itself, so an owner can keep it open
// while the input is invalid.
//
// Enter runs `default-action` (a focused button runs itself), Escape and a scrim click run
// `cancel-action`. Tab stays inside: the children may hold one focus stop of their own; set
// `content-focus` and focus it in `focus-content`.
export component Dialog inherits Overlay {
in property <string> title;
in property <string> body;
// Left to right; the main action last.
in property <[DialogAction]> actions;
in property <int> default-action: root.actions.length - 1;
// -1: Escape and the scrim do nothing.
in property <int> cancel-action: 0;
// Fixed per kind of dialog so it doesn't resize while typing.
in property <length> dialog-width: 400px;
in property <bool> content-focus;
callback action(index: int);
callback focus-content;
public function show() {
root.open = true;
if root.content-focus {
root.focus-content();
} else {
root.focus-action(root.default-action, false);
}
}
public function close() {
root.open = false;
trap.clear-focus();
}
modal: true;
enter-duration: Motion.medium2;
exit-duration: Motion.short4;
dismissed => {
root.cancel();
}
// The action button that has focus, -1 when focus is in the children (or nowhere).
property <int> focused-action: -1;
// Each request bumps the serial; the button at `focus-target` takes the focus.
property <int> focus-target: -1;
property <bool> focus-ring;
property <int> focus-serial;
function focus-action(index: int, ring: bool) {
root.focus-target = index;
root.focus-ring = ring;
root.focus-serial += 1;
}
function cancel() {
if root.open && root.cancel-action >= 0 {
root.action(root.cancel-action);
}
}
// Wraps Tab at the ends of the dialog's focus stops instead of leaving to the window.
function wrap-focus(forward: bool) -> bool {
let last = root.actions.length - 1;
if forward && root.focused-action == last {
if root.content-focus {
root.focus-content();
} else {
root.focus-action(0, true);
}
return true;
}
if !forward && (root.focused-action == 0 || (root.focused-action == -1 && root.content-focus)) {
if root.focused-action == 0 && root.content-focus {
root.focus-content();
} else {
root.focus-action(last, true);
}
return true;
}
false
}
surface := Rectangle {
width: min(root.dialog-width, root.width - 2 * Theme.window-margin);
height: column.preferred-height;
x: (root.width - self.width) / 2;
y: max(Theme.window-margin, (root.height - self.height) / 2);
background: Theme.surface-container-high;
border-radius: Theme.corner-xl;
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-scale: Motion.enter-scale + (1 - Motion.enter-scale) * root.progress;
accessible-role: groupbox;
accessible-label: root.title;
accessible-description: root.body;
// Clicks on the surface stay on the dialog instead of cancelling it.
TouchArea { }
trap := FocusScope {
focus-on-click: false;
focus-on-tab-navigation: false;
// Runs before the focused child sees the key.
capture-key-pressed(event) => {
if event.text == Key.Escape {
root.cancel();
return accept;
}
if event.text == Key.Tab || event.text == Key.Backtab {
return root.wrap-focus(event.text == Key.Tab && !event.modifiers.shift) ? accept : reject;
}
if event.text == Key.Return && root.focused-action == -1 && root.default-action >= 0 {
root.action(root.default-action);
return accept;
}
reject
}
column := VerticalLayout {
padding: Theme.space-6;
spacing: Theme.space-4;
Text {
text: root.title;
color: Theme.on-surface;
font-size: Theme.title-large.size;
font-weight: Theme.title-large.weight;
wrap: word-wrap;
}
if root.body != "": Text {
text: root.body;
color: Theme.on-surface-variant;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
wrap: word-wrap;
}
@children
HorizontalLayout {
alignment: end;
spacing: Theme.space-2;
for item[i] in root.actions: Button {
property <int> serial: root.focus-serial;
text: item.text;
kind: item.kind;
changed serial => {
if i == root.focus-target {
self.grab-focus(root.focus-ring);
}
}
changed has-focus => {
if self.has-focus {
root.focused-action = i;
} else if root.focused-action == i {
root.focused-action = -1;
}
}
clicked => {
root.action(i);
}
}
}
}
}
}
}

View file

@ -32,4 +32,13 @@ export global Icons {
out property <string> grid-view: "\u{e9b0}";
out property <string> music-note: "\u{e405}";
out property <string> description: "\u{e873}";
out property <string> chevron-right: "\u{e5cc}";
out property <string> content-copy: "\u{e14d}";
out property <string> content-cut: "\u{e14e}";
out property <string> content-paste: "\u{e14f}";
out property <string> edit: "\u{e3c9}";
out property <string> delete: "\u{e872}";
out property <string> open-in-new: "\u{e89e}";
out property <string> sort: "\u{e164}";
out property <string> info: "\u{e88e}";
}

View file

@ -0,0 +1,386 @@
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 : 32px;
border-radius: Theme.corner-xs;
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-sm;
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, 112px, 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-1;
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-1;
width: clamp(sub-column.preferred-width, 112px, 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-1;
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);
}
}
}
}
}

View file

@ -0,0 +1,55 @@
import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
// The layer a menu or dialog draws on. Slint's PopupWindow closes at once, so floating
// surfaces live here instead and stay until their exit motion has played. Put the layer
// last in the window with `width: 100%; height: 100%;` so it covers everything and its
// origin is the window's; open it with window coordinates (`absolute-position`).
//
// While open it takes every click and scroll: a click outside the surface calls `dismissed`.
export component Overlay {
// Whether the surface shows. Closing mid-entrance reverses from where it is.
in-out property <bool> open;
// Dims the window below while open.
in property <bool> modal;
in property <duration> enter-duration: Motion.short4;
in property <duration> exit-duration: Motion.short3;
// 0 closed, 1 open. Surfaces derive their opacity and scale from it.
out property <float> progress: root.open ? 1 : 0;
animate progress {
duration: Motion.time(root.open ? root.enter-duration : root.exit-duration);
easing: root.open ? Motion.emphasized-decelerate : Motion.standard;
}
// A click or Escape outside the surface; the user closes the overlay in response.
callback dismissed;
// Keeps a surface of `size` starting at `pos` inside `extent`, flipping it to end at
// `flip` when it doesn't fit (a menu opens above or left of its anchor).
public pure function place(pos: length, flip: length, size: length, extent: length) -> length {
if pos + size <= extent - Theme.window-margin {
return pos;
}
clamp(flip - size, Theme.window-margin, max(Theme.window-margin, extent - Theme.window-margin - size))
}
// Hidden (and out of the focus chain) once the exit has finished: nothing is left drawing.
visible: root.progress > 0;
Rectangle {
background: root.modal ? Theme.scrim.with-alpha(Theme.scrim-opacity * root.progress) : transparent;
}
TouchArea {
enabled: root.open;
clicked => {
root.dismissed();
}
scroll-event(event) => {
accept
}
}
@children
}

View file

@ -1,31 +1,10 @@
import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Tooltip } from "tooltip.slint";
// A state shown regardless of the pointer and focus, for gallery screenshots.
export enum ForceState { none, hover, pressed, focused }
// The small label an icon-only control shows on hover.
export component Tooltip inherits Rectangle {
in property <string> text;
background: Theme.inverse-surface;
border-radius: Theme.corner-xs;
height: 24px;
HorizontalLayout {
padding-left: Theme.space-2;
padding-right: Theme.space-2;
Text {
text: root.text;
color: Theme.inverse-on-surface;
font-size: Theme.body-small.size;
font-weight: Theme.body-small.weight;
vertical-alignment: center;
}
}
}
// Shared by every control: hover and press tint, press squeeze, keyboard focus ring and
// tooltip. A control inherits it, sets its shape (`border-radius`) and container colours, and
// puts its content in as children. Space and Enter activate the control when it has focus.
@ -73,7 +52,15 @@ export component StateLayer inherits Rectangle {
root.clicked();
}
FocusScope {
// Moves keyboard focus here from code (a dialog focusing its default button); `ring`
// shows the focus ring as Tab would.
out property <bool> has-focus: keys.has-focus;
public function grab-focus(ring: bool) {
keys.focus();
root.keyboard-focus = ring;
}
keys := FocusScope {
x: 0;
width: 0;
enabled: root.enabled;

View file

@ -0,0 +1,231 @@
import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icon, Icons } from "icon.slint";
import { StateLayer } from "state-layer.slint";
export enum ToastProgress { none, determinate, indeterminate }
export struct ToastData {
message: string,
// Button label, e.g. "Undo"; empty for none.
action: string,
// Shows a cancel button (a running file operation).
cancellable: bool,
progress-kind: ToastProgress,
// 0 to 1 for `determinate`.
progress: float,
// Set by the owner to end the toast (the operation finished); it then plays its exit.
closing: bool,
}
// A button on the inverse surface of a toast: label (action) or icon (cancel).
component ToastButton inherits StateLayer {
in property <string> text;
in property <string> icon;
property <color> content: root.icon != "" ? Theme.inverse-on-surface : Theme.inverse-primary;
height: 32px;
width: root.icon != "" ? self.height : label.preferred-width + 2 * Theme.space-3;
border-radius: Theme.corner-full(self.height);
tint: root.content;
squeeze: root.icon != "" ? Motion.squeeze-small : Motion.squeeze;
accessible-role: button;
accessible-label: root.icon != "" ? root.tooltip : root.text;
accessible-action-default => {
self.activate();
}
if root.icon != "": Icon {
x: (root.width - self.width) / 2;
y: (root.height - self.height) / 2;
glyph: root.icon;
size: 20px;
color: root.content;
}
label := Text {
visible: root.icon == "";
x: Theme.space-3;
text: root.text;
color: root.content;
font-size: Theme.label-large.size;
font-weight: Theme.label-large.weight;
vertical-alignment: center;
}
}
component Toast inherits Rectangle {
in property <ToastData> data;
// 0 is the bottom of the stack.
in property <int> slot;
callback action;
callback cancelled;
callback removed;
// Set once created so the entrance animates; set again when the toast leaves by itself.
property <bool> entered;
property <bool> dismissed;
property <bool> leaving: root.data.closing || root.dismissed;
property <float> progress: root.entered && !root.leaving ? 1 : 0;
animate progress {
duration: Motion.time(root.leaving ? Motion.short4 : Motion.medium1);
easing: root.leaving ? Motion.standard : Motion.emphasized-decelerate;
}
// Rises or falls as toasts below it come and go.
property <length> stack-offset: root.slot * (root.height + Theme.space-2);
animate stack-offset {
duration: Motion.time(Motion.medium1);
easing: Motion.emphasized;
}
// Where the parent wants the bottom toast's top edge.
in property <length> base-y;
init => {
self.entered = true;
}
height: 48px;
y: root.base-y - root.stack-offset + (1 - root.progress) * Theme.space-6;
opacity: root.progress;
background: Theme.inverse-surface;
border-radius: Theme.corner-xs;
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);
accessible-role: text;
accessible-label: root.data.message;
// Hover keeps the toast up while the pointer is on it.
touch := TouchArea { }
// A message without progress or action goes after a while; it waits while hovered.
Timer {
interval: Motion.toast-timeout;
running: root.entered && !root.leaving && !touch.has-hover && root.data.progress-kind == ToastProgress.none && root.data.action == "";
triggered => {
root.dismissed = true;
}
}
// Runs only during the exit: then the owner removes the toast from its model.
property <bool> gone;
Timer {
interval: Motion.short4;
running: root.leaving && !root.gone;
triggered => {
root.gone = true;
root.removed();
}
}
HorizontalLayout {
padding-left: Theme.space-4;
padding-right: Theme.space-2;
padding-bottom: root.data.progress-kind == ToastProgress.none ? 0 : Theme.space-1;
spacing: Theme.space-1;
Text {
horizontal-stretch: 1;
text: root.data.message;
color: Theme.inverse-on-surface;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
vertical-alignment: center;
overflow: elide;
}
if root.data.action != "": VerticalLayout {
alignment: center;
ToastButton {
text: root.data.action;
enabled: !root.leaving;
clicked => {
root.dismissed = true;
root.action();
}
}
}
if root.data.cancellable: VerticalLayout {
alignment: center;
ToastButton {
icon: Icons.close;
tooltip: "Cancel";
enabled: !root.leaving;
clicked => {
root.dismissed = true;
root.cancelled();
}
}
}
}
if root.data.progress-kind != ToastProgress.none: track := Rectangle {
x: Theme.space-4;
y: root.height - Theme.space-2;
width: root.width - 2 * Theme.space-4;
height: Theme.space-1;
border-radius: Theme.corner-full(self.height);
background: Theme.inverse-on-surface.with-alpha(Theme.disabled-container);
clip: true;
if root.data.progress-kind == ToastProgress.determinate: Rectangle {
x: 0;
width: parent.width * clamp(root.data.progress, 0, 1);
border-radius: parent.border-radius;
background: Theme.inverse-primary;
animate width {
duration: Motion.time(Motion.short4);
easing: Motion.standard;
}
}
// A segment sweeps while the toast shows (the work is moving, so the bar does too).
// Reduced motion: a still, dimmed full bar instead.
if root.data.progress-kind == ToastProgress.indeterminate && Motion.reduced: Rectangle {
background: Theme.inverse-primary.with-alpha(Theme.disabled-content);
}
if root.data.progress-kind == ToastProgress.indeterminate && !Motion.reduced && !root.leaving: Rectangle {
property <float> phase: Math.mod(animation-tick() / Motion.indeterminate-period, 1);
width: parent.width * 0.4;
x: -self.width + self.phase * (parent.width + self.width);
border-radius: parent.border-radius;
background: Theme.inverse-primary;
}
}
}
// The toasts of a window, newest at the bottom, centred above the bottom edge. Place it
// last in the window (`width: 100%; height: 100%;`); it takes input only on the toasts.
// The owner appends to `toasts`, sets `closing` to end one, and removes the entry when
// `removed(index)` fires (after its exit). Action and cancel also end the toast.
export component ToastStack {
in property <[ToastData]> toasts;
in property <length> toast-width: 400px;
callback action(index: int);
callback cancelled(index: int);
callback removed(index: int);
for toast[i] in root.toasts: Toast {
data: toast;
slot: root.toasts.length - 1 - i;
width: min(root.toast-width, root.width - 2 * Theme.window-margin);
x: (root.width - self.width) / 2;
base-y: root.height - Theme.space-6 - self.height;
action => {
root.action(i);
}
cancelled => {
root.cancelled(i);
}
removed => {
root.removed(i);
}
}
}

View file

@ -0,0 +1,35 @@
import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
// The small label an icon-only control shows after hovering for `Motion.tooltip-delay`
// (StateLayer's `tooltip`). It fades in; it leaves at once with its popup.
export component Tooltip inherits Rectangle {
in property <string> text;
property <bool> shown;
init => {
self.shown = true;
}
background: Theme.inverse-surface;
border-radius: Theme.corner-xs;
height: 24px;
opacity: root.shown ? 1 : 0;
animate opacity {
duration: Motion.time(Motion.short3);
easing: Motion.standard;
}
HorizontalLayout {
padding-left: Theme.space-2;
padding-right: Theme.space-2;
Text {
text: root.text;
color: Theme.inverse-on-surface;
font-size: Theme.body-small.size;
font-weight: Theme.body-small.weight;
vertical-alignment: center;
}
}
}

View file

@ -1,6 +1,7 @@
import { Theme } from "theme.slint";
import { Icon, Icons } from "components/icon.slint";
import { ForceState, Tooltip } from "components/state-layer.slint";
import { ForceState } from "components/state-layer.slint";
import { Tooltip } from "components/tooltip.slint";
import { Button, ButtonKind } from "components/button.slint";
import { IconButton } from "components/icon-button.slint";
import { Chip, ChipKind } from "components/chip.slint";
@ -8,6 +9,12 @@ import { Switch } from "components/switch.slint";
import { ListRow } from "components/list-row.slint";
import { Tab, TabBar } from "components/tab.slint";
import { TextField } from "components/text-field.slint";
import { Menu } from "components/menu.slint";
import { Dialog } from "components/dialog.slint";
import { ToastStack, ToastProgress } from "components/toast.slint";
// What the gallery shows: every control, or a stand-in folder under open overlays.
export enum GalleryPage { controls, menu, dialog }
// Every control in every state, for screenshots: `tunal --gallery --screenshot <png>`.
// Columns force the state; the last one is the disabled control.
@ -57,8 +64,19 @@ export component Gallery inherits Window {
property <[ForceState]> states: [ForceState.none, ForceState.hover, ForceState.pressed, ForceState.focused, ForceState.none];
property <int> disabled-column: 4;
in property <GalleryPage> page;
VerticalLayout {
// Opens the page's overlays; Rust calls it before taking the screenshot.
public function stage() {
if root.page == GalleryPage.menu {
menu.open-at(200px, 120px);
menu.expand(7);
} else if root.page == GalleryPage.dialog {
dialog.show();
}
}
if root.page == GalleryPage.controls: VerticalLayout {
padding: Theme.space-6;
spacing: Theme.space-3;
alignment: start;
@ -323,4 +341,71 @@ export component Gallery inherits Window {
}
}
}
if root.page != GalleryPage.controls: VerticalLayout {
padding: Theme.space-6;
spacing: Theme.space-1;
alignment: start;
for name[i] in ["Documents", "Downloads", "Music", "Pictures", "Videos", "notes.txt", "Quarterly report.pdf"]: ListRow {
width: 360px;
icon: i < 5 ? Icons.folder : Icons.description;
title: name;
selected: i == 2;
}
}
ToastStack {
width: 100%;
height: 100%;
toasts: root.page == GalleryPage.menu ? [
{ message: "Copying 12 files to Music", cancellable: true, progress-kind: ToastProgress.determinate, progress: 0.62 },
{ message: "Moved 3 items to the trash", action: "Undo" },
] : root.page == GalleryPage.dialog ? [
{ message: "Counting files in Videos", cancellable: true, progress-kind: ToastProgress.indeterminate },
] : [];
}
menu := Menu {
width: 100%;
height: 100%;
leading-icons: true;
items: [
{ text: "Open", icon: Icons.open-in-new, shortcut: "Enter" },
{ separator: true },
{ text: "Cut", icon: Icons.content-cut, shortcut: "Ctrl+X" },
{ text: "Copy", icon: Icons.content-copy, shortcut: "Ctrl+C" },
{ text: "Paste", icon: Icons.content-paste, shortcut: "Ctrl+V", disabled: true },
{ separator: true },
{ text: "Rename", icon: Icons.edit, shortcut: "F2" },
{
text: "Sort by",
icon: Icons.sort,
submenu: [{ text: "Name" }, { text: "Size" }, { text: "Modified" }, { separator: true }, { text: "Type", disabled: true }],
},
{ separator: true },
{ text: "Move to trash", icon: Icons.delete, shortcut: "Delete" },
];
}
dialog := Dialog {
width: 100%;
height: 100%;
title: "Rename folder";
body: "Other apps that point at this folder lose track of it.";
content-focus: true;
actions: [{ text: "Cancel", kind: ButtonKind.text }, { text: "Rename", kind: ButtonKind.filled }];
focus-content => {
name-field.focus();
}
action => {
self.close();
}
name-field := TextField {
label: "Name";
text: "Music";
}
}
}

View file

@ -36,6 +36,14 @@ export global Motion {
out property <float> squeeze: 0.96;
out property <float> squeeze-large: 0.985;
// Scale a floating surface starts from (and leaves towards), never 0.
out property <float> enter-scale: 0.92;
// How long a toast without progress or action stays. A delay, not motion.
out property <duration> toast-timeout: 4s;
// One sweep of an indeterminate progress bar.
out property <duration> indeterminate-period: 1500ms;
// Hover time before a tooltip shows. A delay, not motion: reduced motion keeps it.
out property <duration> tooltip-delay: 500ms;

View file

@ -68,6 +68,16 @@ export global Theme {
// Hairline borders.
out property <length> hairline: 1px;
// Floating surfaces (menus, dialogs, toasts, tooltips): one soft shadow, mixed from
// `shadow`. Resting surfaces have none.
out property <length> float-shadow-blur: 12px;
out property <length> float-shadow-offset: 4px;
out property <float> float-shadow-opacity: 0.18;
// Dims the window behind a modal dialog, mixed from `scrim`.
out property <float> scrim-opacity: 0.32;
// Gap kept between a floating surface and the window edge.
out property <length> window-margin: 8px;
// Keyboard focus ring, drawn outside the control's shape.
out property <length> focus-ring-width: 2px;
out property <length> focus-ring-offset: 2px;