linux & mac support, refactoring on minimize

This commit is contained in:
2026-08-29 21:13:29 +02:00
parent c7137fc11f
commit 9f87eae8aa
9 changed files with 1760 additions and 535 deletions
+31 -17
View File
@@ -5,10 +5,12 @@
;; This example demonstrates the two common "close to tray" behaviours:
;;
;; * minimize: handled by racket-tray through #:hide-on-minimize?
;; * minimize: handled portably by racket-tray through
;; #:hide-on-minimize?
;; * close [X]: handled by frame%'s normal on-close callback
;;
;; The frame remains alive in both cases because the tray icon uses its HWND.
;; The frame remains alive in both cases. Only the tray menu's Exit action
;; terminates the application.
(define simple%
(class frame%
(super-new [label "Racket Tray"]
@@ -17,7 +19,7 @@
(define lbl
(new message%
[label "Counting tray clicks"]
[label "Counting tray activations"]
[parent this]
[auto-resize #t]
[stretchable-width #t]))
@@ -27,11 +29,11 @@
(define/public (count-next)
(set! count (add1 count))
(send lbl set-label
(format "Counting tray clicks: ~a" count)))
(format "Counting tray activations: ~a" count)))
;; Clicking the window's close button hides the frame instead of
;; destroying it. The tray menu's Exit item terminates the application.
(define/override (on-close)
(define/augment (on-close)
(send this show #f))))
(define frame (new simple%))
@@ -42,32 +44,44 @@
; post : frame is visible and no longer iconized.
; result : Unspecified.
; internals:
; A frame hidden in response to SIZE_MINIMIZED can still retain its
; iconized state, so show #t is followed by iconize #f when needed.
; The portable minimize watcher hides an iconized frame. Explicitly
; de-iconizing here makes restoring predictable on every platform.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (show-frame)
;; A frame hidden after a minimize can still be iconized. Make sure it is
;; restored when it is opened from the tray.
(send frame show #t)
(when (send frame is-iconized?)
(send frame iconize #f)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; goal : Handle every symbolic action generated by the tray icon/menu.
; pre : action is one of the identifiers installed with tray-set-menu!.
; post : 'open restores the frame and increments the counter; 'exit removes
; the tray icon and terminates the example.
; result : Unspecified.
; internals:
; The same callback is used for menu selections and for native direct
; activation on platforms that support the configured default action.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (tray-action action)
(case action
[(open)
(send frame count-next)
(show-frame)]
[(exit)
(tray-close tray)
(exit)]))
(define tray
(mk-tray frame
"simple.png"
(λ ()
(send frame count-next)
(show-frame))
(list tray-action 'open)
#:hide-on-minimize? #t))
(tray-set-menu!
tray
(list
(list "Open" show-frame)
(list 'open "Open")
'separator
(list "Exit"
(λ ()
(tray-close tray)
(exit)))))
(list 'exit "Exit")))
(send frame show #t)