1 # General purpose code for being a tray applet
3 # Copyright 2016,2020 Ian Jackson
4 # SPDX-License-Identifier: GPL-3.0-or-later
5 # There is NO WARRANTY.
10 #----- general machinery -----
14 # tk::tktray widget is called .i
19 # applet::setup-tooltip ON-VISIBLE ON-INVISIBLE
20 # to make applet have a tooltip.
22 # ON-VISIBLE and ON-INVISIBLE will be globally eval'd
23 # when the tooltip becomes visible and invisible.
26 # applet::tooltip-set TEXT-MAYBE-MULTILINE
31 # Caller may bind .i.i <ButtonPress-$b>
33 # Alternatively caller may call applet::setup-button-menu $b
34 # which will generate a menu .m$b which the user can configure
35 # and which will automatically be posted and unposted etc.
36 # In this case the caller should arrange that all of their
37 # menus, when an item is selected, call
43 # applet::setimage IMAGE
46 # Alternatively of icon, it may provide other arrangements for
47 # using the provided subwindow. Such a caller should call
48 # applet::setup-subwindow ON-DESTROYING ON-READY
49 # Then the main code will call ON-DESTROYING just before
50 # destroying the inner window and recreating it, and
51 # [concat ON-READY [list ORIENTATION]]
52 # just after. The inner window to use is called .i.i.b.
54 # This uses variables, in the applet namespace,
55 # w h border_colour border_width deforient
56 # These should be set before setup-subwindow is called and not
57 # modified thereafter.
59 # The user of the subwindow machinery may call
60 # applet::subwindow-need-recreate
61 # if for any reason the inner window should be destroyed and recreated.
63 # Alternatively, it may request that a subprocess be spawned
64 # repeatedly with the xid of a suitable window.
65 # applet::setup-subproc GET-CMDLINE
66 # Then the main code will call [concat GET_CMDLINE [list XID ORIENTATION]]
67 # to get the command line to run.
69 # This also uses the same variables as setup-subwindow.
71 namespace eval applet {
76 tktray::icon .i -class example
77 .i configure -docked 1
79 fconfigure stdout -buffering none
80 fconfigure stderr -buffering none
83 # used by both menus and tooltips
85 variable tooltip_offset {9 9}
89 proc setup-button-menu {b} {
90 bind .i.i <ButtonPress> { applet::menubuttonpressed %b %X %Y }
94 proc menubuttonpressed {b x y} {
98 debug::debug "unpost $posted toggle"
101 } elseif {[winfo exists .m$b]} {
104 debug::debug "unpost $posted other"
106 debug::debug "post $b"
117 #----- tooltips -----
119 variable tooltip_on_vis {}
120 variable tooltip_on_invis {}
122 proc tooltip-starttimer {state x y} {
123 variable tooltip_after
125 variable tooltip_inwindow
126 if {$state || $posted || !$tooltip_inwindow} { tooltip-cancel; return }
127 catch { after cancel $tooltip_after }
128 set tooltip_after [after 500 applet::tooltip-show $x $y]
131 proc tooltip-cancel {} {
132 variable tooltip_after
133 variable tooltip_on_invis
134 catch { after cancel $tooltip_after }
135 catch { unset $tooltip_after }
137 uplevel #0 $tooltip_on_invis
140 set tooltip_inwindow 0
142 proc tooltip-enter {state x y} {
143 variable tooltip_inwindow
144 set tooltip_inwindow 1
145 tooltip-starttimer $state $x $y
148 proc tooltip-leave {} {
149 variable tooltip_inwindow
150 set tooltip_inwindow 0
154 proc setup-tooltip {on_vis on_invis} {
155 foreach v {vis invis} {
156 variable tooltip_on_$v [set on_$v]
158 bind .i <Enter> { applet::tooltip-enter %s %X %Y }
159 bind .i <Leave> { applet::tooltip-leave }
160 bind .i <ButtonRelease> {
161 applet::tooltip-cancel
162 applet::tooltip-starttimer %s %X %Y
164 bind .i <Motion> { applet::tooltip-starttimer %s %X %Y }
165 toplevel .tt -background black
167 wm overrideredirect .tt 1
168 label .tt.t -justify left -background {#EEE1B3}
169 pack .tt.t -padx 1 -pady 1
173 proc tooltip-set {s} {
174 .tt.t configure -text $s
177 proc tooltip-show {x y} {
178 variable tooltip_on_vis
179 variable tooltip_offset
180 incr x [lindex $tooltip_offset 0]
181 incr y [lindex $tooltip_offset 1]
182 wm geometry .tt +$x+$y
185 uplevel #0 $tooltip_on_vis
188 #----- simple images -----
190 proc setimage {image} {
191 .i configure -image $image
194 #----- subwindow -----
196 variable subwindow_on_destroying
197 variable subwindow_on_ready
201 variable deforient horizontal
202 variable border_colour darkblue
203 variable border_width 1
204 variable tray_width X
205 variable tray_height X
206 variable orientation vertical
208 proc subwindow-need-recreate {evtype why} {
210 variable innerwindow_after
213 debug::debug "IW-EVENT $evtype $why [winfo reqwidth .i] [winfo reqheight .i] [winfo width .i] [winfo height .i] $orientation"
214 switch -exact $orientation {
215 horizontal { set szv height }
216 vertical { set szv width }
219 set new_sz [winfo req$szv .i]
220 if {![string compare $new_sz [set tray_$szv]]} {
223 set tray_$szv $new_sz
224 # switch -exact -- $evtype 35 { return }
225 if {[info exists innerwindow_after]} return
226 set innerwindow_after [after idle applet::innerwindow-resetup]
229 proc innerwindow-resetup {} {
230 variable innerwindow_after
231 variable subwindow_on_destroying
232 variable subwindow_on_ready
233 variable border_colour
234 variable border_width
237 unset innerwindow_after
241 if {![winfo exists .i.i]} return
242 destroy [frame .i.i.make-exist]
244 uplevel #0 $subwindow_on_destroying
245 catch { destroy .i.i.b }
247 set orientation [.i orientation]
248 debug::debug "orientation $orientation"
249 if {![string compare $orientation unknown]} {
250 set orientation $deforient
252 .i configure -image applet::innerwindow-ph-$orientation
254 frame .i.i.b -background $border_colour -bd $border_width
255 pack .i.i.b -fill both -side left -expand 1
257 uplevel #0 $subwindow_on_ready [list $orientation]
260 proc setup-subwindow {on_destroying on_ready} {
264 foreach v {on_destroying on_ready} {
265 variable subwindow_$v [set $v]
268 image create photo applet::innerwindow-ph-horizontal -width $w -height 2
269 image create photo applet::innerwindow-ph-vertical -width 2 -height $h
270 .i configure -image applet::innerwindow-ph-horizontal
272 destroy [frame .i.make-exist]
273 #destroy [frame .i.i.make-exist]
274 bind .i <<IconConfigure>> {
275 applet::subwindow-need-recreate %T "%T i=%i k=%K N=%N R=%R S=%S k=%k m=%m d=%d s=%s a=%a b=%b c=%c f=%f w,h=%w,%h o=%o p=%p t=%t x,y=%x,%y B=%B D=%D E=%E P=%P W=%W X,Y=%X,%Y"
279 #----- subprocess -----
281 variable subproc none
282 variable ratelimit {}
284 proc setup-subproc {get_cmdline} {
285 variable subproc_get_cmdline $get_cmdline
286 setup-subwindow applet::subproc-destroying applet::subproc-ready
289 proc subproc-destroying {} {
291 debug::debug "DESTROYING $subproc"
293 catch { destroy .i.i.b.c }
295 switch -exact $subproc {
298 default { kill $subproc; set subproc old }
302 proc subproc-ready {orientation} {
304 variable subproc_orientation $orientation
305 debug::debug "READY $subproc"
307 frame .i.i.b.c -container 1 -background orange
308 pack .i.i.b.c -fill both -side left -expand 1
310 switch -exact $subproc {
318 error "unexpected state $subproc"
321 debug::debug "READY-done $subproc"
327 variable subproc_get_cmdline
328 variable subproc_orientation
330 set id [winfo id .i.i.b.c]
331 set cmd [uplevel #0 $subproc_get_cmdline [list $id $subproc_orientation]]
333 debug::debug "RUN-CHILD $subproc"
334 set now [clock seconds]
335 lappend ratelimit $now
336 while {[lindex $ratelimit 0] < {$now - 10}} {
337 set ratelimit [lrange $ratelimit 1 end]
339 if {[llength $ratelimit] > 10} {
340 puts stderr "crashing repeatedly, quitting $ratelimit"
345 set subproc [subproc::fork applet::child-died {
346 execl [lindex $cmd 0] [lrange $cmd 1 end]
348 debug::debug "FORKED $subproc"
351 proc child-died {how how2} {
352 debug::debug "DIED $how $how2"
354 switch -exact $subproc {
361 subwindow-need-recreate child-died child-died