-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathnotify_windows_compat.go
More file actions
109 lines (88 loc) · 1.86 KB
/
notify_windows_compat.go
File metadata and controls
109 lines (88 loc) · 1.86 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
//go:build windows && !go1.21 && !linux && !freebsd && !netbsd && !openbsd && !darwin && !js
package beeep
import (
"fmt"
"image/png"
"os"
"time"
"github.com/sergeymakinen/go-ico"
"github.com/tadvi/systray"
)
var isWindows10 = false
// 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 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 {
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")
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
}