xbintsc 0.5.31 → 0.5.32
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.
- package/README.md +16 -10
- package/dist/src/binder/references.js +14 -1
- package/dist/src/binder/references.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/codegen/generator/module.d.ts +1 -0
- package/dist/src/codegen/generator/module.js +54 -0
- package/dist/src/codegen/generator/module.js.map +1 -1
- package/dist/src/codegen/generator/statements/variables.js +6 -4
- package/dist/src/codegen/generator/statements/variables.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/gui/css.d.ts +26 -0
- package/dist/src/extensions/gui/css.js +100 -0
- package/dist/src/extensions/gui/css.js.map +1 -0
- package/dist/src/extensions/gui/html.js +9 -2
- package/dist/src/extensions/gui/html.js.map +1 -1
- package/dist/src/extensions/gui/index.js +16 -2
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/gui/three.d.ts +16 -0
- package/dist/src/extensions/gui/three.js +22 -0
- package/dist/src/extensions/gui/three.js.map +1 -0
- package/dist/src/extensions/media/index.d.ts +6 -5
- package/dist/src/extensions/media/index.js +12 -14
- package/dist/src/extensions/media/index.js.map +1 -1
- package/dist/tests/e2e/compile-builtins.test.js +24 -0
- package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
- package/dist/tests/e2e/compile-objects-classes.test.js +23 -0
- package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
- package/dist/tests/e2e/gui-browser.test.js +1 -1
- package/dist/tests/e2e/gui-browser.test.js.map +1 -1
- package/dist/tests/e2e/gui-effects.test.d.ts +6 -0
- package/dist/tests/e2e/gui-effects.test.js +262 -0
- package/dist/tests/e2e/gui-effects.test.js.map +1 -0
- package/dist/tests/e2e/gui-example-webgl.test.d.ts +10 -0
- package/dist/tests/e2e/gui-example-webgl.test.js +75 -0
- package/dist/tests/e2e/gui-example-webgl.test.js.map +1 -0
- package/dist/tests/e2e/gui-example-webgl2.test.d.ts +8 -0
- package/dist/tests/e2e/gui-example-webgl2.test.js +72 -0
- package/dist/tests/e2e/gui-example-webgl2.test.js.map +1 -0
- package/dist/tests/e2e/gui-layout.test.js +48 -0
- package/dist/tests/e2e/gui-layout.test.js.map +1 -1
- package/dist/tests/e2e/gui-media.test.js +5 -4
- package/dist/tests/e2e/gui-media.test.js.map +1 -1
- package/dist/tests/e2e/gui-webgl.test.d.ts +11 -0
- package/dist/tests/e2e/gui-webgl.test.js +179 -0
- package/dist/tests/e2e/gui-webgl.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2.test.d.ts +10 -0
- package/dist/tests/e2e/gui-webgl2.test.js +238 -0
- package/dist/tests/e2e/gui-webgl2.test.js.map +1 -0
- package/dist/tests/extensions/gui.test.js +41 -0
- package/dist/tests/extensions/gui.test.js.map +1 -1
- package/dist/tests/extensions/media.test.js +7 -3
- package/dist/tests/extensions/media.test.js.map +1 -1
- package/doc/ai/extensions.md +29 -9
- package/doc/ai/troubleshooting.md +12 -8
- package/doc/ai/zh-CN/extensions.md +26 -8
- package/doc/ai/zh-CN/troubleshooting.md +10 -6
- package/doc/gui/api.md +3 -2
- package/doc/gui/build.md +72 -16
- package/doc/gui/canvas.md +11 -3
- package/doc/gui/dom.md +33 -18
- package/doc/gui/known-issues.md +57 -24
- package/doc/gui/layout.md +18 -0
- package/doc/gui/media.md +17 -13
- package/doc/gui/rendering.md +50 -3
- package/doc/gui/webgl.md +256 -0
- package/doc/gui.md +108 -17
- package/doc/npm-package.md +6 -4
- package/doc/requirements.md +18 -9
- package/doc/zh-CN/gui/api.md +3 -2
- package/doc/zh-CN/gui/build.md +64 -15
- package/doc/zh-CN/gui/canvas.md +9 -2
- package/doc/zh-CN/gui/dom.md +20 -7
- package/doc/zh-CN/gui/known-issues.md +45 -19
- package/doc/zh-CN/gui/layout.md +14 -0
- package/doc/zh-CN/gui/media.md +15 -11
- package/doc/zh-CN/gui/rendering.md +41 -4
- package/doc/zh-CN/gui/webgl.md +115 -0
- package/doc/zh-CN/gui.md +89 -14
- package/doc/zh-CN/npm-package.md +9 -6
- package/doc/zh-CN/requirements.md +12 -6
- package/llms.txt +2 -0
- package/package.json +1 -1
- package/runtime/ext_gui/browser.cpp +39 -13
- package/runtime/ext_gui/canvas.cpp +3 -18
- package/runtime/ext_gui/canvas_internal.h +10 -0
- package/runtime/ext_gui/canvas_raster.cpp +60 -0
- package/runtime/ext_gui/clip_values.cpp +246 -0
- package/runtime/ext_gui/css.cpp +94 -0
- package/runtime/ext_gui/css.h +22 -1
- package/runtime/ext_gui/css_match.cpp +3 -0
- package/runtime/ext_gui/css_selector.cpp +9 -0
- package/runtime/ext_gui/css_sheet.cpp +89 -45
- package/runtime/ext_gui/document.cpp +4 -1
- package/runtime/ext_gui/document.h +2 -0
- package/runtime/ext_gui/dom.h +16 -0
- package/runtime/ext_gui/dom_api.cpp +1 -0
- package/runtime/ext_gui/dom_api_canvas.cpp +17 -80
- package/runtime/ext_gui/dom_api_canvas_extra.cpp +130 -1
- package/runtime/ext_gui/dom_api_canvas_pending.cpp +41 -0
- package/runtime/ext_gui/dom_api_internal.h +259 -0
- package/runtime/ext_gui/dom_api_webgl.cpp +376 -0
- package/runtime/ext_gui/dom_api_webgl2.cpp +434 -0
- package/runtime/ext_gui/dom_api_webgl2_draw.cpp +269 -0
- package/runtime/ext_gui/dom_api_webgl_buffer.cpp +212 -0
- package/runtime/ext_gui/dom_api_webgl_rt.h +30 -0
- package/runtime/ext_gui/dom_api_webgl_shader.cpp +443 -0
- package/runtime/ext_gui/dom_api_webgl_state.cpp +444 -0
- package/runtime/ext_gui/dom_api_webgl_texture.cpp +445 -0
- package/runtime/ext_gui/effect_values.cpp +312 -0
- package/runtime/ext_gui/gradient_values.cpp +476 -0
- package/runtime/ext_gui/grid_values.cpp +230 -0
- package/runtime/ext_gui/gui_canvas.cpp +50 -2
- package/runtime/ext_gui/gui_engine.h +27 -0
- package/runtime/ext_gui/gui_events.cpp +4 -1
- package/runtime/ext_gui/gui_media.cpp +1 -1
- package/runtime/ext_gui/gui_webgl.cpp +83 -0
- package/runtime/ext_gui/layout.cpp +21 -0
- package/runtime/ext_gui/layout_flow.cpp +81 -4
- package/runtime/ext_gui/layout_flow_internal.h +31 -0
- package/runtime/ext_gui/layout_grid.cpp +374 -0
- package/runtime/ext_gui/media.h +1 -1
- package/runtime/ext_gui/media_api.h +2 -2
- package/runtime/ext_gui/media_query.cpp +211 -0
- package/runtime/ext_gui/paint.cpp +111 -77
- package/runtime/ext_gui/paint.h +33 -3
- package/runtime/ext_gui/paint_background.cpp +229 -0
- package/runtime/ext_gui/paint_clip.cpp +268 -0
- package/runtime/ext_gui/paint_effects.cpp +324 -0
- package/runtime/ext_gui/paint_internal.h +85 -0
- package/runtime/ext_gui/renderer.cpp +8 -31
- package/runtime/ext_gui/renderer_clip.cpp +196 -0
- package/runtime/ext_gui/renderer_image.cpp +13 -8
- package/runtime/ext_gui/renderer_internal.h +15 -8
- package/runtime/ext_gui/renderer_text.cpp +6 -31
- package/runtime/ext_gui/style.cpp +343 -20
- package/runtime/ext_gui/style.h +144 -3
- package/runtime/ext_gui/style_print.cpp +134 -1
- package/runtime/ext_gui/style_values.cpp +152 -126
- package/runtime/ext_gui/style_values_internal.h +35 -0
- package/runtime/ext_gui/webgl.h +466 -0
- package/runtime/ext_gui/webgl2_constants.h +274 -0
- package/runtime/ext_gui/webgl_buffer.cpp +565 -0
- package/runtime/ext_gui/webgl_constants.h +317 -0
- package/runtime/ext_gui/webgl_context.cpp +466 -0
- package/runtime/ext_gui/webgl_draw.cpp +495 -0
- package/runtime/ext_gui/webgl_glsl.h +125 -0
- package/runtime/ext_gui/webgl_glsl_compile.cpp +431 -0
- package/runtime/ext_gui/webgl_glsl_rewrite.cpp +349 -0
- package/runtime/ext_gui/webgl_glsl_scan.cpp +581 -0
- package/runtime/ext_gui/webgl_msl.cpp +62 -0
- package/runtime/ext_gui/webgl_msl.h +33 -0
- package/runtime/ext_gui/webgl_pipeline.cpp +274 -0
- package/runtime/ext_gui/webgl_program_internal.h +18 -0
- package/runtime/ext_gui/webgl_shader.cpp +582 -0
- package/runtime/ext_gui/webgl_texture.cpp +293 -0
- package/runtime/ext_gui/webgl_uniform.cpp +167 -0
- package/runtime/ext_gui/webgl_webgl2.cpp +224 -0
- package/runtime/ext_media/media.cpp +1 -1
- package/runtime/ext_media/media_bridge.c +9 -3
- package/runtime/rt_internal.h +1 -0
- package/runtime/xt_containers/access.inc +8 -0
- package/runtime/xt_regex/api.inc +2 -0
- package/runtime/xt_regex/matcher.inc +16 -3
- package/runtime/xt_regex/parser.inc +8 -2
- package/runtime/xt_regex/types.inc +3 -0
- package/runtime/xt_stdlib2/regexp-match.inc +60 -21
- package/runtime/xt_stdlib2/regexp.inc +1 -0
- package/runtime/xt_typed_array.c +1 -1
- package/scripts/build-gui.ts +90 -3
- package/scripts/build-media.ts +323 -58
- package/scripts/setup-vs-toolchain.ps1 +152 -0
- package/src/binder/references.ts +13 -1
- package/src/codegen/generator/module.ts +50 -0
- package/src/codegen/generator/statements/variables.ts +6 -3
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/css.ts +113 -0
- package/src/extensions/gui/html.ts +9 -2
- package/src/extensions/gui/index.ts +16 -2
- package/src/extensions/gui/three.ts +25 -0
- package/src/extensions/media/index.ts +13 -16
|
@@ -65,11 +65,17 @@ xtgui::Canvas2D *xt_gui_canvas_get(XtGuiWindow *win, const xtgui::Node *node) {
|
|
|
65
65
|
auto canvas = std::make_unique<xtgui::Canvas2D>(width, height);
|
|
66
66
|
xtgui::Canvas2D *raw = canvas.get();
|
|
67
67
|
win->canvases[node] = std::move(canvas);
|
|
68
|
+
/* A context obtained after the first frame must reach the display list
|
|
69
|
+
* immediately, so `paintList`/the next paint sees its size and texture id. */
|
|
70
|
+
if (win->document != nullptr) {
|
|
71
|
+
win->document->setCanvasInfo(node, (float)width, (float)height, raw->id());
|
|
72
|
+
}
|
|
68
73
|
return raw;
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
void xt_gui_canvas_forget(XtGuiWindow *win, const xtgui::Node *node) {
|
|
72
77
|
if (win == nullptr || node == nullptr) return;
|
|
78
|
+
xt_gui_webgl_forget(win, node);
|
|
73
79
|
auto found = win->canvases.find(node);
|
|
74
80
|
if (found == win->canvases.end()) return;
|
|
75
81
|
releaseCanvasTexture(found->second.get());
|
|
@@ -81,8 +87,46 @@ void xt_gui_canvas_sync(XtGuiWindow *win) {
|
|
|
81
87
|
std::vector<const xtgui::Node *> nodes;
|
|
82
88
|
collectCanvases(win->document->root(), &nodes);
|
|
83
89
|
for (const xtgui::Node *node : nodes) {
|
|
84
|
-
xtgui::
|
|
85
|
-
|
|
90
|
+
if (xtgui::WebGLContext *webgl = xt_gui_webgl_find(win, node)) {
|
|
91
|
+
win->document->setCanvasInfo(node, (float)webgl->width(), (float)webgl->height(),
|
|
92
|
+
webgl->id());
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
xtgui::Canvas2D *canvas = xt_gui_canvas_find(win, node);
|
|
96
|
+
if (canvas != nullptr) {
|
|
97
|
+
win->document->setCanvasInfo(node, (float)canvas->width(), (float)canvas->height(),
|
|
98
|
+
canvas->id());
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
/* No context yet: the attributes still define the intrinsic box, but there
|
|
102
|
+
* is nothing to paint (a canvas with no context is transparent). */
|
|
103
|
+
int width = canvasAttributeSize(node, "width", 300);
|
|
104
|
+
int height = canvasAttributeSize(node, "height", 150);
|
|
105
|
+
win->document->setCanvasInfo(node, (float)width, (float)height, 0);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
void xt_gui_canvas_bind_pending(XtGuiWindow *win, const xtgui::Node *node, xt_value handle) {
|
|
110
|
+
if (win == nullptr || node == nullptr) return;
|
|
111
|
+
xt_value pending = xt_object_get_cstr(handle, "__xt_gui_pending_canvas");
|
|
112
|
+
if (!XT_IS_NUMBER(pending)) return;
|
|
113
|
+
uint64_t id = (uint64_t)xt_to_number(pending);
|
|
114
|
+
xt_object_set(handle, xt_string_from_cstr("__xt_gui_pending_canvas"), XT_UNDEFINED);
|
|
115
|
+
auto found = win->pending_canvases.find(id);
|
|
116
|
+
if (found == win->pending_canvases.end()) return;
|
|
117
|
+
if (!isCanvasElement(node)) {
|
|
118
|
+
win->pending_canvases.erase(found);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
xtgui::Canvas2D *canvas = found->second.get();
|
|
122
|
+
/* The parsed element's width/height attributes win over the default 300x150
|
|
123
|
+
* a pre-load context started with. */
|
|
124
|
+
int width = canvasAttributeSize(node, "width", canvas->width());
|
|
125
|
+
int height = canvasAttributeSize(node, "height", canvas->height());
|
|
126
|
+
if (width != canvas->width() || height != canvas->height()) canvas->resize(width, height);
|
|
127
|
+
win->canvases[node] = std::move(found->second);
|
|
128
|
+
win->pending_canvases.erase(found);
|
|
129
|
+
if (win->document != nullptr) {
|
|
86
130
|
win->document->setCanvasInfo(node, (float)canvas->width(), (float)canvas->height(),
|
|
87
131
|
canvas->id());
|
|
88
132
|
}
|
|
@@ -90,6 +134,10 @@ void xt_gui_canvas_sync(XtGuiWindow *win) {
|
|
|
90
134
|
|
|
91
135
|
void xt_gui_canvas_shutdown(XtGuiWindow *win) {
|
|
92
136
|
if (win == nullptr) return;
|
|
137
|
+
xt_gui_webgl_shutdown(win);
|
|
93
138
|
for (auto &entry : win->canvases) releaseCanvasTexture(entry.second.get());
|
|
94
139
|
win->canvases.clear();
|
|
140
|
+
/* `pending_canvases` survives `loadHTML`: a module that asked for a context
|
|
141
|
+
* before the page existed still needs its bitmap when the parsed element
|
|
142
|
+
* binds. They are released with the window record. */
|
|
95
143
|
}
|
|
@@ -84,6 +84,7 @@ xt_value xt_gui_audio_create(int32_t argc, xt_value *argv);
|
|
|
84
84
|
#include "document.h"
|
|
85
85
|
#include "media.h"
|
|
86
86
|
#include "renderer.h"
|
|
87
|
+
#include "webgl.h"
|
|
87
88
|
|
|
88
89
|
/** One element-level event listener registered through `addEventListener`. */
|
|
89
90
|
struct XtGuiNodeListener {
|
|
@@ -162,6 +163,14 @@ struct XtGuiWindow {
|
|
|
162
163
|
* lazily by `getContext("2d")` (or by `xt_gui_canvas_sync` for a live canvas),
|
|
163
164
|
* released when the node is forgotten or the document reloads. */
|
|
164
165
|
std::unordered_map<const xtgui::Node *, std::unique_ptr<xtgui::Canvas2D>> canvases;
|
|
166
|
+
/** One WebGL context per `<canvas>` that asked for `getContext("webgl")`.
|
|
167
|
+
* A canvas uses either this map or `canvases`, never both. */
|
|
168
|
+
std::unordered_map<const xtgui::Node *, std::unique_ptr<xtgui::WebGLContext>> webgl_canvases;
|
|
169
|
+
/** Canvases whose element handle was created before `loadHTML` (a module
|
|
170
|
+
* that calls `getElementById("scene").getContext("2d")` while it
|
|
171
|
+
* initialises). Keyed by canvas id; moved into `canvases` when the pending
|
|
172
|
+
* handle binds to the parsed element. */
|
|
173
|
+
std::unordered_map<uint64_t, std::unique_ptr<xtgui::Canvas2D>> pending_canvases;
|
|
165
174
|
/** The batch currently running: its callbacks have been swapped out of
|
|
166
175
|
* `animation_frames`, and a collection triggered by one of them must still
|
|
167
176
|
* see the rest. Kept on the record (not a local) so the GC root provider can
|
|
@@ -243,12 +252,30 @@ xtgui::Canvas2D *xt_gui_canvas_get(XtGuiWindow *win, const xtgui::Node *node);
|
|
|
243
252
|
xtgui::Canvas2D *xt_gui_canvas_find(XtGuiWindow *win, const xtgui::Node *node);
|
|
244
253
|
/** Drop a node's canvas (called when its handle is forgotten). */
|
|
245
254
|
void xt_gui_canvas_forget(XtGuiWindow *win, const xtgui::Node *node);
|
|
255
|
+
/** Move a pre-load canvas into the live table when its pending element handle
|
|
256
|
+
* binds. `handle` is the DOM handle that was used for `getContext("2d")`. */
|
|
257
|
+
void xt_gui_canvas_bind_pending(XtGuiWindow *win, const xtgui::Node *node, xt_value handle);
|
|
258
|
+
|
|
246
259
|
/** Publish live canvases to the document (sizes + render ids) so layout and
|
|
247
260
|
* paint see them; creates a context for a canvas that has none yet. */
|
|
248
261
|
void xt_gui_canvas_sync(XtGuiWindow *win);
|
|
249
262
|
/** Release every canvas of `win` (window close / `loadHTML`). */
|
|
250
263
|
void xt_gui_canvas_shutdown(XtGuiWindow *win);
|
|
251
264
|
|
|
265
|
+
/** The WebGL context of a `<canvas>`, creating it on first use. `webgl2`
|
|
266
|
+
* selects the WebGL 2 API/prototype; a canvas keeps the first context kind it
|
|
267
|
+
* asked for, so a later request for the other one answers null. */
|
|
268
|
+
xtgui::WebGLContext *xt_gui_webgl_get(XtGuiWindow *win, const xtgui::Node *node, bool webgl2);
|
|
269
|
+
/** The existing WebGL context of `node`, or null (no side effects). */
|
|
270
|
+
xtgui::WebGLContext *xt_gui_webgl_find(XtGuiWindow *win, const xtgui::Node *node);
|
|
271
|
+
/** Drop a node's WebGL context (called when its handle is forgotten). */
|
|
272
|
+
void xt_gui_webgl_forget(XtGuiWindow *win, const xtgui::Node *node);
|
|
273
|
+
/** Release every WebGL context of `win`. */
|
|
274
|
+
void xt_gui_webgl_shutdown(XtGuiWindow *win);
|
|
275
|
+
/** Submit every live WebGL context's queued commands. Called once per frame
|
|
276
|
+
* before the page's own render pass samples a canvas texture. */
|
|
277
|
+
void xt_gui_webgl_submit_all();
|
|
278
|
+
|
|
252
279
|
#endif /* __cplusplus */
|
|
253
280
|
|
|
254
281
|
#endif /* XT_GUI_ENGINE_H */
|
|
@@ -272,7 +272,7 @@ void xt_gui_render_window(XtGuiWindow *win) {
|
|
|
272
272
|
bool can_paint = xt_gui_renderer_ensure(g_device, win->window);
|
|
273
273
|
if (can_paint && win->geometry.dirty && win->document != nullptr) {
|
|
274
274
|
xtgui::DisplayList list;
|
|
275
|
-
xtgui::xt_paint_build(win->document->layout().root(), list);
|
|
275
|
+
xtgui::xt_paint_build(win->document->layout().root(), list, viewport_width, viewport_height);
|
|
276
276
|
xt_gui_geometry_upload(g_device, &win->geometry, list);
|
|
277
277
|
float density = SDL_GetWindowPixelDensity(win->window);
|
|
278
278
|
float text_scale = density > 0.0f ? density : 1.0f;
|
|
@@ -287,6 +287,9 @@ void xt_gui_render_window(XtGuiWindow *win) {
|
|
|
287
287
|
xt_gui_media_upload_frames(g_device);
|
|
288
288
|
/* Canvases are CPU-drawn: push any new bitmap without a relayout. */
|
|
289
289
|
xt_gui_canvas_upload_frames(g_device);
|
|
290
|
+
/* WebGL contexts record into their own command buffers; submit them first so
|
|
291
|
+
* the page's render pass samples this frame's drawing-buffer contents. */
|
|
292
|
+
xt_gui_webgl_submit_all();
|
|
290
293
|
SDL_GPUCommandBuffer *cmd = SDL_AcquireGPUCommandBuffer(g_device);
|
|
291
294
|
if (cmd == NULL) return;
|
|
292
295
|
SDL_GPUTexture *swapchain = NULL;
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - the renderer (`renderer_image.cpp`), which owns the GPU textures.
|
|
9
9
|
*
|
|
10
10
|
* It also provides the SDL audio sink the decoder writes PCM to, which keeps
|
|
11
|
-
* SDL out of `media.a`.
|
|
11
|
+
* SDL out of `media.a`/`media.lib`.
|
|
12
12
|
*
|
|
13
13
|
* The frame loop calls `xt_gui_media_sync` (open/close decoders so the next
|
|
14
14
|
* layout sees the current sources and intrinsic sizes) and `xt_gui_media_tick`
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — `<canvas>` WebGL lifetime.
|
|
3
|
+
*
|
|
4
|
+
* A WebGL context is owned by the window, keyed by its DOM element (the same
|
|
5
|
+
* shape as the 2D canvas and video tables). Its colour texture is what the
|
|
6
|
+
* page's image pipeline samples, so no pixels pass through the CPU.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include <memory>
|
|
10
|
+
|
|
11
|
+
#include "gui_engine.h"
|
|
12
|
+
#include "gui_internal.h"
|
|
13
|
+
|
|
14
|
+
namespace {
|
|
15
|
+
|
|
16
|
+
bool isCanvasElement(const xtgui::Node *node) {
|
|
17
|
+
return node != nullptr && node->isElement() && node->isTag("canvas");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
uint64_t next_webgl_id() {
|
|
21
|
+
static uint64_t counter = 0;
|
|
22
|
+
counter++;
|
|
23
|
+
/* Keep WebGL ids out of the Canvas2D id space so the renderer can tell the
|
|
24
|
+
* two kinds of canvas apart from the id alone. */
|
|
25
|
+
return (1ULL << 32) | counter;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
} // namespace
|
|
29
|
+
|
|
30
|
+
xtgui::WebGLContext *xt_gui_webgl_find(XtGuiWindow *win, const xtgui::Node *node) {
|
|
31
|
+
if (win == nullptr || node == nullptr) return nullptr;
|
|
32
|
+
auto found = win->webgl_canvases.find(node);
|
|
33
|
+
return found == win->webgl_canvases.end() ? nullptr : found->second.get();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
xtgui::WebGLContext *xt_gui_webgl_get(XtGuiWindow *win, const xtgui::Node *node, bool webgl2) {
|
|
37
|
+
if (win == nullptr || !isCanvasElement(node)) return nullptr;
|
|
38
|
+
xtgui::WebGLContext *existing = xt_gui_webgl_find(win, node);
|
|
39
|
+
if (existing != nullptr) return existing->is_webgl2() == webgl2 ? existing : nullptr;
|
|
40
|
+
if (win->canvases.find(node) != win->canvases.end()) return nullptr; // already a 2D canvas
|
|
41
|
+
/* WebGL shaders reach SDL_GPU as SPIR-V (Vulkan, translated by glslang) or as
|
|
42
|
+
* MSL (Metal, the same module cross-compiled by SPIRV-Cross). A device whose
|
|
43
|
+
* backend consumes neither format — Direct3D 12 needs DXIL — has no WebGL
|
|
44
|
+
* context in this build, so `getContext("webgl")` answers null instead of
|
|
45
|
+
* handing out a context whose draws could never create a pipeline. */
|
|
46
|
+
if (guidev::g_device == nullptr ||
|
|
47
|
+
(SDL_GetGPUShaderFormats(guidev::g_device) &
|
|
48
|
+
(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_MSL)) == 0) {
|
|
49
|
+
return nullptr;
|
|
50
|
+
}
|
|
51
|
+
const std::string *width_attr = node->attr("width");
|
|
52
|
+
const std::string *height_attr = node->attr("height");
|
|
53
|
+
float width = 300.0f;
|
|
54
|
+
float height = 150.0f;
|
|
55
|
+
if (width_attr != nullptr) xtgui::xt_css_parse_number(*width_attr, &width);
|
|
56
|
+
if (height_attr != nullptr) xtgui::xt_css_parse_number(*height_attr, &height);
|
|
57
|
+
auto context = std::make_unique<xtgui::WebGLContext>(
|
|
58
|
+
next_webgl_id(), (int)(width + 0.5f), (int)(height + 0.5f), webgl2);
|
|
59
|
+
xtgui::WebGLContext *raw = context.get();
|
|
60
|
+
win->webgl_canvases[node] = std::move(context);
|
|
61
|
+
if (win->document != nullptr) {
|
|
62
|
+
win->document->setCanvasInfo(node, (float)raw->width(), (float)raw->height(), raw->id());
|
|
63
|
+
}
|
|
64
|
+
return raw;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
void xt_gui_webgl_forget(XtGuiWindow *win, const xtgui::Node *node) {
|
|
68
|
+
if (win == nullptr || node == nullptr) return;
|
|
69
|
+
win->webgl_canvases.erase(node);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void xt_gui_webgl_shutdown(XtGuiWindow *win) {
|
|
73
|
+
if (win == nullptr) return;
|
|
74
|
+
win->webgl_canvases.clear();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
void xt_gui_webgl_submit_all() {
|
|
78
|
+
for (int i = 0; i < XT_GUI_MAX_WINDOWS; i++) {
|
|
79
|
+
XtGuiWindow &win = guidev::g_windows[i];
|
|
80
|
+
if (!win.open) continue;
|
|
81
|
+
for (auto &entry : win.webgl_canvases) entry.second->submit();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -123,6 +123,21 @@ std::unique_ptr<LayoutBox> buildBoxTree(const Node *node,
|
|
|
123
123
|
if (box->intrinsic_height <= 0.0f) box->intrinsic_height = 150.0f;
|
|
124
124
|
}
|
|
125
125
|
if (box->is_inline_svg) return box;
|
|
126
|
+
|
|
127
|
+
/* `::before` / `::after` boxes are emitted around the DOM children. Only a
|
|
128
|
+
* pseudo-element with `content` generates a box (CSS: a `::before` rule with
|
|
129
|
+
* no content has no effect). Replaced elements never generate them. */
|
|
130
|
+
auto appendPseudo = [&](Node *pseudo) {
|
|
131
|
+
if (pseudo == nullptr || box->is_image || box->is_canvas || box->is_media) return;
|
|
132
|
+
auto pseudoStyle = styles.find(pseudo);
|
|
133
|
+
if (pseudoStyle == styles.end() || !pseudoStyle->second.has_content) return;
|
|
134
|
+
std::unique_ptr<LayoutBox> pseudoBox = buildBoxTree(pseudo, styles, style, media, canvases);
|
|
135
|
+
if (pseudoBox != nullptr) {
|
|
136
|
+
pseudoBox->parent = box.get();
|
|
137
|
+
box->children.push_back(std::move(pseudoBox));
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
if (node->isElement() && !node->is_pseudo) appendPseudo(node->pseudo_before);
|
|
126
141
|
for (const std::unique_ptr<Node> &child : node->children) {
|
|
127
142
|
std::unique_ptr<LayoutBox> childBox =
|
|
128
143
|
buildBoxTree(child.get(), styles, style, media, canvases);
|
|
@@ -131,6 +146,7 @@ std::unique_ptr<LayoutBox> buildBoxTree(const Node *node,
|
|
|
131
146
|
box->children.push_back(std::move(childBox));
|
|
132
147
|
}
|
|
133
148
|
}
|
|
149
|
+
if (node->isElement() && !node->is_pseudo) appendPseudo(node->pseudo_after);
|
|
134
150
|
return box;
|
|
135
151
|
}
|
|
136
152
|
|
|
@@ -211,15 +227,20 @@ namespace {
|
|
|
211
227
|
|
|
212
228
|
const LayoutBox *hitTestBox(const LayoutBox *box, float x, float y) {
|
|
213
229
|
if (box == nullptr || box->display == Display::None) return nullptr;
|
|
230
|
+
if (box->style != nullptr && !box->style->visible) return nullptr;
|
|
214
231
|
/* Later siblings paint on top, so probe them first. */
|
|
215
232
|
for (auto it = box->children.rbegin(); it != box->children.rend(); ++it) {
|
|
216
233
|
if (const LayoutBox *hit = hitTestBox(it->get(), x, y)) return hit;
|
|
217
234
|
}
|
|
235
|
+
if (box->style != nullptr && !box->style->pointer_events) return nullptr;
|
|
218
236
|
float local_x = x;
|
|
219
237
|
float local_y = y;
|
|
220
238
|
if (box->has_transform) box->inverse.apply(x, y, &local_x, &local_y);
|
|
221
239
|
if (local_x >= box->x && local_x < box->x + box->width && local_y >= box->y &&
|
|
222
240
|
local_y < box->y + box->height) {
|
|
241
|
+
/* A generated box is part of its originating element: events must target
|
|
242
|
+
* the real element handle, not the synthetic node. */
|
|
243
|
+
if (box->node != nullptr && box->node->is_pseudo && box->parent != nullptr) return box->parent;
|
|
223
244
|
return box;
|
|
224
245
|
}
|
|
225
246
|
return nullptr;
|
|
@@ -46,6 +46,24 @@ float Layouter::preferredContentWidth(const LayoutBox *box, float available) con
|
|
|
46
46
|
if (!isAuto(style.width)) return px(style.width, available, style.font_size, 0.0f);
|
|
47
47
|
if ((box->is_image || box->is_canvas || box->is_inline_svg) && box->intrinsic_width > 0)
|
|
48
48
|
return box->intrinsic_width;
|
|
49
|
+
if (box->is_text) return textWidth(box->text, style);
|
|
50
|
+
/* A row flex container's intrinsic width is the sum of its items (and gaps);
|
|
51
|
+
* taking the widest child would make a shrink-to-fit toolbar narrower than
|
|
52
|
+
* its buttons and then shrink every button to zero. */
|
|
53
|
+
if (style.display == Display::Flex &&
|
|
54
|
+
(style.flex_direction == FlexDirection::Row ||
|
|
55
|
+
style.flex_direction == FlexDirection::RowReverse)) {
|
|
56
|
+
float total = 0;
|
|
57
|
+
int count = 0;
|
|
58
|
+
for (const std::unique_ptr<LayoutBox> &child : box->children) {
|
|
59
|
+
if (child->display == Display::None || isAbsoluteChild(child.get())) continue;
|
|
60
|
+
if (isCollapsibleText(child.get())) continue;
|
|
61
|
+
total += preferredContentWidth(child.get(), available) + horizontalEdges(child.get());
|
|
62
|
+
count++;
|
|
63
|
+
}
|
|
64
|
+
if (count > 1) total += px(style.column_gap, available, style.font_size, 0.0f) * (float)(count - 1);
|
|
65
|
+
return total;
|
|
66
|
+
}
|
|
49
67
|
float best = 0;
|
|
50
68
|
for (const std::unique_ptr<LayoutBox> &child : box->children) {
|
|
51
69
|
if (child->display == Display::None) continue;
|
|
@@ -165,10 +183,41 @@ void Layouter::applyPosition(LayoutBox *box) const {
|
|
|
165
183
|
translate(box, x - box->x, y - box->y);
|
|
166
184
|
}
|
|
167
185
|
|
|
186
|
+
namespace {
|
|
187
|
+
void clearTextFragments(LayoutBox *box) {
|
|
188
|
+
if (box == nullptr) return;
|
|
189
|
+
box->fragments.clear();
|
|
190
|
+
for (const std::unique_ptr<LayoutBox> &child : box->children) clearTextFragments(child.get());
|
|
191
|
+
}
|
|
192
|
+
} // namespace
|
|
193
|
+
|
|
168
194
|
float Layouter::layoutBlock(LayoutBox *box, float x, float y, float availableWidth,
|
|
169
195
|
float availableHeight, float forcedContentWidth,
|
|
170
196
|
float forcedContentHeight, bool inFlow) const {
|
|
171
197
|
const XtStyle &style = *box->style;
|
|
198
|
+
/* Flex and grid measure an item before placing it; inline runs append to the
|
|
199
|
+
* text boxes' fragment lists, so drop the previous pass's fragments first. */
|
|
200
|
+
clearTextFragments(box);
|
|
201
|
+
|
|
202
|
+
/* A text box is normally laid out by its parent's inline formatting context.
|
|
203
|
+
* Flex/grid items can be text boxes themselves (`<kbd>W</kbd>` with
|
|
204
|
+
* `display:grid`), and `layoutChildren` would otherwise give them no geometry
|
|
205
|
+
* or fragment at all — the glyph then silently disappears from paint. */
|
|
206
|
+
if (box->is_text) {
|
|
207
|
+
float textW = textWidth(box->text, style);
|
|
208
|
+
float textH = lineHeightOf(style);
|
|
209
|
+
box->x = x;
|
|
210
|
+
box->y = y;
|
|
211
|
+
box->content_x = x;
|
|
212
|
+
box->content_y = y;
|
|
213
|
+
box->width = textW;
|
|
214
|
+
box->content_width = textW;
|
|
215
|
+
box->height = textH;
|
|
216
|
+
box->content_height = textH;
|
|
217
|
+
box->baseline = textH;
|
|
218
|
+
box->fragments.push_back({x, y, textW, textH, box->text});
|
|
219
|
+
return textH;
|
|
220
|
+
}
|
|
172
221
|
float em = style.font_size;
|
|
173
222
|
resolveEdges(box, availableWidth);
|
|
174
223
|
|
|
@@ -197,6 +246,9 @@ float Layouter::layoutBlock(LayoutBox *box, float x, float y, float availableWid
|
|
|
197
246
|
}
|
|
198
247
|
} else {
|
|
199
248
|
contentWidth = px(style.width, availableWidth, em);
|
|
249
|
+
if (style.box_sizing == BoxSizing::BorderBox) {
|
|
250
|
+
contentWidth -= box->border_left + box->border_right + box->padding_left + box->padding_right;
|
|
251
|
+
}
|
|
200
252
|
}
|
|
201
253
|
contentWidth = clampSize(contentWidth, style.min_width, style.max_width, availableWidth, em);
|
|
202
254
|
if (contentWidth < 0) contentWidth = 0;
|
|
@@ -212,6 +264,19 @@ float Layouter::layoutBlock(LayoutBox *box, float x, float y, float availableWid
|
|
|
212
264
|
float definiteHeight = -1;
|
|
213
265
|
if (!isAuto(style.height) && !(style.height.unit == Unit::Percent && availableHeight < 0)) {
|
|
214
266
|
definiteHeight = px(style.height, availableHeight, em, -1);
|
|
267
|
+
if (style.box_sizing == BoxSizing::BorderBox && definiteHeight >= 0) {
|
|
268
|
+
definiteHeight -= box->border_top + box->border_bottom + box->padding_top + box->padding_bottom;
|
|
269
|
+
if (definiteHeight < 0) definiteHeight = 0;
|
|
270
|
+
}
|
|
271
|
+
} else if (outOfFlow && !isAuto(style.top) && !isAuto(style.bottom)) {
|
|
272
|
+
/* An absolutely positioned box with both vertical insets set and `height:
|
|
273
|
+
* auto` fills the containing block (the full-screen overlays rely on
|
|
274
|
+
* `inset: 0`). `layoutBlock` otherwise only knows definite heights from
|
|
275
|
+
* `height`, so derive it here. */
|
|
276
|
+
float top = px(style.top, box->cb_height, em);
|
|
277
|
+
float bottom = px(style.bottom, box->cb_height, em);
|
|
278
|
+
definiteHeight = box->cb_height - top - bottom - verticalEdges(box);
|
|
279
|
+
if (definiteHeight < 0) definiteHeight = 0;
|
|
215
280
|
}
|
|
216
281
|
|
|
217
282
|
float contentHeight;
|
|
@@ -225,9 +290,15 @@ float Layouter::layoutBlock(LayoutBox *box, float x, float y, float availableWid
|
|
|
225
290
|
availableHeight < 0 ? contentHeight : availableHeight, em);
|
|
226
291
|
if (contentHeight < 0) contentHeight = 0;
|
|
227
292
|
}
|
|
228
|
-
/* Replaced elements keep their aspect ratio when only one axis is set.
|
|
293
|
+
/* Replaced elements keep their aspect ratio when only one axis is set. A
|
|
294
|
+
* percentage height in an indefinite containing block behaves like `auto`
|
|
295
|
+
* (CSS 10.5), so it must take this path too: the portrait canvases are
|
|
296
|
+
* `height: 100%` inside a shrink-to-fit frame and were otherwise zero-height
|
|
297
|
+
* and invisible. */
|
|
298
|
+
bool replacedHeightAuto =
|
|
299
|
+
isAuto(style.height) || (style.height.unit == Unit::Percent && availableHeight < 0);
|
|
229
300
|
if ((box->is_image || box->is_canvas || box->is_inline_svg) && forcedContentHeight < 0 &&
|
|
230
|
-
|
|
301
|
+
replacedHeightAuto && box->intrinsic_width > 0 && box->intrinsic_height > 0) {
|
|
231
302
|
contentHeight = contentWidth * box->intrinsic_height / box->intrinsic_width;
|
|
232
303
|
}
|
|
233
304
|
box->content_height = contentHeight;
|
|
@@ -255,7 +326,11 @@ void Layouter::layoutAbsoluteChildren(LayoutBox *block, float contentWidth,
|
|
|
255
326
|
float availableHeight) const {
|
|
256
327
|
for (const std::unique_ptr<LayoutBox> &child : block->children) {
|
|
257
328
|
if (child->display == Display::None || !isAbsoluteChild(child.get())) continue;
|
|
258
|
-
|
|
329
|
+
/* An absolute box resolves against the nearest *positioned* ancestor, not
|
|
330
|
+
* necessarily its parent: under a static <body> the containing block is the
|
|
331
|
+
* initial containing block (the viewport). Passing the parent directly made
|
|
332
|
+
* `inset:0` hug the body's content height instead of filling the window. */
|
|
333
|
+
setContainingBlock(child.get(), containingBlockAncestor(block));
|
|
259
334
|
layoutBlock(child.get(), block->content_x, block->content_y, contentWidth, availableHeight, -1,
|
|
260
335
|
-1, false);
|
|
261
336
|
}
|
|
@@ -265,6 +340,8 @@ float Layouter::layoutChildren(LayoutBox *block, float contentWidth, float avail
|
|
|
265
340
|
float definiteHeight) const {
|
|
266
341
|
if (block->style->display == Display::Flex)
|
|
267
342
|
return layoutFlex(block, contentWidth, definiteHeight);
|
|
343
|
+
if (block->style->display == Display::Grid)
|
|
344
|
+
return layoutGrid(block, contentWidth, definiteHeight);
|
|
268
345
|
|
|
269
346
|
float y = block->content_y;
|
|
270
347
|
float startY = y;
|
|
@@ -345,7 +422,7 @@ float Layouter::layoutChildren(LayoutBox *block, float contentWidth, float avail
|
|
|
345
422
|
continue;
|
|
346
423
|
}
|
|
347
424
|
if (display == Display::Block || display == Display::ListItem || display == Display::Flex ||
|
|
348
|
-
child->style == nullptr) {
|
|
425
|
+
display == Display::Grid || child->style == nullptr) {
|
|
349
426
|
flush();
|
|
350
427
|
resolveEdges(child.get(), contentWidth);
|
|
351
428
|
/* Adjacent vertical margins collapse: the gap is the collapsed value of
|
|
@@ -105,6 +105,33 @@ struct FloatContext {
|
|
|
105
105
|
inline float toPx(const Length &length, float base, float em, float root, float vw, float vh,
|
|
106
106
|
float fallback) {
|
|
107
107
|
switch (length.unit) {
|
|
108
|
+
case Unit::Clamp: {
|
|
109
|
+
if (length.args.size() < 3) return fallback;
|
|
110
|
+
float low = toPx(length.args[0], base, em, root, vw, vh, fallback);
|
|
111
|
+
float value = toPx(length.args[1], base, em, root, vw, vh, fallback);
|
|
112
|
+
float high = toPx(length.args[2], base, em, root, vw, vh, fallback);
|
|
113
|
+
return value < low ? low : (value > high ? high : value);
|
|
114
|
+
}
|
|
115
|
+
case Unit::Min: {
|
|
116
|
+
float best = fallback;
|
|
117
|
+
bool first = true;
|
|
118
|
+
for (const Length &arg : length.args) {
|
|
119
|
+
float value = toPx(arg, base, em, root, vw, vh, fallback);
|
|
120
|
+
if (first || value < best) best = value;
|
|
121
|
+
first = false;
|
|
122
|
+
}
|
|
123
|
+
return first ? fallback : best;
|
|
124
|
+
}
|
|
125
|
+
case Unit::Max: {
|
|
126
|
+
float best = fallback;
|
|
127
|
+
bool first = true;
|
|
128
|
+
for (const Length &arg : length.args) {
|
|
129
|
+
float value = toPx(arg, base, em, root, vw, vh, fallback);
|
|
130
|
+
if (first || value > best) best = value;
|
|
131
|
+
first = false;
|
|
132
|
+
}
|
|
133
|
+
return first ? fallback : best;
|
|
134
|
+
}
|
|
108
135
|
case Unit::Px:
|
|
109
136
|
case Unit::Number:
|
|
110
137
|
return length.value;
|
|
@@ -242,6 +269,10 @@ struct Layouter {
|
|
|
242
269
|
|
|
243
270
|
float layoutFlex(LayoutBox *container, float contentWidth, float definiteHeight) const;
|
|
244
271
|
|
|
272
|
+
/** Grid formatting context (explicit/implicit tracks, gaps, auto-placement
|
|
273
|
+
* and item alignment). Returns the used content height. */
|
|
274
|
+
float layoutGrid(LayoutBox *container, float contentWidth, float definiteHeight) const;
|
|
275
|
+
|
|
245
276
|
/** Flex row main-axis layout, wrapping items into lines when `flex-wrap`
|
|
246
277
|
* allows it. Returns the used cross size. */
|
|
247
278
|
float layoutFlexRow(LayoutBox *container, const std::vector<LayoutBox *> &items,
|