Files
racket-tray/examples/simple.rkt
T

88 lines
2.6 KiB
Racket

#lang racket/gui
;(require racket-tray)
(require "../main.rkt")
;; This example demonstrates the two common "close to tray" behaviours:
;;
;; * 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. Only the tray menu's Exit action
;; terminates the application.
(define simple%
(class frame%
(super-new [label "Racket Tray"]
[width 400]
[height 250])
(define lbl
(new message%
[label "Counting tray activations"]
[parent this]
[auto-resize #t]
[stretchable-width #t]))
(define count 0)
(define/public (count-next)
(set! count (add1 count))
(send lbl set-label
(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/augment (on-close)
(send this show #f))))
(define frame (new simple%))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; goal : Show and restore the example frame from the tray.
; pre : frame has been created and has not been destroyed.
; post : frame is visible and no longer iconized.
; result : Unspecified.
; internals:
; The portable minimize watcher hides an iconized frame. Explicitly
; de-iconizing here makes restoring predictable on every platform.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (show-frame)
(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"
(list tray-action 'open)
#:hide-on-minimize? #t))
(tray-set-menu!
tray
(list
(list 'open "Open")
'separator
(list 'exit "Exit")))
(send frame show #t)