From 65d72577ca2819a4d03ac04c26a1ff647cd8bf42 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 18 Nov 2023 23:11:10 +0100 Subject: [PATCH] Update OpenURL to use portal if possible --- app/app_xdg.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/app/app_xdg.go b/app/app_xdg.go index fe1da93247..f9e3c7eb17 100644 --- a/app/app_xdg.go +++ b/app/app_xdg.go @@ -29,11 +29,36 @@ func defaultVariant() fyne.ThemeVariant { } func (a *fyneApp) OpenURL(url *url.URL) error { + if openURLThroughPortal(url) == nil { + return nil + } + cmd := execabs.Command("xdg-open", url.String()) cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr return cmd.Start() } +func openURLThroughPortal(url *url.URL) error { + conn, err := dbus.SessionBus() // shared connection, don't close + if err != nil { + fyne.LogError("Unable to connect to session D-Bus", err) + return err + } + + parentWindow := "" + uri := url.String() + options := map[string]dbus.Variant{} + + obj := conn.Object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop") + call := obj.Call("org.freedesktop.portal.OpenURI.OpenURI", 0, parentWindow, uri, options) + if call.Err != nil { + fyne.LogError("Failed to open url with xdg-desktop-portal", call.Err) + return call.Err + } + + return nil +} + // fetch color variant from dbus portal desktop settings. func findFreedestktopColorScheme() fyne.ThemeVariant { dbusConn, err := dbus.SessionBus() @@ -82,7 +107,7 @@ func (a *fyneApp) SendNotification(n *fyne.Notification) { return } - if a.sendXDGDesktopPortalNotification(conn, n) == nil { + if a.sendNotificationThroughPortal(conn, n) == nil { return // No need to use the fallback path. } @@ -98,11 +123,11 @@ func (a *fyneApp) SendNotification(n *fyne.Notification) { } // Sending with same ID replaces the old notification. -var notificationID int = 0 +var notificationID uint64 = 0 // See https://flatpak.github.io/xdg-desktop-portal/docs/#gdbus-org.freedesktop.portal.Notification. -func (a *fyneApp) sendXDGDesktopPortalNotification(conn *dbus.Conn, n *fyne.Notification) error { - id := strconv.Itoa(notificationID) +func (a *fyneApp) sendNotificationThroughPortal(conn *dbus.Conn, n *fyne.Notification) error { + id := strconv.FormatUint(notificationID, 10) data := map[string]dbus.Variant{ "title": dbus.MakeVariant(n.Title), "body": dbus.MakeVariant(n.Content),