- Go 主进程 cmd/zerorigin(托盘常驻 + 触发器/规则/动作引擎) - Wails v3 设置进程 cmd/settings(独立 module) - 监督进程 cmd/watchdog(-supervise 异常重启) - 构建脚本 build.bat / NSIS 安装包 deploy/、便携版 portable/ - 架构与 MVP 计划文档 docs/、阶段 0-4 验收记录 deploy/acceptance-0-4.md
153 lines
3.5 KiB
Go
153 lines
3.5 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 事件,
|
|
// 由 wheelDebouncer 合并为一次触发;方向切换立即放行。
|
|
type edgeWheelTrigger struct {
|
|
edge string
|
|
threshold int32
|
|
|
|
mu sync.Mutex
|
|
started bool
|
|
hook *pwin.MouseHook
|
|
emit func(Event)
|
|
cancel context.CancelFunc
|
|
deb wheelDebouncer
|
|
}
|
|
|
|
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
|
|
}
|
|
delta := wheelDelta(ev.MouseData)
|
|
if delta == 0 {
|
|
continue
|
|
}
|
|
dir := "up"
|
|
if delta < 0 {
|
|
dir = "down"
|
|
}
|
|
now := time.Now()
|
|
if !t.deb.allow(dir, now) {
|
|
continue // 同一方向的连续滑动:合并为一次触发
|
|
}
|
|
t.emit(Event{
|
|
Type: t.ID(),
|
|
Timestamp: now,
|
|
Payload: map[string]any{"edge": t.edge, "direction": dir, "delta": delta},
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// debounceWindow 为滚轮滑动去抖窗口:同一方向在此窗口内的
|
|
// 连续事件视为同一次滑动。方向切换立即放行。
|
|
const debounceWindow = 120 * time.Millisecond
|
|
|
|
// wheelDebouncer 合并同一方向的连续滚轮事件。
|
|
type wheelDebouncer struct {
|
|
window time.Duration
|
|
last time.Time
|
|
dir string
|
|
}
|
|
|
|
// allow 报告方向 dir 在时刻 now 是否允许触发(未在去抖窗口内)。
|
|
func (d *wheelDebouncer) allow(dir string, now time.Time) bool {
|
|
if d.window == 0 {
|
|
d.window = debounceWindow
|
|
}
|
|
if now.Sub(d.last) < d.window && d.dir == dir {
|
|
d.last = now
|
|
return false
|
|
}
|
|
d.last = now
|
|
d.dir = dir
|
|
return true
|
|
}
|
|
|
|
// wheelDelta 从 MSLLHOOKSTRUCT.mouseData 高位字提取带符号滚轮增量。
|
|
func wheelDelta(mouseData int32) int32 {
|
|
return int32(int16(uint32(mouseData) >> 16))
|
|
}
|