2026-03-13 14:34:30 +00:00
|
|
|
// pkg/keybinding/service.go
|
|
|
|
|
package keybinding
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2026-03-31 16:14:19 +01:00
|
|
|
coreerr "dappco.re/go/core/log"
|
|
|
|
|
core "dappco.re/go/core"
|
2026-03-13 14:34:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Options struct{}
|
|
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
|
*core.ServiceRuntime[Options]
|
2026-03-31 05:13:43 +00:00
|
|
|
platform Platform
|
|
|
|
|
registeredBindings map[string]BindingInfo
|
2026-03-13 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 16:14:19 +01:00
|
|
|
func (s *Service) OnStartup(_ context.Context) core.Result {
|
2026-03-13 14:34:30 +00:00
|
|
|
s.Core().RegisterQuery(s.handleQuery)
|
2026-03-31 16:14:19 +01:00
|
|
|
s.Core().Action("keybinding.add", func(_ context.Context, opts core.Options) core.Result {
|
|
|
|
|
t, _ := opts.Get("task").Value.(TaskAdd)
|
|
|
|
|
return core.Result{Value: nil, OK: true}.New(s.taskAdd(t))
|
|
|
|
|
})
|
|
|
|
|
s.Core().Action("keybinding.remove", func(_ context.Context, opts core.Options) core.Result {
|
|
|
|
|
t, _ := opts.Get("task").Value.(TaskRemove)
|
|
|
|
|
return core.Result{Value: nil, OK: true}.New(s.taskRemove(t))
|
|
|
|
|
})
|
|
|
|
|
s.Core().Action("keybinding.process", func(_ context.Context, opts core.Options) core.Result {
|
|
|
|
|
t, _ := opts.Get("task").Value.(TaskProcess)
|
|
|
|
|
return core.Result{Value: nil, OK: true}.New(s.taskProcess(t))
|
|
|
|
|
})
|
|
|
|
|
return core.Result{OK: true}
|
2026-03-13 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 16:14:19 +01:00
|
|
|
func (s *Service) HandleIPCEvents(_ *core.Core, _ core.Message) core.Result {
|
|
|
|
|
return core.Result{OK: true}
|
2026-03-13 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Query Handlers ---
|
|
|
|
|
|
2026-03-31 16:14:19 +01:00
|
|
|
func (s *Service) handleQuery(_ *core.Core, q core.Query) core.Result {
|
2026-03-13 14:34:30 +00:00
|
|
|
switch q.(type) {
|
|
|
|
|
case QueryList:
|
2026-03-31 16:14:19 +01:00
|
|
|
return core.Result{Value: s.queryList(), OK: true}
|
2026-03-13 14:34:30 +00:00
|
|
|
default:
|
2026-03-31 16:14:19 +01:00
|
|
|
return core.Result{}
|
2026-03-13 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) queryList() []BindingInfo {
|
2026-03-31 05:13:43 +00:00
|
|
|
result := make([]BindingInfo, 0, len(s.registeredBindings))
|
|
|
|
|
for _, info := range s.registeredBindings {
|
2026-03-13 14:34:30 +00:00
|
|
|
result = append(result, info)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) taskAdd(t TaskAdd) error {
|
2026-03-31 05:13:43 +00:00
|
|
|
if _, exists := s.registeredBindings[t.Accelerator]; exists {
|
|
|
|
|
return ErrorAlreadyRegistered
|
2026-03-13 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register on platform with a callback that broadcasts ActionTriggered
|
|
|
|
|
err := s.platform.Add(t.Accelerator, func() {
|
|
|
|
|
_ = s.Core().ACTION(ActionTriggered{Accelerator: t.Accelerator})
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
fix(dx): use coreerr.E() and go-io, update CLAUDE.md, add tests
- Replace 90+ fmt.Errorf calls with coreerr.E() from go-log across
display, window, systray, keybinding, contextmenu, and mcp packages
- Replace os.ReadFile/WriteFile/MkdirAll with coreio.Local in
window/layout.go and window/state.go
- Update CLAUDE.md: fix key files table for new package structure,
document error handling and file I/O conventions, add missing deps
- Add 37 tests for window package (task handlers, persistence,
tiling modes, snap positions, workflow layouts)
- Window coverage: 47.1% → 69.8%
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-17 09:05:35 +00:00
|
|
|
return coreerr.E("keybinding.taskAdd", "platform add failed", err)
|
2026-03-13 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 05:13:43 +00:00
|
|
|
s.registeredBindings[t.Accelerator] = BindingInfo{
|
2026-03-13 14:34:30 +00:00
|
|
|
Accelerator: t.Accelerator,
|
|
|
|
|
Description: t.Description,
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) taskRemove(t TaskRemove) error {
|
2026-03-31 05:13:43 +00:00
|
|
|
if _, exists := s.registeredBindings[t.Accelerator]; !exists {
|
2026-03-31 14:55:01 +01:00
|
|
|
return coreerr.E("keybinding.taskRemove", "not registered: "+t.Accelerator, ErrorNotRegistered)
|
2026-03-13 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := s.platform.Remove(t.Accelerator)
|
|
|
|
|
if err != nil {
|
fix(dx): use coreerr.E() and go-io, update CLAUDE.md, add tests
- Replace 90+ fmt.Errorf calls with coreerr.E() from go-log across
display, window, systray, keybinding, contextmenu, and mcp packages
- Replace os.ReadFile/WriteFile/MkdirAll with coreio.Local in
window/layout.go and window/state.go
- Update CLAUDE.md: fix key files table for new package structure,
document error handling and file I/O conventions, add missing deps
- Add 37 tests for window package (task handlers, persistence,
tiling modes, snap positions, workflow layouts)
- Window coverage: 47.1% → 69.8%
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-17 09:05:35 +00:00
|
|
|
return coreerr.E("keybinding.taskRemove", "platform remove failed", err)
|
2026-03-13 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 05:13:43 +00:00
|
|
|
delete(s.registeredBindings, t.Accelerator)
|
2026-03-13 14:34:30 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-03-31 14:55:01 +01:00
|
|
|
|
|
|
|
|
// taskProcess triggers the registered handler for the given accelerator programmatically.
|
|
|
|
|
// Broadcasts ActionTriggered if handled; returns ErrorNotRegistered if the accelerator is unknown.
|
|
|
|
|
//
|
2026-03-31 16:14:19 +01:00
|
|
|
// c.Action("keybinding.process").Run(ctx, core.NewOptions(core.Option{Key:"task", Value:keybinding.TaskProcess{Accelerator:"Ctrl+S"}}))
|
2026-03-31 14:55:01 +01:00
|
|
|
func (s *Service) taskProcess(t TaskProcess) error {
|
|
|
|
|
if _, exists := s.registeredBindings[t.Accelerator]; !exists {
|
|
|
|
|
return coreerr.E("keybinding.taskProcess", "not registered: "+t.Accelerator, ErrorNotRegistered)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handled := s.platform.Process(t.Accelerator)
|
|
|
|
|
if !handled {
|
|
|
|
|
return coreerr.E("keybinding.taskProcess", "platform did not handle: "+t.Accelerator, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|