迁移到 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)
This commit is contained in:
zerorigin committed 2026-11-16 10:31:44 +08:00
1 parent 814012ec52
commit 0b8b9b0775
202 files changed
+7254 -13572

No files matched your search

+131 -13
View File
@@ -14,7 +14,13 @@ import (
const MaxConfigSize = 1 << 20 // 1MB
// CurrentVersion 为当前配置格式版本。
const CurrentVersion = 1
// v2:滚轮音量按"格"生效(steps 提升到 4、冷却清零),并新增屏幕底部
// 滚轮调亮度规则。
// v3:全局新增 notify(调节时是否弹出桌面通知,默认开启;音量/静音的
// 自动通知默认关闭,见动作参数 toast)。
// v4:每条规则新增 notify(该规则是否弹通知,默认开启),global.notify
// 仍作为总开关。
const CurrentVersion = 5
// ErrCorrupted 表示配置损坏、已备份并恢复为默认配置(可继续运行)。
var ErrCorrupted = errors.New("config corrupted, recovered from default")
@@ -31,16 +37,61 @@ type Config struct {
// GlobalConfig 为全局设置。
type GlobalConfig struct {
Enabled bool `json:"enabled"`
Theme string `json:"theme"`
LogLevel string `json:"log_level"`
Enabled bool `json:"enabled"`
Notify bool `json:"notify"` // 调节音量/亮度时是否弹桌面通知
Theme string `json:"theme"`
LogLevel string `json:"log_level"`
OSD OSDConfig `json:"osd"` // 调节提示条的位置
}
// OSDConfig 为调节提示条(音量/亮度胶囊)的位置设置。
//
// Position 为 "top" / "bottom" / "left" / "right":前两者在水平方向居中,
// 后两者在垂直方向居中;Margin 为该边与显示器工作区边缘的距离(DIP)。
type OSDConfig struct {
Position string `json:"position"`
Margin int `json:"margin"`
}
// 提示条位置的合法取值。
const (
OSDPositionTop = "top"
OSDPositionBottom = "bottom"
OSDPositionLeft = "left"
OSDPositionRight = "right"
)
// DefaultOSD 返回提示条位置的默认值:底部居中、距底边 64 DIP。
func DefaultOSD() OSDConfig {
return OSDConfig{Position: OSDPositionBottom, Margin: 64}
}
// OSDMaxMargin 为提示条与屏幕边缘距离的上限(DIP);实际显示时会再按
// 显示器工作区尺寸钳制,保证胶囊不会跑到屏幕外。
const OSDMaxMargin = 1000
// OSDMarginValid 报告提示条边距是否在允许范围内。
func (o OSDConfig) OSDMarginValid() bool {
return o.Margin >= 0 && o.Margin <= OSDMaxMargin
}
// OSDPositionValid 报告提示条位置字符串是否合法(空值视为默认底部)。
func (o OSDConfig) OSDPositionValid() bool {
switch o.Position {
case "", OSDPositionTop, OSDPositionBottom, OSDPositionLeft, OSDPositionRight:
return true
}
return false
}
// Rule 为一条规则:触发器 + 条件(MVP 仅空)+ 动作链。
type Rule struct {
ID string `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
ID string `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
// Notify 控制本规则触发时是否弹右下角系统通知;调节提示条不受影响。
// 与 global.notify 是"与"关系:两者都开启才弹通知。
Notify bool `json:"notify"`
Priority int `json:"priority"`
Trigger TriggerConfig `json:"trigger"`
Conditions []ConditionConfig `json:"conditions,omitempty"`
@@ -106,20 +157,23 @@ type ActionConfig struct {
Params map[string]any `json:"params"`
}
// Default 返回内置默认配置(三条首批规则)。
// Default 返回内置默认配置(虚拟桌面/音量/亮度/快捷键示例规则)。
func Default() *Config {
return &Config{
Version: 1,
Version: CurrentVersion,
Global: GlobalConfig{
Enabled: true,
Notify: true,
Theme: "system",
LogLevel: "info",
OSD: DefaultOSD(),
},
Rules: []Rule{
{
ID: "desktop-left-bottom",
Name: "左下角切换虚拟桌面",
Enabled: true,
Notify: true,
Priority: 100,
Trigger: TriggerConfig{Type: "mouse_corner", Params: map[string]any{"corner": "bottom_left", "screen": "all", "threshold_px": 32}},
Actions: []ActionConfig{{Type: "switch_virtual_desktop", Params: map[string]any{"mode": "alternate"}}},
@@ -129,33 +183,59 @@ func Default() *Config {
ID: "volume-top-wheel-up",
Name: "顶部边缘滚轮增大音量",
Enabled: true,
Notify: true,
Priority: 100,
Trigger: TriggerConfig{Type: "mouse_edge_wheel", Params: map[string]any{"edge": "top", "direction": "up", "threshold_px": 32}},
Actions: []ActionConfig{{Type: "volume_up", Params: map[string]any{}}},
CooldownMs: 50,
Actions: []ActionConfig{{Type: "volume_up", Params: map[string]any{"steps": 4}}},
CooldownMs: 0,
},
{
ID: "volume-top-wheel-down",
Name: "顶部边缘滚轮减小音量",
Enabled: true,
Notify: true,
Priority: 100,
Trigger: TriggerConfig{Type: "mouse_edge_wheel", Params: map[string]any{"edge": "top", "direction": "down", "threshold_px": 32}},
Actions: []ActionConfig{{Type: "volume_down", Params: map[string]any{}}},
CooldownMs: 50,
Actions: []ActionConfig{{Type: "volume_down", Params: map[string]any{"steps": 4}}},
CooldownMs: 0,
},
{
ID: "hotkey-switch-desktop",
Name: "示例:Ctrl+Alt+D 切换桌面(默认关闭)",
Enabled: false,
Notify: true,
Priority: 50,
Trigger: TriggerConfig{Type: "hotkey", Params: map[string]any{"hotkey": "ctrl+alt+d"}},
Actions: []ActionConfig{{Type: "switch_virtual_desktop", Params: map[string]any{"mode": "alternate"}}},
CooldownMs: 400,
},
defaultBrightnessRule("up"),
defaultBrightnessRule("down"),
},
}
}
// defaultBrightnessRule 构造屏幕底部边缘滚轮调亮/调暗规则。
func defaultBrightnessRule(dir string) Rule {
actionType, label := "brightness_up", "调亮"
if dir == "down" {
actionType, label = "brightness_down", "调暗"
}
return Rule{
ID: "brightness-bottom-wheel-" + dir,
Name: "屏幕底部滚轮" + label,
Enabled: true,
Notify: true,
Priority: 100,
Trigger: TriggerConfig{Type: "mouse_edge_wheel", Params: map[string]any{"edge": "bottom", "direction": dir, "threshold_px": 32}},
Actions: []ActionConfig{{Type: actionType, Params: map[string]any{"steps": 5}}},
// 滚轮事件按"格"上报,冷却清零才能让快速连滚每一格都生效;
// 动作层有合并队列兜底,不会因为 DDC 慢而堆积。
CooldownMs: 0,
Fullscreen: "ignore",
}
}
// AppDataDir 返回应用数据目录。
// 便携模式:exe 同目录存在 config.json 时,配置/日志跟随 exe 目录
// (绿色便携版);否则使用 %LOCALAPPDATA%\ZerOriginTool(安装版默认)。
@@ -206,6 +286,42 @@ func Load() (*Config, error) {
return loadAt(path)
}
// Validate 校验全局设置,返回可直接展示给用户的问题描述。
// 加载、热重载与保存都会调用;有问题时调用方应回退到默认值而不是拒绝加载。
func (c *Config) Validate() []string {
var out []string
if !c.Global.OSD.OSDPositionValid() {
out = append(out, fmt.Sprintf("提示条位置 %q 无效(应为 top/bottom/left/right)", c.Global.OSD.Position))
}
if !c.Global.OSD.OSDMarginValid() {
out = append(out, fmt.Sprintf("提示条边距 %d 超出范围(0–%d)", c.Global.OSD.Margin, OSDMaxMargin))
}
return out
}
// Normalize 修正可以自动修复的全局设置(目前是提示条位置与边距),
// 返回被修正的字段数。加载与热重载都会调用,保证运行期一定拿到合法值;
// 与 osd.SetPosition 的归一化重复是刻意的双保险(配置层负责持久化数据)。
func (c *Config) Normalize() int {
fixed := 0
if !c.Global.OSD.OSDPositionValid() {
c.Global.OSD.Position = OSDPositionBottom
fixed++
}
if c.Global.OSD.Position == "" {
c.Global.OSD.Position = OSDPositionBottom
}
if !c.Global.OSD.OSDMarginValid() {
if c.Global.OSD.Margin < 0 {
c.Global.OSD.Margin = 0
} else {
c.Global.OSD.Margin = OSDMaxMargin
}
fixed++
}
return fixed
}
// loadAt 在指定路径上执行完整加载流程(测试可注入临时路径)。
func loadAt(path string) (*Config, error) {
fi, err := os.Stat(path)
@@ -240,6 +356,8 @@ func loadAt(path string) (*Config, error) {
return nil, fmt.Errorf("save migrated config: %w", err)
}
}
// 手工改坏的全局设置就地修正(不写盘),避免非法值传到运行期。
cfg.Normalize()
return &cfg, nil
}