-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify_linux.go
More file actions
79 lines (64 loc) · 1.63 KB
/
notify_linux.go
File metadata and controls
79 lines (64 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package nativenotify
import (
"fmt"
"strconv"
"sync/atomic"
"git.sr.ht/~whereswaldon/shout"
"github.com/godbus/dbus/v5"
)
var notifier atomic.Pointer[shout.Notifier]
func setup(cfg Config) error {
conn, err := dbus.SessionBus()
if err != nil {
return fmt.Errorf("getting dbus session: %w", err)
}
n, err := shout.NewNotifier(
conn,
cfg.Linux.AppName,
cfg.Linux.AppIcon,
func(id, action string, platformData map[string]dbus.Variant, target, response dbus.Variant, err error) {
fn, ok := callbacksTake(&callbacks, id)
if !ok || fn == nil {
return
}
fn(action, target.String())
},
)
if err != nil {
return fmt.Errorf("building notifier: %w", err)
}
notifier.Store(&n)
return nil
}
func push(n Notification) (err error) {
notifier := notifier.Load()
if notifier == nil {
return fmt.Errorf("notifier is nil, call setup to initialize")
}
id := nextID.Add(1)
buttons := []shout.Button{}
for _, a := range n.ButtonActions {
buttons = append(buttons, shout.Button{
Action: a.ID,
Label: a.LabelText,
Target: a.Value,
})
}
if err := (*notifier).Send(fmt.Sprintf("%d", id), shout.Notification{
Title: n.Title,
Body: n.Body,
ReplaceID: "",
Markup: false,
IconPath: n.Icon,
Priority: shout.Normal,
DefaultAction: "default",
DefaultActionLabel: "",
DefaultActionTarget: dbus.Variant{},
Buttons: buttons,
ExpirationTimeout: 0,
}); err != nil {
return fmt.Errorf("sending notification: %w", err)
}
callbacksPut(&callbacks, strconv.FormatInt(id, 10), n.Callback)
return nil
}