Files
client/native/ui/settings.slint
T
2026-11-12 18:33:57 +03:00

280 lines
16 KiB
Plaintext

import { ScrollView, ListView, AboutSlint } from "std-widgets.slint";
import { Theme } from "theme.slint";
import { ActionButton, TextField, Toggle, Choice } from "controls.slint";
import { RichDocument, ChatVisual } from "chat.slint";
export struct SettingsRow {
token: string, stable-id: string, title: string, subtitle: string, kind: int,
checked: bool, value: string, options: [string], selected: int,
enabled: bool, destructive: bool, inset: bool, error: string,
meter: float, decrease: string, increase: string, rich: [ChatVisual],
}
export global SettingsBridge {
in-out property <[string]> categories;
in-out property <[string]> category-groups;
in-out property <[SettingsRow]> rows;
in-out property <int> category;
in-out property <string> title;
in-out property <string> status;
in-out property <string> error;
in-out property <string> back-label;
in-out property <string> apply-label;
in-out property <string> cancel-label;
in-out property <bool> panel-open;
in-out property <bool> subpage;
in-out property <bool> show-slint;
in-out property <bool> capturing;
callback category-chosen(int);
callback back();
callback invoke(string);
callback toggle-value(string, bool);
callback choice-value(string, int);
callback draft(string, string);
callback commit(string, string);
callback step(string, int);
callback capture-lost();
}
component SettingCell inherits Rectangle {
in property <SettingsRow> setting;
in property <bool> compact;
private property <string> secret-draft;
private property <string> secret-owner: setting.stable-id;
changed secret-owner => { root.secret-draft = ""; }
callback field-focused(bool);
background: setting.kind == 0 ? transparent : Theme.header;
min-height: Theme.android ? content.preferred-height : (setting.kind == 0 ? 48px : content.preferred-height + 16px);
preferred-height: self.min-height;
vertical-stretch: Theme.android ? 0 : 1;
VerticalLayout { padding: 0px;
content := VerticalLayout {
padding-top: root.setting.kind == 0 ? (Theme.android ? 12px : 14px) : 8px;
padding-bottom: 8px;
padding-left: Theme.android ? (root.setting.inset ? 24px : 12px) : (root.setting.inset ? 36px : 20px);
padding-right: Theme.android ? 12px : 20px;
spacing: Theme.android ? 6px : 8px;
if !Theme.android || (root.setting.title != "" && root.setting.kind != 6 && root.setting.kind != 2): Text { text: root.setting.title; color: Theme.fg; font-family: Theme.font-family;
font-size: Theme.font-size; font-weight: root.setting.kind == 0 ? 700 : 450;
wrap: word-wrap; accessible-role: text; accessible-label: self.text;
}
if root.setting.subtitle != "" && (!Theme.android || root.setting.kind != 2): Text { text: root.setting.subtitle; color: Theme.muted;
font-family: Theme.font-family; font-size: Theme.font-size - 1px; wrap: word-wrap;
accessible-role: text; accessible-label: self.text;
}
if root.setting.kind == 2 && !Theme.android: Toggle {
checked: root.setting.checked; enabled: root.setting.enabled; label: root.setting.title;
min-height: max(48px, Theme.touch-target);
toggled(value) => { self.checked = root.setting.checked; SettingsBridge.toggle-value(root.setting.token, value); }
}
if root.setting.kind == 2 && Theme.android: HorizontalLayout {
spacing: 8px;
Text { text: root.setting.title; color: Theme.fg; font-family: Theme.font-family;
font-size: Theme.font-size; wrap: word-wrap; vertical-alignment: center;
horizontal-stretch: 1; min-width: 0px;
}
Toggle {
width: max(48px, Theme.touch-target); horizontal-stretch: 0;
checked: root.setting.checked; enabled: root.setting.enabled; label: root.setting.title;
toggled(value) => { self.checked = root.setting.checked; SettingsBridge.toggle-value(root.setting.token, value); }
}
}
if Theme.android && root.setting.kind == 2 && root.setting.subtitle != "": Text {
text: root.setting.subtitle; color: Theme.muted; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size - 1px;
accessible-role: text; accessible-label: self.text;
}
if root.setting.kind == 3: Choice {
values: root.setting.options; current-index: root.setting.selected;
enabled: root.setting.enabled; label: root.setting.title;
min-height: max(48px, Theme.touch-target);
selected(index) => { self.current-index = root.setting.selected; SettingsBridge.choice-value(root.setting.token, index); }
}
if root.setting.kind == 4 || root.setting.kind == 5 || root.setting.kind == 8: VerticalLayout {
spacing: 4px;
field := TextField {
text: Theme.android && root.setting.kind == 8 ? root.secret-draft : root.setting.value;
placeholder: root.setting.title; secret: root.setting.kind == 8;
enabled: root.setting.enabled; min-height: max(48px, Theme.touch-target);
min-width: 0px;
changed has-focus => { root.field-focused(self.has-focus); }
edited(value) => {
if Theme.android && root.setting.kind == 8 { root.secret-draft = value; }
else { SettingsBridge.draft(root.setting.token, value); }
}
accepted() => {
SettingsBridge.commit(root.setting.token, self.text);
if root.setting.kind == 8 { root.secret-draft = ""; }
}
}
HorizontalLayout { spacing: 4px;
if root.setting.kind == 5: ActionButton { text: "-"; accessible-label: root.setting.decrease; enabled: root.setting.enabled;
min-height: max(48px, Theme.touch-target);
clicked => { SettingsBridge.step(root.setting.token, -1); }
}
if root.setting.kind == 5: ActionButton { text: "+"; accessible-label: root.setting.increase; enabled: root.setting.enabled;
min-height: max(48px, Theme.touch-target);
clicked => { SettingsBridge.step(root.setting.token, 1); }
}
ActionButton { text: SettingsBridge.apply-label; enabled: root.setting.enabled;
min-height: max(48px, Theme.touch-target);
clicked => {
SettingsBridge.commit(root.setting.token, field.text);
if root.setting.kind == 8 { root.secret-draft = ""; }
}
}
}
}
if root.setting.kind == 6: ActionButton {
text: root.setting.title; enabled: root.setting.enabled; destructive: root.setting.destructive;
min-height: max(48px, Theme.touch-target);
clicked => { SettingsBridge.invoke(root.setting.token); }
}
if root.setting.kind == 7: Rectangle {
height: 8px; background: Theme.contrast; border-radius: 4px;
Rectangle { width: parent.width * clamp(root.setting.meter, 0, 1); height: parent.height;
background: Theme.success; border-radius: 4px; }
}
if root.setting.kind == 9: RichDocument { rows: root.setting.rich; inert: true; }
if root.setting.error != "": Text { text: root.setting.error; color: Theme.error; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size; accessible-role: text; accessible-label: self.text; }
}
}
}
export component SettingsScreen inherits Rectangle {
background: Theme.shaded;
min-width: 0px; min-height: 0px;
preferred-width: 100%; preferred-height: 100%;
private property <bool> compact: self.width < 600px;
private property <length> focused-row-y: -1px;
private property <string> focused-row;
forward-focus: keyboard;
keyboard := FocusScope {
key-pressed(event) => {
if !Theme.android && event.text == Key.Escape && !SettingsBridge.capturing { SettingsBridge.back(); return accept; }
if !SettingsBridge.panel-open && event.text == Key.DownArrow { SettingsBridge.category-chosen(SettingsBridge.category < 11 ? SettingsBridge.category + 1 : 0); return accept; }
if !SettingsBridge.panel-open && event.text == Key.UpArrow { SettingsBridge.category-chosen(SettingsBridge.category > 0 ? SettingsBridge.category - 1 : 11); return accept; }
if !SettingsBridge.panel-open && event.text == Key.Home { SettingsBridge.category-chosen(0); return accept; }
if !SettingsBridge.panel-open && event.text == Key.End { SettingsBridge.category-chosen(11); return accept; }
return reject;
}
if !Theme.android: HorizontalLayout {
spacing: 0px;
if !root.compact || !SettingsBridge.panel-open: Rectangle {
width: root.compact ? parent.width : min(280px, root.width * 0.34);
background: Theme.bg;
VerticalLayout { padding: 12px; spacing: 4px;
ActionButton { text: SettingsBridge.back-label; clicked => { SettingsBridge.back(); } min-height: 48px; }
ScrollView {
VerticalLayout { spacing: 4px;
for category[index] in SettingsBridge.categories: VerticalLayout {
spacing: 4px;
if index == 0 || index == 2 || index == 11: Text {
text: SettingsBridge.category-groups[index == 0 ? 0 : index == 2 ? 1 : 2];
color: Theme.muted; font-family: Theme.font-family; font-size: 13px;
min-height: 32px; vertical-alignment: center;
}
Rectangle {
min-height: max(48px, Theme.touch-target);
background: index == SettingsBridge.category ? Theme.deviant : transparent;
border-radius: 4px;
ActionButton { text: category;
clicked => { SettingsBridge.category-chosen(index); }
}
}
}
Rectangle { vertical-stretch: 1; }
}
}
}
}
if !root.compact || SettingsBridge.panel-open: VerticalLayout {
max-width: 680px; horizontal-stretch: 1;
padding-left: root.compact ? 8px : 20px;
padding-right: root.compact ? 8px : 20px;
padding-top: 12px; padding-bottom: 12px; spacing: 8px;
if root.compact || SettingsBridge.subpage: ActionButton {
text: SettingsBridge.back-label; min-height: 48px;
clicked => { SettingsBridge.back(); keyboard.focus(); }
}
Text { text: SettingsBridge.title; color: Theme.fg; font-family: Theme.font-family;
font-size: 24px; font-weight: 700; wrap: word-wrap;
accessible-role: text; accessible-label: self.text; }
if SettingsBridge.error != "": Text { text: SettingsBridge.error; color: Theme.error; wrap: word-wrap; accessible-role: text; accessible-label: self.text; }
if SettingsBridge.status != "": Text { text: SettingsBridge.status; color: Theme.muted; wrap: word-wrap; accessible-role: text; accessible-label: self.text; }
settings-list := ListView {
for row in SettingsBridge.rows: cell := SettingCell {
setting: row; compact: root.compact;
}
}
if SettingsBridge.show-slint: AboutSlint { }
}
}
if Theme.android: VerticalLayout {
padding: 8px; spacing: 0px; min-width: 0px;
if !SettingsBridge.panel-open: ScrollView {
min-height: 0px; min-width: 0px; vertical-stretch: 1;
VerticalLayout {
spacing: 4px; alignment: start; min-width: 0px; preferred-width: 0px;
for category[index] in SettingsBridge.categories: VerticalLayout {
spacing: 4px; vertical-stretch: 0; min-width: 0px; preferred-width: 0px;
if index == 0 || index == 2 || index == 11: Text {
text: SettingsBridge.category-groups[index == 0 ? 0 : index == 2 ? 1 : 2];
color: Theme.muted; font-family: Theme.font-family; font-size: Theme.font-size - 1px;
min-height: 32px; vertical-alignment: center; wrap: word-wrap;
}
ActionButton {
text: category; primary: index == SettingsBridge.category;
min-width: 0px; vertical-stretch: 0;
clicked => { SettingsBridge.category-chosen(index); }
}
}
}
}
if SettingsBridge.panel-open: phone-list := ScrollView {
min-height: 0px; min-width: 0px; vertical-stretch: 1;
changed visible-height => {
if root.focused-row-y >= 0px { self.content-y = clamp(-root.focused-row-y, min(0px, self.visible-height - self.content-height), 0px); }
}
VerticalLayout {
spacing: 4px; alignment: start; min-width: 0px; preferred-width: 0px;
if SettingsBridge.error != "": Text {
text: SettingsBridge.error; color: Theme.error; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size;
accessible-role: text; accessible-label: self.text; accessible-live-region: assertive;
}
if SettingsBridge.status != "": Text {
text: SettingsBridge.status; color: Theme.muted; wrap: word-wrap;
font-family: Theme.font-family; font-size: Theme.font-size;
accessible-role: text; accessible-label: self.text;
}
for row in SettingsBridge.rows: phone-row := VerticalLayout {
spacing: 0px; vertical-stretch: 0; min-width: 0px; preferred-width: 0px;
changed y => {
if root.focused-row == row.stable-id {
root.focused-row-y = self.y;
phone-list.content-y = clamp(-self.y, min(0px, phone-list.visible-height - phone-list.content-height), 0px);
}
}
if row.kind != 0 || row.title != SettingsBridge.title: SettingCell {
setting: row; compact: true;
min-width: 0px; preferred-width: 0px; vertical-stretch: 0;
field-focused(focused) => {
if focused {
root.focused-row = row.stable-id;
root.focused-row-y = phone-row.y;
phone-list.content-y = clamp(-phone-row.y, min(0px, phone-list.visible-height - phone-list.content-height), 0px);
} else if root.focused-row == row.stable-id {
root.focused-row = "";
root.focused-row-y = -1px;
}
}
}
}
if SettingsBridge.show-slint: AboutSlint { }
}
}
}
}
}