mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
Windows tray menus (#2181)
* Add example * Add windows systray * Add gitkeep * use windows.GUID
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/wailsapp/wails/v2/internal/app"
|
||||
"github.com/wailsapp/wails/v2/internal/signal"
|
||||
"github.com/wailsapp/wails/v2/pkg/menu"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Application is the main Wails application
|
||||
@@ -12,8 +14,13 @@ type Application struct {
|
||||
application *app.App
|
||||
options *options.App
|
||||
|
||||
// System Trays
|
||||
systemTrays []*SystemTray
|
||||
|
||||
// running flag
|
||||
running bool
|
||||
|
||||
shutdown sync.Once
|
||||
}
|
||||
|
||||
// NewWithOptions creates a new Application with the given options
|
||||
@@ -46,6 +53,10 @@ func (a *Application) SetApplicationMenu(appMenu *menu.Menu) {
|
||||
// Run starts the application
|
||||
func (a *Application) Run() error {
|
||||
|
||||
for _, systemtray := range a.systemTrays {
|
||||
go systemtray.run()
|
||||
}
|
||||
|
||||
err := applicationInit()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -66,10 +77,44 @@ func (a *Application) Run() error {
|
||||
|
||||
a.running = true
|
||||
|
||||
return a.application.Run()
|
||||
err = a.application.Run()
|
||||
a.Quit()
|
||||
return err
|
||||
}
|
||||
|
||||
// Quit will shut down the application
|
||||
func (a *Application) Quit() {
|
||||
a.application.Shutdown()
|
||||
a.shutdown.Do(func() {
|
||||
for _, systray := range a.systemTrays {
|
||||
systray.Close()
|
||||
}
|
||||
a.application.Shutdown()
|
||||
})
|
||||
}
|
||||
|
||||
// Bind the given struct to the application
|
||||
func (a *Application) Bind(boundStruct any) {
|
||||
a.options.Bind = append(a.options.Bind, boundStruct)
|
||||
}
|
||||
|
||||
func (a *Application) On(eventType EventType, callback func()) {
|
||||
|
||||
c := func(ctx context.Context) {
|
||||
callback()
|
||||
}
|
||||
|
||||
switch eventType {
|
||||
case StartUp:
|
||||
a.options.OnStartup = c
|
||||
case ShutDown:
|
||||
a.options.OnShutdown = c
|
||||
case DomReady:
|
||||
a.options.OnDomReady = c
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Application) NewSystemTray(options *options.SystemTray) *SystemTray {
|
||||
systemTray := newSystemTray(options)
|
||||
a.systemTrays = append(a.systemTrays, systemTray)
|
||||
return systemTray
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user