Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
835 lines
26 KiB
Go
835 lines
26 KiB
Go
package mygo
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/egoist/mygo/internal/platform"
|
|
)
|
|
|
|
// ActivationPolicy controls how a macOS application appears in the Dock and
|
|
// app switcher.
|
|
type ActivationPolicy string
|
|
|
|
// Activation policies.
|
|
const (
|
|
// ActivationPolicyRegular is an ordinary app with a Dock icon and menu bar.
|
|
ActivationPolicyRegular ActivationPolicy = "regular"
|
|
// ActivationPolicyAccessory hides the Dock icon; windows can still be
|
|
// shown. Typical for menu bar (tray) apps.
|
|
ActivationPolicyAccessory ActivationPolicy = "accessory"
|
|
// ActivationPolicyProhibited hides the app entirely.
|
|
ActivationPolicyProhibited ActivationPolicy = "prohibited"
|
|
)
|
|
|
|
// DMABUFRenderer says whether WebKitGTK draws web pages with its DMA-BUF
|
|
// renderer on Linux; see Application.SetDMABUFRenderer.
|
|
type DMABUFRenderer string
|
|
|
|
// DMA-BUF renderer settings.
|
|
const (
|
|
// DMABUFRendererOff turns the renderer off, unless the user set
|
|
// WEBKIT_DISABLE_DMABUF_RENDERER: the default.
|
|
DMABUFRendererOff DMABUFRenderer = "off"
|
|
// DMABUFRendererOn leaves the renderer to WebKitGTK, which uses it
|
|
// wherever it can.
|
|
DMABUFRendererOn DMABUFRenderer = "on"
|
|
// DMABUFRendererAuto leaves the renderer on where the host is known to
|
|
// draw with it: on Wayland, with a GPU of Mesa's, outside containers
|
|
// and virtual machines. Elsewhere it is off.
|
|
DMABUFRendererAuto DMABUFRenderer = "auto"
|
|
)
|
|
|
|
// Application controls the event loop and the lifecycle of the process.
|
|
// Use the App singleton.
|
|
type Application struct {
|
|
mu sync.Mutex
|
|
name string
|
|
version string
|
|
policy ActivationPolicy
|
|
dmabuf DMABUFRenderer
|
|
running bool
|
|
ready bool
|
|
readyCh chan struct{}
|
|
whenReady []func()
|
|
menu *Menu
|
|
menuSet bool
|
|
paths map[PathName]string
|
|
// stateStore is the WindowStateStore, or nil for the file.
|
|
stateStore WindowStateStore
|
|
finished sync.Once
|
|
|
|
// quitting is only touched on the main thread.
|
|
quitting bool
|
|
stopping bool // the quit sequence ran: the event loop is to stop (main thread)
|
|
ended bool // the event loop stopped: finish runs (main thread)
|
|
relaunch bool // Relaunch: start again once quit (main thread)
|
|
initialized bool // Run initialized the backend (main thread)
|
|
|
|
// relaunchArgs are Relaunch's arguments, nil for the ones the app
|
|
// started with (main thread).
|
|
relaunchArgs []string
|
|
|
|
// Dock controls the Dock icon on macOS.
|
|
Dock *Dock
|
|
// JumpList controls the jump list of the taskbar button on Windows.
|
|
JumpList *JumpList
|
|
|
|
onReady listeners[func()]
|
|
onWindowAllClosed listeners[func()]
|
|
onBeforeQuit listeners[func(*QuitEvent)]
|
|
onWillQuit listeners[func(*QuitEvent)]
|
|
onQuitCanceled listeners[func()]
|
|
onQuit listeners[func()]
|
|
onMemoryWarning listeners[func()]
|
|
saveState func() []byte
|
|
keepingState bool
|
|
onActivate listeners[func(bool)]
|
|
onDidBecomeActive listeners[func()]
|
|
onDidResignActive listeners[func()]
|
|
onOpenURL listeners[func(string)]
|
|
onOpenFile listeners[func(string)]
|
|
onWindowCreated listeners[func(*Window)]
|
|
|
|
onNotificationClick listeners[func(string)]
|
|
}
|
|
|
|
// App is the application singleton.
|
|
var App = &Application{readyCh: make(chan struct{}), Dock: &Dock{}, JumpList: &JumpList{}}
|
|
|
|
// Run starts the native event loop and blocks until the application quits.
|
|
// It must be called from the main goroutine, usually as the last statement
|
|
// of main.
|
|
//
|
|
// SIGINT and SIGTERM quit the application like Quit; a second signal exits
|
|
// immediately.
|
|
//
|
|
// When the MYGO_GENERATE environment variable is set (see `mygo generate`),
|
|
// Run writes the TypeScript client for the bound services to that path, or
|
|
// nothing when the app binds none, and returns without starting the event
|
|
// loop.
|
|
func (a *Application) Run() error {
|
|
if out := os.Getenv("MYGO_GENERATE"); out != "" {
|
|
return writeClient(out)
|
|
}
|
|
takeDevMarker()
|
|
if !isMainThread() {
|
|
return errors.New("mygo: App.Run must be called from the main goroutine")
|
|
}
|
|
a.mu.Lock()
|
|
if a.running {
|
|
a.mu.Unlock()
|
|
return errors.New("mygo: App.Run called twice")
|
|
}
|
|
a.running = true
|
|
a.mu.Unlock()
|
|
go cleanUpdateLeftovers()
|
|
|
|
b := backend()
|
|
if err := b.Init(appHandler{}, platform.AppOptions{
|
|
Name: a.Name(),
|
|
ActivationPolicy: string(a.ActivationPolicy()),
|
|
DMABUFRenderer: string(a.dmabuf),
|
|
Background: devBackground,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
a.initialized = true
|
|
// Settings made before Run, which the backend could not take yet.
|
|
Theme.apply()
|
|
if load := pendingGTKCSS; load != nil {
|
|
pendingGTKCSS = nil
|
|
load()
|
|
}
|
|
a.startLocale()
|
|
if m := a.Dock.Menu(); m != nil {
|
|
b.App().SetDockMenu(m.snapshot())
|
|
}
|
|
quitOnSignals()
|
|
// Run whatever was scheduled before the event loop existed.
|
|
b.Signal()
|
|
err := b.Run()
|
|
a.finish()
|
|
return err
|
|
}
|
|
|
|
// needsApp panics when main calls something that needs the running app
|
|
// before Run: the backend is not initialized yet (on Linux its libraries
|
|
// are not even loaded), and main cannot wait for it. Other goroutines
|
|
// wait for the app to start instead. call names the method.
|
|
func needsApp(call string) {
|
|
if isMainThread() && !App.initialized {
|
|
panic("mygo: " + call + " called before App.Run; call it once the application is ready (App.WhenReady)")
|
|
}
|
|
}
|
|
|
|
// WhenReady calls fn on the main thread once the application has finished
|
|
// launching. Windows can only be created after that point. If the
|
|
// application is already ready, fn is scheduled right away.
|
|
func (a *Application) WhenReady(fn func()) {
|
|
a.mu.Lock()
|
|
if !a.ready {
|
|
a.whenReady = append(a.whenReady, fn)
|
|
a.mu.Unlock()
|
|
return
|
|
}
|
|
a.mu.Unlock()
|
|
postMain(fn)
|
|
}
|
|
|
|
// IsReady reports whether the application has finished launching.
|
|
func (a *Application) IsReady() bool {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return a.ready
|
|
}
|
|
|
|
// Quit closes all windows and then quits. OnBeforeQuit and OnWillQuit
|
|
// listeners, as well as the OnClose listeners of every window, can cancel
|
|
// it.
|
|
func (a *Application) Quit() {
|
|
postMain(func() {
|
|
if a.stopping {
|
|
backend().Quit()
|
|
return
|
|
}
|
|
if a.prepareQuit() {
|
|
a.stopLoop()
|
|
}
|
|
})
|
|
}
|
|
|
|
// stopGrace is how long the event loop has to stop once the quit sequence
|
|
// has run, before the app finishes and exits without it; 0 waits on.
|
|
var stopGrace = 3 * time.Second
|
|
|
|
// stopLoop stops the event loop once the quit sequence has run.
|
|
func (a *Application) stopLoop() {
|
|
a.loopStopping()
|
|
backend().Quit()
|
|
}
|
|
|
|
// loopStopping records that the quit sequence has run. Its windows closed,
|
|
// the app must not stay behind should the event loop go on: AppKit's has
|
|
// been seen to after a relaunch asked for from a daemon's reconnection,
|
|
// when every later quit was taken for one already under way. A later quit
|
|
// asks the loop to stop again (Quit, QuitRequested), and after stopGrace
|
|
// the app finishes and exits anyway.
|
|
func (a *Application) loopStopping() {
|
|
a.stopping = true
|
|
if stopGrace > 0 {
|
|
time.AfterFunc(stopGrace, func() { postMain(a.forceStop) })
|
|
}
|
|
}
|
|
|
|
// forceStop finishes the app and exits, should its event loop not have
|
|
// stopped in time.
|
|
func (a *Application) forceStop() {
|
|
if a.ended {
|
|
return
|
|
}
|
|
a.finish()
|
|
os.Exit(0)
|
|
}
|
|
|
|
// Exit terminates the process immediately with the given exit code,
|
|
// without emitting quit events or asking windows.
|
|
func (a *Application) Exit(code int) {
|
|
onMain(func() {
|
|
for _, w := range Windows() {
|
|
w.destroy()
|
|
}
|
|
saveWindowStates()
|
|
backendExiting()
|
|
})
|
|
os.Exit(code)
|
|
}
|
|
|
|
// prepareQuit runs the quit sequence on the main thread and reports whether
|
|
// the application may quit.
|
|
func (a *Application) prepareQuit() bool {
|
|
if a.quitting {
|
|
return false
|
|
}
|
|
before := &QuitEvent{}
|
|
fire1(&a.onBeforeQuit, before)
|
|
if before.prevented {
|
|
fire(&a.onQuitCanceled)
|
|
return false
|
|
}
|
|
a.quitting = true
|
|
for _, w := range Windows() {
|
|
if !w.close() {
|
|
a.quitting = false
|
|
fire(&a.onQuitCanceled)
|
|
return false
|
|
}
|
|
}
|
|
will := &QuitEvent{}
|
|
fire1(&a.onWillQuit, will)
|
|
if will.prevented {
|
|
a.quitting = false
|
|
fire(&a.onQuitCanceled)
|
|
return false
|
|
}
|
|
// Providers are application-owned, independent of the windows just
|
|
// closed. Persist them before the native loop ends; callers needing
|
|
// error handling may Flush explicitly from a quit listener.
|
|
_ = Clipboard.Flush()
|
|
stopNativeCalls()
|
|
return true
|
|
}
|
|
|
|
// endSession runs the quit sequence when the OS ends the session
|
|
// (Terminating: logout, restart or shutdown on Windows and macOS), on
|
|
// the main thread before finish. The OS has decided, so it cannot be
|
|
// canceled: QuitEvent.SessionEnding is set, and what OnBeforeQuit,
|
|
// OnClose and OnWillQuit listeners prevent is ignored. A quit whose
|
|
// sequence ran, or is running in a nested loop, already called them,
|
|
// so they are not called again.
|
|
func (a *Application) endSession() {
|
|
if a.quitting || a.stopping || a.ended {
|
|
return
|
|
}
|
|
a.quitting = true // the windows closing below do not quit
|
|
a.stopping = true // a Quit from a listener only asks the loop to stop
|
|
fire1(&a.onBeforeQuit, &QuitEvent{SessionEnding: true})
|
|
for _, w := range Windows() {
|
|
if w.native != nil {
|
|
fire1(&w.onClose, &CloseEvent{Window: w})
|
|
w.destroy()
|
|
}
|
|
}
|
|
fire1(&a.onWillQuit, &QuitEvent{SessionEnding: true})
|
|
_ = Clipboard.Flush()
|
|
stopNativeCalls()
|
|
}
|
|
|
|
// finish runs once after the event loop has stopped.
|
|
func (a *Application) finish() {
|
|
a.finished.Do(func() {
|
|
a.ended = true
|
|
stopNativeCalls()
|
|
stopFileWatches()
|
|
clipboardStopped = true
|
|
backend().Clipboard().Close()
|
|
saveWindowStates()
|
|
fire(&a.onQuit)
|
|
backendExiting()
|
|
if a.relaunch {
|
|
// After OnQuit, which stops taking the command lines of later
|
|
// instances: the new one waits for this process to exit, which
|
|
// holds the single instance lock until then.
|
|
relaunchNow()
|
|
}
|
|
loop.shutdown()
|
|
})
|
|
}
|
|
|
|
// backendExiting tells the backend that the process is about to exit,
|
|
// once the app has finished (or Exit was called): see
|
|
// platform.ExitBackend. Main thread only.
|
|
func backendExiting() {
|
|
if b, ok := backend().(platform.ExitBackend); ok {
|
|
b.Exiting()
|
|
}
|
|
}
|
|
|
|
// lastWindowClosed is called on the main thread when the last window has
|
|
// been closed.
|
|
func (a *Application) lastWindowClosed() {
|
|
if a.quitting {
|
|
return
|
|
}
|
|
if a.onWindowAllClosed.len() > 0 {
|
|
fire(&a.onWindowAllClosed)
|
|
return
|
|
}
|
|
a.Quit()
|
|
}
|
|
|
|
func (a *Application) handleReady() {
|
|
a.mu.Lock()
|
|
menuSet := a.menuSet
|
|
a.mu.Unlock()
|
|
var def *Menu
|
|
if !menuSet {
|
|
// Built outside the lock: role labels need App.Name.
|
|
def = defaultMenu()
|
|
}
|
|
|
|
a.mu.Lock()
|
|
a.ready = true
|
|
fns := a.whenReady
|
|
a.whenReady = nil
|
|
if !a.menuSet {
|
|
a.menu = def
|
|
}
|
|
menu := a.menu
|
|
a.mu.Unlock()
|
|
close(a.readyCh)
|
|
|
|
backend().SetApplicationMenu(menu.snapshot())
|
|
fire(&a.onReady)
|
|
for _, fn := range fns {
|
|
fn()
|
|
}
|
|
// After the windows the app opens when ready, which URLs may target.
|
|
postMain(launchArgs)
|
|
}
|
|
|
|
// waitReady blocks a goroutine other than the main one until the
|
|
// application is ready.
|
|
func (a *Application) waitReady() { <-a.readyCh }
|
|
|
|
// Name returns the application name. It defaults to the bundle name of a
|
|
// packaged app, else to the executable name.
|
|
func (a *Application) Name() string {
|
|
a.mu.Lock()
|
|
name := a.name
|
|
a.mu.Unlock()
|
|
if name != "" {
|
|
return name
|
|
}
|
|
if info, ok := packageInfo(); ok && info.Name != "" {
|
|
return info.Name
|
|
}
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return "MyGo"
|
|
}
|
|
return strings.TrimSuffix(filepath.Base(exe), ".exe")
|
|
}
|
|
|
|
// SetName overrides the application name. Call it before Run.
|
|
func (a *Application) SetName(name string) {
|
|
a.mu.Lock()
|
|
a.name = name
|
|
a.mu.Unlock()
|
|
}
|
|
|
|
// Version returns the application version, which defaults to the version of
|
|
// the packaged app.
|
|
func (a *Application) Version() string {
|
|
a.mu.Lock()
|
|
v := a.version
|
|
a.mu.Unlock()
|
|
if v == "" {
|
|
if info, ok := packageInfo(); ok {
|
|
return info.Version
|
|
}
|
|
}
|
|
return v
|
|
}
|
|
|
|
// SetVersion overrides the application version.
|
|
func (a *Application) SetVersion(v string) {
|
|
a.mu.Lock()
|
|
a.version = v
|
|
a.mu.Unlock()
|
|
}
|
|
|
|
// IsPackaged reports whether the application runs from an app bundle
|
|
// (created by `mygo dev` and `mygo build`) rather than a plain executable.
|
|
func (a *Application) IsPackaged() bool {
|
|
_, ok := packageInfo()
|
|
return ok
|
|
}
|
|
|
|
// SetMenu sets the application menu: the menu bar on macOS and the menu bar
|
|
// of windows without their own menu on Linux and Windows. nil removes it.
|
|
// Without a call to SetMenu a default menu with the usual App, Edit, View
|
|
// and Window items is installed.
|
|
func (a *Application) SetMenu(m *Menu) {
|
|
a.mu.Lock()
|
|
a.menu = m
|
|
a.menuSet = true
|
|
ready := a.ready
|
|
a.mu.Unlock()
|
|
if ready {
|
|
onMain(func() { backend().SetApplicationMenu(m.snapshot()) })
|
|
}
|
|
}
|
|
|
|
// Menu returns the application menu.
|
|
func (a *Application) Menu() *Menu {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return a.menu
|
|
}
|
|
|
|
// ActivationPolicy returns how the app appears in the Dock (macOS):
|
|
// ActivationPolicyRegular unless SetActivationPolicy changed it.
|
|
func (a *Application) ActivationPolicy() ActivationPolicy {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
if a.policy == "" {
|
|
return ActivationPolicyRegular
|
|
}
|
|
return a.policy
|
|
}
|
|
|
|
// SetActivationPolicy changes how the app appears in the Dock (macOS).
|
|
func (a *Application) SetActivationPolicy(p ActivationPolicy) {
|
|
a.mu.Lock()
|
|
a.policy = p
|
|
running := a.running
|
|
a.mu.Unlock()
|
|
if running {
|
|
onMain(func() { backend().App().SetActivationPolicy(string(p)) })
|
|
}
|
|
}
|
|
|
|
// SetDMABUFRenderer sets whether WebKitGTK may draw web pages with its
|
|
// DMA-BUF renderer on Linux, which MyGo turns off by default because it
|
|
// shows blank windows on NVIDIA's driver, in virtual machines and in
|
|
// containers. Where it works, pages scroll at several times the frame
|
|
// rate with less CPU. WEBKIT_DISABLE_DMABUF_RENDERER, set by the user,
|
|
// overrides it. Call it before Run; other systems ignore it.
|
|
func (a *Application) SetDMABUFRenderer(r DMABUFRenderer) {
|
|
if r == DMABUFRendererAuto {
|
|
linkDMABUFProbe()
|
|
}
|
|
a.mu.Lock()
|
|
a.dmabuf = r
|
|
a.mu.Unlock()
|
|
}
|
|
|
|
// Focus brings the application to the foreground.
|
|
func (a *Application) Focus() {
|
|
needsApp("App.Focus")
|
|
onMain(func() { backend().App().Activate() })
|
|
}
|
|
|
|
// IsActive reports whether the application is the active one: the app in
|
|
// front on macOS, the app with the window that has the keyboard on Windows
|
|
// and Linux, the focused page in browsers.
|
|
func (a *Application) IsActive() bool {
|
|
needsApp("App.IsActive")
|
|
return onMainValue(func() bool { return backend().App().IsActive() })
|
|
}
|
|
|
|
// Hide hides all windows of the application (macOS).
|
|
func (a *Application) Hide() {
|
|
needsApp("App.Hide")
|
|
onMain(func() { backend().App().Hide() })
|
|
}
|
|
|
|
// Show shows the application after Hide (macOS).
|
|
func (a *Application) Show() {
|
|
needsApp("App.Show")
|
|
onMain(func() { backend().App().Unhide() })
|
|
}
|
|
|
|
// IsHidden reports whether the application is hidden (macOS).
|
|
func (a *Application) IsHidden() bool {
|
|
needsApp("App.IsHidden")
|
|
return onMainValue(func() bool { return backend().App().IsHidden() })
|
|
}
|
|
|
|
// SetBadgeCount shows a counter on the application icon. Zero clears it.
|
|
func (a *Application) SetBadgeCount(n int) {
|
|
needsApp("App.SetBadgeCount")
|
|
label := ""
|
|
if n > 0 {
|
|
label = strconv.Itoa(n)
|
|
}
|
|
a.Dock.SetBadge(label)
|
|
}
|
|
|
|
// AboutPanelOptions customizes the standard about panel.
|
|
type AboutPanelOptions struct {
|
|
ApplicationName string
|
|
ApplicationVersion string
|
|
// Version is the build version shown next to ApplicationVersion.
|
|
Version string
|
|
Copyright string
|
|
Credits string
|
|
}
|
|
|
|
// ShowAboutPanel shows the standard about panel (macOS).
|
|
func (a *Application) ShowAboutPanel(opts AboutPanelOptions) {
|
|
needsApp("App.ShowAboutPanel")
|
|
onMain(func() { backend().App().ShowAboutPanel(platform.AboutPanelOptions(opts)) })
|
|
}
|
|
|
|
// OnReady is called once the application has finished launching.
|
|
func (a *Application) OnReady(fn func()) (off func()) { return a.onReady.add(fn, false) }
|
|
|
|
// OnWindowAllClosed is called when the last window has been closed, unless
|
|
// the application is quitting. Without listeners the application quits;
|
|
// registering one keeps it running (the common choice on macOS).
|
|
func (a *Application) OnWindowAllClosed(fn func()) (off func()) {
|
|
return a.onWindowAllClosed.add(fn, false)
|
|
}
|
|
|
|
// OnBeforeQuit is called when a quit starts, before windows are closed.
|
|
// e.PreventDefault cancels the quit.
|
|
//
|
|
// When the OS ends the session (logout, restart, shutdown), on Windows
|
|
// (WM_ENDSESSION) and macOS (applicationWillTerminate:), OnBeforeQuit,
|
|
// the windows' OnClose and OnWillQuit listeners are called too, with
|
|
// e.SessionEnding set: the quit cannot be canceled, and the process may
|
|
// be ended as soon as the listeners and OnQuit return, so they must do
|
|
// their work synchronously, without dialogs or goroutines to wait for.
|
|
// They are not called twice: when a quit had already called them, the
|
|
// session's end only finishes it. On Linux, MyGo hears of no session end:
|
|
// at shutdown, and at a logout that stops the user's processes, the app
|
|
// gets SIGTERM, which quits like Quit, cancelable.
|
|
func (a *Application) OnBeforeQuit(fn func(e *QuitEvent)) (off func()) {
|
|
return a.onBeforeQuit.add(fn, false)
|
|
}
|
|
|
|
// OnWillQuit is called after all windows have been closed, right before the
|
|
// event loop stops. e.PreventDefault cancels the quit, unless the OS ends
|
|
// the session (e.SessionEnding; see OnBeforeQuit).
|
|
func (a *Application) OnWillQuit(fn func(e *QuitEvent)) (off func()) {
|
|
return a.onWillQuit.add(fn, false)
|
|
}
|
|
|
|
// OnQuitCanceled is called when a quit that started was canceled: a
|
|
// listener prevented it in OnBeforeQuit or OnWillQuit, or a window kept
|
|
// itself open in OnClose. The windows a canceled OnWillQuit had already
|
|
// closed stay closed. Apps that track the quit in their own state from
|
|
// OnBeforeQuit clear it here; without this event that state would stay set
|
|
// although the app goes on running.
|
|
func (a *Application) OnQuitCanceled(fn func()) (off func()) {
|
|
return a.onQuitCanceled.add(fn, false)
|
|
}
|
|
|
|
// OnQuit is called after the event loop has stopped, and after the
|
|
// windows' states are saved (WindowOptions.StateKey). When the OS ends
|
|
// the session, it is called after OnBeforeQuit and OnWillQuit (see
|
|
// OnBeforeQuit), and the process may end as soon as it returns: it is the
|
|
// last hook, and must finish its work synchronously.
|
|
func (a *Application) OnQuit(fn func()) (off func()) { return a.onQuit.add(fn, false) }
|
|
|
|
// OnSaveState has fn give what the app needs to come back as it was, called on
|
|
// the main thread as a phone's app goes to the background, where the system may
|
|
// end it without a word. The next launch finds it in RestoredState. Keep it
|
|
// small: it is written before the app may do no more. Desktops never call it,
|
|
// but under mygo dev, which restarts the app as the code changes: it is called
|
|
// every second there, for the app to come back as it was on every platform.
|
|
func (a *Application) OnSaveState(fn func() []byte) {
|
|
a.saveState = fn
|
|
a.keepStateInDevelopment()
|
|
}
|
|
|
|
// RestoredState returns what OnSaveState gave as the app went to the background,
|
|
// if the system ended it there. Android tells when the user meant to leave, which
|
|
// gives nil. iOS doesn't say by the time the app starts: an app the user force-quit
|
|
// from the background gets it too. An end while the app is on screen, as a crash,
|
|
// gives nil.
|
|
func (a *Application) RestoredState() []byte { return restoredState() }
|
|
|
|
// OnMemoryWarning is called on the main thread when the system is short of
|
|
// memory: let go of what can be made again, as caches. Phones end apps that
|
|
// keep too much; desktops do not report it. Native UI drops its own caches
|
|
// after these listeners ran, and the garbage collector returns what it can.
|
|
func (a *Application) OnMemoryWarning(fn func()) (off func()) {
|
|
return a.onMemoryWarning.add(fn, false)
|
|
}
|
|
|
|
// OnActivate is called when the application is re-activated, for example by
|
|
// clicking its Dock icon (macOS). Apps usually open a window when
|
|
// hasVisibleWindows is false.
|
|
func (a *Application) OnActivate(fn func(hasVisibleWindows bool)) (off func()) {
|
|
return a.onActivate.add(fn, false)
|
|
}
|
|
|
|
// OnDidBecomeActive is called when the application becomes the active one.
|
|
func (a *Application) OnDidBecomeActive(fn func()) (off func()) {
|
|
return a.onDidBecomeActive.add(fn, false)
|
|
}
|
|
|
|
// OnDidResignActive is called when the application stops being active.
|
|
func (a *Application) OnDidResignActive(fn func()) (off func()) {
|
|
return a.onDidResignActive.add(fn, false)
|
|
}
|
|
|
|
// OnOpenURL is called when the application is asked to open a URL of a
|
|
// scheme it handles: one listed in urlSchemes in mygo.config.ts or registered
|
|
// with RegisterURLScheme. Register it before Run to receive the URL the app
|
|
// was launched with. On Windows and Linux, where a URL starts a new
|
|
// instance of the app, use RequestSingleInstanceLock so that the first
|
|
// instance gets the URLs of later ones.
|
|
func (a *Application) OnOpenURL(fn func(url string)) (off func()) {
|
|
return a.onOpenURL.add(fn, false)
|
|
}
|
|
|
|
// OnOpenFile is called when a file is opened with the application: one of
|
|
// the types of fileAssociations in mygo.config.ts opened from the file manager,
|
|
// a file dropped on the Dock icon (macOS), or one passed on the command
|
|
// line. Register it before Run to receive the files the app was launched
|
|
// with; on Windows and Linux, RequestSingleInstanceLock makes the first
|
|
// instance get those of later ones.
|
|
func (a *Application) OnOpenFile(fn func(path string)) (off func()) {
|
|
return a.onOpenFile.add(fn, false)
|
|
}
|
|
|
|
// OnNotificationClick is called with the ID of every notification of the
|
|
// app that the user clicks, after its OnClick listeners: also one of an
|
|
// earlier run, which no Notification of this run holds, such as the one
|
|
// whose click launched the app (macOS). Register it before Run to receive
|
|
// that one. Give notifications IDs of the app's own, such as the
|
|
// conversation they are about, to know what to open.
|
|
func (a *Application) OnNotificationClick(fn func(id string)) (off func()) {
|
|
return a.onNotificationClick.add(fn, false)
|
|
}
|
|
|
|
// OnWindowCreated is called for every new window.
|
|
func (a *Application) OnWindowCreated(fn func(w *Window)) (off func()) {
|
|
return a.onWindowCreated.add(fn, false)
|
|
}
|
|
|
|
// ClearBrowsingData deletes what the app's pages stored in the default
|
|
// session: cookies, local and session storage, IndexedDB, service workers
|
|
// and caches, e.g. when the user signs out. Open pages keep what they hold
|
|
// in memory until they are reloaded. Session.ClearData clears the data of
|
|
// other sessions, or some kinds of data.
|
|
func (a *Application) ClearBrowsingData() error {
|
|
needsApp("App.ClearBrowsingData")
|
|
ch := make(chan error, 1)
|
|
onMain(func() {
|
|
backend().App().ClearBrowsingData(nil, platform.SiteDataAll, func(err error) { deliver(ch, err) })
|
|
})
|
|
return await(ch)
|
|
}
|
|
|
|
// Dock controls the application's Dock icon (macOS). Its methods do
|
|
// nothing on other platforms.
|
|
type Dock struct {
|
|
mu sync.Mutex
|
|
menu *Menu
|
|
}
|
|
|
|
// SetMenu sets the menu the Dock icon shows above the standard items, e.g.
|
|
// to open a new window; nil removes it. It may be called before App.Run.
|
|
func (d *Dock) SetMenu(m *Menu) {
|
|
d.mu.Lock()
|
|
d.menu = m // its items must stay reachable for clicks
|
|
d.mu.Unlock()
|
|
onMain(func() {
|
|
if App.initialized { // else Run installs it
|
|
// The menu set last: calls may reach the main thread in
|
|
// another order.
|
|
backend().App().SetDockMenu(d.Menu().snapshot())
|
|
}
|
|
})
|
|
}
|
|
|
|
// Menu returns the menu set with SetMenu.
|
|
func (d *Dock) Menu() *Menu {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
return d.menu
|
|
}
|
|
|
|
// SetBadge shows text on the Dock icon; "" clears it.
|
|
func (d *Dock) SetBadge(text string) {
|
|
needsApp("App.Dock.SetBadge")
|
|
onMain(func() { backend().App().SetBadge(text) })
|
|
}
|
|
|
|
// Badge returns the text shown on the Dock icon.
|
|
func (d *Dock) Badge() string {
|
|
needsApp("App.Dock.Badge")
|
|
return onMainValue(func() string { return backend().App().Badge() })
|
|
}
|
|
|
|
// Bounce bounces the Dock icon to request attention and returns an id for
|
|
// CancelBounce. Critical bounces continue until the app is activated.
|
|
func (d *Dock) Bounce(critical bool) int {
|
|
needsApp("App.Dock.Bounce")
|
|
return onMainValue(func() int { return backend().App().Bounce(critical) })
|
|
}
|
|
|
|
// CancelBounce stops a bounce started with Bounce.
|
|
func (d *Dock) CancelBounce(id int) {
|
|
needsApp("App.Dock.CancelBounce")
|
|
onMain(func() { backend().App().CancelBounce(id) })
|
|
}
|
|
|
|
// SetIcon replaces the Dock icon with a PNG image.
|
|
func (d *Dock) SetIcon(png []byte) error {
|
|
needsApp("App.Dock.SetIcon")
|
|
return onMainValue(func() error { return backend().App().SetDockIcon(png) })
|
|
}
|
|
|
|
// Hide removes the Dock icon (accessory activation policy).
|
|
func (d *Dock) Hide() { App.SetActivationPolicy(ActivationPolicyAccessory) }
|
|
|
|
// Show restores the Dock icon (regular activation policy).
|
|
func (d *Dock) Show() { App.SetActivationPolicy(ActivationPolicyRegular) }
|
|
|
|
// appHandler receives application events from the backend.
|
|
type appHandler struct{}
|
|
|
|
func (appHandler) Ready() { App.handleReady() }
|
|
func (appHandler) Dispatch() { loop.drain() }
|
|
func (appHandler) QuitRequested() bool {
|
|
if App.stopping {
|
|
// The loop goes on although asked to stop: ask again.
|
|
return true
|
|
}
|
|
if !App.prepareQuit() {
|
|
return false
|
|
}
|
|
// The backend stops its loop itself.
|
|
App.loopStopping()
|
|
return true
|
|
}
|
|
func (appHandler) Terminating() {
|
|
App.endSession()
|
|
App.finish()
|
|
}
|
|
func (appHandler) Activated(visible bool) {
|
|
fire1(&App.onActivate, visible)
|
|
}
|
|
func (appHandler) DidBecomeActive() {
|
|
backgroundShow = false
|
|
fire(&App.onDidBecomeActive)
|
|
}
|
|
func (appHandler) DidResignActive() { fire(&App.onDidResignActive) }
|
|
func (appHandler) OpenURLs(urls []string) {
|
|
for _, u := range urls {
|
|
fire1(&App.onOpenURL, u)
|
|
}
|
|
}
|
|
func (appHandler) OpenFiles(paths []string) {
|
|
for _, p := range paths {
|
|
fire1(&App.onOpenFile, p)
|
|
}
|
|
}
|
|
func (appHandler) MenuItemClicked(id int) { menuItemClicked(id, FocusedWindow()) }
|
|
func (appHandler) ThemeChanged() {
|
|
updateBackgrounds()
|
|
contentThemeChanged()
|
|
Theme.changed()
|
|
// Pages hear the accent and the settings of accessibility, which the
|
|
// media queries do not tell them.
|
|
sendPreferences()
|
|
}
|
|
func (appHandler) MemoryWarning() { fire(&App.onMemoryWarning) }
|
|
func (appHandler) DisplaysChanged() { Screen.changed() }
|
|
func (appHandler) LocaleChanged() { App.localeChangedFromSystem() }
|
|
func (appHandler) PowerEvent(event string) {
|
|
switch event {
|
|
case "suspend":
|
|
App.writeState()
|
|
case "resume":
|
|
forgetKeptState()
|
|
}
|
|
Power.event(event)
|
|
}
|
|
func (appHandler) HotkeyPressed(id int) { GlobalShortcut.pressed(id) }
|
|
func (appHandler) NotificationClicked(id string) { notificationClicked(id) }
|
|
|
|
func (appHandler) NetworkChanged(s platform.NetworkStatus) { Network.changed(networkStatus(s)) }
|
|
func (appHandler) HotkeyBound(id int, keys string) { GlobalShortcut.bound(id, keys) }
|