refactor(ax): clarify window adapter naming and docs
This commit is contained in:
parent
02b7fa9230
commit
761b3b0784
4 changed files with 66 additions and 60 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 := ""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue