blob: 6948ba07451b0e651bdf5dad2c77fcffed8f004e (
plain)
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
|
# systray.tcl --
# This file defines the 'tk systray' command for icon display and manipulation
# in the system tray on X11, Windows, and macOS, and the 'tk sysnotify' command
# for system alerts on each platform. It implements an abstraction layer that
# presents a consistent API across the three platforms.
# Copyright © 2020 Kevin Walzer/WordTech Communications LLC.
# Copyright © 2020 Eric Boudaillier.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# Pure-Tcl system tooltip window for use with system tray icon if native
# implementation not available.
namespace eval ::tk::systray {
variable _created 0
variable _options {-image "" -text "" -button1 "" -button3 ""}
variable _current {}
variable _ico
proc _balloon {w help} {
bind $w <Any-Enter> "after 1000 [list ::tk::systray::_balloon_show %W [list $help]]"
bind $w <Any-Leave> "destroy %W._balloon"
}
proc _balloon_show {w arg} {
if {[winfo containing [winfo pointerxy .]] ne $w} {
return
}
set top $w._balloon
catch {destroy $top}
toplevel $top -bg black
wm overrideredirect $top 1
if {[tk windowingsystem] eq "aqua"} {
::tk::unsupported::MacWindowStyle style $top help none
}
pack [message $top._txt -aspect 10000 -text $arg]
set wmx [winfo rootx $w]
set wmy [expr {[winfo rooty $w] + [winfo height $w]}]
wm geometry $top [winfo reqwidth $top._txt]x[winfo reqheight $top._txt]+$wmx+$wmy
raise $top
}
proc _win_callback {msg icn} {
variable _current
switch -exact -- $msg {
WM_LBUTTONDOWN {
uplevel #0 [dict get $_current -button1]
}
WM_RBUTTONDOWN {
uplevel #0 [dict get $_current -button3]
}
}
}
namespace export create configure destroy
namespace ensemble create
}
# Pure-Tcl system notification window for use if native implementation not available.
namespace eval ::tk::sysnotify:: {
proc _notifywindow {title msg} {
catch {destroy ._notify}
set w [toplevel ._notify]
if {[tk windowingsystem] eq "aqua"} {
::tk::unsupported::MacWindowStyle style $w utility {hud closeBox resizable}
wm title $w "Alert"
}
if {[tk windowingsystem] eq "win32"} {
wm attributes $w -toolwindow true
wm title $w "Alert"
}
label $w.l -bg gray30 -fg white -image ::tk::icons::information
pack $w.l -fill both -expand yes -side left
message $w.message -aspect 150 -bg gray30 -fg white -aspect 150 -text $title\n\n$msg -width 280
pack $w.message -side right -fill both -expand yes
if {[tk windowingsystem] eq "x11"} {
wm overrideredirect $w true
}
wm attributes $w -alpha 0.0
set xpos [expr {[winfo screenwidth $w] - 325}]
wm geometry $w +$xpos+30
::tk::sysnotify::_fade_in $w
after 3000 ::tk::sysnotify::_fade_out $w
}
#Fade and destroy window.
proc _fade_out {w} {
catch {
set prev_degree [wm attributes $w -alpha]
set new_degree [expr {$prev_degree - 0.05}]
set current_degree [wm attributes $w -alpha $new_degree]
if {$new_degree > 0.0 && $new_degree != $prev_degree} {
after 10 [list ::tk::sysnotify::_fade_out $w]
} else {
destroy $w
}
}
}
#Fade the window into view.
proc _fade_in {w} {
catch {
raise $w
wm attributes $w -topmost 1
set prev_degree [wm attributes $w -alpha]
set new_degree [expr {$prev_degree + 0.05}]
set current_degree [wm attributes $w -alpha $new_degree]
focus -force $w
if {$new_degree < 0.9 && $new_degree != $prev_degree} {
after 10 [list ::tk::sysnotify::_fade_in $w]
}
}
}
namespace export *
}
# tk systray --
# This procedure creates an icon display in the platform-specific system tray.
#
# Subcommands:
#
# create - create systray icon.
# Arguments:
# -image - Tk image to display.
# -text - string to display in tooltip over image.
# -button1 - Tcl proc to invoke on <Button-1> event.
# -button3 - Tcl proc to invoke on <Button-3> event.
#
# configure - change one of the systray properties.
# Arguments (Any or all can be called):
# -image - Tk image to update.
# -text - string to update.
# -button1 - Tcl proc to change for <Button-1> event.
# -button3 - Tcl proc to change for <Button-3> event.
#
# destroy - destroy systray icon.
# Arguments:
# none.
proc ::tk::systray::create {args} {
variable _created
variable _options
variable _current
variable _ico
if {$_created} {
return -code error -errorcode {TK SYSTRAY CREATE} "only one system tray icon supported per interpeter"
}
_check_options $args 0
if {![dict exists $args -image]} {
return -code error -errorcode {TK SYSTRAY CREATE} "missing required option \"-image\""
}
set values [dict merge $_options $args]
try {
switch -- [tk windowingsystem] {
"win32" {
set _ico [_systray add -image [dict get $values -image] \
-text [dict get $values -text] \
-callback [list ::tk::systray::_win_callback %m %i]]
}
"x11" {
_systray ._tray -image [dict get $values -image] -visible true
_balloon ._tray [dict get $values -text]
bind ._tray <Button-1> [dict get $values -button1]
bind ._tray <Button-3> [dict get $values -button3]
}
"aqua" {
_systray create [dict get $values -image] [dict get $values -text] \
[dict get $values -button1] [dict get $values -button3]
}
}
} on ok {} {
set _current $values
set _created 1
return
} on error {msg opts} {
return -code error -errorcode [dict get $opts -errorcode] $msg
}
}
# Modify the systray icon.
proc ::tk::systray::configure {args} {
variable _created
variable _options
variable _current
variable _ico
if {!$_created} {
return -code error -errorcode {TK SYSTRAY CREATE} "systray not created"
}
_check_options $args 1
if {[llength $args] == 0} {
return $_current
} elseif {[llength $args] == 1} {
return [dict get $_current [lindex $args 0]]
}
set values [dict merge $_current $args]
try {
switch -- [tk windowingsystem] {
"win32" {
if {[dict exists $args -image]} {
_systray modify $_ico -image [dict get $args -image]
}
if {[dict exists $args -text]} {
_systray modify $_ico -text [dict get $args -text]
}
}
"x11" {
if {[dict exists $args -image]} {
._tray configure -image [dict get $args -image]
}
if {[dict exists $args -text]} {
_balloon ._tray [dict get $args -text]
}
if {[dict exists $args -button1]} {
bind ._tray <Button-1> [dict get $args -button1]
}
if {[dict exists $args -button3]} {
bind ._tray <Button-3> [dict get $args -button3]
}
}
"aqua" {
foreach {key opt} {image -image text \
-text b1_callback -button1 b3_callback -button3} {
if {[dict exists $args $opt]} {
_systray modify $key [dict get $args $opt]
}
}
}
}
} on ok {} {
set _current $values
return
} on error {msg opts} {
return -code error -errorcode [dict get $opts -errorcode] $msg
}
}
# Remove the systray icon.
proc ::tk::systray::destroy {} {
variable _created
variable _current
variable _ico
if {!$_created} {
return -code error -errorcode {TK SYSTRAY DESTROY} "systray not created"
}
switch -- [tk windowingsystem] {
"win32" {
_systray delete $_ico
set _ico ""
}
"x11" {
::destroy ._tray
}
"aqua" {
_systray destroy
}
}
set _created 0
set _current {}
return
}
# Check systray options
proc ::tk::systray::_check_options {argsList singleOk} {
variable _options
set len [llength $argsList]
while {[llength $argsList] > 0} {
set opt [lindex $argsList 0]
if {![dict exists $_options $opt]} {
tailcall return -code error -errorcode {TK SYSTRAY OPTION} \
"unknown option \"$opt\": must be -image, -text, -button1 or -button3"
}
if {[llength $argsList] == 1 && !($len == 1 && $singleOk)} {
tailcall return -code error -errorcode {TK SYSTRAY OPTION} \
"missing value for option \"$opt\""
}
set argsList [lrange $argsList 2 end]
}
}
# tk sysnotify --
# This procedure implements a platform-specific system notification alert.
#
# Arguments:
# title - main text of alert.
# message - body text of alert.
proc ::tk::sysnotify::sysnotify {title message} {
switch -- [tk windowingsystem] {
"win32" {
if {!$::tk::systray::_created} {
error "must create a system tray icon with the \"tk systray\" command first"
}
_sysnotify notify $::tk::systray::_ico $title $message
}
"x11" {
if {[info commands ::tk::sysnotify::_sysnotify] eq ""} {
_notifywindow $title $message
} else {
_sysnotify $title $message
}
}
"aqua" {
_sysnotify $title $message
}
}
return
}
#Add these commands to the tk command ensemble: tk systray, tk sysnotify
#Thanks to Christian Gollwitzer for the guidance here
set map [namespace ensemble configure tk -map]
dict set map systray ::tk::systray
dict set map sysnotify ::tk::sysnotify::sysnotify
namespace ensemble configure tk -map $map
|