- 单进程:托盘常驻 + 原生设置窗口(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)
152 lines
3.7 KiB
Go
152 lines
3.7 KiB
Go
package trigger
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"sync"
|
||
"time"
|
||
|
||
pwin "zerorigin/internal/platform/windows"
|
||
)
|
||
|
||
func init() {
|
||
Register("mouse_edge_wheel", func(params map[string]any) (Trigger, error) {
|
||
edge, _ := params["edge"].(string)
|
||
if edge == "" {
|
||
edge = "top"
|
||
}
|
||
switch edge {
|
||
case "top", "bottom", "left", "right":
|
||
default:
|
||
return nil, fmt.Errorf("mouse_edge_wheel: invalid edge %q", edge)
|
||
}
|
||
threshold := int32(32)
|
||
if v, ok := params["threshold_px"]; ok {
|
||
if f, ok := v.(float64); ok {
|
||
threshold = int32(f)
|
||
}
|
||
}
|
||
return &edgeWheelTrigger{edge: edge, threshold: threshold}, nil
|
||
})
|
||
}
|
||
|
||
// edgeWheelTrigger 检测光标位于屏幕边缘时的滚轮事件。
|
||
//
|
||
// 触发粒度是"一格"而不是"一次滑动":标准滚轮每格产生 WM_MOUSEWHEEL
|
||
// delta=±120(WHEEL_DELTA),wheelNotcher 按 120 累积,因此滚 3-4 格就会
|
||
// 连续产生 3-4 个事件,动作层无需等待滑动结束。高分辨率滚轮(delta 小于
|
||
// 120 的平滑滚动)会按累计量折算成同样的"格",避免小 delta 造成几十次触发。
|
||
type edgeWheelTrigger struct {
|
||
edge string
|
||
threshold int32
|
||
|
||
mu sync.Mutex
|
||
started bool
|
||
hook *pwin.MouseHook
|
||
emit func(Event)
|
||
cancel context.CancelFunc
|
||
notch wheelNotcher
|
||
}
|
||
|
||
func (t *edgeWheelTrigger) ID() string { return "mouse_edge_wheel" }
|
||
func (t *edgeWheelTrigger) Name() string { return "屏幕边缘滚轮" }
|
||
|
||
func (t *edgeWheelTrigger) Start(ctx context.Context, emit func(Event)) error {
|
||
t.mu.Lock()
|
||
defer t.mu.Unlock()
|
||
if t.started {
|
||
return nil
|
||
}
|
||
ctx, cancel := context.WithCancel(ctx)
|
||
t.cancel = cancel
|
||
t.emit = emit
|
||
t.hook = pwin.NewMouseHook(256)
|
||
if err := t.hook.Start(); err != nil {
|
||
cancel()
|
||
return err
|
||
}
|
||
t.started = true
|
||
go t.loop(ctx)
|
||
return nil
|
||
}
|
||
|
||
func (t *edgeWheelTrigger) Stop() error {
|
||
t.mu.Lock()
|
||
defer t.mu.Unlock()
|
||
if !t.started {
|
||
return nil
|
||
}
|
||
t.started = false
|
||
t.cancel()
|
||
return t.hook.Stop()
|
||
}
|
||
|
||
func (t *edgeWheelTrigger) loop(ctx context.Context) {
|
||
// 触发器 loop 为常驻 goroutine,panic 会拖垮整个进程:兜底记录不中断。
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
pwin.LogGoPanic("trigger.edgeWheel.loop", r, stackBuf())
|
||
}
|
||
}()
|
||
rects, err := pwin.MonitorRects()
|
||
if err != nil {
|
||
rects = []pwin.MonitorRect{pwin.PrimaryMonitorRect()}
|
||
}
|
||
for {
|
||
select {
|
||
case <-ctx.Done():
|
||
return
|
||
case ev := <-t.hook.Events():
|
||
if ev.Message != pwin.WmMouseWheel {
|
||
continue
|
||
}
|
||
if !pwin.EdgeHit(rects, ev.X, ev.Y, t.threshold, t.edge) {
|
||
continue
|
||
}
|
||
dir, ok := t.notch.feed(wheelDelta(ev.MouseData))
|
||
if !ok {
|
||
continue // 尚未累计满一格(平滑滚轮的中间事件)
|
||
}
|
||
t.emit(Event{
|
||
Type: t.ID(),
|
||
Timestamp: time.Now(),
|
||
Payload: map[string]any{"edge": t.edge, "direction": dir},
|
||
})
|
||
}
|
||
}
|
||
}
|
||
|
||
// wheelNotchDelta 为滚轮一格的增量(Win32 WHEEL_DELTA)。
|
||
const wheelNotchDelta = 120
|
||
|
||
// wheelNotcher 把连续的滚轮增量累积成"格":每满一格产生一次触发。
|
||
// 方向切换时丢弃残留量,避免"先滚上去半格、再滚下来"被误算成一格。
|
||
type wheelNotcher struct {
|
||
acc int32
|
||
}
|
||
|
||
// feed 输入一次滚轮增量,返回触发方向(up/down)与是否达到一格。
|
||
func (n *wheelNotcher) feed(delta int32) (string, bool) {
|
||
if delta == 0 {
|
||
return "", false
|
||
}
|
||
if (delta > 0 && n.acc < 0) || (delta < 0 && n.acc > 0) {
|
||
n.acc = 0
|
||
}
|
||
n.acc += delta
|
||
switch {
|
||
case n.acc >= wheelNotchDelta:
|
||
n.acc -= wheelNotchDelta
|
||
return "up", true
|
||
case n.acc <= -wheelNotchDelta:
|
||
n.acc += wheelNotchDelta
|
||
return "down", true
|
||
}
|
||
return "", false
|
||
}
|
||
|
||
// wheelDelta 从 MSLLHOOKSTRUCT.mouseData 高位字提取带符号滚轮增量。
|
||
func wheelDelta(mouseData int32) int32 {
|
||
return int32(int16(uint32(mouseData) >> 16))
|
||
}
|