xbintsc 0.5.36 → 0.5.37

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 (66) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/driver/version.d.ts +1 -1
  3. package/dist/src/driver/version.js +1 -1
  4. package/dist/src/extensions/gui/index.js +12 -0
  5. package/dist/src/extensions/gui/index.js.map +1 -1
  6. package/dist/tests/e2e/gui-browser.test.js +65 -0
  7. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  8. package/dist/tests/e2e/gui-canvas-args.test.d.ts +6 -0
  9. package/dist/tests/e2e/gui-canvas-args.test.js +217 -0
  10. package/dist/tests/e2e/gui-canvas-args.test.js.map +1 -0
  11. package/dist/tests/e2e/gui-canvas.test.js.map +1 -1
  12. package/dist/tests/e2e/gui-dom.test.js +215 -0
  13. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  14. package/dist/tests/e2e/gui-events.test.js +112 -0
  15. package/dist/tests/e2e/gui-events.test.js.map +1 -1
  16. package/doc/gui/api.md +38 -9
  17. package/doc/gui/canvas.md +49 -18
  18. package/doc/gui/dom.md +51 -4
  19. package/doc/zh-CN/gui/api.md +33 -4
  20. package/doc/zh-CN/gui/canvas.md +44 -19
  21. package/doc/zh-CN/gui/dom.md +45 -3
  22. package/package.json +7 -7
  23. package/runtime/ext_gui/browser.cpp +34 -49
  24. package/runtime/ext_gui/browser_abort.cpp +249 -0
  25. package/runtime/ext_gui/browser_dialogs.cpp +76 -0
  26. package/runtime/ext_gui/browser_media.cpp +176 -0
  27. package/runtime/ext_gui/browser_navigation.cpp +130 -0
  28. package/runtime/ext_gui/canvas.cpp +88 -30
  29. package/runtime/ext_gui/canvas.h +74 -9
  30. package/runtime/ext_gui/canvas_image_data.cpp +143 -0
  31. package/runtime/ext_gui/canvas_internal.h +8 -5
  32. package/runtime/ext_gui/canvas_pattern.cpp +90 -0
  33. package/runtime/ext_gui/canvas_raster.cpp +31 -9
  34. package/runtime/ext_gui/canvas_style.cpp +4 -2
  35. package/runtime/ext_gui/dom.h +4 -0
  36. package/runtime/ext_gui/dom_api.cpp +1 -0
  37. package/runtime/ext_gui/dom_api_canvas.cpp +120 -114
  38. package/runtime/ext_gui/dom_api_canvas_extra.cpp +57 -2
  39. package/runtime/ext_gui/dom_api_canvas_image.cpp +239 -0
  40. package/runtime/ext_gui/dom_api_canvas_paths.cpp +179 -0
  41. package/runtime/ext_gui/dom_api_computed_style.cpp +124 -0
  42. package/runtime/ext_gui/dom_api_dataset.cpp +154 -0
  43. package/runtime/ext_gui/dom_api_document.cpp +58 -5
  44. package/runtime/ext_gui/dom_api_events.cpp +93 -17
  45. package/runtime/ext_gui/dom_api_internal.h +55 -2
  46. package/runtime/ext_gui/dom_api_mutation.cpp +159 -0
  47. package/runtime/ext_gui/dom_api_node.cpp +40 -19
  48. package/runtime/ext_gui/dom_api_node_read.cpp +37 -1
  49. package/runtime/ext_gui/dom_api_path2d.cpp +358 -0
  50. package/runtime/ext_gui/dom_api_proto.cpp +35 -0
  51. package/runtime/ext_gui/dom_api_style.cpp +223 -13
  52. package/runtime/ext_gui/gui.cpp +59 -5
  53. package/runtime/ext_gui/gui_engine.h +47 -0
  54. package/runtime/ext_gui/gui_events.cpp +80 -9
  55. package/runtime/ext_gui/listener_options.h +43 -0
  56. package/runtime/ext_gui/paint.cpp +28 -0
  57. package/runtime/ext_gui/paint.h +4 -0
  58. package/runtime/ext_gui/window.cpp +14 -65
  59. package/runtime/ext_gui/window_events.cpp +125 -0
  60. package/runtime/rt.h +26 -0
  61. package/runtime/rt_internal.h +4 -0
  62. package/runtime/xt_builtins.c +0 -21
  63. package/runtime/xt_containers/access.inc +94 -2
  64. package/runtime/xt_containers/objects.inc +29 -31
  65. package/src/driver/version.ts +1 -1
  66. package/src/extensions/gui/index.ts +12 -0
@@ -51,17 +51,18 @@ static void write_declarations(xtgui::Node *node, const std::vector<xtgui::Decla
51
51
  }
52
52
 
53
53
  static void set_style_property(XtGuiWindow *win, xtgui::Node *node, const std::string &name,
54
- const std::string &value) {
54
+ const std::string &value, bool important) {
55
55
  std::vector<xtgui::Declaration> decls = xtgui::xt_css_parse_declarations(inline_style_of(node));
56
56
  bool found = false;
57
57
  for (xtgui::Declaration &decl : decls) {
58
58
  if (decl.property == name) {
59
59
  decl.value = value;
60
- decl.important = false;
60
+ decl.important = important;
61
61
  found = true;
62
+ break;
62
63
  }
63
64
  }
64
- if (!found) decls.push_back({name, value, false});
65
+ if (!found) decls.push_back({name, value, important});
65
66
  write_declarations(node, decls);
66
67
  mark_dirty(win);
67
68
  }
@@ -75,6 +76,26 @@ static std::string get_style_property(const xtgui::Node *node, const std::string
75
76
  return value;
76
77
  }
77
78
 
79
+ static std::string get_style_priority(const xtgui::Node *node, const std::string &name) {
80
+ std::vector<xtgui::Declaration> decls = xtgui::xt_css_parse_declarations(inline_style_of(node));
81
+ for (const xtgui::Declaration &decl : decls) {
82
+ if (decl.property == name) return decl.important ? "important" : "";
83
+ }
84
+ return "";
85
+ }
86
+
87
+ static std::string serialize_declarations(const xtgui::Node *node) {
88
+ std::vector<xtgui::Declaration> decls = xtgui::xt_css_parse_declarations(inline_style_of(node));
89
+ std::string out;
90
+ for (const xtgui::Declaration &decl : decls) {
91
+ if (!out.empty()) out += " ";
92
+ out += decl.property + ": " + decl.value;
93
+ if (decl.important) out += " !important";
94
+ out += ";";
95
+ }
96
+ return out;
97
+ }
98
+
78
99
  static void remove_style_property(XtGuiWindow *win, xtgui::Node *node, const std::string &name) {
79
100
  std::vector<xtgui::Declaration> decls = xtgui::xt_css_parse_declarations(inline_style_of(node));
80
101
  std::vector<xtgui::Declaration> kept;
@@ -133,17 +154,36 @@ METHOD(class_list_toggle) {
133
154
  xtgui::Node *node = class_list_node(self, &win);
134
155
  if (node == nullptr || argc < 1) return XT_FALSE;
135
156
  std::string name = to_string_value(xt_arg(argc, argv, 0));
157
+ if (name.empty()) return XT_FALSE;
136
158
  std::vector<std::string> names = split_classes(class_name_of(node));
137
159
  auto it = std::find(names.begin(), names.end(), name);
138
- if (it != names.end()) {
139
- names.erase(it);
140
- set_class_name(win, node, join_classes(names));
160
+ bool present = it != names.end();
161
+ /* `toggle(name, force)`: with `force` the call is idempotent — it adds when
162
+ * the second argument is truthy and removes when it is falsy, instead of
163
+ * flipping. The argument is optional, so `undefined` still means "flip". */
164
+ bool forceProvided = argc >= 2 && !XT_IS_UNDEFINED(xt_arg(argc, argv, 1));
165
+ bool force = forceProvided && value_truthy(xt_arg(argc, argv, 1));
166
+ if (forceProvided) {
167
+ if (force) {
168
+ if (!present) {
169
+ names.push_back(name);
170
+ set_class_name(win, node, join_classes(names));
171
+ }
172
+ return XT_TRUE;
173
+ }
174
+ if (present) {
175
+ names.erase(it);
176
+ set_class_name(win, node, join_classes(names));
177
+ }
141
178
  return XT_FALSE;
142
179
  }
143
- if (!name.empty()) {
144
- names.push_back(name);
180
+ if (present) {
181
+ names.erase(it);
145
182
  set_class_name(win, node, join_classes(names));
183
+ return XT_FALSE;
146
184
  }
185
+ names.push_back(name);
186
+ set_class_name(win, node, join_classes(names));
147
187
  return XT_TRUE;
148
188
  }
149
189
 
@@ -165,8 +205,19 @@ METHOD(style_set_property) {
165
205
  XtGuiWindow *win = nullptr;
166
206
  xtgui::Node *node = style_node(self, &win);
167
207
  if (node == nullptr || argc < 2) return XT_UNDEFINED;
168
- set_style_property(win, node, to_string_value(xt_arg(argc, argv, 0)),
169
- to_string_value(xt_arg(argc, argv, 1)));
208
+ std::string name = xtgui::xt_css_lower(xtgui::xt_css_trim(to_string_value(xt_arg(argc, argv, 0))));
209
+ if (name.empty()) return XT_UNDEFINED;
210
+ /* `priority` is "" or "important"; any other value makes the whole call a
211
+ * no-op, exactly as in the CSSOM. */
212
+ std::string priority;
213
+ if (argc >= 3) priority = xtgui::xt_css_lower(xtgui::xt_css_trim(to_string_value(xt_arg(argc, argv, 2))));
214
+ if (!priority.empty() && priority != "important") return XT_UNDEFINED;
215
+ std::string value = to_string_value(xt_arg(argc, argv, 1));
216
+ if (xtgui::xt_css_trim(value).empty()) {
217
+ remove_style_property(win, node, name);
218
+ return XT_UNDEFINED;
219
+ }
220
+ set_style_property(win, node, name, value, priority == "important");
170
221
  return XT_UNDEFINED;
171
222
  }
172
223
 
@@ -175,18 +226,177 @@ METHOD(style_get_property_value) {
175
226
  xtgui::Node *node = style_node(self, nullptr);
176
227
  if (node == nullptr || argc < 1) return xt_string_from_cstr("");
177
228
  return xt_string_from_cstr(
178
- get_style_property(node, to_string_value(xt_arg(argc, argv, 0))).c_str());
229
+ get_style_property(node, xtgui::xt_css_lower(xtgui::xt_css_trim(to_string_value(xt_arg(argc, argv, 0)))))
230
+ .c_str());
231
+ }
232
+
233
+ METHOD(style_get_property_priority) {
234
+ (void)env;
235
+ xtgui::Node *node = style_node(self, nullptr);
236
+ if (node == nullptr || argc < 1) return xt_string_from_cstr("");
237
+ return xt_string_from_cstr(
238
+ get_style_priority(node, xtgui::xt_css_lower(xtgui::xt_css_trim(to_string_value(xt_arg(argc, argv, 0)))))
239
+ .c_str());
179
240
  }
180
241
 
181
242
  METHOD(style_remove_property) {
182
243
  (void)env;
183
244
  XtGuiWindow *win = nullptr;
184
245
  xtgui::Node *node = style_node(self, &win);
185
- if (node == nullptr || argc < 1) return XT_UNDEFINED;
186
- remove_style_property(win, node, to_string_value(xt_arg(argc, argv, 0)));
246
+ if (node == nullptr || argc < 1) return xt_string_from_cstr("");
247
+ std::string name = xtgui::xt_css_lower(xtgui::xt_css_trim(to_string_value(xt_arg(argc, argv, 0))));
248
+ std::string previous = get_style_property(node, name);
249
+ remove_style_property(win, node, name);
250
+ return xt_string_from_cstr(previous.c_str());
251
+ }
252
+
253
+ METHOD(style_css_text_get) {
254
+ (void)env;
255
+ (void)argc;
256
+ (void)argv;
257
+ xtgui::Node *node = style_node(self, nullptr);
258
+ if (node == nullptr) return xt_string_from_cstr("");
259
+ return xt_string_from_cstr(serialize_declarations(node).c_str());
260
+ }
261
+
262
+ METHOD(style_css_text_set) {
263
+ (void)env;
264
+ XtGuiWindow *win = nullptr;
265
+ xtgui::Node *node = style_node(self, &win);
266
+ if (node == nullptr) return XT_UNDEFINED;
267
+ std::vector<xtgui::Declaration> decls =
268
+ xtgui::xt_css_parse_declarations(to_string_value(xt_arg(argc, argv, 0)));
269
+ write_declarations(node, decls);
270
+ mark_dirty(win);
187
271
  return XT_UNDEFINED;
188
272
  }
189
273
 
274
+ /* -- named properties (`style.width = "50%"`) -----------------------------
275
+ *
276
+ * A CSSStyleDeclaration exposes every supported CSS property as a camel-cased
277
+ * property (`style.width`, `style.backgroundColor`) as well as through
278
+ * `setProperty`. There are hundreds of them, so instead of enumerating
279
+ * accessors the style object installs the runtime's dynamic-property hooks:
280
+ * a miss on the prototype is answered from the inline declaration block, and
281
+ * an assignment writes it back there. That is what makes the common
282
+ * `el.style.display = "none"` idiom affect layout instead of silently parking
283
+ * a field on the JS object.
284
+ */
285
+
286
+ namespace {
287
+
288
+ bool property_key(xt_value key, std::string *out) {
289
+ if (!XT_IS_STRING(key)) return false;
290
+ /* Named style properties are camel-cased (`style.backgroundColor`); the
291
+ * declaration block stores the CSS spelling (`background-color`). Custom
292
+ * properties keep their leading `--`, and internal bookkeeping keys stay
293
+ * out of the view (looking one up must not re-enter it). */
294
+ std::string raw = to_string_value(key);
295
+ if (raw.compare(0, 6, "__xt_") == 0) return false;
296
+ std::string mapped;
297
+ for (char c : raw) {
298
+ if (c >= 'A' && c <= 'Z') {
299
+ mapped.push_back('-');
300
+ mapped.push_back((char)std::tolower((unsigned char)c));
301
+ } else {
302
+ mapped.push_back(c);
303
+ }
304
+ }
305
+ *out = xtgui::xt_css_lower(xtgui::xt_css_trim(mapped));
306
+ return !out->empty();
307
+ }
308
+
309
+ xt_value style_view_get(xt_value self, xt_value key, void *data) {
310
+ (void)data;
311
+ std::string name;
312
+ if (!property_key(key, &name)) return XT_UNDEFINED;
313
+ xtgui::Node *node = style_node(self, nullptr);
314
+ if (node == nullptr) return xt_string_from_cstr("");
315
+ return xt_string_from_cstr(get_style_property(node, name).c_str());
316
+ }
317
+
318
+ int style_view_set(xt_value self, xt_value key, xt_value value, void *data) {
319
+ (void)data;
320
+ std::string name;
321
+ if (!property_key(key, &name)) return 0;
322
+ XtGuiWindow *win = nullptr;
323
+ xtgui::Node *node = style_node(self, &win);
324
+ if (node == nullptr) return 1;
325
+ std::string text = to_string_value(value);
326
+ if (text.empty()) {
327
+ remove_style_property(win, node, name);
328
+ } else {
329
+ set_style_property(win, node, name, text, false);
330
+ }
331
+ return 1;
332
+ }
333
+
334
+ int style_view_has(xt_value self, xt_value key, void *data) {
335
+ (void)data;
336
+ std::string name;
337
+ if (!property_key(key, &name)) return 0;
338
+ xtgui::Node *node = style_node(self, nullptr);
339
+ if (node == nullptr) return 0;
340
+ std::vector<xtgui::Declaration> decls = xtgui::xt_css_parse_declarations(inline_style_of(node));
341
+ for (const xtgui::Declaration &decl : decls) {
342
+ if (decl.property == name) return 1;
343
+ }
344
+ return 0;
345
+ }
346
+
347
+ int style_view_remove(xt_value self, xt_value key, void *data) {
348
+ (void)data;
349
+ std::string name;
350
+ if (!property_key(key, &name)) return 0;
351
+ XtGuiWindow *win = nullptr;
352
+ xtgui::Node *node = style_node(self, &win);
353
+ if (node == nullptr) return 1;
354
+ remove_style_property(win, node, name);
355
+ return 1;
356
+ }
357
+
358
+ xt_value style_view_keys(xt_value self, void *data) {
359
+ (void)data;
360
+ xtgui::Node *node = style_node(self, nullptr);
361
+ xt_value result = xt_array_new(0, nullptr);
362
+ if (node == nullptr) return result;
363
+ std::vector<xtgui::Declaration> decls = xtgui::xt_css_parse_declarations(inline_style_of(node));
364
+ for (const xtgui::Declaration &decl : decls) {
365
+ xt_array_push(result, xt_string_from_cstr(decl.property.c_str()));
366
+ }
367
+ return result;
368
+ }
369
+
370
+ const xt_object_hooks kStyleViewHooks = {style_view_get, style_view_set, style_view_has,
371
+ style_view_remove, style_view_keys};
372
+
373
+ } // namespace
374
+
375
+ void install_style_hooks(xt_value style) {
376
+ xt_object_set_hooks(style, &kStyleViewHooks, nullptr);
377
+ }
378
+
379
+ METHOD(style_length_get) {
380
+ (void)env;
381
+ (void)argc;
382
+ (void)argv;
383
+ xtgui::Node *node = style_node(self, nullptr);
384
+ if (node == nullptr) return xt_number(0);
385
+ return xt_number(
386
+ (double)xtgui::xt_css_parse_declarations(inline_style_of(node)).size());
387
+ }
388
+
389
+ /** `style.item(index)`: the property name at `index`, or "" past the end. */
390
+ METHOD(style_item) {
391
+ (void)env;
392
+ xtgui::Node *node = style_node(self, nullptr);
393
+ if (node == nullptr || argc < 1) return xt_string_from_cstr("");
394
+ std::vector<xtgui::Declaration> decls = xtgui::xt_css_parse_declarations(inline_style_of(node));
395
+ double index = xt_to_number(xt_arg(argc, argv, 0));
396
+ if (index < 0 || index >= (double)decls.size()) return xt_string_from_cstr("");
397
+ return xt_string_from_cstr(decls[(size_t)index].property.c_str());
398
+ }
399
+
190
400
  #undef METHOD
191
401
 
192
402
  } // namespace domapi
@@ -90,10 +90,18 @@ static void xt_gui_gc_scan_roots(void) {
90
90
  xt_gc_mark_value(win.document_object);
91
91
  for (const auto &entry : win.node_handles) xt_gc_mark_value(entry.second);
92
92
  for (const auto &entry : win.node_listeners) {
93
- for (const XtGuiNodeListener &listener : entry.second) xt_gc_mark_value(listener.fn);
93
+ for (const XtGuiNodeListener &listener : entry.second) {
94
+ xt_gc_mark_value(listener.fn);
95
+ xt_gc_mark_value(listener.signal);
96
+ }
97
+ }
98
+ for (const XtGuiWindowListener &listener : win.window_listeners) {
99
+ xt_gc_mark_value(listener.fn);
100
+ xt_gc_mark_value(listener.signal);
94
101
  }
95
102
  for (const XtGuiAnimationFrame &frame : win.animation_frames) xt_gc_mark_value(frame.fn);
96
103
  for (const XtGuiAnimationFrame &frame : win.running_frames) xt_gc_mark_value(frame.fn);
104
+ for (const xt_value list : win.media_lists) xt_gc_mark_value(list);
97
105
  }
98
106
  }
99
107
 
@@ -177,6 +185,9 @@ void xt_gui_quit_window(XtGuiWindow *win) {
177
185
  win->open = 0;
178
186
  xt_gui_emit(win, "close");
179
187
  win->animation_frames.clear();
188
+ win->media_lists.clear();
189
+ win->media_lists_by_query.clear();
190
+ win->window_listeners.clear();
180
191
  xt_gui_media_shutdown(win);
181
192
  xt_gui_canvas_shutdown(win);
182
193
  xt_gui_handles_reset(win);
@@ -218,12 +229,12 @@ static int xt_gui_int_or(xt_value value, int fallback) {
218
229
  /* Build (and record) one window. Shared by `createWindow` and the implicit
219
230
  * window a page written against `window`/`document` gets on first access. */
220
231
  static XtGuiWindow *create_window_record(const char *title, int width, int height,
221
- bool high_density) {
232
+ bool high_density, SDL_WindowFlags extra_flags) {
222
233
  if (g_window_count >= XT_GUI_MAX_WINDOWS) {
223
234
  fprintf(stderr, "xt_gui: too many windows (max %d)\n", XT_GUI_MAX_WINDOWS);
224
235
  return NULL;
225
236
  }
226
- SDL_WindowFlags flags = SDL_WINDOW_RESIZABLE;
237
+ SDL_WindowFlags flags = extra_flags;
227
238
  if (high_density) flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
228
239
  SDL_Window *window = SDL_CreateWindow(title, width, height, flags);
229
240
  if (window == NULL) {
@@ -253,6 +264,11 @@ static XtGuiWindow *create_window_record(const char *title, int width, int heigh
253
264
  record->animation_frames.clear();
254
265
  record->media_players.clear();
255
266
  record->next_animation_frame_id = 1;
267
+ record->scroll_x = 0.0f;
268
+ record->scroll_y = 0.0f;
269
+ record->focused = 0;
270
+ record->media_lists.clear();
271
+ record->media_lists_by_query.clear();
256
272
  record->background[0] = 0.08f;
257
273
  record->background[1] = 0.09f;
258
274
  record->background[2] = 0.11f;
@@ -283,7 +299,8 @@ xt_value xt_gui_ensure_window(void) {
283
299
  if (g_windows[i].open) return g_windows[i].object;
284
300
  }
285
301
  ensure_init();
286
- XtGuiWindow *record = create_window_record("xbintsc gui", 800, 600, true);
302
+ XtGuiWindow *record =
303
+ create_window_record("xbintsc gui", 800, 600, true, SDL_WINDOW_RESIZABLE);
287
304
  if (record == NULL) {
288
305
  throw_gui_error("xbintsc gui: could not open the implicit window (see the diagnostic above)");
289
306
  return XT_UNDEFINED;
@@ -299,6 +316,25 @@ extern "C" xt_value xt_gui_create_window(int32_t argc, xt_value *argv) {
299
316
  int width = 800;
300
317
  int height = 600;
301
318
  bool high_density = true;
319
+ SDL_WindowFlags flags = SDL_WINDOW_RESIZABLE;
320
+ bool has_x = false;
321
+ bool has_y = false;
322
+ bool has_opacity = false;
323
+ int x = 0;
324
+ int y = 0;
325
+ float opacity = 1.0f;
326
+ bool maximized = false;
327
+ bool minimized = false;
328
+ auto option_bool = [](xt_value options, const char *name, bool fallback) {
329
+ xt_value value = xt_object_get_cstr(options, name);
330
+ return XT_IS_BOOL(value) ? (xt_truthy(value) != 0) : fallback;
331
+ };
332
+ auto option_number = [](xt_value options, const char *name, bool *present, double fallback) {
333
+ xt_value value = xt_object_get_cstr(options, name);
334
+ if (!XT_IS_NUMBER(value)) return fallback;
335
+ *present = true;
336
+ return xt_to_number(value);
337
+ };
302
338
  xt_value options = xt_arg(argc, argv, 0);
303
339
  if (XT_IS_OBJECT(options)) {
304
340
  title = xt_gui_string_or(xt_object_get_cstr(options, "title"), title);
@@ -306,6 +342,19 @@ extern "C" xt_value xt_gui_create_window(int32_t argc, xt_value *argv) {
306
342
  height = xt_gui_int_or(xt_object_get_cstr(options, "height"), height);
307
343
  xt_value density = xt_object_get_cstr(options, "highDensity");
308
344
  if (!XT_IS_UNDEFINED(density)) high_density = xt_truthy(density);
345
+ /* Window flags and placement: the rest of the options object used to be
346
+ * ignored, which made `{ resizable: false }` or `{ borderless: true }`
347
+ * silently open a plain window. */
348
+ if (!option_bool(options, "resizable", true)) flags &= ~SDL_WINDOW_RESIZABLE;
349
+ if (option_bool(options, "borderless", false)) flags |= SDL_WINDOW_BORDERLESS;
350
+ if (option_bool(options, "alwaysOnTop", false)) flags |= SDL_WINDOW_ALWAYS_ON_TOP;
351
+ if (option_bool(options, "hidden", false)) flags |= SDL_WINDOW_HIDDEN;
352
+ if (option_bool(options, "fullscreen", false)) flags |= SDL_WINDOW_FULLSCREEN;
353
+ x = (int)option_number(options, "x", &has_x, 0);
354
+ y = (int)option_number(options, "y", &has_y, 0);
355
+ opacity = (float)option_number(options, "opacity", &has_opacity, 1.0);
356
+ maximized = option_bool(options, "maximized", false);
357
+ minimized = option_bool(options, "minimized", false);
309
358
  } else if (XT_IS_STRING(options)) {
310
359
  title = xt_string_data(options);
311
360
  width = xt_gui_int_or(xt_arg(argc, argv, 1), width);
@@ -334,11 +383,16 @@ extern "C" xt_value xt_gui_create_window(int32_t argc, xt_value *argv) {
334
383
  return record->object;
335
384
  }
336
385
 
337
- XtGuiWindow *record = create_window_record(title, width, height, high_density);
386
+ XtGuiWindow *record = create_window_record(title, width, height, high_density, flags);
338
387
  if (record == NULL) {
339
388
  throw_gui_error("xbintsc gui: could not open a window (see the diagnostic above)");
340
389
  return XT_UNDEFINED;
341
390
  }
391
+ if (has_x || has_y) SDL_SetWindowPosition(record->window, has_x ? x : SDL_WINDOWPOS_CENTERED,
392
+ has_y ? y : SDL_WINDOWPOS_CENTERED);
393
+ if (has_opacity) SDL_SetWindowOpacity(record->window, opacity);
394
+ if (maximized) SDL_MaximizeWindow(record->window);
395
+ if (minimized) SDL_MinimizeWindow(record->window);
342
396
  return record->object;
343
397
  }
344
398
 
@@ -55,6 +55,8 @@ xt_value xt_browser_computed_style(int32_t argc, xt_value *argv);
55
55
  xt_value xt_browser_escape(int32_t argc, xt_value *argv);
56
56
  xt_value xt_browser_alert(int32_t argc, xt_value *argv);
57
57
  xt_value xt_browser_prompt(int32_t argc, xt_value *argv);
58
+ /** The bare `confirm(message)` global (the window method shares the engine). */
59
+ xt_value xt_browser_confirm_global(int32_t argc, xt_value *argv);
58
60
  /** Install `innerWidth`, `setTimeout`, `navigator`, … on the window prototype. */
59
61
  void xt_browser_install_window(xt_value proto);
60
62
  /** Poll and dispatch pending SDL input events once (the browser frame pump in
@@ -97,6 +99,22 @@ struct XtGuiNodeListener {
97
99
  xt_value fn;
98
100
  bool once;
99
101
  bool capture;
102
+ /** `{ passive: true }`: `preventDefault()` is ignored while it runs. */
103
+ bool passive;
104
+ /** `{ signal }`: the listener is skipped once the signal is aborted. */
105
+ xt_value signal;
106
+ };
107
+
108
+ /** One window-level listener (`window.addEventListener`). Window delivery has
109
+ * no capture phase, but `capture` is still part of listener identity, as in the
110
+ * DOM; `once`, `passive` and `signal` behave like the element ones. */
111
+ struct XtGuiWindowListener {
112
+ std::string type;
113
+ xt_value fn;
114
+ bool once;
115
+ bool capture;
116
+ bool passive;
117
+ xt_value signal;
100
118
  };
101
119
 
102
120
  /** One pending `requestAnimationFrame` callback. */
@@ -142,6 +160,14 @@ struct XtGuiWindow {
142
160
  float background[4];
143
161
  int width;
144
162
  int height;
163
+ /** Page scroll offset (CSS pixels); painting and hit testing subtract it. */
164
+ float scroll_x = 0;
165
+ float scroll_y = 0;
166
+ /** `document.hasFocus()` state, toggled by `window.focus()` / `blur()`. */
167
+ int focused = 0;
168
+ /** Live `matchMedia` lists for this window (one per query string). */
169
+ std::vector<xt_value> media_lists;
170
+ std::unordered_map<std::string, xt_value> media_lists_by_query;
145
171
 
146
172
  /* -- DOM object model (element handles + events) ----------------------- */
147
173
  /** `document` handle for this window (lazily created). */
@@ -156,6 +182,8 @@ struct XtGuiWindow {
156
182
  std::unordered_map<const xtgui::Node *, xt_value> node_handles;
157
183
  /** Element-level listeners, keyed by node. */
158
184
  std::unordered_map<const xtgui::Node *, std::vector<XtGuiNodeListener>> node_listeners;
185
+ /** Window-level listeners registered through `window.addEventListener`. */
186
+ std::vector<XtGuiWindowListener> window_listeners;
159
187
  /** Set by a DOM mutation; consumed by the frame loop before repainting. */
160
188
  int struct_dirty = 0;
161
189
  /** Callbacks queued for the next frame, plus the id counter. */
@@ -213,6 +241,9 @@ void xt_gui_run_scripts(XtGuiWindow *win);
213
241
  void xt_gui_script_gc_scan(void);
214
242
  /** Shared prototype carrying the window methods. */
215
243
  xt_value xt_gui_window_proto(void);
244
+ /** Install `on` / `off` / `addEventListener` / `removeEventListener` (the
245
+ * listener options bag is parsed in `window_events.cpp`). */
246
+ void xt_gui_install_window_events(xt_value proto);
216
247
  /** Release GPU claim + destroy the window and mark the record closed. */
217
248
  void xt_gui_quit_window(XtGuiWindow *win);
218
249
  /** Deliver `event` (no arguments) to handlers registered with `on`. */
@@ -226,6 +257,22 @@ void xt_gui_render_window(XtGuiWindow *win);
226
257
  void xt_gui_run_animation_frames(XtGuiWindow *win, double timestamp_ms);
227
258
  /** Current logical window size (CSS viewport), in pixels. */
228
259
  void xt_gui_window_viewport(XtGuiWindow *win, float *width, float *height);
260
+ /** Scroll the page to `(x, y)` (clamped to the content bounds), repaint and
261
+ * notify live `matchMedia` lists. */
262
+ void xt_gui_scroll_to(XtGuiWindow *win, float x, float y);
263
+ /** The largest valid scroll offset for the current layout. */
264
+ void xt_gui_scroll_bounds(XtGuiWindow *win, float *max_x, float *max_y);
265
+ /** Re-evaluate every live `matchMedia` list (window resize / scroll). */
266
+ void xt_gui_media_lists_notify(XtGuiWindow *win);
267
+ /** `window.matchMedia(query)`: the cached live list for this window. */
268
+ xt_value xt_browser_media_list(xt_value window, const std::string &query);
269
+ /** `window.confirm` / `window.prompt` (native dialog only when
270
+ * `XT_GUI_DIALOGS=1`; otherwise the message goes to stderr and the documented
271
+ * default is returned). */
272
+ xt_value xt_browser_confirm(XtGuiWindow *win, int32_t argc, xt_value *argv);
273
+ xt_value xt_browser_prompt(XtGuiWindow *win, int32_t argc, xt_value *argv);
274
+ /** Install `scrollTo`/`scrollBy`/`scrollX`/`scrollY`/`focus`/`blur`. */
275
+ void xt_browser_install_window_navigation(xt_value proto);
229
276
  /** CSS-like descriptor of the deepest element at (x, y), or `""` (test/debug). */
230
277
  std::string xt_gui_hit_test(XtGuiWindow *win, float x, float y);
231
278
  /** Synthesise a pointer event (`mousemove`/`mousedown`/`mouseup`/`click`). */
@@ -4,9 +4,11 @@
4
4
  */
5
5
 
6
6
  #include "gui_internal.h"
7
+ #include "listener_options.h"
7
8
 
8
9
  #include "paint.h"
9
10
 
11
+ #include <algorithm>
10
12
  #include <stdio.h>
11
13
  #include <string.h>
12
14
 
@@ -20,22 +22,39 @@ void xt_gui_emit(XtGuiWindow *win, const char *event) {
20
22
  xt_gui_emit_payload(win, event, XT_UNDEFINED);
21
23
  }
22
24
 
25
+ /** Drop one window listener (by identity: type + fn + capture). */
26
+ static void remove_window_listener(XtGuiWindow *win, const XtGuiWindowListener &listener) {
27
+ std::vector<XtGuiWindowListener> &listeners = win->window_listeners;
28
+ for (auto it = listeners.begin(); it != listeners.end(); ++it) {
29
+ if (it->type == listener.type && it->capture == listener.capture &&
30
+ xt_truthy(xt_seq(it->fn, listener.fn))) {
31
+ listeners.erase(it);
32
+ return;
33
+ }
34
+ }
35
+ }
36
+
23
37
  void xt_gui_emit_payload(XtGuiWindow *win, const char *event, xt_value payload) {
24
38
  if (win == NULL || !XT_IS_OBJECT(win->object)) return;
25
- xt_value listeners = xt_object_get_cstr(win->object, "__xt_gui_events");
26
- if (!XT_IS_OBJECT(listeners)) return;
27
- xt_value array = xt_object_get(listeners, xt_string_from_cstr(event));
28
- if (!XT_IS_ARRAY(array)) return;
29
39
  xt_value args[1];
30
40
  int32_t count = 0;
31
41
  if (!XT_IS_UNDEFINED(payload)) {
32
42
  args[0] = payload;
33
43
  count = 1;
34
44
  }
35
- int32_t listener_count = (int32_t)xt_to_number(xt_array_length(array));
36
- for (int32_t i = 0; i < listener_count; i++) {
37
- xt_value fn = xt_array_get(array, xt_number((double)i));
38
- if (XT_IS_FUNCTION(fn)) xt_call_with_this(fn, win->object, count, args);
45
+ /* Snapshot the list: a handler may register or remove listeners while it
46
+ * runs, and the DOM runs the batch as it stood when dispatch started. */
47
+ std::vector<XtGuiWindowListener> snapshot = win->window_listeners;
48
+ for (const XtGuiWindowListener &listener : snapshot) {
49
+ if (listener.type != event) continue;
50
+ /* `{ signal }` removes the listener at abort time (checked here, so no
51
+ * callback plumbing is needed inside the signal). */
52
+ if (xt_gui_signal_aborted(listener.signal)) {
53
+ remove_window_listener(win, listener);
54
+ continue;
55
+ }
56
+ if (listener.once) remove_window_listener(win, listener);
57
+ if (XT_IS_FUNCTION(listener.fn)) xt_call_with_this(listener.fn, win->object, count, args);
39
58
  }
40
59
  }
41
60
 
@@ -52,6 +71,53 @@ void xt_gui_flush_dom(XtGuiWindow *win) {
52
71
  win->geometry.dirty = 1;
53
72
  }
54
73
 
74
+ /* -- page scroll ---------------------------------------------------------- */
75
+
76
+ namespace {
77
+
78
+ void content_bounds(const xtgui::LayoutBox *box, float *max_x, float *max_y) {
79
+ if (box == nullptr) return;
80
+ float right = box->x + box->width;
81
+ float bottom = box->y + box->height;
82
+ if (right > *max_x) *max_x = right;
83
+ if (bottom > *max_y) *max_y = bottom;
84
+ for (const std::unique_ptr<xtgui::LayoutBox> &child : box->children) {
85
+ content_bounds(child.get(), max_x, max_y);
86
+ }
87
+ }
88
+
89
+ } // namespace
90
+
91
+ void xt_gui_scroll_bounds(XtGuiWindow *win, float *max_x, float *max_y) {
92
+ float content_x = 0;
93
+ float content_y = 0;
94
+ if (win != nullptr && win->document != nullptr) {
95
+ xt_gui_flush_dom(win);
96
+ content_bounds(win->document->layout().root(), &content_x, &content_y);
97
+ }
98
+ float viewport_width = 0;
99
+ float viewport_height = 0;
100
+ if (win != nullptr) xt_gui_window_viewport(win, &viewport_width, &viewport_height);
101
+ *max_x = content_x > viewport_width ? content_x - viewport_width : 0.0f;
102
+ *max_y = content_y > viewport_height ? content_y - viewport_height : 0.0f;
103
+ }
104
+
105
+ void xt_gui_scroll_to(XtGuiWindow *win, float x, float y) {
106
+ if (win == nullptr) return;
107
+ float max_x = 0;
108
+ float max_y = 0;
109
+ xt_gui_scroll_bounds(win, &max_x, &max_y);
110
+ if (x < 0.0f) x = 0.0f;
111
+ if (y < 0.0f) y = 0.0f;
112
+ if (x > max_x) x = max_x;
113
+ if (y > max_y) y = max_y;
114
+ if (x == win->scroll_x && y == win->scroll_y) return;
115
+ win->scroll_x = x;
116
+ win->scroll_y = y;
117
+ win->geometry.dirty = 1;
118
+ xt_gui_emit(win, "scroll");
119
+ }
120
+
55
121
  static XtGuiWindow *xt_gui_find_by_window_id(SDL_WindowID id) {
56
122
  SDL_Window *window = SDL_GetWindowFromID(id);
57
123
  if (window == NULL) return NULL;
@@ -66,7 +132,8 @@ static XtGuiWindow *xt_gui_find_by_window_id(SDL_WindowID id) {
66
132
  /** Deepest element under (x, y), or NULL. */
67
133
  static const xtgui::Node *xt_gui_hit_node(XtGuiWindow *win, float x, float y) {
68
134
  if (win->document == nullptr) return nullptr;
69
- const xtgui::LayoutBox *box = win->document->layout().hitTest(x, y);
135
+ const xtgui::LayoutBox *box =
136
+ win->document->layout().hitTest(x + win->scroll_x, y + win->scroll_y);
70
137
  const xtgui::Node *node = box != nullptr ? box->node : nullptr;
71
138
  while (node != nullptr && !node->isElement()) node = node->parent;
72
139
  return node;
@@ -122,6 +189,8 @@ void xt_gui_dispatch_wheel(XtGuiWindow *win, float x, float y, float delta_x, fl
122
189
  const xtgui::Node *node = xt_gui_hit_node(win, x, y);
123
190
  if (node != nullptr) xt_gui_emit_dom_event(win, "wheel", node, payload);
124
191
  xt_gui_emit_payload(win, "wheel", payload);
192
+ /* Default action: scroll the page (content that fits does not move). */
193
+ xt_gui_scroll_to(win, win->scroll_x + delta_x, win->scroll_y + delta_y);
125
194
  }
126
195
 
127
196
  static xt_value xt_gui_key_payload(const char *type, const char *key, const char *code, bool repeat,
@@ -168,6 +237,7 @@ void handle_event(const SDL_Event *event) {
168
237
  /* Web 'resize', so a document can re-measure on a window change in every
169
238
  * host (a browser already fires one). */
170
239
  xt_gui_emit(win, "resize");
240
+ xt_gui_media_lists_notify(win);
171
241
  return;
172
242
  }
173
243
 
@@ -273,6 +343,7 @@ void xt_gui_render_window(XtGuiWindow *win) {
273
343
  if (can_paint && win->geometry.dirty && win->document != nullptr) {
274
344
  xtgui::DisplayList list;
275
345
  xtgui::xt_paint_build(win->document->layout().root(), list, viewport_width, viewport_height);
346
+ xt_paint_translate(list, -win->scroll_x, -win->scroll_y);
276
347
  xt_gui_geometry_upload(g_device, &win->geometry, list);
277
348
  float density = SDL_GetWindowPixelDensity(win->window);
278
349
  float text_scale = density > 0.0f ? density : 1.0f;