Files
zerorigin 0b8b9b0775 迁移到 mygo 单进程架构并接入签名自更新
- 单进程:托盘常驻 + 原生设置窗口(internal/settings),移除 Wails 前端与 IPC
- 触发器(鼠标角落 / 边缘滚轮 / 热键)+ 规则引擎 + 动作注册表
- 调节提示条:截屏 + glass.Blur 三层毛玻璃、背景跟随屏幕变化、悬停光感与拖动
- 音量 Core Audio COM、亮度 DDC/CI(gamma 兜底)、多显示器求平均
- 配置热重载 + 版本迁移 v1→v5、便携模式、崩溃取证三件套
- updater/native 插件 + 托盘「检查更新…」,watchdog 识别更新重启
- 自建 Gitea 发布:scripts/publish-gitea.ps1(updates.url 指向 latest Release)
2026-11-16 10:31:44 +08:00

589 lines
18 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package settings
import (
"fmt"
"strconv"
"github.com/egoist/mygo/ui"
"zerorigin/internal/action"
"zerorigin/internal/config"
"zerorigin/internal/trigger"
)
// 设置窗口的两个页面。page 由左侧分段控件直接绑定。
const (
pageRules = iota
pageGeneral
)
// view 每帧重建整个设置窗口(mygo 原生 UI 没有虚拟 DOM,状态放在 Window 上)。
func (w *Window) view(c *ui.Context) {
t := c.Theme()
ui.Row(c).Fill().AlignItems(ui.Stretch).Children(func() {
w.sidebar(c, t)
ui.Column(c).Grow(1).MinWidth(0).Background(t.Background).Children(func() {
w.header(c, t)
ui.Divider(c)
ui.Scroll(c).Grow(1).TrackScroll(&w.scroll).Children(func() {
ui.Column(c).FillWidth().Padding(20, 28).Gap(16).Children(func() {
if w.page == pageGeneral {
w.generalPage(c, t)
return
}
w.rulesPage(c, t)
})
})
})
})
}
// sidebar 渲染品牌、页面导航、规则列表与运行状态。
func (w *Window) sidebar(c *ui.Context, t *ui.Theme) {
ui.Column(c).Width(264).Shrink(0).Padding(16, 14).Gap(16).Background(t.Surface).Children(func() {
ui.Row(c).Gap(10).AlignItems(ui.Center).Children(func() {
ui.Box(c).Size(34, 34).Radius(10).Background(t.Accent).Center().Children(func() {
ui.Text(c, "零").FontSize(16).Bold().TextColor(t.AccentText)
})
ui.Column(c).Gap(1).Children(func() {
ui.Text(c, "零源工具箱").FontSize(15).Bold()
ui.Text(c, "设置").FontSize(11).TextColor(t.TextMuted)
})
})
if ui.Segmented(c, &w.page, "规则", "通用设置").FillEqually().Label("页面").Changed() {
// 切页不提交草稿;保存时统一写入,避免切页失败打断浏览。
w.scroll.Y = 0
}
ui.Divider(c)
if w.page == pageRules {
w.ruleList(c, t)
} else {
ui.Spacer(c)
}
ui.Divider(c)
ui.Row(c).Gap(7).AlignItems(ui.Center).Children(func() {
color := t.TextMuted
label := "规则引擎已暂停"
if w.cfg.Global.Enabled {
color = t.Success
label = "规则引擎运行中"
}
ui.Box(c).Size(7, 7).Radius(4).Background(color)
ui.Text(c, label).FontSize(12).TextColor(t.TextMuted)
})
if w.opts.Version != "" {
ui.Text(c, "版本 "+w.opts.Version).FontSize(11).TextColor(t.TextMuted)
}
})
}
// ruleList 渲染「规则列表」标题、新建按钮与选中列表。
func (w *Window) ruleList(c *ui.Context, t *ui.Theme) {
ui.Row(c).AlignItems(ui.Center).Children(func() {
ui.Column(c).Grow(1).Gap(1).Children(func() {
ui.Text(c, "规则列表").FontSize(12).FontWeight(600).TextColor(t.TextMuted)
ui.Text(c, "按优先级从高到低匹配").FontSize(11).TextColor(t.TextMuted)
})
if ui.Button(c, "+ 新建").FontSize(12).Padding(4, 8).Clicked() {
w.addRule()
}
})
if len(w.cfg.Rules) == 0 {
ui.Text(c, "还没有规则,点击上方新建第一条。").FontSize(12).TextColor(t.TextMuted)
return
}
sb := ui.Sidebar(c, &w.sel, func() {
for i := range w.cfg.Rules {
r := &w.cfg.Rules[i]
item := ui.SidebarItem(c.Key(r.ID), r.ID, nil, w.ruleLabel(r))
if !r.Enabled {
item.Children(func() { ui.Badge(c, "停用") })
}
}
}).Grow(1).Padding(0).Label("规则列表")
if sb.Changed() {
w.selectRule(w.sel)
}
}
// ruleLabel 返回规则在列表中的显示文本。
func (w *Window) ruleLabel(r *config.Rule) string {
name := r.Name
if name == "" {
name = r.ID
}
return name
}
// header 渲染页标题、状态提示与保存按钮;全局选项不再挤在顶部一行。
func (w *Window) header(c *ui.Context, t *ui.Theme) {
title := "规则"
subtitle := w.ruleSummary()
if w.page == pageGeneral {
title = "通用设置"
subtitle = "调整应用外观、通知与启动行为"
}
msg, msgColor := w.headerStatus(t)
ui.Row(c).Padding(18, 28).Gap(20).AlignItems(ui.Center).Children(func() {
ui.Column(c).Grow(1).MinWidth(0).Gap(3).Children(func() {
ui.Text(c, title).FontSize(20).Bold()
ui.Text(c, subtitle).FontSize(12).TextColor(t.TextMuted)
// 固定留出状态行高度,编辑时出现“有未保存的修改”不会把整页内容顶下去。
if msg == "" {
ui.Box(c).Height(16)
} else {
ui.Row(c).Height(16).Gap(6).AlignItems(ui.Center).Children(func() {
ui.Box(c).Size(6, 6).Radius(3).Background(msgColor)
ui.Text(c, msg).FontSize(12).TextColor(msgColor)
})
}
})
save := ui.PrimaryButton(c, "保存").Width(88).Disabled(!w.dirty)
if save.Clicked() {
w.save()
}
})
}
// headerStatus 返回页头状态提示及其颜色:错误优先,其次是未保存修改。
func (w *Window) headerStatus(t *ui.Theme) (string, ui.Color) {
if w.statusKind == statusError && w.status != "" {
return w.status, t.Danger
}
if w.dirty {
return "有未保存的修改", t.Warning
}
if w.status == "" {
return "", t.TextMuted
}
if w.statusKind == statusSuccess {
return w.status, t.Success
}
return w.status, t.TextMuted
}
// ruleSummary 返回规则数量摘要。
func (w *Window) ruleSummary() string {
if len(w.cfg.Rules) == 0 {
return "还没有规则"
}
enabled := 0
for i := range w.cfg.Rules {
if w.cfg.Rules[i].Enabled {
enabled++
}
}
return fmt.Sprintf("共 %d 条规则 · %d 条已启用", len(w.cfg.Rules), enabled)
}
// rulesPage 渲染规则页内容。
func (w *Window) rulesPage(c *ui.Context, t *ui.Theme) {
if w.draft == nil {
ui.Column(c).FillWidth().Padding(44, 32).Gap(12).AlignItems(ui.Center).
Background(t.Surface).Radius(12).Border(1, t.Border).Children(func() {
ui.Text(c, "还没有规则").FontSize(18).Bold()
ui.Text(c, "创建一条规则,为鼠标手势或快捷键指定动作。").FontSize(13).TextColor(t.TextMuted)
if ui.PrimaryButton(c, "新建规则").Clicked() {
w.addRule()
}
})
return
}
w.basicCard(c, t)
w.triggerCard(c, t)
w.actionsCard(c, t)
w.ruleFooter(c, t)
}
// basicCard 渲染规则名称、启用状态与匹配选项。
func (w *Window) basicCard(c *ui.Context, t *ui.Theme) {
d := w.draft
w.sectionCard(c, t, "基本信息", "名称用于列表识别,优先级决定匹配顺序。", func() {
ui.Field(c, "规则名称", func() {
if ui.TextInput(c, d.Name).Placeholder("例如:左下角切换虚拟桌面").FillWidth().Changed() {
w.markDirty()
}
}).FillWidth()
ui.Row(c).FillWidth().Gap(24).AlignItems(ui.Center).Children(func() {
ui.Column(c).Grow(1).Gap(2).Children(func() {
ui.Text(c, "启用这条规则").FontSize(13).FontWeight(500)
ui.Text(c, "停用后规则仍保留在配置中,但不会触发。").FontSize(12).TextColor(t.TextMuted)
})
if ui.Switch(c, &d.Enabled).Label("启用这条规则").Changed() {
w.markDirty()
}
})
w.settingRow(c, t, "显示通知", "本条规则触发时在右下角弹出系统通知;屏幕底部的调节提示条不受影响。仍受通用设置中的总开关控制。", &d.Notify, func() {
w.markDirty()
})
ui.Row(c).FillWidth().Gap(16).AlignItems(ui.Start).Children(func() {
ui.Column(c).Grow(1).Children(func() {
ui.Field(c, "优先级", func() {
if ui.TextInput(c, d.Priority).Width(160).Changed() {
w.markDirty()
}
}).Description("数值越大越先匹配;相同优先级按配置顺序")
})
ui.Column(c).Grow(1).Children(func() {
ui.Field(c, "冷却时间 (ms)", func() {
if ui.TextInput(c, d.CooldownMs).Width(160).Changed() {
w.markDirty()
}
}).Description("同一条规则两次触发之间的最小间隔")
})
})
ui.Field(c, "全屏行为", func() {
if ui.Select(c, d.Fullscreen, fullscreenOptions).Width(320).Changed() {
w.markDirty()
}
}).Description("前台全屏(游戏/视频)时是否跳过本规则")
})
}
// triggerCard 渲染触发器类型与参数。
func (w *Window) triggerCard(c *ui.Context, t *ui.Theme) {
d := w.draft
w.sectionCard(c, t, "触发器", "决定这条规则在什么条件下被触发。", func() {
ui.Field(c, "触发器类型", func() {
name := trigger.DisplayName(d.TriggerType)
if ui.Select(c, &name, triggerNames()).Width(360).Changed() {
d.setTriggerType(triggerIDByName(name))
w.markDirty()
}
})
if specs := triggerSpecs(d.TriggerType); len(specs) > 0 {
w.paramRows(c, specs, d.TriggerVals)
}
})
}
// actionsCard 渲染按顺序执行的动作列表。
func (w *Window) actionsCard(c *ui.Context, t *ui.Theme) {
d := w.draft
deleteIdx := -1
w.sectionCard(c, t, "动作", "按列表顺序执行;每一步都可以单独删除或更换类型。", func() {
if len(d.Actions) == 0 {
ui.Text(c, "还没有动作。添加至少一个动作后才能保存。").
FontSize(12).TextColor(t.TextMuted)
}
for i, ad := range d.Actions {
idx := i
adc := ad
ui.Column(c).FillWidth().Padding(14, 16).Gap(14).
Background(t.Background).Radius(8).Border(1, t.Border).Children(func() {
ui.Row(c).FillWidth().AlignItems(ui.Center).Gap(8).Children(func() {
ui.Text(c, fmt.Sprintf("动作 %d", idx+1)).FontSize(13).FontWeight(600).Grow(1)
if ui.Button(c, "删除").FontSize(12).Padding(4, 8).TextColor(t.Danger).Clicked() {
deleteIdx = idx
}
})
ui.Field(c, "动作类型", func() {
name := action.DisplayName(adc.Type)
if ui.Select(c, &name, actionNames()).Width(360).Changed() {
adc.setType(actionIDByName(name))
w.markDirty()
}
})
if specs := actionSpecs(adc.Type); len(specs) > 0 {
w.paramRows(c, specs, adc.Vals)
}
})
}
ui.Row(c).Gap(8).AlignItems(ui.Center).Children(func() {
ui.MenuButton(c, "+ 添加动作", func(m *ui.Menu) {
for _, name := range actionNames() {
if m.Item(name).Chosen() {
d.Actions = append(d.Actions, newActionDraft(actionIDByName(name)))
w.markDirty()
}
}
}).Width(150)
})
if deleteIdx >= 0 {
d.Actions = append(d.Actions[:deleteIdx], d.Actions[deleteIdx+1:]...)
w.markDirty()
}
})
}
// ruleFooter 渲染规则 ID 与独立的删除入口。
func (w *Window) ruleFooter(c *ui.Context, t *ui.Theme) {
d := w.draft
ui.Divider(c)
ui.Row(c).FillWidth().Gap(16).AlignItems(ui.Center).PaddingX(2).Children(func() {
ui.Text(c, "规则 ID:"+d.ID+" · 仅用于日志与配置定位").
FontSize(11).TextColor(t.TextMuted).Grow(1)
if ui.Button(c, "删除这条规则").FontSize(12).Padding(4, 8).TextColor(t.Danger).Clicked() {
w.deleteRule()
}
})
}
// paramRows 把参数按两列排布;奇数个参数时最后一格留空。
func (w *Window) paramRows(c *ui.Context, specs []paramSpec, vals map[string]*string) {
for i := 0; i < len(specs); i += 2 {
left := specs[i]
var right *paramSpec
if i+1 < len(specs) {
right = &specs[i+1]
}
ui.Row(c).FillWidth().Gap(16).AlignItems(ui.Start).Children(func() {
ui.Column(c).Grow(1).Children(func() {
w.paramField(c, left, vals[left.Name])
})
if right != nil {
ui.Column(c).Grow(1).Children(func() {
w.paramField(c, *right, vals[right.Name])
})
} else {
ui.Box(c).Grow(1)
}
})
}
}
// generalPage 渲染通用设置页。
func (w *Window) generalPage(c *ui.Context, t *ui.Theme) {
w.sectionCard(c, t, "启动与运行", "控制规则引擎与登录 Windows 后的启动行为。", func() {
w.settingRow(c, t, "启用规则引擎", "关闭后所有规则暂停,托盘程序仍常驻;保存后生效。", &w.cfg.Global.Enabled, func() {
w.markDirty()
})
ui.Divider(c)
w.settingRow(c, t, "开机自启", "登录 Windows 后自动启动零源工具箱;此项立即生效。", &w.autoStart, func() {
w.toggleAutoStart()
})
})
w.sectionCard(c, t, "提醒", "所有规则通知的总开关;调节音量、亮度时底部的提示条不受影响。", func() {
w.settingRow(c, t, "显示系统通知(总开关)", "关闭后所有规则都不弹右下角通知;屏幕底部的调节提示条仍会显示。保存后生效。", &w.cfg.Global.Notify, func() {
w.markDirty()
})
})
w.sectionCard(c, t, "调节提示条", "调节音量、屏幕亮度时出现的胶囊条。位置与边距保存后立即生效。", func() {
ui.Row(c).FillWidth().Gap(16).AlignItems(ui.Start).Children(func() {
ui.Column(c).Grow(1).Children(func() {
ui.Field(c, "出现位置", func() {
// 只改编辑态字段,保存时再写回配置(与主题下拉一致),
// 避免每帧重建视图时覆盖用户刚选中的项。
if ui.Select(c, &w.osdPosition, osdPositionOptions).FillWidth().Changed() {
w.markDirty()
}
}).Description("提示条贴在屏幕的上、下、左、右哪一边")
})
ui.Column(c).Grow(1).Children(func() {
ui.Field(c, "距屏幕边缘", func() {
if ui.NumberInput(c, &w.osdMargin, 0, config.OSDMaxMargin, 4).FillWidth().Changed() {
w.markDirty()
}
}).Description("单位像素(100% 缩放),0 表示贴边")
})
})
ui.Text(c, "提示条所在那一边的另一方向始终居中;换显示器后同样按此设置定位。").
FontSize(12).TextColor(t.TextMuted)
})
w.sectionCard(c, t, "外观与诊断", "主题保存后立即应用,日志级别写入配置文件。", func() {
ui.Row(c).FillWidth().Gap(16).AlignItems(ui.Start).Children(func() {
ui.Column(c).Grow(1).Children(func() {
ui.Field(c, "主题", func() {
if ui.Select(c, &w.themeChoice, []string{themeSystem, themeLight, themeDark}).FillWidth().Changed() {
w.markDirty()
}
}).Description("保存后立即应用到设置窗口与主程序")
})
ui.Column(c).Grow(1).Children(func() {
ui.Field(c, "日志级别", func() {
if ui.Select(c, &w.cfg.Global.LogLevel, []string{"debug", "info", "warn", "error"}).FillWidth().Changed() {
w.markDirty()
}
}).Description("用于问题诊断,保存在配置文件中")
})
})
})
w.sectionCard(c, t, "配置文件", "修改点击“保存”后写入,现有规则会立即重新加载。", func() {
ui.Row(c).FillWidth().Gap(16).AlignItems(ui.Center).Children(func() {
ui.Text(c, "配置目录中包含当前配置与损坏恢复时保留的备份。").
FontSize(12).TextColor(t.TextMuted).Grow(1)
if ui.Button(c, "打开配置目录").Clicked() {
if w.opts.OpenConfigDir != nil {
w.opts.OpenConfigDir()
}
}
})
})
}
// osdPositionOptions 是提示条出现位置的下拉选项,顺序与内部值一一对应。
var osdPositionOptions = []string{"底部", "顶部", "左侧", "右侧"}
// osdPositionValues 与 osdPositionOptions 一一对应的配置内部值。
var osdPositionValues = []string{
config.OSDPositionBottom, config.OSDPositionTop, config.OSDPositionLeft, config.OSDPositionRight,
}
// osdPositionLabel 把配置内部值转换成下拉显示文本(未知值按底部)。
func osdPositionLabel(value string) string {
for i, v := range osdPositionValues {
if v == value {
return osdPositionOptions[i]
}
}
return osdPositionOptions[0]
}
// osdPositionValue 把下拉显示文本转换成配置内部值(反查失败时按底部)。
func osdPositionValue(label string) string {
for i, opt := range osdPositionOptions {
if opt == label {
return osdPositionValues[i]
}
}
return config.OSDPositionBottom
}
// settingRow 渲染左侧标题/说明、右侧开关的一行设置。
func (w *Window) settingRow(c *ui.Context, t *ui.Theme, title, desc string, on *bool, apply func()) {
ui.Row(c).FillWidth().Gap(24).AlignItems(ui.Center).Children(func() {
ui.Column(c).Grow(1).Gap(2).Children(func() {
ui.Text(c, title).FontSize(13).FontWeight(500)
ui.Text(c, desc).FontSize(12).TextColor(t.TextMuted)
})
if ui.Switch(c, on).Label(title).Changed() && apply != nil {
apply()
}
})
}
// sectionCard 渲染带标题和说明的卡片,fn 构建卡片正文。
func (w *Window) sectionCard(c *ui.Context, t *ui.Theme, title, desc string, fn func()) {
ui.Column(c).FillWidth().Padding(18, 20).Gap(16).
Background(t.Surface).Radius(12).Border(1, t.Border).Children(func() {
ui.Column(c).Gap(3).Children(func() {
ui.Text(c, title).FontSize(15).Bold()
ui.Text(c, desc).FontSize(12).TextColor(t.TextMuted)
})
fn()
})
}
// paramField 渲染单个参数输入(下拉、开关或文本)。
func (w *Window) paramField(c *ui.Context, p paramSpec, val *string) {
if val == nil {
return
}
label := p.Label
if p.Required {
label += " *"
}
help := p.Help
if p.Required && help == "" {
help = "必填"
}
if p.Type == "bool" {
ui.Row(c).FillWidth().Gap(16).AlignItems(ui.Center).Children(func() {
ui.Column(c).Grow(1).Gap(2).Children(func() {
ui.Text(c, label).FontSize(13).FontWeight(500)
if help != "" {
ui.Text(c, help).FontSize(12).TextColor(c.Theme().TextMuted)
}
})
b := false
if s, err := strconv.ParseBool(*val); err == nil {
b = s
}
if ui.Switch(c, &b).Label(label).Changed() {
*val = strconv.FormatBool(b)
w.markDirty()
}
})
return
}
field := ui.Field(c, label, func() {
switch p.Type {
case "select":
opts := p.Options
if len(p.Labels) == len(p.Options) {
opts = p.Labels
}
if ui.Select(c, val, opts).FillWidth().Changed() {
w.markDirty()
}
default:
if ui.TextInput(c, val).FillWidth().Changed() {
w.markDirty()
}
}
}).FillWidth()
if help != "" {
field.Description(help)
}
}
// triggerNames 返回触发器下拉的显示文本。
func triggerNames() []string {
ids := trigger.IDs()
out := make([]string, 0, len(ids))
for _, id := range ids {
out = append(out, trigger.DisplayName(id))
}
return out
}
// triggerIDByName 由显示文本反查触发器 ID。
func triggerIDByName(name string) string {
for _, id := range trigger.IDs() {
if trigger.DisplayName(id) == name {
return id
}
}
return name
}
// actionNames 返回动作下拉的显示文本。
func actionNames() []string {
ids := action.IDs()
out := make([]string, 0, len(ids))
for _, id := range ids {
out = append(out, action.DisplayName(id))
}
return out
}
// actionIDByName 由显示文本反查动作 ID。
func actionIDByName(name string) string {
for _, id := range action.IDs() {
if action.DisplayName(id) == name {
return id
}
}
return name
}
// toggleAutoStart 应用开机自启开关并提示结果。
func (w *Window) toggleAutoStart() {
if w.opts.SetAutoStart == nil {
return
}
if err := w.opts.SetAutoStart(w.autoStart); err != nil {
w.autoStart = !w.autoStart
w.setStatus("设置开机自启失败:"+err.Error(), statusError)
return
}
if w.autoStart {
w.setStatus("已启用开机自启", statusInfo)
} else {
w.setStatus("已关闭开机自启", statusInfo)
}
}