From 632deda30e21e4c8decb3f61f561f76de533db2f Mon Sep 17 00:00:00 2001 From: lucor Date: Sat, 11 Jan 2025 19:56:28 +0100 Subject: [PATCH] systray: enable only if the StatusNotifierWatcher is available on the supported unix system --- CHANGELOG.md | 1 + go.mod | 2 +- internal/ui/app.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2f5a59..0f5c054 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - all: update Fyne to v2.5.3 - all: update age to v1.2.1 - ui: fix a possible nil pointer on loginURL when upgrading paw from previous versions +- systray: enable only if the StatusNotifierWatcher is available on the supported unix system - deps update: - fyne.io/fyne v2.5.3 diff --git a/go.mod b/go.mod index b642321..7707869 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( filippo.io/age v1.2.1 fyne.io/fyne/v2 v2.5.3 github.com/fyne-io/image v0.0.0-20220602074514-4956b0afb3d2 + github.com/godbus/dbus/v5 v5.1.0 github.com/stretchr/testify v1.8.4 golang.design/x/clipboard v0.7.0 golang.org/x/crypto v0.32.0 @@ -28,7 +29,6 @@ require ( github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a // indirect github.com/go-text/render v0.2.0 // indirect github.com/go-text/typesetting v0.2.0 // indirect - github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/gopherjs/gopherjs v1.17.2 // indirect github.com/jeandeaual/go-locale v0.0.0-20240223122105-ce5225dcaa49 // indirect github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect diff --git a/internal/ui/app.go b/internal/ui/app.go index fe51903..aa1825b 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -17,6 +17,7 @@ import ( "fyne.io/fyne/v2/driver/desktop" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" + "github.com/godbus/dbus/v5" "lucor.dev/paw/internal/agent" "lucor.dev/paw/internal/icon" @@ -86,6 +87,10 @@ func (a *app) agentClient() agent.PawAgent { func (a *app) makeSysTray() { if desk, ok := fyne.CurrentApp().(desktop.App); ok { + if err := checkStatusNotifierWatcher(); err != nil { + log.Println("systray not available: %w", err) + return + } a.win.SetCloseIntercept(a.win.Hide) // don't close the window if system tray used menu := fyne.NewMenu("Vaults", a.makeVaultMenuItems()...) desk.SetSystemTrayMenu(menu) @@ -260,3 +265,27 @@ func imageFromResource(resource fyne.Resource) *canvas.Image { img.SetMinSize(fyne.NewSize(64, 64)) return img } + +// checkStatusNotifierWatcher checks if the StatusNotifierWatcher is available on the supported unix system +func checkStatusNotifierWatcher() error { + // systrayUnixSupportedOSes list the supported unix system by https://github.com/fyne-io/systray + // see: https://github.com/fyne-io/systray/blob/master/systray_unix.go#L1 + systrayUnixSupportedOSes := map[string]bool{ + "linux": true, + "freebsd": true, + "openbsd": true, + "netbsd": true, + } + if !systrayUnixSupportedOSes[runtime.GOOS] { + return nil + } + conn, err := dbus.ConnectSessionBus() + if err != nil { + return fmt.Errorf("Failed to connect to session bus: %w", err) + } + defer conn.Close() + + obj := conn.Object("org.kde.StatusNotifierWatcher", "/StatusNotifierWatcher") + call := obj.Call("org.kde.StatusNotifierWatcher.RegisterStatusNotifierItem", 0, "/StatusNotifierItem") + return call.Err +}