Some checks failed
Security Scan / security (push) Failing after 25s
- Import paths: forge.lthn.ai/core/go → dappco.re/go/core
- Import paths: forge.lthn.ai/core/go-log → dappco.re/go/core/log
- Import paths: forge.lthn.ai/core/go-io → dappco.re/go/core/io
- RegisterTask → c.Action("name", handler) across all 15 services
- QueryHandler signature: (any, bool, error) → core.Result
- PERFORM(task) → Action.Run(ctx, opts)
- QUERY returns single core.Result (not 3 values)
- All 17 packages build and test clean on v0.8.0-alpha.1
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
108 lines
3.2 KiB
Go
108 lines
3.2 KiB
Go
// pkg/keybinding/service.go
|
|
package keybinding
|
|
|
|
import (
|
|
"context"
|
|
|
|
coreerr "dappco.re/go/core/log"
|
|
core "dappco.re/go/core"
|
|
)
|
|
|
|
type Options struct{}
|
|
|
|
type Service struct {
|
|
*core.ServiceRuntime[Options]
|
|
platform Platform
|
|
registeredBindings map[string]BindingInfo
|
|
}
|
|
|
|
func (s *Service) OnStartup(_ context.Context) core.Result {
|
|
s.Core().RegisterQuery(s.handleQuery)
|
|
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}
|
|
}
|
|
|
|
func (s *Service) HandleIPCEvents(_ *core.Core, _ core.Message) core.Result {
|
|
return core.Result{OK: true}
|
|
}
|
|
|
|
// --- Query Handlers ---
|
|
|
|
func (s *Service) handleQuery(_ *core.Core, q core.Query) core.Result {
|
|
switch q.(type) {
|
|
case QueryList:
|
|
return core.Result{Value: s.queryList(), OK: true}
|
|
default:
|
|
return core.Result{}
|
|
}
|
|
}
|
|
|
|
func (s *Service) queryList() []BindingInfo {
|
|
result := make([]BindingInfo, 0, len(s.registeredBindings))
|
|
for _, info := range s.registeredBindings {
|
|
result = append(result, info)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (s *Service) taskAdd(t TaskAdd) error {
|
|
if _, exists := s.registeredBindings[t.Accelerator]; exists {
|
|
return ErrorAlreadyRegistered
|
|
}
|
|
|
|
// 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 {
|
|
return coreerr.E("keybinding.taskAdd", "platform add failed", err)
|
|
}
|
|
|
|
s.registeredBindings[t.Accelerator] = BindingInfo{
|
|
Accelerator: t.Accelerator,
|
|
Description: t.Description,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) taskRemove(t TaskRemove) error {
|
|
if _, exists := s.registeredBindings[t.Accelerator]; !exists {
|
|
return coreerr.E("keybinding.taskRemove", "not registered: "+t.Accelerator, ErrorNotRegistered)
|
|
}
|
|
|
|
err := s.platform.Remove(t.Accelerator)
|
|
if err != nil {
|
|
return coreerr.E("keybinding.taskRemove", "platform remove failed", err)
|
|
}
|
|
|
|
delete(s.registeredBindings, t.Accelerator)
|
|
return nil
|
|
}
|
|
|
|
// taskProcess triggers the registered handler for the given accelerator programmatically.
|
|
// Broadcasts ActionTriggered if handled; returns ErrorNotRegistered if the accelerator is unknown.
|
|
//
|
|
// c.Action("keybinding.process").Run(ctx, core.NewOptions(core.Option{Key:"task", Value:keybinding.TaskProcess{Accelerator:"Ctrl+S"}}))
|
|
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
|
|
}
|