-
Notifications
You must be signed in to change notification settings - Fork 9
/
trees.lisp
378 lines (325 loc) · 11 KB
/
trees.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
;;; trees.lisp --- generic folding hierarchical list widget with
;;; indentation and headlines, a la orgmode
;; Copyright (C) 2011, 2012 David O'Toole
;; Author: David O'Toole <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(in-package :blocky)
;;; Trees
(defvar *tree-depth* 0)
(defmacro deeper (&rest body)
`(let ((*tree-depth* (1+ *tree-depth*)))
,@body))
(defparameter *depth-gray-slope* -4)
(defparameter *depth-gray-base* 50)
(defun depth-gray (depth)
(percent-gray (+ *depth-gray-base* (* depth *depth-gray-slope*))))
(define-block (tree :super list)
(category :initform :structure)
(treep :initform t)
(always-visible :initform nil)
(style :initform :rounded)
(method :initform nil)
(draw-frame :initform t)
(indentation-width :initform (dash 2))
(top-level :initform nil)
(locked :initform nil)
(temporary :initform t)
action target (expanded :initform nil) (visible :initform t))
(defun treep (thing)
(is-a 'tree thing))
(define-method children tree () %inputs)
(define-method initialize tree
(&key action target top-level inputs pinned locked method category
expanded (draw-frame t) label)
(initialize%super self)
(setf %action action
%pinned pinned
%draw-frame draw-frame
%expanded expanded
%category category
%locked locked
%target target
%method method
%top-level top-level
%label label)
(when inputs (setf %inputs inputs))
;; become the parent
(when inputs
(dolist (each inputs)
(pin each)
(set-parent each self))))
(define-method evaluate tree ()
(deeper (mapcar #'evaluate %inputs)))
(define-method toggle-expanded tree (&optional force)
(with-fields (expanded locked) self
(when (or force (not locked))
(setf expanded (if expanded nil t))
(invalidate-layout self))))
(define-method expandedp tree ()
%expanded)
(define-method expand tree (&optional force)
(when (or force (not %locked))
(setf %expanded t)
(invalidate-layout self)))
(define-method unexpand tree (&optional force)
(when (or force (not %locked))
(setf %expanded nil)
(invalidate-layout self)))
(define-method tap tree (x y)
(declare (ignore x y))
(toggle-expanded self))
(define-method display-string tree ()
(with-fields (action label top-level) self
(let ((ellipsis (concatenate 'string (or label "") *null-display-string*)))
(if action
(etypecase action
((or string blocky:object) ellipsis)
(keyword (pretty-string action)))
(if top-level (or label "") ellipsis)))))
(define-method layout-as-string tree (string)
(with-fields (height width) self
(setf height (dash 1 (font-height *font*)))
(setf width
(+ (dash 2) (font-text-width string *font*)))))
(define-method layout tree ()
(with-fields (expanded x y always-visible height inputs label width) self
(if expanded
;; we're an expanded subtree. lay it out
(progn
;; lay out the children as in a typical list
(layout-vertically self)
;; add a little padding to the bottom
(incf height (dash 2))
;; handle the case that the label is wider than the content.
(when label
(setf width
(max width
(dash 6 (font-text-width label *font*)))))
;; make all inputs equally wide
(dolist (each inputs)
(setf (field-value :width each) (- width (dash 2))))
;; possibly adjust to stay onscreen
(when always-visible
(multiple-value-bind (top left bottom right)
(window-bounding-box (current-buffer))
(let ((overlap (- bottom
(+ y height))))
(when (minusp overlap)
(incf y overlap)
(layout-vertically self))))))
;; we're not expanded. just lay out for label.
(layout-as-string self (display-string self)))))
(define-method header-height tree ()
(if %label (font-height *font*) 0))
(define-method header-width tree ()
(if %expanded
(dash 2 (font-text-width (display-string self) *font*))
%width))
(define-method hit tree (mouse-x mouse-y)
(with-field-values (x y expanded inputs width height) self
(when (within-extents mouse-x mouse-y x y (+ x width) (+ y height))
(flet ((try (item)
(hit item mouse-x mouse-y)))
(if (not expanded)
self
;; we're expanded. is the mouse to the left of this
;; tree's header tab thingy?
(if %top-level
(when (and (< mouse-x (+ x (header-width self)))
(< (header-height self) mouse-y))
(some #'try inputs))
(or (some #'try inputs) self)))))))
;; (let ((hh (header-height self))
;; (hw (header-width self)))
;; ;; (message "HIT TREE")
;; (if (< y mouse-y (+ y hh))
;; ;; we're even with the header text for this tree.
;; ;; are we touching it?
;; (if (< x mouse-x (+ x hw))
;; ;; mouse is over tree title. return self to get event
;; ;; we're in the corner (possibly over top of the text
;; ;; of the next tree item's title in the tree bar).
;; ;; so, we close this tree.
;; (prog1 nil (unexpand self)))
;; (labels ((try (it)
;; (hit it mouse-x mouse-y)))
;; (some #'try inputs)))))))
(define-method draw-hover tree ()
nil)
(define-method draw-border tree ()
nil)
(define-method draw-highlight tree ()
nil)
(define-method draw-expanded tree (&optional label)
(with-field-values (x y width height parent inputs) self
(let ((display-string (or label *null-display-string*))
(header (header-height self)))
;; possibly draw a background
(when (or (null parent)
(not (null inputs))
(not (treep parent)))
(draw-patch self x y (+ x width) (+ y height)))
;; possibly colored by depth
;; (when (plusp *tree-depth*)
;; (draw-box x y width height :color (depth-gray *tree-depth*))))
(draw-label-string self display-string)
;; (draw-indicator :down-triangle-open
;; (+ %x (font-text-width display-string)
;; (dash 4))
;; (+ %y (dash 2))
;; :scale 1.6
;; :color "gray60")
(when %label
(draw-line (+ x 1) (dash 2 y header)
(+ x width -1) (dash 2 y header)
:color (find-color self :highlight))))))
(define-method draw-unexpanded tree (&optional label)
; (draw-background self)
(let ((string (or label (display-string self))))
(draw-label-string self string)
(draw-indicator :down-triangle-closed
(+ %x (font-text-width string)
(dash 4))
(+ %y (dash 2))
:scale 1.6
:color "yellow")))
(define-method draw-subtree tree ()
(deeper
(dolist (each %inputs)
(draw each))))
(define-method draw tree (&optional highlight)
(with-fields (visible draw-frame expanded label inputs) self
(when visible
(with-style %style
(if expanded
(progn
(when draw-frame
(draw-expanded self label))
(draw-subtree self))
(when draw-frame (draw-unexpanded self label)))))))
;; see system.lisp for example tree menu
(defun make-tree (items &key target category (tree-prototype "BLOCKY:TREE"))
(labels ((xform (item)
(if (listp item)
(if (listp (first item))
(mapcar #'xform item)
(apply #'clone tree-prototype
:target target
:category category
(mapcar #'xform item)))
item)))
(xform items)))
;;; Menus
(define-block (menu :super tree)
(action :initform nil)
(always-visible :initform t)
(style :initform :rounded)
(top-level :initform nil)
(category :initform :menu)
(tags :initform '(:menu)))
(defun menup (thing)
(is-a 'menu thing))
(define-method siblings menu ()
(when %parent
(remove-if-not #'menup (%inputs %parent))))
(defvar *menu-prototype* nil)
(defun make-menu (items &key target)
(make-tree items
:target target
:category :menu
:tree-prototype
"BLOCKY:MENU"))
;; menu items should not accept any dragged widgets.
(define-method accept menu (&rest args) nil)
(define-method can-pick menu ()
;; allow making code blocks from menu items
(or %method
(or (keywordp %action)
;; disallow pulling main menus
(not %top-level))))
(define-method pick menu ()
(when %target
(if (keywordp %method)
(let ((message
(message-for-method %method %target)))
(prog1 message
(with-fields (x y) message
(setf x %x y %y))))
self)))
(define-method alternate-tap menu (x y)
(when (or (null %parent)
(not (is-a 'menu %parent)))
(alternate-tap%super self x y)))
(define-method tap menu (x y)
(declare (ignore x y))
(with-fields (action target) self
(typecase action
(function (funcall action))
(string (evaluate action))
(keyword (when (has-method action target)
(send action (or target (symbol-value '*system*)))))
(otherwise
;; we're a submenu, not an individual menu command.
;; first close any other open menus
(mapc #'unexpand (siblings self))
(toggle-expanded self)))))
(defparameter *menu-tab-color* "gray60")
(defparameter *menu-title-color* "white")
(define-method draw-expanded menu (&optional label)
(with-field-values (action x y width height parent inputs top-level) self
(let ((header (header-height self)))
(if top-level
;; draw the header a bit differently to avoid over-drawing
;; other headers in a menu bar situation.
(progn
(assert parent)
(draw-patch self x (+ 1 y)
(+ (dash 2) x (header-width self))
(dash 3 y header)
:color *menu-tab-color*)
(draw-label-string
self (or label *null-display-string*) *menu-title-color*)
;; draw the rest of the tree background
(draw-patch self
x (dash 2 y header)
(dash 0 x width)
(- (dash 1 y height) (dash 1))))
;; nope, draw in the typical fashion.
(draw-expanded%super self label))
;; draw status indicator on submenus
(when (and (not %locked) parent (menup parent))
(draw-indicator :down-triangle-open
(+ %x (font-text-width (or label *null-display-string*))
(dash 4))
(+ %y (dash 2))
:scale 1.6
:color "gray50")))))
(define-method draw-unexpanded menu (&optional label)
(with-fields (action target top-level) self
(let ((text (or label (display-string self))))
(draw-label-string self
text
(find-color self :foreground)))))
(define-method draw-highlight menu ()
(with-fields (y height expanded parent top-level) self
(when parent
(with-fields (x width) parent
;; don't highlight top-level trees.
(when (and (not expanded) (not top-level))
(draw-box (+ x (dash 2))
(+ y (dash 1))
(- width (dash 4))
(+ height 1)
:color *highlight-background-color*)
(draw-label-string self (display-string self)))))))
;;; menus.lisp ends here