Files
client/native/ui/controls.slint
T

200 lines
9.0 KiB
Plaintext

import { ComboBox } from "std-widgets.slint";
import { Theme } from "theme.slint";
export component ActionButton inherits Rectangle {
in property <string> text;
in property <bool> enabled: true;
in property <bool> primary: false;
in property <bool> destructive: false;
callback clicked();
in property <bool> dialog-action: false;
in property <bool> busy: false;
out property <bool> has-focus: focus.has-focus;
out property <length> natural-width: max(Theme.touch-controls ? 44px : 22px, label.preferred-width + 2 * root.horizontal-inset);
forward-focus: focus;
private property <length> horizontal-inset: Theme.touch-controls ? 12px : (root.primary ? 20px : 0px);
private property <length> action-height: Theme.touch-controls ? Theme.touch-target : (root.dialog-action ? 30px : Theme.button-height);
private property <color> plain-text: root.destructive ? Theme.error-light : Theme.tint-secondary;
min-height: max(root.action-height, label.preferred-height + (root.primary ? 8px : 0px));
min-width: self.natural-width;
preferred-width: self.min-width;
preferred-height: self.min-height;
border-radius: root.primary ? self.height / 2 : 4px;
background: root.primary ? (root.destructive
? (touch.pressed || touch.has-hover ? Theme.destructive-hover : Theme.destructive-fill)
: (touch.pressed || touch.has-hover ? Theme.button-hover : Theme.button-fill))
: (touch.pressed ? Theme.shaded : (touch.has-hover && (!Theme.dark || root.destructive) ? Theme.fg.with-alpha(0.05) : transparent));
border-width: focus.has-focus || Theme.high-contrast ? 2px : 0px;
border-color: Theme.focus-ring;
opacity: root.enabled ? 1 : (root.busy ? 0.7 : (root.dialog-action && !root.primary ? 0.5 : 0.4));
accessible-role: button;
accessible-label: text;
accessible-enabled: enabled;
accessible-action-default => { if root.enabled { root.clicked(); } }
label := Text {
x: min(root.horizontal-inset, root.width / 4);
width: max(0px, parent.width - 2 * self.x);
text: root.text;
color: root.primary ? Theme.fg : (Theme.high-contrast ? Theme.fg : (touch.has-hover && !root.destructive && Theme.dark ? #8ac5fc : root.plain-text));
font-family: Theme.font-family;
font-size: Theme.font-size;
font-weight: root.dialog-action && root.primary ? Theme.bold-weight : Theme.normal-weight;
horizontal-alignment: center;
vertical-alignment: center;
overflow: elide;
accessible-role: none;
}
focus := FocusScope {
enabled: root.enabled;
key-pressed(event) => { if event.text == Key.Return || event.text == " " { root.clicked(); return accept; } return reject; }
}
touch := TouchArea { enabled: root.enabled; mouse-cursor: pointer; clicked => { focus.focus(); root.clicked(); } }
}
export component TextField inherits Rectangle {
in-out property <string> text;
in property <string> placeholder;
in property <bool> secret: false;
in property <bool> preserve-newlines: false;
in property <bool> enabled: true;
out property <bool> has-focus: input.has-focus;
callback edited(string);
in property <bool> invalid: false;
private property <length> inset: Theme.touch-controls ? 10px : 5px;
callback accepted();
min-height: max(Theme.touch-controls ? Theme.touch-target : Theme.field-height, Theme.font-size + 9px);
preferred-height: self.min-height;
background: Theme.bg;
border-radius: Theme.touch-controls ? 4px : 5px;
forward-focus: input;
border-width: Theme.high-contrast || (Theme.touch-controls && input.has-focus) ? 2px : (Theme.touch-controls ? 1px : 1.5px);
border-color: input.has-focus || (root.enabled && hover.has-hover) ? Theme.focus-ring : root.invalid ? Theme.error-light : (Theme.touch-controls || Theme.high-contrast ? Theme.outline : transparent);
if root.text == "" && input.preedit-text == "": Text {
x: root.inset;
width: max(0px, parent.width - 2 * self.x);
text: root.placeholder;
color: Theme.high-contrast ? Theme.fg : Theme.muted;
vertical-alignment: center;
font-family: Theme.font-family;
font-size: Theme.font-size;
font-weight: Theme.normal-weight;
overflow: elide;
accessible-role: none;
}
hover := TouchArea { enabled: root.enabled; mouse-cursor: MouseCursor.text; }
input := TextInput {
x: root.inset;
width: max(0px, parent.width - 2 * self.x);
text <=> root.text;
single-line: !root.preserve-newlines;
input-type: root.secret ? InputType.password : InputType.text;
enabled: root.enabled;
color: root.enabled || Theme.high-contrast ? Theme.fg : Theme.muted;
font-family: Theme.font-family;
font-size: Theme.font-size;
font-weight: Theme.normal-weight;
vertical-alignment: center;
selection-background-color: Theme.tint;
selection-foreground-color: Theme.dark ? Theme.contrast : Theme.fg;
edited => { root.edited(self.text); }
accepted => { root.accepted(); }
accessible-label: root.placeholder;
}
}
export component Toggle inherits Rectangle {
in property <bool> checked;
in property <bool> enabled: true;
in property <string> label;
callback toggled(bool);
min-width: Theme.touch-controls ? 44px : 40px;
min-height: Theme.touch-controls ? Theme.touch-target : 24px;
preferred-width: Theme.touch-controls ? 48px : 40px;
preferred-height: self.min-height;
opacity: root.enabled || Theme.high-contrast ? 1 : 0.5;
private property <color> knob: Theme.high-contrast ? Theme.fg : (root.checked ? Theme.toggle-checked : Theme.subtitle);
accessible-role: checkbox;
accessible-label: label;
accessible-checked: checked;
accessible-enabled: enabled;
accessible-action-default => { if root.enabled { root.toggled(!root.checked); } }
Rectangle {
width: 40px;
height: 20px;
x: (parent.width - self.width)/2;
y: (parent.height - self.height)/2;
border-radius: 14px;
background: touch.pressed ? Theme.shaded : transparent;
border-width: focus.has-focus || Theme.high-contrast ? 2px : 1px;
border-color: focus.has-focus ? Theme.focus-ring : root.knob;
Rectangle {
x: root.checked ? parent.width - self.width - 2px : 2px;
y: 2px;
width: 16px;
height: 16px;
border-radius: 8px;
background: root.knob;
animate x { duration: Theme.reduced-motion ? 0ms : 120ms; }
}
}
focus := FocusScope { enabled: root.enabled; key-pressed(e) => { if e.text == " " { root.toggled(!root.checked); return accept; } return reject; } }
touch := TouchArea { enabled: root.enabled; mouse-cursor: pointer; clicked => { focus.focus(); root.toggled(!root.checked); } }
}
export component Choice inherits Rectangle {
in property <[string]> values;
in property <int> current-index;
in property <bool> enabled: true;
in property <string> label;
callback selected(int);
min-height: max(Theme.touch-controls ? Theme.touch-target : Theme.field-height, Theme.font-size + 9px);
preferred-height: self.min-height;
// Opacity only affects painting; the ComboBox still owns interaction, focus and semantics.
if !Theme.touch-controls: Rectangle {
background: Theme.bg;
border-radius: 10px;
border-width: input.has-focus || Theme.high-contrast ? 2px : 0px;
border-color: Theme.focus-ring;
opacity: root.enabled || Theme.high-contrast ? 1 : 0.6;
Text {
x: 10px;
width: max(0px, parent.width - 36px);
text: input.current-value;
color: Theme.fg;
font-family: Theme.font-family;
font-size: Theme.font-size;
font-weight: Theme.normal-weight;
vertical-alignment: center;
overflow: elide;
accessible-role: none;
}
Path {
x: parent.width - 20px;
y: (parent.height - self.height) / 2;
width: 10px;
height: 5px;
commands: "M 0 0 L 10 0 L 5 5 Z";
fill: Theme.high-contrast ? Theme.fg : #2580c3;
}
}
forward-focus: input;
// ComboBox normalizes its local model/index internally. Never let those writes sever
// the caller's authoritative selected-index binding during a snapshot/model update.
changed current-index => { input.current-index = root.current-index; }
changed values => { input.current-index = root.current-index; }
input := ComboBox {
width: parent.width;
height: parent.height;
opacity: Theme.touch-controls ? 1 : 0;
model: root.values;
current-index: root.current-index;
enabled: root.enabled;
accessible-label: root.label;
selected(value) => {
let requested = self.current-index;
self.current-index = root.current-index;
root.selected(requested);
}
}
}