File: gui.rkt

package info (click to toggle)
racket-mode 20201227git0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,040 kB
  • sloc: lisp: 9,808; makefile: 55
file content (42 lines) | stat: -rw-r--r-- 1,368 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#lang at-exp racket/base

(require (only-in racket/format ~a)
         "util.rkt")

(provide gui-required?
         require-gui
         txt/gui)

(define root-eventspace #f) ;#f until racket/gui/base required first time

(define (gui-required?)
  (and root-eventspace #t))

;; Extra explanation for situations like issue 93, entering `(require
;; redex)` in the REPL, as opposed to having it in a .rkt file.
(define more-explanation
  @~a{The namespace was reset. Any `require`s you entered in the REPL were "undone".
      This includes the `require` you just entered. You may want to enter it again.})

;; This must be called from the main thread, under the main custodian!
(define (require-gui in-repl?)
  (when (gui-required?)
    (error 'require-gui "Already required"))
  (display-commented "On-demand, one-time instantiation of racket/gui/base.")
  (when in-repl?
    (display-commented more-explanation))
  (define current-eventspace (gui-dyn-req 'current-eventspace))
  (define make-eventspace    (gui-dyn-req 'make-eventspace))
  (set! root-eventspace (make-eventspace))
  (current-eventspace root-eventspace))

;; Like mz/mr from racket/sandbox.
(define-syntax txt/gui
  (syntax-rules ()
    [(_ txtval guisym)
     (if (gui-required?)
         (gui-dyn-req 'guisym)
         txtval)]))

(define (gui-dyn-req sym)
  (dynamic-require 'racket/gui/base sym))