Skip to content

Commit

Permalink
Simplify code by using rymdport/portal package
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Nov 19, 2023
1 parent 181d83f commit db28e34
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
55 changes: 18 additions & 37 deletions app/app_xdg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"sync"

"github.com/godbus/dbus/v5"
"github.com/rymdport/portal/notification"
"github.com/rymdport/portal/openuri"
"golang.org/x/sys/execabs"

"fyne.io/fyne/v2"
Expand All @@ -29,36 +30,17 @@ func defaultVariant() fyne.ThemeVariant {
}

func (a *fyneApp) OpenURL(url *url.URL) error {
if openURLThroughPortal(url) == nil {
if err := openuri.OpenURI(url.String()); err == nil {
return nil
} else {
fyne.LogError("Opening url in portal failed", err)
}

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()
Expand Down Expand Up @@ -107,8 +89,10 @@ func (a *fyneApp) SendNotification(n *fyne.Notification) {
return
}

if a.sendNotificationThroughPortal(conn, n) == nil {
if err := a.sendNotificationThroughPortal(conn, n); err == nil {
return // No need to use the fallback path.
} else {
fyne.LogError("Sending notification using portal failed", err)
}

appIcon := a.cachedIconPath()
Expand All @@ -123,22 +107,19 @@ func (a *fyneApp) SendNotification(n *fyne.Notification) {
}

// Sending with same ID replaces the old notification.
var notificationID uint64 = 0
var notificationID uint = 0

// See https://flatpak.github.io/xdg-desktop-portal/docs/#gdbus-org.freedesktop.portal.Notification.
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),
"icon": dbus.MakeVariant(a.uniqueID),
}

obj := conn.Object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop")
call := obj.Call("org.freedesktop.portal.Notification.AddNotification", 0, id, data)
if call.Err != nil {
fyne.LogError("Failed to send notification to xdg-desktop-portal", call.Err)
return call.Err
err := notification.Add(notificationID,
&notification.Content{
Title: n.Title,
Body: n.Content,
Icon: a.uniqueID,
},
)
if err != nil {
return err
}

notificationID++
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/lucor/goinfo v0.9.0
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/rymdport/portal v0.0.0-20231119111435-f007cc4c83bb
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/rymdport/portal v0.0.0-20231119111435-f007cc4c83bb h1:G7vOtmOMTjkjbApmz4Rfa9vSft+8faoe8i9kBqmzBuo=
github.com/rymdport/portal v0.0.0-20231119111435-f007cc4c83bb/go.mod h1:RYYAnv4sssTQ7ceErKl7UD8auER/0yFV7CgmfS/uAD8=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
Expand Down

0 comments on commit db28e34

Please sign in to comment.