From 8086262a8bb6294cb8b4b5dbcf3ca3ecf6b69dcd Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Tue, 4 Apr 2023 18:42:21 -0700 Subject: [PATCH] add system tray + close to tray support (no settigs UI yet) --- backend/config.go | 4 ++++ main.go | 9 +++++++-- ui/mainwindow.go | 32 +++++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/backend/config.go b/backend/config.go index 3ef76ca..413acb3 100644 --- a/backend/config.go +++ b/backend/config.go @@ -20,6 +20,8 @@ type AppConfig struct { WindowWidth int WindowHeight int LastCheckedVersion string + EnableSystemTray bool + CloseToSystemTray bool } type AlbumPageConfig struct { @@ -87,6 +89,8 @@ func DefaultConfig(appVersionTag string) *Config { WindowWidth: 1000, WindowHeight: 800, LastCheckedVersion: appVersionTag, + EnableSystemTray: true, + CloseToSystemTray: false, }, AlbumPage: AlbumPageConfig{ TracklistColumns: []string{"Artist", "Time", "Plays", "Favorite"}, diff --git a/main.go b/main.go index a0ccf0c..5d22edf 100644 --- a/main.go +++ b/main.go @@ -57,10 +57,15 @@ func main() { mainWindow.Window.SetCloseIntercept(func() { myApp.Config.Application.WindowHeight = int(mainWindow.Canvas().Size().Height) myApp.Config.Application.WindowWidth = int(mainWindow.Canvas().Size().Width) - mainWindow.Window.Close() + if myApp.Config.Application.CloseToSystemTray && + mainWindow.HaveSystemTray() { + mainWindow.Window.Hide() + } else { + fyneApp.Quit() + } }) fyneApp.Run() - // shutdown tasks + log.Println("Running shutdown tasks...") myApp.Shutdown() } diff --git a/ui/mainwindow.go b/ui/mainwindow.go index 1536c67..83e7b90 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -40,7 +40,8 @@ type MainWindow struct { BrowsingPane *browsing.BrowsingPane BottomPanel *BottomPanel - container *fyne.Container + haveSystemTray bool + container *fyne.Container } var ( @@ -54,6 +55,9 @@ func NewMainWindow(fyneApp fyne.App, appName, appVersion string, app *backend.Ap BrowsingPane: browsing.NewBrowsingPane(app), } + if app.Config.Application.EnableSystemTray { + m.SetupSystemTrayMenu(appName, fyneApp) + } m.Controller = &controller.Controller{ AppVersion: appVersion, MainWindow: m.Window, @@ -123,6 +127,32 @@ func NewMainWindow(fyneApp fyne.App, appName, appVersion string, app *backend.Ap return m } +func (m *MainWindow) SetupSystemTrayMenu(appName string, fyneApp fyne.App) { + if desk, ok := fyneApp.(desktop.App); ok { + menu := fyne.NewMenu(appName, + fyne.NewMenuItem("Play/Pause", func() { + m.App.Player.PlayPause() + }), + fyne.NewMenuItem("Previous", func() { + m.App.Player.SeekBackOrPrevious() + }), + fyne.NewMenuItem("Next", func() { + m.App.Player.SeekNext() + }), + fyne.NewMenuItemSeparator(), + fyne.NewMenuItem("Show", func() { + m.Show() + })) + desk.SetSystemTrayMenu(menu) + desk.SetSystemTrayIcon(res.ResAppicon250Png) + m.haveSystemTray = true + } +} + +func (m *MainWindow) HaveSystemTray() bool { + return m.haveSystemTray +} + func (m *MainWindow) ShowNewVersionDialog(appName, versionTag string) { contentStr := fmt.Sprintf("A new version of %s (%s) is available", appName, versionTag)