13
13
14
14
@centered[@image[(build-path media "1.1-hello-world.png " ) #:scale 0.5 ]]
15
15
16
- @racketblock[
16
+ @codeblock|{
17
+ #lang racket/base
18
+
19
+ (require racket/gui/easy)
20
+
17
21
(window
18
22
(text "Hello, World! " ))
19
- ]
23
+ }|
20
24
21
25
The code above describes a view hierarchy rooted in a window that
22
26
contains the text ``Hello, World!''. By itself, it doesn't do much,
23
27
but you can take it and pass it to @racket[render] to convert it into
24
28
a native GUI:
25
29
26
- @racketblock[
30
+ @codeblock|{
31
+ #lang racket/base
32
+
33
+ (require racket/gui/easy)
34
+
27
35
(render
28
36
(window
29
37
(text "Hello, World! " )))
30
- ]
38
+ }|
31
39
32
40
@section{Counter}
33
41
34
42
@centered[@image[(build-path media "1.2-counter.png " ) #:scale 0.5 ]]
35
43
36
44
State in @tt{gui-easy} is held by @tech{observables}.
37
45
38
- @racketblock[
39
- (define |@count| (|@| 0 ))
46
+ @codeblock|{
47
+ #lang racket/base
48
+
49
+ (require racket/gui/easy
50
+ racket/gui/easy/operator)
51
+
52
+ (define @count (@ 0 ))
40
53
(render
41
54
(window
42
55
(hpanel
43
- (button "- " (λ () (| @count| . <~ . sub1)))
44
- (text (| @count| . ~> . number->string))
45
- (button "+ " (λ () (| @count| . <~ . add1))))))
46
- ]
56
+ (button "- " (λ () (@count . <~ . sub1)))
57
+ (text (@count . ~> . number->string))
58
+ (button "+ " (λ () (@count . <~ . add1))))))
59
+ }|
47
60
48
61
Here we define an observable called @racket[|@count|] that holds the
49
62
current value of a counter. The two @racket[button]s change the value
@@ -58,22 +71,27 @@ laid out horizontally by the @racket[hpanel].
58
71
Since views are at their core just descriptions of a GUI, it's easy to
59
72
abstract over them and make them reusable.
60
73
61
- @racketblock[
62
- (define (counter |@count| action)
74
+ @codeblock|{
75
+ #lang racket/base
76
+
77
+ (require racket/gui/easy
78
+ racket/gui/easy/operator)
79
+
80
+ (define (counter @count action)
63
81
(hpanel
64
82
(button "- " (λ () (action sub1)))
65
- (text (| @count| . ~> . number->string))
83
+ (text (@count . ~> . number->string))
66
84
(button "+ " (λ () (action add1)))))
67
85
68
- (define | @counter-1| (|@| 0 ))
69
- (define | @counter-2| (|@| 0 ))
86
+ (define @counter-1 (@ 0 ))
87
+ (define @counter-2 (@ 0 ))
70
88
71
89
(render
72
90
(window
73
91
(vpanel
74
- (counter | @counter-1| (λ (proc) (| @counter-1| . <~ . proc)))
75
- (counter | @counter-2| (λ (proc) (| @counter-2| . <~ . proc))))))
76
- ]
92
+ (counter @counter-1 (λ (proc) (@counter-1 . <~ . proc)))
93
+ (counter @counter-2 (λ (proc) (@counter-2 . <~ . proc))))))
94
+ }|
77
95
78
96
@section{Dynamic Counters}
79
97
@@ -82,8 +100,13 @@ abstract over them and make them reusable.
82
100
Taking the previous example further, we can render a dynamic list of
83
101
counters.
84
102
85
- @racketblock[
86
- (define |@counters| (|@| '((0 . 0 ))))
103
+ @codeblock|{
104
+ #lang racket/base
105
+
106
+ (require racket/gui/easy
107
+ racket/gui/easy/operator)
108
+
109
+ (define @counters (@ '((0 . 0 ))))
87
110
88
111
(define (append-counter counts)
89
112
(define next-id (add1 (apply max (map car counts))))
@@ -95,11 +118,11 @@ counters.
95
118
(cons k (proc (cdr entry)))
96
119
entry)))
97
120
98
- (define (counter | @count| action)
121
+ (define (counter @count action)
99
122
(hpanel
100
123
#:stretch '(#t #f )
101
124
(button "- " (λ () (action sub1)))
102
- (text (| @count| . ~> . number->string))
125
+ (text (@count . ~> . number->string))
103
126
(button "+ " (λ () (action add1)))))
104
127
105
128
(render
@@ -109,16 +132,16 @@ counters.
109
132
(hpanel
110
133
#:alignment '(center top)
111
134
#:stretch '(#t #f )
112
- (button "Add counter " (λ () (| @counters| . <~ . append-counter))))
135
+ (button "Add counter " (λ () (@counters . <~ . append-counter))))
113
136
(list-view
114
- | @counters|
137
+ @counters
115
138
#:key car
116
- (λ (k | @entry| )
139
+ (λ (k @entry)
117
140
(counter
118
- (| @entry| . ~> . cdr)
141
+ (@entry . ~> . cdr)
119
142
(λ (proc)
120
- (| @counters| . <~ . (λ (counts) (update-count counts k proc))))))))))
121
- ]
143
+ (@counters . <~ . (λ (counts) (update-count counts k proc))))))))))
144
+ }|
122
145
123
146
Here the @racket[|@counters|] observable holds a list of pairs where
124
147
the first element of a pair is the id of each counter and the second
0 commit comments