-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
notify_windows.go
206 lines (177 loc) · 4.45 KB
/
notify_windows.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package zenity
import (
"encoding/xml"
"math/rand"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"text/template"
"time"
"unsafe"
"github.com/ncruces/zenity/internal/win"
"github.com/ncruces/zenity/internal/zenutil"
)
func notify(text string, opts options) error {
if opts.ctx != nil && opts.ctx.Err() != nil {
return opts.ctx.Err()
}
file, err := os.CreateTemp("", "*.ps1")
if err != nil {
return err
}
defer os.Remove(file.Name())
data := struct {
Title string
Text string
Icon string
}{
Title: "zenity",
Text: text,
}
if opts.title != nil {
data.Title = *opts.title
}
if i, ok := opts.icon.(string); ok {
_, err := os.Stat(i)
if err != nil {
return err
}
data.Icon, err = filepath.Abs(i)
if err != nil {
return err
}
}
err = script.Execute(file, data)
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
var cmd *exec.Cmd
if opts.ctx != nil {
cmd = exec.CommandContext(opts.ctx, "PowerShell", "-ExecutionPolicy", "Bypass", "-File", file.Name())
} else {
cmd = exec.Command("PowerShell", "-ExecutionPolicy", "Bypass", "-File", file.Name())
}
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err = cmd.Run()
if opts.ctx != nil && opts.ctx.Err() != nil {
return opts.ctx.Err()
}
if err != nil {
return shellNotify(text, opts)
}
return nil
}
func shellNotify(text string, opts options) error {
var args win.NOTIFYICONDATA
args.StructSize = uint32(unsafe.Sizeof(args))
args.ID = rand.Uint32()
args.Flags = win.NIF_INFO
args.State = win.NIS_HIDDEN
info := syscall.StringToUTF16(text)
copy(args.Info[:len(args.Info)-1], info)
if opts.title != nil {
title := syscall.StringToUTF16(*opts.title)
copy(args.InfoTitle[:len(args.InfoTitle)-1], title)
}
switch opts.icon {
case InfoIcon, QuestionIcon:
args.InfoFlags |= win.NIIF_INFO
case WarningIcon:
args.InfoFlags |= win.NIIF_WARNING
case ErrorIcon:
args.InfoFlags |= win.NIIF_ERROR
default:
icon, _ := getIcon(opts.icon)
if icon.handle != 0 {
defer icon.delete()
args.Icon = icon.handle
args.Flags |= win.NIF_ICON
args.InfoFlags |= win.NIIF_USER
}
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if !win.ShellNotifyIcon(win.NIM_ADD, &args) {
return wtsMessage(text, opts)
}
major, minor, _ := win.RtlGetNtVersionNumbers()
// On Windows 7 (6.1) and lower, wait up to 10 seconds to clean up.
if major < 6 || major == 6 && minor < 2 {
if opts.ctx != nil && opts.ctx.Done() != nil {
select {
case <-opts.ctx.Done():
case <-time.After(10 * time.Second):
}
} else {
time.Sleep(10 * time.Second)
}
}
win.ShellNotifyIcon(win.NIM_DELETE, &args)
return nil
}
func wtsMessage(text string, opts options) error {
var flags uint32
switch opts.icon {
case ErrorIcon:
flags |= win.MB_ICONERROR
case QuestionIcon:
flags |= win.MB_ICONQUESTION
case WarningIcon:
flags |= win.MB_ICONWARNING
case InfoIcon:
flags |= win.MB_ICONINFORMATION
}
title := opts.title
if title == nil {
title = ptr("Notification")
}
timeout := zenutil.Timeout
if timeout == 0 {
timeout = 10
}
ptext := syscall.StringToUTF16(text)
ptitle := syscall.StringToUTF16(*title)
var res uint32
return win.WTSSendMessage(
win.WTS_CURRENT_SERVER_HANDLE, win.WTS_CURRENT_SESSION,
&ptitle[0], 2*len(ptitle), &ptext[0], 2*len(ptext),
flags, timeout, &res, false)
}
var script = template.Must(template.New("").Funcs(template.FuncMap{"xml": func(s string) string {
var buf strings.Builder
xml.EscapeText(&buf, []byte(s))
return buf.String()
}}).Parse("\xEF\xBB\xBF" + `
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime]
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
$title = @"
{{.Title}}
"@
$data = @"
<toast activationType="protocol">
<visual>
<binding template="ToastGeneric">
{{- if .Text}}
<text><![CDATA[{{.Text}}]]></text>
{{- end}}
{{- if .Icon}}
<image placement="appLogoOverride" src="{{xml .Icon}}" />
{{- end}}
</binding>
</visual>
<audio src="ms-winsoundevent:Notification.Default" />
</toast>
"@
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($data)
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($title).Show($toast)
`))