2026-04-02 19:50:55 +00:00
|
|
|
// pkg/systray/service.go
|
2026-03-13 13:26:18 +00:00
|
|
|
package systray
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"forge.lthn.ai/core/go/pkg/core"
|
2026-04-02 13:03:55 +00:00
|
|
|
"forge.lthn.ai/core/gui/pkg/notification"
|
2026-03-13 13:26:18 +00:00
|
|
|
)
|
|
|
|
|
|
2026-04-02 14:13:58 +00:00
|
|
|
// Options configures the systray service.
|
2026-04-02 20:42:42 +00:00
|
|
|
// Use: core.WithService(systray.Register(platform))
|
2026-03-13 13:26:18 +00:00
|
|
|
type Options struct{}
|
|
|
|
|
|
2026-04-02 14:13:58 +00:00
|
|
|
// Service manages system tray operations via Core tasks.
|
2026-04-02 20:36:02 +00:00
|
|
|
// Use: svc := &systray.Service{}
|
2026-03-13 13:26:18 +00:00
|
|
|
type Service struct {
|
|
|
|
|
*core.ServiceRuntime[Options]
|
|
|
|
|
manager *Manager
|
|
|
|
|
platform Platform
|
2026-03-13 13:54:28 +00:00
|
|
|
iconPath string
|
2026-03-13 13:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 14:13:58 +00:00
|
|
|
// OnStartup loads tray config and registers task handlers.
|
2026-04-02 20:36:02 +00:00
|
|
|
// Use: _ = svc.OnStartup(context.Background())
|
2026-03-13 13:26:18 +00:00
|
|
|
func (s *Service) OnStartup(ctx context.Context) error {
|
|
|
|
|
cfg, handled, _ := s.Core().QUERY(QueryConfig{})
|
|
|
|
|
if handled {
|
|
|
|
|
if tCfg, ok := cfg.(map[string]any); ok {
|
|
|
|
|
s.applyConfig(tCfg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s.Core().RegisterTask(s.handleTask)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) applyConfig(cfg map[string]any) {
|
|
|
|
|
tooltip, _ := cfg["tooltip"].(string)
|
|
|
|
|
if tooltip == "" {
|
|
|
|
|
tooltip = "Core"
|
|
|
|
|
}
|
|
|
|
|
_ = s.manager.Setup(tooltip, tooltip)
|
2026-03-13 13:54:28 +00:00
|
|
|
|
|
|
|
|
if iconPath, ok := cfg["icon"].(string); ok && iconPath != "" {
|
|
|
|
|
// Icon loading is deferred to when assets are available.
|
|
|
|
|
// Store the path for later use.
|
|
|
|
|
s.iconPath = iconPath
|
|
|
|
|
}
|
2026-03-13 13:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 14:13:58 +00:00
|
|
|
// HandleIPCEvents satisfies Core's IPC hook.
|
2026-03-13 13:26:18 +00:00
|
|
|
func (s *Service) HandleIPCEvents(c *core.Core, msg core.Message) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) handleTask(c *core.Core, t core.Task) (any, bool, error) {
|
|
|
|
|
switch t := t.(type) {
|
|
|
|
|
case TaskSetTrayIcon:
|
|
|
|
|
return nil, true, s.manager.SetIcon(t.Data)
|
2026-04-02 13:39:36 +00:00
|
|
|
case TaskSetTooltip:
|
|
|
|
|
return nil, true, s.manager.SetTooltip(t.Tooltip)
|
|
|
|
|
case TaskSetLabel:
|
|
|
|
|
return nil, true, s.manager.SetLabel(t.Label)
|
2026-03-13 13:26:18 +00:00
|
|
|
case TaskSetTrayMenu:
|
|
|
|
|
return nil, true, s.taskSetTrayMenu(t)
|
|
|
|
|
case TaskShowPanel:
|
2026-04-02 18:59:54 +00:00
|
|
|
return nil, true, s.manager.ShowPanel()
|
2026-03-13 13:26:18 +00:00
|
|
|
case TaskHidePanel:
|
2026-04-02 18:59:54 +00:00
|
|
|
return nil, true, s.manager.HidePanel()
|
2026-04-02 13:03:55 +00:00
|
|
|
case TaskShowMessage:
|
2026-04-02 19:50:55 +00:00
|
|
|
return nil, true, s.showTrayMessage(t.Title, t.Message)
|
2026-03-13 13:26:18 +00:00
|
|
|
default:
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) taskSetTrayMenu(t TaskSetTrayMenu) error {
|
|
|
|
|
// Register IPC-emitting callbacks for each menu item
|
|
|
|
|
for _, item := range t.Items {
|
|
|
|
|
if item.ActionID != "" {
|
|
|
|
|
actionID := item.ActionID
|
|
|
|
|
s.manager.RegisterCallback(actionID, func() {
|
|
|
|
|
_ = s.Core().ACTION(ActionTrayMenuItemClicked{ActionID: actionID})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return s.manager.SetMenu(t.Items)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 19:50:55 +00:00
|
|
|
func (s *Service) showTrayMessage(title, message string) error {
|
2026-04-02 13:03:55 +00:00
|
|
|
if s.manager == nil || !s.manager.IsActive() {
|
|
|
|
|
_, _, err := s.Core().PERFORM(notification.TaskSend{
|
|
|
|
|
Opts: notification.NotificationOptions{Title: title, Message: message},
|
|
|
|
|
})
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
tray := s.manager.Tray()
|
|
|
|
|
if tray == nil {
|
2026-04-02 19:50:55 +00:00
|
|
|
return core.E("systray.showTrayMessage", "tray not initialised", nil)
|
2026-04-02 13:03:55 +00:00
|
|
|
}
|
|
|
|
|
if messenger, ok := tray.(interface{ ShowMessage(title, message string) }); ok {
|
|
|
|
|
messenger.ShowMessage(title, message)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
_, _, err := s.Core().PERFORM(notification.TaskSend{
|
|
|
|
|
Opts: notification.NotificationOptions{Title: title, Message: message},
|
|
|
|
|
})
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 13:26:18 +00:00
|
|
|
// Manager returns the underlying systray Manager.
|
2026-04-02 20:36:02 +00:00
|
|
|
// Use: manager := svc.Manager()
|
2026-03-13 13:26:18 +00:00
|
|
|
func (s *Service) Manager() *Manager {
|
|
|
|
|
return s.manager
|
|
|
|
|
}
|