-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifyicon.go
333 lines (267 loc) · 7.65 KB
/
notifyicon.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"syscall"
"unsafe"
)
import (
"github.com/lxn/win"
)
const notifyIconWindowClass = `\o/ Walk_NotifyIcon_Class \o/`
func init() {
MustRegisterWindowClass(notifyIconWindowClass)
}
func notifyIconWndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) (result uintptr) {
// Retrieve our *NotifyIcon from the message window.
ptr := win.GetWindowLongPtr(hwnd, win.GWLP_USERDATA)
ni := (*NotifyIcon)(unsafe.Pointer(ptr))
switch lParam {
case win.WM_LBUTTONDOWN:
ni.publishMouseEvent(&ni.mouseDownPublisher, LeftButton)
case win.WM_LBUTTONUP:
ni.publishMouseEvent(&ni.mouseUpPublisher, LeftButton)
case win.WM_RBUTTONDOWN:
ni.publishMouseEvent(&ni.mouseDownPublisher, RightButton)
case win.WM_RBUTTONUP:
ni.publishMouseEvent(&ni.mouseUpPublisher, RightButton)
win.SendMessage(hwnd, msg, wParam, win.WM_CONTEXTMENU)
case win.WM_CONTEXTMENU:
if ni.contextMenu.Actions().Len() == 0 {
break
}
win.SetForegroundWindow(hwnd)
var p win.POINT
if !win.GetCursorPos(&p) {
lastError("GetCursorPos")
}
actionId := uint16(win.TrackPopupMenuEx(
ni.contextMenu.hMenu,
win.TPM_NOANIMATION|win.TPM_RETURNCMD,
p.X,
p.Y,
hwnd,
nil))
if actionId != 0 {
if action, ok := actionsById[actionId]; ok {
action.raiseTriggered()
}
}
return 0
}
return win.DefWindowProc(hwnd, msg, wParam, lParam)
}
// NotifyIcon represents an icon in the taskbar notification area.
type NotifyIcon struct {
id uint32
hWnd win.HWND
contextMenu *Menu
icon *Icon
toolTip string
visible bool
mouseDownPublisher MouseEventPublisher
mouseUpPublisher MouseEventPublisher
}
// NewNotifyIcon creates and returns a new NotifyIcon.
//
// The NotifyIcon is initially not visible.
func NewNotifyIcon() (*NotifyIcon, error) {
// Create the message-only window for the NotifyIcon.
hWnd := win.CreateWindowEx(
0,
syscall.StringToUTF16Ptr(notifyIconWindowClass),
nil,
0,
0,
0,
0,
0,
win.HWND_MESSAGE,
0,
0,
nil)
if hWnd == 0 {
return nil, lastError("CreateWindowEx")
}
// Add our notify icon to the status area and make sure it is hidden.
nid := win.NOTIFYICONDATA{
HWnd: hWnd,
UFlags: win.NIF_MESSAGE | win.NIF_STATE,
DwState: win.NIS_HIDDEN,
DwStateMask: win.NIS_HIDDEN,
UCallbackMessage: notifyIconMessageId,
}
nid.CbSize = uint32(unsafe.Sizeof(nid))
if !win.Shell_NotifyIcon(win.NIM_ADD, &nid) {
return nil, newError("Shell_NotifyIcon")
}
// We want XP-compatible message behavior.
nid.UVersion = win.NOTIFYICON_VERSION
if !win.Shell_NotifyIcon(win.NIM_SETVERSION, &nid) {
return nil, newError("Shell_NotifyIcon")
}
// Create and initialize the NotifyIcon already.
menu, err := NewMenu()
if err != nil {
return nil, err
}
ni := &NotifyIcon{
id: nid.UID,
hWnd: hWnd,
contextMenu: menu,
}
// Set our *NotifyIcon as user data for the message window.
win.SetWindowLongPtr(hWnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(ni)))
return ni, nil
}
func (ni *NotifyIcon) notifyIconData() *win.NOTIFYICONDATA {
nid := &win.NOTIFYICONDATA{
UID: ni.id,
HWnd: ni.hWnd,
}
nid.CbSize = uint32(unsafe.Sizeof(*nid))
return nid
}
// Dispose releases the operating system resources associated with the
// NotifyIcon.
//
// The associated Icon is not disposed of.
func (ni *NotifyIcon) Dispose() error {
if ni.hWnd == 0 {
return nil
}
nid := ni.notifyIconData()
if !win.Shell_NotifyIcon(win.NIM_DELETE, nid) {
return newError("Shell_NotifyIcon")
}
if !win.DestroyWindow(ni.hWnd) {
return lastError("DestroyWindow")
}
ni.hWnd = 0
return nil
}
func (ni *NotifyIcon) showMessage(title, info string, iconType uint32) error {
nid := ni.notifyIconData()
nid.UFlags = win.NIF_INFO
nid.DwInfoFlags = iconType
copy(nid.SzInfoTitle[:], syscall.StringToUTF16(title))
copy(nid.SzInfo[:], syscall.StringToUTF16(info))
if !win.Shell_NotifyIcon(win.NIM_MODIFY, nid) {
return newError("Shell_NotifyIcon")
}
return nil
}
// ShowMessage displays a neutral message balloon above the NotifyIcon.
//
// The NotifyIcon must be visible before calling this method.
func (ni *NotifyIcon) ShowMessage(title, info string) error {
return ni.showMessage(title, info, win.NIIF_NONE)
}
// ShowInfo displays an info message balloon above the NotifyIcon.
//
// The NotifyIcon must be visible before calling this method.
func (ni *NotifyIcon) ShowInfo(title, info string) error {
return ni.showMessage(title, info, win.NIIF_INFO)
}
// ShowWarning displays a warning message balloon above the NotifyIcon.
//
// The NotifyIcon must be visible before calling this method.
func (ni *NotifyIcon) ShowWarning(title, info string) error {
return ni.showMessage(title, info, win.NIIF_WARNING)
}
// ShowError displays an error message balloon above the NotifyIcon.
//
// The NotifyIcon must be visible before calling this method.
func (ni *NotifyIcon) ShowError(title, info string) error {
return ni.showMessage(title, info, win.NIIF_ERROR)
}
// ShowCustom displays a custom icon message balloon above the NotifyIcon.
//
// The NotifyIcon must be visible before calling this method.
func (ni *NotifyIcon) ShowCustom(title, info string) error {
return ni.showMessage(title, info, win.NIIF_USER)
}
// ContextMenu returns the context menu of the NotifyIcon.
func (ni *NotifyIcon) ContextMenu() *Menu {
return ni.contextMenu
}
// Icon returns the Icon of the NotifyIcon.
func (ni *NotifyIcon) Icon() *Icon {
return ni.icon
}
// SetIcon sets the Icon of the NotifyIcon.
func (ni *NotifyIcon) SetIcon(icon *Icon) error {
if icon == ni.icon {
return nil
}
nid := ni.notifyIconData()
nid.UFlags = win.NIF_ICON
if icon == nil {
nid.HIcon = 0
} else {
nid.HIcon = icon.hIcon
}
if !win.Shell_NotifyIcon(win.NIM_MODIFY, nid) {
return newError("Shell_NotifyIcon")
}
ni.icon = icon
return nil
}
// ToolTip returns the tool tip text of the NotifyIcon.
func (ni *NotifyIcon) ToolTip() string {
return ni.toolTip
}
// SetToolTip sets the tool tip text of the NotifyIcon.
func (ni *NotifyIcon) SetToolTip(toolTip string) error {
if toolTip == ni.toolTip {
return nil
}
nid := ni.notifyIconData()
nid.UFlags = win.NIF_TIP
copy(nid.SzTip[:], syscall.StringToUTF16(toolTip))
if !win.Shell_NotifyIcon(win.NIM_MODIFY, nid) {
return newError("Shell_NotifyIcon")
}
ni.toolTip = toolTip
return nil
}
// Visible returns if the NotifyIcon is visible.
func (ni *NotifyIcon) Visible() bool {
return ni.visible
}
// SetVisible sets if the NotifyIcon is visible.
func (ni *NotifyIcon) SetVisible(visible bool) error {
if visible == ni.visible {
return nil
}
nid := ni.notifyIconData()
nid.UFlags = win.NIF_STATE
nid.DwStateMask = win.NIS_HIDDEN
if !visible {
nid.DwState = win.NIS_HIDDEN
}
if !win.Shell_NotifyIcon(win.NIM_MODIFY, nid) {
return newError("Shell_NotifyIcon")
}
ni.visible = visible
return nil
}
func (ni *NotifyIcon) publishMouseEvent(publisher *MouseEventPublisher, button MouseButton) {
var p win.POINT
if !win.GetCursorPos(&p) {
lastError("GetCursorPos")
}
publisher.Publish(int(p.X), int(p.Y), button)
}
// MouseDown returns the event that is published when a mouse button is pressed
// while the cursor is over the NotifyIcon.
func (ni *NotifyIcon) MouseDown() *MouseEvent {
return ni.mouseDownPublisher.Event()
}
// MouseDown returns the event that is published when a mouse button is released
// while the cursor is over the NotifyIcon.
func (ni *NotifyIcon) MouseUp() *MouseEvent {
return ni.mouseUpPublisher.Event()
}