-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
menu.ts
1874 lines (1642 loc) · 46.4 KB
/
menu.ts
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2017, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import { ArrayExt } from '@lumino/algorithm';
import { CommandRegistry } from '@lumino/commands';
import { JSONExt, ReadonlyJSONObject } from '@lumino/coreutils';
import { ElementExt } from '@lumino/domutils';
import { getKeyboardLayout } from '@lumino/keyboard';
import { Message, MessageLoop } from '@lumino/messaging';
import { ISignal, Signal } from '@lumino/signaling';
import {
ARIAAttrNames,
ElementARIAAttrs,
ElementDataset,
h,
VirtualDOM,
VirtualElement
} from '@lumino/virtualdom';
import { Widget } from './widget';
/**
* A widget which displays items as a canonical menu.
*/
export class Menu extends Widget {
/**
* Construct a new menu.
*
* @param options - The options for initializing the menu.
*/
constructor(options: Menu.IOptions) {
super({ node: Private.createNode() });
this.addClass('lm-Menu');
this.setFlag(Widget.Flag.DisallowLayout);
this.commands = options.commands;
this.renderer = options.renderer || Menu.defaultRenderer;
}
/**
* Dispose of the resources held by the menu.
*/
dispose(): void {
this.close();
this._items.length = 0;
super.dispose();
}
/**
* A signal emitted just before the menu is closed.
*
* #### Notes
* This signal is emitted when the menu receives a `'close-request'`
* message, just before it removes itself from the DOM.
*
* This signal is not emitted if the menu is already detached from
* the DOM when it receives the `'close-request'` message.
*/
get aboutToClose(): ISignal<this, void> {
return this._aboutToClose;
}
/**
* A signal emitted when a new menu is requested by the user.
*
* #### Notes
* This signal is emitted whenever the user presses the right or left
* arrow keys, and a submenu cannot be opened or closed in response.
*
* This signal is useful when implementing menu bars in order to open
* the next or previous menu in response to a user key press.
*
* This signal is only emitted for the root menu in a hierarchy.
*/
get menuRequested(): ISignal<this, 'next' | 'previous'> {
return this._menuRequested;
}
/**
* The command registry used by the menu.
*/
readonly commands: CommandRegistry;
/**
* The renderer used by the menu.
*/
readonly renderer: Menu.IRenderer;
/**
* The parent menu of the menu.
*
* #### Notes
* This is `null` unless the menu is an open submenu.
*/
get parentMenu(): Menu | null {
return this._parentMenu;
}
/**
* The child menu of the menu.
*
* #### Notes
* This is `null` unless the menu has an open submenu.
*/
get childMenu(): Menu | null {
return this._childMenu;
}
/**
* The root menu of the menu hierarchy.
*/
get rootMenu(): Menu {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let menu: Menu = this;
while (menu._parentMenu) {
menu = menu._parentMenu;
}
return menu;
}
/**
* The leaf menu of the menu hierarchy.
*/
get leafMenu(): Menu {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let menu: Menu = this;
while (menu._childMenu) {
menu = menu._childMenu;
}
return menu;
}
/**
* The menu content node.
*
* #### Notes
* This is the node which holds the menu item nodes.
*
* Modifying this node directly can lead to undefined behavior.
*/
get contentNode(): HTMLUListElement {
return this.node.getElementsByClassName(
'lm-Menu-content'
)[0] as HTMLUListElement;
}
/**
* Get the currently active menu item.
*/
get activeItem(): Menu.IItem | null {
return this._items[this._activeIndex] || null;
}
/**
* Set the currently active menu item.
*
* #### Notes
* If the item cannot be activated, the item will be set to `null`.
*/
set activeItem(value: Menu.IItem | null) {
this.activeIndex = value ? this._items.indexOf(value) : -1;
}
/**
* Get the index of the currently active menu item.
*
* #### Notes
* This will be `-1` if no menu item is active.
*/
get activeIndex(): number {
return this._activeIndex;
}
/**
* Set the index of the currently active menu item.
*
* #### Notes
* If the item cannot be activated, the index will be set to `-1`.
*/
set activeIndex(value: number) {
// Adjust the value for an out of range index.
if (value < 0 || value >= this._items.length) {
value = -1;
}
// Ensure the item can be activated.
if (value !== -1 && !Private.canActivate(this._items[value])) {
value = -1;
}
// Bail if the index will not change.
if (this._activeIndex === value) {
return;
}
// Update the active index.
this._activeIndex = value;
// Make active element in focus
if (
this._activeIndex >= 0 &&
this.contentNode.childNodes[this._activeIndex]
) {
(this.contentNode.childNodes[this._activeIndex] as HTMLElement).focus();
}
// schedule an update of the items.
this.update();
}
/**
* A read-only array of the menu items in the menu.
*/
get items(): ReadonlyArray<Menu.IItem> {
return this._items;
}
/**
* Activate the next selectable item in the menu.
*
* #### Notes
* If no item is selectable, the index will be set to `-1`.
*/
activateNextItem(): void {
let n = this._items.length;
let ai = this._activeIndex;
let start = ai < n - 1 ? ai + 1 : 0;
let stop = start === 0 ? n - 1 : start - 1;
this.activeIndex = ArrayExt.findFirstIndex(
this._items,
Private.canActivate,
start,
stop
);
}
/**
* Activate the previous selectable item in the menu.
*
* #### Notes
* If no item is selectable, the index will be set to `-1`.
*/
activatePreviousItem(): void {
let n = this._items.length;
let ai = this._activeIndex;
let start = ai <= 0 ? n - 1 : ai - 1;
let stop = start === n - 1 ? 0 : start + 1;
this.activeIndex = ArrayExt.findLastIndex(
this._items,
Private.canActivate,
start,
stop
);
}
/**
* Trigger the active menu item.
*
* #### Notes
* If the active item is a submenu, it will be opened and the first
* item will be activated.
*
* If the active item is a command, the command will be executed.
*
* If the menu is not attached, this is a no-op.
*
* If there is no active item, this is a no-op.
*/
triggerActiveItem(): void {
// Bail if the menu is not attached.
if (!this.isAttached) {
return;
}
// Bail if there is no active item.
let item = this.activeItem;
if (!item) {
return;
}
// Cancel the pending timers.
this._cancelOpenTimer();
this._cancelCloseTimer();
// If the item is a submenu, open it.
if (item.type === 'submenu') {
this._openChildMenu(true);
return;
}
// Close the root menu before executing the command.
this.rootMenu.close();
// Execute the command for the item.
let { command, args } = item;
if (this.commands.isEnabled(command, args)) {
this.commands.execute(command, args);
} else {
console.log(`Command '${command}' is disabled.`);
}
}
/**
* Add a menu item to the end of the menu.
*
* @param options - The options for creating the menu item.
*
* @returns The menu item added to the menu.
*/
addItem(options: Menu.IItemOptions): Menu.IItem {
return this.insertItem(this._items.length, options);
}
/**
* Insert a menu item into the menu at the specified index.
*
* @param index - The index at which to insert the item.
*
* @param options - The options for creating the menu item.
*
* @returns The menu item added to the menu.
*
* #### Notes
* The index will be clamped to the bounds of the items.
*/
insertItem(index: number, options: Menu.IItemOptions): Menu.IItem {
// Close the menu if it's attached.
if (this.isAttached) {
this.close();
}
// Reset the active index.
this.activeIndex = -1;
// Clamp the insert index to the array bounds.
let i = Math.max(0, Math.min(index, this._items.length));
// Create the item for the options.
let item = Private.createItem(this, options);
// Insert the item into the array.
ArrayExt.insert(this._items, i, item);
// Schedule an update of the items.
this.update();
// Return the item added to the menu.
return item;
}
/**
* Remove an item from the menu.
*
* @param item - The item to remove from the menu.
*
* #### Notes
* This is a no-op if the item is not in the menu.
*/
removeItem(item: Menu.IItem): void {
this.removeItemAt(this._items.indexOf(item));
}
/**
* Remove the item at a given index from the menu.
*
* @param index - The index of the item to remove.
*
* #### Notes
* This is a no-op if the index is out of range.
*/
removeItemAt(index: number): void {
// Close the menu if it's attached.
if (this.isAttached) {
this.close();
}
// Reset the active index.
this.activeIndex = -1;
// Remove the item from the array.
let item = ArrayExt.removeAt(this._items, index);
// Bail if the index is out of range.
if (!item) {
return;
}
// Schedule an update of the items.
this.update();
}
/**
* Remove all menu items from the menu.
*/
clearItems(): void {
// Close the menu if it's attached.
if (this.isAttached) {
this.close();
}
// Reset the active index.
this.activeIndex = -1;
// Bail if there is nothing to remove.
if (this._items.length === 0) {
return;
}
// Clear the items.
this._items.length = 0;
// Schedule an update of the items.
this.update();
}
/**
* Open the menu at the specified location.
*
* @param x - The client X coordinate of the menu location.
*
* @param y - The client Y coordinate of the menu location.
*
* @param options - The additional options for opening the menu.
*
* #### Notes
* The menu will be opened at the given location unless it will not
* fully fit on the screen. If it will not fit, it will be adjusted
* to fit naturally on the screen.
*
* This is a no-op if the menu is already attached to the DOM.
*/
open(x: number, y: number, options: Menu.IOpenOptions = {}): void {
// Bail early if the menu is already attached.
if (this.isAttached) {
return;
}
// Extract the position options.
let forceX = options.forceX || false;
let forceY = options.forceY || false;
// Open the menu as a root menu.
Private.openRootMenu(this, x, y, forceX, forceY);
// Activate the menu to accept keyboard input.
this.activate();
}
/**
* Handle the DOM events for the menu.
*
* @param event - The DOM event sent to the menu.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the menu's DOM nodes. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'keydown':
this._evtKeyDown(event as KeyboardEvent);
break;
case 'mouseup':
this._evtMouseUp(event as MouseEvent);
break;
case 'mousemove':
this._evtMouseMove(event as MouseEvent);
break;
case 'mouseenter':
this._evtMouseEnter(event as MouseEvent);
break;
case 'mouseleave':
this._evtMouseLeave(event as MouseEvent);
break;
case 'mousedown':
this._evtMouseDown(event as MouseEvent);
break;
case 'contextmenu':
event.preventDefault();
event.stopPropagation();
break;
}
}
/**
* A message handler invoked on a `'before-attach'` message.
*/
protected onBeforeAttach(msg: Message): void {
this.node.addEventListener('keydown', this);
this.node.addEventListener('mouseup', this);
this.node.addEventListener('mousemove', this);
this.node.addEventListener('mouseenter', this);
this.node.addEventListener('mouseleave', this);
this.node.addEventListener('contextmenu', this);
document.addEventListener('mousedown', this, true);
}
/**
* A message handler invoked on an `'after-detach'` message.
*/
protected onAfterDetach(msg: Message): void {
this.node.removeEventListener('keydown', this);
this.node.removeEventListener('mouseup', this);
this.node.removeEventListener('mousemove', this);
this.node.removeEventListener('mouseenter', this);
this.node.removeEventListener('mouseleave', this);
this.node.removeEventListener('contextmenu', this);
document.removeEventListener('mousedown', this, true);
}
/**
* A message handler invoked on an `'activate-request'` message.
*/
protected onActivateRequest(msg: Message): void {
if (this.isAttached) {
this.node.focus();
}
}
/**
* A message handler invoked on an `'update-request'` message.
*/
protected onUpdateRequest(msg: Message): void {
let items = this._items;
let renderer = this.renderer;
let activeIndex = this._activeIndex;
let collapsedFlags = Private.computeCollapsed(items);
let content = new Array<VirtualElement>(items.length);
for (let i = 0, n = items.length; i < n; ++i) {
let item = items[i];
let active = i === activeIndex;
let collapsed = collapsedFlags[i];
content[i] = renderer.renderItem({
item,
active,
collapsed,
onfocus: () => {
this.activeIndex = i;
}
});
}
VirtualDOM.render(content, this.contentNode);
}
/**
* A message handler invoked on a `'close-request'` message.
*/
protected onCloseRequest(msg: Message): void {
// Cancel the pending timers.
this._cancelOpenTimer();
this._cancelCloseTimer();
// Reset the active index.
this.activeIndex = -1;
// Close any open child menu.
let childMenu = this._childMenu;
if (childMenu) {
this._childIndex = -1;
this._childMenu = null;
childMenu._parentMenu = null;
childMenu.close();
}
// Remove this menu from its parent and activate the parent.
let parentMenu = this._parentMenu;
if (parentMenu) {
this._parentMenu = null;
parentMenu._childIndex = -1;
parentMenu._childMenu = null;
parentMenu.activate();
}
// Emit the `aboutToClose` signal if the menu is attached.
if (this.isAttached) {
this._aboutToClose.emit(undefined);
}
// Finish closing the menu.
super.onCloseRequest(msg);
}
/**
* Handle the `'keydown'` event for the menu.
*
* #### Notes
* This listener is attached to the menu node.
*/
private _evtKeyDown(event: KeyboardEvent): void {
// A menu handles all keydown events.
event.preventDefault();
event.stopPropagation();
// Fetch the key code for the event.
let kc = event.keyCode;
// Enter
if (kc === 13) {
this.triggerActiveItem();
return;
}
// Escape
if (kc === 27) {
this.close();
return;
}
// Left Arrow
if (kc === 37) {
if (this._parentMenu) {
this.close();
} else {
this._menuRequested.emit('previous');
}
return;
}
// Up Arrow
if (kc === 38) {
this.activatePreviousItem();
return;
}
// Right Arrow
if (kc === 39) {
let item = this.activeItem;
if (item && item.type === 'submenu') {
this.triggerActiveItem();
} else {
this.rootMenu._menuRequested.emit('next');
}
return;
}
// Down Arrow
if (kc === 40) {
this.activateNextItem();
return;
}
// Get the pressed key character.
let key = getKeyboardLayout().keyForKeydownEvent(event);
// Bail if the key is not valid.
if (!key) {
return;
}
// Search for the next best matching mnemonic item.
let start = this._activeIndex + 1;
let result = Private.findMnemonic(this._items, key, start);
// Handle the requested mnemonic based on the search results.
// If exactly one mnemonic is matched, that item is triggered.
// Otherwise, the next mnemonic is activated if available,
// followed by the auto mnemonic if available.
if (result.index !== -1 && !result.multiple) {
this.activeIndex = result.index;
this.triggerActiveItem();
} else if (result.index !== -1) {
this.activeIndex = result.index;
} else if (result.auto !== -1) {
this.activeIndex = result.auto;
}
}
/**
* Handle the `'mouseup'` event for the menu.
*
* #### Notes
* This listener is attached to the menu node.
*/
private _evtMouseUp(event: MouseEvent): void {
if (event.button !== 0) {
return;
}
event.preventDefault();
event.stopPropagation();
this.triggerActiveItem();
}
/**
* Handle the `'mousemove'` event for the menu.
*
* #### Notes
* This listener is attached to the menu node.
*/
private _evtMouseMove(event: MouseEvent): void {
// Hit test the item nodes for the item under the mouse.
let index = ArrayExt.findFirstIndex(this.contentNode.children, node => {
return ElementExt.hitTest(node, event.clientX, event.clientY);
});
// Bail early if the mouse is already over the active index.
if (index === this._activeIndex) {
return;
}
// Update and coerce the active index.
this.activeIndex = index;
index = this.activeIndex;
// If the index is the current child index, cancel the timers.
if (index === this._childIndex) {
this._cancelOpenTimer();
this._cancelCloseTimer();
return;
}
// If a child menu is currently open, start the close timer.
if (this._childIndex !== -1) {
this._startCloseTimer();
}
// Cancel the open timer to give a full delay for opening.
this._cancelOpenTimer();
// Bail if the active item is not a valid submenu item.
let item = this.activeItem;
if (!item || item.type !== 'submenu' || !item.submenu) {
return;
}
// Start the open timer to open the active item submenu.
this._startOpenTimer();
}
/**
* Handle the `'mouseenter'` event for the menu.
*
* #### Notes
* This listener is attached to the menu node.
*/
private _evtMouseEnter(event: MouseEvent): void {
// Synchronize the active ancestor items.
for (let menu = this._parentMenu; menu; menu = menu._parentMenu) {
menu._cancelOpenTimer();
menu._cancelCloseTimer();
menu.activeIndex = menu._childIndex;
}
}
/**
* Handle the `'mouseleave'` event for the menu.
*
* #### Notes
* This listener is attached to the menu node.
*/
private _evtMouseLeave(event: MouseEvent): void {
// Cancel any pending submenu opening.
this._cancelOpenTimer();
// If there is no open child menu, just reset the active index.
if (!this._childMenu) {
this.activeIndex = -1;
return;
}
// If the mouse is over the child menu, cancel the close timer.
let { clientX, clientY } = event;
if (ElementExt.hitTest(this._childMenu.node, clientX, clientY)) {
this._cancelCloseTimer();
return;
}
// Otherwise, reset the active index and start the close timer.
this.activeIndex = -1;
this._startCloseTimer();
}
/**
* Handle the `'mousedown'` event for the menu.
*
* #### Notes
* This listener is attached to the document node.
*/
private _evtMouseDown(event: MouseEvent): void {
// Bail if the menu is not a root menu.
if (this._parentMenu) {
return;
}
// The mouse button which is pressed is irrelevant. If the press
// is not on a menu, the entire hierarchy is closed and the event
// is allowed to propagate. This allows other code to act on the
// event, such as focusing the clicked element.
if (Private.hitTestMenus(this, event.clientX, event.clientY)) {
event.preventDefault();
event.stopPropagation();
} else {
this.close();
}
}
/**
* Open the child menu at the active index immediately.
*
* If a different child menu is already open, it will be closed,
* even if the active item is not a valid submenu.
*/
private _openChildMenu(activateFirst = false): void {
// If the item is not a valid submenu, close the child menu.
let item = this.activeItem;
if (!item || item.type !== 'submenu' || !item.submenu) {
this._closeChildMenu();
return;
}
// Do nothing if the child menu will not change.
let submenu = item.submenu;
if (submenu === this._childMenu) {
return;
}
// Ensure the current child menu is closed.
this._closeChildMenu();
// Update the private child state.
this._childMenu = submenu;
this._childIndex = this._activeIndex;
// Set the parent menu reference for the child.
submenu._parentMenu = this;
// Ensure the menu is updated and lookup the item node.
MessageLoop.sendMessage(this, Widget.Msg.UpdateRequest);
let itemNode = this.contentNode.children[this._activeIndex];
// Open the submenu at the active node.
Private.openSubmenu(submenu, itemNode as HTMLElement);
// Activate the first item if desired.
if (activateFirst) {
submenu.activeIndex = -1;
submenu.activateNextItem();
}
// Activate the child menu.
submenu.activate();
}
/**
* Close the child menu immediately.
*
* This is a no-op if a child menu is not open.
*/
private _closeChildMenu(): void {
if (this._childMenu) {
this._childMenu.close();
}
}
/**
* Start the open timer, unless it is already pending.
*/
private _startOpenTimer(): void {
if (this._openTimerID === 0) {
this._openTimerID = window.setTimeout(() => {
this._openTimerID = 0;
this._openChildMenu();
}, Private.TIMER_DELAY);
}
}
/**
* Start the close timer, unless it is already pending.
*/
private _startCloseTimer(): void {
if (this._closeTimerID === 0) {
this._closeTimerID = window.setTimeout(() => {
this._closeTimerID = 0;
this._closeChildMenu();
}, Private.TIMER_DELAY);
}
}
/**
* Cancel the open timer, if the timer is pending.
*/
private _cancelOpenTimer(): void {
if (this._openTimerID !== 0) {
clearTimeout(this._openTimerID);
this._openTimerID = 0;
}
}
/**
* Cancel the close timer, if the timer is pending.
*/
private _cancelCloseTimer(): void {
if (this._closeTimerID !== 0) {
clearTimeout(this._closeTimerID);
this._closeTimerID = 0;
}
}
private _childIndex = -1;
private _activeIndex = -1;
private _openTimerID = 0;
private _closeTimerID = 0;
private _items: Menu.IItem[] = [];
private _childMenu: Menu | null = null;
private _parentMenu: Menu | null = null;
private _aboutToClose = new Signal<this, void>(this);
private _menuRequested = new Signal<this, 'next' | 'previous'>(this);
}
/**
* The namespace for the `Menu` class statics.
*/
export namespace Menu {
/**
* An options object for creating a menu.
*/
export interface IOptions {
/**
* The command registry for use with the menu.
*/
commands: CommandRegistry;
/**
* A custom renderer for use with the menu.
*
* The default is a shared renderer instance.
*/
renderer?: IRenderer;
}
/**
* An options object for the `open` method on a menu.
*/
export interface IOpenOptions {
/**
* Whether to force the X position of the menu.
*
* Setting to `true` will disable the logic which repositions the
* X coordinate of the menu if it will not fit entirely on screen.
*
* The default is `false`.
*/
forceX?: boolean;
/**
* Whether to force the Y position of the menu.
*
* Setting to `true` will disable the logic which repositions the
* Y coordinate of the menu if it will not fit entirely on screen.
*
* The default is `false`.
*/
forceY?: boolean;
}
/**
* A type alias for a menu item type.
*/
export type ItemType = 'command' | 'submenu' | 'separator';
/**
* An options object for creating a menu item.
*/
export interface IItemOptions {
/**
* The type of the menu item.
*
* The default value is `'command'`.
*/
type?: ItemType;
/**
* The command to execute when the item is triggered.
*
* The default value is an empty string.
*/
command?: string;
/**
* The arguments for the command.
*
* The default value is an empty object.
*/
args?: ReadonlyJSONObject;
/**
* The submenu for a `'submenu'` type item.
*