Files
client/native/ui/controls.slint
T
bit9992 87265a3e75
android-ci / android-arm64 (push) Successful in 17m10s
Implement Android Room UI and customizable palette
2026-11-15 12:32:59 +03:00

187 lines
10 KiB
Plaintext

import { ComboBox, ScrollView } 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();
min-height: Theme.android ? max(Theme.touch-target, label.preferred-height + 16px) : Theme.touch-target;
min-width: Theme.android ? 48px : max(44px, label.preferred-width + 24px);
preferred-height: Theme.android ? max(Theme.touch-target, label.preferred-height + 16px) : Theme.touch-target;
vertical-stretch: Theme.android ? 0 : 1;
border-radius: Theme.android || primary ? self.height / 2 : 4px;
background: primary ? (destructive ? Theme.error : Theme.tint) : (Theme.android || touch.has-hover || focus.has-focus ? Theme.bg : transparent);
border-width: focus.has-focus || Theme.high-contrast ? 2px : 0px;
border-color: Theme.outline;
opacity: enabled ? 1 : 0.45;
accessible-role: button;
accessible-label: text;
accessible-enabled: enabled;
accessible-action-default => { if root.enabled { root.clicked(); } }
label := Text { x: Theme.android ? 12px : 0px; width: parent.width - (Theme.android ? 24px : 0px); text: root.text; color: root.primary ? (Theme.android ? Theme.on-tint : (Theme.dark ? Theme.contrast : #fff)) : (root.destructive ? Theme.error : Theme.fg); font-family: Theme.font-family; font-size: Theme.font-size; horizontal-alignment: center; vertical-alignment: center; wrap: Theme.android ? word-wrap : no-wrap; overflow: elide; }
focus := FocusScope {
enabled: root.enabled;
key-pressed(event) => { if event.text == "\n" || event.text == " " { root.clicked(); return accept; } return reject; }
}
touch := TouchArea { enabled: root.enabled; clicked => { focus.focus(); root.clicked(); } }
}
export component TextField inherits Rectangle {
in-out property <string> text;
in property <string> placeholder;
in property <string> label: placeholder;
in property <bool> secret: false;
in property <bool> enabled: true;
out property <bool> has-focus: input.has-focus;
callback edited(string);
callback accepted();
min-height: Theme.touch-target;
preferred-height: Theme.touch-target;
vertical-stretch: Theme.android ? 0 : 1;
background: Theme.bg;
border-radius: 4px;
forward-focus: input;
border-width: input.has-focus || Theme.high-contrast ? 2px : 1px;
border-color: input.has-focus ? Theme.tint : Theme.outline;
if root.text == "" && input.preedit-text == "": Text { x: 10px; width: parent.width - 20px; text: root.placeholder; color: Theme.muted; vertical-alignment: center; font-family: Theme.font-family; font-size: Theme.font-size; overflow: elide; }
input := TextInput {
x: 10px; width: parent.width - 20px;
text <=> root.text;
single-line: true;
input-type: root.secret ? InputType.password : InputType.text;
enabled: root.enabled;
color: Theme.fg;
font-family: Theme.font-family;
font-size: Theme.font-size;
vertical-alignment: center;
selection-background-color: Theme.tint;
selection-foreground-color: Theme.contrast;
edited => { root.edited(self.text); }
accepted => { root.accepted(); }
accessible-label: root.label;
}
}
export component Toggle inherits Rectangle {
in-out property <bool> checked;
in property <bool> enabled: true;
in property <string> label;
callback toggled(bool);
min-width: Theme.android ? 48px : 44px;
min-height: Theme.touch-target;
preferred-width: 48px;
accessible-role: checkbox;
accessible-label: label;
accessible-checked: checked;
accessible-enabled: enabled;
accessible-action-default => { if root.enabled { root.checked = !root.checked; root.toggled(root.checked); } }
Rectangle { width: 40px; height: 20px; x: (parent.width - self.width)/2; y: (parent.height - self.height)/2; border-radius: 10px; background: root.checked ? Theme.success : Theme.contrast; border-width: focus.has-focus || Theme.high-contrast ? 2px : 1px; border-color: Theme.outline;
Rectangle { x: root.checked ? 22px : 2px; y: 2px; width: 16px; height: 16px; border-radius: 8px; background: Theme.fg; }
}
focus := FocusScope { enabled: root.enabled; key-pressed(e) => { if e.text == " " { root.checked = !root.checked; root.toggled(root.checked); return accept; } return reject; } }
TouchArea { enabled: root.enabled; clicked => { focus.focus(); root.checked = !root.checked; root.toggled(root.checked); } }
}
export component Choice inherits Rectangle {
in property <[string]> values;
in-out property <int> current-index;
in property <bool> enabled: true;
in property <string> label;
callback selected(int);
min-width: Theme.android ? 48px : 0px;
min-height: Theme.android ? mobile.preferred-height : Theme.touch-target;
preferred-height: Theme.android ? mobile.preferred-height : Theme.touch-target;
vertical-stretch: Theme.android ? 0 : 1;
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 {
visible: !Theme.android;
width: parent.width; height: parent.height;
model: root.values;
current-index: root.current-index;
enabled: root.enabled;
accessible-label: root.label;
selected(value) => { root.selected(self.current-index); }
}
mobile := ActionButton {
visible: Theme.android;
width: parent.width; height: parent.height;
text: root.current-index >= 0 && root.current-index < root.values.length ? root.values[root.current-index] : root.label;
enabled: root.enabled && root.values.length > 0;
background: Theme.bg; border-width: 1px; border-color: Theme.outline;
accessible-label: root.label + ": " + self.text;
clicked => { options.show(); }
}
options := PopupWindow {
x: 0px; y: root.height;
width: root.width; height: min(240px, root.values.length * Theme.touch-target + 16px);
Rectangle {
background: Theme.contrast; border-width: 1px; border-color: Theme.outline;
ScrollView {
width: parent.width; height: parent.height;
VerticalLayout {
padding: 8px; spacing: 4px; alignment: start;
for value[index] in root.values: ActionButton {
text: value; primary: root.current-index == index;
clicked => { root.current-index = index; root.selected(index); options.close(); }
}
}
}
}
}
}
// Android Room controls share one stroke vocabulary and labelled 48dp hit targets.
export component RoomIcon inherits Path {
in property <string> name;
in property <color> ink: Theme.fg;
width: 24px; height: 24px;
fill: transparent; stroke: ink; stroke-width: 1.8px;
commands: name == "back" ? "M19 12H5 M12 5L5 12L12 19"
: name == "more" ? "M5 11V13 M12 11V13 M19 11V13"
: name == "close" ? "M6 6L18 18 M6 18L18 6"
: name == "mic" ? "M9 5A3 3 0 0 1 15 5V12A3 3 0 0 1 9 12Z M5 10V12A7 7 0 0 0 19 12V10 M12 19V22 M9 22H15"
: name == "mic-off" ? "M3 3L21 21 M9 9V12A3 3 0 0 0 14 14 M9 5A3 3 0 0 1 15 5V11 M5 10V12A7 7 0 0 0 17 17 M19 12V10 M12 19V22 M9 22H15"
: name == "headphones" ? "M4 13V12A8 8 0 0 1 20 12V13 M4 12H3V20H7V12Z M20 12H21V20H17V12Z"
: name == "sound-off" ? "M4 9H8L13 5V19L8 15H4Z M17 10L22 15 M17 15L22 10"
: name == "chat" ? "M4 4H20V16H9L4 20Z M8 8H16 M8 12H13"
: name == "users" ? "M9 12A4 4 0 1 0 9 4A4 4 0 0 0 9 12 M2 21V19A6 6 0 0 1 14 19V21 M17 4A4 4 0 0 1 17 12 M19 15A5 5 0 0 1 22 19V21"
: name == "user" ? "M12 12A4 4 0 1 0 12 4A4 4 0 0 0 12 12 M5 21V19A7 7 0 0 1 19 19V21"
: name == "settings" ? "M9 3L8 6L5 7L3 10L5 12L4 15L6 18L9 17L12 19L15 17L18 18L20 15L19 12L21 10L19 7L16 6L15 3Z M12 9A3 3 0 1 0 12 15A3 3 0 0 0 12 9"
: name == "server" ? "M4 3H20V10H4Z M4 14H20V21H4Z M7 6V7 M7 17V18 M11 6H17 M11 17H17"
: name == "screen" ? "M3 4H21V17H3Z M12 17V21 M8 21H16"
: name == "down" ? "M6 9L12 15L18 9"
: name == "chevron" ? "M9 5L16 12L9 19"
: name == "leave" ? "M9 4H4V20H9 M13 7L18 12L13 17 M8 12H21"
: name == "send" ? "M3 3L22 12L3 21L7 12Z M7 12H22"
: name == "plus" ? "M12 5V19 M5 12H19"
: name == "search" ? "M10 17A7 7 0 1 0 10 3A7 7 0 0 0 10 17 M15 15L21 21"
: name == "palette" ? "M12 3A9 9 0 1 0 12 21H14A2 2 0 0 0 15 17A2 2 0 0 1 16 13H18A3 3 0 0 0 21 10A9 9 0 0 0 12 3 M7 7V9 M12 5V7 M17 7V9 M6 12V14"
: name == "shield" ? "M12 3L20 6V12C20 17 12 21 12 21C12 21 4 17 4 12V6Z"
: name == "bell" ? "M5 16H19L17 13V9A5 5 0 0 0 7 9V13Z M10 20H14"
: "M12 3A9 9 0 1 0 12 21A9 9 0 0 0 12 3 M12 10V16 M12 6V8";
}
export component RoomIconButton inherits Rectangle {
in property <string> icon;
in property <string> label;
in property <bool> enabled: true;
in property <bool> primary: false;
callback clicked();
width: 48px; min-height: 48px; preferred-height: 48px; vertical-stretch: 0;
border-radius: 12px;
background: primary ? Theme.tint : (touch.has-hover || focus.has-focus ? Theme.bg : transparent);
border-width: focus.has-focus || Theme.high-contrast ? 2px : 0px;
border-color: Theme.outline; opacity: enabled ? 1 : 0.45;
accessible-role: button; accessible-label: label; accessible-enabled: enabled;
accessible-action-default => { if root.enabled { root.clicked(); } }
RoomIcon { name: root.icon; ink: root.primary ? Theme.on-tint : Theme.fg; x: (parent.width - self.width)/2; y: (parent.height - self.height)/2; }
focus := FocusScope { enabled: root.enabled; key-pressed(e) => { if e.text == " " || e.text == Key.Return { root.clicked(); return accept; } return reject; } }
touch := TouchArea { enabled: root.enabled; clicked => { focus.focus(); root.clicked(); } }
}