I have a racket/gui question. Does anyone know how I can safely close a frame%
? The documentation appears to say that the default on-exit handler just hides the window, but I want the frame to actually be closed.
Does (send _a-top-level-window_ on-close)
work?
I’m not sure but I think the on-exit
handler sends on-close
, wouldn’t that just be the same thing?
I don’t think on-close
actually does anything unless you augment it.
The exit handler is called when the application stops. On the other hand the close
handler is called when a frame (window) needs to close. For an application with more than one frame, it’s not the same.
> I don’t think on-close
actually does anything unless you augment it. Hmm. So we need a way to provoke such an event.
I understand, but the documentation is confusing me as it seems to imply that if you click the X to close the window that it is not actually closed but is just hidden. I could just be getting confused.
#lang racket
(require framework
racket/gui)
(application:current-app-name "Test")
(define main-frame%
(class frame%
(super-new)
(define (on-close)
(println "Closing"))
(augment on-close)
))
(define frame (new main-frame% [label "Test"]))
(send frame show #t)
If I run this code in DrRacket and then click the X on the displayed window it disappears, but if I then type (send frame show #t)
in DrRacket, the window is shown again, indicating the program has not stopped but just been hidden.
I see. Hmm. Maybe the frame is kept until it is garbage collected? But how do we test that?
I was looking for a close
or dispose
message you could send the frame, but I haven’t seen one yet. Maybe it works differently if you run it from the command line.
I can’t find one either.
I was thinking that perhaps: > (send frame show #f) > (send frame on-close) > (set! frame #f) Might make the garbage collector remove the window.
Hmm.. Maybe, if I were thinking in a native windows way I would say the event dispatch loop was not terminating. Setting the frame to false might be hazardous but I’ll give it a try.
Maybe we can test this using “Wills”? (Looking at the example: https://docs.racket-lang.org/reference/willexecutor.html )
@mark.warren There’s no difference between “closed” and “hidden and not referenced, so can be GCed” for a frame%
object. But if an application needs to know when the user clicked the close box for a frame%
, that’s what on-close
is for.
Ooh, blimey that is going to be some reading. @mflatt So, if I re-run the above example will that GC the original frame?
Yes, at least eventually
Ah right so I’m concerned about nothing then, thanks.
@soegaard2 Thanks for the reading matter anyway, looks like I don’t need to worry, but that wills stuff looks interesting.
@wanpeebaw has joined the channel