Files
client/native/ui/settings.slint
T

184 lines
11 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;
callback field-focused();
background: setting.kind == 0 ? transparent : Theme.header;
min-height: setting.kind == 0 ? 48px : content.preferred-height + 16px;
preferred-height: self.min-height;
VerticalLayout { padding: 0px;
content := VerticalLayout {
padding-top: root.setting.kind == 0 ? 14px : 8px;
padding-bottom: 8px;
padding-left: root.setting.inset ? 36px : 20px;
padding-right: 20px;
spacing: 8px;
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 != "": 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: 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 == 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: root.setting.value; placeholder: root.setting.title; secret: root.setting.kind == 8;
enabled: root.setting.enabled; min-height: max(48px, Theme.touch-target);
changed has-focus => { if self.has-focus { root.field-focused(); } }
edited(value) => { SettingsBridge.draft(root.setting.token, value); }
accepted() => { SettingsBridge.commit(root.setting.token, self.text); }
}
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 == 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;
forward-focus: keyboard;
keyboard := FocusScope {
key-pressed(event) => {
if 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;
}
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 {
changed visible-height => { if Theme.android { self.content-y = clamp(-root.focused-row-y, min(0px, self.visible-height - self.content-height), 0px); } }
for row in SettingsBridge.rows: cell := SettingCell {
setting: row; compact: root.compact;
field-focused => { if Theme.android { root.focused-row-y = cell.y; settings-list.content-y = clamp(-cell.y, min(0px, settings-list.visible-height - settings-list.content-height), 0px); } }
}
}
if SettingsBridge.show-slint: AboutSlint { }
}
}
}
}