134 lines
3.7 KiB
Go
134 lines
3.7 KiB
Go
//go:build windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/lxn/walk"
|
|
. "github.com/lxn/walk/declarative"
|
|
)
|
|
|
|
var styleOptions = []struct {
|
|
Label string
|
|
Value string
|
|
}{
|
|
{"保持 Windows 当前设置", "keep"},
|
|
{"填充", "fill"},
|
|
{"适应", "fit"},
|
|
{"拉伸", "stretch"},
|
|
{"平铺", "tile"},
|
|
{"居中", "center"},
|
|
{"跨区", "span"},
|
|
}
|
|
|
|
func (a *App) showSettings() {
|
|
if a.settingsDialog != nil {
|
|
a.settingsDialog.Show()
|
|
_ = a.settingsDialog.Activate()
|
|
return
|
|
}
|
|
|
|
var dlg *walk.Dialog
|
|
var apiEdit, intervalEdit, cacheEdit *walk.LineEdit
|
|
var autoCheck, startupCheck *walk.CheckBox
|
|
var styleCombo *walk.ComboBox
|
|
|
|
labels := make([]string, len(styleOptions))
|
|
styleIndex := 0
|
|
for i, option := range styleOptions {
|
|
labels[i] = option.Label
|
|
if option.Value == a.config.WallpaperStyle {
|
|
styleIndex = i
|
|
}
|
|
}
|
|
|
|
err := Dialog{
|
|
AssignTo: &dlg,
|
|
Title: "GoWallpaper 设置",
|
|
MinSize: Size{520, 300},
|
|
Layout: VBox{MarginsZero: false},
|
|
Children: []Widget{
|
|
Label{Text: "随机图片 API 地址(HTTP GET 直接返回图片)"},
|
|
LineEdit{AssignTo: &apiEdit, Text: a.config.APIURL},
|
|
Composite{
|
|
Layout: Grid{Columns: 2},
|
|
Children: []Widget{
|
|
CheckBox{AssignTo: &autoCheck, Text: "启用自动切换", Checked: a.config.AutoChangeEnabled},
|
|
CheckBox{AssignTo: &startupCheck, Text: "开机自动启动", Checked: a.config.StartWithWindows},
|
|
Label{Text: "自动切换间隔(分钟)"},
|
|
LineEdit{AssignTo: &intervalEdit, Text: strconv.Itoa(a.config.IntervalMinutes)},
|
|
Label{Text: "最多保留缓存图片数"},
|
|
LineEdit{AssignTo: &cacheEdit, Text: strconv.Itoa(a.config.MaxCacheCount)},
|
|
Label{Text: "壁纸显示方式"},
|
|
ComboBox{AssignTo: &styleCombo, Model: labels, CurrentIndex: styleIndex},
|
|
},
|
|
},
|
|
VSpacer{},
|
|
Composite{
|
|
Layout: HBox{},
|
|
Children: []Widget{
|
|
HSpacer{},
|
|
PushButton{Text: "保存", OnClicked: func() {
|
|
interval, parseErr := parsePositiveInt(intervalEdit.Text(), "自动切换间隔")
|
|
if parseErr != nil {
|
|
walk.MsgBox(dlg, "设置无效", parseErr.Error(), walk.MsgBoxIconWarning)
|
|
return
|
|
}
|
|
maxCache, parseErr := parsePositiveInt(cacheEdit.Text(), "缓存数量")
|
|
if parseErr != nil {
|
|
walk.MsgBox(dlg, "设置无效", parseErr.Error(), walk.MsgBoxIconWarning)
|
|
return
|
|
}
|
|
index := styleCombo.CurrentIndex()
|
|
if index < 0 || index >= len(styleOptions) {
|
|
index = 0
|
|
}
|
|
cfg := Config{
|
|
APIURL: strings.TrimSpace(apiEdit.Text()),
|
|
AutoChangeEnabled: autoCheck.Checked(),
|
|
IntervalMinutes: interval,
|
|
MaxCacheCount: maxCache,
|
|
WallpaperStyle: styleOptions[index].Value,
|
|
StartWithWindows: startupCheck.Checked(),
|
|
}
|
|
if err := a.applyConfig(cfg); err != nil {
|
|
walk.MsgBox(dlg, "保存失败", err.Error(), walk.MsgBoxIconError)
|
|
return
|
|
}
|
|
dlg.Accept()
|
|
if cfg.APIURL == "" {
|
|
a.notifyInfo("配置已保存。刷新壁纸前需要先设置随机图片 API 地址。")
|
|
} else {
|
|
a.notifyInfo("配置已保存。")
|
|
}
|
|
}},
|
|
PushButton{Text: "取消", OnClicked: func() { dlg.Cancel() }},
|
|
},
|
|
},
|
|
},
|
|
}.Create(nil)
|
|
if err != nil {
|
|
a.notifyError("无法打开设置窗口:" + err.Error())
|
|
return
|
|
}
|
|
a.settingsDialog = dlg
|
|
dlg.Closing().Attach(func(_ *bool, _ walk.CloseReason) {
|
|
if a.settingsDialog == dlg {
|
|
a.settingsDialog = nil
|
|
}
|
|
})
|
|
dlg.Show()
|
|
_ = dlg.Activate()
|
|
}
|
|
|
|
func parsePositiveInt(value, field string) (int, error) {
|
|
n, err := strconv.Atoi(strings.TrimSpace(value))
|
|
if err != nil || n <= 0 {
|
|
return 0, fmt.Errorf("%s必须是大于 0 的整数", field)
|
|
}
|
|
return n, nil
|
|
}
|