gui/pkg/environment/service.go
Snider 13a493f57d
Some checks failed
Security Scan / security (push) Failing after 26s
Test / test (push) Failing after 56s
refactor(ax): AX compliance sweep — comments, error handling, test names
- Fix window/service.go: replace 3 fmt.Errorf calls with coreerr.E() (removes implicit fmt dependency)
- Add usage-example comments to all bare Register() functions across 10 packages
- Remove redundant prose comments (Options/Service/Register/OnStartup/HandleIPCEvents boilerplate)
- Add Result-type comments to message types in contextmenu, keybinding, notification packages
- Fix test naming to TestFilename_Function_{Good,Bad,Ugly} pattern in display_test, window_test, persistence_test, service_screen_test
- Convert New() and CreateWindowOptions doc comments to usage-example style

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 12:18:41 +01:00

76 lines
1.8 KiB
Go

// pkg/environment/service.go
package environment
import (
"context"
"forge.lthn.ai/core/go/pkg/core"
)
type Options struct{}
type Service struct {
*core.ServiceRuntime[Options]
platform Platform
cancelTheme func() // returned by Platform.OnThemeChange — called on shutdown
}
// Register(p) binds the environment service to a Core instance.
// core.WithService(environment.Register(wailsEnvironment))
func Register(p Platform) func(*core.Core) (any, error) {
return func(c *core.Core) (any, error) {
return &Service{
ServiceRuntime: core.NewServiceRuntime[Options](c, Options{}),
platform: p,
}, nil
}
}
func (s *Service) OnStartup(ctx context.Context) error {
s.Core().RegisterQuery(s.handleQuery)
s.Core().RegisterTask(s.handleTask)
// Register theme change callback — broadcasts ActionThemeChanged via IPC
s.cancelTheme = s.platform.OnThemeChange(func(isDark bool) {
_ = s.Core().ACTION(ActionThemeChanged{IsDark: isDark})
})
return nil
}
func (s *Service) OnShutdown(ctx context.Context) error {
if s.cancelTheme != nil {
s.cancelTheme()
}
return nil
}
func (s *Service) HandleIPCEvents(c *core.Core, msg core.Message) error {
return nil
}
func (s *Service) handleQuery(c *core.Core, q core.Query) (any, bool, error) {
switch q.(type) {
case QueryTheme:
isDark := s.platform.IsDarkMode()
theme := "light"
if isDark {
theme = "dark"
}
return ThemeInfo{IsDark: isDark, Theme: theme}, true, nil
case QueryInfo:
return s.platform.Info(), true, nil
case QueryAccentColour:
return s.platform.AccentColour(), true, nil
default:
return nil, false, nil
}
}
func (s *Service) handleTask(c *core.Core, t core.Task) (any, bool, error) {
switch t := t.(type) {
case TaskOpenFileManager:
return nil, true, s.platform.OpenFileManager(t.Path, t.Select)
default:
return nil, false, nil
}
}