-
Notifications
You must be signed in to change notification settings - Fork 17
/
demo.js
314 lines (298 loc) · 10.1 KB
/
demo.js
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
let nothingness_state = true;
let radio_state = "foo";
/** @type {OSGUITopLevelMenus} */
const demo_menus = {
"&File": [
{
label: "&Open",
action: () => {
const $w = $Window({ title: "Ali Baba and the Forty Thieves", resizable: false, maximizeButton: false, minimizeButton: false });
$w.$content.html("<p>\"Open Sesame!\"</p>");
$w.$Button("OK", () => $w.close()).focus().css({ width: 100 });
$w.center();
},
shortcutLabel: "Ctrl+O",
description: "Shows a silly dialog box.",
},
MENU_DIVIDER,
{
label: "&Brexit",
action: () => {
const $w = $Window({ title: "Membership Status", resizable: false, maximizeButton: false, minimizeButton: false });
$w.$content.html("<p>You have left the EU.</p>");
$w.$Button("OK", () => $w.close()).focus().css({ width: 100 });
$w.center();
},
description: "Shows a stupid dialog box.",
}
],
"&View": [
{
label: "&Nothingness",
checkbox: {
check: () => nothingness_state,
toggle: () => {
nothingness_state = !nothingness_state;
}
},
description: "Tick a meaningless checkbox.",
},
{
label: "&Physics",
submenu: [
{
label: "&Schrödinger's Checkbox",
checkbox: {
check: () => {
// this.cat_is_alive = this.cat_is_alive ?? Math.random() > 0.5;
// return this.cat_is_alive;
return Math.random() > 0.5;
}
},
description: "The checked state is indeterminate until observed.",
},
],
description: "Contains a stupid physics joke.",
},
{
label: "&Checkboxes",
submenu: new Array(100).fill(0).map((_, i) => ({
label: `Item ${i}`,
description: `Toggles item ${i}.`,
checkbox: {
check: function () {
// @ts-ignore
return this._pointless_checkbox_value;
},
toggle: function () { this._pointless_checkbox_value = !this._pointless_checkbox_value; },
_pointless_checkbox_value: Math.sin((i / 5) ** 2) > 0,
},
shortcutLabel: `Ctrl+${i}`,
})),
description: "Contains many checkbox items.",
},
{
label: "&Radio Buttons",
submenu: [
{
radioItems: [
{
label: "&Foo",
value: "foo",
description: "Sets the radio state to \"foo\".",
},
{
label: "&Bar",
value: "bar",
description: "Sets the radio state to \"bar\".",
},
{
label: "&Baz",
value: "baz",
description: "Sets the radio state to \"baz\".",
},
],
getValue: () => radio_state,
setValue: (new_value) => { radio_state = new_value; },
ariaLabel: "Example radio group",
},
],
description: "Contains radio button menu items.",
},
],
"&Edit": [
{
label: "Copy",
action: () => {
const $w = $Window({ title: "Radio Message", resizable: false, maximizeButton: false, minimizeButton: false });
$w.$content.html("<p>\"Over and out!\"</p>");
$w.$Button("OK", () => $w.close()).focus().css({ width: 100 });
$w.center();
},
shortcutLabel: "Ctrl+C",
description: "Shows a pointless dialog.",
},
{
label: "Paste",
enabled: false,
shortcutLabel: "Ctrl+V",
description: "This menu item is disabled.",
},
],
};
// wait for page load (could alternatively just move the script so it executes after the elements are declared)
$(() => {
// Create menu bar
const menubar = MenuBar(demo_menus);
$(menubar.element).appendTo("#menubar-example");
// Create demo windows
const $app_window_1 = new $Window({ title: "Application Window", resizable: true });
$app_window_1.$content.append(`
<p>This is a window that can be moved around and resized.</p>
`);
fake_closing($app_window_1);
const $tool_window_1 = new $Window({ title: "Tool Window", toolWindow: true });
$tool_window_1.$content.text("This is a tool window.");
fake_closing($tool_window_1);
const $app_window_2 = new $Window({ title: "Application Example", resizable: true });
const app_window_2_menu_bar = new MenuBar(demo_menus);
$app_window_2.setMenuBar(app_window_2_menu_bar);
$app_window_2.$content.css({
padding: 0,
display: "flex",
flexDirection: "column",
});
$app_window_2.$content.append(`
<div class="inset-deep" style="padding: 20px; background: var(--Window); color: var(--WindowText); user-select: text; cursor: text; flex: 1;">
<p>This is the main application window.</p>
<p>It has a tool window that belongs to it, as well as a menu bar.</p>
</div>
`);
const $status_bar = $("<div class='status-bar inset-shallow' style='height:1.5em;line-height:1.5em;font-size:12px;margin-top:2px;'>").appendTo($app_window_2.$content);
$(app_window_2_menu_bar.element).on("info", (event, info) => {
// info = `event.detail.description: ${event.detail.description}, jQuery second arg: ${info}`; // testing
// @ts-ignore (Hm, kind of annoying that the `detail` is typed as a number here... A simple custom listener API could be better.)
info = event.detail.description; // new API
$status_bar.text(info);
});
function showDefaultStatus() {
$status_bar.text("I am a status bar. This is my default text.");
}
showDefaultStatus();
$(app_window_2_menu_bar.element).on("default-info", (event) => {
showDefaultStatus();
});
fake_closing($app_window_2);
const $tool_window_2 = new $Window({ title: "Tool Window", toolWindow: true, parentWindow: $app_window_2 });
$tool_window_2.$content.text("This tool window is a child of the app window.");
fake_closing($tool_window_2);
$($app_window_2.element).on("closed", () => {
$tool_window_2.close();
});
const $app_window_3 = new $Window({ title: "Right-To-Left Example", resizable: true });
$($app_window_3.element).css("direction", "rtl");
$app_window_3.setMenuBar(new MenuBar(demo_menus));
$app_window_3.$content.css({
padding: 0,
display: "flex",
flexDirection: "column",
});
$app_window_3.$content.append(`
<div style='padding: 20px; background: var(--Window); color: var(--WindowText); user-select: text; cursor: text; flex: 1;'>
<p dir="ltr">You can imagine some Hebrew/Arabic/etc. text in the menus and titlebar.</p>
</div>
`);
fake_closing($app_window_3);
// Position the windows within the demo page, in the flow of text, but freely moveable
/** @type {WeakMap<HTMLElement, {top: number, left: number} | undefined>} */
const new_offsets = new WeakMap();
/** @type {WeakMap<HTMLElement, {top: number, left: number} | undefined>} */
const old_offsets = new WeakMap();
/** @type {[OSGUI$Window, JQuery<HTMLElement>][]} */
const $windows_and_$positioners = [
[$app_window_1, $("#app-window-example")],
[$tool_window_1, $("#tool-window-example")],
[$app_window_2, $("#app-window-2-positioner")],
[$tool_window_2, $("#tool-window-2-positioner")],
[$app_window_3, $("#app-window-3-positioner")],
];
function position_windows() {
// Make measurements first in a separate loop to prevent layout thrashing (untested performance optimization)
for (const [$window, $positioning_el] of $windows_and_$positioners) {
const new_offset = $positioning_el.offset();
new_offsets.set($positioning_el[0], new_offset);
}
// Then apply the new positions
for (const [$window, $positioning_el] of $windows_and_$positioners) {
const new_offset = new_offsets.get($positioning_el[0]);
const old_offset = old_offsets.get($positioning_el[0]);
if (!new_offset) { continue; } // Type narrowing. May skip if the element is not visible, perhaps?
if (!old_offset || new_offset.top !== old_offset.top || new_offset.left !== old_offset.left) {
$window.restore(); // in case it was minimized or maximized
$($window.element).css({
left: new_offset.left,
top: new_offset.top,
width: "",
height: "",
});
old_offsets.set($positioning_el[0], new_offset);
}
}
}
$(window).on("resize", position_windows);
position_windows();
/**
* Fake closing the windows (hide and fade back in), for demo purposes
* @param {OSGUI$Window} $window
*/
function fake_closing($window) {
$($window.element).on("close", (event) => {
event.preventDefault();
$($window.element).triggerHandler("closed");
$window.closed = true;
$($window.element).hide();
setTimeout(() => {
// Restore position
const $positioning_el = $windows_and_$positioners.find(([$other_window]) => $window === $other_window)?.[1];
const offset = $positioning_el?.offset();
if (!offset) { return console.error("Could not find positioning element for window", $window); }
$window.restore(); // in case it was minimized or maximized (TODO: avoid animation, which can cause incorrect positioning)
$($window.element).css({
left: offset.left,
top: offset.top,
width: "",
height: "",
});
// Fade back in
$($window.element).fadeIn();
// Ta-da! It was there all along!
$window.closed = false;
}, 1000);
});
}
// Handle toggle buttons
$("button.toggle").on("click", (e) => {
$(e.target).toggleClass("selected");
$(e.target).attr("aria-pressed", $(e.target).hasClass("selected") ? "true" : "false");
});
// Load themes on drag and drop (.theme/.themepack files)
/**
* @param {File} file
*/
async function loadThemeFile(file) {
const fileText = await file.text();
const cssProperties = parseThemeFileString(fileText);
if (cssProperties) {
applyCSSProperties(cssProperties, { recurseIntoIframes: true });
console.log(makeThemeCSSFile(cssProperties));
}
}
$("html").on("dragover", (event) => {
event.preventDefault();
event.stopPropagation();
});
$("html").on("dragleave", (event) => {
event.preventDefault();
event.stopPropagation();
});
$("html").on("drop", (event) => {
event.preventDefault();
event.stopPropagation();
const files = [...event.originalEvent?.dataTransfer?.files ?? []];
for (const file of files) {
if (file.name.match(/\.theme(pack)?$/i)) {
loadThemeFile(file);
}
}
});
// Generate variations of scrollbars
const $scrollbar_buttons = $(".scrollbar-demo");
$scrollbar_buttons.after(
$scrollbar_buttons.clone().css("--scrollbar-size", "15px"),
$scrollbar_buttons.clone().css("--scrollbar-size", "16px"),
$scrollbar_buttons.clone().css("--scrollbar-size", "30px"),
);
$(".scrollbar-demo").each((index, element) => {
applyCSSProperties(renderThemeGraphics(getComputedStyle(element)), { element });
});
});