-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard-handlers.red
679 lines (633 loc) · 22 KB
/
standard-handlers.red
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
Red [
title: "Standard event handlers for Draw-based widgets"
author: @hiiamboris
license: BSD-3
]
;-- requires events.red (on load)
is-key-printable?: function [event [map!]] [
to logic! all [
char? char: event/key
char >= #" "
not event/ctrl?
]
]
define-handlers [
;-- *************************************************************************************
scrollable: [
on-down [space path event] [
set [item: _: subitem:] skip path 2
case [
find [hscroll vscroll] select item 'type [ ;-- move or start dragging
axis: item/axis
switch select subitem 'type [
forth-arrow [space/move-by 'line 'forth axis]
back-arrow [space/move-by 'line 'back axis]
forth-page [space/move-by 'page 'forth axis]
back-page [space/move-by 'page 'back axis]
thumb [drag?: on]
]
]
any [item =? space/content item = none] [ ;-- 'none' is useful if content is smaller than the scrollable
drag?: find [pan scroll] space/behavior/draggable
clear skip (path: clone/flat path) 4 ;-- drag by content, not by its child (child may override this)
pass ;-- content may still handle it (e.g. grid-view within grid-view)
]
]
;; don't override drags from inherited handlers (grid-view, etc.), but override from parent handlers (child takes priority)
if all [drag? not dragging?/from space] [start-drag path]
space/last-xy: path/2 ;@@ kludge
]
on-up [space path event] [
either dragging?/from space [ ;-- since 'down' is sent to children, let 'up' be sent as well
if (drag-offset path) +<= (3,3) [pass] ;-- do not eat clicks on content, only drags (experimental) ;@@ or pass anyway?
stop-drag
][
pass
]
]
on-over [space path event] [
unless own?: dragging?/from space [pass exit] ;-- let inner spaces handle it
set [item: _: subitem:] skip path 2
switch/default select item 'type [ ;-- item may be none
hscroll vscroll [
unless all [
own?
thumb?: 'thumb = select subitem 'type ;-- do not react to drag of arrows (used by timer)
] [exit]
scroll: item
x: scroll/axis
;; map/subitem/size should take precedence over subitem/size
;; because map can get fetched from cache without affecting subitem object sizes (they become invalid at this point)
forth-arrow-geom: select/same scroll/map scroll/forth-arrow
back-arrow-geom: select/same scroll/map scroll/back-arrow
band: scroll/size/:x - forth-arrow-geom/size/:x - back-arrow-geom/size/:x
csize: space/content/size ;@@ may get out of sync with the map?
vport: space/viewport
hidden: csize/:x - vport/:x
ofs: drag-offset skip path 2 ;-- get offset relative to the scrollbar
ofs: ofs/:x / max 1 band ;-- scale it down by scrollbar's size
ofs: ofs * (csize * axis2pair x) ;-- now scale up by content size
]
][
switch space/behavior/draggable [
pan [
if own? [ofs: negate drag-offset path]
]
scroll [
space/last-xy: path/2 ;@@ kludge
]
]
]
if ofs [space/clip-origin space/origin - ofs] ;-- clipping in the event handler guarantees validity of size
if own? [
if any [
thumb?
not find [scroll select] space/behavior/draggable
] [start-drag path] ;-- restart from the new offset or it will accumulate
]
]
on-key-down [space path event] [
; unless single? path [pass exit]
code: switch event/key [
down [[space/move-by pick [page line] event/ctrl? 'forth 'y]]
up [[space/move-by pick [page line] event/ctrl? 'back 'y]]
right [[space/move-by pick [page line] event/ctrl? 'forth 'x]]
left [[space/move-by pick [page line] event/ctrl? 'back 'x]]
page-down [[space/move-by 'page 'forth 'y]]
page-up [[space/move-by 'page 'back 'y]]
home [[space/move-to 'head]]
end [[space/move-to 'tail]]
]
either code [
do code
][
pass ;-- key was not handled (useful for tabbing)
]
]
on-wheel [space path event] [
if event/ctrl? [exit] ;-- ignore ctrl+wheel, which is used for zoom usually
if 100 < absolute amount: event/picked [ ;@@ workaround for #5110
amount: -256 * sign? amount + amount
]
horz?: to logic! any [ ;-- shift+wheel changes direction - wish #9
path/3 =? space/hscroll
all [path/3 =? space/content event/shift?]
]
space/move-by/scale
'line
pick [forth back] amount <= 0
pick [x y] horz?
abs amount * 4
]
on-focus [space path event] [
invalidate space/hscroll/thumb
invalidate space/vscroll/thumb
]
on-unfocus [space path event] [
invalidate space/hscroll/thumb
invalidate space/vscroll/thumb
]
scroll-timer: [
on-time [space path event delay [percent!]] [ ;-- press & hold way of scrolling
unless all [
drag-path
found: find/reverse/same next drag-path path/-1
] [exit]
set [scrollable: xy: item: _: subitem:] found
switch/default select item 'type [ ;-- 'item' can be none
hscroll vscroll [
scrollable/move-by/scale
switch/default select subitem 'type [back-page forth-page ['page] back-arrow forth-arrow ['line]] [exit]
switch subitem/type [back-arrow back-page ['back] forth-arrow forth-page ['forth]]
any [select [hscroll x vscroll y] item/type]
delay + 100%
]
][
;; scroll viewport when dragging out of it (useful for content selection)
;; this relies on on-over rewriting drag-path on every event, because timer doesn't have access to offsets
if all [
scrollable/behavior/draggable = 'scroll
not (xy: scrollable/last-xy) inside? scrollable ;@@ kludge
][
cxy: xy - center: half scrollable/size
cxy': cxy / center ;-- normalized to [-1,-1]..[1,1]
ofs: cxy' - (cxy' / max abs cxy'/x abs cxy'/y) * center
unless zero? ofs [
scrollable/clip-origin scrollable/origin - ofs
]
]
]
]
]
]
;-- *************************************************************************************
inf-scrollable: extends 'scrollable [ ;-- adds automatic window movement when near the edges
on-wheel [space path event] [space/slide] ;-- faster wheel-scrolling, without slide-timer delays
;; trick here is that inf-scrollable/on-key fires after scrollable/on-key-down:
;@@ (otherwise I would have to extend the handler dialect to add delayed handlers, child after parent - maybe I should?)
on-key [space path event] [space/slide pass] ;-- most useful for fast seamless scrolling on pageup/pagedown
slide-timer: [
on-time [space path event delay] [ ;-- during scroller dragging
path/-1/slide
]
]
]
;-- *************************************************************************************
list-view: extends 'inf-scrollable [
on-focus [space path event] [if space/behavior/selectable [invalidate space/list]]
on-unfocus [space path event] [if space/behavior/selectable [invalidate space/list]]
on-down [space path event] [
multi?: space/behavior/selectable = 'multi
if set [item:] locate path [obj - .. obj/type = 'item] [
i: space/list/frame/range/1 + half skip? find/same space/list/map item
mode: case [
all [event/shift? multi?] ['extend]
all [event/ctrl? multi?] ['invert]
'default ['replace]
]
range: either all [event/shift? multi?] [i][i thru i]
batch space [
select-range/mode range mode
if i <> here [move-cursor i] ;-- don't move the already selected item around
]
]
; if all [multi? space/behavior/draggable <> 'pan] [start-drag path] ;-- start selection by dragging
;; let scrollable get the event, for dragging viewport by item
]
on-up [space path event] [
if dragging?/from space [stop-drag]
]
on-over [space path event] [
unless all [
drag-path
found: find/reverse/same next drag-path space ;-- dragging from inside of this list-view, maybe from the item
multi?: space/behavior/selectable = 'multi
] [exit]
y: space/list/axis
wxy1: found/4
wxy2: path/4
set-pair [i1: i2:] batch space [frame/items-between wxy1/:y wxy2/:y]
mode: case [
all [event/shift? multi?] ['extend]
all [event/ctrl? multi?] ['invert]
'default ['replace]
]
batch space [
select-range/mode i1 thru i2 mode
if i2 <> here [move-cursor i2] ;-- don't move the already selected item around
]
]
on-key-down [space path event] [
unless space/behavior/selectable [exit] ;-- this handler is only responsible for selection
list: space/list
y: list/axis
range: list/frame/range
multi?: space/behavior/selectable = 'multi
;@@ would be nice to use key->plan here but it's tuned for editing text paragraphs
switch/default event/key [
#"C" [if event/ctrl? [batch space [copy-items/clip selected]]]
#" " [ ;-- space/ctrl+space selected item toggle
if i: space/cursor [
mode: case [
multi? ['invert]
find space/selected i ['exclude]
'else ['replace]
]
batch space [select-range/mode here mode]
]
]
down up page-down page-up home end [
old: batch space [here]
target: switch event/key [
up ['line-up]
down ['line-down]
page-up ['page-up]
page-down ['page-down]
home [either event/ctrl? ['far-head]['head]]
end [either event/ctrl? ['far-tail]['tail]]
]
new: batch space [locate target]
far-jump?: find [far-head far-tail] target
;; do not(!) move to items that are not drawn yet (too many bugs with that)
;; (must avoid anchor-moving branch of kit/move-to)
unless far-jump? [
range: space/list/frame/range
new: clip range/1 range/2 new
]
select?: case [
;; dangerous to select items with far-jump (can be billions)
all [multi? event/shift? not far-jump?] ['range]
any [far-jump? not event/ctrl?] ['single]
]
batch space [slide] ;-- slide it before it was modified ;@@ needs to consider invalidation type ideally
switch select? [
range [
mode: either event/ctrl? ['include]['extend]
batch space [select-range/mode old thru new mode]
]
single [
batch space [select-range/mode new thru new 'replace]
]
]
batch space [
move-cursor/no-clip new ;-- /no-clip is safe as long as given margin does not exceed list/margin
]
]
] [exit] ;-- unhandled keys belong to inf-scrollable
stop/now ;-- handled keys are not passed into inf-scrollable
]
]
;-- *************************************************************************************
grid-view: extends 'inf-scrollable [
on-down [gview path event] [
set [grid: _: cell:] skip path 4
if 'cell <> select cell 'type [exit]
cxy: grid-ctx/get-cell-address grid cell
unless cxy [exit]
multi?: gview/behavior/selectable = 'multi
mode: either all [event/ctrl? multi?] ['invert]['replace]
cursor: max cxy grid/pinned + 1
unless all [multi? event/shift?] [gview/selection-start: cursor]
start: gview/selection-start
batch grid [move-cursor cursor]
case [
grid/pinned +< cxy [
batch grid [
select-range start cxy
drag?: on
]
]
not multi? ['done]
cxy +<= grid/pinned [
batch grid [select-range 1x1 'far-tail] ;-- include headers into selection
]
x: case [
cxy/y <= grid/pinned/y ['x]
cxy/x <= grid/pinned/x ['y]
] [
batch grid [
select-along ortho x 'all
select-along/mode x start/:x thru cxy/:x mode
]
drag?: on
]
]
if drag? [
if locate path [o - .. find [hscroll vscroll] o/type] [ ;-- allow scrollbars in children to override grid dragging ;@@ a kludge!
pass exit
]
clear skip (path': clone/flat path) 6 ;-- drag around grid, not around its cell
start-drag path'
stop/now
]
; stop/now
; pass
]
on-over [gview path event] [
unless all [
dragging?/from gview
not event/ctrl? ;-- handle only ctrl-click, not ctrl-drag
start: gview/selection-start
gview/behavior/selectable = 'multi
] [pass exit]
set [grid: _: cell:] skip path 4
if all ['cell = select cell 'type] [
#assert [grid/type = 'grid]
cxy: grid-ctx/get-cell-address grid cell
unless cxy [exit]
case [
all [cxy/x <= grid/pinned/x cxy/y > grid/pinned/y] [ ;-- rows after header
batch grid [
select-columns 'all
select-rows start/y thru cxy/y
]
]
all [cxy/y <= grid/pinned/y cxy/x > grid/pinned/x] [ ;-- columns after header
batch grid [
select-rows 'all
select-columns start/x thru cxy/x
]
]
grid/pinned +< cxy [ ;-- 2D range selection
batch grid [select-range start cxy]
]
]
batch grid [move-cursor max cxy grid/pinned + 1]
stop/now
]
]
;; guidelines: https://www.w3.org/WAI/ARIA/apg/patterns/grid/
on-key-down [gview path event] [
unless gview/behavior/selectable [exit] ;-- has to be selectable to have cursor
cursor: batch grid: gview/grid [here]
switch/default event/key [
#"A" [
if event/ctrl? [
batch grid [select-range 1x1 'far-tail] ;-- include headers into selection
]
]
#"C" insert [
if event/ctrl? [
batch grid [copy-selection/clip]
]
]
#" " [ ;-- column or row selection
if axis: case [
event/ctrl? ['x]
event/shift? ['y]
][
gview/selection-start: none ;-- indicate it's not an area selection
batch grid [
select-along ortho axis 'all
select-along axis cursor/:axis
]
]
]
;@@ consider navigation across multicells - follow spreadsheets behavior
up down left right page-up page-down home end [
target: pick select [
up [column-head line-up]
down [column-tail line-down]
home [far-head head]
end [far-tail tail]
left [row-head prev-cell]
right [row-tail next-cell]
page-down [page-down page-down]
page-up [page-up page-up]
] event/key event/ctrl?
multi?: all [gview/behavior/selectable = 'multi event/shift?]
batch gview [cursor: locate target]
batch grid [
old-cursor: here
;@@ these should be known automatically, in the frame or where
cell1: first grid/locate-point org: negate gview/window/origin
cell2: first grid/locate-point org + gview/window/size
; ?? [org cell1 cell2 old-cursor]
cursor-drawn?: cell1 +<= old-cursor +<= cell2
repeatable?: find [line-up line-down prev-cell next-cell page-up page-down] target
;@@ temporary limit until redesign:
;@@ moving cursor outside the currently drawn area shows empty space until filled by multiple slides
;@@ it's slow when holding arrow keys, so I disable it (but for far jumps it's a necessity to have it)
if any [cursor-drawn? not repeatable?] [
move-cursor cursor
old-sel: selected-range
either all [multi? old-sel not gview/selection-start] [ ;-- current selection is not a limited area, but bits
axis: either cursor/y = old-cursor/y ['x]['y]
select-along/mode axis old-cursor/:axis thru cursor/:axis 'include
][ ;-- current selection is either empty or a limited area
either multi? [
default gview/selection-start: old-cursor
][
gview/selection-start: cursor
]
select-range gview/selection-start cursor
]
]
]
batch gview [pan-to-cursor]
gview/slide ;@@ put into the batch
]
][exit] ;-- let unhandled keys go into inf-scrollable
stop/now
]
]
;-- *************************************************************************************
switch: [
on-up [space path event] [
space/state: not space/state
]
]
;-- *************************************************************************************
link: [
;; without explicit start-drag here
;; parent (e.g. scrollable) may start it's own dragging
;; and -up event won't reach the link
on-down [space path event] [start-drag path]
on-up [space path event] [stop-drag]
on-click [space path event] [
do space/command
]
]
;-- *************************************************************************************
clickable: [
on-click [space path event] [
do space/command
]
on-down [space path event] [] ;-- prevent clicks from passing over to children
on-up [space path event] []
]
data-clickable: extends 'clickable []
menu: [
list: [
clickable: extends 'clickable [
; on-up [space path event] [
; hide-popups event/window 1 ;-- click on a menu item hides all visible menus
; ]
on-over [space path event] [ ;-- on-hover highlight ;@@ should it affect /color though?
space/color: if path/2 inside? space [impose 'panel opaque 'text 15%]
]
]
]
ring: [
clickable: extends 'menu/list/clickable []
round-clickable: extends 'menu/list/clickable []
]
on-click [space path event] [
item: path/5
if find [round-clickable clickable] select item 'type [
do item/command
]
popups/hide 1 ;-- click on a menu hides all visible menus
]
]
button: [ ;-- focusable unlike `clickable` space
on-down [space path event] [
space/pushed?: yes
start-drag path ;-- otherwise `up` event might not be caught, leaving button "pressed"
]
on-up [space path event] [
space/pushed?: no ;@@ TODO: avoid the command when pointer goes out of button box (also maybe ESC key)
stop-drag
]
on-key [space path event] [
either all [
find " ^M" event/key
not space/pushed?
][
space/pushed?: yes
][pass]
]
on-key-up [space path event] [
either all [
find " ^M" event/key
space/pushed?
][
space/pushed?: no
][pass]
]
]
;-- *************************************************************************************
rotor: [
ring: [
on-down [space path event] [
rotor: path/-2
ofs: path/-1 - (rotor/size / 2)
angle: arctangent2 ofs/y ofs/x
start-drag/with path reduce [rotor/angle angle]
]
on-up [space path event] [stop-drag]
on-over [space path event] [
unless dragging? [exit]
rotor: path/-2
ofs: path/-1 - (rotor/size / 2)
angle: arctangent2 ofs/y ofs/x
parm: drag-parameter
rotor/angle: (parm/1 + angle - parm/2) // 360
]
]
]
;-- *************************************************************************************
field: [
;; `key-down` supports key-combos like Ctrl+Tab, `key` does not seem to
;; OTOH `key` properly reflects Shift state in chars
;; so we have to use both, just separate who handles what
on-key [space path event] [ ;-- char keys branch (inserts stuff as you type)
unless is-key-printable? event [ ;-- handled by on-key-down (e.g. ctrl+BS=#"^~")
unless find [#"^[" #"^M" #"^H" left right delete home end insert] event/key [pass] ;-- keys unhandled by field can go to the parent
exit ;@@ what about enter key / on-enter event?
]
;@@ TODO: input validation / filtering
batch space compose [
(key->plan event space/selected)
frame/adjust-origin ;@@ should be automatic
]
invalidate space ;-- has to reconstruct layout in order to measure caret location
invalidate/only space/caret ;@@ any way to properly invalidate both at once? -- need layout under cache
]
on-key-down [space path event] [ ;-- control keys & key combos branch (navigation)
if is-key-printable? event [exit]
unless any [
event/ctrl?
find [#"^[" #"^M" #"^H" left right delete home end insert] event/key
] [pass exit] ;-- keys unhandled by field can go to the parent
batch space compose [
(key->plan event space/selected)
frame/adjust-origin
]
invalidate space
]
on-key-up [space path event] [ ;-- eats the event so it's not passed forth
unless any [
is-key-printable? event
event/ctrl?
find [#"^[" #"^M" #"^H" left right delete home end insert] event/key
] [pass] ;-- keys unhandled by field can go to the parent
]
on-down [space path event] [
batch space [
select-range none
move-caret frame/point->caret path/2
]
start-drag path
]
on-over [space path event] [
#assert [space/spaces/text/layout]
dpath: drag-path
if all [dpath dpath/1 =? path/1] [ ;-- if started dragging also on this field
batch space [
select-range frame/point->caret path/2
frame/adjust-origin
]
]
]
on-up [space path event] [stop-drag]
on-focus [space path event] [space/caret/visible?: yes]
on-unfocus [space path event] [space/caret/visible?: no]
]
slider: [
on-down [space path event] [
space/offset: batch space [frame/x->offset path/2/x]
start-drag path ;-- keep tracking knob when pointer leaves the slider
]
on-up [space path event] [stop-drag]
on-over [space path event] [
if event/down? [space/offset: batch space [frame/x->offset path/2/x]]
]
on-key [space path event] [
if integer? step: space/step [
step: step / (space/size/x - space/knob/size/x)
]
either offset: switch event/key [
left up [space/offset - step]
right down [space/offset + step]
page-up [space/offset - max 10% step * 20]
page-down [space/offset + max 10% step * 20]
home [0%]
end [100%]
][
space/offset: 100% * clip 0 1 offset
][
pass ;-- ignore other keys, esp. tab
]
]
on-focus [space path event] [invalidate space]
on-unfocus [space path event] [invalidate space]
]
fps-meter: [
on-time [space path event] [
time: now/precise/utc
limit: time - space/aggregate
frames: space/frames
forall frames [
if frames/1 > limit [
remove/part frames frames: head frames
break
]
]
append frames time ;-- let frames never be empty, so frame/1 is not none
elapsed: to float! difference time frames/1
fps: (length? frames) / (max 0.01 elapsed) ;-- max for overflow protection
space/text: rejoin ["FPS: " 0.1 * to integer! 10 * fps]
]
]
]