-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
keymap.clj
637 lines (598 loc) · 24.7 KB
/
keymap.clj
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
;; Copyright 2020-2024 The Defold Foundation
;; Copyright 2014-2020 King
;; Copyright 2009-2014 Ragnar Svensson, Christian Murray
;; Licensed under the Defold License version 1.0 (the "License"); you may not use
;; this file except in compliance with the License.
;;
;; You may obtain a copy of the License, together with FAQs at
;; https://www.defold.com/license
;;
;; Unless required by applicable law or agreed to in writing, software distributed
;; under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
;; CONDITIONS OF ANY KIND, either express or implied. See the License for the
;; specific language governing permissions and limitations under the License.
(ns editor.keymap
(:require [editor.ui :as ui]
[editor.util :as util])
(:import [javafx.scene Scene]
[javafx.scene.input KeyCharacterCombination KeyCodeCombination KeyCombination KeyCombination$ModifierValue KeyEvent]))
(set! *warn-on-reflection* true)
(def platform->default-key-bindings
;; We should generally avoid adding shortcuts that can also be
;; interpreted as typable text. This includes "A", "Shift+E" but
;; also combinations like Alt+Ctrl+2 because on windows Alt+Ctrl is
;; an alternative way to enter AltGR and AltGR+2 is @ on some layouts -
;; something you may want to type in the code editor.
;; The function key-binding-data->keymap attempts to catch this type
;; of mistake.
;; Note that specifying Shortcut key (which is Meta on mac and Ctrl on windows
;; and linux) is not allowed.
{:macos [["A" :add]
["Alt+Backspace" :delete-prev-word]
["Alt+Delete" :delete-next-word]
["Alt+Down" :end-of-line]
["Alt+Down" :move-down]
["Alt+Left" :prev-word]
["Alt+F9" :edit-breakpoint]
["Alt+Meta+E" :select-next-occurrence]
["Alt+Meta+F" :replace-text]
["Alt+Meta+G" :replace-next]
["Alt+Right" :next-word]
["Alt+Up" :beginning-of-line]
["Alt+Up" :move-up]
["Backspace" :delete-backward]
["Ctrl+A" :beginning-of-line]
["Ctrl+D" :delete-line]
["Ctrl+E" :end-of-line]
["Ctrl+I" :reindent]
["Ctrl+K" :delete-to-end-of-line]
["Ctrl+Meta+H" :toggle-component-guides]
["Ctrl+R" :open-recent-file]
["Ctrl+Space" :proposals]
["Delete" :delete]
["Down" :down]
["E" :rotate-tool]
["End" :end-of-line]
["Enter" :enter]
["Esc" :escape]
["F" :frame-selection]
["F1" :documentation]
["F10" :step-over]
["F11" :step-into]
["F2" :rename]
["F5" :start-debugger]
["F5" :continue]
["F6" :toggle-pane-left]
["F7" :toggle-pane-bottom]
["F8" :toggle-pane-right]
["F9" :toggle-breakpoint]
["F12" :goto-definition]
["Home" :beginning-of-line-text]
["Left" :left]
["Meta+'+'" :zoom-in]
["Meta+'-'" :zoom-out]
["Meta+A" :select-all]
["Meta+B" :build]
["Meta+C" :copy]
["Meta+Comma" :preferences]
["Meta+D" :select-next-occurrence]
["Meta+Delete" :delete-to-end-of-line]
["Meta+Down" :end-of-file]
["Meta+E" :hide-toggle-selected]
["Meta+F" :filter-form]
["Meta+F" :find-text]
["Meta+G" :find-next]
["Meta+L" :goto-line]
["Meta+Left" :beginning-of-line-text]
["Meta+N" :new-file]
["Meta+O" :open]
["Meta+P" :open-asset]
["Meta+Q" :quit]
["Meta+R" :hot-reload]
["Meta+Right" :end-of-line]
["Meta+S" :save-all]
["Meta+'/'" :toggle-comment]
["Meta+T" :scene-stop]
["Meta+U" :rebundle]
["Meta+Up" :beginning-of-file]
["Meta+V" :paste]
["Meta+W" :close]
["Meta+X" :cut]
["Meta+Z" :undo]
["Page Down" :page-down]
["Page Up" :page-up]
["Period" :realign-camera]
["R" :scale-tool]
["Right" :right]
["Shift+A" :add-secondary]
["Shift+Alt+Down" :select-end-of-line]
["Shift+Alt+Left" :select-prev-word]
["Shift+Alt+Right" :select-next-word]
["Shift+Alt+Up" :select-beginning-of-line]
["Shift+Backspace" :delete-backward]
["Shift+Ctrl+A" :select-beginning-of-line]
["Shift+Ctrl+E" :select-end-of-line]
["Shift+Ctrl+Left" :select-prev-word]
["Shift+Ctrl+Right" :select-next-word]
["Shift+Down" :down-major]
["Shift+Down" :select-down]
["Shift+E" :erase-tool]
["Shift+End" :select-end-of-line]
["Shift+F11" :step-out]
["Shift+F12" :find-references]
["Shift+Home" :select-beginning-of-line-text]
["Shift+Left" :left-major]
["Shift+Left" :select-left]
["Shift+Meta+B" :rebuild]
["Shift+Meta+Delete" :delete-to-end-of-line]
["Shift+Meta+Down" :select-end-of-file]
["Shift+Meta+E" :show-last-hidden]
["Shift+Meta+F" :search-in-files]
["Shift+Meta+G" :find-prev]
["Shift+Meta+I" :toggle-visibility-filters]
["Shift+Meta+L" :split-selection-into-lines]
["Shift+Meta+Left" :select-beginning-of-line-text]
["Shift+Meta+R" :open-asset]
["Shift+Meta+Right" :select-end-of-line]
["Shift+Meta+T" :reopen-recent-file]
["Shift+Meta+Up" :select-beginning-of-file]
["Shift+Meta+W" :close-all]
["Shift+Meta+Y" :async-reload]
["Shift+Meta+Z" :redo]
["Shift+Page Down" :select-page-down]
["Shift+Page Up" :select-page-up]
["Shift+Right" :right-major]
["Shift+Right" :select-right]
["Shift+Tab" :backwards-tab-trigger]
["Shift+Up" :select-up]
["Shift+Up" :up-major]
["Space" :scene-play]
["Space" :show-palette]
["Tab" :tab]
["Up" :up]
["W" :move-tool]
["X" :flip-brush-horizontally]
["Y" :flip-brush-vertically]
["Z" :rotate-brush-90-degrees]]
:win32 [["A" :add]
["Alt+Down" :move-down]
["Alt+F9" :edit-breakpoint]
["Alt+R" :open-recent-file]
["Alt+Up" :move-up]
["Backspace" :delete-backward]
["Ctrl+'+'" :zoom-in]
["Ctrl+'-'" :zoom-out]
["Ctrl+A" :select-all]
["Ctrl+B" :build]
["Ctrl+Backspace" :delete-prev-word]
["Ctrl+C" :copy]
["Ctrl+Comma" :preferences]
["Ctrl+D" :select-next-occurrence]
["Ctrl+Delete" :delete-next-word]
["Ctrl+E" :hide-toggle-selected]
["Ctrl+End" :end-of-file]
["Ctrl+F" :filter-form]
["Ctrl+F" :find-text]
["Ctrl+G" :find-next]
["Ctrl+H" :replace-text]
["Ctrl+H" :toggle-component-guides]
["Ctrl+Home" :beginning-of-file]
["Ctrl+I" :reindent]
["Ctrl+K" :delete-to-end-of-line]
["Ctrl+L" :goto-line]
["Ctrl+Left" :prev-word]
["Ctrl+N" :new-file]
["Ctrl+O" :open]
["Ctrl+P" :open-asset]
["Ctrl+Q" :quit]
["Ctrl+R" :hot-reload]
["Ctrl+Right" :next-word]
["Ctrl+S" :save-all]
["Ctrl+'/'" :toggle-comment]
["Ctrl+Space" :proposals]
["Ctrl+T" :scene-stop]
["Ctrl+U" :rebundle]
["Ctrl+V" :paste]
["Ctrl+W" :close]
["Ctrl+X" :cut]
["Ctrl+Z" :undo]
["Delete" :delete]
["Down" :down]
["E" :rotate-tool]
["End" :end-of-line]
["Enter" :enter]
["Esc" :escape]
["F" :frame-selection]
["F1" :documentation]
["F10" :step-over]
["F11" :step-into]
["F2" :rename]
["F5" :start-debugger]
["F5" :continue]
["F6" :toggle-pane-left]
["F7" :toggle-pane-bottom]
["F8" :toggle-pane-right]
["F9" :toggle-breakpoint]
["F12" :goto-definition]
["Home" :beginning-of-line-text]
["Left" :left]
["Page Down" :page-down]
["Page Up" :page-up]
["Period" :realign-camera]
["R" :scale-tool]
["Right" :right]
["Shift+A" :add-secondary]
["Shift+Backspace" :delete-backward]
["Shift+Ctrl+B" :rebuild]
["Shift+Ctrl+Delete" :delete-to-end-of-line]
["Shift+Ctrl+E" :show-last-hidden]
["Shift+Ctrl+End" :select-end-of-file]
["Shift+Ctrl+F" :search-in-files]
["Shift+Ctrl+G" :find-prev]
["Shift+Ctrl+H" :replace-next]
["Shift+Ctrl+Home" :select-beginning-of-file]
["Shift+Ctrl+I" :toggle-visibility-filters]
["Shift+Ctrl+L" :split-selection-into-lines]
["Shift+Ctrl+Left" :select-prev-word]
["Shift+Ctrl+R" :open-asset]
["Shift+Ctrl+Right" :select-next-word]
["Shift+Ctrl+T" :reopen-recent-file]
["Shift+Ctrl+W" :close-all]
["Shift+Ctrl+Y" :async-reload]
["Shift+Ctrl+Z" :redo]
["Shift+Down" :down-major]
["Shift+Down" :select-down]
["Shift+E" :erase-tool]
["Shift+End" :select-end-of-line]
["Shift+F11" :step-out]
["Shift+F12" :find-references]
["Shift+F5" :stop-debugger]
["Shift+Home" :select-beginning-of-line-text]
["Shift+Left" :left-major]
["Shift+Left" :select-left]
["Shift+Page Down" :select-page-down]
["Shift+Page Up" :select-page-up]
["Shift+Right" :right-major]
["Shift+Right" :select-right]
["Shift+Tab" :backwards-tab-trigger]
["Shift+Up" :select-up]
["Shift+Up" :up-major]
["Space" :scene-play]
["Space" :show-palette]
["Tab" :tab]
["Up" :up]
["W" :move-tool]
["X" :flip-brush-horizontally]
["Y" :flip-brush-vertically]
["Z" :rotate-brush-90-degrees]]
:linux [["A" :add]
["Alt+Down" :move-down]
["Alt+F9" :edit-breakpoint]
["Alt+R" :open-recent-file]
["Alt+Up" :move-up]
["Backspace" :delete-backward]
["Ctrl+'+'" :zoom-in]
["Ctrl+'-'" :zoom-out]
["Ctrl+A" :select-all]
["Ctrl+B" :build]
["Ctrl+Backspace" :delete-prev-word]
["Ctrl+C" :copy]
["Ctrl+Comma" :preferences]
["Ctrl+D" :select-next-occurrence]
["Ctrl+Delete" :delete-next-word]
["Ctrl+E" :hide-toggle-selected]
["Ctrl+End" :end-of-file]
["Ctrl+F" :filter-form]
["Ctrl+F" :find-text]
["Ctrl+G" :find-next]
["Ctrl+H" :replace-text]
["Ctrl+H" :toggle-component-guides]
["Ctrl+Home" :beginning-of-file]
["Ctrl+I" :reindent]
["Ctrl+K" :delete-to-end-of-line]
["Ctrl+L" :goto-line]
["Ctrl+Left" :prev-word]
["Ctrl+N" :new-file]
["Ctrl+O" :open]
["Ctrl+P" :open-asset]
["Ctrl+Q" :quit]
["Ctrl+R" :hot-reload]
["Ctrl+Right" :next-word]
["Ctrl+S" :save-all]
["Ctrl+'/'" :toggle-comment]
["Ctrl+Space" :proposals]
["Ctrl+T" :scene-stop]
["Ctrl+U" :rebundle]
["Ctrl+V" :paste]
["Ctrl+W" :close]
["Ctrl+X" :cut]
["Ctrl+Z" :undo]
["Delete" :delete]
["Down" :down]
["E" :rotate-tool]
["End" :end-of-line]
["Enter" :enter]
["Esc" :escape]
["F" :frame-selection]
["F1" :documentation]
["F10" :step-over]
["F11" :step-into]
["F2" :rename]
["F5" :start-debugger]
["F5" :continue]
["F6" :toggle-pane-left]
["F7" :toggle-pane-bottom]
["F8" :toggle-pane-right]
["F9" :toggle-breakpoint]
["F12" :goto-definition]
["Home" :beginning-of-line-text]
["Left" :left]
["Page Down" :page-down]
["Page Up" :page-up]
["Period" :realign-camera]
["R" :scale-tool]
["Right" :right]
["Shift+A" :add-secondary]
["Shift+Backspace" :delete-backward]
["Shift+Ctrl+B" :rebuild]
["Shift+Ctrl+Delete" :delete-to-end-of-line]
["Shift+Ctrl+E" :show-last-hidden]
["Shift+Ctrl+End" :select-end-of-file]
["Shift+Ctrl+F" :search-in-files]
["Shift+Ctrl+G" :find-prev]
["Shift+Ctrl+H" :replace-next]
["Shift+Ctrl+Home" :select-beginning-of-file]
["Shift+Ctrl+I" :toggle-visibility-filters]
["Shift+Ctrl+L" :split-selection-into-lines]
["Shift+Ctrl+Left" :select-prev-word]
["Shift+Ctrl+R" :open-asset]
["Shift+Ctrl+Right" :select-next-word]
["Shift+Ctrl+T" :reopen-recent-file]
["Shift+Ctrl+W" :close-all]
["Shift+Ctrl+Y" :async-reload]
["Shift+Ctrl+Z" :redo]
["Shift+Down" :down-major]
["Shift+Down" :select-down]
["Shift+E" :erase-tool]
["Shift+End" :select-end-of-line]
["Shift+F11" :step-out]
["Shift+F12" :find-references]
["Shift+F5" :stop-debugger]
["Shift+Home" :select-beginning-of-line-text]
["Shift+Left" :left-major]
["Shift+Left" :select-left]
["Shift+Page Down" :select-page-down]
["Shift+Page Up" :select-page-up]
["Shift+Right" :right-major]
["Shift+Right" :select-right]
["Shift+Tab" :backwards-tab-trigger]
["Shift+Up" :select-up]
["Shift+Up" :up-major]
["Space" :scene-play]
["Space" :show-palette]
["Tab" :tab]
["Up" :up]
["W" :move-tool]
["X" :flip-brush-horizontally]
["Y" :flip-brush-vertically]
["Z" :rotate-brush-90-degrees]]})
(def default-host-key-bindings
(platform->default-key-bindings (util/os)))
(def ^:private default-allowed-duplicate-shortcuts
#{"Alt+Down"
"Alt+Up"
"Ctrl+F"
"Ctrl+H"
"F5"
"Meta+F"
"Shift+Down"
"Shift+Left"
"Shift+Right"
"Shift+Up"
"Space"})
;; These are only (?) used in contexts where there is no text field
;; interested in the actual typable input.
(def ^:private default-allowed-typable-shortcuts
#{"A" ; :add
"E" ; :rotate-tool
"F" ; :frame-selection
"R" ; :scale-tool
"W" ; :move-tool
"X" ; :flip-brush-horizontally
"Y" ; :flip-brush-vertically
"Z" ; :rotate-brush-90-degrees
"Shift+A" ; :add-secondary
"Shift+E"}) ; :erase-tool
(defprotocol KeyComboData
(key-combo->map [this] "returns a data representation of a KeyCombination."))
(extend-protocol KeyComboData
KeyCodeCombination
(key-combo->map [key-combo]
{:key (.getName (.getCode key-combo))
:alt-down? (= KeyCombination$ModifierValue/DOWN (.getAlt key-combo))
:control-down? (= KeyCombination$ModifierValue/DOWN (.getControl key-combo))
:meta-down? (= KeyCombination$ModifierValue/DOWN (.getMeta key-combo))
:shift-down? (= KeyCombination$ModifierValue/DOWN (.getShift key-combo))
:shortcut-down? (= KeyCombination$ModifierValue/DOWN (.getShortcut key-combo))})
KeyCharacterCombination
(key-combo->map [key-combo]
{:key (.getCharacter key-combo)
:alt-down? (= KeyCombination$ModifierValue/DOWN (.getAlt key-combo))
:control-down? (= KeyCombination$ModifierValue/DOWN (.getControl key-combo))
:meta-down? (= KeyCombination$ModifierValue/DOWN (.getMeta key-combo))
:shift-down? (= KeyCombination$ModifierValue/DOWN (.getShift key-combo))
:shortcut-down? (= KeyCombination$ModifierValue/DOWN (.getShortcut key-combo))}))
(defn key-event->map [^KeyEvent e]
{:key (.getCharacter e)
:alt-down? (.isAltDown e)
:control-down? (.isControlDown e)
:meta-down? (.isMetaDown e)
:shift-down? (.isShiftDown e)
:shortcut-down? (.isShortcutDown e)})
(defn key-combo->display-text [s]
(.getDisplayText (KeyCombination/keyCombination s)))
(def ^:private typable-truth-table
"Only act on key pressed events that look like textual input, and
skip what is likely shortcut combinations.
Transcribed, and altered, from javafx TextInputControlBehaviour/defaultKeyTyped. See:
http://grepcode.com/file/repo1.maven.org/maven2/net.java.openjfx.backport/openjfx-78-backport/1.8.0-ea-b96.1/com/sun/javafx/scene/control/behavior/TextInputControlBehavior.java#TextInputControlBehavior.defaultKeyTyped%28javafx.scene.input.KeyEvent%29
Original:
if (event.isControlDown() || event.isAltDown() || (isMac() && event.isMetaDown())) {
if (!((event.isControlDown() || isMac()) && event.isAltDown())) return;
}
Transcribed:
(not (and (or control-down? alt-down? (and mac? meta-down?))
(not (and (or control-down? mac?) alt-down?))))
Simplified:
(or (and control-down? alt-down?)
(and mac? alt-down?)
(and (not mac?) (not control-down?) (not alt-down?))
(and (not control-down?) (not alt-down?) (not meta-down?)))
Truth table:
MAC CTRL ALT META RESULT
no no no no => typable
no no no yes => typable -- Haven't been able to repro META on non-mac, assume not typable if even possible
no no yes no => shortcut
no no yes yes => shortcut
no yes no no => shortcut
no yes no yes => shortcut
no yes yes no => typable
no yes yes yes => typable -- As above
yes no no no => typable
yes no no yes => shortcut
yes no yes no => typable
yes no yes yes => typable -- No other tested mac app reacts like this, but seems you can use cmd (Meta) as modifier in keyboard input sources
yes yes no no => shortcut
yes yes no yes => shortcut
yes yes yes no => typable -- As above
yes yes yes yes => typable -- As above
This does not seem right. We probably want something like that:
MAC CTRL ALT META RESULT"
{[:no :no :no :no ] :typable
[:no :no :no :yes] :shortcut ;; Now treated as possibly shortcut
[:no :no :yes :no ] :shortcut
[:no :no :yes :yes] :shortcut
[:no :yes :no :no ] :shortcut
[:no :yes :no :yes] :shortcut
[:no :yes :yes :no ] :typable
[:no :yes :yes :yes] :shortcut ;; As above
[:yes :no :no :no ] :typable
[:yes :no :no :yes] :shortcut
[:yes :no :yes :no ] :typable
[:yes :no :yes :yes] :shortcut ;; Now treated as possibly shortcut
[:yes :yes :no :no ] :shortcut
[:yes :yes :no :yes] :shortcut
[:yes :yes :yes :no ] :typable ;; As above
[:yes :yes :yes :yes] :shortcut}) ;; Now treated as possibly shortcut
(defn- boolean->kw [b]
(if b :yes :no))
(defn typable?
([key-combo-data]
(typable? key-combo-data (util/os)))
([{:keys [alt-down? control-down? meta-down?]} os]
(let [mac? (= os :macos)]
(-> typable-truth-table
(get (mapv boolean->kw [mac? control-down? alt-down? meta-down?]))
(= :typable)))))
(defn- platform-typable-shortcut? [key-combo-data os]
(and (typable? key-combo-data os)
(= 1 (count (:key key-combo-data)))))
(defn- key-binding-data [key-bindings]
(map (fn [[shortcut command]]
(let [key-combo (KeyCombination/keyCombination shortcut)
key-combo-data (key-combo->map key-combo)]
{:shortcut shortcut
:command command
:key-combo key-combo
:key-combo-data key-combo-data}))
key-bindings))
(defn- key-binding-data->keymap
[key-bindings-data platform valid-command? allowed-duplicate-shortcuts allowed-typable-shortcuts]
(reduce (fn [ret {:keys [key-combo-data shortcut command ^KeyCombination key-combo]}]
(cond
(not (valid-command? command))
(update ret :errors conj {:type :unknown-command
:command command
:shortcut shortcut})
(not= shortcut (.getName key-combo))
(update ret :errors conj {:type :unknown-shortcut
:command command
:shortcut shortcut})
(:shortcut-down? key-combo-data)
(update ret :errors conj {:type :shortcut-key
:command command
:shortcut shortcut})
(and (platform-typable-shortcut? key-combo-data platform)
(not (allowed-typable-shortcuts shortcut)))
(update ret :errors conj {:type :typable-shortcut
:command command
:shortcut shortcut})
(and (some? (get-in ret [:keymap key-combo-data]))
(not (allowed-duplicate-shortcuts shortcut)))
(update ret :errors into [{:type :duplicate-shortcut
:command command
:shortcut shortcut}
{:type :duplicate-shortcut
:command (get-in ret [:keymap key-combo-data])
:shortcut shortcut}])
:else
(update-in ret [:keymap key-combo-data] (fnil conj []) {:command command
:shortcut shortcut
:key-combo key-combo})))
{:keymap {}
:errors #{}}
key-bindings-data))
(defn- make-keymap*
[key-bindings platform valid-command? allowed-duplicate-shortcuts allowed-typable-shortcuts]
(-> (key-binding-data key-bindings)
(key-binding-data->keymap platform valid-command? allowed-duplicate-shortcuts allowed-typable-shortcuts)))
(defn make-keymap
([key-bindings]
(make-keymap key-bindings nil))
([key-bindings {:keys [valid-command?
platform
throw-on-error?
allowed-duplicate-shortcuts
allowed-typable-shortcuts]
:or {valid-command? (constantly true)
platform (util/os)
throw-on-error? false
allowed-duplicate-shortcuts default-allowed-duplicate-shortcuts
allowed-typable-shortcuts default-allowed-typable-shortcuts}}]
(let [{:keys [errors keymap]} (make-keymap* key-bindings platform valid-command? allowed-duplicate-shortcuts allowed-typable-shortcuts)]
(if (and (seq errors) throw-on-error?)
(throw (ex-info "Keymap has errors"
{:errors errors
:key-bindings key-bindings}))
keymap))))
(defn command->shortcut
[keymap]
(into {}
(comp cat
(map (fn [{:keys [command shortcut]}]
[command shortcut])))
(vals keymap)))
(defn- execute-command
[commands]
;; It is imperative that the handler is invoked using run-later as
;; this avoids a JVM crash on some macOS versions. Prior to macOS
;; Sierra, the order in which native menu events are delivered
;; triggered a segfault in the native menu implementation when the
;; stage is changed during the event dispatch. This happens for
;; example when we have a shortcut triggering the opening of a
;; dialog.
(ui/run-later (let [command-contexts (ui/contexts (ui/main-scene))]
(loop [[command & rest] commands]
(when (some? command)
(let [ret (ui/invoke-handler command-contexts command)]
(if (or (= ret ::ui/not-active)
(= ret ::ui/not-enabled))
(recur rest)
ret)))))))
(defn install-key-bindings!
[^Scene scene keymap]
(let [accelerators (.getAccelerators scene)]
(run! (fn [[_ commands]]
(let [key-combo (-> commands first :key-combo)]
(.put accelerators key-combo #(execute-command (map :command commands)))))
keymap)))