Files
openchamber-android-native/third_party/mygo/window.go
T

2497 lines
86 KiB
Go
Raw Normal View History

package mygo
import (
"cmp"
"context"
"errors"
"fmt"
"image/color"
"log"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/egoist/mygo/internal/platform"
"github.com/egoist/mygo/internal/surface"
)
// TitleBarStyle selects how the title bar of a framed window looks.
type TitleBarStyle string
// Title bar styles.
const (
TitleBarDefault TitleBarStyle = "default"
// TitleBarHidden gives the page the whole window while keeping the
// window controls, which sit over it (see WindowOptions.TitleBarStyle).
TitleBarHidden TitleBarStyle = "hidden"
// TitleBarHiddenInset is TitleBarHidden with the window controls inset
// further from the edges (macOS; TitleBarHidden elsewhere).
TitleBarHiddenInset TitleBarStyle = "hiddenInset"
)
// DevTools selects whether the web inspector is available.
type DevTools int
// DevTools modes.
const (
// DevToolsAuto enables the inspector unless the app was built for
// production with `mygo build`.
DevToolsAuto DevTools = iota
DevToolsEnabled
DevToolsDisabled
)
// Point is a position in screen coordinates.
type Point struct{ X, Y int }
// Size is a width and height in DIPs.
type Size struct{ Width, Height int }
// Rectangle is a position and size in screen coordinates (DIPs, origin at
// the top-left corner of the primary display).
type Rectangle struct{ X, Y, Width, Height int }
// WindowOptions configures NewWindow. The zero value is a visible,
// resizable 800x600 window centered on screen.
type WindowOptions struct {
// OwnScene opens the window as a window of the system's own on iPad, beside
// the app's others, which ios.multipleWindows in mygo.json allows. Elsewhere
// it's a window as any: on a phone or Android, over the one before.
OwnScene bool
// Title of the window. Defaults to the application name. The title
// follows the page's <title> unless an OnPageTitleUpdated listener
// prevents it.
Title string
// URL is loaded right after the window is created. "/" and other URLs
// without a scheme are pages of the app's frontend (see LoadURL).
// Page.SessionState, when set, takes its place.
URL string
// Width and Height of the window in DIPs (default 800x600).
Width, Height int
// UseContentSize makes Width and Height describe the page area instead
// of the whole window.
UseContentSize bool
// X and Y of the top-left corner. The window is centered when both are
// zero.
X, Y int
MinWidth, MinHeight int
MaxWidth, MaxHeight int
// FitContent sizes the page area to its page's content, and follows
// it as it changes, as FlyoutOptions.FitContent sizes a flyout: a
// panel showing a launcher's results, say. Width and Height are its
// size until the page tells its own. MinWidth, MinHeight, MaxWidth
// and MaxHeight bound it, and the work area of its display. It changes
// only when the content's size does, and only in the dimension that
// changed, so a size the user gives a resizable window holds until
// then, and not while the window is maximized or in full screen.
// Native UI (Content) does not fit.
FitContent bool
// Hidden creates the window without showing it. Call Show, typically
// from OnReadyToShow, to avoid a blank window while the page loads.
Hidden bool
// Frameless removes the title bar and window chrome. Mark draggable
// areas with the CSS `--app-region: drag`.
Frameless bool
// TitleBarStyle hides the title bar but keeps the window controls, so
// the page fills the window and draws its own title bar under them:
// the traffic lights on macOS, and minimize, maximize and close at the
// top corner on Linux and Windows. On Linux they are GTK's own title
// buttons, and the desktop's button layout decides which show and on
// which side, possibly none. Pages keep clear of them with the
// --mygo-titlebar-* CSS variables and drag the window by their title
// bar with --app-region: drag, as in a frameless window; native UI
// with ui.Context.TitleBar and ui.Element.DragWindow.
TitleBarStyle TitleBarStyle
// TrafficLightPosition moves the window controls of a window with a
// hidden title bar (macOS): the top-left corner of the close button
// goes this far from the top-left corner of the window.
TrafficLightPosition *Point
// TitleBarHeight is the height of the title bar that the page of a
// window with a hidden title bar draws (Linux, Windows): the window
// controls fill it on Windows and are centered in it on Linux. Zero is
// 32 on Windows, as Windows 11's own title bars, and the height of the
// desktop's header bars on Linux.
TitleBarHeight int
// HideWindowButtons hides the window controls of a window with a hidden
// title bar (TitleBarStyle), which keeps the rest of it: the shadow,
// the rounded corners and the resizing edges, the top one included,
// which a Frameless window lacks on Windows. The page or the view draws
// its own controls, anywhere, and calls Minimize, Maximize, Unmaximize
// and Close; the --mygo-titlebar-* CSS variables and
// ui.Context.TitleBar are then zero.
HideWindowButtons bool
DisableResize bool
DisableMove bool
DisableMinimize bool
DisableMaximize bool
DisableClose bool
DisableFullScreen bool
DisableShadow bool
AlwaysOnTop bool
FullScreen bool
// Maximized creates the window maximized.
Maximized bool
// SkipTaskbar hides the window from the taskbar (Linux, Windows).
SkipTaskbar bool
// AutoHideMenuBar keeps the window's menu bar out of sight until the
// user presses Alt or F10, and hides it again once they leave it
// (Linux, Windows). Its keyboard shortcuts work all along.
AutoHideMenuBar bool
// Transparent makes the window background transparent, so a page with a
// transparent background shows the desktop through.
Transparent bool
// BackgroundColor fills the window until the page paints, which avoids
// a flash of another color while it loads. CSS syntax: "#1e1e1e",
// "#rgba", "rgb(30 30 30)", or "light-dark(#f5f5f7, #1e1e1e)" for a
// page that follows the light or dark appearance.
BackgroundColor string
// Vibrancy puts a translucent, blurred material behind a transparent
// page, or behind native UI where it draws no background (macOS and
// Windows 11 22H2), e.g. VibrancySidebar; ui.Context.Vibrancy tells
// native UI whether it shows. On Windows, only a window created with a
// material can show one, and it has no menu bar: Alt and F10 open its
// menus in a popup.
Vibrancy Vibrancy
// VibrancyTint tints the material, in the syntax of BackgroundColor:
// "#80ff0000", "rgba(255, 0, 0, 0.5)", or "light-dark(...)" to follow
// the appearance. Only macOS 26's Liquid Glass takes a tint; other
// materials and platforms ignore it.
VibrancyTint string
// CornerRadius rounds a frameless window's corners on macOS, in DIPs:
// its content and material are cut to them, and its shadow follows
// them, as a launcher's or a floating panel's. The window need not be
// Transparent: its BackgroundColor fills its content, inside the
// corners. Windows 11 rounds a
// frameless window given one with the corners of its own windows,
// whatever the radius; Linux does not round it.
CornerRadius int
// Opacity of the window between 0 and 1 (default 1).
Opacity float64
// IgnoreMouseEvents creates the window letting the mouse through to
// what is behind it, as SetIgnoreMouseEvents(true) does: an overlay
// that marks a part of the screen never takes a click, not even
// between its creation and that call.
IgnoreMouseEvents bool
// Parent makes this window a child window that stays on top of it
// and closes with it. It can go to any display, and stays where it is
// as its parent moves. On macOS it is ordered above its parent
// whenever the parent comes in front, stays above a parent that is
// AlwaysOnTop, goes with it into full screen, and hides while the
// parent is minimized or hidden. The Parent may be a flyout, a tray's
// included: without X and Y, the window is centered over it, in the
// work area of its display, and stays above it.
Parent *Window
// Modal makes a child window modal to its Parent, which takes no
// input until it closes: a sheet of the parent on macOS, unless the
// parent is a flyout. A flyout stays open while a window is modal to
// it, wherever the keyboard goes, and closing the flyout closes the
// window: a confirmation a tray's flyout asks for, wider than it.
Modal bool
// Panel makes the window a panel, as the windows of launchers and
// quick entry (Spotlight, Raycast) are: it floats above the windows of
// all apps, on whichever Space or virtual desktop the user is on, over
// full-screen apps too, and stays out of the taskbar, the Dock and the
// app switcher. It cannot be minimized, maximized or go full screen. On
// macOS, showing or focusing it gives it the keyboard without
// activating the app, so the app the user was in stays active and gets
// the keyboard back when the panel hides.
Panel bool
// Page configures the window's web page.
Page PageOptions
// StateKey remembers the window's position, size, and maximized and
// full screen state under this key, in window-state.json in
// PathUserData or the store of App.SetWindowStateStore, saved when
// the window closes and when the app quits. A window created with the
// same key, for example at the next launch, gets them back as long as
// it would show on a connected display: they override X, Y, Width,
// Height, UseContentSize, Maximized and FullScreen.
// App.ClearWindowStates forgets them.
StateKey string
// Content makes the window show a user interface MyGo draws itself,
// on the GPU where it can, instead of a web page: create it with
// package ui. The window then has no page (Window.Page is nil): URL
// and Page are ignored.
//
// The view first runs once NewWindow returned, so that it can use the
// window NewWindow returns, kept in a variable:
//
// a.win = mygo.NewWindow(mygo.WindowOptions{Content: ui.View(func(c *ui.Context) {
// ui.Text(c, a.win.Title())
// })})
//
// Called on the main thread, as in App.WhenReady or an event handler,
// NewWindow leaves showing the window to the main loop's next turn, or
// to the first method that acts on it, such as IsVisible or Maximize,
// which finds it shown. Called from another goroutine, NewWindow shows
// the window and returns while the main thread goes on drawing it:
// guard the variable there, or set it with Window.Update.
Content Content
}
// WindowState is what WindowOptions.StateKey remembers of a window: its
// bounds in the normal state, which it gets back when it leaves the
// maximized or full screen state, and that state.
type WindowState struct {
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
Maximized bool `json:"maximized,omitempty"`
FullScreen bool `json:"fullScreen,omitempty"`
// PixelX, PixelY, PixelWidth and PixelHeight are the normal bounds in
// the screen's pixels, which Windows keeps: where displays have
// different scales, X and Y change with the scales, and earlier
// versions' cannot tell the displays apart. Without them, a window is
// centered there when the displays have different scales.
PixelX int `json:"pixelX,omitzero"`
PixelY int `json:"pixelY,omitzero"`
PixelWidth int `json:"pixelWidth,omitzero"`
PixelHeight int `json:"pixelHeight,omitzero"`
}
// WindowStateStore keeps the states of the windows with a
// WindowOptions.StateKey, by key, for App.SetWindowStateStore. MyGo calls
// it on the main thread, so it should return quickly. It keeps every
// field of a state, as JSON does: on Windows, a window whose state lost
// its pixels (WindowState.PixelX) is centered when the displays have
// different scales.
//
// In a quit, the states are saved after the OnBeforeQuit listeners have
// run, as each window closes, and once more after the event loop has
// stopped, right before the OnQuit listeners run: states that the app
// removes from the store itself in OnBeforeQuit are written again.
// App.ClearWindowStates drops them for good instead, from any of those
// listeners or at any other time, through SaveWindowStates with no
// states.
type WindowStateStore interface {
// LoadWindowStates returns the saved states. It is called once, when
// the first window with a StateKey is created. Without states (nil, or
// an error) windows get their options.
LoadWindowStates() (map[string]WindowState, error)
// SaveWindowStates replaces the saved states with states, which the
// store may keep. It is called when a window with a StateKey closes
// and when the app quits, if a state changed, and with an empty map
// by App.ClearWindowStates. After an error the next call saves the
// states again.
SaveWindowStates(states map[string]WindowState) error
}
// SetWindowStateStore keeps the states of windows (WindowOptions.StateKey)
// in s, such as the app's own database, instead of window-state.json in
// PathUserData. nil restores the file. Call it before the first window
// with a StateKey is created, usually before Run. Browsers keep no window
// state, and never call s.
func (a *Application) SetWindowStateStore(s WindowStateStore) {
a.mu.Lock()
a.stateStore = s
a.mu.Unlock()
}
// ClearWindowStates forgets the states of the windows
// (WindowOptions.StateKey), as an app's "Reset settings" does: it saves
// no states to the store (window-state.json, or the store of
// SetWindowStateStore, with an empty map), and the windows save theirs no
// more for the rest of the run. The windows open keep their bounds; those
// created afterwards, as those of the next launch, get their options.
// It may be called at any time, from any goroutine, from the OnBeforeQuit,
// OnWillQuit and OnQuit listeners too, which run before the quit's last
// save, and before Run. It returns the store's error; the next save, when
// a window closes or the app quits, tries again.
func (a *Application) ClearWindowStates() error { return onMainValue(clearWindowStates) }
// Window is a native window hosting a web page, or the Content of package
// ui. Create windows with NewWindow once the application is ready.
type Window struct {
id int
parent *Window
// flyout places a window NewFlyout created, at flyoutSize, the size
// it asked for. Main thread only.
flyout *platform.Flyout
flyoutSize platform.Size
// panel is WindowOptions.Panel.
panel bool
// active tells that the window is drawn as active (active.go);
// focusKnown, that it had the keyboard once or showed without it;
// keepActive, that SetActiveAppearance keeps it active. Main thread
// only.
active, focusKnown, keepActive bool
// focusedAt orders the windows by when they last had the keyboard,
// from focusCount, 0 for never: raiseApp shows the latest. Main
// thread only.
focusedAt uint64
// fit sizes a window that takes its page's size (FitContent), nil
// for others. Main thread only.
fit *fitContent
// host is the window a web view is in, for the page of a web view
// (webView); webViews are the pages of a window's web views, guarded
// by the lock of windows.
host *Window
webView *WebView
webViews []*Window
// pg is the window's page, which Page returns unless the window shows
// native UI.
pg *Page
// content tells that the window shows WindowOptions.Content, which
// the window hands to its surface as it attaches it and keeps no
// reference to: the Content's view, and all it reaches, stays only as
// long as the window is open (#1281). conn connects it to the native
// surface (main thread only). devTools tells that PageOptions.DevTools
// turned the developer tools on: the web inspector, or the inspector
// of the Content.
content bool
conn *surface.Conn
devTools bool
invalidating atomic.Bool
// userScripts are the scripts the window's pages run at document
// start: the bridge, the preferences and the preload script. Main
// thread only.
userScripts []platform.UserScript
// secret starts every message of the bridge in this window's pages.
// Only the bridge knows it, so messages posted from elsewhere (an
// iframe of another origin) are ignored.
secret string
// preload is PageOptions.PreloadScript, which the scripts of the pages
// loaded later keep (pageScripts), in their iframes too with
// preloadAllFrames.
preload string
preloadAllFrames bool
// native is only accessed on the main thread; nil once destroyed.
native platform.Window
destroyed atomic.Bool
shown bool
readyShow bool
// loads counts the loads the app asked the page for, so that a failed
// load knows whether its listeners started another. Main thread only.
loads int
// showLater tells that NewWindow left showing the window of native UI
// to the main loop's next turn (settleShow), and framing that its
// content draws a frame. Main thread only.
showLater, framing bool
// background is the BackgroundColor, if any. Main thread only.
background *background
// vibrancyTint is VibrancyTint, if any. Main thread only.
vibrancyTint *background
// hiddenTitleBar is a TitleBarStyle that hides the title bar: the pages
// hear the room the window controls take. Main thread only.
hiddenTitleBar bool
menu *Menu
// title is the title the app or the page set; representedFile and
// documentEdited are the document the window shows. Main thread only.
title string
representedFile string
documentEdited bool
// stateKey is WindowOptions.StateKey; stateTimer captures the state
// once it settled. Main thread only.
stateKey string
stateTimer *time.Timer
// trusted reports whether the current page may call bound methods.
// Main thread only.
trusted bool
trustedOrigins []string
// untrusted is PageOptions.Untrusted; origin is the origin of its
// current page. Main thread only.
untrusted bool
origin string
// html is the document LoadHTML loaded, while it is the page's or
// about to be. Main thread only.
html *htmlDocument
mu sync.Mutex
openHandler func(WindowOpenRequest) *WindowOptions
permissionHandler func(PermissionRequest) bool
authHandler func(AuthRequest) AuthResponse
certificateHandler func(CertificateError) bool
clientCertHandler func(ClientCertificateRequest) ClientCertificateResponse
pageCtx context.Context
pageCancel context.CancelFunc
// channels are those of the current page, by the page's id for them.
channels map[int64]*channel
// closedEarly holds the channels the current page closed before
// their calls made them, with the page's token.
closedEarly map[int64]string
// cancels are the calls of the current page that it may cancel, by
// the page's id for them, with the page's token.
cancels map[int64]pageCancel
// ran holds the ids of the calls of the current page that ran and
// are over, oldest first: a cancel that comes after their reply left
// finds them here, and is dropped.
ran []int64
// canceledEarly holds the calls the current page canceled before
// their calls made them, with the page's token.
canceledEarly map[int64]string
outMu sync.Mutex
outbox []message
flushing bool
// held keeps events until the page's DOM is ready, so events sent right
// after creating a window or during a navigation are not lost.
held []message
domReady bool
onClose listeners[func(*CloseEvent)]
onClosed listeners[func()]
onFocus listeners[func()]
onBlur listeners[func()]
onShow listeners[func()]
onHide listeners[func()]
onReadyToShow listeners[func()]
onResize listeners[func()]
onFileDrop listeners[func(*FileDropEvent)]
onWillDownload listeners[func(*DownloadEvent)]
onDownloadDone listeners[func(*Download)]
onMove listeners[func()]
onMaximize listeners[func()]
onUnmaximize listeners[func()]
onMinimize listeners[func()]
onRestore listeners[func()]
onEnterFullScreen listeners[func()]
onLeaveFullScreen listeners[func()]
onPageTitleUpdated listeners[func(*TitleEvent)]
onWillNavigate listeners[func(*NavigateEvent)]
onDidNavigate listeners[func(string)]
onDOMReady listeners[func()]
onDidFinishLoad listeners[func()]
onDidFailLoad listeners[func(*LoadError)]
onRenderGone listeners[func(string)]
// themeColor is the page's theme color, as its bridge told it, and
// hasThemeColor whether it has one. themeColorPending is set from a
// navigation's commit until the new page tells its color, which it
// does by the time its DOM is ready; a page without the bridge (an
// image) never does, and has none once it loaded. Main thread only.
themeColor platform.Color
hasThemeColor bool
themeColorPending bool
onThemeColorChanged listeners[func(color.NRGBA, bool)]
// audible is what the backend last told of the page's sound
// (AudibleChanged), so that listeners hear only changes. Main thread
// only.
audible bool
onAudibleChanged listeners[func(bool)]
// contextTarget is what the bridge recorded of the page's latest
// contextmenu event, until a context menu opens; shownContextMenu
// the menu shown last, whose items' Click is found by their weak IDs
// once chosen (context_menu.go). Main thread only.
contextTarget *contextTarget
shownContextMenu *Menu
onContextMenu listeners[func(*ContextMenuEvent)]
// capture is what the backend last told of the page's capture of
// each CaptureKind (CaptureStateChanged), so that listeners hear only
// changes. Main thread only.
capture [CaptureDisplay + 1]platform.CaptureState
onCaptureStateChanged listeners[func(CaptureKind, CaptureState)]
}
// LoadError describes a failed page load.
type LoadError struct {
URL string
Code int
Description string
}
func (e *LoadError) Error() string {
return fmt.Sprintf("mygo: failed to load %s: %s (%d)", e.URL, e.Description, e.Code)
}
// WindowOpenRequest describes a window.open() call or a click on a
// target="_blank" link.
type WindowOpenRequest struct {
URL string
FrameName string
// Width and Height requested through window.open() features; 0 when
// not specified.
Width, Height int
// UserInitiated is true when a user's gesture opened the window: a
// click on the link, or a click or key press whose handler called
// window.open(). It is false for a window a script opened on its own,
// as a pop-up from a timer or at load, which a handler may refuse.
UserInitiated bool
}
var windows struct {
sync.Mutex
nextID int
list []*Window
focused *Window
}
// focusCount counts the times a window took the keyboard
// (Window.focusedAt). Main thread only.
var focusCount uint64
// NewWindow creates a window. It must be called after the application is
// ready (see Application.WhenReady); when called from another goroutine it
// waits for that.
func NewWindow(opts WindowOptions) *Window {
// Validate in the caller's goroutine so mistakes point at the call.
bg, err := backgroundOption(opts.BackgroundColor)
if err != nil {
panic(err)
}
tint, err := backgroundOption(opts.VibrancyTint)
if err != nil {
panic(err)
}
return createWindow("NewWindow", opts, bg, tint, nil)
}
// createWindow creates a window, or a flyout, once the application is
// ready.
func createWindow(caller string, opts WindowOptions, bg, tint *background, flyout *flyoutOptions) *Window {
main := isMainThread()
if !main {
App.waitReady()
} else if !App.IsReady() {
panic("mygo: " + caller + " called before the application is ready; create windows in App.WhenReady")
}
var w *Window
onMain(func() {
w = newWindow(opts, bg, tint, 0, flyout)
if !main {
// Called from another goroutine, NewWindow returns while the
// main thread goes on drawing: the window of native UI shows
// before it does, as one of a page.
w.settleShow()
}
})
if w == nil {
// The event loop has already stopped.
w = &Window{}
w.pg = &Page{w}
w.destroyed.Store(true)
}
return w
}
func backgroundOption(s string) (*background, error) {
if s == "" {
return nil, nil
}
bg, err := parseBackground(s)
if err != nil {
return nil, err
}
return &bg, nil
}
// backgroundColor returns the window's background for the current
// appearance, or nil. Main thread only.
func (w *Window) backgroundColor() *platform.Color {
if w.background == nil {
return nil
}
c := w.background.light
if w.background.dark != c && backend().Theme().IsDark() {
c = w.background.dark
}
return &c
}
// vibrancyTintColor returns the window's material tint for the current
// appearance, or nil. Main thread only.
func (w *Window) vibrancyTintColor() *platform.Color {
if w.vibrancyTint == nil {
return nil
}
c := w.vibrancyTint.light
if w.vibrancyTint.dark != c && backend().Theme().IsDark() {
c = w.vibrancyTint.dark
}
return &c
}
// updateBackgrounds gives windows with a light and a dark background the
// one of the current appearance. Main thread only.
func updateBackgrounds() {
for _, w := range pageWindows() {
if bg := w.background; w.native != nil && bg != nil && bg.light != bg.dark {
w.native.SetBackgroundColor(*w.backgroundColor())
}
if tint := w.vibrancyTint; w.native != nil && tint != nil && tint.light != tint.dark {
w.native.SetVibrancyTint(w.vibrancyTintColor())
}
}
}
func newWindow(opts WindowOptions, bg, tint *background, native uintptr, flyout *flyoutOptions) *Window {
windows.Lock()
windows.nextID++
id := windows.nextID
windows.Unlock()
w := &Window{id: id, parent: opts.Parent, trustedOrigins: opts.Page.TrustedOrigins, untrusted: opts.Page.Untrusted, secret: randomText(), stateKey: opts.StateKey, background: bg, vibrancyTint: tint, content: opts.Content != nil, panel: opts.Panel && flyout == nil, active: true}
w.pg = &Page{w}
if flyout != nil {
w.fit = flyout.fit
} else if opts.Content == nil && opts.FitContent {
w.fit = &fitContent{
min: platform.Size{Width: opts.MinWidth, Height: opts.MinHeight},
max: platform.Size{Width: opts.MaxWidth, Height: opts.MaxHeight},
}
}
w.hiddenTitleBar = !opts.Frameless && (opts.TitleBarStyle == TitleBarHidden || opts.TitleBarStyle == TitleBarHiddenInset)
w.resetPage()
popts := w.platformOptions(&opts)
popts.BackgroundColor = w.backgroundColor()
popts.Surface = opts.Content != nil
popts.OwnScene = opts.OwnScene
if opts.Transparent {
// Backends keep transparent windows clear; so do theme changes.
w.background = nil
}
popts.Native = native
if flyout != nil {
w.flyout = flyout.flyout
if t := flyout.tray; t != nil {
if t.native == nil {
panic("mygo: NewFlyout: the tray was destroyed")
}
w.flyout.Tray = t.native
}
w.flyoutSize = platform.Size{Width: popts.Width, Height: popts.Height}
popts.Flyout = w.flyout
popts.Focusable = w.flyout.Focusable
popts.Center = false
}
w.devTools = popts.DevTools
if w.stateKey != "" {
restoreWindowState(w.stateKey, popts)
}
w.title = popts.Title
if s := opts.Page.Session; s != nil && opts.Content == nil {
store, err := s.attach()
if err != nil {
panic(fmt.Sprintf("mygo: cannot create window: %v", err))
}
w.joinSession(s)
popts.DataStore = store
}
nw, err := backend().NewWindow(popts, &windowHandler{w})
if err != nil {
w.detachSession()
panic(fmt.Sprintf("mygo: cannot create window: %v", err))
}
w.native = nw
if opts.IgnoreMouseEvents {
// Before the window shows, and on the turn that made it: no click
// reaches it in between.
nw.SetIgnoreMouseEvents(true)
}
if w.stateKey != "" {
w.initState(popts)
}
if w.content {
w.attachContent(opts.Content)
} else {
startContentRules(nw, opts.Page.ContentRules)
}
windows.Lock()
windows.list = append(windows.list, w)
windows.Unlock()
fire1(&App.onWindowCreated, w)
if !w.content {
w.loadPage(opts.Page.SessionState, opts.URL)
}
switch {
case opts.Hidden:
case w.content:
// The window of native UI shows, and its view runs for the first
// frame, once NewWindow returned on the main thread, so that the
// view can use what NewWindow returns (#1053): shown now, the
// window would draw its first frame at once on some systems, as
// AppKit may display a window it orders in and Windows a window
// maximized as it shows. The frame the content asked for draws on
// that turn as well, while the window is still hidden, as a
// window made Hidden draws its first, or as it shows.
w.showLater = true
postMain(w.settleShow)
default:
w.Show()
}
return w
}
func (w *Window) platformOptions(o *WindowOptions) *platform.WindowOptions {
po := o.Page
p := &platform.WindowOptions{
Title: o.Title,
X: o.X,
Y: o.Y,
Width: or(o.Width, 800),
Height: or(o.Height, 600),
Center: o.X == 0 && o.Y == 0,
UseContentSize: o.UseContentSize,
MinSize: platform.Size{Width: o.MinWidth, Height: o.MinHeight},
MaxSize: platform.Size{Width: o.MaxWidth, Height: o.MaxHeight},
Resizable: !o.DisableResize,
Movable: !o.DisableMove,
Minimizable: !o.DisableMinimize,
Maximizable: !o.DisableMaximize,
Closable: !o.DisableClose,
Focusable: true,
Fullscreenable: !o.DisableFullScreen,
AlwaysOnTop: o.AlwaysOnTop,
FullScreen: o.FullScreen,
Maximized: o.Maximized,
SkipTaskbar: o.SkipTaskbar,
AutoHideMenu: o.AutoHideMenuBar,
HasShadow: !o.DisableShadow,
Frameless: o.Frameless,
Transparent: o.Transparent,
TitleBarStyle: string(o.TitleBarStyle),
TitleBarHeight: max(o.TitleBarHeight, 0),
HideWindowButtons: o.HideWindowButtons,
Vibrancy: string(o.Vibrancy),
VibrancyTint: w.vibrancyTintColor(),
CornerRadius: max(o.CornerRadius, 0),
Opacity: o.Opacity,
Modal: o.Modal,
UserAgent: po.UserAgent,
UserAgentSuffix: strings.TrimSpace(po.UserAgentSuffix),
Zoom: po.ZoomFactor,
// Tab reaches every control, as desktop apps do, unless the app
// asked for Safari's behavior.
TabFocusesLinks: !po.SafariTabFocus,
NoBackgroundThrottling: po.BackgroundThrottling == ThrottlingOff,
Autoplay: po.Autoplay == AutoplayAllow,
SwipeNavigation: po.SwipeNavigation,
PinchZoom: po.PinchZoom,
Schemes: Protocol.schemes(),
Panel: o.Panel,
PictureInPicture: po.PictureInPicture,
}
if o.Panel {
p.Minimizable, p.Maximizable, p.Fullscreenable = false, false, false
p.AlwaysOnTop, p.SkipTaskbar = true, true
}
if p.Title == "" {
p.Title = App.Name()
}
if p.Opacity <= 0 || p.Opacity > 1 {
p.Opacity = 1
}
if p.Zoom <= 0 {
p.Zoom = 1
}
switch po.DevTools {
case DevToolsEnabled:
p.DevTools = true
case DevToolsAuto:
p.DevTools = IsDev()
}
if o.TrafficLightPosition != nil {
p.TrafficLightPosition = &platform.Point{X: o.TrafficLightPosition.X, Y: o.TrafficLightPosition.Y}
}
if o.Parent != nil && o.Parent.native != nil {
p.Parent = o.Parent.native
if o.Parent.flyout != nil && p.Center {
p.Center = false
p.X, p.Y = overFlyout(o.Parent.native.Bounds(), platform.Size{Width: p.Width, Height: p.Height})
}
}
w.preload, w.preloadAllFrames = po.PreloadScript, po.PreloadAllFrames
if w.fit != nil {
w.fit.zoom = p.Zoom
}
p.UserScripts = w.pageScripts()
// Kept to replace the preferences' and the locale's (sendPreferences,
// pagesLocaleChanged).
w.userScripts = p.UserScripts
return p
}
// overFlyout returns where a window of size goes over a flyout at
// bounds: centered on it, and moved into the work area of its display,
// which a dialog wider than a flyout at the edge of the screen, as a
// tray's, would cross.
func overFlyout(bounds platform.Rect, size platform.Size) (x, y int) {
r := platform.Rect{X: bounds.X + (bounds.Width-size.Width)/2, Y: bounds.Y + (bounds.Height-size.Height)/2, Width: size.Width, Height: size.Height}
if wa := platform.WorkAreaFor(bounds, backend().Screen().Displays()); wa.Width > 0 && wa.Height > 0 {
r.X = max(min(r.X, wa.X+wa.Width-r.Width), wa.X)
r.Y = max(min(r.Y, wa.Y+wa.Height-r.Height), wa.Y)
}
return r.X, r.Y
}
func jsPlatform() string {
if runtime.GOOS == "windows" {
return "win32"
}
return runtime.GOOS
}
func or[T comparable](v, fallback T) T {
var zero T
if v == zero {
return fallback
}
return v
}
// Windows returns all open windows in creation order.
func Windows() []*Window {
windows.Lock()
defer windows.Unlock()
return append([]*Window(nil), windows.list...)
}
// FocusedWindow returns the focused window of this application, or nil.
func FocusedWindow() *Window {
windows.Lock()
defer windows.Unlock()
return windows.focused
}
// WindowByID returns the window with the given id, or nil.
func WindowByID(id int) *Window {
windows.Lock()
defer windows.Unlock()
for _, w := range windows.list {
if w.id == id {
return w
}
}
return nil
}
// ID returns the unique id of the window. The page sees it as
// window.mygo.windowId.
func (w *Window) ID() int { return w.id }
// IsDestroyed reports whether the window has been closed.
func (w *Window) IsDestroyed() bool { return w.destroyed.Load() }
// Parent returns the parent window, or nil.
func (w *Window) Parent() *Window { return w.parent }
// do runs fn with the native window on the main thread, unless the window
// has been destroyed.
func (w *Window) do(fn func(n platform.Window)) {
onMain(func() {
w.settleShow()
if w.native != nil {
fn(w.native)
}
})
}
// get is do for functions returning a value.
func get[T any](w *Window, fn func(n platform.Window) T) T {
return onMainValue(func() T {
w.settleShow()
if w.native == nil {
var zero T
return zero
}
return fn(w.native)
})
}
// settleShow shows the window that NewWindow left to show on the main
// loop's next turn, unless Show, ShowInactive or Hide came first: the
// methods acting on the native window show it first, so that they find it
// shown, as NewWindow returned it. Not from the first frame, drawn while
// the window is hidden, whose view may call them (Title): the system may
// draw a frame as the window shows, which would run within that one. Main
// thread only.
func (w *Window) settleShow() {
if w.showLater && !w.framing {
w.show()
}
}
// Close closes the window as if the user clicked its close button, so
// OnClose listeners can cancel it.
func (w *Window) Close() { onMain(func() { w.close() }) }
// close runs on the main thread and reports whether the window was closed.
func (w *Window) close() bool {
if w.native == nil {
return true
}
e := &CloseEvent{Window: w}
fire1(&w.onClose, e)
if e.prevented {
return false
}
w.destroy()
return true
}
// pageSessions are the sessions of the pages that use one other than
// the default session (PageOptions.Session), while they do. Window has no
// field of them: a type a Window's fields reach would keep its exported
// methods in every app, as Window's own (the linker keeps those of the
// types reflection may reach). Main thread only.
var pageSessions map[*Window]*Session
// joinSession records that the page uses s, which attach counted. Main
// thread only.
func (w *Window) joinSession(s *Session) {
if pageSessions == nil {
pageSessions = map[*Window]*Session{}
}
pageSessions[w] = s
}
// pageSession returns the session of the page, nil for the default one.
// Main thread only.
func (w *Window) pageSession() *Session { return pageSessions[w] }
// detachSession lets the page's session go, whose store an ephemeral
// session drops with its last page. Main thread only.
func (w *Window) detachSession() {
if s := pageSessions[w]; s != nil {
delete(pageSessions, w)
s.detach()
}
}
// Destroy closes the window without emitting OnClose.
func (w *Window) Destroy() { onMain(w.destroy) }
func (w *Window) destroy() {
if n := w.native; n != nil {
w.closeState()
n.Close()
// Backends report Closed synchronously, but be defensive.
(&windowHandler{w}).Closed()
}
}
// Show shows and focuses the window.
func (w *Window) Show() { onMain(w.show) }
func (w *Window) show() {
if backgroundShow {
w.showInactive()
return
}
w.showLater = false
if w.native == nil {
return
}
visible := w.native.IsVisible()
if w.fitShow(visible); w.native == nil {
return // the frame it laid out closed it
}
w.native.Show()
w.shown = true
if !visible {
fire(&w.onShow)
}
}
// ShowInactive shows the window without focusing it.
func (w *Window) ShowInactive() { onMain(w.showInactive) }
func (w *Window) showInactive() {
w.showLater = false
if w.native == nil {
return
}
visible := w.native.IsVisible()
if w.fitShow(visible); w.native == nil {
return // the frame it laid out closed it
}
w.native.ShowInactive()
w.shown = true
if !w.focusKnown {
w.focusKnown = true
scheduleActive()
}
if !visible {
fire(&w.onShow)
}
}
// backgroundShow tells that an app `mygo dev -background` launched
// (devBackground) has not come to the front yet: its windows show without
// the focus, those NewWindow shows and those the app shows itself, as one
// made Hidden until OnReadyToShow, since Show would bring the app there,
// until one of its windows takes the focus, as the user clicks it, or the
// app becomes the active one. Main thread only.
var backgroundShow = devBackground
// fitShow sizes a hidden flyout of native UI that takes its content's
// size to that content before it shows (fitContent.show). Main thread
// only.
func (w *Window) fitShow(visible bool) {
if f := w.fit; f != nil && f.show != nil && !visible {
f.show(w)
}
}
// Hide hides the window. Called right after NewWindow, it keeps a window
// of native UI from showing at all.
func (w *Window) Hide() {
onMain(func() {
w.showLater = false
if w.native == nil || !w.native.IsVisible() {
return
}
w.native.Hide()
fire(&w.onHide)
})
}
// IsPanel reports whether the window was created with
// WindowOptions.Panel.
func (w *Window) IsPanel() bool { return w.panel }
// IsVisible reports whether the window is shown: Show or ShowInactive
// (or WindowOptions) showed it and Hide did not hide it since. A
// minimized window is still shown, on every platform, as is one on
// another desktop or covered by other windows. To ask whether it is
// minimized, use IsMinimized; whether any of a window of native UI shows
// on screen, Context.WindowHidden in its view.
func (w *Window) IsVisible() bool { return get(w, platform.Window.IsVisible) }
// Focus focuses the window.
func (w *Window) Focus() { w.do(platform.Window.Focus) }
// Blur removes focus from the window.
func (w *Window) Blur() { w.do(platform.Window.Blur) }
// IsFocused reports whether the window has keyboard focus.
func (w *Window) IsFocused() bool { return get(w, platform.Window.IsFocused) }
// CornerRadius returns the radius, in DIPs, that the window's corners are
// rounded to on screen, for content inset from its edges to round its own
// corners concentric with them: a card inset by d takes CornerRadius less
// d. On macOS it is the frameless window's WindowOptions.CornerRadius, or
// the one the system draws the window with, which depends on the system
// and the title bar (on macOS 26, 26 with TitleBarHiddenInset's toolbar
// and 16 without one); on Windows 11, the 8 of its rounded windows. It is 0 where the corners are square (full screen, a maximized
// window on Windows, Linux, browsers) or the system does not tell it.
func (w *Window) CornerRadius() float64 { return get(w, platform.Window.CornerRadius) }
// Minimize minimizes the window.
func (w *Window) Minimize() { w.do(platform.Window.Minimize) }
// IsMinimized reports whether the window is minimized.
func (w *Window) IsMinimized() bool { return get(w, platform.Window.IsMinimized) }
// Maximize maximizes the window.
func (w *Window) Maximize() { w.do(platform.Window.Maximize) }
// Unmaximize restores a maximized window.
func (w *Window) Unmaximize() { w.do(platform.Window.Unmaximize) }
// ToggleMaximize maximizes the window or restores it if it is maximized.
func (w *Window) ToggleMaximize() {
w.do(func(n platform.Window) {
if n.IsMaximized() {
n.Unmaximize()
} else {
n.Maximize()
}
})
}
// IsMaximized reports whether the window is maximized.
func (w *Window) IsMaximized() bool { return get(w, platform.Window.IsMaximized) }
// Restore restores a minimized window.
func (w *Window) Restore() { w.do(platform.Window.Restore) }
// SetFullScreen enters or leaves full screen.
func (w *Window) SetFullScreen(v bool) {
w.do(func(n platform.Window) { n.SetFullScreen(v) })
}
// ToggleFullScreen enters full screen or leaves it.
func (w *Window) ToggleFullScreen() {
w.do(func(n platform.Window) { n.SetFullScreen(!n.IsFullScreen()) })
}
// IsFullScreen reports whether the window is in full screen.
func (w *Window) IsFullScreen() bool { return get(w, platform.Window.IsFullScreen) }
// Center moves the window to the center of its screen.
func (w *Window) Center() { w.do(platform.Window.Center) }
// SetTitle sets the native window title.
func (w *Window) SetTitle(title string) {
w.do(func(n platform.Window) {
w.title = title
w.applyTitle(n)
})
}
// Title returns the window title, without the mark of an edited document
// (see SetDocumentEdited).
func (w *Window) Title() string {
return get(w, func(platform.Window) string { return w.title })
}
// SetRepresentedFile tells which file the window shows, "" for none. On
// macOS the title bar shows its icon, which the user can drag, and
// Command-clicking the title lists the folders it is in. Other platforms
// show nothing. Documents (NewDocuments) set it for their windows.
func (w *Window) SetRepresentedFile(path string) {
if path != "" {
if abs, err := filepath.Abs(path); err == nil {
path = abs
}
}
w.do(func(n platform.Window) {
w.representedFile = path
n.SetRepresentedFile(path)
})
}
// RepresentedFile returns the path SetRepresentedFile set.
func (w *Window) RepresentedFile() string {
return onMainValue(func() string { return w.representedFile })
}
// SetDocumentEdited tells whether the window's document has unsaved
// changes: a dot in the close button on macOS, and a "*" before the title
// on Linux and Windows, as Notepad and gedit show it. Documents
// (NewDocuments) set it for their windows.
func (w *Window) SetDocumentEdited(edited bool) {
w.do(func(n platform.Window) {
if w.documentEdited == edited {
return
}
w.documentEdited = edited
n.SetDocumentEdited(edited)
w.applyTitle(n)
})
}
// IsDocumentEdited reports what SetDocumentEdited set.
func (w *Window) IsDocumentEdited() bool {
return onMainValue(func() bool { return w.documentEdited })
}
// applyTitle gives the native window its title, marked when the document
// is edited where the title bar has no other mark. Main thread only.
func (w *Window) applyTitle(n platform.Window) {
title := w.title
if w.documentEdited && runtime.GOOS != "darwin" {
title = "*" + title
}
n.SetTitle(title)
}
// SetBounds moves and resizes the window.
func (w *Window) SetBounds(r Rectangle) {
w.do(func(n platform.Window) { n.SetBounds(platform.Rect(r)) })
}
// Bounds returns the position and size of the window.
func (w *Window) Bounds() Rectangle {
return get(w, func(n platform.Window) Rectangle { return Rectangle(n.Bounds()) })
}
// SetContentBounds moves and resizes the window so the page area has the
// given bounds.
func (w *Window) SetContentBounds(r Rectangle) {
w.do(func(n platform.Window) { n.SetContentBounds(platform.Rect(r)) })
}
// ContentBounds returns the bounds of the page area.
func (w *Window) ContentBounds() Rectangle {
return get(w, func(n platform.Window) Rectangle { return Rectangle(n.ContentBounds()) })
}
// SetSize resizes the window.
func (w *Window) SetSize(width, height int) {
w.do(func(n platform.Window) {
if w.setFlyoutSize(width, height) {
return
}
b := n.Bounds()
b.Width, b.Height = width, height
n.SetBounds(b)
})
}
// Size returns the size of the window.
func (w *Window) Size() (width, height int) {
b := w.Bounds()
return b.Width, b.Height
}
// SetContentSize resizes the window so the page area has the given size,
// in the window's DIPs. A zoom does not change them: a page zoomed by z
// (Page.SetZoomFactor), or native UI (ui's Context.SetZoom), lays out in
// width/z × height/z of its own. Multiply a size the view needs by the
// zoom:
//
// z := c.Zoom() // in native UI; Page.ZoomFactor for a page
// win.SetContentSize(int(math.Ceil(float64(320*z))), int(math.Ceil(float64(400*z))))
func (w *Window) SetContentSize(width, height int) {
w.do(func(n platform.Window) {
if w.setFlyoutSize(width, height) {
return
}
b := n.ContentBounds()
b.Width, b.Height = width, height
n.SetContentBounds(b)
})
}
// ContentSize returns the size of the page area, in the window's DIPs:
// a view zoomed by z sees it divided by z, as ui's Context.Size returns it.
func (w *Window) ContentSize() (width, height int) {
b := w.ContentBounds()
return b.Width, b.Height
}
// SetPosition moves the window's top-left corner.
func (w *Window) SetPosition(x, y int) {
w.do(func(n platform.Window) {
b := n.Bounds()
b.X, b.Y = x, y
n.SetBounds(b)
})
}
// Position returns the window's top-left corner.
func (w *Window) Position() (x, y int) {
b := w.Bounds()
return b.X, b.Y
}
// SetMinimumSize limits how small the window can be resized; 0 means no
// limit.
func (w *Window) SetMinimumSize(width, height int) {
w.do(func(n platform.Window) { n.SetMinimumSize(platform.Size{Width: width, Height: height}) })
}
// SetMaximumSize limits how large the window can be resized; 0 means no
// limit.
func (w *Window) SetMaximumSize(width, height int) {
w.do(func(n platform.Window) { n.SetMaximumSize(platform.Size{Width: width, Height: height}) })
}
// SetResizable sets whether the user can resize the window.
func (w *Window) SetResizable(v bool) { w.do(func(n platform.Window) { n.SetResizable(v) }) }
// IsResizable reports whether the user can resize the window.
func (w *Window) IsResizable() bool { return get(w, platform.Window.IsResizable) }
// SetMovable sets whether the user can move the window.
func (w *Window) SetMovable(v bool) { w.do(func(n platform.Window) { n.SetMovable(v) }) }
// IsMovable reports whether the user can move the window.
func (w *Window) IsMovable() bool { return get(w, platform.Window.IsMovable) }
// SetMinimizable sets whether the window can be minimized.
func (w *Window) SetMinimizable(v bool) { w.do(func(n platform.Window) { n.SetMinimizable(v) }) }
// IsMinimizable reports whether the window can be minimized.
func (w *Window) IsMinimizable() bool { return get(w, platform.Window.IsMinimizable) }
// SetMaximizable sets whether the window can be maximized.
func (w *Window) SetMaximizable(v bool) { w.do(func(n platform.Window) { n.SetMaximizable(v) }) }
// IsMaximizable reports whether the window can be maximized.
func (w *Window) IsMaximizable() bool { return get(w, platform.Window.IsMaximizable) }
// SetClosable sets whether the user can close the window.
func (w *Window) SetClosable(v bool) { w.do(func(n platform.Window) { n.SetClosable(v) }) }
// IsClosable reports whether the user can close the window.
func (w *Window) IsClosable() bool { return get(w, platform.Window.IsClosable) }
// SetAlwaysOnTop keeps the window above other windows.
func (w *Window) SetAlwaysOnTop(v bool) { w.do(func(n platform.Window) { n.SetAlwaysOnTop(v) }) }
// IsAlwaysOnTop reports whether the window stays above other windows.
func (w *Window) IsAlwaysOnTop() bool { return get(w, platform.Window.IsAlwaysOnTop) }
// SetBackgroundColor sets the color shown behind the page, in the syntax of
// WindowOptions.BackgroundColor.
func (w *Window) SetBackgroundColor(color string) error {
bg, err := parseBackground(color)
if err != nil {
return err
}
w.do(func(n platform.Window) {
w.background = &bg
n.SetBackgroundColor(*w.backgroundColor())
})
return nil
}
// SetOpacity sets the window opacity between 0 and 1.
func (w *Window) SetOpacity(v float64) {
w.do(func(n platform.Window) { n.SetOpacity(min(max(v, 0), 1)) })
}
// Opacity returns the window opacity.
func (w *Window) Opacity() float64 { return get(w, platform.Window.Opacity) }
// SetHasShadow sets whether the window has a shadow. On Linux a window
// keeps the shadow it was created with (DisableShadow).
func (w *Window) SetHasShadow(v bool) { w.do(func(n platform.Window) { n.SetHasShadow(v) }) }
// HasShadow reports whether the window has a shadow.
func (w *Window) HasShadow() bool { return get(w, platform.Window.HasShadow) }
// SetIgnoreMouseEvents makes the window transparent to mouse events. It
// wins over SetInputRegion.
func (w *Window) SetIgnoreMouseEvents(v bool) {
w.do(func(n platform.Window) { n.SetIgnoreMouseEvents(v) })
}
// IgnoresMouseEvents reports whether the window lets the mouse through, as
// SetIgnoreMouseEvents or WindowOptions.IgnoreMouseEvents made it. A
// window with an input region (SetInputRegion) takes the mouse on a part
// of it, and does not ignore it.
func (w *Window) IgnoresMouseEvents() bool { return get(w, platform.Window.IgnoresMouseEvents) }
// SetInputRegion limits the part of the window that takes the mouse to
// rects, in DIPs from the top-left corner of its content: presses, moves
// and the wheel elsewhere go through to what is behind the window, as
// with SetIgnoreMouseEvents, another app's window or the desktop. It is
// what a desktop pet needs, a frameless, transparent window always on
// top: its character and speech bubble take clicks, and the rest of the
// screen works as if the window were not there. Native UI's PassThrough
// only lets the pointer through an element to the window's own content.
//
// nil gives the window all of it back, as it starts; an empty slice none
// of it. Rectangles without area are dropped. Set the region again as
// what takes clicks moves or changes size.
//
// On Linux, the window's input shape is the region (X11's SHAPE
// extension, Wayland's input region). macOS and Windows shape no window's
// input: the window takes the mouse or lets all of it through, switched
// as the pointer comes into the region or leaves it, which macOS tells as
// it moves and Windows is asked at each tick of its clock (15.6 ms) while
// the window shows.
// A button held keeps it as it is, so that a drag that started on the
// character, as one moving the window, ends there; the release settles
// it. In browsers it does nothing.
func (w *Window) SetInputRegion(rects []Rectangle) {
var region []platform.Rect
if rects != nil {
region = make([]platform.Rect, 0, len(rects))
for _, r := range rects {
if r.Width > 0 && r.Height > 0 {
region = append(region, platform.Rect(r))
}
}
}
w.do(func(n platform.Window) {
if n, ok := n.(platform.InputRegionWindow); ok {
n.SetInputRegion(region)
}
})
}
// SetActiveAppearance keeps the window drawn as active, the key window of
// the app in front, while it is not (macOS): its controls and material keep
// their active look, as the clear Liquid Glass (VibrancyGlassClear) stays
// clear in a panel floating over another app, which never is active and
// whose glass the system would frost. It uses AppKit's private
// _setForceMainAppearance:, which a later macOS may drop: the window then
// looks as any other, but for its NSVisualEffectView material. Apps for
// the Mac App Store should not call it. On every platform, its pages and
// native UI stay active too (ui.Context.WindowActive).
func (w *Window) SetActiveAppearance(v bool) {
w.do(func(n platform.Window) {
n.SetActiveAppearance(v)
w.keepActive = v
scheduleActive()
})
}
// SetWindowButtonsTheme draws the window controls of a window with a
// hidden title bar (TitleBarStyle) for a light or a dark background,
// whatever the app's appearance, for a page whose title bar differs from
// it, as a dark view under the controls of a light app: ThemeDark draws
// light glyphs, ThemeLight dark ones, and ThemeSystem follows the app's
// appearance again (Theme.SetSource). The page's prefers-color-scheme and
// the menus keep the app's. Windows only: the traffic lights of macOS and
// GTK's title buttons on Linux keep the app's appearance.
func (w *Window) SetWindowButtonsTheme(s ThemeSource) {
if s == "" {
s = ThemeSystem
}
w.do(func(n platform.Window) { n.SetWindowButtonsTheme(string(s)) })
}
// SetContentProtection keeps the window content out of screenshots and
// screen recordings.
func (w *Window) SetContentProtection(v bool) {
w.do(func(n platform.Window) { n.SetContentProtection(v) })
}
// SetVibrancy sets the material behind the page; VibrancyNone removes it.
// On Windows, it changes the material of a window created with one, and
// shows none in other windows. See WindowOptions.Vibrancy.
func (w *Window) SetVibrancy(v Vibrancy) {
w.do(func(n platform.Window) { n.SetVibrancy(string(v)) })
// Native UI looks again whether the material shows.
w.Invalidate()
}
// SetVibrancyTint tints the window's material, in the syntax of
// BackgroundColor, or "" to remove the tint. Only macOS 26's Liquid Glass
// takes one; other materials and platforms ignore it. See
// WindowOptions.VibrancyTint.
func (w *Window) SetVibrancyTint(color string) error {
tint, err := backgroundOption(color)
if err != nil {
return err
}
w.do(func(n platform.Window) {
w.vibrancyTint = tint
n.SetVibrancyTint(w.vibrancyTintColor())
})
return nil
}
// ProgressState is the state of a progress bar; see ProgressBar.
type ProgressState string
// Progress states.
const (
ProgressNone ProgressState = ""
ProgressNormal ProgressState = "normal"
ProgressIndeterminate ProgressState = "indeterminate"
// ProgressPaused and ProgressError color the bar yellow and red on
// Windows, and show it like ProgressNormal elsewhere.
ProgressPaused ProgressState = "paused"
ProgressError ProgressState = "error"
)
// ProgressBar is the progress of a task, shown on the window's taskbar
// button (Windows), on the application's Dock icon (macOS) or on its
// launcher entry (Linux docks that implement the Unity launcher API, such
// as KDE Plasma's and Ubuntu's). The zero value shows no progress.
type ProgressBar struct {
// State defaults to ProgressNormal when Value is above 0, else to
// ProgressNone.
State ProgressState
// Value is the fraction done, between 0 and 1.
Value float64
}
// SetProgressBar shows the progress of a task:
//
// win.SetProgressBar(mygo.ProgressBar{Value: done / total})
// win.SetProgressBar(mygo.ProgressBar{}) // done: remove it
//
// On macOS and Linux the progress belongs to the application: the window
// that set it last wins.
func (w *Window) SetProgressBar(p ProgressBar) {
state, value := p.State, min(max(p.Value, 0), 1)
if state == ProgressNone && p.Value > 0 {
state = ProgressNormal
}
w.do(func(n platform.Window) { n.SetProgressBar(string(state), value) })
}
// FlashFrame draws the user's attention to the window, or stops doing so:
// its taskbar button flashes until it is focused (Windows), it is marked
// urgent (Linux), or the Dock icon bounces (macOS, while the app is not
// active).
func (w *Window) FlashFrame(flash bool) {
w.do(func(n platform.Window) { n.FlashFrame(flash) })
}
// SetSkipTaskbar hides the window from the taskbar, or shows it there again
// (Linux, Windows). See WindowOptions.SkipTaskbar.
func (w *Window) SetSkipTaskbar(v bool) {
w.do(func(n platform.Window) { n.SetSkipTaskbar(v) })
}
// SetVisibleOnAllWorkspaces shows the window on every workspace (macOS
// Spaces, Linux virtual desktops), or on the current one only. On macOS it
// also shows over other apps in full screen, even with DisableFullScreen,
// if the app is an accessory (ActivationPolicyAccessory, Dock.Hide): macOS
// shows no windows of regular apps there.
func (w *Window) SetVisibleOnAllWorkspaces(v bool) {
w.do(func(n platform.Window) { n.SetVisibleOnAllWorkspaces(v) })
}
// SetIcon sets the icon of the window, shown in its title bar and taskbar
// button (Linux, Windows), from a PNG image; nil restores the application
// icon. macOS windows show no icon of their own.
func (w *Window) SetIcon(png []byte) error {
var err error
w.do(func(n platform.Window) { err = n.SetIcon(png) })
return err
}
// SetMenu sets the menu bar of this window (Linux, Windows). On macOS the
// menu bar belongs to the application; see Application.SetMenu.
func (w *Window) SetMenu(m *Menu) {
onMain(func() {
w.menu = m
if w.native != nil {
w.native.SetMenu(m.snapshot())
}
})
}
// SetAutoHideMenuBar keeps the menu bar of this window out of sight until
// the user presses Alt or F10, or shows it for good again (Linux,
// Windows). See WindowOptions.AutoHideMenuBar.
func (w *Window) SetAutoHideMenuBar(v bool) {
w.do(func(n platform.Window) { n.SetAutoHideMenu(v) })
}
// NativeHandle returns the native window: NSWindow* on macOS, GtkWindow* on
// Linux and HWND on Windows.
func (w *Window) NativeHandle() uintptr { return get(w, platform.Window.Handle) }
// LoadURL navigates the page to url. A URL without a scheme, such as "/" or
// "/settings?tab=1", is a page of the app's frontend: the dev server at
// devUrl during `mygo dev`, the frontend built into the app otherwise (see
// SetFrontend). Besides http(s) URLs, schemes registered with
// Protocol.Handle can be used.
func (p *Page) LoadURL(rawURL string) error {
if p.w.content {
return errNoPage
}
resolved, err := resolveURL(rawURL)
if err != nil {
return err
}
p.w.page(func(n platform.Window) {
p.w.html = nil
p.w.loads++
n.LoadURL(resolved)
})
return nil
}
// LoadFile loads a local HTML file. Relative paths are resolved against the
// working directory, then against the directory of the executable (and the
// Resources directory of a macOS app bundle).
func (p *Page) LoadFile(path string) error {
if p.w.content {
return errNoPage
}
abs, err := resolveFile(path)
if err != nil {
return err
}
p.w.page(func(n platform.Window) {
p.w.html = nil
p.w.loads++
n.LoadFile(abs, filepath.Dir(abs))
})
return nil
}
func resolveFile(path string) (string, error) {
if filepath.IsAbs(path) {
if _, err := os.Stat(path); err != nil {
return "", err
}
return path, nil
}
candidates := []string{}
if wd, err := os.Getwd(); err == nil {
candidates = append(candidates, filepath.Join(wd, path))
}
if exe, err := os.Executable(); err == nil {
dir := filepath.Dir(exe)
candidates = append(candidates, filepath.Join(dir, path), filepath.Join(dir, "..", "Resources", path))
}
for _, c := range candidates {
if _, err := os.Stat(c); err == nil {
return c, nil
}
}
return "", fmt.Errorf("mygo: file %q not found", path)
}
// isTrusted reports whether a page at rawURL may call bound methods.
func (w *Window) isTrusted(rawURL string) bool {
u, err := url.Parse(rawURL)
if err != nil || w.untrusted {
return false
}
scheme := strings.ToLower(u.Scheme)
switch {
case scheme == "about" || scheme == "file" || scheme == frontendScheme:
return true
case Protocol.IsHandled(scheme):
return true
}
origin := scheme + "://" + strings.ToLower(u.Host)
if isDevOrigin(origin) {
return true
}
for _, o := range w.trustedOrigins {
if o == "*" || strings.EqualFold(strings.TrimSuffix(o, "/"), origin) {
return true
}
}
if IsDev() && (scheme == "http" || scheme == "https") {
switch u.Hostname() {
case "localhost", "127.0.0.1", "::1":
return true
}
}
return false
}
// LoadHTML loads an HTML string. Relative URLs in it resolve against
// baseURL, which may be empty, and the page is at baseURL, or about:blank.
// Reloading the page, with Reload or from the page, loads html again.
func (p *Page) LoadHTML(html, baseURL string) {
p.w.page(func(n platform.Window) { p.w.loadHTML(n, html, baseURL) })
}
// htmlDocument is a document LoadHTML loaded.
type htmlDocument struct {
html, baseURL string
// shown is set once the page committed it.
shown bool
}
// loadHTML loads html into the page and remembers it for its reloads: web
// views reload the page's URL instead, the base URL or a blank page. Main
// thread only.
func (w *Window) loadHTML(n platform.Window, html, baseURL string) {
w.html = &htmlDocument{html: html, baseURL: baseURL}
w.loads++
n.LoadHTML(html, baseURL)
}
// isAt reports whether url, which the page committed, is the document's,
// fragments aside.
func (d *htmlDocument) isAt(url string) bool {
base := d.baseURL
if base == "" {
base = "about:blank"
}
trim := func(u string) string {
u, _, _ = strings.Cut(u, "#")
return strings.TrimSuffix(u, "/")
}
return trim(url) == trim(base)
}
// Reload reloads the page.
func (p *Page) Reload() { p.w.page(func(n platform.Window) { p.w.loads++; n.Reload(false) }) }
// ReloadIgnoringCache reloads the page bypassing the cache.
func (p *Page) ReloadIgnoringCache() {
p.w.page(func(n platform.Window) { p.w.loads++; n.Reload(true) })
}
// Stop stops loading the page.
func (p *Page) Stop() { p.w.page(platform.Window.StopLoading) }
// GoBack navigates back in history.
func (p *Page) GoBack() { p.w.page(platform.Window.GoBack) }
// GoForward navigates forward in history.
func (p *Page) GoForward() { p.w.page(platform.Window.GoForward) }
// CanGoBack reports whether there is a previous page in history.
func (p *Page) CanGoBack() bool { return pageGet(p.w, platform.Window.CanGoBack) }
// CanGoForward reports whether there is a next page in history.
func (p *Page) CanGoForward() bool { return pageGet(p.w, platform.Window.CanGoForward) }
// URL returns the URL of the current page.
func (p *Page) URL() string { return pageGet(p.w, platform.Window.URL) }
// IsLoading reports whether the page is still loading.
func (p *Page) IsLoading() bool { return pageGet(p.w, platform.Window.IsLoading) }
// SetZoomFactor zooms the page; 1 is 100%.
func (p *Page) SetZoomFactor(f float64) {
if f <= 0 {
return
}
p.w.page(func(n platform.Window) {
n.SetZoom(f)
p.w.zoomChanged()
})
}
// ZoomFactor returns the page zoom; 1 is 100%.
func (p *Page) ZoomFactor() float64 { return pageGet(p.w, platform.Window.Zoom) }
// SetUserAgent overrides the user agent for subsequent requests.
func (p *Page) SetUserAgent(ua string) { p.w.page(func(n platform.Window) { n.SetUserAgent(ua) }) }
// UserAgent returns the user agent of the page.
func (p *Page) UserAgent() string { return pageGet(p.w, platform.Window.UserAgent) }
// ProcessID returns the ID of the operating system's process that runs
// the page, 0 when it is unknown: before the web view started one, after
// the window closed, for a window showing native UI, and on Linux. Which
// process it is depends on the platform:
//
// - macOS: the page's web content process (WebKit's
// com.apple.WebKit.WebContent), from WebKit's private
// -[WKWebView _webProcessIdentifier]; 0 when WebKit doesn't answer it.
// WebKit may give a page another process as it navigates to another
// site or after its process crashed, so ask again rather than keep it.
// - Windows: WebView2's browser process (msedgewebview2.exe), the
// parent of the page's renderer and other processes, which the app's
// web views share (ICoreWebView2::get_BrowserProcessId).
// - Linux: always 0, WebKitGTK has no API for the web process of a page.
func (p *Page) ProcessID() int { return pageGet(p.w, platform.Window.ProcessID) }
// GPUProcessID returns the ID of the operating system's process that does
// the page's graphics and media, which on macOS is where its sound comes
// out (to capture it with a Core Audio process tap, for instance), 0 when
// it is unknown: before the web view started one, after the window
// closed, for a window showing native UI, and on Linux and in browsers.
// Which process it is depends on the platform:
//
// - macOS: WebKit's GPU process (com.apple.WebKit.GPU), which plays the
// page's audio and video, from WebKit's private
// -[WKWebView _gpuProcessIdentifier]; 0 when WebKit doesn't answer it
// or hasn't started the process yet, which it does once a page first
// needs it, to play media or draw with the GPU.
// - Windows: WebView2's GPU process (msedgewebview2.exe
// --type=gpu-process), a child of ProcessID's browser process, from
// ICoreWebView2Environment8's GetProcessInfos; 0 on runtimes without
// it. Sound on Windows comes out of WebView2's audio service, a
// utility process, not this one: capture the tree of ProcessID's
// process for it.
// - Linux: always 0, WebKitGTK has no API for its GPU process.
//
// The app's web views may share the process. It gets a new ID each time
// the app runs and whenever the web view starts it again, after it
// crashed or exited, so ask again rather than keep it.
func (p *Page) GPUProcessID() int { return pageGet(p.w, platform.Window.GPUProcessID) }
// OpenDevTools opens the web inspector (unless disabled with
// PageOptions.DevTools).
func (p *Page) OpenDevTools() { p.w.page(platform.Window.OpenDevTools) }
// CloseDevTools closes the web inspector.
func (p *Page) CloseDevTools() { p.w.page(platform.Window.CloseDevTools) }
// IsDevToolsOpened reports whether the web inspector is open.
func (p *Page) IsDevToolsOpened() bool { return pageGet(p.w, platform.Window.IsDevToolsOpened) }
// ToggleDevTools opens or closes the web inspector.
func (p *Page) ToggleDevTools() {
p.w.page(func(n platform.Window) {
if n.IsDevToolsOpened() {
n.CloseDevTools()
} else {
n.OpenDevTools()
}
})
}
// Print opens the print dialog for the page.
func (p *Page) Print() { p.w.page(platform.Window.Print) }
// CapturePage returns a PNG screenshot of the visible page. For a window
// showing Content it renders the content as it is.
func (w *Window) CapturePage() ([]byte, error) {
if w.content {
return w.captureContent()
}
type result struct {
png []byte
err error
}
ch := make(chan result, 1)
w.do(func(n platform.Window) {
n.CapturePage(func(png []byte, err error) { deliver(ch, result{png, err}) })
})
if w.IsDestroyed() {
return nil, errDestroyed
}
r := await(ch)
return r.png, r.err
}
var errDestroyed = errors.New("mygo: window has been destroyed")
// errNoPage is returned by the page methods of windows showing Content.
var errNoPage = errors.New("mygo: the window shows Content, not a web page")
// Eval evaluates JavaScript in the page and returns its result, decoded
// from JSON. An expression's value is returned, with promises awaited:
//
// title, err := win.Page().Eval("document.title")
// data, err := win.Page().Eval("fetch('/data.json').then(r => r.json())")
//
// Statements run as the body of an async function, so use return to produce
// a value:
//
// n, err := win.Page().Eval("const items = document.querySelectorAll('li'); return items.length")
//
// Eval is not subject to the page's Content Security Policy. It returns an
// error when the window closes or the app quits before the page answers.
func (p *Page) Eval(code string) (any, error) {
return p.EvalContext(context.Background(), code)
}
// EvalContext is Eval with a context that can abort the wait.
func (p *Page) EvalContext(ctx context.Context, code string) (any, error) {
raw, err := p.w.eval(ctx, code)
if err != nil || len(raw) == 0 {
return nil, err
}
var v any
if err := decodeResult(raw, &v); err != nil {
return nil, err
}
return v, nil
}
// EvalAs evaluates JavaScript in a page like Page.Eval and decodes the
// result into a value of type T:
//
// title, err := mygo.EvalAs[string](win.Page(), "document.title")
//
// A nil page, as a window showing native UI has, returns an error.
func EvalAs[T any](p *Page, code string) (T, error) {
var v T
if p == nil {
return v, errNoPage
}
raw, err := p.w.eval(context.Background(), code)
if err != nil || len(raw) == 0 {
return v, err
}
err = decodeResult(raw, &v)
return v, err
}
// EvalError is returned by Eval when the script throws.
type EvalError struct {
Message string
}
func (e *EvalError) Error() string { return "mygo: javascript error: " + e.Message }
// SetWindowOpenHandler decides what happens on window.open() and clicks on
// target="_blank" links. The handler returns the options of the window to
// open, or nil to deny the request. Without a handler, http(s) URLs open in
// the default browser and everything else is denied.
// WindowOpenRequest.UserInitiated tells the windows a user opened from
// the pop-ups a script opened on its own.
//
// On macOS and Windows the new page keeps its relation to the opener
// (window.opener). On Linux it opens as an independent page, because
// WebKitGTK would share the opener's script message routing with a
// related page.
func (p *Page) SetWindowOpenHandler(fn func(req WindowOpenRequest) *WindowOptions) {
p.w.mu.Lock()
p.w.openHandler = fn
p.w.mu.Unlock()
}
// resetPage starts a new page context; the previous one is canceled.
func (w *Window) resetPage() {
ctx := context.WithValue(context.WithValue(context.Background(), callerKey{}, w.top()), pageKey{}, w.pg)
ctx, cancel := context.WithCancel(ctx)
w.mu.Lock()
prev := w.pageCancel
w.pageCtx, w.pageCancel = ctx, cancel
w.channels = nil // they close with the previous page's context
w.closedEarly = nil
w.cancels = nil
w.ran = nil
w.canceledEarly = nil
w.mu.Unlock()
if prev != nil {
prev()
}
}
// pageCancel is a call of a page that the page may cancel. A call with no
// cancel left is one that ran and is over: it is kept, so a cancel that
// comes after the reply left for the page finds it here.
type pageCancel struct {
token string
cancel context.CancelFunc
}
// maxCanceledEarly bounds the calls a page may cancel before their calls
// made them.
const maxCanceledEarly = 1024
// maxRan bounds the calls of a page that are remembered as run. A cancel
// reaches the page's window within a reply's journey of the call it gives
// up, so the ids of the calls that ran lately are enough.
const maxRan = 1024
// addCancel remembers the cancel of a call of the current page, so that
// the page may cancel it with a signal. A cancel that came before the
// call was made, as of a page aborting what it just started, cancels it
// here and now.
func (w *Window) addCancel(id int64, token string, cancel context.CancelFunc) {
w.mu.Lock()
defer w.mu.Unlock()
if w.cancels == nil {
w.cancels = map[int64]pageCancel{}
}
w.cancels[id] = pageCancel{token: token, cancel: cancel}
if tok, ok := w.canceledEarly[id]; ok && tok == token {
delete(w.canceledEarly, id)
cancel()
}
}
// removeCancel ends the call of the page with the id: its cancel is of no
// use now, and a cancel that came before the call made it is dropped with
// it. The id stays as one that ran, so a cancel that comes after the reply
// left for the page finds it here and is dropped, rather than waiting in
// canceledEarly until the page reloads: a page that lives long would fill
// it, and its early cancels would be forgotten. The ids of the calls that
// ran go oldest first, as a cancel for a call that ended longer ago than
// a reply's journey would have come by now.
func (w *Window) removeCancel(id int64, token string) {
w.mu.Lock()
defer w.mu.Unlock()
c, ok := w.cancels[id]
if !ok || c.token != token {
// The page reloaded or navigated while the call ran, or gave the
// call up: the call is of no page's business now, and the id may
// be the one of a call of the page that loaded since.
return
}
delete(w.canceledEarly, id)
w.cancels[id] = pageCancel{token: token}
w.ran = append(w.ran, id)
for len(w.ran) > maxRan {
old := w.ran[0]
w.ran = w.ran[1:]
if c, ok := w.cancels[old]; ok && c.cancel == nil {
delete(w.cancels, old)
}
}
}
// cancelCall cancels the call of the page with the id, if the page may
// cancel it: the method's context is canceled, and its reply is of no
// use to the page, which gave it up. Calls make their cancels on
// goroutines of their own, so the page may cancel one before its call
// made it, e.g. by aborting a request right after starting it: the call
// is then canceled as soon as it makes it.
func (w *Window) cancelCall(id int64, token string) {
w.mu.Lock()
defer w.mu.Unlock()
c, ok := w.cancels[id]
if !ok {
if len(w.canceledEarly) < maxCanceledEarly {
if w.canceledEarly == nil {
w.canceledEarly = map[int64]string{}
}
w.canceledEarly[id] = token
}
return
}
if c.token != token {
return // another page's call
}
delete(w.cancels, id)
if c.cancel == nil {
return // the call ran and is over: there is nothing left to cancel
}
c.cancel()
}
// pageContext returns the context of the current page. It is canceled when
// the page navigates away or the window is destroyed.
func (w *Window) pageContext() context.Context {
w.mu.Lock()
defer w.mu.Unlock()
return w.pageCtx
}
// message is a message for the page: a JSON object, in parts that are
// joined when it is sent, so that an encoded value, which can be large, is
// not copied into it.
type message struct {
head, value, tail []byte
}
func (m message) len() int { return len(m.head) + len(m.value) + len(m.tail) }
func (m message) appendTo(b []byte) []byte {
return append(append(append(b, m.head...), m.value...), m.tail...)
}
// maxHeldEvents bounds the events kept for a page that never becomes ready
// (e.g. an image or a failed load).
const maxHeldEvents = 1024
// enqueue schedules a message for the page. Messages are batched into a
// single script evaluation per main loop iteration. Events are held until
// the DOM of the page is ready; replies to calls are sent right away since
// the page is waiting for them.
func (w *Window) enqueue(msg message, event bool) {
if w.content {
return
}
w.outMu.Lock()
if event && !w.domReady {
if len(w.held) == maxHeldEvents {
w.held = w.held[1:]
}
w.held = append(w.held, msg)
w.outMu.Unlock()
return
}
w.outbox = append(w.outbox, msg)
schedule := !w.flushing
w.flushing = true
w.outMu.Unlock()
if schedule {
postMain(w.flush)
}
}
func (w *Window) flush() {
w.outMu.Lock()
msgs := w.outbox
w.outbox = nil
w.flushing = false
w.outMu.Unlock()
if w.native == nil || len(msgs) == 0 {
return
}
size := 64
for _, m := range msgs {
size += m.len() + 1
}
js := make([]byte, 0, size)
// A script shaped like a.b(JSON) runs without being compiled: WebKit's
// JavaScriptCore parses the value as JSON unless the inspector is on,
// several times faster. Pages without the runtime only throw.
js = append(js, "__mygo.receive(["...)
for i, m := range msgs {
if i > 0 {
js = append(js, ',')
}
js = m.appendTo(js)
}
js = append(js, "])"...)
// js is not used again, so the script can share its memory.
w.native.Eval(unsafe.String(unsafe.SliceData(js), len(js)))
}
// OnClose is called when the window is about to close. Call
// e.PreventDefault to keep it open, except when the OS ends the session
// (see Application.OnBeforeQuit): the window closes then all the same.
func (w *Window) OnClose(fn func(e *CloseEvent)) (off func()) { return w.onClose.add(fn, false) }
// OnClosed is called after the window has been closed.
func (w *Window) OnClosed(fn func()) (off func()) { return w.onClosed.add(fn, false) }
// OnFocus is called when the window gains focus.
func (w *Window) OnFocus(fn func()) (off func()) { return w.onFocus.add(fn, false) }
// OnBlur is called when the window loses focus.
func (w *Window) OnBlur(fn func()) (off func()) { return w.onBlur.add(fn, false) }
// OnShow is called when the window is shown.
func (w *Window) OnShow(fn func()) (off func()) { return w.onShow.add(fn, false) }
// OnHide is called when the window is hidden.
func (w *Window) OnHide(fn func()) (off func()) { return w.onHide.add(fn, false) }
// OnReadyToShow is called once, when the first page is ready to be
// displayed. Create the window with Hidden and call Show here to avoid a
// visual flash. A page that failed to load makes the window ready too,
// unless an OnDidFailLoad listener loaded another, which the window then
// waits for. A window showing Content is ready once it drew its first
// frame, which a hidden window draws while hidden.
func (w *Window) OnReadyToShow(fn func()) (off func()) { return w.onReadyToShow.add(fn, false) }
// OnResize is called after the window was resized.
func (w *Window) OnResize(fn func()) (off func()) { return w.onResize.add(fn, false) }
// OnFileDrop is called when files, for example from Finder or Explorer, are
// dropped on the page, with their paths. The app's own pages also get them,
// through onFileDrop of mygo-runtime. In a window showing native UI, it
// gets the files no element takes (ui.Element.DroppedFiles), with X and Y
// in DIPs.
//
// The page's own drag and drop keeps working: drop events still reach it
// with the File objects, and drags that start in the page are left alone.
// Only where the page does not handle dragged files are they accepted, so
// that dropping them there reaches OnFileDrop instead of replacing the page
// with the file.
func (w *Window) OnFileDrop(fn func(e *FileDropEvent)) (off func()) {
return w.onFileDrop.add(fn, false)
}
// OnMove is called after the window was moved.
func (w *Window) OnMove(fn func()) (off func()) { return w.onMove.add(fn, false) }
// OnMaximize is called when the window is maximized.
func (w *Window) OnMaximize(fn func()) (off func()) { return w.onMaximize.add(fn, false) }
// OnUnmaximize is called when the window leaves the maximized state.
func (w *Window) OnUnmaximize(fn func()) (off func()) { return w.onUnmaximize.add(fn, false) }
// OnMinimize is called when the window is minimized.
func (w *Window) OnMinimize(fn func()) (off func()) { return w.onMinimize.add(fn, false) }
// OnRestore is called when the window is restored from minimized.
func (w *Window) OnRestore(fn func()) (off func()) { return w.onRestore.add(fn, false) }
// OnEnterFullScreen is called when the window entered full screen.
func (w *Window) OnEnterFullScreen(fn func()) (off func()) {
return w.onEnterFullScreen.add(fn, false)
}
// OnLeaveFullScreen is called when the window left full screen.
func (w *Window) OnLeaveFullScreen(fn func()) (off func()) {
return w.onLeaveFullScreen.add(fn, false)
}
// OnPageTitleUpdated is called when the page's <title> changes. Preventing
// the event keeps the native window title.
func (p *Page) OnPageTitleUpdated(fn func(e *TitleEvent)) (off func()) {
return p.w.onPageTitleUpdated.add(fn, false)
}
// OnWillNavigate is called before the page navigates to another URL, for
// example after a link click. Preventing the event cancels the navigation.
// It is not called for LoadURL and friends.
func (p *Page) OnWillNavigate(fn func(e *NavigateEvent)) (off func()) {
return p.w.onWillNavigate.add(fn, false)
}
// OnDidNavigate is called when a navigation committed and a new page
// started.
func (p *Page) OnDidNavigate(fn func(url string)) (off func()) {
return p.w.onDidNavigate.add(fn, false)
}
// OnDOMReady is called when the page's DOM is ready (DOMContentLoaded).
func (p *Page) OnDOMReady(fn func()) (off func()) { return p.w.onDOMReady.add(fn, false) }
// OnDidFinishLoad is called when the page finished loading.
func (p *Page) OnDidFinishLoad(fn func()) (off func()) { return p.w.onDidFinishLoad.add(fn, false) }
// OnDidFailLoad is called when the page failed to load.
func (p *Page) OnDidFailLoad(fn func(err *LoadError)) (off func()) {
return p.w.onDidFailLoad.add(fn, false)
}
// OnRenderProcessGone is called when the web content process crashed or
// was killed. Call Reload to recover.
func (p *Page) OnRenderProcessGone(fn func(reason string)) (off func()) {
return p.w.onRenderGone.add(fn, false)
}
func (w *Window) readyToShow() {
if w.readyShow {
return
}
w.readyShow = true
fire(&w.onReadyToShow)
}
// windowHandler receives native window events.
type windowHandler struct{ w *Window }
func (h *windowHandler) ShouldClose() bool {
e := &CloseEvent{Window: h.w}
fire1(&h.w.onClose, e)
if !e.prevented {
h.w.closeState()
}
return !e.prevented
}
func (h *windowHandler) Closed() {
w := h.w
if w.native == nil {
return
}
w.cancelDataDrag()
w.destroyWebViews()
w.detachContent()
w.native = nil
w.destroyed.Store(true)
w.detachSession()
windows.Lock()
for i, x := range windows.list {
if x == w {
windows.list = append(windows.list[:i:i], windows.list[i+1:]...)
break
}
}
if windows.focused == w {
windows.focused = nil
scheduleActive()
}
children := []*Window{}
for _, x := range windows.list {
if x.parent == w {
children = append(children, x)
}
}
windows.Unlock()
// Children close with their parent, which reports the last window
// once they are all gone.
closingParents++
for _, c := range children {
c.destroy()
}
closingParents--
w.mu.Lock()
cancel := w.pageCancel
w.mu.Unlock()
cancel()
fire(&w.onClosed)
if closingParents == 0 && len(Windows()) == 0 {
App.lastWindowClosed()
}
}
// closingParents counts the windows closing their children (main thread
// only).
var closingParents int
func (h *windowHandler) Focused() {
backgroundShow = false
windows.Lock()
windows.focused = h.w
windows.Unlock()
focusCount++
h.w.focusKnown, h.w.focusedAt = true, focusCount
scheduleActive()
fire(&h.w.onFocus)
}
func (h *windowHandler) Blurred() {
windows.Lock()
if windows.focused == h.w {
windows.focused = nil
}
windows.Unlock()
scheduleActive()
fire(&h.w.onBlur)
}
func (h *windowHandler) MenuItemClicked(id int) {
if h.w.native != nil {
menuItemClicked(id, h.w)
}
}
func (h *windowHandler) Resized() { h.w.stateChanged(); h.w.placeFlyouts(); fire(&h.w.onResize) }
func (h *windowHandler) Moved() { h.w.stateChanged(); h.w.placeFlyouts(); fire(&h.w.onMove) }
func (h *windowHandler) Minimized() { h.w.stateChanged(); fire(&h.w.onMinimize) }
func (h *windowHandler) Restored() { h.w.stateChanged(); fire(&h.w.onRestore) }
func (h *windowHandler) Maximized() { h.w.stateChanged(); fire(&h.w.onMaximize) }
func (h *windowHandler) Unmaximized() { h.w.stateChanged(); fire(&h.w.onUnmaximize) }
func (h *windowHandler) EnteredFullScreen() { h.w.stateChanged(); fire(&h.w.onEnterFullScreen) }
func (h *windowHandler) LeftFullScreen() { h.w.stateChanged(); fire(&h.w.onLeaveFullScreen) }
func (h *windowHandler) TitleBarChanged() {
h.w.sendTitleBar()
if c := h.w.conn; c != nil && c.TitleBarChanged != nil {
c.TitleBarChanged()
}
}
func (h *windowHandler) Message(msg string) { h.w.handleMessage(msg) }
func (h *windowHandler) WillNavigate(nav platform.Navigation) bool {
if doc := h.w.html; nav.IsMainFrame && nav.Reload && doc != nil && doc.shown {
// The reload of a document LoadHTML loaded loads it again.
postMain(func() {
if w := h.w; w.html == doc && w.native != nil {
w.loadHTML(w.native, doc.html, doc.baseURL)
}
})
return false
}
if !nav.IsMainFrame || nav.Unasked {
return true
}
if h.w.untrusted && h.w.onWillNavigate.len() == 0 {
return pageOrigin(nav.URL) == h.w.origin
}
e := &NavigateEvent{URL: nav.URL, UserInitiated: nav.UserInitiated}
fire1(&h.w.onWillNavigate, e)
return !e.prevented
}
func (h *windowHandler) NavigationStarted(string) {}
// pageOrigin returns the origin of a page at rawURL, as scheme://host,
// that of the page that made it for a blob: URL.
func pageOrigin(rawURL string) string {
u, err := url.Parse(rawURL)
if err != nil {
return ""
}
if strings.EqualFold(u.Scheme, "blob") {
if u, err = url.Parse(u.Opaque); err != nil {
return ""
}
}
return strings.ToLower(u.Scheme + "://" + u.Host)
}
func (h *windowHandler) NavigationCommitted(url string) {
if doc := h.w.html; doc != nil {
switch {
case doc.shown:
h.w.html = nil // another document replaced it
case doc.isAt(url):
doc.shown = true
}
}
h.w.trusted = h.w.isTrusted(url)
if h.w.untrusted {
h.w.origin = pageOrigin(url)
}
h.w.outMu.Lock()
h.w.domReady = false
h.w.outMu.Unlock()
h.w.resetPage()
h.w.contextTarget = nil
h.w.themeColorPending = true
fire1(&h.w.onDidNavigate, url)
}
func (h *windowHandler) LoadFinished() {
h.w.themeColorSettled()
fire(&h.w.onDidFinishLoad)
// Pages without the bridge (e.g. images) never report DOM ready.
h.w.readyToShow()
}
func (h *windowHandler) LoadFailed(url string, code int, desc string) {
h.w.themeColorSettled()
loads := h.w.loads
fire1(&h.w.onDidFailLoad, &LoadError{URL: url, Code: code, Description: desc})
// A page that failed to load never reports DOM ready, and its load
// never finishes: a window shown from OnReadyToShow would stay hidden
// for good. A listener that loaded another page instead, as one of
// the app's saying what went wrong, leaves the window to that page.
if h.w.loads == loads {
h.w.readyToShow()
}
}
func (h *windowHandler) TitleChanged(title string) {
if title == "" {
return
}
e := &TitleEvent{Title: title}
fire1(&h.w.onPageTitleUpdated, e)
if !e.prevented && h.w.native != nil {
h.w.title = title
h.w.applyTitle(h.w.native)
}
}
func (h *windowHandler) NewWindow(req platform.NewWindowRequest) platform.Window {
h.w.mu.Lock()
handler := h.w.openHandler
h.w.mu.Unlock()
r := WindowOpenRequest{URL: req.URL, FrameName: req.FrameName, Width: req.Width, Height: req.Height, UserInitiated: req.UserInitiated}
if handler == nil {
if h.w.untrusted {
return nil
}
if u, err := url.Parse(req.URL); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
go Shell.OpenExternal(req.URL)
}
return nil
}
opts := handler(r)
if opts == nil {
return nil
}
o := *opts
if o.Width == 0 && req.Width > 0 {
o.Width, o.Height = req.Width, req.Height
}
// The webview loads the URL itself, in the opener's session.
o.URL = ""
o.Page.Session = h.w.pageSession()
bg, err := backgroundOption(o.BackgroundColor)
if err != nil {
log.Printf("mygo: window open handler: %v", err)
}
tint, err := backgroundOption(o.VibrancyTint)
if err != nil {
log.Printf("mygo: window open handler: %v", err)
}
child := newWindow(o, bg, tint, req.Native, nil)
return child.native
}
func (h *windowHandler) ClosedByPage() {
if h.w.host == nil { // a web view's page does not close its window
h.w.close()
}
}
func (h *windowHandler) RenderProcessGone(reason string) { fire1(&h.w.onRenderGone, reason) }
func (h *windowHandler) AudibleChanged(audible bool) {
if audible == h.w.audible {
return
}
h.w.audible = audible
fire1(&h.w.onAudibleChanged, audible)
}
func (h *windowHandler) CaptureStateChanged(kind platform.CaptureKind, state platform.CaptureState) {
k := CaptureKind(kind)
if !k.valid() || state == h.w.capture[k] {
return
}
h.w.capture[k] = state
for _, fn := range h.w.onCaptureStateChanged.snapshot() {
fn(k, CaptureState(state))
}
}
// filesDropped tells OnFileDrop listeners, and the app's own pages, about
// dropped files.
func (w *Window) filesDropped(paths []string, x, y int) {
if len(paths) == 0 {
return
}
if n, ok := w.native.(*webViewNative); ok {
// Where the web view shows in its window, scaled as it shows.
sx, sy := cmp.Or(n.scaleX, 1), cmp.Or(n.scaleY, 1)
x, y = int(n.frame.X+float64(x)*sx), int(n.frame.Y+float64(y)*sy)
}
fire1(&w.top().onFileDrop, &FileDropEvent{Paths: paths, X: x, Y: y})
// Local paths are none of other pages' business.
if w.trusted {
if msg, err := encodeEvent(fileDropEvent, FileDropEvent{Paths: paths, X: x, Y: y}); err == nil {
w.enqueue(msg, true)
}
}
}