Files
client/native/ui/dialogs.slint
T
omar 8fb4356c91 Defer language changes until restart and preserve the saved choice
Save first, show the native restart confirmation only on success, and dispatch the existing relaunch route only on confirmation.
Keep the startup catalog active until the next launch and verify cancel, policy-denied confirmation, failed save, and separate-process restore.
2026-11-28 08:00:29 +03:00

256 lines
16 KiB
Plaintext

import { ScrollView, Slider, TextEdit } 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 <bool> cancel-enabled: true;
in-out property <bool> preserve-message-input;
in-out property <string> advanced-label;
in-out property <string> loading-label;
in-out property <string> reset-label;
in-out property <bool> speaker;
in-out property <float> speaker-gain;
in-out property <float> speaker-travel;
in-out property <bool> speaker-muted;
in-out property <int> speaker-gesture;
public pure function speaker-db(p: float) -> float {
return clamp(floor(2 * (p < 4 ? -200 + 35 * p : p < 16 ? -60 + (p - 4) * 20 / 12 : p < 52 ? -40 + (p - 16) * 10 / 12 : -10 + (p - 52) * 5 / 12) + 0.5) / 2, -200, 10);
}
public pure function speaker-position(db: float) -> float {
return db < -60 ? (db + 200) / 35 : db < -40 ? 4 + (db + 60) * 0.6 : db < -10 ? 16 + (db + 40) * 1.2 : 52 + (db + 10) * 2.4;
}
callback speaker-edit(float, bool);
callback mute-speaker(bool);
callback reset-speaker();
callback edit(string, string);
callback choose(string, int);
callback confirm(string);
callback submit();
callback cancel();
}
export component ShellDialog inherits Rectangle {
background: Theme.contrast.with-alpha(Theme.high-contrast ? 0.55 : 0.25);
backdrop-blur: Theme.touch-controls ? 0px : 2px;
// An overlay blocks background pointer input without turning Cancel into a submit.
TouchArea { }
dialog := Rectangle {
width: max(0px, min(parent.width - (Theme.touch-controls ? 24px : 40px), Theme.touch-controls ? 640px : 480px));
height: max(0px, min(parent.height - (Theme.touch-controls ? 24px : 16px), layout.preferred-height));
x: (parent.width - self.width)/2;
y: (parent.height - self.height)/2;
background: Theme.contrast;
border-color: Theme.high-contrast ? Theme.fg : Theme.tint-secondary;
border-width: Theme.high-contrast ? 2px : 1px;
border-radius: 8px;
drop-shadow-color: #0000008c;
drop-shadow-offset-y: 24px;
drop-shadow-blur: 60px;
accessible-role: AccessibleRole.form;
accessible-label: DialogBridge.title;
property <bool> advanced: false;
private property <length> inset: self.width < 360px ? 12px : 20px;
layout := VerticalLayout {
padding-left: dialog.inset;
padding-right: dialog.inset;
padding-top: 18px;
padding-bottom: 14px;
spacing: 0px;
HorizontalLayout {
spacing: 10px;
VerticalLayout {
padding-top: 2px;
spacing: 2px;
horizontal-stretch: 1;
Text { text: DialogBridge.title; font-size: Theme.large-font; font-weight: Theme.bold-weight; color: Theme.fg; min-width: 0px; wrap: word-wrap; }
if DialogBridge.description != "": Text { text: DialogBridge.description; font-size: Theme.small-font; font-weight: Theme.normal-weight; color: Theme.muted; min-width: 0px; wrap: word-wrap; }
}
if !Theme.touch-controls: Rectangle {
visible: DialogBridge.cancel-enabled;
width: 30px;
height: 30px;
border-radius: 8px;
background: close-touch.pressed ? Theme.shaded : (close-touch.has-hover ? Theme.fg.with-alpha(0.05) : transparent);
border-width: close-focus.has-focus || Theme.high-contrast ? 2px : 0px;
border-color: Theme.focus-ring;
accessible-role: button;
accessible-label: DialogBridge.cancel-label;
accessible-action-default => { DialogBridge.cancel(); }
Path {
x: (parent.width - self.width) / 2;
y: (parent.height - self.height) / 2;
width: 10px;
height: 10px;
stroke: Theme.high-contrast ? Theme.fg : Theme.error-light;
stroke-width: 1.5px;
commands: "M 0 0 L 10 10 M 0 10 L 10 0";
}
close-focus := FocusScope {
key-pressed(event) => {
if event.text == Key.Return || event.text == " " { DialogBridge.cancel(); return accept; }
return reject;
}
}
close-touch := TouchArea { mouse-cursor: pointer; clicked => { DialogBridge.cancel(); } }
}
}
form-scroll := ScrollView {
min-height: 0px;
preferred-height: min(600px, form.preferred-height);
vertical-stretch: 1;
content-width: self.visible-width;
content-height: form.preferred-height;
form := VerticalLayout {
width: form-scroll.visible-width;
padding-top: 18px;
padding-bottom: 20px;
spacing: 0px;
for field in DialogBridge.fields: field-row := Rectangle {
visible: !field.advanced || dialog.advanced;
height: self.visible ? row.preferred-height : 0px;
row := VerticalLayout {
padding-bottom: 16px;
spacing: 4px;
Text { text: field.label; color: Theme.fg; font-size: Theme.small-font; font-weight: Theme.normal-weight; min-width: 0px; 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;
// Slint's single-line TextInput replaces pasted newlines with spaces.
preserve-newlines: DialogBridge.preserve-message-input && field.id == "message";
invalid: field.error != "";
edited(value) => { DialogBridge.edit(field.id,value); }
changed has-focus => { if self.has-focus { 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;
font-family: Theme.font-family; font-size: Theme.small-font;
accessible-label: field.label;
edited => { DialogBridge.edit(field.id,self.text); }
changed has-focus => { if self.has-focus { form-scroll.content-y = clamp(-field-row.y,min(0px,form-scroll.visible-height - form-scroll.content-height),0px); } }
}
if field.kind == 7: HorizontalLayout {
spacing: 8px;
Rectangle {
horizontal-stretch: 1;
min-height: 24px;
parity-speaker-fader := Slider {
width: parent.width; height: parent.height;
minimum: 0; maximum: 100; step: 1;
value <=> DialogBridge.speaker-travel;
accessible-label: field.label; enabled: field.enabled && !DialogBridge.busy;
init => { self.focus(); }
// The pointer overlay owns drag events; native keyboard and accessibility changes commit directly.
changed(position) => { DialogBridge.speaker-edit(DialogBridge.speaker-db(position), true); }
}
parity-speaker-pointer := TouchArea {
width: parent.width; height: parent.height;
enabled: parity-speaker-fader.enabled;
private property <bool> dragging;
private property <bool> reset-on-release;
private property <int> gesture;
pure function gain-at-pointer() -> float {
return DialogBridge.speaker-db(floor(clamp(100 * self.mouse-x / max(1px, self.width), 0, 100) + 0.5));
}
pointer-event(e) => {
if e.kind == PointerEventKind.cancel { self.dragging = false; return; }
if e.button != PointerEventButton.left { return; }
if e.kind == PointerEventKind.down {
parity-speaker-fader.focus();
self.gesture = DialogBridge.speaker-gesture;
self.dragging = true;
self.reset-on-release = e.modifiers.alt;
if !self.reset-on-release { DialogBridge.speaker-edit(self.gain-at-pointer(), false); }
} else if e.kind == PointerEventKind.up && self.dragging {
self.dragging = false;
if self.gesture == DialogBridge.speaker-gesture {
DialogBridge.speaker-edit(self.reset-on-release ? 0 : self.gain-at-pointer(), true);
}
}
}
moved => {
if self.dragging && !self.reset-on-release && self.gesture == DialogBridge.speaker-gesture {
DialogBridge.speaker-edit(self.gain-at-pointer(), false);
}
}
}
}
Text { text: DialogBridge.speaker-gain <= -200 ? "-∞ dB" : DialogBridge.speaker-gain + " dB"; min-width: 64px; color: Theme.fg; font-size: Theme.small-font; vertical-alignment: center; }
ActionButton { text: DialogBridge.reset-label; enabled: field.enabled && !DialogBridge.busy; clicked => { DialogBridge.reset-speaker(); } }
}
if field.kind == 5 && DialogBridge.speaker: parity-speaker-mute := Toggle {
checked: DialogBridge.speaker-muted; label: field.label; enabled: field.enabled && !DialogBridge.busy;
toggled(value) => { DialogBridge.mute-speaker(value); }
}
if field.kind == 5 && !DialogBridge.speaker: 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; font-size: Theme.small-font; min-width: 0px; wrap: word-wrap; }
if field.error != "": Text { text: field.error; color: Theme.error-light; font-size: Theme.small-font; min-width: 0px; 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); } }
if !DialogBridge.speaker && DialogBridge.fields.length > 0: ActionButton { text: DialogBridge.advanced-label; clicked => { dialog.advanced = !dialog.advanced; } }
if DialogBridge.confirmation-label != "": VerticalLayout {
padding-top: 16px;
spacing: 4px;
Text { text: DialogBridge.confirmation-label; color: Theme.warning; font-size: Theme.small-font; min-width: 0px; wrap: word-wrap; }
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-light; font-size: Theme.small-font; min-width: 0px; wrap: word-wrap; accessible-live-region: assertive; }
if DialogBridge.busy: Text { text: DialogBridge.loading-label; color: Theme.muted; font-size: Theme.small-font; }
if DialogBridge.speaker: HorizontalLayout {
Rectangle { horizontal-stretch: 1; }
parity-speaker-close := ActionButton {
text: DialogBridge.cancel-label;
dialog-action: true;
enabled: DialogBridge.cancel-enabled;
clicked => { DialogBridge.cancel(); }
}
}
if !DialogBridge.speaker: footer := Rectangle {
private property <bool> stacked: cancel.preferred-width + submit.preferred-width + 10px > self.width;
preferred-height: self.stacked ? cancel.height + submit.height + 10px : max(cancel.height, submit.height);
vertical-stretch: 0;
cancel := ActionButton {
text: DialogBridge.cancel-label;
dialog-action: true;
enabled: DialogBridge.cancel-enabled;
width: footer.stacked ? footer.width : self.preferred-width;
height: self.preferred-height;
x: footer.stacked ? 0px : footer.width - self.width - submit.width - 10px;
y: footer.stacked ? 0px : (footer.height - self.height) / 2;
clicked => { DialogBridge.cancel(); }
}
submit := ActionButton {
text: DialogBridge.submit-label;
dialog-action: true;
primary: true;
destructive: DialogBridge.destructive;
enabled: !DialogBridge.busy;
busy: DialogBridge.busy;
width: footer.stacked ? footer.width : self.preferred-width;
height: self.preferred-height;
x: footer.width - self.width;
y: footer.stacked ? cancel.height + 10px : (footer.height - self.height) / 2;
clicked => { DialogBridge.submit(); }
}
}
}
}
}