2026-03-13 14:34:30 +00:00
|
|
|
// pkg/keybinding/service.go
|
|
|
|
|
package keybinding
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
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
|
|
|
coreerr "forge.lthn.ai/core/go-log"
|
2026-03-13 14:34:30 +00:00
|
|
|
"forge.lthn.ai/core/go/pkg/core"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) OnStartup(ctx context.Context) error {
|
|
|
|
|
s.Core().RegisterQuery(s.handleQuery)
|
|
|
|
|
s.Core().RegisterTask(s.handleTask)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) HandleIPCEvents(c *core.Core, msg core.Message) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Query Handlers ---
|
|
|
|
|
|
|
|
|
|
func (s *Service) handleQuery(c *core.Core, q core.Query) (any, bool, error) {
|
|
|
|
|
switch q.(type) {
|
|
|
|
|
case QueryList:
|
|
|
|
|
return s.queryList(), true, nil
|
|
|
|
|
default:
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Task Handlers ---
|
|
|
|
|
|
|
|
|
|
func (s *Service) handleTask(c *core.Core, t core.Task) (any, bool, error) {
|
|
|
|
|
switch t := t.(type) {
|
|
|
|
|
case TaskAdd:
|
|
|
|
|
return nil, true, s.taskAdd(t)
|
|
|
|
|
case TaskRemove:
|
|
|
|
|
return nil, true, s.taskRemove(t)
|
|
|
|
|
default:
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
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", "not registered: "+t.Accelerator, nil)
|
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
|
|
|
|
|
}
|