153 lines
5.8 KiB
Go
153 lines
5.8 KiB
Go
// pkg/window/wails.go
|
|
package window
|
|
|
|
import (
|
|
"github.com/wailsapp/wails/v3/pkg/application"
|
|
"github.com/wailsapp/wails/v3/pkg/events"
|
|
)
|
|
|
|
// WailsPlatform implements Platform using Wails v3.
|
|
type WailsPlatform struct {
|
|
app *application.App
|
|
}
|
|
|
|
// NewWailsPlatform returns a Platform backed by a Wails app.
|
|
func NewWailsPlatform(app *application.App) *WailsPlatform {
|
|
return &WailsPlatform{app: app}
|
|
}
|
|
|
|
func (platform *WailsPlatform) CreateWindow(options PlatformWindowOptions) PlatformWindow {
|
|
windowOptions := application.WebviewWindowOptions{
|
|
Name: options.Name,
|
|
Title: options.Title,
|
|
URL: options.URL,
|
|
Width: options.Width,
|
|
Height: options.Height,
|
|
X: options.X,
|
|
Y: options.Y,
|
|
MinWidth: options.MinWidth,
|
|
MinHeight: options.MinHeight,
|
|
MaxWidth: options.MaxWidth,
|
|
MaxHeight: options.MaxHeight,
|
|
Frameless: options.Frameless,
|
|
Hidden: options.Hidden,
|
|
AlwaysOnTop: options.AlwaysOnTop,
|
|
DisableResize: options.DisableResize,
|
|
EnableFileDrop: options.EnableFileDrop,
|
|
BackgroundColour: application.NewRGBA(options.BackgroundColour[0], options.BackgroundColour[1], options.BackgroundColour[2], options.BackgroundColour[3]),
|
|
}
|
|
windowHandle := platform.app.Window.NewWithOptions(windowOptions)
|
|
return &wailsWindow{w: windowHandle, title: options.Title}
|
|
}
|
|
|
|
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 platformWindows
|
|
}
|
|
|
|
// wailsWindow wraps *application.WebviewWindow to implement PlatformWindow.
|
|
// It stores the title locally because Wails v3 does not expose a title getter.
|
|
type wailsWindow struct {
|
|
w *application.WebviewWindow
|
|
title string
|
|
}
|
|
|
|
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) IsMinimised() bool { return windowHandle.w.IsMinimised() }
|
|
func (windowHandle *wailsWindow) IsVisible() bool { return windowHandle.w.IsVisible() }
|
|
func (windowHandle *wailsWindow) IsFocused() bool { return windowHandle.w.IsFocused() }
|
|
func (windowHandle *wailsWindow) SetTitle(title string) {
|
|
windowHandle.title = title
|
|
windowHandle.w.SetTitle(title)
|
|
}
|
|
func (windowHandle *wailsWindow) SetBounds(x, y, width, height int) {
|
|
windowHandle.w.SetPosition(x, y)
|
|
windowHandle.w.SetSize(width, height)
|
|
}
|
|
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 {
|
|
windowHandle.w.Show()
|
|
} else {
|
|
windowHandle.w.Hide()
|
|
}
|
|
}
|
|
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 (windowHandle *wailsWindow) OnWindowEvent(handler func(event WindowEvent)) {
|
|
name := windowHandle.w.Name()
|
|
|
|
// Map common Wails window events to our WindowEvent type.
|
|
windowEventMap := map[events.WindowEventType]string{
|
|
events.Common.WindowFocus: "focus",
|
|
events.Common.WindowLostFocus: "blur",
|
|
events.Common.WindowDidMove: "move",
|
|
events.Common.WindowDidResize: "resize",
|
|
events.Common.WindowClosing: "close",
|
|
}
|
|
|
|
for eventType, eventName := range windowEventMap {
|
|
mappedEventName := eventName // capture for closure
|
|
windowHandle.w.OnWindowEvent(eventType, func(event *application.WindowEvent) {
|
|
data := make(map[string]any)
|
|
switch mappedEventName {
|
|
case "move":
|
|
x, y := windowHandle.w.Position()
|
|
data["x"] = x
|
|
data["y"] = y
|
|
case "resize":
|
|
width, height := windowHandle.w.Size()
|
|
data["width"] = width
|
|
data["height"] = height
|
|
}
|
|
handler(WindowEvent{
|
|
Type: mappedEventName,
|
|
Name: name,
|
|
Data: data,
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
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 := ""
|
|
if details != nil {
|
|
targetID = details.ElementID
|
|
}
|
|
handler(files, targetID)
|
|
})
|
|
}
|
|
|
|
// Ensure wailsWindow satisfies PlatformWindow at compile time.
|
|
var _ PlatformWindow = (*wailsWindow)(nil)
|
|
|
|
// Ensure WailsPlatform satisfies Platform at compile time.
|
|
var _ Platform = (*WailsPlatform)(nil)
|