Bogdanp / racket-gui-easy

Declarative GUIs in Racket.
https://docs.racket-lang.org/gui-easy/index.html
134 stars 18 forks source link

Is it possible to detect when the main window closes? #27

Closed cloudrac3r closed 2 years ago

cloudrac3r commented 2 years ago

My program runs a timer in the background to periodically refresh the information displayed in the GUI. When the window is closed, the timer remains running and the process keeps running, invisibly.

Is it possible to detect when the main window closes, so that I can stop the timer and end the process?

As always, thank you so much for everything you've done for the Racket world!

Bogdanp commented 2 years ago

You can do this by wrapping the window in a mixin and augmenting its on-close method:

#lang racket/base

(require racket/class
         (prefix-in gui: racket/gui)
         racket/gui/easy)

(define timer
  (new gui:timer%
       [interval 5000]
       [notify-callback (λ () (displayln "timer called"))]))

(render
 (window
  #:mixin (λ (%)
            (class %
              (super-new)
              (define/augment (on-close)
                (send timer stop))))
  (text "Hello")))
cloudrac3r commented 2 years ago

Fantastic, works perfectly!