xbintsc 0.6.9 → 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.
- package/dist/src/checker/globals.js +2 -1
- package/dist/src/checker/globals.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/main.js +1 -0
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/codegen/generator/state.js +3 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +3 -0
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/compiler.js +1 -1
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/config.js +1 -0
- package/dist/src/driver/config.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +2 -0
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/driver/version.js.map +1 -1
- package/dist/src/extensions/node/crypto/index.js +12 -1
- package/dist/src/extensions/node/crypto/index.js.map +1 -1
- package/dist/tests/driver/config.test.js +15 -2
- package/dist/tests/driver/config.test.js.map +1 -1
- package/dist/tests/e2e/compile-builtins.test.js +26 -0
- package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
- package/dist/tests/e2e/compile-node.test.js +37 -1
- package/dist/tests/e2e/compile-node.test.js.map +1 -1
- package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
- package/dist/tests/e2e/crypto-random.test.js +63 -0
- package/dist/tests/e2e/crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/date-parse.test.js +27 -1
- package/dist/tests/e2e/date-parse.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-coverage.test.js +1 -1
- package/dist/tests/e2e/gui-dom.test.js +29 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -1
- package/dist/tests/e2e/gui-form.test.d.ts +9 -0
- package/dist/tests/e2e/gui-form.test.js +359 -0
- package/dist/tests/e2e/gui-form.test.js.map +1 -0
- package/dist/tests/e2e/gui-helpers.d.ts +2 -0
- package/dist/tests/e2e/gui-helpers.js +1 -0
- package/dist/tests/e2e/gui-helpers.js.map +1 -1
- package/dist/tests/e2e/gui-layout.test.js +47 -0
- package/dist/tests/e2e/gui-layout.test.js.map +1 -1
- package/dist/tests/e2e/gui-paint.test.js +108 -0
- package/dist/tests/e2e/gui-paint.test.js.map +1 -1
- package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
- package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
- package/dist/tests/e2e/gui-style.test.js +82 -0
- package/dist/tests/e2e/gui-style.test.js.map +1 -1
- package/dist/tests/e2e/gui-unsupported.test.d.ts +6 -0
- package/dist/tests/e2e/gui-unsupported.test.js +124 -0
- package/dist/tests/e2e/gui-unsupported.test.js.map +1 -0
- package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
- package/dist/tests/e2e/json-parse-errors.test.js +48 -0
- package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
- package/doc/ai/extensions.md +7 -4
- package/doc/ai/language-support.md +2 -1
- package/doc/ai/zh-CN/extensions.md +6 -3
- package/doc/ai/zh-CN/language-support.md +2 -1
- package/doc/gui/api.md +8 -2
- package/doc/gui/dom.md +81 -4
- package/doc/gui/input.md +74 -3
- package/doc/gui/known-issues.md +12 -5
- package/doc/gui/layout.md +19 -6
- package/doc/gui/rendering.md +16 -9
- package/doc/implemented/node/crypto.md +8 -0
- package/doc/implemented/node/fs.md +1 -1
- package/doc/implemented/stdlib.md +10 -2
- package/doc/zh-CN/gui/api.md +8 -2
- package/doc/zh-CN/gui/dom.md +70 -4
- package/doc/zh-CN/gui/input.md +65 -4
- package/doc/zh-CN/gui/known-issues.md +11 -4
- package/doc/zh-CN/gui/layout.md +14 -4
- package/doc/zh-CN/gui/rendering.md +13 -10
- package/doc/zh-CN/implemented/node/crypto.md +6 -0
- package/doc/zh-CN/implemented/node/fs.md +1 -1
- package/doc/zh-CN/implemented/stdlib.md +10 -2
- package/package.json +7 -7
- package/runtime/ext_gui/clip_values.cpp +11 -0
- package/runtime/ext_gui/css.cpp +27 -2
- package/runtime/ext_gui/css_match.cpp +21 -1
- package/runtime/ext_gui/css_selector.cpp +23 -0
- package/runtime/ext_gui/css_sheet.cpp +7 -0
- package/runtime/ext_gui/document.cpp +15 -0
- package/runtime/ext_gui/document_animation.cpp +4 -1
- package/runtime/ext_gui/dom.h +41 -2
- package/runtime/ext_gui/dom_api.cpp +3 -0
- package/runtime/ext_gui/dom_api_document.cpp +28 -0
- package/runtime/ext_gui/dom_api_events.cpp +17 -14
- package/runtime/ext_gui/dom_api_form.cpp +567 -0
- package/runtime/ext_gui/dom_api_internal.h +46 -214
- package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
- package/runtime/ext_gui/dom_api_node.cpp +10 -3
- package/runtime/ext_gui/dom_api_node_read.cpp +1 -1
- package/runtime/ext_gui/dom_api_proto.cpp +45 -0
- package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
- package/runtime/ext_gui/dom_parse.cpp +118 -1
- package/runtime/ext_gui/effect_values.cpp +28 -7
- package/runtime/ext_gui/form_focus.cpp +293 -0
- package/runtime/ext_gui/form_input.cpp +448 -0
- package/runtime/ext_gui/form_internal.h +30 -0
- package/runtime/ext_gui/form_shared.cpp +126 -0
- package/runtime/ext_gui/form_state.cpp +536 -0
- package/runtime/ext_gui/form_state.h +97 -0
- package/runtime/ext_gui/gradient_values.cpp +25 -5
- package/runtime/ext_gui/gui_engine.h +40 -4
- package/runtime/ext_gui/gui_events.cpp +63 -48
- package/runtime/ext_gui/inline_svg.cpp +3 -0
- package/runtime/ext_gui/layout.cpp +6 -0
- package/runtime/ext_gui/layout.h +4 -0
- package/runtime/ext_gui/layout_flow.cpp +35 -3
- package/runtime/ext_gui/layout_inline.cpp +80 -7
- package/runtime/ext_gui/media_query.cpp +7 -1
- package/runtime/ext_gui/paint.cpp +62 -24
- package/runtime/ext_gui/paint.h +7 -4
- package/runtime/ext_gui/paint_clip.cpp +74 -19
- package/runtime/ext_gui/paint_form.cpp +389 -0
- package/runtime/ext_gui/paint_internal.h +13 -1
- package/runtime/ext_gui/renderer_clip.cpp +19 -17
- package/runtime/ext_gui/style.cpp +5 -1
- package/runtime/ext_gui/style.h +5 -0
- package/runtime/ext_gui/style_cascade.cpp +2 -0
- package/runtime/ext_gui/style_internal.h +7 -2
- package/runtime/ext_gui/style_print.cpp +33 -0
- package/runtime/ext_gui/style_text.cpp +11 -0
- package/runtime/ext_gui/style_ua.cpp +42 -0
- package/runtime/ext_gui/style_values.cpp +89 -90
- package/runtime/ext_gui/transform.cpp +2 -0
- package/runtime/ext_gui/warnings.cpp +298 -0
- package/runtime/ext_gui/warnings.h +59 -0
- package/runtime/ext_gui/window.cpp +31 -55
- package/runtime/ext_gui/window_diagnostics.cpp +58 -0
- package/runtime/ext_gui/window_emit.cpp +51 -0
- package/runtime/ext_node/crypto/crypto.c +3 -2
- package/runtime/ext_node/crypto/crypto_hash.h +13 -0
- package/runtime/ext_node/crypto/crypto_random.c +339 -0
- package/runtime/ext_node/fs/async_worker.c +1 -1
- package/runtime/ext_node/fs/fs_ops.c +1 -1
- package/runtime/ext_node/fs/parts/platform.inc +65 -0
- package/runtime/rt.h +27 -0
- package/runtime/xt_crypto.c +132 -0
- package/runtime/xt_random.c +143 -0
- package/runtime/xt_regex/matcher.inc +9 -28
- package/runtime/xt_regex/parser.inc +65 -2
- package/runtime/xt_regex/types.inc +11 -1
- package/runtime/xt_regex.h +6 -2
- package/runtime/xt_stdlib/dispatch.inc +2 -0
- package/runtime/xt_stdlib/math.inc +1 -1
- package/runtime/xt_stdlib2/date.inc +204 -4
- package/runtime/xt_stdlib2/json.inc +4 -546
- package/runtime/xt_stdlib2/json_parse.inc +347 -0
- package/runtime/xt_stdlib2/json_stringify.inc +368 -0
- package/runtime/xt_stdlib2/regexp-match.inc +6 -11
- package/runtime/xt_stdlib2.c +0 -4
- package/runtime/xt_typed_array/elements.inc +6 -1
- package/src/checker/globals.ts +2 -0
- package/src/cli/main.ts +1 -0
- package/src/codegen/generator/state.ts +3 -0
- package/src/codegen/generator/tables.ts +3 -0
- package/src/driver/compiler.ts +1 -1
- package/src/driver/config.ts +1 -0
- package/src/driver/runtime-objects.ts +2 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/node/crypto/index.ts +12 -1
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — text input, IME and the keyboard/pointer default actions
|
|
3
|
+
* of form controls.
|
|
4
|
+
*
|
|
5
|
+
* SDL delivers *produced text* in `SDL_EVENT_TEXT_INPUT` (that is the only
|
|
6
|
+
* source that survives a non-US layout or an IME) and pre-edit text in
|
|
7
|
+
* `SDL_EVENT_TEXT_EDITING`. Key events handle the editing commands
|
|
8
|
+
* (Backspace/arrows/clipboard); printable synthetic keys are inserted directly
|
|
9
|
+
* so `win.sendEvent("keydown", { key: "a" })` works headlessly without waiting
|
|
10
|
+
* for a matching text-input event.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
#include "gui_engine.h"
|
|
14
|
+
|
|
15
|
+
#include <SDL3/SDL.h>
|
|
16
|
+
|
|
17
|
+
#include <algorithm>
|
|
18
|
+
#include <cctype>
|
|
19
|
+
#include <cmath>
|
|
20
|
+
#include <cstdio>
|
|
21
|
+
#include <cstdlib>
|
|
22
|
+
#include <string>
|
|
23
|
+
#include <vector>
|
|
24
|
+
|
|
25
|
+
#include "form_state.h"
|
|
26
|
+
#include "form_internal.h"
|
|
27
|
+
#include "layout.h"
|
|
28
|
+
|
|
29
|
+
namespace {
|
|
30
|
+
|
|
31
|
+
using xtgui::FormState;
|
|
32
|
+
using xtgui::Node;
|
|
33
|
+
|
|
34
|
+
FormState *focused_state(XtGuiWindow *win, Node **node_out) {
|
|
35
|
+
if (node_out != nullptr) *node_out = nullptr;
|
|
36
|
+
if (win == nullptr || win->document == nullptr) return nullptr;
|
|
37
|
+
Node *node = const_cast<Node *>(win->document->focus());
|
|
38
|
+
if (!xtgui::formdetail::editable(node)) return nullptr;
|
|
39
|
+
if (node_out != nullptr) *node_out = node;
|
|
40
|
+
return xtgui::xt_form_state(node);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
void emit_composition(XtGuiWindow *win, Node *node, const char *type, const std::string &data) {
|
|
45
|
+
xt_value payload = xtgui::formdetail::make_payload(type);
|
|
46
|
+
xt_object_set(payload, xt_string_from_cstr("data"), xt_string_from_cstr(data.c_str()));
|
|
47
|
+
xt_object_set(payload, xt_string_from_cstr("isComposing"),
|
|
48
|
+
std::string(type) == "compositionend" ? XT_FALSE : XT_TRUE);
|
|
49
|
+
xt_object_set(payload, xt_string_from_cstr("value"),
|
|
50
|
+
xt_string_from_cstr(xtgui::xt_form_value(node).c_str()));
|
|
51
|
+
xt_gui_emit_dom_event(win, type, node, payload);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
bool before_input(XtGuiWindow *win, Node *node, const std::string &data, const char *input_type) {
|
|
55
|
+
xt_value payload = xtgui::formdetail::make_payload("beforeinput");
|
|
56
|
+
xt_object_set(payload, xt_string_from_cstr("data"), xt_string_from_cstr(data.c_str()));
|
|
57
|
+
xt_object_set(payload, xt_string_from_cstr("inputType"), xt_string_from_cstr(input_type));
|
|
58
|
+
xt_object_set(payload, xt_string_from_cstr("value"),
|
|
59
|
+
xt_string_from_cstr(xtgui::xt_form_value(node).c_str()));
|
|
60
|
+
return xt_gui_emit_dom_event(win, "beforeinput", node, payload);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Emit `keypress`; true when a listener cancelled the insertion. */
|
|
64
|
+
bool emit_keypress(XtGuiWindow *win, Node *node, const std::string &text) {
|
|
65
|
+
if (text.empty()) return false;
|
|
66
|
+
xt_value payload = xtgui::formdetail::make_payload("keypress");
|
|
67
|
+
xt_object_set(payload, xt_string_from_cstr("key"), xt_string_from_cstr(text.c_str()));
|
|
68
|
+
unsigned char first = (unsigned char)text[0];
|
|
69
|
+
double code = first;
|
|
70
|
+
xt_object_set(payload, xt_string_from_cstr("charCode"), xt_number(code));
|
|
71
|
+
xt_object_set(payload, xt_string_from_cstr("keyCode"), xt_number(code));
|
|
72
|
+
return xt_gui_emit_dom_event(win, "keypress", node, payload);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
int max_length_of(const Node *node) {
|
|
76
|
+
const std::string *value = node->attr("maxlength");
|
|
77
|
+
if (value == nullptr) return -1;
|
|
78
|
+
return (int)std::strtod(value->c_str(), nullptr);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Drop characters a browser's number/date/time editors would not accept. */
|
|
82
|
+
std::string filter_for_type(const Node *node, const std::string &text) {
|
|
83
|
+
std::string type = xtgui::xt_form_type(node);
|
|
84
|
+
std::string out;
|
|
85
|
+
for (char c : text) {
|
|
86
|
+
bool keep = true;
|
|
87
|
+
if (type == "number") {
|
|
88
|
+
keep = (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E';
|
|
89
|
+
} else if (type == "date" || type == "month" || type == "week") {
|
|
90
|
+
keep = (c >= '0' && c <= '9') || c == '-' || c == '/';
|
|
91
|
+
} else if (type == "time") {
|
|
92
|
+
keep = (c >= '0' && c <= '9') || c == ':' || c == '.';
|
|
93
|
+
} else if (type == "datetime-local") {
|
|
94
|
+
keep = (c >= '0' && c <= '9') || c == '-' || c == ':' || c == 'T' || c == ' ';
|
|
95
|
+
}
|
|
96
|
+
if (keep) out.push_back(c);
|
|
97
|
+
}
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Replace `[start, end)` (UTF-16 units) with `text`. Returns the new caret. */
|
|
102
|
+
int replace_range(Node *node, int start, int end, std::string text) {
|
|
103
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
104
|
+
if (state == nullptr) return 0;
|
|
105
|
+
text = filter_for_type(node, text);
|
|
106
|
+
int length = (int)xtgui::xt_form_utf16_length(state->value);
|
|
107
|
+
if (start < 0) start = 0;
|
|
108
|
+
if (end < start) end = start;
|
|
109
|
+
if (end > length) end = length;
|
|
110
|
+
int max_length = max_length_of(node);
|
|
111
|
+
if (max_length >= 0) {
|
|
112
|
+
int current = length - (end - start);
|
|
113
|
+
int available = max_length - current;
|
|
114
|
+
if (available < 0) available = 0;
|
|
115
|
+
size_t units = xtgui::xt_form_utf16_length(text);
|
|
116
|
+
if ((int)units > available) text = xtgui::xt_form_utf16_slice(text, 0, (size_t)available);
|
|
117
|
+
}
|
|
118
|
+
std::string before = xtgui::xt_form_utf16_slice(state->value, 0, (size_t)start);
|
|
119
|
+
std::string after = xtgui::xt_form_utf16_slice(state->value, (size_t)end, (size_t)length);
|
|
120
|
+
state->value = before + text + after;
|
|
121
|
+
int caret = start + (int)xtgui::xt_form_utf16_length(text);
|
|
122
|
+
state->selection_start = caret;
|
|
123
|
+
state->selection_end = caret;
|
|
124
|
+
state->selection_direction = 0;
|
|
125
|
+
return caret;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
int selection_length(const FormState *state) {
|
|
129
|
+
return state->selection_end - state->selection_start;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
void commit_composition(XtGuiWindow *win, Node *node, const std::string &text) {
|
|
133
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
134
|
+
if (state == nullptr) return;
|
|
135
|
+
int start = state->selection_start;
|
|
136
|
+
int end = state->selection_end;
|
|
137
|
+
state->composing = false;
|
|
138
|
+
state->composition.clear();
|
|
139
|
+
if (!text.empty() && !before_input(win, node, text, "insertCompositionText")) {
|
|
140
|
+
replace_range(node, start, end, text);
|
|
141
|
+
xtgui::formdetail::dirty(win);
|
|
142
|
+
xtgui::formdetail::emit_input(win, node, text, "insertCompositionText");
|
|
143
|
+
}
|
|
144
|
+
emit_composition(win, node, "compositionend", text);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
void move_caret(Node *node, int target, bool extend) {
|
|
148
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
149
|
+
if (state == nullptr) return;
|
|
150
|
+
size_t length = xtgui::xt_form_utf16_length(state->value);
|
|
151
|
+
if (target < 0) target = 0;
|
|
152
|
+
if ((size_t)target > length) target = (int)length;
|
|
153
|
+
if (!extend) {
|
|
154
|
+
state->selection_start = target;
|
|
155
|
+
state->selection_end = target;
|
|
156
|
+
state->selection_direction = 0;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (target < state->selection_start) {
|
|
160
|
+
state->selection_start = target;
|
|
161
|
+
state->selection_direction = -1;
|
|
162
|
+
} else {
|
|
163
|
+
state->selection_end = target;
|
|
164
|
+
state->selection_direction = 1;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
bool delete_by(Node *node, int direction) {
|
|
169
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
170
|
+
if (state == nullptr) return false;
|
|
171
|
+
if (selection_length(state) > 0) {
|
|
172
|
+
replace_range(node, state->selection_start, state->selection_end, "");
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
size_t caret = (size_t)state->selection_start;
|
|
176
|
+
size_t other = direction < 0 ? xtgui::xt_form_utf16_prev(state->value, caret)
|
|
177
|
+
: xtgui::xt_form_utf16_next(state->value, caret);
|
|
178
|
+
if (other == caret) return false;
|
|
179
|
+
size_t begin = direction < 0 ? other : caret;
|
|
180
|
+
size_t end = direction < 0 ? caret : other;
|
|
181
|
+
replace_range(node, (int)begin, (int)end, "");
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
void select_all(Node *node) {
|
|
186
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
187
|
+
if (state == nullptr) return;
|
|
188
|
+
int length = (int)xtgui::xt_form_utf16_length(state->value);
|
|
189
|
+
state->selection_start = 0;
|
|
190
|
+
state->selection_end = length;
|
|
191
|
+
state->selection_direction = 1;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
void copy_selection(Node *node, bool cut) {
|
|
195
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
196
|
+
if (state == nullptr || state->selection_end <= state->selection_start) return;
|
|
197
|
+
std::string selected =
|
|
198
|
+
xtgui::xt_form_utf16_slice(state->value, (size_t)state->selection_start,
|
|
199
|
+
(size_t)state->selection_end);
|
|
200
|
+
SDL_SetClipboardText(selected.c_str());
|
|
201
|
+
if (cut) replace_range(node, state->selection_start, state->selection_end, "");
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
void paste_clipboard(XtGuiWindow *win, Node *node) {
|
|
205
|
+
char *text = SDL_GetClipboardText();
|
|
206
|
+
if (text == nullptr) return;
|
|
207
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
208
|
+
if (state != nullptr && !before_input(win, node, text, "insertFromPaste")) {
|
|
209
|
+
replace_range(node, state->selection_start, state->selection_end, text);
|
|
210
|
+
xtgui::formdetail::dirty(win);
|
|
211
|
+
xtgui::formdetail::emit_input(win, node, text, "insertFromPaste");
|
|
212
|
+
}
|
|
213
|
+
SDL_free(text);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
} // namespace
|
|
217
|
+
bool xt_gui_set_focus(XtGuiWindow *win, xtgui::Node *node) {
|
|
218
|
+
if (win == nullptr || win->document == nullptr) return false;
|
|
219
|
+
Node *old = const_cast<Node *>(win->document->focus());
|
|
220
|
+
if (old == node) return false;
|
|
221
|
+
if (old != nullptr) {
|
|
222
|
+
FormState *old_state = xtgui::xt_form_state(old);
|
|
223
|
+
if (old_state != nullptr) {
|
|
224
|
+
/* `change` for a text field fires when focus leaves and the value differs
|
|
225
|
+
* from the value it had when focus arrived (the browser's rule). */
|
|
226
|
+
if (xtgui::formdetail::editable(old) && old_state->focus_value_valid &&
|
|
227
|
+
old_state->value != old_state->focus_value) {
|
|
228
|
+
xtgui::formdetail::emit_change(win, old);
|
|
229
|
+
}
|
|
230
|
+
old_state->focus_value_valid = false;
|
|
231
|
+
old_state->focused = false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
bool changed = win->document->setFocus(node);
|
|
235
|
+
if (node != nullptr) {
|
|
236
|
+
FormState *new_state = xtgui::xt_form_state(node);
|
|
237
|
+
if (new_state != nullptr) {
|
|
238
|
+
new_state->focused = true;
|
|
239
|
+
if (xtgui::formdetail::editable(node)) {
|
|
240
|
+
new_state->focus_value = new_state->value;
|
|
241
|
+
new_state->focus_value_valid = true;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (win->window != nullptr) {
|
|
246
|
+
if (xtgui::formdetail::editable(node)) {
|
|
247
|
+
SDL_StartTextInput(win->window);
|
|
248
|
+
} else {
|
|
249
|
+
SDL_StopTextInput(win->window);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/* The caret, focus ring and `:focus` style all change even when the sheet has
|
|
253
|
+
* no `:focus` rule (`XtDocument::setFocus` then returns false). */
|
|
254
|
+
xtgui::formdetail::dirty(win);
|
|
255
|
+
win->present_dirty = 1;
|
|
256
|
+
return changed || node != nullptr;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
void xt_gui_text_input(XtGuiWindow *win, const char *text) {
|
|
260
|
+
Node *node = nullptr;
|
|
261
|
+
FormState *state = focused_state(win, &node);
|
|
262
|
+
if (state == nullptr || text == nullptr) return;
|
|
263
|
+
std::string produced(text);
|
|
264
|
+
if (produced.empty()) return;
|
|
265
|
+
if (state->composing) {
|
|
266
|
+
commit_composition(win, node, produced);
|
|
267
|
+
xtgui::formdetail::dirty(win);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (emit_keypress(win, node, produced)) return;
|
|
271
|
+
if (produced == "\n" || produced == "\r" || produced == "\r\n") {
|
|
272
|
+
if (!xtgui::xt_form_is_textarea(node)) return;
|
|
273
|
+
produced = "\n";
|
|
274
|
+
}
|
|
275
|
+
if (before_input(win, node, produced, "insertText")) return;
|
|
276
|
+
int start = state->selection_start;
|
|
277
|
+
int end = state->selection_end;
|
|
278
|
+
replace_range(node, start, end, produced);
|
|
279
|
+
xtgui::formdetail::dirty(win);
|
|
280
|
+
xtgui::formdetail::emit_input(win, node, produced, "insertText");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
void xt_gui_text_editing(XtGuiWindow *win, const char *text, int start, int length) {
|
|
284
|
+
(void)start;
|
|
285
|
+
(void)length;
|
|
286
|
+
Node *node = nullptr;
|
|
287
|
+
FormState *state = focused_state(win, &node);
|
|
288
|
+
if (state == nullptr || text == nullptr) return;
|
|
289
|
+
if (!state->composing) {
|
|
290
|
+
state->composing = true;
|
|
291
|
+
state->composition_start = state->selection_start;
|
|
292
|
+
state->composition_end = state->selection_end;
|
|
293
|
+
emit_composition(win, node, "compositionstart", text);
|
|
294
|
+
}
|
|
295
|
+
state->composition = text;
|
|
296
|
+
emit_composition(win, node, "compositionupdate", text);
|
|
297
|
+
xtgui::formdetail::dirty(win);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
void xt_gui_composition_event(XtGuiWindow *win, const char *type, const char *data) {
|
|
301
|
+
Node *node = nullptr;
|
|
302
|
+
FormState *state = focused_state(win, &node);
|
|
303
|
+
if (state == nullptr) return;
|
|
304
|
+
std::string name = type != nullptr ? type : "";
|
|
305
|
+
std::string text = data != nullptr ? data : "";
|
|
306
|
+
if (name == "compositionstart") {
|
|
307
|
+
if (!state->composing) {
|
|
308
|
+
state->composing = true;
|
|
309
|
+
state->composition_start = state->selection_start;
|
|
310
|
+
state->composition_end = state->selection_end;
|
|
311
|
+
}
|
|
312
|
+
state->composition = text;
|
|
313
|
+
emit_composition(win, node, "compositionstart", text);
|
|
314
|
+
xtgui::formdetail::dirty(win);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (name == "compositionupdate") {
|
|
318
|
+
if (!state->composing) return;
|
|
319
|
+
state->composition = text;
|
|
320
|
+
emit_composition(win, node, "compositionupdate", text);
|
|
321
|
+
xtgui::formdetail::dirty(win);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
if (name == "compositionend") {
|
|
325
|
+
if (text.empty()) text = state->composition;
|
|
326
|
+
commit_composition(win, node, text);
|
|
327
|
+
xtgui::formdetail::dirty(win);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
void xt_gui_form_key_down(XtGuiWindow *win, xt_value payload) {
|
|
332
|
+
Node *node = nullptr;
|
|
333
|
+
FormState *state = focused_state(win, &node);
|
|
334
|
+
if (state == nullptr) return;
|
|
335
|
+
auto field = [payload](const char *name) { return xt_object_get_cstr(payload, name); };
|
|
336
|
+
auto text_field = [&field](const char *name) {
|
|
337
|
+
xt_value value = field(name);
|
|
338
|
+
if (!XT_IS_STRING(value)) return std::string();
|
|
339
|
+
const char *data = xt_string_data(value);
|
|
340
|
+
return data != nullptr ? std::string(data) : std::string();
|
|
341
|
+
};
|
|
342
|
+
std::string key = text_field("key");
|
|
343
|
+
if (key.empty()) return;
|
|
344
|
+
bool ctrl = xt_truthy(field("ctrlKey")) || xt_truthy(field("ctrl"));
|
|
345
|
+
bool meta = xt_truthy(field("metaKey")) || xt_truthy(field("meta"));
|
|
346
|
+
bool shift = xt_truthy(field("shiftKey")) || xt_truthy(field("shift"));
|
|
347
|
+
bool alt = xt_truthy(field("altKey")) || xt_truthy(field("alt"));
|
|
348
|
+
bool physical = xt_truthy(field("__xt_gui_physical"));
|
|
349
|
+
|
|
350
|
+
if (ctrl || meta) {
|
|
351
|
+
std::string lower = key;
|
|
352
|
+
for (char &c : lower) c = (char)std::tolower((unsigned char)c);
|
|
353
|
+
if (lower == "a") {
|
|
354
|
+
select_all(node);
|
|
355
|
+
xtgui::formdetail::dirty(win);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (lower == "c") {
|
|
359
|
+
copy_selection(node, false);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (lower == "x") {
|
|
363
|
+
if (before_input(win, node, "", "deleteByCut")) return;
|
|
364
|
+
copy_selection(node, true);
|
|
365
|
+
xtgui::formdetail::dirty(win);
|
|
366
|
+
xtgui::formdetail::emit_input(win, node, "", "deleteByCut");
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
if (lower == "v") {
|
|
370
|
+
paste_clipboard(win, node);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
int caret = state->selection_start;
|
|
377
|
+
int end = state->selection_end;
|
|
378
|
+
if (key == "Backspace") {
|
|
379
|
+
if (before_input(win, node, "", "deleteContentBackward")) return;
|
|
380
|
+
if (delete_by(node, -1)) {
|
|
381
|
+
xtgui::formdetail::dirty(win);
|
|
382
|
+
xtgui::formdetail::emit_input(win, node, "", "deleteContentBackward");
|
|
383
|
+
}
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (key == "Delete") {
|
|
387
|
+
if (before_input(win, node, "", "deleteContentForward")) return;
|
|
388
|
+
if (delete_by(node, 1)) {
|
|
389
|
+
xtgui::formdetail::dirty(win);
|
|
390
|
+
xtgui::formdetail::emit_input(win, node, "", "deleteContentForward");
|
|
391
|
+
}
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if (key == "ArrowLeft") {
|
|
395
|
+
size_t target = selection_length(state) > 0 && !shift
|
|
396
|
+
? (size_t)state->selection_start
|
|
397
|
+
: xtgui::xt_form_utf16_prev(state->value, (size_t)caret);
|
|
398
|
+
move_caret(node, (int)target, shift);
|
|
399
|
+
xtgui::formdetail::dirty(win);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (key == "ArrowRight") {
|
|
403
|
+
size_t target = selection_length(state) > 0 && !shift
|
|
404
|
+
? (size_t)state->selection_end
|
|
405
|
+
: xtgui::xt_form_utf16_next(state->value, (size_t)end);
|
|
406
|
+
move_caret(node, (int)target, shift);
|
|
407
|
+
xtgui::formdetail::dirty(win);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (key == "Home" || key == "End") {
|
|
411
|
+
int target = key == "Home" ? 0 : (int)xtgui::xt_form_utf16_length(state->value);
|
|
412
|
+
move_caret(node, target, shift);
|
|
413
|
+
xtgui::formdetail::dirty(win);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (key == "Enter") {
|
|
417
|
+
if (xtgui::xt_form_is_textarea(node)) {
|
|
418
|
+
if (emit_keypress(win, node, "\n")) return;
|
|
419
|
+
if (before_input(win, node, "\n", "insertLineBreak")) return;
|
|
420
|
+
replace_range(node, caret, end, "\n");
|
|
421
|
+
xtgui::formdetail::dirty(win);
|
|
422
|
+
xtgui::formdetail::emit_input(win, node, "\n", "insertLineBreak");
|
|
423
|
+
} else {
|
|
424
|
+
/* Enter in a single-line field commits the value and submits its form. */
|
|
425
|
+
if (state->focus_value_valid && state->value != state->focus_value) {
|
|
426
|
+
xtgui::formdetail::emit_change(win, node);
|
|
427
|
+
state->focus_value = state->value;
|
|
428
|
+
}
|
|
429
|
+
Node *form = xtgui::formdetail::form_ancestor(node);
|
|
430
|
+
if (form != nullptr) xtgui::formdetail::submit_form(win, form, nullptr);
|
|
431
|
+
}
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (key == "Tab") return; /* Tab order is handled by the focus pass. */
|
|
435
|
+
|
|
436
|
+
if (physical) return; /* SDL sends the produced text separately. */
|
|
437
|
+
std::string produced = text_field("text");
|
|
438
|
+
if (produced.empty() && !alt) {
|
|
439
|
+
size_t units = xtgui::xt_form_utf16_length(key);
|
|
440
|
+
if (units >= 1 && key != "Escape" && key != "ArrowUp" && key != "ArrowDown") produced = key;
|
|
441
|
+
}
|
|
442
|
+
if (produced.empty()) return;
|
|
443
|
+
if (emit_keypress(win, node, produced)) return;
|
|
444
|
+
if (before_input(win, node, produced, "insertText")) return;
|
|
445
|
+
replace_range(node, caret, end, produced);
|
|
446
|
+
xtgui::formdetail::dirty(win);
|
|
447
|
+
xtgui::formdetail::emit_input(win, node, produced, "insertText");
|
|
448
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — helpers shared by the form input (`form_input.cpp`) and
|
|
3
|
+
* focus/activation (`form_focus.cpp`) units.
|
|
4
|
+
*/
|
|
5
|
+
#ifndef XT_GUI_FORM_INTERNAL_H
|
|
6
|
+
#define XT_GUI_FORM_INTERNAL_H
|
|
7
|
+
|
|
8
|
+
#include <string>
|
|
9
|
+
|
|
10
|
+
#include "gui_engine.h"
|
|
11
|
+
|
|
12
|
+
namespace xtgui {
|
|
13
|
+
namespace formdetail {
|
|
14
|
+
|
|
15
|
+
xt_value make_payload(const char *type);
|
|
16
|
+
void dirty(XtGuiWindow *win);
|
|
17
|
+
void emit_input(XtGuiWindow *win, Node *node, const std::string &data, const char *input_type);
|
|
18
|
+
void emit_change(XtGuiWindow *win, Node *node);
|
|
19
|
+
bool editable(const Node *node);
|
|
20
|
+
/** UTF-16 caret index nearest to the viewport x coordinate. */
|
|
21
|
+
int caret_at_x(XtGuiWindow *win, Node *node, float x);
|
|
22
|
+
Node *form_ancestor(Node *node);
|
|
23
|
+
void restyle(XtGuiWindow *win);
|
|
24
|
+
void reset_controls(XtGuiWindow *win, Node *form);
|
|
25
|
+
bool submit_form(XtGuiWindow *win, Node *form, Node *submitter);
|
|
26
|
+
|
|
27
|
+
} // namespace formdetail
|
|
28
|
+
} // namespace xtgui
|
|
29
|
+
|
|
30
|
+
#endif /* XT_GUI_FORM_INTERNAL_H */
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — form helpers shared by text input and activation.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#include "form_internal.h"
|
|
6
|
+
|
|
7
|
+
#include <cmath>
|
|
8
|
+
#include <cstring>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <string>
|
|
11
|
+
|
|
12
|
+
#include "form_state.h"
|
|
13
|
+
#include "layout.h"
|
|
14
|
+
|
|
15
|
+
namespace xtgui {
|
|
16
|
+
namespace formdetail {
|
|
17
|
+
|
|
18
|
+
xt_value make_payload(const char *type) {
|
|
19
|
+
xt_value payload = xt_object_new();
|
|
20
|
+
xt_object_set(payload, xt_string_from_cstr("type"), xt_string_from_cstr(type));
|
|
21
|
+
return payload;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
void dirty(XtGuiWindow *win) {
|
|
25
|
+
if (win == nullptr) return;
|
|
26
|
+
win->geometry.dirty = 1;
|
|
27
|
+
win->present_dirty = 1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
void emit_input(XtGuiWindow *win, Node *node, const std::string &data, const char *input_type) {
|
|
31
|
+
xt_value payload = make_payload("input");
|
|
32
|
+
xt_object_set(payload, xt_string_from_cstr("data"), xt_string_from_cstr(data.c_str()));
|
|
33
|
+
xt_object_set(payload, xt_string_from_cstr("inputType"), xt_string_from_cstr(input_type));
|
|
34
|
+
xt_object_set(payload, xt_string_from_cstr("value"),
|
|
35
|
+
xt_string_from_cstr(xt_form_value(node).c_str()));
|
|
36
|
+
xt_object_set(payload, xt_string_from_cstr("isComposing"), XT_FALSE);
|
|
37
|
+
xt_gui_emit_dom_event(win, "input", node, payload);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
void emit_change(XtGuiWindow *win, Node *node) {
|
|
41
|
+
xt_value payload = make_payload("change");
|
|
42
|
+
xt_object_set(payload, xt_string_from_cstr("value"),
|
|
43
|
+
xt_string_from_cstr(xt_form_value(node).c_str()));
|
|
44
|
+
xt_object_set(payload, xt_string_from_cstr("checked"), xt_form_checked(node) ? XT_TRUE : XT_FALSE);
|
|
45
|
+
xt_gui_emit_dom_event(win, "change", node, payload);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
bool editable(const Node *node) {
|
|
49
|
+
return node != nullptr && xt_form_is_text_like(node) && !xt_form_is_disabled(node) &&
|
|
50
|
+
!xt_form_is_readonly(node);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
int caret_at_x(XtGuiWindow *win, Node *node, float x) {
|
|
54
|
+
if (win == nullptr || win->document == nullptr) return 0;
|
|
55
|
+
xt_gui_flush_dom(win);
|
|
56
|
+
const LayoutBox *box = win->document->boxOf(node);
|
|
57
|
+
const XtStyle *style = win->document->styleOf(node);
|
|
58
|
+
if (box == nullptr || style == nullptr) return 0;
|
|
59
|
+
float local = x + win->scroll_x - box->content_x;
|
|
60
|
+
if (local <= 0.0f) return 0;
|
|
61
|
+
const FormState *state = xt_form_state(node);
|
|
62
|
+
if (state == nullptr) return 0;
|
|
63
|
+
std::string display = state->value;
|
|
64
|
+
if (xt_form_type(node) == "password") display = xt_form_mask(display);
|
|
65
|
+
size_t length = xt_form_utf16_length(state->value);
|
|
66
|
+
size_t best = 0;
|
|
67
|
+
float best_distance = std::fabs(local);
|
|
68
|
+
for (size_t index = 0; index <= length; index++) {
|
|
69
|
+
std::string prefix = xt_form_utf16_slice(display, 0, index);
|
|
70
|
+
float width = xt_layout_text_width(prefix, *style);
|
|
71
|
+
float distance = std::fabs(local - width);
|
|
72
|
+
if (distance < best_distance) {
|
|
73
|
+
best_distance = distance;
|
|
74
|
+
best = index;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return (int)best;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
Node *form_ancestor(Node *node) {
|
|
81
|
+
for (Node *parent = node != nullptr ? node->parent : nullptr; parent != nullptr;
|
|
82
|
+
parent = parent->parent) {
|
|
83
|
+
if (parent->isElement() && parent->tag == "form") return parent;
|
|
84
|
+
}
|
|
85
|
+
return nullptr;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
void restyle(XtGuiWindow *win) {
|
|
89
|
+
if (win == nullptr) return;
|
|
90
|
+
win->struct_dirty = 1;
|
|
91
|
+
if (win->document != nullptr) win->document->invalidate();
|
|
92
|
+
win->geometry.dirty = 1;
|
|
93
|
+
win->present_dirty = 1;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
void reset_controls(XtGuiWindow *win, Node *form) {
|
|
97
|
+
if (form == nullptr) return;
|
|
98
|
+
if (xt_form_is_control(form)) {
|
|
99
|
+
if (form->form) {
|
|
100
|
+
form->form->initialized = false;
|
|
101
|
+
form->form->composing = false;
|
|
102
|
+
form->form->composition.clear();
|
|
103
|
+
}
|
|
104
|
+
xt_form_init(form);
|
|
105
|
+
if (form->form) {
|
|
106
|
+
form->form->selection_start = (int)xt_form_utf16_length(form->form->value);
|
|
107
|
+
form->form->selection_end = form->form->selection_start;
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
for (const std::unique_ptr<Node> &child : form->children) reset_controls(win, child.get());
|
|
112
|
+
if (form->tag == "form") restyle(win);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
bool submit_form(XtGuiWindow *win, Node *form, Node *submitter) {
|
|
116
|
+
if (form == nullptr || !form->isElement() || form->tag != "form") return false;
|
|
117
|
+
xt_value payload = make_payload("submit");
|
|
118
|
+
if (submitter != nullptr) {
|
|
119
|
+
xt_object_set(payload, xt_string_from_cstr("submitter"), xt_gui_node_handle(win, submitter));
|
|
120
|
+
}
|
|
121
|
+
xt_gui_emit_dom_event(win, "submit", form, payload);
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
} // namespace formdetail
|
|
126
|
+
} // namespace xtgui
|