| 1 |
# MenuBar.nas - a menu bar that can be added as a normal widget to a layout |
| 2 |
|
| 3 |
# SPDX-FileCopyrightText: (C) 2022 Frederic Croix <[email protected]> |
| 4 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
|
| 6 |
gui.widgets.MenuBar = { |
| 7 |
new: func(parent, style = nil, cfg = nil) { |
| 8 |
style = style or canvas.style; |
| 9 |
cfg = Config.new(cfg); |
| 10 |
var m = gui.Widget.new(gui.widgets.MenuBar, cfg); |
| 11 |
m._focus_policy = m.NoFocus; |
| 12 |
|
| 13 |
m._setView(style.createWidget(parent, "menu-bar", cfg)); |
| 14 |
|
| 15 |
m._layout = HBoxLayout.new(); |
| 16 |
m._layout.setSpacing(0); |
| 17 |
m._layout.setCanvas(m._view._root.getCanvas()); |
| 18 |
m.setCanvasItem(parent); |
| 19 |
|
| 20 |
m.setLayoutMinimumSize([48, 24]); |
| 21 |
m.setLayoutSizeHint([48, 24]); |
| 22 |
m.setLayoutMaximumSize([48, 24]); |
| 23 |
|
| 24 |
return m; |
| 25 |
}, |
| 26 |
|
| 27 |
setCanvasItem: func(item) { |
| 28 |
me._canvas_item = item; |
| 29 |
for (var i = 0; i < me._layout.count(); i += 1) { |
| 30 |
me._layout.itemAt(i)._setParentMenu(me); |
| 31 |
} |
| 32 |
}, |
| 33 |
|
| 34 |
# @description Create, insert and return a `canvas.gui.MenuItem with the given text and assign the given submenu to it, |
| 35 |
# optionally add the given icon and set the given enabled state |
| 36 |
# @param text: str required Text to display on the menu item |
| 37 |
# @param menu: canvas.gui.Menu Submenu that shall be assigned to the new menu item |
| 38 |
# @param enabled: bool optional Whether the item should be enabled (1) or disabled (0) |
| 39 |
# @return canvas.gui.MenuItem The item that was created |
| 40 |
addMenu: func(text = nil, menu = nil, enabled = 1) { |
| 41 |
if (text == nil) { |
| 42 |
die("cannot create a menu item without text"); |
| 43 |
} |
| 44 |
if (menu == nil) { |
| 45 |
die("cannot create a submenu item without submenu"); |
| 46 |
} |
| 47 |
var item = gui.MenuItem.new(me._view._items, style, |
| 48 |
{ |
| 49 |
text: text, cb: nil, shortcut: nil, icon: nil, enabled: enabled, |
| 50 |
menu_position: gui.MenuItem.MenuPosition.Below, |
| 51 |
} |
| 52 |
); |
| 53 |
item._is_menubar_item = 1; |
| 54 |
item._setParentMenu(me); |
| 55 |
item.setMenu(menu); |
| 56 |
me._layout.addItem(item); |
| 57 |
me.setSize(math.max(me._layout.minimumSize()[0], 64), math.max(me._layout.minimumSize()[1], 24)); |
| 58 |
return me; |
| 59 |
}, |
| 60 |
|
| 61 |
createMenu: func(text = nil, enabled = 1) { |
| 62 |
if (text == nil) { |
| 63 |
die("cannot create a submenu item without text"); |
| 64 |
} |
| 65 |
var menu = gui.Menu.new(); |
| 66 |
me.addMenu(text, menu, enabled); |
| 67 |
return menu; |
| 68 |
}, |
| 69 |
|
| 70 |
# @description Remove all items from the menu |
| 71 |
# @return canvas.gui.Menu Return me to enable method chaining |
| 72 |
clear: func { |
| 73 |
me._layout.clear(); |
| 74 |
return me; |
| 75 |
}, |
| 76 |
|
| 77 |
# @description If `item` is a `canvas.gui.Widget`, remove the given `canvas.gui.Widget` from the menu |
| 78 |
# Else assume `item` to be a scalar and try to find an item of the menu that has a `getText` method |
| 79 |
# and whose result of calling its `getText` method equals `item` and remove that item |
| 80 |
# @param item: Union[str, canvas.gui.Widget] required The widget or the text of the menu item to remove |
| 81 |
removeMenu: func(item) { |
| 82 |
if (isa(item, gui.Widget)) { |
| 83 |
me._layout.removeItem(item); |
| 84 |
} else { |
| 85 |
for (var i = 0; i < me._layout.count(); i += 1) { |
| 86 |
if (me._layout.itemAt(i)["getText"] != nil and me._layout.itemAt(i).getText() == item) { |
| 87 |
me._layout.takeAt(i); |
| 88 |
return me; |
| 89 |
} |
| 90 |
} |
| 91 |
die("No menu with given text '" ~ item ~ "' found, could not remove !"); |
| 92 |
} |
| 93 |
}, |
| 94 |
|
| 95 |
# @description If `index` is an integer, remove and return the item at the given `index` |
| 96 |
# Else assume `item` to be a scalar and try to find an item of the menu that has a `getText` method |
| 97 |
# and whose result of calling its `getText` method equals `item` and remove and return that item |
| 98 |
# @param index: Union[int, str] required The index or text of the menu item to remove |
| 99 |
# @return canvas.gui.Widget The item with given text `index` or at the given position `index` |
| 100 |
takeAt: func(index) { |
| 101 |
if (isint(index)) { |
| 102 |
return me._layout.takeAt(index); |
| 103 |
} else { |
| 104 |
for (var i = 0; i < me._layout.count(); i += 1) { |
| 105 |
if (me._layout.itemAt(i)["getText"] != nil and me._layout.itemAt(i).getText() == index) { |
| 106 |
return me._layout.takeAt(i)._menu; |
| 107 |
} |
| 108 |
} |
| 109 |
die("No menu with given text '" ~ index ~ "' found, could not remove !"); |
| 110 |
} |
| 111 |
}, |
| 112 |
|
| 113 |
# @description Count the items of the menu |
| 114 |
# @return int Number of items |
| 115 |
count: func() { |
| 116 |
return me._layout.count(); |
| 117 |
}, |
| 118 |
|
| 119 |
# @description If `index` is an integer, eturn the item at the given `index` |
| 120 |
# Else assume `item` to be a scalar and try to find an item of the menu that has a `getText` method |
| 121 |
# and whose result of calling its `getText` method equals `item` and eturn that item |
| 122 |
# @param index: Union[int, str] required The index or text of the menu item to return |
| 123 |
# @return canvas.gui.Widget The item with given text `index` or at the given position `index` |
| 124 |
getItem: func(index) { |
| 125 |
if (isint(index)) { |
| 126 |
return me._layout.itemAt(index); |
| 127 |
} else { |
| 128 |
for (var i = 0; i < me._layout.count(); i += 1) { |
| 129 |
if (me._layout.itemAt(i)["getText"] != nil and me._layout.itemAt(i).getText() == index) { |
| 130 |
return me._layout.itemAt(i)._menu; |
| 131 |
} |
| 132 |
} |
| 133 |
die("No menu item with given text '" ~ index ~ "' found, could not remove !"); |
| 134 |
} |
| 135 |
}, |
| 136 |
|
| 137 |
getMenu: func(index) { |
| 138 |
return me.getItem(index)._menu; |
| 139 |
}, |
| 140 |
|
| 141 |
setSize: func { |
| 142 |
if (size(arg) == 1) { |
| 143 |
var arg = arg[0]; |
| 144 |
} |
| 145 |
var (x, y) = arg; |
| 146 |
me._size = [x, 24]; |
| 147 |
me.setAlignment(0x01 | 0x20); |
| 148 |
return me.update(); |
| 149 |
}, |
| 150 |
|
| 151 |
# @description Update the menu and its items |
| 152 |
update: func() { |
| 153 |
if(me._layout.getParent() == nil) { |
| 154 |
me._layout.setParent(me); |
| 155 |
} |
| 156 |
|
| 157 |
me._layout.setGeometry([0, 0, me._size[0], me._size[1]]); |
| 158 |
me._view.setSize(me, me._size[0], me._size[1]); |
| 159 |
me._view.update(me); |
| 160 |
|
| 161 |
return me; |
| 162 |
}, |
| 163 |
}; |
| 164 |
|