1605b5b7ae
- win32/autostart: SetAutoStart/IsAutoStartEnabled 操作 HKCU Run 键 - win32/window_size: GetWindowSize 通过 GetWindowRect 获取窗口尺寸 - config: 新增 AutoStart、WindowWidth、WindowHeight 字段 - store: Preferences/SetPreferences 扩展支持 autoStart + windowSize - dialog_preferences: 新增"开机自启动"复选框 - app.go: 启动时恢复窗口大小,关闭时通过 SetWindowSize 保存 - i18n: label_auto_start / hint_auto_start 三语翻译
185 lines
6.2 KiB
Go
185 lines
6.2 KiB
Go
package ui
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gioui.org/layout"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
"gioui.org/widget/material"
|
|
|
|
"git.wxccs.org/iceking2nd/winauth-go/internal/i18n"
|
|
)
|
|
|
|
// preferencesDialog edits the persistent UI prefs: UI language, theme,
|
|
// auto-lock timeout, minimize-to-tray, and auto-start behaviour.
|
|
type preferencesDialog struct {
|
|
lang string
|
|
theme themeMode
|
|
|
|
// Theme selection — three "radio" buttons.
|
|
themeSystemBt widget.Clickable
|
|
themeLightBt widget.Clickable
|
|
themeDarkBt widget.Clickable
|
|
|
|
// Language selection — one button per supported locale. Cheap
|
|
// substitute for a dropdown until Gio gains a native one.
|
|
langEnBt widget.Clickable
|
|
langZhCNBt widget.Clickable
|
|
langDeBt widget.Clickable
|
|
|
|
// Auto-lock timeout (whole minutes). 0 disables. Only takes effect
|
|
// when the config is encrypted — the prefs dialog does not gate the
|
|
// input on that, the store does.
|
|
autoLockEd widget.Editor
|
|
|
|
// Minimize-to-tray: when on, the X button hides the window to the
|
|
// system tray instead of exiting. Tray icon stays visible either way.
|
|
minToTray widget.Bool
|
|
|
|
// Auto-start: when on, the app is registered in the Windows Run key
|
|
// to launch at logon.
|
|
autoStart widget.Bool
|
|
|
|
okBt widget.Clickable
|
|
cancelBt widget.Clickable
|
|
}
|
|
|
|
// preferencesResult is what Layout's callback receives. cancel skips
|
|
// any persistence.
|
|
type preferencesResult struct {
|
|
cancel bool
|
|
language string
|
|
theme themeMode
|
|
autoLockMinutes int
|
|
minimizeToTray bool
|
|
autoStart bool
|
|
}
|
|
|
|
// supportedLanguages is the closed set the prefs dialog exposes. Keep
|
|
// in sync with internal/i18n/locales/*.toml.
|
|
var supportedLanguages = []struct {
|
|
tag string
|
|
label string
|
|
}{
|
|
{"en", "English"},
|
|
{"zh-CN", "中文 (简体)"},
|
|
{"de", "Deutsch"},
|
|
}
|
|
|
|
func newPreferencesDialog(currentLang string, currentTheme themeMode, currentAutoLock int, currentMinToTray, currentAutoStart bool) *preferencesDialog {
|
|
if currentLang == "" {
|
|
currentLang = "en"
|
|
}
|
|
if currentTheme == "" {
|
|
currentTheme = themeSystem
|
|
}
|
|
d := &preferencesDialog{lang: currentLang, theme: currentTheme}
|
|
d.autoLockEd.SingleLine = true
|
|
d.autoLockEd.SetText(strconv.Itoa(currentAutoLock))
|
|
d.minToTray.Value = currentMinToTray
|
|
d.autoStart.Value = currentAutoStart
|
|
return d
|
|
}
|
|
|
|
func (d *preferencesDialog) Layout(
|
|
gtx layout.Context, th *material.Theme,
|
|
onDone func(preferencesResult),
|
|
) layout.Dimensions {
|
|
if d.cancelBt.Clicked(gtx) {
|
|
onDone(preferencesResult{cancel: true})
|
|
return layout.Dimensions{Size: gtx.Constraints.Max}
|
|
}
|
|
if d.okBt.Clicked(gtx) {
|
|
// Best-effort parse — non-numeric or negative input maps to 0
|
|
// (disabled) rather than surfacing an error, since the field
|
|
// is paired with a "0 disables" hint.
|
|
mins, err := strconv.Atoi(strings.TrimSpace(d.autoLockEd.Text()))
|
|
if err != nil || mins < 0 {
|
|
mins = 0
|
|
}
|
|
onDone(preferencesResult{
|
|
language: d.lang,
|
|
theme: d.theme,
|
|
autoLockMinutes: mins,
|
|
minimizeToTray: d.minToTray.Value,
|
|
autoStart: d.autoStart.Value,
|
|
})
|
|
return layout.Dimensions{Size: gtx.Constraints.Max}
|
|
}
|
|
|
|
// Map button clicks to model updates before the next layout.
|
|
switch {
|
|
case d.themeSystemBt.Clicked(gtx):
|
|
d.theme = themeSystem
|
|
case d.themeLightBt.Clicked(gtx):
|
|
d.theme = themeLight
|
|
case d.themeDarkBt.Clicked(gtx):
|
|
d.theme = themeDark
|
|
case d.langEnBt.Clicked(gtx):
|
|
d.lang = "en"
|
|
case d.langZhCNBt.Clicked(gtx):
|
|
d.lang = "zh-CN"
|
|
case d.langDeBt.Clicked(gtx):
|
|
d.lang = "de"
|
|
}
|
|
|
|
// Render one labeled "chip" — emphasised when selected, regular
|
|
// otherwise. Implemented as a material.Button so the click target
|
|
// stays generous without us hand-rolling a Pointer area.
|
|
chip := func(btn *widget.Clickable, label string, selected bool) layout.FlexChild {
|
|
return layout.Rigid(func(gtx layout.Context) layout.Dimensions {
|
|
return layout.Inset{Right: unit.Dp(8), Bottom: unit.Dp(4)}.Layout(gtx,
|
|
func(gtx layout.Context) layout.Dimensions {
|
|
b := material.Button(th, btn, label)
|
|
if !selected {
|
|
b.Background = activePalette.RingBg
|
|
b.Color = activePalette.OnBackground
|
|
}
|
|
return b.Layout(gtx)
|
|
})
|
|
})
|
|
}
|
|
|
|
body := func(gtx layout.Context) layout.Dimensions {
|
|
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
|
|
layout.Rigid(material.Body1(th, i18n.T("label_theme")).Layout),
|
|
layout.Rigid(layout.Spacer{Height: unit.Dp(6)}.Layout),
|
|
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
|
|
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
|
|
chip(&d.themeSystemBt, i18n.T("theme_system"), d.theme == themeSystem),
|
|
chip(&d.themeLightBt, i18n.T("theme_light"), d.theme == themeLight),
|
|
chip(&d.themeDarkBt, i18n.T("theme_dark"), d.theme == themeDark),
|
|
)
|
|
}),
|
|
layout.Rigid(layout.Spacer{Height: unit.Dp(12)}.Layout),
|
|
layout.Rigid(material.Body1(th, i18n.T("label_language")).Layout),
|
|
layout.Rigid(layout.Spacer{Height: unit.Dp(6)}.Layout),
|
|
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
|
|
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
|
|
chip(&d.langEnBt, supportedLanguages[0].label, d.lang == "en"),
|
|
chip(&d.langZhCNBt, supportedLanguages[1].label, d.lang == "zh-CN"),
|
|
chip(&d.langDeBt, supportedLanguages[2].label, d.lang == "de"),
|
|
)
|
|
}),
|
|
layout.Rigid(layout.Spacer{Height: unit.Dp(12)}.Layout),
|
|
layout.Rigid(labeledEditor(th, i18n.T("label_auto_lock_minutes"), &d.autoLockEd, "")),
|
|
layout.Rigid(layout.Spacer{Height: unit.Dp(2)}.Layout),
|
|
layout.Rigid(material.Caption(th, i18n.T("hint_auto_lock_disabled")).Layout),
|
|
layout.Rigid(layout.Spacer{Height: unit.Dp(12)}.Layout),
|
|
layout.Rigid(material.CheckBox(th, &d.minToTray, i18n.T("label_minimize_to_tray")).Layout),
|
|
layout.Rigid(layout.Spacer{Height: unit.Dp(2)}.Layout),
|
|
layout.Rigid(material.Caption(th, i18n.T("hint_minimize_to_tray")).Layout),
|
|
layout.Rigid(layout.Spacer{Height: unit.Dp(12)}.Layout),
|
|
layout.Rigid(material.CheckBox(th, &d.autoStart, i18n.T("label_auto_start")).Layout),
|
|
layout.Rigid(layout.Spacer{Height: unit.Dp(2)}.Layout),
|
|
layout.Rigid(material.Caption(th, i18n.T("hint_auto_start")).Layout),
|
|
)
|
|
}
|
|
|
|
return modalCard(gtx, th, i18n.T("dialog_preferences_title"),
|
|
i18n.T("btn_ok"), i18n.T("btn_cancel"),
|
|
&d.okBt, &d.cancelBt, body)
|
|
}
|