-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathnotify_windows.go
More file actions
157 lines (128 loc) · 2.69 KB
/
notify_windows.go
File metadata and controls
157 lines (128 loc) · 2.69 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//go:build windows && go1.21 && !linux && !freebsd && !netbsd && !openbsd && !darwin && !js
package beeep
import (
"fmt"
"image/png"
"os"
"time"
"git.sr.ht/~jackmordaunt/go-toast"
"github.com/sergeymakinen/go-ico"
"github.com/tadvi/systray"
"golang.org/x/sys/windows/registry"
)
var isWindows10 bool
func init() {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
if err != nil {
return
}
defer k.Close()
maj, _, err := k.GetIntegerValue("CurrentMajorVersionNumber")
if err != nil {
return
}
isWindows10 = maj == 10
}
// Notify sends desktop notification.
// The icon can be string with a path to png file or png []byte data. Stock icon names can also be used where supported.
//
// On Windows 10/11 it will use Windows Runtime COM API and will fall back to PowerShell. Windows 7 will use win32 API.
func Notify(title, message string, icon any) error {
var img string
switch i := icon.(type) {
case string:
img = i
case []byte:
var err error
img, err = bytesToFilename(i)
if err != nil {
return err
}
defer os.Remove(img)
default:
return fmt.Errorf("unsupported argument: %T", icon)
}
if isWindows10 {
if err := toastNotify(title, message, img, false); err != nil {
return err
}
time.Sleep(time.Millisecond * 100)
} else {
if err := balloonNotify(title, message, img); err != nil {
return err
}
}
return nil
}
func balloonNotify(title, message, icon string) error {
tray, err := systray.New()
if err != nil {
return err
}
tmp, err := pngToIco(pathAbs(icon))
if err != nil {
return err
}
defer os.Remove(tmp)
err = tray.ShowCustom(tmp, title)
if err != nil {
return err
}
go func() {
go func() {
_ = tray.Run()
}()
time.Sleep(timeout)
_ = tray.Stop()
}()
err = tray.ShowMessage(title, message, true)
if err != nil {
return err
}
return nil
}
func toastNotify(title, message, icon string, urgent bool) error {
n := toast.Notification{
AppID: AppName,
Title: title,
Body: message,
Icon: pathAbs(icon),
}
n.Duration = toast.Short
if timeout.Seconds() > 10 {
n.Duration = toast.Long
}
if urgent {
n.Audio = toast.Default
} else {
n.Audio = toast.Silent
}
err := n.Push()
if err != nil {
return err
}
return nil
}
func pngToIco(icon string) (string, error) {
var out string
f, err := os.Open(icon)
if err != nil {
return out, err
}
defer f.Close()
img, err := png.Decode(f)
if err != nil {
return out, err
}
tmp, err := os.CreateTemp(os.TempDir(), "beeep*.ico")
if err != nil {
return out, err
}
defer tmp.Close()
out = tmp.Name()
err = ico.Encode(tmp, img)
if err != nil {
return out, err
}
return out, nil
}