From 2383bcd743a262a7a2f95579feccc927690a60f9 Mon Sep 17 00:00:00 2001 From: Snider Date: Fri, 13 Mar 2026 12:04:38 +0000 Subject: [PATCH] feat(window): add Platform and PlatformWindow interfaces Co-Authored-By: Claude Opus 4.6 --- pkg/window/mock_test.go | 66 ++++++++++++++++++++++++++++++++++++++++ pkg/window/platform.go | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 pkg/window/mock_test.go create mode 100644 pkg/window/platform.go diff --git a/pkg/window/mock_test.go b/pkg/window/mock_test.go new file mode 100644 index 0000000..1329772 --- /dev/null +++ b/pkg/window/mock_test.go @@ -0,0 +1,66 @@ +// pkg/window/mock_test.go +package window + +type mockPlatform struct { + windows []*mockWindow +} + +func newMockPlatform() *mockPlatform { + return &mockPlatform{} +} + +func (m *mockPlatform) CreateWindow(opts PlatformWindowOptions) PlatformWindow { + w := &mockWindow{ + name: opts.Name, title: opts.Title, url: opts.URL, + width: opts.Width, height: opts.Height, + x: opts.X, y: opts.Y, + } + m.windows = append(m.windows, w) + return w +} + +func (m *mockPlatform) GetWindows() []PlatformWindow { + out := make([]PlatformWindow, len(m.windows)) + for i, w := range m.windows { + out[i] = w + } + return out +} + +type mockWindow struct { + name, title, url string + width, height, x, y int + maximised, focused bool + visible, alwaysOnTop bool + closed bool + eventHandlers []func(WindowEvent) +} + +func (w *mockWindow) Name() string { return w.name } +func (w *mockWindow) Position() (int, int) { return w.x, w.y } +func (w *mockWindow) Size() (int, int) { return w.width, w.height } +func (w *mockWindow) IsMaximised() bool { return w.maximised } +func (w *mockWindow) IsFocused() bool { return w.focused } +func (w *mockWindow) SetTitle(title string) { w.title = title } +func (w *mockWindow) SetPosition(x, y int) { w.x = x; w.y = y } +func (w *mockWindow) SetSize(width, height int) { w.width = width; w.height = height } +func (w *mockWindow) SetBackgroundColour(r, g, b, a uint8) {} +func (w *mockWindow) SetVisibility(visible bool) { w.visible = visible } +func (w *mockWindow) SetAlwaysOnTop(alwaysOnTop bool) { w.alwaysOnTop = alwaysOnTop } +func (w *mockWindow) Maximise() { w.maximised = true } +func (w *mockWindow) Restore() { w.maximised = false } +func (w *mockWindow) Minimise() {} +func (w *mockWindow) Focus() { w.focused = true } +func (w *mockWindow) Close() { w.closed = true } +func (w *mockWindow) Show() { w.visible = true } +func (w *mockWindow) Hide() { w.visible = false } +func (w *mockWindow) Fullscreen() {} +func (w *mockWindow) UnFullscreen() {} +func (w *mockWindow) OnWindowEvent(handler func(WindowEvent)) { w.eventHandlers = append(w.eventHandlers, handler) } + +// emit fires a test event to all registered handlers. +func (w *mockWindow) emit(e WindowEvent) { + for _, h := range w.eventHandlers { + h(e) + } +} diff --git a/pkg/window/platform.go b/pkg/window/platform.go new file mode 100644 index 0000000..7fb665f --- /dev/null +++ b/pkg/window/platform.go @@ -0,0 +1,67 @@ +// pkg/window/platform.go +package window + +// Platform abstracts the windowing backend (Wails v3). +type Platform interface { + CreateWindow(opts PlatformWindowOptions) PlatformWindow + GetWindows() []PlatformWindow +} + +// PlatformWindowOptions are the backend-specific options passed to CreateWindow. +type PlatformWindowOptions struct { + Name string + Title string + URL string + Width, Height int + X, Y int + MinWidth, MinHeight int + MaxWidth, MaxHeight int + Frameless bool + Hidden bool + AlwaysOnTop bool + BackgroundColour [4]uint8 // RGBA + DisableResize bool + EnableDragAndDrop bool + Centered bool +} + +// PlatformWindow is a live window handle from the backend. +type PlatformWindow interface { + // Identity + Name() string + + // Queries + Position() (int, int) + Size() (int, int) + IsMaximised() bool + IsFocused() bool + + // Mutations + SetTitle(title string) + SetPosition(x, y int) + SetSize(width, height int) + SetBackgroundColour(r, g, b, a uint8) + SetVisibility(visible bool) + SetAlwaysOnTop(alwaysOnTop bool) + + // Window state + Maximise() + Restore() + Minimise() + Focus() + Close() + Show() + Hide() + Fullscreen() + UnFullscreen() + + // Events + OnWindowEvent(handler func(event WindowEvent)) +} + +// WindowEvent is emitted by the backend for window state changes. +type WindowEvent struct { + Type string // "focus", "blur", "move", "resize", "close" + Name string // window name + Data map[string]any +}