/
menusvg.ts
218 lines (192 loc) · 5.81 KB
/
menusvg.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IDisposable } from '@lumino/disposable';
import { ISignal, Signal } from '@lumino/signaling';
import { h, VirtualElement } from '@lumino/virtualdom';
import { ContextMenu, Menu } from '@lumino/widgets';
import { LabIconStyle } from '../../style';
import { classes } from '../../utils';
import { caretRightIcon, checkIcon } from '../iconimports';
const submenuIcon = caretRightIcon.bindprops({
stylesheet: 'menuItem'
});
/**
* An object which implements a universal context menu.
* Tweaked to use inline svg icons
*/
export class ContextMenuSvg extends ContextMenu implements IDisposable {
/**
* Construct a new context menu.
*
* @param options - The options for initializing the menu.
*/
constructor(options: ContextMenu.IOptions) {
super(options);
// override the vanilla .menu
this.menu = new MenuSvg(options);
}
readonly menu: MenuSvg;
/**
* Test whether the context menu is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* A signal fired when the context menu is opened.
*/
get opened(): ISignal<ContextMenu, void> {
return this._opened;
}
/**
* Dispose of the resources held by the context menu.
*/
dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this.menu.dispose();
Signal.disconnectSender(this);
}
/**
* Open the context menu in response to a `'contextmenu'` event.
*
* @param event - The `'contextmenu'` event of interest.
*
* @returns `true` if the menu was opened, or `false` if no items
* matched the event and the menu was not opened.
*
* #### Notes
* This method will populate the context menu with items which match
* the propagation path of the event, then open the menu at the mouse
* position indicated by the event.
*/
open(event: MouseEvent): boolean {
if (this._isDisposed) {
return false;
}
const hasItems = super.open(event);
if (hasItems) {
this._opened.emit();
}
return hasItems;
}
protected _isDisposed = false;
protected _opened: Signal<ContextMenu, void> = new Signal<ContextMenu, void>(
this
);
}
/**
* a widget which displays items as a canonical menu.
* Tweaked to use inline svg icons
*/
export class MenuSvg extends Menu {
/**
* construct a new menu. Overrides the default renderer
*
* @param options - The options for initializing the tab bar.
*/
constructor(options: Menu.IOptions) {
options.renderer = options.renderer || MenuSvg.defaultRenderer;
super(options);
}
/**
* insert a menu item into the menu at the specified index. Replaces the
* default renderer for submenus
*
* @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 {
if (options.submenu) {
MenuSvg.overrideDefaultRenderer(options.submenu);
}
return super.insertItem(index, options);
}
}
export namespace MenuSvg {
export function overrideDefaultRenderer(menu: Menu): void {
// override renderer, if needed
if (menu.renderer === Menu.defaultRenderer) {
// cast away readonly on menu.renderer
(menu as any).renderer = MenuSvg.defaultRenderer;
}
// ensure correct renderer on any submenus that get added in the future
const originalInsertItem = menu.insertItem.bind(menu);
menu.insertItem = (index: number, options: Menu.IItemOptions) => {
if (options.submenu) {
MenuSvg.overrideDefaultRenderer(options.submenu);
}
return originalInsertItem(index, options);
};
// recurse through submenus
for (const item of (menu as any)._items as Menu.IItem[]) {
if (item.submenu) {
overrideDefaultRenderer(item.submenu);
}
}
}
/**
* a modified implementation of the Menu Renderer
*/
export class Renderer extends Menu.Renderer {
/**
* Render the icon element for a menu item.
*
* @param data - The data to use for rendering the icon.
*
* @returns A virtual element representing the item icon.
*/
renderIcon(data: Menu.IRenderData): VirtualElement {
const className = this.createIconClass(data);
if (data.item.isToggled) {
// check mark icon takes precedence
return h.div({ className }, checkIcon, data.item.iconLabel);
}
// if data.item.icon is undefined, it will be ignored
return h.div({ className }, data.item.icon!, data.item.iconLabel);
}
/**
* Create the class name for the menu item icon.
*
* @param data - The data to use for the class name.
*
* @returns The full class name for the item icon.
*/
createIconClass(data: Menu.IRenderData): string {
let name = 'lm-Menu-itemIcon';
if (data.item.type === 'separator') {
return classes(data.item.iconClass, name);
} else {
return classes(
LabIconStyle.styleClass({ stylesheet: 'menuItem' }),
data.item.iconClass,
name
);
}
}
/**
* Render the submenu icon element for a menu item.
*
* @param data - The data to use for rendering the submenu icon.
*
* @returns A virtual element representing the submenu icon.
*/
renderSubmenu(data: Menu.IRenderData): VirtualElement {
const className = 'lm-Menu-itemSubmenuIcon';
if (data.item.type === 'submenu') {
return h.div({ className }, submenuIcon);
} else {
return h.div({ className });
}
}
}
export const defaultRenderer = new Renderer();
}