package main import ( "embed" "fmt" "log" "math/rand" "runtime" "strconv" "time" "github.com/samber/lo" "github.com/wailsapp/wails/v3/pkg/events" "github.com/wailsapp/wails/v3/pkg/application" ) // This is a stub for non-windows platforms var getExStyle = func() int { return 0 } //go:embed assets/* var assets embed.FS type WindowService struct{} // ============================================== func (s *WindowService) SetPos(relative bool, x, y float64) { win := application.Get().CurrentWindow() initX, initY := win.Position() if relative { x += float64(initX) y += float64(initY) } win.SetPosition(int(x), int(y)) currentX, currentY := win.Position() fmt.Printf("SetPos: %d, %d => %d, %d\n", initX, initY, currentX, currentY) } func (s *WindowService) SetSize(relative bool, wdt, hgt float64) { win := application.Get().CurrentWindow() initW, initH := win.Size() if relative { wdt += float64(initW) hgt += float64(initH) } win.SetSize(int(wdt), int(hgt)) currentW, currentH := win.Size() fmt.Printf("SetSize: %d, %d => %d, %d\n", initW, initH, currentW, currentH) } func (s *WindowService) SetBounds(x, y, w, h float64) { win := application.Get().CurrentWindow() initR := win.Bounds() win.SetBounds(application.Rect{ X: int(x), Y: int(y), Width: int(w), Height: int(h), }) currentR := win.Bounds() fmt.Printf("SetBounds: %+v => %+v\n", initR, currentR) } func (s *WindowService) GetBounds() application.Rect { win := application.Get().CurrentWindow() r := win.Bounds() mid := r.X + (r.Width-1)/2 fmt.Printf("GetBounds: %+v: mid: %d\n", r, mid) return r } // ============================================== func main() { app := application.New(application.Options{ Name: "WebviewWindow Demo", Description: "A demo of the WebviewWindow API", Assets: application.AssetOptions{ Handler: application.BundledAssetFileServer(assets), }, Mac: application.MacOptions{ ApplicationShouldTerminateAfterLastWindowClosed: false, }, Services: []application.Service{ application.NewService(&WindowService{}), }, }) app.OnApplicationEvent(events.Common.ApplicationStarted, func(event *application.ApplicationEvent) { log.Println("ApplicationDidFinishLaunching") }) var hiddenWindows []*application.WebviewWindow currentWindow := func(fn func(window *application.WebviewWindow)) { if app.CurrentWindow() != nil { fn(app.CurrentWindow()) } else { println("Current WebviewWindow is nil") } } // Create a custom menu menu := app.NewMenu() if runtime.GOOS == "darwin" { menu.AddRole(application.AppMenu) } else { menu.AddRole(application.FileMenu) } windowCounter := 1 // Let's make a "Demo" menu myMenu := menu.AddSubmenu("New") myMenu.Add("New WebviewWindow"). SetAccelerator("CmdOrCtrl+N"). OnClick(func(ctx *application.Context) { app.NewWebviewWindow(). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) if runtime.GOOS != "linux" { myMenu.Add("New WebviewWindow (Disable Minimise)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ MinimiseButtonState: application.ButtonDisabled, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (Disable Maximise)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ MaximiseButtonState: application.ButtonDisabled, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (Hide Minimise)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ MinimiseButtonState: application.ButtonHidden, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (Always on top)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ AlwaysOnTop: true, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (Hide Maximise)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ MaximiseButtonState: application.ButtonHidden, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (Centered)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ MaximiseButtonState: application.ButtonHidden, InitialPosition: application.WindowCentered, }). SetTitle("WebviewWindow " + strconv.Itoa(windowCounter)). SetURL("https://wails.io"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (Position 100,100)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ MaximiseButtonState: application.ButtonHidden, X: 100, Y: 100, InitialPosition: application.WindowXY, }). SetTitle("WebviewWindow " + strconv.Itoa(windowCounter)). SetURL("https://wails.io"). Show() windowCounter++ }) } if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { myMenu.Add("New WebviewWindow (Disable Close)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ CloseButtonState: application.ButtonDisabled, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (Hide Close)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ CloseButtonState: application.ButtonHidden, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) } if runtime.GOOS == "windows" { myMenu.Add("New WebviewWindow (Custom ExStyle)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ Windows: application.WindowsWindow{ ExStyle: getExStyle(), }, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) } myMenu.Add("New WebviewWindow (Listen to Move)"). OnClick(func(ctx *application.Context) { w := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{}). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() w.OnWindowEvent(events.Common.WindowDidMove, func(event *application.WindowEvent) { x, y := w.Position() fmt.Printf("WindowDidMove event triggered. New position: (%d, %d)\n", x, y) }) windowCounter++ }) myMenu.Add("New WebviewWindow (Listen to Resize)"). OnClick(func(ctx *application.Context) { w := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{}). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() w.OnWindowEvent(events.Common.WindowDidResize, func(event *application.WindowEvent) { width, height := w.Size() fmt.Printf("WindowDidResize event triggered. New size: (%d, %d)\n", width, height) }) windowCounter++ }) myMenu.Add("New WebviewWindow (Hides on Close one time)"). SetAccelerator("CmdOrCtrl+H"). OnClick(func(ctx *application.Context) { w := app.NewWebviewWindow() w.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) { if !lo.Contains(hiddenWindows, w) { hiddenWindows = append(hiddenWindows, w) go func() { time.Sleep(5 * time.Second) w.Show() }() w.Hide() e.Cancel() } // Remove the window from the hiddenWindows list hiddenWindows = lo.Without(hiddenWindows, w) }) w.SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetURL("https://wails.io"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (Frameless)"). SetAccelerator("CmdOrCtrl+F"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ X: rand.Intn(1000), Y: rand.Intn(800), BackgroundColour: application.NewRGB(33, 37, 41), Frameless: true, Mac: application.MacWindow{ InvisibleTitleBarHeight: 50, }, }).Show() windowCounter++ }) myMenu.Add("New WebviewWindow (Ignores mouse events)"). SetAccelerator("CmdOrCtrl+F"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ HTML: "
", X: rand.Intn(1000), Y: rand.Intn(800), IgnoreMouseEvents: true, BackgroundType: application.BackgroundTypeTransparent, Mac: application.MacWindow{ InvisibleTitleBarHeight: 50, }, }).Show() windowCounter++ }) if runtime.GOOS == "darwin" { myMenu.Add("New WebviewWindow (MacTitleBarHiddenInset)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ Mac: application.MacWindow{ TitleBar: application.MacTitleBarHiddenInset, InvisibleTitleBarHeight: 25, }, }). SetBackgroundColour(application.NewRGB(33, 37, 41)). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetHTML("A MacTitleBarHiddenInset WebviewWindow example
"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (MacTitleBarHiddenInsetUnified)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ Mac: application.MacWindow{ TitleBar: application.MacTitleBarHiddenInsetUnified, InvisibleTitleBarHeight: 50, }, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetHTML("A MacTitleBarHiddenInsetUnified WebviewWindow example
"). Show() windowCounter++ }) myMenu.Add("New WebviewWindow (MacTitleBarHidden)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ Mac: application.MacWindow{ TitleBar: application.MacTitleBarHidden, InvisibleTitleBarHeight: 25, }, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetHTML("A MacTitleBarHidden WebviewWindow example
"). Show() windowCounter++ }) } if runtime.GOOS == "windows" { myMenu.Add("New WebviewWindow (Mica)"). OnClick(func(ctx *application.Context) { app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ Title: "WebviewWindow " + strconv.Itoa(windowCounter), X: rand.Intn(1000), Y: rand.Intn(800), BackgroundType: application.BackgroundTypeTranslucent, HTML: `