Debian Codegraph @

Nasal/canvas/gui/Menu.nas

raw
1 # SPDX-FileCopyrightText: (C) 2022 Frederic Croix <[email protected]>
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 #
4 # Usage example for the menu, with submenu:
5 #
6 # var m = canvas.gui.Menu.new();
7 # var i1 = m.createItem("Text");
8 # var i2 = m.createItem("Text 2");
9 # var sub = canvas.gui.Menu.new();
10 # var s1 = sub.createItem("Sub", nil);
11 # var s2 = sub.createItem("Sub", nil);
12 # i1.setMenu(sub);
13 # m.setPosition(200, 200);
14 # m.show();
15
16 gui.MenuItem = {
17 MenuPosition: {
18 Above: 0x0,
19 Right: 0x1,
20 Below: 0x2,
21 Left: 0x4
22 },
23 # @description Create a new menu item widget
24 # @cfg_field text: str Text of the new menu item
25 # @cfg_field shortcut: str String representation of the keyboard shortcut for the item
26 # @cfg_field cb: callable Function / method to call when the item is clicked
27 # @cfg_field icon: str Path of an icon to be displayed (relative to the path in `canvas.style._dir_widgets`)
28 # @cfg_field enabled: bool Initial state of the menu item: enabled (1) or disabled (0)
29 new: func(parent, style, cfg) {
30 var cfg = Config.new(cfg);
31 var m = gui.Widget.new(gui.MenuItem);
32 m._text = cfg.get("text", "Menu item");
33 m._shortcut = cfg.get("shortcut", nil);
34 m._cb = cfg.get("cb", nil);
35 m._cb_me = cfg.get("cb_me", m);
36 m._icon = cfg.get("icon", nil);
37 m._enabled = cfg.get("enabled", 1);
38 m._menu_position = cfg.get("menu_position", gui.MenuItem.MenuPosition.Right);
39 m._hovered = 0;
40 m._menu = nil;
41 m._parent_menu = nil;
42 m._is_menubar_item = 0;
43 m._mouseOver = 0;
44
45 m._setView(style.createWidget(parent, cfg.get("type", "menu-item"), cfg));
46
47 m.setLayoutMinimumSize([48, 24]);
48 m.setLayoutSizeHint([64, 24]);
49 m.setLayoutMaximumSize([1024, 24]);
50
51 m.setText(m._text);
52 m.setIcon(m._icon);
53 m.setShortcut(m._shortcut);
54 return m;
55 },
56
57 text: func {
58 return me._text;
59 },
60
61 setMenu: func(menu) {
62 menu._parent_item = me;
63 menu._canvas_item = me._parent_menu._canvas_item;
64 me._menu = menu;
65
66 return me.update();
67 },
68
69 onClicked: func(e) {
70 if (!me._menu and me._cb) {
71 call(me._cb, [e], me._cb_me, var errors = []);
72 if (size(errors)) {
73 logprint(LOG_ALERT, "Error(s) executing callback for menu item '" ~ me._text ~ "':");
74 debug.printerror(errors);
75 }
76 }
77 me._unsetOthersHovered();
78 if (me._is_menubar_item and me._enabled) {
79 if (!me._menu.isVisible()) {
80 me._hovered = 1;
81 me.update();
82 var x = e.screenX - e.localX;
83 var y = e.screenY - e.localY;
84 me._showMenu(x, y);
85 } else {
86 me._hovered = 0;
87 me.update();
88 me._hideMenu();
89 }
90 } elsif (me._parent_menu != nil and me._menu == nil) {
91 me._hovered = 0;
92 me.update();
93 me._parent_menu.hide();
94 }
95 },
96
97 _unsetOthersHovered: func {
98 for (var i = 0; i < me._parent_menu._layout.count(); i += 1) {
99 var item = me._parent_menu._layout.itemAt(i);
100 item._hovered = 0;
101 item.update();
102 if (item._menu != nil) {
103 item._menu.hide();
104 }
105 }
106 },
107
108 onMouseEnter: func(e) {
109 var any_hovered = 0;
110 for (var i = 0; i < me._parent_menu._layout.count(); i += 1) {
111 any_hovered |= me._parent_menu._layout.itemAt(i)._hovered;
112 }
113 me._unsetOthersHovered();
114 me._mouseOver = 1;
115 if ((!me._is_menubar_item or any_hovered) and me._enabled) {
116 me._hovered = 1;
117 me.update();
118 var x = e.screenX - e.localX;
119 var y = e.screenY - e.localY;
120 me._showMenu(x, y);
121 }
122 },
123
124 onMouseLeave: func(e) {
125 me._mouseOver = 0;
126 if (me._menu == nil) {
127 me._hovered = 0;
128 }
129 me.update();
130 },
131
132 removeMenu: func() {
133 me._menu = nil;
134 return me.update();
135 },
136
137 _showMenu: func(x, y) {
138 if (me._menu) {
139 var pos = [0, 0];
140 if (me._menu_position == gui.MenuItem.MenuPosition.Right) {
141 pos = [x + me.geometry()[2], y];
142 } elsif (me._menu_position == gui.MenuItem.MenuPosition.Below) {
143 pos = [x, me.geometry()[3] + y];
144 } elsif (me._menu_position == gui.MenuItem.MenuPosition.Above) {
145 pos = [x, y - me._menu.getSize()[1]];
146 } elsif (me._menu_position == gui.MenuItem.MenuPosition.Left) {
147 pos = [x - me._menu.getSize()[0], y];
148 }
149 me._menu.show(pos[0], pos[1]);
150 }
151 },
152
153 _hideMenu: func {
154 if (me._menu) {
155 me._menu.hide(1);
156 }
157 },
158
159 setEnabled: func(enabled = 1) {
160 me._enabled = enabled;
161 return me.update();
162 },
163
164 setText: func(text) {
165 me._text = text;
166 me._view.setText(me, text);
167 return me.update();
168 },
169
170 text: func {
171 return me._text;
172 },
173
174 setShortcut: func(shortcut) {
175 if (!shortcut) {
176 me._keyBinding = nil;
177 return;
178 }
179 if (!isstr(shortcut)) {
180 logprint(LOG_ALERT, "Menu.setShortcut: invalid shortcut");
181 return;
182 }
183
184 me._keyBinding = KeyBinding.fromShortcut(shortcut, me._cb);
185 getDesktop().addKeyBinding(me._keyBinding);
186 me._view.setShortcut(me, KeyBinding.repr(me._keyBinding));
187 return me.update();
188 },
189
190 _setParentMenu: func(m) {
191 me._parent_menu = m;
192 if (me._parent_menu != nil and me._parent_menu._canvas_item != nil and me._cb != nil) {
193 # fixme: add window-level shortcurt handling
194 # if (me._shortcut != nil) {
195 # me._parent_menu._canvas_item.bindShortcut(me._shortcut, me._cb);
196 # }
197 if (me._menu != nil) {
198 for (var i = 0; i < me._menu.count(); i += 1) {
199 me._menu.getItem(i).setCanvasItem(me._parent_menu._canvas_item);
200 }
201 }
202 }
203 },
204
205 setIcon: func(icon) {
206 me._icon = icon;
207 me._view.setIcon(me, icon);
208 return me.update();
209 },
210
211 setCallback: func(cb = nil) {
212 me._cb = cb;
213 if (me._keyBinding) {
214 me._keyBinding.addBinding(cb);
215 }
216 return me;
217 },
218
219 update: func {
220 if (me._view != nil) {
221 me._view.update(me);
222 me._view._updateLayoutSizes(me);
223 }
224 return me;
225 },
226
227 _setView: func(view) {
228 call(gui.Widget._setView, [view], me);
229
230 var el = view._root;
231 el.addEventListener("click", func(e) me.onClicked(e));
232
233 el.addEventListener("mouseenter", func(e) me.onMouseEnter(e));
234 el.addEventListener("mouseleave", func(e) me.onMouseLeave(e));
235 el.addEventListener("drag", func(e) e.stopPropagation());
236 }
237 };
238
239 gui.Menu = {
240 new: func(id = nil) {
241 var m = gui.Popup.new([100, 60], id);
242 m.parents = [gui.Menu] ~ m.parents;
243 m.style = style;
244 m._parent_item = nil;
245
246 m._canvas = m.createCanvas().setColorBackground(style.getColor("bg_color"));
247 m._root = m._canvas.createGroup();
248 m._layout = VBoxLayout.new();
249 m._layout.setSpacing(0);
250 m._canvas_item = nil;
251 m.setLayout(m._layout);
252 m.hide();
253
254 return m;
255 },
256
257 setCanvasItem: func(item) {
258 me._canvas_item = item;
259 for (var i = 0; i < me._layout.count(); i += 1) {
260 me._layout.itemAt(i)._setParentMenu(me);
261 }
262 },
263
264 # @description Add the given menu item to the menu (normally a `canvas.gui.MenuItem`, but can be any `canvas.gui.Widget` in theory)
265 # @return canvas.gui.Menu Return me to enable method chaining
266 addItem: func(item) {
267 item._setParentMenu(me);
268 me._layout.addItem(item);
269 me.setSize(math.max(me._layout.minimumSize()[0], 64), math.max(me._layout.minimumSize()[1], 24));
270 return me;
271 },
272
273 # @description Create, insert and return a `canvas.gui.MenuItem` with given text and an optional callback, icon and enabled state.
274 # @param text: str required Text to display on the menu item
275 # @param cb: callable optional Function / method to call when the item is clicked - if no callback is wanted, nil can be used
276 # @param cb_me: Union[hash, ghost] optional Object to use for any me references in the callback function (see `call`)
277 # @param shortcut: str String representation of the keyboard shortcut for the item
278 # @param icon: str optional Path to the icon (relative to canvas.style._dir_widgets) or nil if none should be displayed
279 # @param enabled: bool optional Whether the item should be enabled (1) or disabled (0)
280 # @return canvas.gui.MenuItem The item that was created
281 createItem: func(text = nil, cb = nil, cb_me = nil, shortcut = nil, icon = nil, enabled = 1) {
282 if (text == nil) {
283 die("cannot create a menu item without text");
284 }
285 var item = gui.MenuItem.new(me._root, me.style, {text: text, cb: cb, cb_me: cb_me, shortcut: shortcut, icon: icon, enabled: enabled});
286 me.addItem(item);
287 return item;
288 },
289
290 # @description Create, insert and return a `canvas.gui.MenuItem with the given text and assign the given submenu to it,
291 # optionally add the given icon and set the given enabled state
292 # @param text: str required Text to display on the menu item
293 # @param menu: canvas.gui.Menu Submenu that shall be assigned to the new menu item
294 # @param enabled: bool optional Whether the item should be enabled (1) or disabled (0)
295 # @return canvas.gui.MenuItem The item that was created
296 addMenu: func(text = nil, menu = nil, enabled = 1) {
297 if (text == nil) {
298 die("cannot create a menu item without text");
299 }
300 if (menu == nil) {
301 die("cannot create a submenu item without submenu");
302 }
303 var item = gui.MenuItem.new(me._root, me.style, {text: text, cb: nil, shortcut: nil, icon: nil, enabled: enabled});
304 menu._parent_item = item;
305 item.setMenu(menu);
306 me.addItem(item);
307 return me;
308 },
309
310 createMenu: func(text = nil, enabled = 1) {
311 if (text == nil) {
312 die("cannot create a submenu item without text");
313 }
314 var menu = gui.Menu.new();
315 var item = gui.MenuItem.new(me._root, me.style, {text: text, cb: nil, shortcut: nil, icon: nil, enabled: enabled});
316 item._setParentMenu(me);
317 item.setMenu(menu);
318 me.addItem(item);
319 return menu;
320 },
321
322 # @description Remove all items from the menu
323 # @return canvas.gui.Menu Return me to enable method chaining
324 clear: func {
325 me._layout.clear();
326 return me;
327 },
328
329 # @description If `item` is a `canvas.gui.Widget`, remove the given `canvas.gui.Widget` from the menu
330 # Else assume `item` to be a scalar and try to find an item of the menu that has a `getText` method
331 # and whose result of calling its `getText` method equals `item` and remove that item
332 # @param item: Union[str, canvas.gui.Widget] required The widget or the text of the menu item to remove
333 removeItem: func(item) {
334 if (isa(item, gui.Widget)) {
335 me._layout.removeItem(item);
336 } else {
337 for (var i = 0; i < me._layout.count(); i += 1) {
338 if (me._layout.itemAt(i)["getText"] != nil and me._layout.itemAt(i).getText() == item) {
339 me._layout.takeAt(i);
340 return me;
341 }
342 }
343 die("No menu item with given text '" ~ item ~ "' found, could not remove !");
344 }
345 },
346
347 # @description If `index` is an integer, remove and return the item at the given `index`
348 # Else assume `item` to be a scalar and try to find an item of the menu that has a `getText` method
349 # and whose result of calling its `getText` method equals `item` and remove and return that item
350 # @param index: Union[int, str] required The index or text of the menu item to remove
351 # @return canvas.gui.Widget The item with given text `index` or at the given position `index`
352 takeAt: func(index) {
353 if (isint(index)) {
354 return me._layout.takeAt(index);
355 } else {
356 for (var i = 0; i < me._layout.count(); i += 1) {
357 if (me._layout.itemAt(i)["getText"] != nil and me._layout.itemAt(i).getText() == index) {
358 return me._layout.takeAt(i);
359 }
360 }
361 die("No menu item with given text '" ~ index ~ "' found, could not remove !");
362 }
363 },
364
365 # @description Count the items of the menu
366 # @return int Number of items
367 count: func() {
368 return me._layout.count();
369 },
370
371 # @description If `index` is an integer, eturn the item at the given `index`
372 # Else assume `item` to be a scalar and try to find an item of the menu that has a `getText` method
373 # and whose result of calling its `getText` method equals `item` and eturn that item
374 # @param index: Union[int, str] required The index or text of the menu item to return
375 # @return canvas.gui.Widget The item with given text `index` or at the given position `index`
376 getItem: func(index) {
377 if (isint(index)) {
378 return me._layout.itemAt(index);
379 } else {
380 for (var i = 0; i < me._layout.count(); i += 1) {
381 if (me._layout.itemAt(i)["getText"] != nil and me._layout.itemAt(i).getText() == index) {
382 return me._layout.itemAt(i);
383 }
384 }
385 die("No menu item with given text '" ~ index ~ "' found, could not remove !");
386 }
387 },
388
389 # @description Show the menu, moving it to the position given by `x` and `y` if ´x` and `y` are not nil
390 show: func(x = nil, y = nil) {
391 if (x != nil and y != nil) {
392 me.setPosition(x, y);
393 }
394 me._ghost.show();
395 me.setInt("z-index", me.get("z-index", gui.STACK_INDEX["always-on-top"]));
396 if (me._canvas != nil) {
397 me._canvas.update();
398 }
399 append(gui.open_popups, me);
400 },
401
402 # @description Hide the menu (force should only be set to 1 from within the click handler of menu items)
403 hide: func(force=0) {
404 if (me._parent_item != nil) {
405 if (
406 force or
407 me._parent_item._parent_menu == nil or
408 #me._parent_item._parent_menu._view._root._node.getValue("id") != "menu-bar" or
409 me._parent_item._mouseOver == 0
410 ) {
411 me._parent_item._hovered = 0;
412 me._parent_item.update();
413 call(me.parents[1].hide, [], me);
414 }
415 } else {
416 call(me.parents[1].hide, [], me);
417 }
418 },
419
420 # @description Destructor
421 del: func() {
422 me.hide();
423 me.clear();
424 me._canvas.del();
425 },
426
427 # @description Update the menu and its items
428 update: func() {
429 me.parents[1].update();
430 for (var i = 0; i < me._layout.count(); i += 1) {
431 me._layout.itemAt(i).update();
432 }
433 return me;
434 },
435 };
436