-
Notifications
You must be signed in to change notification settings - Fork 2
/
scenic-helpers.lisp
295 lines (259 loc) · 11.3 KB
/
scenic-helpers.lisp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
(in-package :scenic-helpers)
(defun background (color child)
(make-instance 'background
:fill-color color
:child child))
(defun border (color width child)
(make-instance 'border
:stroke-color color
:stroke-width width
:child child))
(defun scene (width height widget)
(make-instance 'scene
:width width
:height height
:widget widget))
(defun placeholder (width height)
(make-instance 'placeholder
:width width
:height height))
(defun padding (left top right bottom child)
(make-instance 'padding
:left-padding left
:top-padding top
:right-padding right
:bottom-padding bottom
:child child))
(defun uniform-padding (padding child)
(make-instance 'padding
:left-padding padding
:top-padding padding
:right-padding padding
:bottom-padding padding
:child child))
(defun stack (&rest children)
(make-instance 'stack
:children children))
(defun filler ()
(make-instance 'filler))
(defun label (text &key
(face "Courier") (size 12) (color '(0 0 0))
(slant :normal) (weight :normal))
(make-instance 'label
:text text
:font-face face
:font-size size
:font-color color
:font-slant slant
:font-weight weight))
(defun button (child)
(make-instance 'button
:child child))
(defun button-text (text)
(button (uniform-padding 2 (aligner (label text :size 18)))))
(defun toggle (text)
(make-instance 'toggle-button
:state nil
:child (uniform-padding 2 (label text :size 18))))
(defun horizontal-slider (min max page)
(make-instance 'slider
:orientation :horizontal
:min-value min
:max-value max
:page-size page
:current-min-position min))
(defun vertical-slider (min max page)
(make-instance 'slider
:orientation :vertical
:min-value min
:max-value max
:page-size page
:current-min-position min))
(defun sizer (child &key (min-width nil) (min-height nil) (max-width nil) (max-height nil))
(make-instance 'sizer
:child child
:min-width min-width
:min-height min-height
:max-width max-width
:max-height max-height))
(defun arrow (direction)
(make-instance 'arrow :direction direction))
(defun horizontal-scrollbar (min max page)
(make-instance 'scrollbar
:orientation :horizontal
:min-value min
:max-value max
:page-size page
:current-min-position min))
(defun vertical-scrollbar (min max page)
(make-instance 'scrollbar
:orientation :vertical
:min-value min
:max-value max
:page-size page
:current-min-position min))
(defun image (image-path)
(make-instance 'image
:image-surface (get-image image-path)))
(defun grid (column-layout-options row-layout-options children-descriptions)
(make-instance 'grid
:column-layout-options column-layout-options
:row-layout-options row-layout-options
:children-descriptions children-descriptions))
(defun vertical-box (space layout-options children)
(if (= space 0)
(grid '(:auto) layout-options `((:column ,@(mapcar (lambda (child) (list :cell child))
children))))
(grid '(:auto)
(intersperse layout-options (list space :px))
`((:column ,@(mapcar (lambda (child) (list :cell child))
(intersperse children
(placeholder 0 space))))))))
(defun horizontal-box (space layout-options children)
(if (= space 0)
(grid layout-options '(:auto) `((:row ,@(mapcar (lambda (child) (list :cell child))
children))))
(grid (intersperse layout-options (list space :px))
'(:auto)
`((:row ,@(mapcar (lambda (child) (list :cell child))
(intersperse children
(placeholder space 0))))))))
(defun aligner (child &key (horizontal :center) (vertical :center))
(make-instance 'aligner
:child child
:horizontal horizontal
:vertical vertical))
(defun clipper (child)
(make-instance 'clipper
:child child))
(defun glass (opacity child)
(make-instance 'glass
:opacity opacity
:child child))
(defun henchman (children-locations children)
(make-instance 'henchman
:children children
:children-locations children-locations))
(defun scroll-view (child &key (inside-width (expt 10 6)) (inside-height (expt 10 6)))
(make-instance 'scroll-view
:child child
:inside-width inside-width
:inside-height inside-height))
(defun scroll-view-auto (child &key
(inside-width (expt 10 6))
(inside-height (expt 10 6))
(always-horizontal-scrollbar t)
(always-vertical-scrollbar t))
(let* ((scroll-view (scroll-view child
:inside-width inside-width
:inside-height inside-height))
(hscroll (horizontal-scrollbar 0 100 10))
(vscroll (vertical-scrollbar 0 100 10))
(grid (grid '(:auto (1 :auto))
'(:auto (1 :auto))
`((:row (:cell ,scroll-view)
(:cell ,vscroll))
(:row (:cell ,hscroll))))))
(add-event-handler scroll-view :scroll-view-measured :bubble
(lambda (o evt)
(declare (ignore o))
(labels ((set-hscroll-visibility ()
(when (measured-width grid)
(setf (visible hscroll)
(or always-horizontal-scrollbar
(< (- (measured-width grid)
(if (visible vscroll) 19 0))
(inner-width evt))))))
(set-vscroll-visibility ()
(when (measured-height grid)
(setf (visible vscroll)
(or always-vertical-scrollbar
(< (- (measured-height grid)
(if (visible hscroll) 19 0))
(inner-height evt)))))))
(setf (page-size hscroll) (outer-width evt))
(setf (max-value hscroll) (inner-width evt))
(setf (page-size vscroll) (outer-height evt))
(setf (max-value vscroll) (inner-height evt))
(set-hscroll-visibility)
(set-vscroll-visibility)
(set-hscroll-visibility))))
(add-event-handler hscroll :position-changed nil
(lambda (o evt)
(declare (ignore o evt))
(setf (horizontal-offset scroll-view)
(current-min-position hscroll))
(invalidate scroll-view)))
(add-event-handler vscroll :position-changed nil
(lambda (o evt)
(declare (ignore o evt))
(setf (vertical-offset scroll-view)
(current-min-position vscroll))
(invalidate scroll-view)))
(add-event-handler scroll-view :scroll-view-offset-changed nil
(lambda (o evt)
(declare (ignore o evt))
(setf (current-min-position vscroll)
(vertical-offset scroll-view))
(setf (current-min-position hscroll)
(horizontal-offset scroll-view))))
(values grid scroll-view)))
(defun textbox (text cursor-position
&key (selection-start 0) (caret-color (list 0.0 0.0 0.0))
(selection-color (list 0.3 0.3 1.0)))
(make-instance 'textbox
:text text
:cursor-position cursor-position
:selection-start selection-start
:caret-color caret-color
:selection-color selection-color
:background-color (list 1.0 1.0 1.0)
:font-face "Courier"
:font-size 14))
(defun wrap-stateful-button (stateful-button text)
(let (label combination)
(setf combination
(horizontal-box
2 '(:auto :auto)
(list (aligner stateful-button)
(setf label
(make-instance 'clickable
:child (uniform-padding
2
(aligner (label text :size 18))))))))
(add-event-handler label :click nil
(lambda (o e)
(declare (ignore o e))
(setf (state stateful-button)
(not (state stateful-button)))))
(values combination stateful-button)))
(defun checkbox (text)
(wrap-stateful-button (make-instance 'checkbox :state nil)
text))
(defun radio-button (text)
(wrap-stateful-button (make-instance 'radio-button :state nil)
text))
(defun group-stateful-buttons (default buttons)
(labels ((all-unchecked ()
(every (lambda (btn) (-> btn state not)) buttons)))
(dolist (b buttons)
(scenic:add-event-handler b :state-changed nil
(lambda (obj e)
(declare (ignore e))
(when (state obj)
(dolist (b buttons)
(unless (eq obj b)
(setf (state b) nil))))
(when (all-unchecked)
(setf (state obj) t)))))
(setf (state default) t)))
(defun simple-horizontal-box (space children)
(make-instance 'box
:space-between-cells space
:orientation :horizontal
:children children))
(defun simple-vertical-box (space children)
(make-instance 'box
:space-between-cells space
:orientation :vertical
:children children))