Files
client/native/ui/dialogs.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

305 lines
18 KiB
Plaintext

import { ScrollView, TextEdit, Slider } from "std-widgets.slint";
import { Theme } from "theme.slint";
import { ActionButton, TextField, Choice, Toggle } from "controls.slint";
import { ShellBridge, ShellCommand } from "shell.slint";
export struct ShellField { id: string, label: string, section: string, hint: string, kind: int, value: string, checked: bool, choices: [string], choice-index: int, enabled: bool, advanced: bool, error: string }
export global DialogBridge {
in-out property <bool> visible;
in-out property <bool> onboarding;
in-out property <string> title;
in-out property <string> description;
in-out property <[ShellField]> fields;
in-out property <[ShellCommand]> actions;
in-out property <string> error;
in-out property <bool> busy;
in-out property <bool> destructive;
in-out property <string> confirmation-label;
in-out property <string> confirmation;
in-out property <string> submit-label;
in-out property <string> cancel-label;
in-out property <string> advanced-label;
in-out property <bool> has-advanced;
in-out property <string> loading-label;
callback edit(string, string);
callback choose(string, int);
callback confirm(string);
callback submit();
callback cancel();
}
// Android forms share a bounded viewport: context and decisions stay on screen while
// every field, auxiliary action and confirmation remains scrollable above the IME.
component AndroidDialog inherits Rectangle {
background: Theme.bg;
border-radius: DialogBridge.onboarding || Theme.ime-inset > 0px ? 0px : 20px;
accessible-role: form;
accessible-label: DialogBridge.title;
property <bool> advanced: false;
property <length> focused-y: -1px;
property <string> focused-field;
property <bool> short: root.height < 400px;
property <bool> stack-actions: root.width < 360px && Theme.font-size > 17px && !root.short;
VerticalLayout {
padding: 20px; spacing: 12px;
if !DialogBridge.onboarding && !root.short && Theme.ime-inset == 0px: HorizontalLayout {
alignment: center; vertical-stretch: 0;
Rectangle { width: 32px; height: 3px; border-radius: 2px; background: Theme.muted; }
}
Text {
text: DialogBridge.title; color: Theme.fg;
font-family: Theme.font-family; font-size: Theme.font-size * 1.25; font-weight: 700;
wrap: word-wrap; vertical-stretch: 0;
accessible-role: text; accessible-label: self.text;
}
body := ScrollView {
min-height: 0px; min-width: 0px; vertical-stretch: 1;
changed visible-height => {
if root.focused-y >= 0px { self.content-y = clamp(-root.focused-y, min(0px, self.visible-height - self.content-height), 0px); }
}
VerticalLayout {
spacing: 12px; alignment: start;
if DialogBridge.description != "": Text {
text: DialogBridge.description; color: Theme.muted; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size;
}
for field in DialogBridge.fields: field-row := Rectangle {
visible: !field.advanced || root.advanced;
height: self.visible ? row.preferred-height : 0px;
vertical-stretch: 0; min-width: 0px;
changed y => {
if root.focused-field == field.id {
root.focused-y = self.y;
body.content-y = clamp(-self.y, min(0px, body.visible-height - body.content-height), 0px);
}
}
row := VerticalLayout {
spacing: 4px;
if field.kind != 5: Text {
text: field.label; color: Theme.fg; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size;
}
if field.kind == 0 || field.kind == 1 || field.kind == 3 || (field.kind == 4 && field.id != "db" && field.id != "volume"): TextField {
text: field.value; placeholder: field.label; secret: field.kind == 1;
enabled: field.enabled && !DialogBridge.busy; min-width: 0px;
edited(value) => { DialogBridge.edit(field.id, value); }
accepted => { if DialogBridge.onboarding { DialogBridge.submit(); } }
changed has-focus => {
if self.has-focus {
root.focused-field = field.id;
root.focused-y = field-row.y;
body.content-y = clamp(-field-row.y, min(0px, body.visible-height - body.content-height), 0px);
} else if root.focused-field == field.id {
root.focused-field = "";
root.focused-y = -1px;
}
}
}
if field.kind == 4 && (field.id == "db" || field.id == "volume"): VerticalLayout {
spacing: 8px;
Text {
text: field.id == "db" ? "\{round(field.value.to-float() * 10) / 10} dB" : "\{round(field.value.to-float() * 100)}%";
color: Theme.tint-text; font-family: Theme.font-family; font-size: Theme.font-size;
horizontal-alignment: right;
}
Slider {
min-height: 48px; minimum: field.id == "db" ? -200 : 0;
maximum: field.id == "db" ? 10 : 2; value: field.value.to-float();
enabled: field.enabled && !DialogBridge.busy;
accessible-label: field.label;
changed(value) => { DialogBridge.edit(field.id, "\{round(value * 100) / 100}"); }
}
}
if field.kind == 2: TextEdit {
text: field.value; enabled: field.enabled && !DialogBridge.busy; wrap: word-wrap;
height: max(96px, Theme.font-size * 4); min-width: 0px;
font-family: Theme.font-family; font-size: Theme.font-size;
accessible-label: field.label;
edited => { DialogBridge.edit(field.id, self.text); }
changed has-focus => {
if self.has-focus {
root.focused-field = field.id;
root.focused-y = field-row.y;
body.content-y = clamp(-field-row.y, min(0px, body.visible-height - body.content-height), 0px);
} else if root.focused-field == field.id {
root.focused-field = "";
root.focused-y = -1px;
}
}
}
if field.kind == 5: HorizontalLayout {
spacing: 8px;
Text {
text: field.label; color: Theme.fg; wrap: word-wrap; vertical-alignment: center;
font-family: Theme.font-family; font-size: Theme.font-size;
horizontal-stretch: 1; min-width: 0px;
}
Toggle {
width: max(48px, Theme.touch-target); horizontal-stretch: 0;
checked: field.checked; label: field.label; enabled: field.enabled && !DialogBridge.busy;
toggled(value) => { DialogBridge.edit(field.id, value ? "true" : "false"); }
}
}
if field.kind == 6: Choice {
values: field.choices; current-index: field.choice-index; label: field.label;
enabled: field.enabled && !DialogBridge.busy; min-width: 0px;
selected(index) => { DialogBridge.choose(field.id, index); }
}
if field.hint != "": Text {
text: field.hint; color: Theme.muted; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size;
}
if field.error != "": Text {
text: field.error; color: Theme.error; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size;
accessible-role: text; accessible-live-region: assertive;
}
}
}
for action in DialogBridge.actions: ActionButton {
text: action.label; enabled: action.enabled && !DialogBridge.busy;
destructive: action.destructive; min-width: 0px;
clicked => { ShellBridge.invoke(action.id); }
}
if !DialogBridge.onboarding && DialogBridge.has-advanced: ActionButton {
text: DialogBridge.advanced-label; min-width: 0px;
clicked => { root.advanced = !root.advanced; }
}
if DialogBridge.confirmation-label != "": confirmation-row := VerticalLayout {
spacing: 4px; vertical-stretch: 0;
changed y => {
if root.focused-field == "__confirmation" {
root.focused-y = self.y;
body.content-y = clamp(-self.y, min(0px, body.visible-height - body.content-height), 0px);
}
}
Text {
text: DialogBridge.confirmation-label; color: Theme.warning; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size;
}
TextField {
text: DialogBridge.confirmation; placeholder: DialogBridge.confirmation-label;
enabled: !DialogBridge.busy; min-width: 0px;
edited(value) => { DialogBridge.confirm(value); }
changed has-focus => {
if self.has-focus {
root.focused-field = "__confirmation";
root.focused-y = confirmation-row.y;
body.content-y = clamp(-confirmation-row.y, min(0px, body.visible-height - body.content-height), 0px);
} else if root.focused-field == "__confirmation" {
root.focused-field = "";
root.focused-y = -1px;
}
}
}
}
if DialogBridge.error != "": Text {
text: DialogBridge.error; color: Theme.error; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size;
accessible-live-region: assertive;
}
if DialogBridge.busy: Text {
text: DialogBridge.loading-label; color: Theme.muted; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size;
}
}
}
if root.stack-actions: VerticalLayout {
spacing: 8px; vertical-stretch: 0;
ActionButton {
text: DialogBridge.submit-label; primary: true; destructive: DialogBridge.destructive;
enabled: !DialogBridge.busy; min-width: 0px;
clicked => { DialogBridge.submit(); }
}
ActionButton {
text: DialogBridge.cancel-label; enabled: !DialogBridge.onboarding || !DialogBridge.busy; min-width: 0px;
clicked => { DialogBridge.cancel(); }
}
}
if !root.stack-actions: HorizontalLayout {
spacing: 8px; vertical-stretch: 0;
ActionButton {
text: DialogBridge.cancel-label; enabled: !DialogBridge.onboarding || !DialogBridge.busy;
min-width: 0px; horizontal-stretch: 1;
clicked => { DialogBridge.cancel(); }
}
ActionButton {
text: DialogBridge.submit-label; primary: true; destructive: DialogBridge.destructive;
enabled: !DialogBridge.busy; min-width: 0px; horizontal-stretch: 1;
clicked => { DialogBridge.submit(); }
}
}
}
}
export component ShellDialog inherits Rectangle {
background: Theme.contrast.with-alpha(0.75);
property <bool> phone: Theme.android;
// An overlay blocks background pointer input without turning Cancel into a submit.
TouchArea { }
if root.phone: AndroidDialog {
width: min(root.width, 640px); x: (root.width - self.width) / 2;
height: DialogBridge.onboarding || Theme.ime-inset > 0px || root.height < 480px ? root.height : min(root.height - 24px, 660px);
y: root.height - self.height;
}
if !root.phone: dialog := Rectangle {
width: min(parent.width - 24px, 640px);
height: min(parent.height - 24px, layout.preferred-height);
x: (parent.width - self.width)/2;
y: (parent.height - self.height)/2;
background: Theme.contrast;
border-color: Theme.outline;
border-width: 1px;
border-radius: 8px;
accessible-role: form;
accessible-label: DialogBridge.title;
property <bool> advanced: false;
property <length> focused-y: 0px;
layout := VerticalLayout {
padding: 20px; spacing: 12px;
Text { text: DialogBridge.title; font-size: Theme.font-size * 1.6; font-weight: 700; color: Theme.fg; wrap: word-wrap; }
if DialogBridge.description != "": Text { text: DialogBridge.description; color: Theme.muted; wrap: word-wrap; }
form-scroll := ScrollView {
min-height: 80px; max-height: 600px;
VerticalLayout {
spacing: 12px;
for field in DialogBridge.fields: field-row := Rectangle {
visible: !field.advanced || dialog.advanced;
height: self.visible ? row.preferred-height : 0px;
row := VerticalLayout {
spacing: 4px;
Text { text: field.label; color: Theme.fg; wrap: word-wrap; }
if field.kind == 0 || field.kind == 1 || field.kind == 3 || field.kind == 4: TextField {
text: field.value; placeholder: field.label; secret: field.kind == 1; enabled: field.enabled && !DialogBridge.busy;
edited(value) => { DialogBridge.edit(field.id,value); }
changed has-focus => { if self.has-focus { dialog.focused-y = field-row.y; form-scroll.content-y = clamp(-field-row.y,min(0px,form-scroll.visible-height - form-scroll.content-height),0px); } }
}
if field.kind == 2: TextEdit {
text: field.value; enabled: field.enabled && !DialogBridge.busy; wrap: word-wrap; min-height: 110px;
accessible-label: field.label;
edited => { DialogBridge.edit(field.id,self.text); }
changed has-focus => { if self.has-focus { dialog.focused-y = field-row.y; form-scroll.content-y = clamp(-field-row.y,min(0px,form-scroll.visible-height - form-scroll.content-height),0px); } }
}
if field.kind == 5: Toggle { checked: field.checked; label: field.label; enabled: field.enabled && !DialogBridge.busy; toggled(value) => { DialogBridge.edit(field.id,value ? "true" : "false"); } }
if field.kind == 6: Choice { values: field.choices; current-index: field.choice-index; label: field.label; enabled: field.enabled && !DialogBridge.busy; selected(index) => { DialogBridge.choose(field.id,index); } }
if field.hint != "": Text { text: field.hint; color: Theme.muted; wrap: word-wrap; }
if field.error != "": Text { text: field.error; color: Theme.error; wrap: word-wrap; accessible-role: text; }
}
}
for action in DialogBridge.actions: ActionButton { text: action.label; enabled: action.enabled && !DialogBridge.busy; destructive: action.destructive; clicked => { ShellBridge.invoke(action.id); } }
ActionButton { text: DialogBridge.advanced-label; clicked => { dialog.advanced = !dialog.advanced; } }
if DialogBridge.confirmation-label != "": Text { text: DialogBridge.confirmation-label; color: Theme.warning; wrap: word-wrap; }
if DialogBridge.confirmation-label != "": TextField { text: DialogBridge.confirmation; placeholder: DialogBridge.confirmation-label; enabled: !DialogBridge.busy; edited(value) => { DialogBridge.confirm(value); } }
}
}
if DialogBridge.error != "": Text { text: DialogBridge.error; color: Theme.error; wrap: word-wrap; accessible-live-region: assertive; }
if DialogBridge.busy: Text { text: DialogBridge.loading-label; color: Theme.muted; }
HorizontalLayout {
alignment: end; spacing: 8px;
ActionButton { text: DialogBridge.cancel-label; clicked => { DialogBridge.cancel(); } }
ActionButton { text: DialogBridge.submit-label; primary: true; destructive: DialogBridge.destructive; enabled: !DialogBridge.busy; clicked => { DialogBridge.submit(); } }
}
}
}
}