xbintsc 0.6.10 → 0.6.12

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 (118) hide show
  1. package/dist/src/checker/globals.js +2 -1
  2. package/dist/src/checker/globals.js.map +1 -1
  3. package/dist/src/cli/help.d.ts +1 -1
  4. package/dist/src/codegen/generator/state.js +3 -0
  5. package/dist/src/codegen/generator/state.js.map +1 -1
  6. package/dist/src/codegen/generator/tables.js +3 -0
  7. package/dist/src/codegen/generator/tables.js.map +1 -1
  8. package/dist/src/driver/compiler.js +1 -1
  9. package/dist/src/driver/compiler.js.map +1 -1
  10. package/dist/src/driver/runtime-objects.d.ts +1 -1
  11. package/dist/src/driver/runtime-objects.js +2 -0
  12. package/dist/src/driver/runtime-objects.js.map +1 -1
  13. package/dist/src/driver/version.d.ts +1 -1
  14. package/dist/src/driver/version.js +1 -1
  15. package/dist/src/extensions/node/crypto/index.js +12 -1
  16. package/dist/src/extensions/node/crypto/index.js.map +1 -1
  17. package/dist/tests/e2e/compile-builtins.test.js +26 -0
  18. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  19. package/dist/tests/e2e/compile-node.test.js +37 -1
  20. package/dist/tests/e2e/compile-node.test.js.map +1 -1
  21. package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
  22. package/dist/tests/e2e/crypto-random.test.js +63 -0
  23. package/dist/tests/e2e/crypto-random.test.js.map +1 -0
  24. package/dist/tests/e2e/date-parse.test.js +27 -1
  25. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  26. package/dist/tests/e2e/gui-css-coverage.test.js +1 -1
  27. package/dist/tests/e2e/gui-dom.test.js +29 -0
  28. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  29. package/dist/tests/e2e/gui-form.test.d.ts +9 -0
  30. package/dist/tests/e2e/gui-form.test.js +359 -0
  31. package/dist/tests/e2e/gui-form.test.js.map +1 -0
  32. package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
  33. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  34. package/dist/tests/e2e/gui-style.test.js +82 -0
  35. package/dist/tests/e2e/gui-style.test.js.map +1 -1
  36. package/dist/tests/e2e/gui-unsupported.test.js +4 -3
  37. package/dist/tests/e2e/gui-unsupported.test.js.map +1 -1
  38. package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
  39. package/dist/tests/e2e/json-parse-errors.test.js +48 -0
  40. package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
  41. package/doc/ai/language-support.md +2 -1
  42. package/doc/ai/zh-CN/language-support.md +2 -1
  43. package/doc/gui/api.md +8 -2
  44. package/doc/gui/dom.md +21 -12
  45. package/doc/gui/input.md +74 -3
  46. package/doc/implemented/node/crypto.md +8 -0
  47. package/doc/implemented/node/fs.md +1 -1
  48. package/doc/implemented/stdlib.md +10 -2
  49. package/doc/zh-CN/gui/api.md +8 -2
  50. package/doc/zh-CN/gui/dom.md +17 -11
  51. package/doc/zh-CN/gui/input.md +65 -4
  52. package/doc/zh-CN/implemented/node/crypto.md +6 -0
  53. package/doc/zh-CN/implemented/node/fs.md +1 -1
  54. package/doc/zh-CN/implemented/stdlib.md +10 -2
  55. package/package.json +7 -7
  56. package/runtime/ext_gui/css_match.cpp +21 -1
  57. package/runtime/ext_gui/css_selector.cpp +2 -2
  58. package/runtime/ext_gui/document.cpp +12 -0
  59. package/runtime/ext_gui/dom.h +41 -2
  60. package/runtime/ext_gui/dom_api_document.cpp +28 -0
  61. package/runtime/ext_gui/dom_api_events.cpp +17 -14
  62. package/runtime/ext_gui/dom_api_form.cpp +567 -0
  63. package/runtime/ext_gui/dom_api_internal.h +46 -214
  64. package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
  65. package/runtime/ext_gui/dom_api_node.cpp +10 -3
  66. package/runtime/ext_gui/dom_api_node_read.cpp +1 -1
  67. package/runtime/ext_gui/dom_api_proto.cpp +45 -0
  68. package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
  69. package/runtime/ext_gui/dom_parse.cpp +114 -1
  70. package/runtime/ext_gui/form_focus.cpp +293 -0
  71. package/runtime/ext_gui/form_input.cpp +448 -0
  72. package/runtime/ext_gui/form_internal.h +30 -0
  73. package/runtime/ext_gui/form_shared.cpp +126 -0
  74. package/runtime/ext_gui/form_state.cpp +536 -0
  75. package/runtime/ext_gui/form_state.h +97 -0
  76. package/runtime/ext_gui/gui_engine.h +40 -4
  77. package/runtime/ext_gui/gui_events.cpp +63 -48
  78. package/runtime/ext_gui/layout.cpp +6 -0
  79. package/runtime/ext_gui/layout.h +4 -0
  80. package/runtime/ext_gui/layout_flow.cpp +23 -1
  81. package/runtime/ext_gui/paint.cpp +4 -1
  82. package/runtime/ext_gui/paint_form.cpp +389 -0
  83. package/runtime/ext_gui/paint_internal.h +6 -0
  84. package/runtime/ext_gui/style_print.cpp +30 -0
  85. package/runtime/ext_gui/style_ua.cpp +42 -0
  86. package/runtime/ext_gui/warnings.cpp +5 -7
  87. package/runtime/ext_gui/window.cpp +31 -55
  88. package/runtime/ext_gui/window_diagnostics.cpp +58 -0
  89. package/runtime/ext_gui/window_emit.cpp +51 -0
  90. package/runtime/ext_node/crypto/crypto.c +3 -2
  91. package/runtime/ext_node/crypto/crypto_hash.h +13 -0
  92. package/runtime/ext_node/crypto/crypto_random.c +339 -0
  93. package/runtime/ext_node/fs/async_worker.c +1 -1
  94. package/runtime/ext_node/fs/fs_ops.c +1 -1
  95. package/runtime/ext_node/fs/parts/platform.inc +65 -0
  96. package/runtime/rt.h +27 -0
  97. package/runtime/xt_crypto.c +132 -0
  98. package/runtime/xt_random.c +143 -0
  99. package/runtime/xt_regex/matcher.inc +9 -28
  100. package/runtime/xt_regex/parser.inc +65 -2
  101. package/runtime/xt_regex/types.inc +11 -1
  102. package/runtime/xt_regex.h +6 -2
  103. package/runtime/xt_stdlib/dispatch.inc +2 -0
  104. package/runtime/xt_stdlib/math.inc +1 -1
  105. package/runtime/xt_stdlib2/date.inc +204 -4
  106. package/runtime/xt_stdlib2/json.inc +4 -546
  107. package/runtime/xt_stdlib2/json_parse.inc +347 -0
  108. package/runtime/xt_stdlib2/json_stringify.inc +368 -0
  109. package/runtime/xt_stdlib2/regexp-match.inc +6 -11
  110. package/runtime/xt_stdlib2.c +0 -4
  111. package/runtime/xt_typed_array/elements.inc +6 -1
  112. package/src/checker/globals.ts +2 -0
  113. package/src/codegen/generator/state.ts +3 -0
  114. package/src/codegen/generator/tables.ts +3 -0
  115. package/src/driver/compiler.ts +1 -1
  116. package/src/driver/runtime-objects.ts +2 -0
  117. package/src/driver/version.ts +1 -1
  118. package/src/extensions/node/crypto/index.ts +12 -1
@@ -7,6 +7,7 @@
7
7
  #include "gui_keymap.h"
8
8
  #include "listener_options.h"
9
9
 
10
+ #include "form_state.h"
10
11
  #include "paint.h"
11
12
  #include "perf.h"
12
13
 
@@ -18,48 +19,6 @@
18
19
 
19
20
  using namespace guidev;
20
21
 
21
- /* -- window events -------------------------------------------------------- */
22
-
23
- void xt_gui_emit(XtGuiWindow *win, const char *event) {
24
- xt_gui_emit_payload(win, event, XT_UNDEFINED);
25
- }
26
-
27
- /** Drop one window listener (by identity: type + fn + capture). */
28
- static void remove_window_listener(XtGuiWindow *win, const XtGuiWindowListener &listener) {
29
- std::vector<XtGuiWindowListener> &listeners = win->window_listeners;
30
- for (auto it = listeners.begin(); it != listeners.end(); ++it) {
31
- if (it->type == listener.type && it->capture == listener.capture &&
32
- xt_truthy(xt_seq(it->fn, listener.fn))) {
33
- listeners.erase(it);
34
- return;
35
- }
36
- }
37
- }
38
-
39
- void xt_gui_emit_payload(XtGuiWindow *win, const char *event, xt_value payload) {
40
- if (win == NULL || !XT_IS_OBJECT(win->object)) return;
41
- xt_value args[1];
42
- int32_t count = 0;
43
- if (!XT_IS_UNDEFINED(payload)) {
44
- args[0] = payload;
45
- count = 1;
46
- }
47
- /* Snapshot the list: a handler may register or remove listeners while it
48
- * runs, and the DOM runs the batch as it stood when dispatch started. */
49
- std::vector<XtGuiWindowListener> snapshot = win->window_listeners;
50
- for (const XtGuiWindowListener &listener : snapshot) {
51
- if (listener.type != event) continue;
52
- /* `{ signal }` removes the listener at abort time (checked here, so no
53
- * callback plumbing is needed inside the signal). */
54
- if (xt_gui_signal_aborted(listener.signal)) {
55
- remove_window_listener(win, listener);
56
- continue;
57
- }
58
- if (listener.once) remove_window_listener(win, listener);
59
- if (XT_IS_FUNCTION(listener.fn)) xt_call_with_this(listener.fn, win->object, count, args);
60
- }
61
- }
62
-
63
22
  void xt_gui_flush_dom(XtGuiWindow *win) {
64
23
  if (win == NULL || win->document == nullptr) return;
65
24
  bool mutated = win->struct_dirty != 0;
@@ -187,27 +146,45 @@ void xt_gui_dispatch_pointer(XtGuiWindow *win, const char *type, float x, float
187
146
  int clicks) {
188
147
  if (win == NULL || !win->open) return;
189
148
  const xtgui::Node *node = xt_gui_hit_node(win, x, y);
149
+ xtgui::Node *target = const_cast<xtgui::Node *>(node);
190
150
  xt_gui_update_hover(win, node);
151
+ bool prevented = false;
191
152
  if (win->document != nullptr) {
192
153
  if (strcmp(type, "mousedown") == 0) {
193
- if (win->document->setFocus(node)) {
194
- win->geometry.dirty = 1;
195
- win->present_dirty = 1;
154
+ if (target != nullptr && xtgui::xt_form_is_focusable(target)) {
155
+ xt_gui_form_pointer(win, "mousedown", target, x, y);
156
+ } else if (target != nullptr && xtgui::xt_form_is_control(target)) {
157
+ /* A disabled/hidden control is not focusable and does not take focus. */
158
+ xt_gui_set_focus(win, nullptr);
159
+ } else {
160
+ xt_gui_set_focus(win, target);
196
161
  }
197
162
  if (win->document->setActive(node)) {
198
163
  win->geometry.dirty = 1;
199
164
  win->present_dirty = 1;
200
165
  }
201
166
  } else if (strcmp(type, "mouseup") == 0) {
167
+ const xtgui::Node *active = win->document->active();
168
+ if (active != nullptr && active->form && active->form->dragging) {
169
+ xt_gui_form_pointer(win, "mouseup", const_cast<xtgui::Node *>(active), x, y);
170
+ }
202
171
  if (win->document->setActive(nullptr)) {
203
172
  win->geometry.dirty = 1;
204
173
  win->present_dirty = 1;
205
174
  }
175
+ } else if (strcmp(type, "mousemove") == 0) {
176
+ const xtgui::Node *active = win->document->active();
177
+ if (active != nullptr && active->form && active->form->dragging) {
178
+ xt_gui_form_pointer(win, "mousemove", const_cast<xtgui::Node *>(active), x, y);
179
+ }
206
180
  }
207
181
  }
208
182
  xt_value payload = xt_gui_pointer_payload(type, win, x, y, button, clicks, node);
209
- if (node != nullptr) xt_gui_emit_dom_event(win, type, node, payload);
183
+ if (node != nullptr) prevented = xt_gui_emit_dom_event(win, type, node, payload);
210
184
  xt_gui_emit_payload(win, type, payload);
185
+ if (!prevented && node != nullptr && strcmp(type, "click") == 0) {
186
+ xt_gui_form_pointer(win, "click", target, x, y);
187
+ }
211
188
  }
212
189
 
213
190
  void xt_gui_dispatch_wheel(XtGuiWindow *win, float x, float y, float delta_x, float delta_y) {
@@ -262,12 +239,26 @@ static const xtgui::Node *xt_gui_key_target(XtGuiWindow *win) {
262
239
  }
263
240
 
264
241
  /** Deliver one fully built keyboard payload to the DOM (bubbling from the
265
- * focused element / body to `document`) and then to `window` listeners. */
242
+ * focused element / body to `document`) and then to `window` listeners. When
243
+ * the DOM did not cancel a `keydown`, run the focused control's editing
244
+ * default action. */
266
245
  static void xt_gui_dispatch_key_payload(XtGuiWindow *win, const char *type, xt_value payload) {
267
246
  if (win == NULL || !win->open) return;
268
247
  const xtgui::Node *node = xt_gui_key_target(win);
269
- if (node != nullptr) xt_gui_emit_dom_event(win, type, node, payload);
248
+ bool prevented = false;
249
+ if (node != nullptr) prevented = xt_gui_emit_dom_event(win, type, node, payload);
270
250
  xt_gui_emit_payload(win, type, payload);
251
+ if (!prevented && strcmp(type, "keydown") == 0) {
252
+ xt_gui_form_key_down(win, payload);
253
+ /* Tab is the one editing command that works from any element, not only a
254
+ * focused text field: move focus along the tabbable order. */
255
+ xt_value key = xt_object_get_cstr(payload, "key");
256
+ if (XT_IS_STRING(key) && strcmp(xt_string_data(key), "Tab") == 0) {
257
+ bool shift = xt_truthy(xt_object_get_cstr(payload, "shiftKey")) ||
258
+ xt_truthy(xt_object_get_cstr(payload, "shift"));
259
+ xt_gui_focus_step(win, !shift);
260
+ }
261
+ }
271
262
  }
272
263
 
273
264
  void xt_gui_dispatch_key(XtGuiWindow *win, const char *type, const char *key, const char *code) {
@@ -275,6 +266,13 @@ void xt_gui_dispatch_key(XtGuiWindow *win, const char *type, const char *key, co
275
266
  xt_gui_dispatch_key_payload(win, type, payload);
276
267
  }
277
268
 
269
+ void xt_gui_dispatch_key_with_modifiers(XtGuiWindow *win, const char *type, const char *key,
270
+ const char *code, bool repeat, bool ctrl, bool shift,
271
+ bool alt, bool meta) {
272
+ xt_value payload = xt_gui_key_payload(type, key, code, repeat, ctrl, shift, alt, meta);
273
+ xt_gui_dispatch_key_payload(win, type, payload);
274
+ }
275
+
278
276
  void xt_gui_dispatch_physical_key(XtGuiWindow *win, const char *type, int scancode, int keycode,
279
277
  bool repeat, bool ctrl, bool shift, bool alt, bool meta,
280
278
  bool caps_lock) {
@@ -284,6 +282,10 @@ void xt_gui_dispatch_physical_key(XtGuiWindow *win, const char *type, int scanco
284
282
  caps_lock, &key, &code);
285
283
  xt_value payload = xt_gui_key_payload(type, key.c_str(), code.c_str(), repeat, ctrl, shift, alt,
286
284
  meta);
285
+ /* Mark the payload so the editing default action knows the produced text will
286
+ * arrive separately as `SDL_EVENT_TEXT_INPUT` (inserting the US `key` here too
287
+ * would double every character on an IME or non-US layout). */
288
+ xt_object_set(payload, xt_string_from_cstr("__xt_gui_physical"), XT_TRUE);
287
289
  xt_gui_dispatch_key_payload(win, type, payload);
288
290
  }
289
291
 
@@ -327,6 +329,19 @@ void handle_event(const SDL_Event *event) {
327
329
  return;
328
330
  }
329
331
 
332
+ if (event->type == SDL_EVENT_TEXT_INPUT) {
333
+ XtGuiWindow *win = xt_gui_find_by_window_id(event->text.windowID);
334
+ if (win == NULL || !win->open) return;
335
+ xt_gui_text_input(win, event->text.text);
336
+ return;
337
+ }
338
+ if (event->type == SDL_EVENT_TEXT_EDITING) {
339
+ XtGuiWindow *win = xt_gui_find_by_window_id(event->edit.windowID);
340
+ if (win == NULL || !win->open) return;
341
+ xt_gui_text_editing(win, event->edit.text, event->edit.start, event->edit.length);
342
+ return;
343
+ }
344
+
330
345
  if (event->type == SDL_EVENT_MOUSE_MOTION) {
331
346
  XtGuiWindow *win = xt_gui_find_by_window_id(event->motion.windowID);
332
347
  xt_gui_dispatch_pointer(win, "mousemove", event->motion.x, event->motion.y, -1, 0);
@@ -23,6 +23,7 @@
23
23
  #include "image.h"
24
24
  #include "inline_svg.h"
25
25
  #include "perf.h"
26
+ #include "form_state.h"
26
27
  #include "style_internal.h"
27
28
 
28
29
  namespace xtgui {
@@ -128,7 +129,12 @@ std::unique_ptr<LayoutBox> buildBoxTree(const Node *node,
128
129
  }
129
130
  if (box->intrinsic_width <= 0.0f) box->intrinsic_width = 300.0f;
130
131
  if (box->intrinsic_height <= 0.0f) box->intrinsic_height = 150.0f;
132
+ } else if (node->isElement() && xt_form_is_control(node)) {
133
+ /* A form control is a leaf: its `<option>`/text children are the model, not
134
+ * laid-out boxes (the value is painted from `Node::form`). */
135
+ box->is_control = true;
131
136
  }
137
+ if (box->is_control) return box;
132
138
  if (box->is_inline_svg) return box;
133
139
 
134
140
  /* `::before` / `::after` boxes are emitted around the DOM children. Only a
@@ -89,6 +89,10 @@ struct LayoutBox {
89
89
  bool is_canvas = false;
90
90
  uint64_t canvas_id = 0;
91
91
 
92
+ /* `<input>` / `<textarea>` / `<select>`: a leaf box whose content is painted
93
+ * from the node's `FormState` (value, caret, decorations). */
94
+ bool is_control = false;
95
+
92
96
  /* Inline `<svg>`: a replaced element whose pixels are the serialized subtree
93
97
  * (`svg_markup`), rasterised like an SVG file at the drawn device size. The
94
98
  * normal box walk does not descend into its children — they are vector art,
@@ -8,6 +8,8 @@
8
8
 
9
9
  #include "layout_flow_internal.h"
10
10
 
11
+ #include "form_state.h"
12
+
11
13
  namespace xtgui {
12
14
  namespace layout_flow_detail {
13
15
 
@@ -44,6 +46,13 @@ float Layouter::verticalEdges(const LayoutBox *box) const {
44
46
  float Layouter::preferredContentWidth(const LayoutBox *box, float available) const {
45
47
  const XtStyle &style = *box->style;
46
48
  if (!isAuto(style.width)) return px(style.width, available, style.font_size, 0.0f);
49
+ if (box->is_control) {
50
+ float width = 0.0f;
51
+ float height = 0.0f;
52
+ xt_form_intrinsic_size(box->node, style, &width, &height);
53
+ (void)height;
54
+ if (width > 0.0f) return width;
55
+ }
47
56
  if ((box->is_image || box->is_canvas || box->is_inline_svg) && box->intrinsic_width > 0)
48
57
  return box->intrinsic_width;
49
58
  if (box->is_text) {
@@ -322,7 +331,20 @@ float Layouter::layoutBlock(LayoutBox *box, float x, float y, float availableWid
322
331
  }
323
332
 
324
333
  float contentHeight;
325
- if (forcedContentHeight >= 0) {
334
+ if (box->is_control) {
335
+ /* Controls are leaves with an intrinsic content height (a line, a textarea's
336
+ * rows, 13px for a checkbox, …); their children are not boxes. */
337
+ float intrinsic_width = 0.0f;
338
+ float intrinsic_height = 0.0f;
339
+ xt_form_intrinsic_size(box->node, style, &intrinsic_width, &intrinsic_height);
340
+ (void)intrinsic_width;
341
+ contentHeight = forcedContentHeight >= 0
342
+ ? forcedContentHeight
343
+ : (definiteHeight >= 0 ? definiteHeight : intrinsic_height);
344
+ contentHeight = clampSize(contentHeight, style.min_height, style.max_height,
345
+ availableHeight < 0 ? contentHeight : availableHeight, em);
346
+ if (contentHeight < 0) contentHeight = 0;
347
+ } else if (forcedContentHeight >= 0) {
326
348
  layoutChildren(box, contentWidth, availableHeight, forcedContentHeight);
327
349
  contentHeight = forcedContentHeight;
328
350
  } else {
@@ -279,7 +279,10 @@ void paintBoxInner(const LayoutBox *box, DisplayList &out, const PaintClip &clip
279
279
  paint_detail::addBoxShadows(out, *box, *style, chain, own, true, viewportWidth,
280
280
  viewportHeight);
281
281
  paint_detail::addOutline(out, *box, *style, chain, own, viewportWidth, viewportHeight);
282
- if (box->is_canvas && box->canvas_id != 0 && box->content_width > 0.0f &&
282
+ if (box->is_control) {
283
+ paint_detail::xt_paint_form_control(*box, *style, out, contentClip, chain, viewportWidth,
284
+ viewportHeight);
285
+ } else if (box->is_canvas && box->canvas_id != 0 && box->content_width > 0.0f &&
283
286
  box->content_height > 0.0f) {
284
287
  PaintImage image;
285
288
  image.x = box->content_x;
@@ -0,0 +1,389 @@
1
+ /*
2
+ * xbintsc GUI engine — form-control painting.
3
+ *
4
+ * The control's value, selection, caret and type-specific decorations
5
+ * (checkbox tick, radio dot, range thumb, select arrow, colour swatch) are
6
+ * painted from `Node::form`. Text is clipped to the content box so a long value
7
+ * cannot bleed over the border.
8
+ */
9
+
10
+ #include "paint_internal.h"
11
+
12
+ #include <algorithm>
13
+ #include <cmath>
14
+ #include <cstdio>
15
+ #include <cstdlib>
16
+ #include <string>
17
+ #include <vector>
18
+
19
+ #include "form_state.h"
20
+ #include "layout.h"
21
+
22
+ namespace xtgui {
23
+
24
+ namespace {
25
+
26
+ FontSpec specOf(const XtStyle &style) {
27
+ FontSpec spec;
28
+ spec.family = style.font_family;
29
+ spec.pixel_size = style.font_size;
30
+ spec.weight = style.font_weight;
31
+ spec.italic = style.font_style == FontStyle::Italic;
32
+ spec.letter_spacing = style.letter_spacing.unit == Unit::Px ? style.letter_spacing.value : 0.0f;
33
+ return spec;
34
+ }
35
+
36
+ float lineHeightOf(const XtStyle &style) { return style.font_size * 1.2f; }
37
+
38
+ void addRun(DisplayList &out, float x, float baseline, const std::string &text,
39
+ const XtStyle &style, const Color &color, const LayoutBox &box, const PaintClip &clip,
40
+ const paint_detail::FilterChain &chain, float viewportWidth, float viewportHeight) {
41
+ if (text.empty() || color.a <= 0.0f) return;
42
+ FontSpec spec = specOf(style);
43
+ float width = xt_text_measure_width(text, spec);
44
+ float height = lineHeightOf(style);
45
+ paint_detail::addTextRun(out, x, baseline, width, height, text, spec, color, box.world, clip,
46
+ style, chain, viewportWidth, viewportHeight);
47
+ }
48
+
49
+ float runWidth(const std::string &text, const XtStyle &style) {
50
+ return xt_text_measure_width(text, specOf(style));
51
+ }
52
+
53
+ float prefixWidth(const std::string &text, size_t units, const XtStyle &style) {
54
+ return runWidth(xt_form_utf16_slice(text, 0, units), style);
55
+ }
56
+
57
+ PaintClip intersectContent(const LayoutBox &box, const PaintClip &clip) {
58
+ PaintClip result = clip;
59
+ float x = box.content_x;
60
+ float y = box.content_y;
61
+ float width = box.content_width;
62
+ float height = box.content_height;
63
+ if (width < 0.0f) width = 0.0f;
64
+ if (height < 0.0f) height = 0.0f;
65
+ if (!clip.active) {
66
+ result.active = true;
67
+ result.x = x;
68
+ result.y = y;
69
+ result.width = width;
70
+ result.height = height;
71
+ return result;
72
+ }
73
+ float x0 = std::max(x, clip.x);
74
+ float y0 = std::max(y, clip.y);
75
+ float x1 = std::min(x + width, clip.x + clip.width);
76
+ float y1 = std::min(y + height, clip.y + clip.height);
77
+ result.x = x0;
78
+ result.y = y0;
79
+ result.width = std::max(0.0f, x1 - x0);
80
+ result.height = std::max(0.0f, y1 - y0);
81
+ return result;
82
+ }
83
+
84
+ void addRect(DisplayList &out, float x, float y, float width, float height, float radius,
85
+ const Color &color, const PaintClip &clip, const LayoutBox &box,
86
+ const paint_detail::FilterChain &chain) {
87
+ if (width <= 0.0f || height <= 0.0f || color.a <= 0.0f) return;
88
+ paint_detail::addRectFiltered(out, x, y, width, height, radius, color, clip, box.world, chain);
89
+ }
90
+
91
+ Color withAlpha(Color color, float alpha) {
92
+ color.a *= alpha;
93
+ return color;
94
+ }
95
+
96
+ Color shade(const XtStyle &style, bool disabled, float alpha) {
97
+ Color color = style.color;
98
+ if (disabled) color = Color{0.55f, 0.55f, 0.55f, color.a};
99
+ color.a *= alpha;
100
+ return color;
101
+ }
102
+
103
+ /** Split `text` into visual lines (`\n`) and report each line's UTF-16 range. */
104
+ struct VisualLine {
105
+ size_t start;
106
+ size_t end;
107
+ std::string text;
108
+ };
109
+
110
+ std::vector<VisualLine> splitLines(const std::string &text) {
111
+ std::vector<VisualLine> lines;
112
+ size_t byte_start = 0;
113
+ size_t units = 0;
114
+ size_t line_start_units = 0;
115
+ size_t offset = 0;
116
+ while (offset <= text.size()) {
117
+ if (offset == text.size() || text[offset] == '\n') {
118
+ VisualLine line;
119
+ line.start = line_start_units;
120
+ line.end = units;
121
+ line.text = text.substr(byte_start, offset - byte_start);
122
+ lines.push_back(line);
123
+ byte_start = offset + 1;
124
+ line_start_units = units;
125
+ if (offset == text.size()) break;
126
+ offset++;
127
+ continue;
128
+ }
129
+ size_t width = 1;
130
+ uint32_t code = (unsigned char)text[offset];
131
+ if (code < 0x80) width = 1;
132
+ else if ((code & 0xE0) == 0xC0) width = 2;
133
+ else if ((code & 0xF0) == 0xE0) width = 3;
134
+ else width = 4;
135
+ units += (code >= 0xF0) ? 2 : 1;
136
+ offset += width;
137
+ }
138
+ return lines;
139
+ }
140
+
141
+ void paintTextControl(const LayoutBox &box, const XtStyle &style, const FormState &state,
142
+ DisplayList &out, const PaintClip &clip,
143
+ const paint_detail::FilterChain &chain, float viewportWidth,
144
+ float viewportHeight) {
145
+ bool disabled = xt_form_is_disabled(box.node);
146
+ bool password = xt_form_type(box.node) == "password";
147
+ std::string display = state.value;
148
+ size_t caret = (size_t)state.selection_end;
149
+ if (state.composing) {
150
+ size_t length = xt_form_utf16_length(state.value);
151
+ size_t start = std::min((size_t)state.selection_start, length);
152
+ size_t end = std::min((size_t)state.selection_end, length);
153
+ display = xt_form_utf16_slice(state.value, 0, start) + state.composition +
154
+ xt_form_utf16_slice(state.value, end, length);
155
+ caret = start + xt_form_utf16_length(state.composition);
156
+ }
157
+ if (password) display = xt_form_mask(display);
158
+ size_t display_units = xt_form_utf16_length(display);
159
+ if (caret > display_units) caret = display_units;
160
+
161
+ float line_height = lineHeightOf(style);
162
+ FontSpec spec = specOf(style);
163
+ float ascent = 0.0f;
164
+ xt_text_metrics(spec, &ascent, nullptr, nullptr);
165
+ if (ascent <= 0.0f) ascent = style.font_size * 0.8f;
166
+
167
+ bool placeholder = display.empty() && !state.composing && !state.focused;
168
+ if (state.value.empty() && !state.composing) {
169
+ const std::string *hint = box.node->attr("placeholder");
170
+ if (hint != nullptr && !hint->empty() && state.value.empty()) {
171
+ std::string text = *hint;
172
+ float baseline = box.content_y + (box.content_height - line_height) / 2.0f + ascent;
173
+ addRun(out, box.content_x, baseline, text, style, shade(style, disabled, 0.45f), box, clip,
174
+ chain, viewportWidth, viewportHeight);
175
+ placeholder = true;
176
+ }
177
+ }
178
+
179
+ bool multiline = xt_form_is_textarea(box.node);
180
+ std::vector<VisualLine> lines = multiline ? splitLines(display) : std::vector<VisualLine>();
181
+ if (!multiline) {
182
+ VisualLine line;
183
+ line.start = 0;
184
+ line.end = display_units;
185
+ line.text = display;
186
+ lines.push_back(line);
187
+ }
188
+
189
+ size_t selection_start = (size_t)state.selection_start;
190
+ size_t selection_end = (size_t)state.selection_end;
191
+ if (selection_end < selection_start) std::swap(selection_start, selection_end);
192
+ bool has_selection = state.focused && selection_end > selection_start && !state.composing;
193
+ Color highlight{0.26f, 0.55f, 1.0f, 0.35f};
194
+
195
+ if (!placeholder) {
196
+ for (size_t index = 0; index < lines.size(); index++) {
197
+ const VisualLine &line = lines[index];
198
+ float y = multiline ? box.content_y + (float)index * line_height
199
+ : box.content_y + (box.content_height - line_height) / 2.0f;
200
+ if (multiline && y >= box.content_y + box.content_height) break;
201
+ float baseline = y + ascent;
202
+ if (has_selection) {
203
+ size_t start = std::max(selection_start, line.start);
204
+ size_t end = std::min(selection_end, line.end);
205
+ if (end > start) {
206
+ float x0 = box.content_x + prefixWidth(line.text, start - line.start, style);
207
+ float x1 = box.content_x + prefixWidth(line.text, end - line.start, style);
208
+ addRect(out, x0, y, x1 - x0, line_height, 0.0f, highlight, clip, box, chain);
209
+ }
210
+ }
211
+ addRun(out, box.content_x, baseline, line.text, style, shade(style, disabled, 1.0f), box,
212
+ clip, chain, viewportWidth, viewportHeight);
213
+ if (state.composing) {
214
+ float x0 = box.content_x;
215
+ float x1 = box.content_x + runWidth(line.text, style);
216
+ addRect(out, x0, y + line_height - 1.0f, x1 - x0, 1.0f, 0.0f,
217
+ shade(style, disabled, 0.9f), clip, box, chain);
218
+ }
219
+ }
220
+ }
221
+
222
+ if (state.focused && !state.composing && !has_selection) {
223
+ size_t line_index = 0;
224
+ size_t column = caret;
225
+ for (size_t index = 0; index < lines.size(); index++) {
226
+ if (caret >= lines[index].start && caret <= lines[index].end) {
227
+ line_index = index;
228
+ column = caret - lines[index].start;
229
+ break;
230
+ }
231
+ }
232
+ float y = multiline ? box.content_y + (float)line_index * line_height
233
+ : box.content_y + (box.content_height - line_height) / 2.0f;
234
+ float x = box.content_x + prefixWidth(lines[line_index].text, column, style);
235
+ addRect(out, x, y + 1.0f, 1.5f, line_height - 2.0f, 0.0f, shade(style, disabled, 1.0f), clip,
236
+ box, chain);
237
+ }
238
+ }
239
+
240
+ void paintCheckbox(const LayoutBox &box, const XtStyle &style, const FormState &state,
241
+ DisplayList &out, const PaintClip &clip, const paint_detail::FilterChain &chain,
242
+ bool radio) {
243
+ bool disabled = xt_form_is_disabled(box.node);
244
+ float size = std::max(13.0f, style.font_size * 0.9f);
245
+ float x = box.content_x + (box.content_width - size) / 2.0f;
246
+ float y = box.content_y + (box.content_height - size) / 2.0f;
247
+ Color border = disabled ? Color{0.6f, 0.6f, 0.6f, 1.0f} : Color{0.34f, 0.34f, 0.34f, 1.0f};
248
+ Color fill = disabled ? Color{0.94f, 0.94f, 0.94f, 1.0f} : Color{1.0f, 1.0f, 1.0f, 1.0f};
249
+ float radius = radio ? size / 2.0f : 2.0f;
250
+ addRect(out, x, y, size, size, radius, border, clip, box, chain);
251
+ addRect(out, x + 1.0f, y + 1.0f, size - 2.0f, size - 2.0f, std::max(0.0f, radius - 1.0f), fill,
252
+ clip, box, chain);
253
+ if (state.checked) {
254
+ Color accent = disabled ? Color{0.6f, 0.6f, 0.6f, 1.0f} : Color{0.17f, 0.42f, 0.69f, 1.0f};
255
+ if (radio) {
256
+ float inner = size - 8.0f;
257
+ addRect(out, x + 4.0f, y + 4.0f, inner, inner, inner / 2.0f, accent, clip, box, chain);
258
+ } else {
259
+ addRect(out, x + 3.0f, y + 3.0f, size - 6.0f, size - 6.0f, 1.0f, accent, clip, box, chain);
260
+ }
261
+ }
262
+ }
263
+
264
+ void paintRange(const LayoutBox &box, const XtStyle &style, const FormState &state,
265
+ DisplayList &out, const PaintClip &clip, const paint_detail::FilterChain &chain) {
266
+ bool disabled = xt_form_is_disabled(box.node);
267
+ double minimum = 0.0;
268
+ double maximum = 100.0;
269
+ const std::string *min_attr = box.node->attr("min");
270
+ const std::string *max_attr = box.node->attr("max");
271
+ if (min_attr != nullptr) minimum = std::strtod(min_attr->c_str(), nullptr);
272
+ if (max_attr != nullptr) maximum = std::strtod(max_attr->c_str(), nullptr);
273
+ if (maximum <= minimum) maximum = minimum + 1.0;
274
+ double value = std::strtod(state.value.c_str(), nullptr);
275
+ float fraction = (float)((value - minimum) / (maximum - minimum));
276
+ if (fraction < 0.0f) fraction = 0.0f;
277
+ if (fraction > 1.0f) fraction = 1.0f;
278
+ float track_height = 4.0f;
279
+ float track_y = box.content_y + (box.content_height - track_height) / 2.0f;
280
+ float thumb = 12.0f;
281
+ float travel = std::max(0.0f, box.content_width - thumb);
282
+ float thumb_x = box.content_x + fraction * travel;
283
+ float thumb_y = box.content_y + (box.content_height - thumb) / 2.0f;
284
+ Color track = disabled ? Color{0.8f, 0.8f, 0.8f, 1.0f} : Color{0.78f, 0.78f, 0.78f, 1.0f};
285
+ Color accent = disabled ? Color{0.6f, 0.6f, 0.6f, 1.0f} : Color{0.17f, 0.42f, 0.69f, 1.0f};
286
+ addRect(out, box.content_x, track_y, box.content_width, track_height, 2.0f, track, clip, box,
287
+ chain);
288
+ addRect(out, box.content_x, track_y, thumb_x - box.content_x + thumb / 2.0f, track_height, 2.0f,
289
+ accent, clip, box, chain);
290
+ addRect(out, thumb_x, thumb_y, thumb, thumb, thumb / 2.0f, accent, clip, box, chain);
291
+ }
292
+
293
+ void paintColorSwatch(const LayoutBox &box, const XtStyle &style, const FormState &state,
294
+ DisplayList &out, const PaintClip &clip,
295
+ const paint_detail::FilterChain &chain) {
296
+ float size = std::min(box.content_width, box.content_height);
297
+ if (size <= 0.0f) return;
298
+ float x = box.content_x + (box.content_width - size) / 2.0f;
299
+ float y = box.content_y + (box.content_height - size) / 2.0f;
300
+ Color color = xt_css_parse_color(state.value);
301
+ if (!color.valid) color = Color{0.0f, 0.0f, 0.0f, 1.0f};
302
+ addRect(out, x, y, size, size, 2.0f, Color{0.34f, 0.34f, 0.34f, 1.0f}, clip, box, chain);
303
+ addRect(out, x + 1.0f, y + 1.0f, size - 2.0f, size - 2.0f, 1.0f, color, clip, box, chain);
304
+ }
305
+
306
+ void paintSelect(const LayoutBox &box, const XtStyle &style, const FormState &state,
307
+ DisplayList &out, const PaintClip &clip, const paint_detail::FilterChain &chain,
308
+ float viewportWidth, float viewportHeight) {
309
+ const Node *option = xt_form_selected_option(box.node);
310
+ std::string label = option != nullptr ? xt_form_option_label(option) : "";
311
+ float line_height = lineHeightOf(style);
312
+ FontSpec spec = specOf(style);
313
+ float ascent = 0.0f;
314
+ xt_text_metrics(spec, &ascent, nullptr, nullptr);
315
+ if (ascent <= 0.0f) ascent = style.font_size * 0.8f;
316
+ float baseline = box.content_y + (box.content_height - line_height) / 2.0f + ascent;
317
+ addRun(out, box.content_x, baseline, label, style, shade(style, xt_form_is_disabled(box.node), 1.0f),
318
+ box, clip, chain, viewportWidth, viewportHeight);
319
+ /* A downward chevron on the right, like a native select. */
320
+ float arrow_size = 5.0f;
321
+ float ax = box.content_x + box.content_width - 10.0f;
322
+ float ay = box.content_y + (box.content_height - arrow_size) / 2.0f + 1.0f;
323
+ addRect(out, ax, ay, arrow_size, 1.5f, 0.0f, Color{0.25f, 0.25f, 0.25f, 1.0f}, clip, box, chain);
324
+ addRect(out, ax + 0.8f, ay + 1.2f, arrow_size - 1.6f, 1.5f, 0.0f,
325
+ Color{0.25f, 0.25f, 0.25f, 1.0f}, clip, box, chain);
326
+ }
327
+
328
+ void paintButtonLabel(const LayoutBox &box, const LayoutBox &shape, const XtStyle &style,
329
+ const std::string &label, DisplayList &out, const PaintClip &clip,
330
+ const paint_detail::FilterChain &chain, float viewportWidth,
331
+ float viewportHeight) {
332
+ float width = runWidth(label, style);
333
+ float line_height = lineHeightOf(style);
334
+ FontSpec spec = specOf(style);
335
+ float ascent = 0.0f;
336
+ xt_text_metrics(spec, &ascent, nullptr, nullptr);
337
+ if (ascent <= 0.0f) ascent = style.font_size * 0.8f;
338
+ float x = box.content_x + (box.content_width - width) / 2.0f;
339
+ float baseline = box.content_y + (box.content_height - line_height) / 2.0f + ascent;
340
+ addRun(out, x, baseline, label, style, shade(style, xt_form_is_disabled(box.node), 1.0f), shape,
341
+ clip, chain, viewportWidth, viewportHeight);
342
+ }
343
+
344
+ } // namespace
345
+
346
+ namespace paint_detail {
347
+
348
+ void xt_paint_form_control(const LayoutBox &box, const XtStyle &style, DisplayList &out,
349
+ const PaintClip &clip, const FilterChain &chain, float viewportWidth,
350
+ float viewportHeight) {
351
+ if (box.node == nullptr || !xt_form_is_control(box.node)) return;
352
+ const FormState *state = xt_form_state(box.node);
353
+ if (state == nullptr) return;
354
+ PaintClip content = intersectContent(box, clip);
355
+ std::string type = xt_form_type(box.node);
356
+ if (xt_form_is_textarea(box.node) || xt_form_is_text_like(box.node)) {
357
+ paintTextControl(box, style, *state, out, content, chain, viewportWidth, viewportHeight);
358
+ return;
359
+ }
360
+ if (type == "checkbox" || type == "radio") {
361
+ paintCheckbox(box, style, *state, out, content, chain, type == "radio");
362
+ return;
363
+ }
364
+ if (type == "range") {
365
+ paintRange(box, style, *state, out, content, chain);
366
+ return;
367
+ }
368
+ if (type == "color") {
369
+ paintColorSwatch(box, style, *state, out, content, chain);
370
+ return;
371
+ }
372
+ if (xt_form_is_select(box.node)) {
373
+ paintSelect(box, style, *state, out, content, chain, viewportWidth, viewportHeight);
374
+ return;
375
+ }
376
+ if (xt_form_is_button_like(box.node)) {
377
+ paintButtonLabel(box, box, style, xt_form_button_label(box.node), out, content, chain,
378
+ viewportWidth, viewportHeight);
379
+ return;
380
+ }
381
+ if (type == "file") {
382
+ paintButtonLabel(box, box, style, "Choose File", out, content, chain, viewportWidth,
383
+ viewportHeight);
384
+ }
385
+ }
386
+
387
+ } // namespace paint_detail
388
+
389
+ } // namespace xtgui
@@ -103,6 +103,12 @@ void addBitmapImage(DisplayList &out, std::shared_ptr<const PaintBitmap> bitmap,
103
103
  bool addBackdropFilter(DisplayList &out, const LayoutBox &box, const XtStyle &style,
104
104
  const PaintClip &clip, float viewportWidth, float viewportHeight);
105
105
 
106
+ /** Paint the value/selection/caret and type-specific decoration of a form
107
+ * control (`<input>`, `<textarea>`, `<select>`). */
108
+ void xt_paint_form_control(const LayoutBox &box, const XtStyle &style, DisplayList &out,
109
+ const PaintClip &clip, const FilterChain &chain,
110
+ float viewportWidth, float viewportHeight);
111
+
106
112
  /** Resolve `clip-path` into a device-space triangulated polygon. */
107
113
  int addClipPathForBox(DisplayList &out, const LayoutBox &box, const XtStyle &style,
108
114
  float viewportWidth, float viewportHeight);