/
$ToolWindow.js
286 lines (256 loc) · 7.32 KB
/
$ToolWindow.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
$Window.Z_INDEX = 50; // dynamically incrementing z-index
function $ToolWindow($component){
const $w = $(E("div")).addClass("window tool-window").appendTo("body");
$w.$titlebar = $(E("div")).addClass("window-titlebar").appendTo($w);
$w.$title = $(E("span")).addClass("window-title").appendTo($w.$titlebar);
$w.$x = $(E("button")).addClass("window-close-button window-button").appendTo($w.$titlebar);
$w.$content = $(E("div")).addClass("window-content").appendTo($w);
if($component){
$w.addClass("component-window");
}
$w.attr("touch-action", "none");
$w.$x.on("click", () => {
$w.close();
});
$w.$x.on("mousedown selectstart", e => {
e.preventDefault();
});
// @TODO: prevent selection *outside* of the window *via* the window
// @TODO: keep track of last focused control in the window, and focus it when clicking on / focusing the window
$w.css({
position: "absolute",
zIndex: $Window.Z_INDEX++
});
$w.on("pointerdown", () => {
$w.css({
zIndex: $Window.Z_INDEX++
});
});
$w.on("keydown", e => {
if(e.ctrlKey || e.altKey || e.shiftKey || e.metaKey){
return;
}
const $buttons = $w.$content.find("button");
const $focused = $(document.activeElement);
const focused_index = $buttons.index($focused);
switch(e.keyCode){
case 40: // Down
case 39: // Right
if($focused.is("button")){
if(focused_index < $buttons.length - 1){
$buttons[focused_index + 1].focus();
e.preventDefault();
}
}
break;
case 38: // Up
case 37: // Left
if($focused.is("button")){
if(focused_index > 0){
$buttons[focused_index - 1].focus();
e.preventDefault();
}
}
break;
case 32: // Space
case 13: // Enter (doesn't actually work in chrome because the button gets clicked immediately)
if($focused.is("button")){
$focused.addClass("pressed");
const release = () => {
$focused.removeClass("pressed");
$focused.off("focusout", release);
$(window).off("keyup", keyup);
};
const keyup = e => {
if(e.keyCode === 32 || e.keyCode === 13){
release();
}
};
$focused.on("focusout", release);
$(window).on("keyup", keyup);
}
break;
case 9: { // Tab
// @TODO: handle shift+tab as well (note: early return at top of function)
// wrap around when tabbing through controls in a window
// @TODO: other element types? also [tabIndex]
const $controls = $w.$content.find("input, textarea, select, button, a");
const focused_control_index = $controls.index($focused);
if(focused_control_index === $controls.length - 1){
e.preventDefault();
$controls[0].focus();
}
break;
}
case 27: // Esc
$w.close();
break;
}
});
$w.applyBounds = () => {
$w.css({
left: Math.max(0, Math.min(innerWidth - $w.outerWidth(), $w[0].getBoundingClientRect().left)),
top: Math.max(0, Math.min(innerHeight - $w.outerHeight(), $w[0].getBoundingClientRect().top)),
});
};
$w.bringTitleBarOnScreen = () => {
// Try to make the titlebar always accessible
const min_horizontal_pixels_on_screen = 40; // enough for space past a close button
$w.css({
left: Math.max(
min_horizontal_pixels_on_screen - $w.outerWidth(),
Math.min(
innerWidth - min_horizontal_pixels_on_screen,
$w[0].getBoundingClientRect().left
)
),
top: Math.max(0, Math.min(
innerHeight - $w.$titlebar.outerHeight() - 5,
$w[0].getBoundingClientRect().top
)),
});
};
$w.center = () => {
$w.css({
left: (innerWidth - $w.outerWidth()) / 2,
top: (innerHeight - $w.outerHeight()) / 2,
});
$w.applyBounds();
};
$G.on("resize", $w.bringTitleBarOnScreen);
let drag_offset_x, drag_offset_y;
const drag = e => {
$w.css({
left: e.clientX - drag_offset_x,
top: e.clientY - drag_offset_y,
});
};
$w.$titlebar.attr("touch-action", "none");
$w.$titlebar.on("mousedown selectstart", e => {
e.preventDefault();
});
$w.$titlebar.on("pointerdown", e => {
if($(e.target).is("button")){
return;
}
drag_offset_x = e.clientX - $w[0].getBoundingClientRect().left;
drag_offset_y = e.clientY - $w[0].getBoundingClientRect().top;
$G.on("pointermove", drag);
$("body").addClass("dragging");
const stop_drag = ()=> {
// $w.applyBounds(); // Windows doesn't really try to keep windows on screen
// but you also can't really drag off of the desktop, whereas here you can drag to way outside the web page.
$w.bringTitleBarOnScreen();
$G.off("pointermove", drag);
$G.off("pointerup pointercancel", stop_drag);
$("body").removeClass("dragging");
};
$G.on("pointerup pointercancel", stop_drag);
});
$w.$titlebar.on("dblclick", ()=> {
if($component){
$component.dock();
}
});
$w.$Button = (text, handler) => {
const $b = $(E("button"))
.appendTo($w.$content)
.text(text)
.on("click", () => {
if(handler){
handler();
}
$w.close();
});
return $b;
};
$w.title = title => {
if(title){
$w.$title.text(title);
return $w;
}else{
return $w.$title.text();
}
};
$w.close = () => {
const e = $.Event("close");
$w.trigger(e);
if(e.isDefaultPrevented()){
return;
}
if($component){
$component.detach();
}
$w.remove();
$w.closed = true;
};
$w.closed = false;
const scale_for_eye_gaze_mode_and_center = ()=> {
if (!$w.is(".edit-colors-window, .storage-manager, .attributes-window, .flip-and-rotate, .stretch-and-skew")) {
return;
}
const c = $w.$content[0];
const t = $w.$titlebar[0];
let scale = 1;
$w.$content.css({
transform: `scale(${scale})`,
transformOrigin: "0 0",
marginRight: "",
marginBottom: "",
});
if (document.body.classList.contains("eye-gaze-mode")) {
scale = Math.min(
(innerWidth) / c.offsetWidth,
(innerHeight - t.offsetHeight) / c.offsetHeight
);
$w.$content.css({
transform: `scale(${scale})`,
transformOrigin: "0 0",
marginRight: c.scrollWidth * (scale - 1),
});
// This is separate to prevent content going off the bottom of the window
// in case the layout changes due to text wrapping.
$w.$content.css({
marginBottom: c.scrollHeight * (scale - 1),
});
$w.center();
}
// for testing (WARNING: can cause rapid flashing, which can cause seizures):
// requestAnimationFrame(scale_for_eye_gaze_mode_and_center);
};
if(!$component){
$w.center();
const scale_for_eye_gaze_mode_and_center_next_frame = ()=> {
requestAnimationFrame(scale_for_eye_gaze_mode_and_center);
};
const on_close = ()=> {
$w.off("close", on_close);
$G.off("eye-gaze-mode-toggled resize", scale_for_eye_gaze_mode_and_center_next_frame);
};
$w.on("close", on_close);
$G.on("eye-gaze-mode-toggled resize", scale_for_eye_gaze_mode_and_center_next_frame);
scale_for_eye_gaze_mode_and_center_next_frame();
}
return $w;
}
function $FormToolWindow(title){
const $w = new $ToolWindow();
$w.title(title);
$w.$form = $(E("form")).appendTo($w.$content);
$w.$main = $(E("div")).appendTo($w.$form);
$w.$buttons = $(E("div")).appendTo($w.$form).addClass("button-group");
$w.$Button = (label, action) => {
const $b = $(E("button")).appendTo($w.$buttons).text(label);
$b.on("click", e => {
// prevent the form from submitting
// @TODO: instead prevent submit event
e.preventDefault();
action();
});
$b.on("pointerdown", () => {
$b[0].focus();
});
return $b;
};
return $w;
}