From 761b3b0784fb776f23ac4d9df6187987c1528cd1 Mon Sep 17 00:00:00 2001 From: Virgil Date: Tue, 31 Mar 2026 08:14:14 +0000 Subject: [PATCH] refactor(ax): clarify window adapter naming and docs --- pkg/display/display.go | 14 +++--- pkg/window/messages.go | 5 +- pkg/window/service.go | 2 +- pkg/window/wails.go | 105 +++++++++++++++++++++-------------------- 4 files changed, 66 insertions(+), 60 deletions(-) diff --git a/pkg/display/display.go b/pkg/display/display.go index 2124571..292141b 100644 --- a/pkg/display/display.go +++ b/pkg/display/display.go @@ -68,12 +68,12 @@ func Register(wailsApp *application.App) func(*core.Core) (any, error) { } // OnStartup loads config and registers IPC handlers synchronously. -// CRITICAL: config handlers MUST be registered before returning — -// sub-services depend on them during their own OnStartup. +// CRITICAL: config handlers MUST be registered before returning. +// Sub-services depend on them during their own OnStartup. func (s *Service) OnStartup(ctx context.Context) error { s.loadConfig() - // Register config query/task handlers — available NOW for sub-services + // Register config query/task handlers. Available now for sub-services. s.Core().RegisterQuery(s.handleConfigQuery) s.Core().RegisterTask(s.handleConfigTask) @@ -86,12 +86,12 @@ func (s *Service) OnStartup(ctx context.Context) error { return nil } -// HandleIPCEvents is auto-discovered and registered by core.WithService. -// It bridges sub-service IPC actions to WebSocket events for TS apps. +// core.WithService auto-registers this handler to bridge sub-service IPC +// actions to WebSocket events for TS apps. func (s *Service) HandleIPCEvents(c *core.Core, msg core.Message) error { switch m := msg.(type) { case core.ActionServiceStartup: - // All services have completed OnStartup — safe to PERFORM on sub-services + // All services have completed OnStartup. Safe to PERFORM on sub-services. s.buildMenu() s.setupTray() case window.ActionWindowOpened: @@ -520,7 +520,7 @@ func (s *Service) loadConfig() { func (s *Service) loadConfigFrom(path string) { configFile, err := config.New(config.WithPath(path)) if err != nil { - // Non-critical — continue with empty configData + // Non-critical: continue with empty configData return } s.configFile = configFile diff --git a/pkg/window/messages.go b/pkg/window/messages.go index bda6185..3efe0c2 100644 --- a/pkg/window/messages.go +++ b/pkg/window/messages.go @@ -19,6 +19,7 @@ type QueryConfig struct{} type QuerySavedWindowStates struct{} +// Example: c.PERFORM(TaskOpenWindow{Window: Window{Name: "editor", URL: "/"}}) type TaskOpenWindow struct { Window Window } @@ -35,10 +36,10 @@ type TaskSetSize struct { Width, Height int } -// TaskMaximize maximizes a named window. +// Example: c.PERFORM(TaskMaximize{Name: "editor"}) type TaskMaximize struct{ Name string } -// TaskMinimize minimizes a named window. +// Example: c.PERFORM(TaskMinimize{Name: "editor"}) type TaskMinimize struct{ Name string } type TaskFocus struct{ Name string } diff --git a/pkg/window/service.go b/pkg/window/service.go index 3a59e9c..be5bce5 100644 --- a/pkg/window/service.go +++ b/pkg/window/service.go @@ -17,7 +17,7 @@ type Service struct { } func (s *Service) OnStartup(ctx context.Context) error { - // Query config — display registers its handler before us (registration order guarantee). + // Query config - display registers its handler before us (registration order guarantee). // If display is not registered, handled=false and we skip config. configValue, handled, _ := s.Core().QUERY(QueryConfig{}) if handled { diff --git a/pkg/window/wails.go b/pkg/window/wails.go index a2587ce..3beb7e0 100644 --- a/pkg/window/wails.go +++ b/pkg/window/wails.go @@ -11,13 +11,13 @@ type WailsPlatform struct { app *application.App } -// NewWailsPlatform creates a Wails-backed Platform. +// NewWailsPlatform returns a Platform backed by a Wails app. func NewWailsPlatform(app *application.App) *WailsPlatform { return &WailsPlatform{app: app} } -func (wp *WailsPlatform) CreateWindow(options PlatformWindowOptions) PlatformWindow { - wOpts := application.WebviewWindowOptions{ +func (platform *WailsPlatform) CreateWindow(options PlatformWindowOptions) PlatformWindow { + windowOptions := application.WebviewWindowOptions{ Name: options.Name, Title: options.Title, URL: options.URL, @@ -36,19 +36,19 @@ func (wp *WailsPlatform) CreateWindow(options PlatformWindowOptions) PlatformWin EnableFileDrop: options.EnableFileDrop, BackgroundColour: application.NewRGBA(options.BackgroundColour[0], options.BackgroundColour[1], options.BackgroundColour[2], options.BackgroundColour[3]), } - w := wp.app.Window.NewWithOptions(wOpts) - return &wailsWindow{w: w, title: options.Title} + windowHandle := platform.app.Window.NewWithOptions(windowOptions) + return &wailsWindow{w: windowHandle, title: options.Title} } -func (wp *WailsPlatform) GetWindows() []PlatformWindow { - all := wp.app.Window.GetAll() - out := make([]PlatformWindow, 0, len(all)) - for _, w := range all { - if wv, ok := w.(*application.WebviewWindow); ok { - out = append(out, &wailsWindow{w: wv}) +func (platform *WailsPlatform) GetWindows() []PlatformWindow { + allWindows := platform.app.Window.GetAll() + platformWindows := make([]PlatformWindow, 0, len(allWindows)) + for _, window := range allWindows { + if windowHandle, ok := window.(*application.WebviewWindow); ok { + platformWindows = append(platformWindows, &wailsWindow{w: windowHandle}) } } - return out + return platformWindows } // wailsWindow wraps *application.WebviewWindow to implement PlatformWindow. @@ -58,41 +58,46 @@ type wailsWindow struct { title string } -func (ww *wailsWindow) Name() string { return ww.w.Name() } -func (ww *wailsWindow) Title() string { return ww.title } -func (ww *wailsWindow) Position() (int, int) { return ww.w.Position() } -func (ww *wailsWindow) Size() (int, int) { return ww.w.Size() } -func (ww *wailsWindow) IsMaximised() bool { return ww.w.IsMaximised() } -func (ww *wailsWindow) IsFocused() bool { return ww.w.IsFocused() } -func (ww *wailsWindow) SetTitle(title string) { ww.title = title; ww.w.SetTitle(title) } -func (ww *wailsWindow) SetPosition(x, y int) { ww.w.SetPosition(x, y) } -func (ww *wailsWindow) SetSize(width, height int) { ww.w.SetSize(width, height) } -func (ww *wailsWindow) SetBackgroundColour(r, g, b, a uint8) { - ww.w.SetBackgroundColour(application.NewRGBA(r, g, b, a)) +func (windowHandle *wailsWindow) Name() string { return windowHandle.w.Name() } +func (windowHandle *wailsWindow) Title() string { return windowHandle.title } +func (windowHandle *wailsWindow) Position() (int, int) { return windowHandle.w.Position() } +func (windowHandle *wailsWindow) Size() (int, int) { return windowHandle.w.Size() } +func (windowHandle *wailsWindow) IsMaximised() bool { return windowHandle.w.IsMaximised() } +func (windowHandle *wailsWindow) IsFocused() bool { return windowHandle.w.IsFocused() } +func (windowHandle *wailsWindow) SetTitle(title string) { + windowHandle.title = title + windowHandle.w.SetTitle(title) } -func (ww *wailsWindow) SetVisibility(visible bool) { +func (windowHandle *wailsWindow) SetPosition(x, y int) { windowHandle.w.SetPosition(x, y) } +func (windowHandle *wailsWindow) SetSize(width, height int) { windowHandle.w.SetSize(width, height) } +func (windowHandle *wailsWindow) SetBackgroundColour(r, g, b, a uint8) { + windowHandle.w.SetBackgroundColour(application.NewRGBA(r, g, b, a)) +} +func (windowHandle *wailsWindow) SetVisibility(visible bool) { if visible { - ww.w.Show() + windowHandle.w.Show() } else { - ww.w.Hide() + windowHandle.w.Hide() } } -func (ww *wailsWindow) SetAlwaysOnTop(alwaysOnTop bool) { ww.w.SetAlwaysOnTop(alwaysOnTop) } -func (ww *wailsWindow) Maximise() { ww.w.Maximise() } -func (ww *wailsWindow) Restore() { ww.w.Restore() } -func (ww *wailsWindow) Minimise() { ww.w.Minimise() } -func (ww *wailsWindow) Focus() { ww.w.Focus() } -func (ww *wailsWindow) Close() { ww.w.Close() } -func (ww *wailsWindow) Show() { ww.w.Show() } -func (ww *wailsWindow) Hide() { ww.w.Hide() } -func (ww *wailsWindow) Fullscreen() { ww.w.Fullscreen() } -func (ww *wailsWindow) UnFullscreen() { ww.w.UnFullscreen() } +func (windowHandle *wailsWindow) SetAlwaysOnTop(alwaysOnTop bool) { + windowHandle.w.SetAlwaysOnTop(alwaysOnTop) +} +func (windowHandle *wailsWindow) Maximise() { windowHandle.w.Maximise() } +func (windowHandle *wailsWindow) Restore() { windowHandle.w.Restore() } +func (windowHandle *wailsWindow) Minimise() { windowHandle.w.Minimise() } +func (windowHandle *wailsWindow) Focus() { windowHandle.w.Focus() } +func (windowHandle *wailsWindow) Close() { windowHandle.w.Close() } +func (windowHandle *wailsWindow) Show() { windowHandle.w.Show() } +func (windowHandle *wailsWindow) Hide() { windowHandle.w.Hide() } +func (windowHandle *wailsWindow) Fullscreen() { windowHandle.w.Fullscreen() } +func (windowHandle *wailsWindow) UnFullscreen() { windowHandle.w.UnFullscreen() } -func (ww *wailsWindow) OnWindowEvent(handler func(event WindowEvent)) { - name := ww.w.Name() +func (windowHandle *wailsWindow) OnWindowEvent(handler func(event WindowEvent)) { + name := windowHandle.w.Name() // Map common Wails window events to our WindowEvent type. - eventMap := map[events.WindowEventType]string{ + windowEventMap := map[events.WindowEventType]string{ events.Common.WindowFocus: "focus", events.Common.WindowLostFocus: "blur", events.Common.WindowDidMove: "move", @@ -100,22 +105,22 @@ func (ww *wailsWindow) OnWindowEvent(handler func(event WindowEvent)) { events.Common.WindowClosing: "close", } - for eventType, eventName := range eventMap { - typeName := eventName // capture for closure - ww.w.OnWindowEvent(eventType, func(event *application.WindowEvent) { + for eventType, eventName := range windowEventMap { + mappedEventName := eventName // capture for closure + windowHandle.w.OnWindowEvent(eventType, func(event *application.WindowEvent) { data := make(map[string]any) - switch typeName { + switch mappedEventName { case "move": - x, y := ww.w.Position() + x, y := windowHandle.w.Position() data["x"] = x data["y"] = y case "resize": - w, h := ww.w.Size() - data["width"] = w - data["height"] = h + width, height := windowHandle.w.Size() + data["width"] = width + data["height"] = height } handler(WindowEvent{ - Type: typeName, + Type: mappedEventName, Name: name, Data: data, }) @@ -123,8 +128,8 @@ func (ww *wailsWindow) OnWindowEvent(handler func(event WindowEvent)) { } } -func (ww *wailsWindow) OnFileDrop(handler func(paths []string, targetID string)) { - ww.w.OnWindowEvent(events.Common.WindowFilesDropped, func(event *application.WindowEvent) { +func (windowHandle *wailsWindow) OnFileDrop(handler func(paths []string, targetID string)) { + windowHandle.w.OnWindowEvent(events.Common.WindowFilesDropped, func(event *application.WindowEvent) { files := event.Context().DroppedFiles() details := event.Context().DropTargetDetails() targetID := ""