ozdenui 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +14 -0
  3. data/NOTICE +5 -0
  4. data/README.md +242 -0
  5. data/exe/ozdenui +17 -0
  6. data/lib/ozdenui/AGENTS.md +210 -0
  7. data/lib/ozdenui/COMPONENTS.md +3017 -0
  8. data/lib/ozdenui/app.rb +749 -0
  9. data/lib/ozdenui/command.rb +107 -0
  10. data/lib/ozdenui/components.rb +368 -0
  11. data/lib/ozdenui/dialogs.rb +212 -0
  12. data/lib/ozdenui/files.rb +124 -0
  13. data/lib/ozdenui/icon.png +0 -0
  14. data/lib/ozdenui/kit.json +1 -0
  15. data/lib/ozdenui/kit.rb +115 -0
  16. data/lib/ozdenui/look.rb +229 -0
  17. data/lib/ozdenui/objects.rb +652 -0
  18. data/lib/ozdenui/server.rb +270 -0
  19. data/lib/ozdenui/values.rb +273 -0
  20. data/lib/ozdenui/version.rb +15 -0
  21. data/lib/ozdenui/web/basic/LICENSE +14 -0
  22. data/lib/ozdenui/web/basic/basic.min.css +1 -0
  23. data/lib/ozdenui/web/basic/basic.min.js +78 -0
  24. data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
  25. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
  26. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
  27. data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
  28. data/lib/ozdenui/web/bridge.js +584 -0
  29. data/lib/ozdenui/web/comp/accordion.js +659 -0
  30. data/lib/ozdenui/web/comp/action-button/add.png +0 -0
  31. data/lib/ozdenui/web/comp/action-button.js +521 -0
  32. data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
  33. data/lib/ozdenui/web/comp/app-bar.js +237 -0
  34. data/lib/ozdenui/web/comp/badge.js +292 -0
  35. data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
  36. data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
  37. data/lib/ozdenui/web/comp/canvas.js +652 -0
  38. data/lib/ozdenui/web/comp/chat-view.js +827 -0
  39. data/lib/ozdenui/web/comp/check-box.js +490 -0
  40. data/lib/ozdenui/web/comp/code-editor.js +965 -0
  41. data/lib/ozdenui/web/comp/color-picker.js +636 -0
  42. data/lib/ozdenui/web/comp/context-menu.js +951 -0
  43. data/lib/ozdenui/web/comp/currency-field.js +209 -0
  44. data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
  45. data/lib/ozdenui/web/comp/data-table.js +937 -0
  46. data/lib/ozdenui/web/comp/date-picker.js +1055 -0
  47. data/lib/ozdenui/web/comp/dialog.js +992 -0
  48. data/lib/ozdenui/web/comp/email-field.js +65 -0
  49. data/lib/ozdenui/web/comp/file-picker.js +920 -0
  50. data/lib/ozdenui/web/comp/filters.js +807 -0
  51. data/lib/ozdenui/web/comp/form.js +602 -0
  52. data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
  53. data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
  54. data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
  55. data/lib/ozdenui/web/comp/gauge.js +439 -0
  56. data/lib/ozdenui/web/comp/graph-box.js +3911 -0
  57. data/lib/ozdenui/web/comp/grid-group.js +185 -0
  58. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
  59. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
  60. data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
  61. data/lib/ozdenui/web/comp/image-view.js +598 -0
  62. data/lib/ozdenui/web/comp/item-list.js +1740 -0
  63. data/lib/ozdenui/web/comp/json-editor.js +3221 -0
  64. data/lib/ozdenui/web/comp/json-view.js +597 -0
  65. data/lib/ozdenui/web/comp/label-editor.js +1140 -0
  66. data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
  67. data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
  68. data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
  69. data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
  70. data/lib/ozdenui/web/comp/left-menu.js +548 -0
  71. data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
  72. data/lib/ozdenui/web/comp/loading-screen.js +413 -0
  73. data/lib/ozdenui/web/comp/log-view.js +530 -0
  74. data/lib/ozdenui/web/comp/login-page.js +685 -0
  75. data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
  76. data/lib/ozdenui/web/comp/menu-bar.js +624 -0
  77. data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
  78. data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
  79. data/lib/ozdenui/web/comp/notice.js +229 -0
  80. data/lib/ozdenui/web/comp/number-field.js +128 -0
  81. data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
  82. data/lib/ozdenui/web/comp/page-control.js +762 -0
  83. data/lib/ozdenui/web/comp/page-header.js +191 -0
  84. data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
  85. data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
  86. data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
  87. data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
  88. data/lib/ozdenui/web/comp/password-field.js +192 -0
  89. data/lib/ozdenui/web/comp/period-bar.js +436 -0
  90. data/lib/ozdenui/web/comp/phone-field.js +322 -0
  91. data/lib/ozdenui/web/comp/progress-bar.js +992 -0
  92. data/lib/ozdenui/web/comp/property-panel.js +3639 -0
  93. data/lib/ozdenui/web/comp/radio-button.js +527 -0
  94. data/lib/ozdenui/web/comp/range-slider.js +569 -0
  95. data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
  96. data/lib/ozdenui/web/comp/scroll-area.js +308 -0
  97. data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
  98. data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
  99. data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
  100. data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
  101. data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
  102. data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
  103. data/lib/ozdenui/web/comp/search-input.js +725 -0
  104. data/lib/ozdenui/web/comp/search-results.js +596 -0
  105. data/lib/ozdenui/web/comp/select-box.js +1862 -0
  106. data/lib/ozdenui/web/comp/settings-row.js +299 -0
  107. data/lib/ozdenui/web/comp/side-panel.js +616 -0
  108. data/lib/ozdenui/web/comp/slider-field.js +952 -0
  109. data/lib/ozdenui/web/comp/slider.js +838 -0
  110. data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
  111. data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
  112. data/lib/ozdenui/web/comp/smart-button.js +981 -0
  113. data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
  114. data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
  115. data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
  116. data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
  117. data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
  118. data/lib/ozdenui/web/comp/smart-table.js +1883 -0
  119. data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
  120. data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
  121. data/lib/ozdenui/web/comp/splitter.js +411 -0
  122. data/lib/ozdenui/web/comp/status-bar.js +408 -0
  123. data/lib/ozdenui/web/comp/step-bar.js +345 -0
  124. data/lib/ozdenui/web/comp/stepper.js +753 -0
  125. data/lib/ozdenui/web/comp/tabs.js +1366 -0
  126. data/lib/ozdenui/web/comp/tag-input.js +1107 -0
  127. data/lib/ozdenui/web/comp/tap-effect.js +170 -0
  128. data/lib/ozdenui/web/comp/text-area-field.js +233 -0
  129. data/lib/ozdenui/web/comp/text-field.js +1006 -0
  130. data/lib/ozdenui/web/comp/time-line.js +398 -0
  131. data/lib/ozdenui/web/comp/time-picker.js +1032 -0
  132. data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
  133. data/lib/ozdenui/web/comp/tiny-select.js +322 -0
  134. data/lib/ozdenui/web/comp/tiny-table.js +283 -0
  135. data/lib/ozdenui/web/comp/toast.js +891 -0
  136. data/lib/ozdenui/web/comp/toggle.js +757 -0
  137. data/lib/ozdenui/web/comp/tool-bar.js +760 -0
  138. data/lib/ozdenui/web/comp/tooltip.js +534 -0
  139. data/lib/ozdenui/web/comp/tree-view.js +1179 -0
  140. data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
  141. data/lib/ozdenui/web/comp/ui-standards.js +403 -0
  142. data/lib/ozdenui/web/comp/url-field.js +190 -0
  143. data/lib/ozdenui/web/comp/web-view.js +588 -0
  144. data/lib/ozdenui/web/components.js +123 -0
  145. data/lib/ozdenui/web/index.html +39 -0
  146. data/lib/ozdenui/window.rb +188 -0
  147. data/lib/ozdenui/window_cocoa.rb +221 -0
  148. data/lib/ozdenui.rb +32 -0
  149. metadata +200 -0
@@ -0,0 +1,624 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ MenuBar - v26.11
6
+
7
+ UI COMPONENT
8
+ - The row of menus at the top of a window of a program: File, Edit, View, Help.
9
+ - Its menus are ContextMenus (comp/context-menu.js: load it first), so an item can have an icon, a check mark, a
10
+ submenu, a shortcut, and can be disabled.
11
+ - A click opens a menu, a second click closes it. While a menu is open, the mouse on another title opens that one.
12
+ - Keyboard: Tab comes to the bar; ArrowLeft / ArrowRight go between the titles (also while a menu is open);
13
+ ArrowDown, Enter or Space open a menu; ArrowUp / ArrowDown go between its items; ArrowRight opens a submenu;
14
+ Escape closes.
15
+ - A shortcut is shown in its item and it runs the item: "Ctrl+S", "Ctrl+Shift+N", "F5". Ctrl is also Cmd on a Mac, and
16
+ the Mac shows it as ⌘S. It does not run under a dialog, nor for a disabled item, and a held key runs it once. In a
17
+ text field only the shortcuts with Ctrl run, and Ctrl+C, V, X, A, Z, Y stay with the field. With a selected text
18
+ on the page Ctrl+C copies it.
19
+ - An item can remove the bar in its onClick (a page that draws its window again).
20
+ - Too many titles for a narrow window: the row scrolls sideways.
21
+ - Style packages: "light" (default), "dark", "minimal", "minimalDark" (a package of params: its height too; setTheme()
22
+ changes only the colors and the shapes). Select with styleName (MenuBar.styles), or later with setTheme().
23
+
24
+ MENU: { text, key, enabled, items, onClick }
25
+ - items: its items (below). A menu without items is a title that only runs its onClick.
26
+ ITEM: { text, key, icon, iconFile, shortcut, checked, enabled, items, data, onClick }
27
+ - A string is also accepted: "Copy" -> { text: "Copy" }. "-": a separator line. (As in ContextMenu)
28
+ - onClick: function (self, item, menu) {}: self is the MenuBar. The onClick of the MenuBar is called too.
29
+
30
+ USAGE:
31
+ const bar = MenuBar({
32
+ menus: [
33
+ { text: "File", key: "file", items: [
34
+ { text: "Open", key: "open", shortcut: "Ctrl+O" },
35
+ { text: "Save", key: "save", shortcut: "Ctrl+S" },
36
+ { text: "Open recent", items: ["a.txt", "b.txt"] },
37
+ "-",
38
+ { text: "Quit", key: "quit", enabled: 0 },
39
+ ] },
40
+ { text: "View", key: "view", items: [
41
+ { text: "Word wrap", key: "wrap", checked: 1 },
42
+ ] },
43
+ ],
44
+ onClick: function (self, item, menu) { println(menu.text + " > " + item.text); },
45
+ });
46
+ bar.setItemEnabled("quit", 1);
47
+ bar.setItemChecked("wrap", 0);
48
+ bar.setMenus([...]);
49
+ bar.closeMenu();
50
+ bar.setTheme("dark"); // the check marks and the disabled items stay
51
+ MenuBar({ styleName: "dark" });
52
+
53
+ Started Date: November 2026
54
+ Webpage: https://bug7a.github.io/js-components/
55
+
56
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
57
+ Licensed under the Apache License, Version 2.0
58
+ SPDX-License-Identifier: Apache-2.0
59
+
60
+ */
61
+
62
+ "use strict";
63
+
64
+ // Default values:
65
+ const MenuBarDefaults = {
66
+ key: "0", // Your name for this object: read it in a callback (self.key)
67
+ left: 0,
68
+ top: 0,
69
+ width: "100%",
70
+ height: 34,
71
+ menus: [], // [{ text, key, enabled, items, onClick }]
72
+ enabled: 1,
73
+ ariaLabel: "Menu bar",
74
+ onClick: function (self, item, menu) { }, // An item (with a click, the keyboard or its shortcut)
75
+ onMenuOpen: function (self, menu) { },
76
+ onMenuClose: function (self, menu) { },
77
+ styleName: "light", // "light", "dark", "minimal", "minimalDark" or a name added to MenuBar.styles
78
+ style: { // Light style package (default)
79
+ box: { color: "#F4F4F2", lineColor: Black(0.12) },
80
+ layout: { padding: 4, gap: 2 },
81
+ title: { fontSize: 13, textColor: Black(0.82), height: 26, padding: 10, round: 5 },
82
+ titleHover: { color: Black(0.07), borderColor: "" }, // borderColor: the edge (title.border). "": its own
83
+ titleOpen: { color: Black(0.11), borderColor: "" },
84
+ disabled: { opacity: 0.4 },
85
+ focus: { ringColor: "rgba(63, 141, 148, 0.8)" },
86
+ menuStyleName: "light", // The package of its ContextMenus
87
+ menu: { // Over that package
88
+ item: { height: 30, fontSize: 13 },
89
+ },
90
+ menuMinWidth: 200,
91
+ },
92
+ };
93
+
94
+ const MenuBar = function (params = {}) {
95
+
96
+ // Merge style package: params.style > MenuBar.styles[styleName] > MenuBarDefaults.style (light)
97
+ const pageStyle = params.style; // WHY: setTheme() puts it over another package.
98
+ basic.applyStylePackage(params, MenuBar.styles, MenuBarDefaults);
99
+
100
+ // Merge params:
101
+ mergeIntoIfMissing(params, MenuBarDefaults);
102
+
103
+ // BOX: Component container
104
+ const box = startObject(params);
105
+
106
+ // *** PRIVATE VARIABLES:
107
+ let _s = box.style;
108
+ let openIndex = -1; // The menu that is open (-1: none)
109
+ let openIndexAtPress = -1; // The menu that was open when the mouse was pressed
110
+ let focusIndex = 0; // The title that the Tab key comes to
111
+ let isUsedWithKeys = 0; // 1: the last thing was a key, not the mouse
112
+
113
+ // *** PUBLIC VARIABLES:
114
+ box.menuList = []; // [{ menu, title, contextMenu }]: the menus and their objects
115
+ // NOTE: Default values are also public variables. (box.menus, box.enabled...)
116
+
117
+ // *** PRIVATE FUNCTIONS:
118
+
119
+ // The style of a package, with the style of the page over it. (setTheme)
120
+ const styleOfPackage = function (styleName) {
121
+ const fresh = { styleName: styleName, style: pageStyle };
122
+ basic.applyStylePackage(fresh, MenuBar.styles, MenuBarDefaults);
123
+ mergeIntoIfMissing(fresh, MenuBarDefaults);
124
+ return fresh.style;
125
+ };
126
+
127
+ const isOpen = function (index) {
128
+ return (openIndex == index);
129
+ };
130
+
131
+ const isMenuEnabled = function (menu) {
132
+ return (box.enabled == 1 && !(menu.enabled !== undefined && !basic.isOn(menu.enabled)));
133
+ };
134
+
135
+ const hasItems = function (menu) {
136
+ return (Array.isArray(menu.items) && menu.items.length > 0);
137
+ };
138
+
139
+ // WHY: A ContextMenu makes its own copy of its items. Each copy knows the item of the bar, so a callback gets its
140
+ // present state. Its shortcut is the text to show, and it runs through runItem() alone.
141
+ const toContextMenuItem = function (item) {
142
+ if (typeof item != "object" || item === null) return item;
143
+ const copy = Object.assign({}, item);
144
+ copy.barItem = item;
145
+ copy.onClick = null;
146
+ if (item.shortcut) copy.shortcut = MenuBar.shortcutText(item.shortcut);
147
+ if (Array.isArray(item.items)) copy.items = item.items.map(toContextMenuItem);
148
+ return copy;
149
+ };
150
+
151
+ const toContextMenuItems = function (menu) {
152
+ return (menu.items || []).map(toContextMenuItem);
153
+ };
154
+
155
+ const runItem = function (item, menu) {
156
+ if (typeof item.onClick == "function") item.onClick(box, item, menu);
157
+ if (!box.isRemoved) box.onClick(box, item, menu); // WHY: The item may have removed the bar (a window drawn again).
158
+ };
159
+
160
+ // The edge of a title (title.border: the minimal packages): open, under the mouse, or its own
161
+ const getTitleEdge = function (index, isOver) {
162
+ if (isOpen(index) && _s.titleOpen.borderColor) return _s.titleOpen.borderColor;
163
+ if (isOver && _s.titleHover.borderColor) return _s.titleHover.borderColor;
164
+ return _s.title.borderColor || "transparent";
165
+ };
166
+
167
+ // PAINT: the titles: open, the mouse on it, disabled; only one of them is reached with Tab.
168
+ const paintTitle = function (entry, index, isOver) {
169
+ const title = entry.title;
170
+ const isEnabled = isMenuEnabled(entry.menu);
171
+ let color = "transparent";
172
+ if (isOver && isEnabled) color = _s.titleHover.color;
173
+ if (isOpen(index)) color = _s.titleOpen.color;
174
+ title.color = color;
175
+ if (_s.title.border) title.borderColor = getTitleEdge(index, isOver && isEnabled);
176
+ title.opacity = (isEnabled) ? 1 : _s.disabled.opacity;
177
+ title.cursor = (isEnabled) ? "pointer" : "default";
178
+ title.tabIndex = (index == focusIndex) ? 0 : -1;
179
+ title.aria = { disabled: isEnabled ? "false" : "true" };
180
+ if (hasItems(entry.menu)) title.aria = { expanded: isOpen(index) ? "true" : "false" };
181
+ };
182
+
183
+ const paintTitles = function () {
184
+ box.menuList.forEach(function (entry, index) { paintTitle(entry, index, 0); });
185
+ };
186
+
187
+ const openMenu = function (index, withKeyboard) {
188
+ const entry = box.menuList[index];
189
+ if (!entry || !hasItems(entry.menu) || !isMenuEnabled(entry.menu)) return;
190
+ box.closeMenu();
191
+ openIndex = index;
192
+ focusIndex = index;
193
+ entry.contextMenu.openBelow(entry.title);
194
+ if (withKeyboard) entry.contextMenu.activateFirst();
195
+ paintTitles();
196
+ box.onMenuOpen(box, entry.menu);
197
+ };
198
+
199
+ // The next title that can be used, in a direction (-1, 1). -1: there is none.
200
+ const nextIndex = function (fromIndex, direction, needsItems) {
201
+ const count = box.menuList.length;
202
+ for (let step = 1; step <= count; step++) {
203
+ const index = (fromIndex + direction * step + count * step) % count;
204
+ const menu = box.menuList[index].menu;
205
+ if (isMenuEnabled(menu) && (!needsItems || hasItems(menu))) return index;
206
+ }
207
+ return -1;
208
+ };
209
+
210
+ const focusTitle = function (index) {
211
+ if (!box.menuList[index]) return;
212
+ focusIndex = index;
213
+ paintTitles();
214
+ box.menuList[index].title.elem.focus();
215
+ };
216
+
217
+ const onTitleClick = function (index, event) {
218
+ const entry = box.menuList[index];
219
+ if (!isMenuEnabled(entry.menu)) return;
220
+
221
+ if (!hasItems(entry.menu)) {
222
+ runItem(entry.menu, entry.menu);
223
+ return;
224
+ }
225
+
226
+ const isFromKeyboard = (event.detail === 0); // WHY: Enter and Space make a click without a mouse.
227
+
228
+ // WHY: The press closed this open menu just before this click (ContextMenu closes on a press outside it),
229
+ // so this click must not open it again.
230
+ const wasOpenAtPress = (!isFromKeyboard && openIndexAtPress == index);
231
+ openIndexAtPress = -1;
232
+ if (wasOpenAtPress) return;
233
+
234
+ openMenu(index, isFromKeyboard);
235
+ };
236
+
237
+ const onTitleKeyDown = function (index, event) {
238
+ if (openIndex >= 0) return; // WHY: The open menu reads the keys.
239
+ if (event.key === "ArrowRight" || event.key === "ArrowLeft") {
240
+ event.preventDefault();
241
+ const next = nextIndex(index, (event.key === "ArrowRight") ? 1 : -1, 0);
242
+ if (next >= 0) focusTitle(next);
243
+ } else if (event.key === "ArrowDown") {
244
+ event.preventDefault();
245
+ openMenu(index, 1);
246
+ }
247
+ };
248
+
249
+ // The side keys of an open menu: the next menu of the bar opens.
250
+ const onSideKey = function (direction) {
251
+ const next = nextIndex(openIndex, direction, 1);
252
+ if (next >= 0 && next != openIndex) openMenu(next, 1);
253
+ };
254
+
255
+ const onContextMenuClose = function (index) {
256
+ if (openIndex != index) return;
257
+ openIndex = -1;
258
+ paintTitles();
259
+ // WHY the keys: the focus was in the menu, and a keyboard user would lose the place. After the mouse a focus by
260
+ // code would show the ring of the keyboard on the title.
261
+ if (isUsedWithKeys) box.menuList[index].title.elem.focus({ preventScroll: true });
262
+ box.onMenuClose(box, box.menuList[index].menu);
263
+ };
264
+
265
+ // WHY: One ContextMenu for each menu. It stands on the page: box.remove() and removeMenus() remove it.
266
+ const createContextMenu = function (menu, index) {
267
+ return ContextMenu({
268
+ items: toContextMenuItems(menu),
269
+ styleName: _s.menuStyleName,
270
+ style: _s.menu,
271
+ minWidth: _s.menuMinWidth,
272
+ ariaLabel: menu.text,
273
+ onClick: function (self, item) { runItem(item.barItem || item, menu); },
274
+ onClose: function () { onContextMenuClose(index); },
275
+ onSideKey: function (self, direction) { onSideKey(direction); },
276
+ });
277
+ };
278
+
279
+ const applyTitleLook = function (title) {
280
+ title.props({ height: _s.title.height, fontSize: _s.title.fontSize, textColor: _s.title.textColor, round: _s.title.round });
281
+ if (_s.title.border) title.props({ border: _s.title.border, borderColor: _s.title.borderColor || "transparent" });
282
+ title.css.padding = "0 " + _s.title.padding + "px";
283
+ title.setFocusRing(_s.focus.ringColor);
284
+ };
285
+
286
+ const createMenu = function (menu, index) {
287
+
288
+ // BUTTON: The title
289
+ const title = Button({
290
+ width: "auto",
291
+ color: "transparent",
292
+ shrink: 0,
293
+ css: { whiteSpace: "nowrap" },
294
+ });
295
+ title.minimal = 1;
296
+ title.plainText = menu.text;
297
+ title.role = "menuitem";
298
+ if (hasItems(menu)) title.aria = { haspopup: "menu" };
299
+ applyTitleLook(title);
300
+
301
+ const contextMenu = createContextMenu(menu, index);
302
+
303
+ title.onHoverChange(function (self, isOver) {
304
+ paintTitle(box.menuList[index], index, isOver);
305
+ const isOtherMenuOpen = (openIndex >= 0 && openIndex != index);
306
+ if (isOver && isOtherMenuOpen && hasItems(menu)) openMenu(index, 0);
307
+ });
308
+ title.on("click", function (self, event) { onTitleClick(index, event); });
309
+ title.on("keydown", function (self, event) { onTitleKeyDown(index, event); });
310
+
311
+ return { menu: menu, title: title, contextMenu: contextMenu };
312
+
313
+ };
314
+
315
+ const removeMenus = function () {
316
+ box.closeMenu();
317
+ box.menuList.forEach(function (entry) {
318
+ entry.contextMenu.remove();
319
+ entry.title.remove();
320
+ });
321
+ box.menuList = [];
322
+ };
323
+
324
+ const applyBarLook = function () {
325
+ box.color = _s.box.color;
326
+ box.css.borderBottom = "1px solid " + _s.box.lineColor;
327
+ box.layout.gap = _s.layout.gap;
328
+ box.layout.padding = [_s.layout.padding, 0];
329
+ };
330
+
331
+ // LOOK: everything that comes from the style (setTheme)
332
+ // WHY the titles stay: one of them may have the focus. The menus are made again: a ContextMenu keeps its style.
333
+ const applyLook = function () {
334
+ applyBarLook();
335
+ box.menuList.forEach(function (entry, index) {
336
+ applyTitleLook(entry.title);
337
+ entry.contextMenu.remove();
338
+ entry.contextMenu = createContextMenu(entry.menu, index);
339
+ });
340
+ paintTitles();
341
+ };
342
+
343
+ const createMenus = function () {
344
+ removeMenus();
345
+ openIndex = -1;
346
+ focusIndex = Math.max(0, box.menus.findIndex(isMenuEnabled));
347
+ box.menuList = box.menus.map(function (menu, index) {
348
+ return createIn(box.layout, function () { return createMenu(menu, index); });
349
+ });
350
+ paintTitles();
351
+ };
352
+
353
+ // The item (and its menu) that runs with this key. null: none.
354
+ const findShortcutItem = function (event) {
355
+ for (const entry of box.menuList) {
356
+ const item = (isMenuEnabled(entry.menu)) ? MenuBar.findShortcutItem(entry.menu.items, event) : null;
357
+ if (item) return { item: item, menu: entry.menu };
358
+ }
359
+ return null;
360
+ };
361
+
362
+ const onWindowKeyDown = function (self, event) {
363
+ if (box.enabled != 1 || event.defaultPrevented) return;
364
+ const found = findShortcutItem(event);
365
+ if (!found || MenuBar.isDialogOpen()) return;
366
+ event.preventDefault(); // WHY: Ctrl+S must not also open the save window of the browser.
367
+ if (event.repeat) return; // WHY: A held key runs it once (a job that starts and stops many times).
368
+ box.closeMenu();
369
+ runItem(found.item, found.menu);
370
+ };
371
+
372
+ // The menu of an item (the first one that has it), and the item
373
+ const findItem = function (key) {
374
+ let found = null;
375
+ box.menuList.forEach(function (entry) {
376
+ MenuBar.forEachItem(entry.menu.items, function (item) {
377
+ if (!found && item.key == key) found = { item: item, entry: entry };
378
+ });
379
+ });
380
+ return found;
381
+ };
382
+
383
+ // The first level is changed in place; an item of a submenu needs its menu to be made again.
384
+ const applyItemChange = function (found, changeInPlace) {
385
+ const contextMenu = found.entry.contextMenu;
386
+ if (contextMenu.getItemByKey(found.item.key)) changeInPlace(contextMenu);
387
+ else contextMenu.setItems(toContextMenuItems(found.entry.menu));
388
+ };
389
+
390
+ // *** PUBLIC FUNCTIONS:
391
+
392
+ box.setMenus = function (menus) {
393
+ box.menus = (Array.isArray(menus)) ? menus.map(MenuBar.copyItem) : [];
394
+ createMenus();
395
+ };
396
+ // USAGE: bar.setMenus([{ text: "File", items: ["Open", "Save"] }])
397
+
398
+ box.closeMenu = function () {
399
+ const entry = box.menuList[openIndex];
400
+ if (entry) entry.contextMenu.close();
401
+ };
402
+ // USAGE: bar.closeMenu()
403
+
404
+ box.setItemEnabled = function (key, value) {
405
+ const found = findItem(key);
406
+ if (!found) return;
407
+ found.item.enabled = basic.isOn(value) ? 1 : 0;
408
+ applyItemChange(found, function (contextMenu) { contextMenu.setItemEnabled(key, found.item.enabled); });
409
+ };
410
+ // USAGE: bar.setItemEnabled("paste", 0)
411
+
412
+ box.setItemChecked = function (key, value) {
413
+ const found = findItem(key);
414
+ if (!found) return;
415
+ found.item.checked = basic.isOn(value) ? 1 : 0;
416
+ applyItemChange(found, function (contextMenu) { contextMenu.setItemChecked(key, found.item.checked); });
417
+ };
418
+ // USAGE: bar.setItemChecked("wrap", 0)
419
+
420
+ box.setEnabled = function (value) {
421
+ box.enabled = basic.isOn(value) ? 1 : 0;
422
+ if (box.enabled != 1) box.closeMenu();
423
+ paintTitles();
424
+ };
425
+ // USAGE: get: bar.enabled, set: bar.setEnabled(0)
426
+
427
+ // Another style package while it is on the page. A style given by the page stays over it.
428
+ box.setTheme = function (styleName) {
429
+ box.styleName = styleName;
430
+ _s = styleOfPackage(styleName);
431
+ box.style = _s;
432
+ applyLook();
433
+ };
434
+ // USAGE: bar.setTheme("dark")
435
+
436
+ // WHY: box.superRemove is overwritten by a component that extends this one, so the local copy is called below.
437
+ const superRemove = box.remove;
438
+ box.superRemove = superRemove;
439
+ box.remove = function () {
440
+ // WHY isRemoved and box is not set to null: an item can remove the bar in its onClick, and the code of its
441
+ // click goes on.
442
+ if (box.isRemoved) return; // WHY: remove() can be called twice (also by the parent's remove()).
443
+ removeMenus(); // WHY: not with box.own(): setMenus() makes new ones, and own() would keep the old ones to the end.
444
+ superRemove.call(box);
445
+ };
446
+ // USAGE: bar.remove();
447
+
448
+ // *** OBJECT VIEW:
449
+
450
+ // BOX: The bar
451
+ box.css.boxSizing = "border-box";
452
+ box.role = "menubar";
453
+ box.aria = { label: box.ariaLabel };
454
+
455
+ // GROUP: The titles (made by createMenus)
456
+ box.layout = HGroup({
457
+ width: "100%",
458
+ height: "100%",
459
+ align: "left center",
460
+ scrollX: 1, // WHY: A narrow window still reaches every title.
461
+ });
462
+ box.layout.css.scrollbarWidth = "none";
463
+ endGroup();
464
+
465
+ // *** OBJECT INIT CODE:
466
+
467
+ // WHY capture on window: it runs before the capture of ContextMenu on document, which closes the menu.
468
+ box.listenTo(window, "pointerdown", function () {
469
+ openIndexAtPress = openIndex;
470
+ isUsedWithKeys = 0;
471
+ }, true);
472
+ box.listenTo(window, "keydown", function () { isUsedWithKeys = 1; }, true);
473
+ box.listenTo(window, "keydown", onWindowKeyDown);
474
+
475
+ applyBarLook();
476
+ box.setMenus(params.menus);
477
+
478
+ return endObject(box);
479
+
480
+ };
481
+
482
+ // *** STATIC VARIABLES AND FUNCTIONS:
483
+
484
+ MenuBar.FIELD_KEYS = ["c", "v", "x", "a", "z", "y"];
485
+
486
+ MenuBar.isMac = function () {
487
+ return /Mac|iPhone|iPad/.test(navigator.platform || "");
488
+ };
489
+
490
+ // "Ctrl+Shift+S" -> { ctrl: true, shift: true, alt: false, key: "s" }
491
+ MenuBar.parseShortcut = function (text) {
492
+ const parts = String(text).toLowerCase().split("+").map(function (part) { return part.trim(); });
493
+ return {
494
+ ctrl: (parts.indexOf("ctrl") >= 0 || parts.indexOf("cmd") >= 0),
495
+ shift: parts.indexOf("shift") >= 0,
496
+ alt: parts.indexOf("alt") >= 0,
497
+ key: parts[parts.length - 1],
498
+ };
499
+ };
500
+
501
+ // WHY ctrlKey or metaKey: "Ctrl" of a shortcut is Cmd on a Mac. A page writes one shortcut for both.
502
+ MenuBar.isShortcutEvent = function (shortcut, event) {
503
+ const isCtrlDown = (event.ctrlKey || event.metaKey);
504
+ if (shortcut.ctrl != isCtrlDown || shortcut.shift != event.shiftKey || shortcut.alt != event.altKey) return false;
505
+ const key = (event.key === " ") ? "space" : String(event.key).toLowerCase();
506
+ return (key == shortcut.key);
507
+ };
508
+
509
+ // WHY a copy: setItemChecked() and setItemEnabled() write in the items, and the data of the page must not change
510
+ // (two bars can be made from one list).
511
+ MenuBar.copyItem = function (item) {
512
+ if (typeof item != "object" || item === null) return item;
513
+ const copy = Object.assign({}, item);
514
+ if (Array.isArray(item.items)) copy.items = item.items.map(MenuBar.copyItem);
515
+ return copy;
516
+ };
517
+
518
+ // SHORTCUTS of a list of items: the items (their submenus too) that have one. (ToolBar uses them too.)
519
+
520
+ MenuBar.forEachItem = function (items, func) {
521
+ (items || []).forEach(function (item) {
522
+ if (typeof item != "object" || item === null) return;
523
+ func(item);
524
+ MenuBar.forEachItem(item.items, func);
525
+ });
526
+ };
527
+
528
+ MenuBar.isTextField = function (element) {
529
+ if (!element || !element.tagName) return false;
530
+ return (element.tagName === "INPUT" || element.tagName === "TEXTAREA" || element.isContentEditable);
531
+ };
532
+
533
+ // WHY the rects: a closed Dialog can stay in the page, hidden.
534
+ MenuBar.isDialogOpen = function () {
535
+ return Array.from(document.querySelectorAll('[aria-modal="true"]')).some(function (element) {
536
+ return element.getClientRects().length > 0;
537
+ });
538
+ };
539
+
540
+ // WHY: In a text field these keys belong to the field (a menu may also have Copy and Paste).
541
+ MenuBar.isKeyOfField = function (shortcut) {
542
+ return (!shortcut.ctrl || (MenuBar.FIELD_KEYS.indexOf(shortcut.key) >= 0 && !shortcut.alt));
543
+ };
544
+
545
+ // WHY: A text selected on the page (a line of a LogView) is copied by the browser, also with a Copy in a menu.
546
+ MenuBar.isCopyOfSelection = function (shortcut) {
547
+ return (shortcut.ctrl && shortcut.key === "c" && !window.getSelection().isCollapsed);
548
+ };
549
+
550
+ // The first item of a list that runs with this key. null: none.
551
+ MenuBar.findShortcutItem = function (items, event) {
552
+ const inTextField = MenuBar.isTextField(event.target);
553
+ let found = null;
554
+ MenuBar.forEachItem(items, function (item) {
555
+ if (found || !item.shortcut) return;
556
+ const shortcut = MenuBar.parseShortcut(item.shortcut);
557
+ if (!MenuBar.isShortcutEvent(shortcut, event)) return;
558
+ if (inTextField && MenuBar.isKeyOfField(shortcut)) return;
559
+ if (MenuBar.isCopyOfSelection(shortcut)) return;
560
+ if (item.enabled !== undefined && !basic.isOn(item.enabled)) return;
561
+ found = item;
562
+ });
563
+ return found;
564
+ };
565
+
566
+ // The text to show in an item. On a Mac: "Ctrl+Shift+S" -> "⇧⌘S"
567
+ MenuBar.shortcutText = function (text) {
568
+ if (!MenuBar.isMac()) return String(text);
569
+ const parts = String(text).split("+").map(function (part) { return part.trim(); });
570
+ const key = parts[parts.length - 1];
571
+ const lowerParts = parts.map(function (part) { return part.toLowerCase(); });
572
+ let marks = "";
573
+ if (lowerParts.indexOf("alt") >= 0) marks += "⌥";
574
+ if (lowerParts.indexOf("shift") >= 0) marks += "⇧";
575
+ if (lowerParts.indexOf("ctrl") >= 0 || lowerParts.indexOf("cmd") >= 0) marks += "⌘";
576
+ return marks + key.toUpperCase();
577
+ };
578
+
579
+ // *** STYLE PACKAGES:
580
+ // USAGE: MenuBar({ styleName: "dark" })
581
+ // USAGE: MenuBar({ styleName: "dark", style: { title: { fontSize: 14 } } }) // Change only some keys.
582
+ // USAGE: MenuBar({ style: { menu: { menu: { round: 4 } } } }) // The ContextMenus too.
583
+ // NOTE: A new package needs only the keys that differ from the default (light) style.
584
+ // USAGE: MenuBar.styles.myStyle = { box: { color: "tomato" } };
585
+ MenuBar.styles = {
586
+
587
+ // A light gray bar, dark text, a soft line under it; white menus.
588
+ light: MenuBarDefaults.style,
589
+
590
+ // For a dark window; dark menus.
591
+ dark: {
592
+ box: { color: "#232322", lineColor: White(0.1) },
593
+ title: { textColor: White(0.86) },
594
+ titleHover: { color: White(0.09) },
595
+ titleOpen: { color: White(0.15) },
596
+ focus: { ringColor: "rgba(111, 198, 205, 0.8)" },
597
+ menuStyleName: "dark",
598
+ },
599
+
600
+ };
601
+
602
+ // The minimal theme (_designs/minimal-theme.htm): no fill (the ground of the window), one line under it, 13 px titles
603
+ // with a line around the one under the mouse and an ink line around the open one; its menus are ContextMenu minimal.
604
+ MenuBar.createMinimalStyle = function (colors) {
605
+ return {
606
+ height: 30,
607
+ style: {
608
+ box: { color: "transparent", lineColor: colors.line },
609
+ layout: { padding: 2, gap: 2 },
610
+ title: { fontSize: 13, textColor: colors.text, height: 24, padding: 9, round: 3, border: 1, borderColor: "transparent" },
611
+ titleHover: { color: "transparent", borderColor: colors.lineHover },
612
+ titleOpen: { color: "transparent", borderColor: colors.ink },
613
+ disabled: { opacity: 0.4 },
614
+ focus: { ringColor: colors.focus },
615
+ menuStyleName: colors.menuStyleName,
616
+ menu: { item: { height: 28, fontSize: 13 } },
617
+ menuMinWidth: 180,
618
+ },
619
+ };
620
+ };
621
+ MenuBar.MINIMAL_COLORS = { text: Black(0.85), line: Black(0.2), lineHover: Black(0.45), ink: "#1F1F1F", focus: "#3D8BD9", menuStyleName: "minimal" };
622
+ MenuBar.styles.minimal = MenuBar.createMinimalStyle(MenuBar.MINIMAL_COLORS);
623
+ MenuBar.MINIMAL_DARK_COLORS = { text: White(0.88), line: White(0.18), lineHover: White(0.42), ink: "#EDEDEB", focus: "#7FB6DE", menuStyleName: "minimalDark" };
624
+ MenuBar.styles.minimalDark = MenuBar.createMinimalStyle(MenuBar.MINIMAL_DARK_COLORS);