Files
VoelinClient/crates/voelin-ui/ui/components/button.slint
T
Claude 477040c0ec ui: tooltips on icon buttons, and a count
IconButton names itself in a tooltip after a moment of hover, also while
it is disabled (mute or deafen when not connected), and can show a
neutral count at its top right (for pins and topics later). Tooltips are
desktop only: the new Bridge.desktop is false in the Android app.

Tooltip showed once (its Timer turned off its own binding) and stayed
open after the pointer left. Both now use Slint's Tooltip element with
our TooltipBubble: it opens at the pointer, closes when the pointer
leaves, and lets clicks and hover through, which a PopupWindow of our own
does not while it is open. A press closes it until the pointer leaves.
The bubble sets its own preferred width, up to 400px, so long text wraps:
the tooltip's popup takes that width, and a max-width would not narrow it.

IconButton's look moves onto an inner rectangle: with an opacity and an
animation on its root, Slint inlined it at each of its 100-odd uses, and
the tooltip and count alone added 3.3 MB of generated code to a crate
whose compile already peaks near 14 GB. Now the generated code is 1.1 MB
smaller than before (70.7 MB).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AVRENKftQqGoEeswiR9TDQ
2026-10-06 04:57:51 +00:00

260 lines
8.9 KiB
Plaintext

import { Theme } from "../theme.slint";
import { Icon } from "icon.slint";
import { CountBadge } from "badge.slint";
import { HoverTooltip } from "tooltip.slint";
export enum ButtonKind {
// Filled accent: the main action.
primary,
// Surface with a border.
secondary,
// Destructive: leave, end stream, delete.
danger,
// No background until hovered.
ghost,
}
// The focus ring shared by controls.
export component FocusRing inherits Rectangle {
in property <bool> shown;
in property <length> radius: Theme.radius-md;
// The size of the ringed element.
in property <length> w;
in property <length> h;
x: -Theme.glow-width;
y: -Theme.glow-width;
width: w + 2 * Theme.glow-width;
height: h + 2 * Theme.glow-width;
border-radius: radius + Theme.glow-width;
border-width: shown ? Theme.glow-width : 0px;
border-color: Theme.glow;
}
// A button with a label, an icon or both.
export component Button inherits Rectangle {
in property <string> text;
in property <image> icon;
in property <ButtonKind> kind: ButtonKind.secondary;
in property <bool> enabled: true;
// Toggled on: drawn as selected (e.g. an open panel).
in property <bool> checked;
// Smaller height and padding.
in property <bool> small;
// Stretch the label to the full width.
in property <bool> centered: true;
callback clicked();
property <bool> active: touch.pressed || checked;
property <color> fill: kind == ButtonKind.primary ? (active ? Theme.accent-pressed : touch.has-hover ? Theme.accent-hover : Theme.accent)
: kind == ButtonKind.danger ? (active ? Theme.danger : touch.has-hover ? Theme.danger-soft.brighter(0.3) : Theme.danger-soft)
: kind == ButtonKind.ghost ? (active ? Theme.surface-4 : touch.has-hover ? Theme.surface-3 : transparent)
: checked ? (touch.has-hover ? Theme.accent-soft-hover : Theme.accent-soft)
: (active ? Theme.surface-4 : touch.has-hover ? Theme.surface-3 : Theme.surface-2);
property <color> ink: kind == ButtonKind.primary ? Theme.on-accent
: !enabled ? Theme.text-disabled
: kind == ButtonKind.danger ? (active ? Theme.on-accent : Theme.danger)
: Theme.text;
min-width: self.min-height;
min-height: small ? Theme.control-height-sm : Theme.control-height;
max-height: self.min-height;
horizontal-stretch: 0;
vertical-stretch: 0;
border-radius: Theme.radius-md;
background: fill;
border-width: kind == ButtonKind.secondary || kind == ButtonKind.danger || checked ? 1px : 0px;
border-color: checked ? Theme.accent : kind == ButtonKind.danger ? Theme.danger.transparentize(55%) : Theme.border-strong;
opacity: enabled ? 1.0 : 0.5;
animate background { duration: Theme.fast; }
accessible-role: AccessibleRole.button;
accessible-label: text;
accessible-enabled: enabled;
accessible-checkable: checked;
accessible-checked: checked;
accessible-action-default => { if enabled { clicked(); } }
FocusRing {
shown: focus.has-focus;
w: root.width;
h: root.height;
}
focus := FocusScope {
enabled: root.enabled;
key-pressed(event) => {
if event.text == " " || event.text == "\n" {
root.clicked();
return accept;
}
reject
}
touch := TouchArea {
enabled: root.enabled;
mouse-cursor: MouseCursor.pointer;
clicked => {
focus.focus();
root.clicked();
}
}
}
HorizontalLayout {
padding-left: text == "" ? 0px : small ? Theme.space-4 : Theme.space-5;
padding-right: self.padding-left;
spacing: Theme.space-3;
alignment: centered ? center : start;
if icon.width > 0: Icon {
source: icon;
size: small ? Theme.icon-sm : Theme.icon-md;
tint: ink;
y: (parent.height - self.height) / 2;
}
if text != "": Text {
text: root.text;
color: ink;
font-size: small ? Theme.font-sm : Theme.font-body;
font-weight: Theme.weight-medium;
vertical-alignment: center;
overflow: elide;
}
}
}
// A square (or round) button with only an icon. `label` names it for
// screen readers and, on the desktop, in a tooltip.
//
// The look is on `face`, not on the root: an opacity or an animation on a
// component's root makes Slint inline the component where it is used, and
// there are over a hundred IconButtons. Shared, their code stays small (and
// so does the compiler's memory).
export component IconButton inherits Rectangle {
in property <image> icon;
in property <string> label;
// Shown when the pointer rests on the button (desktop only, also while
// disabled); "" for none.
in property <string> tooltip: label;
in property <bool> enabled: true;
in property <bool> checked;
in property <bool> round;
// Drawn on a surface instead of transparent.
in property <bool> filled;
// Red while checked (muted microphone).
in property <bool> danger;
in property <length> size: Theme.control-height;
in property <length> icon-size: Theme.icon-md;
in property <brush> tint: checked ? (danger ? Theme.danger : Theme.accent-text) : Theme.icon;
// A small dot at the top right (unread, live).
in property <bool> dot;
in property <color> dot-color: Theme.live;
// A neutral count at the top right (pins, topics); hidden at 0 or less.
in property <int> count;
callback clicked();
width: size;
height: size;
accessible-role: AccessibleRole.button;
accessible-label: label;
accessible-enabled: enabled;
accessible-action-default => { if enabled { clicked(); } }
face := Rectangle {
border-radius: root.round ? root.size / 2 : Theme.radius-md;
background: touch.pressed ? Theme.surface-4
: root.checked ? (root.danger ? Theme.danger-soft : Theme.accent-soft)
: touch.has-hover ? Theme.surface-3
: root.filled ? Theme.surface-2 : transparent;
border-width: root.filled || root.checked ? 1px : 0px;
border-color: root.checked ? (root.danger ? Theme.danger.transparentize(50%) : Theme.accent) : Theme.border;
opacity: root.enabled ? 1.0 : 0.45;
animate background { duration: Theme.fast; }
FocusRing {
shown: focus.has-focus;
radius: face.border-radius;
w: root.width;
h: root.height;
}
focus := FocusScope {
enabled: root.enabled;
key-pressed(event) => {
if event.text == " " || event.text == "\n" {
root.clicked();
return accept;
}
reject
}
touch := TouchArea {
enabled: root.enabled;
mouse-cursor: MouseCursor.pointer;
clicked => {
focus.focus();
root.clicked();
}
}
}
Icon {
source: root.icon;
size: root.icon-size;
tint: root.tint;
}
if root.dot: Rectangle {
x: parent.width - self.width - root.size * 0.18;
y: root.size * 0.16;
width: 9px;
height: 9px;
border-radius: 4.5px;
background: root.dot-color;
border-width: 2px;
border-color: Theme.surface;
}
if root.count > 0: CountBadge {
x: parent.width - self.width + 4px;
y: -4px;
count: root.count;
fill: Theme.icon;
ink: Theme.surface;
}
HoverTooltip {
text: root.tooltip;
pressed: touch.pressed;
hovered: touch.has-hover;
}
}
}
// A round control with a caption below (voice bar: Mute, Deafen, Go Live).
export component ActionButton inherits VerticalLayout {
in property <image> icon;
in property <string> text;
in property <bool> checked;
in property <bool> danger;
in property <bool> enabled: true;
in property <length> size: 40px;
callback clicked();
spacing: Theme.space-1;
alignment: center;
HorizontalLayout {
alignment: center;
IconButton {
icon: root.icon;
label: root.text;
// The caption already names it.
tooltip: "";
checked: root.checked;
danger: root.danger;
enabled: root.enabled;
round: true;
filled: true;
size: root.size;
clicked => { root.clicked(); }
}
}
Text {
text: root.text;
font-size: Theme.font-xs;
color: root.checked ? (root.danger ? Theme.danger : Theme.accent-text) : Theme.text-secondary;
horizontal-alignment: center;
accessible-role: AccessibleRole.none;
}
}