@ng-native/desktop 0.26.0 → 0.27.0
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 +6 -3
- package/bin/app-config.mjs +1 -0
- package/bin/archive.mjs +166 -0
- package/bin/autolink.mjs +61 -30
- package/bin/cli.mjs +127 -27
- package/bin/host.mjs +255 -58
- package/bin/package-linux.mjs +1394 -0
- package/bin/package.mjs +171 -35
- package/bin/plist.mjs +6 -2
- package/bin/png-icon.mjs +315 -0
- package/bin/prebuilt.mjs +147 -26
- package/bin/toolchain.mjs +258 -19
- package/native/CMakeLists.txt +64 -6
- package/native/appkit/CMakeLists.txt +2 -1
- package/native/appkit/appkit-backend.h +1 -0
- package/native/appkit/appkit-backend.mm +13 -0
- package/native/appkit/header-toolbar.mm +28 -25
- package/native/appkit/host-view.h +10 -0
- package/native/appkit/host-view.mm +146 -40
- package/native/appkit/input-replay.h +6 -0
- package/native/appkit/input-replay.mm +21 -12
- package/native/appkit/main.mm +11 -7
- package/native/appkit/native-controls.mm +13 -1
- package/native/appkit/split-view.mm +7 -13
- package/native/appkit/text-input-field.h +4 -0
- package/native/appkit/text-input-field.mm +20 -1
- package/native/cmake/curl.cmake +79 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +5 -0
- package/native/cmake/libraries/react-native-worklets.cmake +5 -0
- package/native/cmake/linked-modules.cmake +20 -4
- package/native/cmake/react-native.cmake +18 -1
- package/native/core/include/ngn/accent-colors.h +38 -0
- package/native/core/include/ngn/accessibility-relations.h +65 -0
- package/native/core/include/ngn/accessibility.h +26 -0
- package/native/core/include/ngn/animated-frame-loop.h +56 -0
- package/native/core/include/ngn/app-info.h +24 -8
- package/native/core/include/ngn/app-menus.h +14 -1
- package/native/core/include/ngn/app-windows.h +15 -2
- package/native/core/include/ngn/aria-role.h +96 -0
- package/native/core/include/ngn/audio.h +2 -0
- package/native/core/include/ngn/camera.h +9 -0
- package/native/core/include/ngn/control-sizes.h +7 -2
- package/native/core/include/ngn/crash-report.h +31 -0
- package/native/core/include/ngn/curl-network.h +93 -0
- package/native/core/include/ngn/dbus-menu.h +39 -0
- package/native/core/include/ngn/default-menu.h +36 -0
- package/native/core/include/ngn/desktop-expo.h +7 -1
- package/native/core/include/ngn/desktop-modules.h +22 -0
- package/native/core/include/ngn/desktop-props.h +21 -1
- package/native/core/include/ngn/desktop-view-props.h +6 -0
- package/native/core/include/ngn/focus-order.h +10 -0
- package/native/core/include/ngn/header-bar.h +22 -0
- package/native/core/include/ngn/host-os.h +6 -0
- package/native/core/include/ngn/host-reload.h +21 -0
- package/native/core/include/ngn/host-state.h +23 -0
- package/native/core/include/ngn/image-source.h +26 -1
- package/native/core/include/ngn/input-script.h +3 -3
- package/native/core/include/ngn/input.h +4 -1
- package/native/core/include/ngn/launch-frame.h +21 -0
- package/native/core/include/ngn/launch-settings.h +23 -5
- package/native/core/include/ngn/mounting-backend.h +5 -0
- package/native/core/include/ngn/mounting-manager.h +1 -0
- package/native/core/include/ngn/native-controls.h +26 -5
- package/native/core/include/ngn/notification-schedule.h +74 -0
- package/native/core/include/ngn/notifications.h +2 -1
- package/native/core/include/ngn/openssl-update-crypto.h +15 -0
- package/native/core/include/ngn/photo-library.h +60 -20
- package/native/core/include/ngn/posix-locale.h +43 -0
- package/native/core/include/ngn/runtime.h +5 -0
- package/native/core/include/ngn/safe-areas.h +14 -2
- package/native/core/include/ngn/split-view.h +78 -0
- package/native/core/include/ngn/surface-metrics.h +7 -0
- package/native/core/include/ngn/system.h +14 -0
- package/native/core/include/ngn/text-input.h +13 -0
- package/native/core/include/ngn/text-units.h +38 -0
- package/native/core/include/ngn/updates.h +14 -2
- package/native/core/include/ngn/view-box.h +40 -0
- package/native/core/include/ngn/view-transform.h +15 -0
- package/native/core/include/ngn/web-browser.h +46 -0
- package/native/core/src/accent-colors.cpp +88 -0
- package/native/core/src/accessibility-relations.cpp +138 -0
- package/native/core/src/accessibility.cpp +50 -0
- package/native/core/src/animated-frame-loop.cpp +166 -0
- package/native/core/src/app-menus.cpp +26 -9
- package/native/core/src/app-windows.cpp +27 -6
- package/native/core/src/aria-role.cpp +127 -0
- package/native/core/src/control-sizes.cpp +9 -5
- package/native/core/src/crash-report.cpp +196 -0
- package/native/core/src/curl-network.cpp +1562 -0
- package/native/core/src/dbus-menu.cpp +71 -0
- package/native/core/src/default-menu.cpp +134 -0
- package/native/core/src/desktop-view-props.cpp +14 -0
- package/native/core/src/focus-order.cpp +19 -0
- package/native/core/src/header-bar.cpp +20 -0
- package/native/core/src/host-os.cpp +19 -0
- package/native/core/src/host-reload.cpp +45 -0
- package/native/core/src/host-state.cpp +8 -0
- package/native/core/src/image-source.cpp +90 -0
- package/native/core/src/input-dispatcher.cpp +27 -5
- package/native/core/src/input-script.cpp +3 -2
- package/native/core/src/launch-settings.cpp +76 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +6 -1
- package/native/core/src/modules/app-info.cpp +55 -6
- package/native/core/src/modules/app-modules.cpp +1 -1
- package/native/core/src/modules/audio.cpp +4 -2
- package/native/core/src/modules/camera.cpp +8 -11
- package/native/core/src/modules/desktop-expo.cpp +10 -5
- package/native/core/src/modules/desktop-modules.cpp +106 -41
- package/native/core/src/modules/file-photo-picker.cpp +408 -0
- package/native/core/src/modules/notifications.cpp +13 -17
- package/native/core/src/modules/photo-library.cpp +42 -13
- package/native/core/src/modules/web-browser.cpp +89 -0
- package/native/core/src/mounting-manager.cpp +12 -0
- package/native/core/src/native-controls.cpp +42 -1
- package/native/core/src/notification-schedule.cpp +429 -0
- package/native/core/src/openssl-update-crypto.cpp +83 -0
- package/native/core/src/posix-locale.cpp +192 -0
- package/native/core/src/runtime.cpp +30 -1
- package/native/core/src/safe-areas.cpp +19 -4
- package/native/core/src/spawn-rules.cpp +4 -1
- package/native/core/src/split-view.cpp +93 -0
- package/native/core/src/system.cpp +19 -0
- package/native/core/src/text-input.cpp +18 -1
- package/native/core/src/text-units.cpp +99 -0
- package/native/core/src/updates.cpp +76 -17
- package/native/core/src/view-box.cpp +75 -40
- package/native/core/src/view-transform.cpp +28 -0
- package/native/docker/Dockerfile +38 -0
- package/native/docker/Dockerfile.fedora +36 -0
- package/native/docker/glibc-floor.sh +49 -0
- package/native/gtk/CMakeLists.txt +137 -0
- package/native/gtk/accent-colour.cpp +57 -0
- package/native/gtk/accent-colour.h +41 -0
- package/native/gtk/accessibility-settings.cpp +159 -0
- package/native/gtk/accessibility-settings.h +62 -0
- package/native/gtk/accessible-text.cpp +151 -0
- package/native/gtk/accessible-text.h +49 -0
- package/native/gtk/accessible.cpp +306 -0
- package/native/gtk/accessible.h +75 -0
- package/native/gtk/alerts.cpp +267 -0
- package/native/gtk/alerts.h +30 -0
- package/native/gtk/animation-frames.cpp +159 -0
- package/native/gtk/animation-frames.h +20 -0
- package/native/gtk/app-info.cpp +91 -0
- package/native/gtk/app-info.h +25 -0
- package/native/gtk/app-windows.cpp +439 -0
- package/native/gtk/app-windows.h +150 -0
- package/native/gtk/audio.cpp +370 -0
- package/native/gtk/audio.h +54 -0
- package/native/gtk/auth-session.cpp +73 -0
- package/native/gtk/auth-session.h +16 -0
- package/native/gtk/battery.cpp +110 -0
- package/native/gtk/battery.h +19 -0
- package/native/gtk/box-paint.cpp +262 -0
- package/native/gtk/box-paint.h +32 -0
- package/native/gtk/cairo-canvas.cpp +755 -0
- package/native/gtk/cairo-canvas.h +49 -0
- package/native/gtk/camera.cpp +909 -0
- package/native/gtk/camera.h +69 -0
- package/native/gtk/capture.cpp +46 -0
- package/native/gtk/capture.h +19 -0
- package/native/gtk/clock-format.cpp +151 -0
- package/native/gtk/clock-format.h +36 -0
- package/native/gtk/crypto.cpp +75 -0
- package/native/gtk/crypto.h +14 -0
- package/native/gtk/dbus-properties.cpp +131 -0
- package/native/gtk/dbus-properties.h +61 -0
- package/native/gtk/drag-and-drop.cpp +339 -0
- package/native/gtk/drag-and-drop.h +30 -0
- package/native/gtk/file-dialogs.cpp +239 -0
- package/native/gtk/file-dialogs.h +21 -0
- package/native/gtk/file-system.cpp +212 -0
- package/native/gtk/file-system.h +25 -0
- package/native/gtk/focus.cpp +282 -0
- package/native/gtk/focus.h +37 -0
- package/native/gtk/fonts.cpp +89 -0
- package/native/gtk/fonts.h +22 -0
- package/native/gtk/gst-frames.cpp +150 -0
- package/native/gtk/gst-frames.h +47 -0
- package/native/gtk/gst-playback.cpp +496 -0
- package/native/gtk/gst-playback.h +81 -0
- package/native/gtk/gtk-backend.cpp +690 -0
- package/native/gtk/gtk-backend.h +134 -0
- package/native/gtk/host-sources.cpp +354 -0
- package/native/gtk/host-sources.h +46 -0
- package/native/gtk/image-manipulator.cpp +254 -0
- package/native/gtk/image-manipulator.h +30 -0
- package/native/gtk/image-view.cpp +256 -0
- package/native/gtk/image-view.h +54 -0
- package/native/gtk/input-replay.cpp +299 -0
- package/native/gtk/input-replay.h +36 -0
- package/native/gtk/launcher-badge.cpp +29 -0
- package/native/gtk/launcher-badge.h +21 -0
- package/native/gtk/locales.cpp +131 -0
- package/native/gtk/locales.h +23 -0
- package/native/gtk/location.cpp +274 -0
- package/native/gtk/location.h +19 -0
- package/native/gtk/main.cpp +1092 -0
- package/native/gtk/menus.cpp +447 -0
- package/native/gtk/menus.h +100 -0
- package/native/gtk/native-controls.cpp +831 -0
- package/native/gtk/native-controls.h +58 -0
- package/native/gtk/network-state.cpp +72 -0
- package/native/gtk/network-state.h +24 -0
- package/native/gtk/notifications.cpp +235 -0
- package/native/gtk/notifications.h +30 -0
- package/native/gtk/pango-text.cpp +274 -0
- package/native/gtk/pango-text.h +73 -0
- package/native/gtk/portal.cpp +451 -0
- package/native/gtk/portal.h +71 -0
- package/native/gtk/printing.cpp +338 -0
- package/native/gtk/printing.h +34 -0
- package/native/gtk/proxy-resolver.cpp +74 -0
- package/native/gtk/proxy-resolver.h +20 -0
- package/native/gtk/screens.cpp +1461 -0
- package/native/gtk/screens.h +274 -0
- package/native/gtk/scroll-view.cpp +414 -0
- package/native/gtk/scroll-view.h +82 -0
- package/native/gtk/secret-store.cpp +120 -0
- package/native/gtk/secret-store.h +27 -0
- package/native/gtk/settings-portal.cpp +129 -0
- package/native/gtk/settings-portal.h +47 -0
- package/native/gtk/split-view.cpp +1105 -0
- package/native/gtk/split-view.h +195 -0
- package/native/gtk/status-notifier.cpp +487 -0
- package/native/gtk/status-notifier.h +43 -0
- package/native/gtk/surface-input.cpp +251 -0
- package/native/gtk/surface-input.h +57 -0
- package/native/gtk/system-access.cpp +295 -0
- package/native/gtk/system-access.h +37 -0
- package/native/gtk/text-field.cpp +827 -0
- package/native/gtk/text-field.h +78 -0
- package/native/gtk/video.cpp +527 -0
- package/native/gtk/video.h +47 -0
- package/native/gtk/view-widget.cpp +431 -0
- package/native/gtk/view-widget.h +105 -0
- package/native/gtk/window-scale.cpp +95 -0
- package/native/gtk/window-scale.h +35 -0
- package/native/scripts/atspi-check.py +310 -0
- package/native/scripts/atspi-check.test.py +48 -0
- package/native/scripts/build.sh +20 -3
- package/native/scripts/compilers.sh +17 -0
- package/native/scripts/deb-depends-check.sh +22 -0
- package/native/scripts/fetch.sh +23 -2
- package/native/scripts/launch-cell.sh +157 -0
- package/native/scripts/launch-probe.sh +14 -3
- package/native/scripts/make-prebuilt.mjs +133 -44
- package/native/scripts/prebuilt-host-cache.mjs +71 -40
- package/native/scripts/reset-for-compilers.sh +44 -0
- package/native/scripts/start-xvfb.sh +35 -0
- package/native/shim/paragraph-platform/react/renderer/components/text/HostPlatformParagraphProps.h +35 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +5 -2
- package/native/shim/worklets/linux-reanimated-props.h +14 -0
- package/native/shim/worklets/linux-worklets.h +23 -0
- package/package.json +6 -3
- package/prebuilt.json +70 -6
|
@@ -0,0 +1,827 @@
|
|
|
1
|
+
#include "text-field.h"
|
|
2
|
+
|
|
3
|
+
#include "ngn/text-input.h"
|
|
4
|
+
|
|
5
|
+
#include <react/renderer/components/textinput/TextInputEventEmitter.h>
|
|
6
|
+
|
|
7
|
+
#include <algorithm>
|
|
8
|
+
#include <cstring>
|
|
9
|
+
#include <limits>
|
|
10
|
+
#include <optional>
|
|
11
|
+
|
|
12
|
+
using namespace facebook::react;
|
|
13
|
+
|
|
14
|
+
namespace ngn::gtk {
|
|
15
|
+
|
|
16
|
+
namespace {
|
|
17
|
+
|
|
18
|
+
bool multiline(const TextField &field) {
|
|
19
|
+
return field.scroll != nullptr;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
GtkTextBuffer *bufferOf(const TextField &field) {
|
|
23
|
+
return gtk_text_view_get_buffer(GTK_TEXT_VIEW(field.text));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The selection in characters, the cursor alone as an empty one.
|
|
27
|
+
std::pair<int, int> selectionOf(const TextField &field) {
|
|
28
|
+
int start = 0, end = 0;
|
|
29
|
+
if (multiline(field)) {
|
|
30
|
+
GtkTextIter a, b;
|
|
31
|
+
gtk_text_buffer_get_selection_bounds(bufferOf(field), &a, &b);
|
|
32
|
+
start = gtk_text_iter_get_offset(&a);
|
|
33
|
+
end = gtk_text_iter_get_offset(&b);
|
|
34
|
+
} else if (!gtk_editable_get_selection_bounds(GTK_EDITABLE(field.text), &start, &end)) {
|
|
35
|
+
start = end = gtk_editable_get_position(GTK_EDITABLE(field.text));
|
|
36
|
+
}
|
|
37
|
+
return {std::min(start, end), std::max(start, end)};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Null once the field's view has gone.
|
|
41
|
+
std::shared_ptr<const TextInputEventEmitter> emitterOf(const TextField &field) {
|
|
42
|
+
return field.view ? std::dynamic_pointer_cast<const TextInputEventEmitter>(field.view->emitter) : nullptr;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
TextInputEventEmitter::Metrics metricsOf(const TextField &field) {
|
|
46
|
+
std::string text = fieldText(field);
|
|
47
|
+
auto [start, end] = selectionOf(field);
|
|
48
|
+
int from = utf16Units(text, start);
|
|
49
|
+
int to = utf16Units(text, end);
|
|
50
|
+
TextInputEventEmitter::Metrics m{};
|
|
51
|
+
m.text = text;
|
|
52
|
+
m.selectionRange = {.location = from, .length = to - from};
|
|
53
|
+
m.eventCount = field.eventCount;
|
|
54
|
+
m.target = field.view->tag;
|
|
55
|
+
return m;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
void setText(TextField &field, const std::string &text) {
|
|
59
|
+
field.settingText = true;
|
|
60
|
+
if (multiline(field)) {
|
|
61
|
+
gtk_text_buffer_set_text(bufferOf(field), text.c_str(), static_cast<int>(text.size()));
|
|
62
|
+
} else {
|
|
63
|
+
gtk_editable_set_text(GTK_EDITABLE(field.text), text.c_str());
|
|
64
|
+
}
|
|
65
|
+
field.settingText = false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Selects characters `start` to `end`, the cursor at the end.
|
|
69
|
+
void select(TextField &field, int start, int end) {
|
|
70
|
+
field.settingText = true;
|
|
71
|
+
if (multiline(field)) {
|
|
72
|
+
GtkTextIter a, b;
|
|
73
|
+
gtk_text_buffer_get_iter_at_offset(bufferOf(field), &a, start);
|
|
74
|
+
gtk_text_buffer_get_iter_at_offset(bufferOf(field), &b, end);
|
|
75
|
+
gtk_text_buffer_select_range(bufferOf(field), &b, &a);
|
|
76
|
+
} else {
|
|
77
|
+
gtk_editable_select_region(GTK_EDITABLE(field.text), start, end);
|
|
78
|
+
}
|
|
79
|
+
field.settingText = false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Selects UTF-16 units `start` to `end` of the field's text.
|
|
83
|
+
void selectUnits(TextField &field, int start, int end) {
|
|
84
|
+
std::string text = fieldText(field);
|
|
85
|
+
select(field, charactersIn(text, start), charactersIn(text, end));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
void changed(TextField *field) {
|
|
89
|
+
if (field->settingText || !field->view) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
field->eventCount++;
|
|
93
|
+
if (auto emitter = emitterOf(*field)) {
|
|
94
|
+
emitter->onChange(metricsOf(*field));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
void selectionMoved(TextField *field) {
|
|
99
|
+
if (field->settingText || !field->view) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (auto emitter = emitterOf(*field)) {
|
|
103
|
+
emitter->onSelectionChange(metricsOf(*field));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// A multiline field's placeholder shows or goes with its text and its composition.
|
|
108
|
+
void redrawPlaceholder(const TextField &field) {
|
|
109
|
+
if (field.scroll && field.view && field.view->widget) {
|
|
110
|
+
gtk_widget_queue_draw(field.view->widget);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
void cancelPendingApply(TextField &field) {
|
|
115
|
+
if (field.applyPendingSource) {
|
|
116
|
+
g_source_remove(field.applyPendingSource);
|
|
117
|
+
field.applyPendingSource = 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Applies the held controlled value, unless the user changed the text since it was captured.
|
|
122
|
+
gboolean applyPendingText(gpointer user) {
|
|
123
|
+
auto *field = static_cast<TextField *>(user);
|
|
124
|
+
field->applyPendingSource = 0;
|
|
125
|
+
if (!field->pendingText || field->composing) {
|
|
126
|
+
return G_SOURCE_REMOVE;
|
|
127
|
+
}
|
|
128
|
+
auto pending = std::move(*field->pendingText);
|
|
129
|
+
field->pendingText.reset();
|
|
130
|
+
if (field->view && pending.seen >= field->eventCount) {
|
|
131
|
+
if (pending.text != fieldText(*field)) {
|
|
132
|
+
setText(*field, pending.text);
|
|
133
|
+
}
|
|
134
|
+
if (pending.selection) {
|
|
135
|
+
selectUnits(*field, pending.selection->first, pending.selection->second);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return G_SOURCE_REMOVE;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
void preeditChanged(GtkWidget *, const char *preedit, gpointer user) {
|
|
142
|
+
auto *field = static_cast<TextField *>(user);
|
|
143
|
+
field->composing = preedit && *preedit;
|
|
144
|
+
if (field->composing) {
|
|
145
|
+
cancelPendingApply(*field);
|
|
146
|
+
} else if (field->pendingText && !field->applyPendingSource) {
|
|
147
|
+
// An input method sends an empty preedit and then commits what was composed: apply the held
|
|
148
|
+
// value after the commit, which counts as a user change that makes it stale.
|
|
149
|
+
field->applyPendingSource = g_idle_add(applyPendingText, field);
|
|
150
|
+
}
|
|
151
|
+
redrawPlaceholder(*field);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
void activated(GtkText *, gpointer user) {
|
|
155
|
+
auto *field = static_cast<TextField *>(user);
|
|
156
|
+
if (auto emitter = emitterOf(*field)) {
|
|
157
|
+
emitter->onSubmitEditing(metricsOf(*field));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
void focused(GtkEventControllerFocus *, gpointer user) {
|
|
162
|
+
auto *field = static_cast<TextField *>(user);
|
|
163
|
+
if (auto emitter = emitterOf(*field)) {
|
|
164
|
+
emitter->onFocus(metricsOf(*field));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
void unfocused(GtkEventControllerFocus *, gpointer user) {
|
|
169
|
+
auto *field = static_cast<TextField *>(user);
|
|
170
|
+
if (auto emitter = emitterOf(*field)) {
|
|
171
|
+
auto metrics = metricsOf(*field);
|
|
172
|
+
emitter->onEndEditing(metrics);
|
|
173
|
+
emitter->onBlur(metrics);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Whether a key press is the Latin letter `latin`: by the keyval it produced when that is a Latin
|
|
178
|
+
// letter, so that Ctrl+Y redoes on a German layout whose Y sits where the first group has Z, and
|
|
179
|
+
// otherwise by what its hardware key makes in the first layout group, so that Ctrl+Z still undoes
|
|
180
|
+
// on a Cyrillic or Greek layout.
|
|
181
|
+
bool pressesLatin(GtkWidget *widget, guint keyval, guint keycode, guint latin) {
|
|
182
|
+
guint produced = gdk_keyval_to_lower(keyval);
|
|
183
|
+
if (produced >= GDK_KEY_a && produced <= GDK_KEY_z) {
|
|
184
|
+
return produced == latin;
|
|
185
|
+
}
|
|
186
|
+
GdkKeymapKey *keys = nullptr;
|
|
187
|
+
guint *keyvals = nullptr;
|
|
188
|
+
int count = 0;
|
|
189
|
+
bool found = false;
|
|
190
|
+
if (gdk_display_map_keycode(gtk_widget_get_display(widget), keycode, &keys, &keyvals, &count)) {
|
|
191
|
+
for (int i = 0; i < count && !found; ++i) {
|
|
192
|
+
found = keys[i].group == 0 && gdk_keyval_to_lower(keyvals[i]) == latin;
|
|
193
|
+
}
|
|
194
|
+
g_free(keys);
|
|
195
|
+
g_free(keyvals);
|
|
196
|
+
}
|
|
197
|
+
return found;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// `onKeyPress`, before the key changes the text, with the key as React Native iOS names it.
|
|
201
|
+
gboolean keyPressed(GtkEventControllerKey *, guint keyval, guint keycode, GdkModifierType state, gpointer user) {
|
|
202
|
+
auto *field = static_cast<TextField *>(user);
|
|
203
|
+
// GtkText's own undo and redo keys, run here so that its replay is known (a multiline field's
|
|
204
|
+
// buffer signals its own): Ctrl+Z, Ctrl+Y and Ctrl+Shift+Z, and the Undo and Redo keys GTK 4.18
|
|
205
|
+
// and later bind.
|
|
206
|
+
if (!field->scroll) {
|
|
207
|
+
const char *action = nullptr;
|
|
208
|
+
GdkModifierType modifiers = static_cast<GdkModifierType>(state & (GDK_CONTROL_MASK | GDK_ALT_MASK | GDK_SUPER_MASK));
|
|
209
|
+
if (modifiers == GDK_CONTROL_MASK) {
|
|
210
|
+
bool shift = state & GDK_SHIFT_MASK;
|
|
211
|
+
action = pressesLatin(field->text, keyval, keycode, GDK_KEY_z) ? (shift ? "text.redo" : "text.undo")
|
|
212
|
+
: !shift && pressesLatin(field->text, keyval, keycode, GDK_KEY_y) ? "text.redo" : nullptr;
|
|
213
|
+
} else if (!modifiers && !(state & GDK_SHIFT_MASK)) {
|
|
214
|
+
action = keyval == GDK_KEY_Undo ? "text.undo" : keyval == GDK_KEY_Redo ? "text.redo" : nullptr;
|
|
215
|
+
}
|
|
216
|
+
if (action) {
|
|
217
|
+
field->replaying = true;
|
|
218
|
+
gtk_widget_activate_action(field->text, action, nullptr);
|
|
219
|
+
field->replaying = false;
|
|
220
|
+
return TRUE;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (state & (GDK_CONTROL_MASK | GDK_ALT_MASK | GDK_SUPER_MASK)) {
|
|
224
|
+
return FALSE;
|
|
225
|
+
}
|
|
226
|
+
std::string key;
|
|
227
|
+
if (keyval == GDK_KEY_BackSpace) {
|
|
228
|
+
key = "Backspace";
|
|
229
|
+
} else if (keyval == GDK_KEY_Return || keyval == GDK_KEY_KP_Enter) {
|
|
230
|
+
key = "Enter";
|
|
231
|
+
} else if (gunichar c = gdk_keyval_to_unicode(keyval); c != 0 && g_unichar_isprint(c)) {
|
|
232
|
+
char utf8[8] = {};
|
|
233
|
+
g_unichar_to_utf8(c, utf8);
|
|
234
|
+
key = utf8;
|
|
235
|
+
}
|
|
236
|
+
if (!key.empty()) {
|
|
237
|
+
if (auto emitter = emitterOf(*field)) {
|
|
238
|
+
emitter->onKeyPress({.text = key, .eventCount = field->eventCount});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return FALSE;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
std::shared_ptr<const DesktopTextInputProps> propsOf(const ViewData &view) {
|
|
245
|
+
return std::dynamic_pointer_cast<const DesktopTextInputProps>(view.props);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
std::u16string toUnits(const std::string &utf8) {
|
|
249
|
+
glong n = 0;
|
|
250
|
+
gunichar2 *units = g_utf8_to_utf16(utf8.c_str(), static_cast<glong>(utf8.size()), nullptr, &n, nullptr);
|
|
251
|
+
std::u16string out = units ? std::u16string(reinterpret_cast<const char16_t *>(units), static_cast<size_t>(n)) : std::u16string();
|
|
252
|
+
g_free(units);
|
|
253
|
+
return out;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
std::string toUtf8(const std::u16string &units) {
|
|
257
|
+
gchar *utf8 = g_utf16_to_utf8(reinterpret_cast<const gunichar2 *>(units.data()), static_cast<glong>(units.size()), nullptr, nullptr, nullptr);
|
|
258
|
+
std::string out = utf8 ? utf8 : "";
|
|
259
|
+
g_free(utf8);
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// GLib's capitals, in the user's locale, and its characters: a code point with the combining marks
|
|
264
|
+
// after it.
|
|
265
|
+
struct GlibCharacters final : TextCharacters {
|
|
266
|
+
std::u16string uppercase(const std::u16string &text) const override {
|
|
267
|
+
std::string utf8 = toUtf8(text);
|
|
268
|
+
gchar *upper = g_utf8_strup(utf8.c_str(), static_cast<gssize>(utf8.size()));
|
|
269
|
+
auto out = toUnits(upper);
|
|
270
|
+
g_free(upper);
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
std::pair<std::size_t, std::size_t> characterAt(const std::u16string &text, std::size_t index) const override {
|
|
274
|
+
auto codePoint = [&text](std::size_t at, gunichar &c) -> std::size_t {
|
|
275
|
+
if (at + 1 < text.size() && text[at] >= 0xD800 && text[at] < 0xDC00 && text[at + 1] >= 0xDC00 && text[at + 1] < 0xE000) {
|
|
276
|
+
c = 0x10000 + ((text[at] - 0xD800) << 10) + (text[at + 1] - 0xDC00);
|
|
277
|
+
return 2;
|
|
278
|
+
}
|
|
279
|
+
c = text[at];
|
|
280
|
+
return 1;
|
|
281
|
+
};
|
|
282
|
+
if (index >= text.size()) {
|
|
283
|
+
return {text.size(), 0};
|
|
284
|
+
}
|
|
285
|
+
auto isHigh = [&text](std::size_t at) { return text[at] >= 0xD800 && text[at] < 0xDC00; };
|
|
286
|
+
auto isLow = [&text](std::size_t at) { return text[at] >= 0xDC00 && text[at] < 0xE000; };
|
|
287
|
+
// Back from `index` to the start of its code point, then over the marks before it to their
|
|
288
|
+
// base: only the text around `index` is read, so fitting a long paste one character at a time
|
|
289
|
+
// stays linear.
|
|
290
|
+
std::size_t start = index > 0 && isLow(index) && isHigh(index - 1) ? index - 1 : index;
|
|
291
|
+
gunichar c;
|
|
292
|
+
codePoint(start, c);
|
|
293
|
+
while (start > 0 && g_unichar_ismark(c)) {
|
|
294
|
+
start -= start > 1 && isLow(start - 1) && isHigh(start - 2) ? 2 : 1;
|
|
295
|
+
codePoint(start, c);
|
|
296
|
+
}
|
|
297
|
+
std::size_t end = start + codePoint(start, c);
|
|
298
|
+
while (end < text.size()) {
|
|
299
|
+
std::size_t size = codePoint(end, c);
|
|
300
|
+
if (!g_unichar_ismark(c)) {
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
end += size;
|
|
304
|
+
}
|
|
305
|
+
return {start, end - start};
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
// The text an insertion of `text` at character `position` puts in the field: capitalized as
|
|
310
|
+
// `autoCapitalize` asks and cut to `maxLength` (text-input.h). Nothing when it goes in as it is.
|
|
311
|
+
std::optional<std::string> fittedInsertion(const TextField &field, const char *text, int length, int position) {
|
|
312
|
+
auto props = field.view ? propsOf(*field.view) : nullptr;
|
|
313
|
+
if (field.settingText || field.replaying || !props) {
|
|
314
|
+
return std::nullopt;
|
|
315
|
+
}
|
|
316
|
+
std::string inserted(text, length < 0 ? strlen(text) : static_cast<size_t>(length));
|
|
317
|
+
std::string current = fieldText(field);
|
|
318
|
+
auto location = static_cast<std::size_t>(utf16Units(current, position));
|
|
319
|
+
auto fitted = toUtf8(fitReplacement(toUnits(current), location, 0, toUnits(inserted), textInputOptions(*props), GlibCharacters{}));
|
|
320
|
+
if (fitted == inserted) {
|
|
321
|
+
return std::nullopt;
|
|
322
|
+
}
|
|
323
|
+
return fitted;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// A multiline field's insertion, as fittedInsertion fits it.
|
|
327
|
+
void insertingText(GtkTextBuffer *buffer, GtkTextIter *at, const char *text, int length, gpointer user) {
|
|
328
|
+
auto *field = static_cast<TextField *>(user);
|
|
329
|
+
auto fitted = fittedInsertion(*field, text, length, gtk_text_iter_get_offset(at));
|
|
330
|
+
if (!fitted) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
g_signal_stop_emission_by_name(buffer, "insert-text");
|
|
334
|
+
if (!fitted->empty()) {
|
|
335
|
+
g_signal_handlers_block_by_func(buffer, reinterpret_cast<gpointer>(insertingText), user);
|
|
336
|
+
gtk_text_buffer_insert(buffer, at, fitted->c_str(), static_cast<int>(fitted->size()));
|
|
337
|
+
g_signal_handlers_unblock_by_func(buffer, reinterpret_cast<gpointer>(insertingText), user);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// A single line's insertion, as fittedInsertion fits it.
|
|
342
|
+
void insertingLine(GtkEditable *editable, const char *text, int length, int *position, gpointer user) {
|
|
343
|
+
auto *field = static_cast<TextField *>(user);
|
|
344
|
+
auto fitted = fittedInsertion(*field, text, length, *position);
|
|
345
|
+
if (!fitted) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
g_signal_stop_emission_by_name(editable, "insert-text");
|
|
349
|
+
if (!fitted->empty()) {
|
|
350
|
+
g_signal_handlers_block_by_func(editable, reinterpret_cast<gpointer>(insertingLine), user);
|
|
351
|
+
gtk_editable_insert_text(editable, fitted->c_str(), static_cast<int>(fitted->size()), position);
|
|
352
|
+
g_signal_handlers_unblock_by_func(editable, reinterpret_cast<gpointer>(insertingLine), user);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// What `autoCapitalize` tells an input method, an on-screen keyboard's shift among them.
|
|
357
|
+
GtkInputHints capitalizeHints(Capitalize capitalize) {
|
|
358
|
+
switch (capitalize) {
|
|
359
|
+
case Capitalize::Characters: return GTK_INPUT_HINT_UPPERCASE_CHARS;
|
|
360
|
+
case Capitalize::Words: return GTK_INPUT_HINT_UPPERCASE_WORDS;
|
|
361
|
+
case Capitalize::Sentences: return GTK_INPUT_HINT_UPPERCASE_SENTENCES;
|
|
362
|
+
case Capitalize::None: break;
|
|
363
|
+
}
|
|
364
|
+
return GTK_INPUT_HINT_NONE;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// The label GtkText draws its placeholder with, its child named "placeholder".
|
|
368
|
+
GtkWidget *placeholderLabel(GtkWidget *text) {
|
|
369
|
+
for (GtkWidget *child = gtk_widget_get_first_child(text); child; child = gtk_widget_get_next_sibling(child)) {
|
|
370
|
+
if (GTK_IS_LABEL(child) && g_strcmp0(gtk_widget_get_css_name(child), "placeholder") == 0) {
|
|
371
|
+
return child;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return nullptr;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// How opaque a placeholder with no placeholderTextColor is, over the theme's text colour.
|
|
378
|
+
constexpr float kPlaceholderAlpha = 0.5f;
|
|
379
|
+
|
|
380
|
+
// The run of text attributes a TextInput's text takes, its font and colour, or its placeholder
|
|
381
|
+
// takes, its font and placeholderTextColor.
|
|
382
|
+
PangoAttrList *textAttributes(const DesktopTextInputProps &props, bool placeholder = false) {
|
|
383
|
+
auto attributes = props.getEffectiveTextAttributes(1);
|
|
384
|
+
PangoAttrList *list = pango_attr_list_new();
|
|
385
|
+
PangoFontDescription *font = fontOf(attributes);
|
|
386
|
+
pango_attr_list_insert(list, pango_attr_font_desc_new(font));
|
|
387
|
+
pango_font_description_free(font);
|
|
388
|
+
SharedColor color = placeholder ? props.placeholderTextColor : attributes.foregroundColor;
|
|
389
|
+
auto channel = [](float v) { return static_cast<guint16>(std::clamp(v, 0.0f, 1.0f) * 65535); };
|
|
390
|
+
if (color) {
|
|
391
|
+
auto c = colorComponentsFromColor(color);
|
|
392
|
+
pango_attr_list_insert(list, pango_attr_foreground_new(channel(c.red), channel(c.green), channel(c.blue)));
|
|
393
|
+
pango_attr_list_insert(list, pango_attr_foreground_alpha_new(channel(c.alpha)));
|
|
394
|
+
}
|
|
395
|
+
return list;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// -1 when the first letter of `text` is of a right-to-left script, 1 when of any other, 0 with none.
|
|
399
|
+
int firstStrongDirection(const char *text) {
|
|
400
|
+
for (const char *p = text; *p; p = g_utf8_next_char(p)) {
|
|
401
|
+
gunichar c = g_utf8_get_char(p);
|
|
402
|
+
if (!g_unichar_isalpha(c)) {
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
switch (g_unichar_get_script(c)) {
|
|
406
|
+
case G_UNICODE_SCRIPT_HEBREW:
|
|
407
|
+
case G_UNICODE_SCRIPT_ARABIC:
|
|
408
|
+
case G_UNICODE_SCRIPT_SYRIAC:
|
|
409
|
+
case G_UNICODE_SCRIPT_THAANA:
|
|
410
|
+
case G_UNICODE_SCRIPT_NKO:
|
|
411
|
+
case G_UNICODE_SCRIPT_SAMARITAN:
|
|
412
|
+
case G_UNICODE_SCRIPT_MANDAIC:
|
|
413
|
+
return -1;
|
|
414
|
+
default: return 1;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return 0;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// GTK resolves a left or right justification against each paragraph's own direction, but an
|
|
421
|
+
// explicit left or right is physical. A paragraph whose strong direction differs from the field's
|
|
422
|
+
// takes the opposite justification, on a tag over it.
|
|
423
|
+
void alignParagraphs(TextField &field) {
|
|
424
|
+
GtkTextBuffer *buffer = bufferOf(field);
|
|
425
|
+
GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "ngn-flip");
|
|
426
|
+
if (!tag) {
|
|
427
|
+
tag = gtk_text_buffer_create_tag(buffer, "ngn-flip", nullptr);
|
|
428
|
+
}
|
|
429
|
+
GtkTextIter start, end;
|
|
430
|
+
gtk_text_buffer_get_bounds(buffer, &start, &end);
|
|
431
|
+
gtk_text_buffer_remove_tag(buffer, tag, &start, &end);
|
|
432
|
+
GtkJustification justification = gtk_text_view_get_justification(GTK_TEXT_VIEW(field.text));
|
|
433
|
+
if (justification != GTK_JUSTIFY_LEFT && justification != GTK_JUSTIFY_RIGHT) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
g_object_set(tag, "justification", justification == GTK_JUSTIFY_LEFT ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT, nullptr);
|
|
437
|
+
bool fieldRtl = gtk_widget_get_direction(field.text) == GTK_TEXT_DIR_RTL;
|
|
438
|
+
GtkTextIter line = start;
|
|
439
|
+
do {
|
|
440
|
+
GtkTextIter lineEnd = line;
|
|
441
|
+
if (!gtk_text_iter_ends_line(&lineEnd)) {
|
|
442
|
+
gtk_text_iter_forward_to_line_end(&lineEnd);
|
|
443
|
+
}
|
|
444
|
+
gchar *text = gtk_text_iter_get_slice(&line, &lineEnd);
|
|
445
|
+
int direction = firstStrongDirection(text);
|
|
446
|
+
g_free(text);
|
|
447
|
+
if (direction != 0 && (direction < 0) != fieldRtl) {
|
|
448
|
+
gtk_text_buffer_apply_tag(buffer, tag, &line, &lineEnd);
|
|
449
|
+
}
|
|
450
|
+
} while (gtk_text_iter_forward_line(&line));
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// A multiline field's font and colour, on a tag over all of its text.
|
|
454
|
+
void styleTextView(TextField &field, const DesktopTextInputProps &props) {
|
|
455
|
+
GtkTextBuffer *buffer = bufferOf(field);
|
|
456
|
+
GtkTextTag *tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "ngn-text");
|
|
457
|
+
if (!tag) {
|
|
458
|
+
tag = gtk_text_buffer_create_tag(buffer, "ngn-text", nullptr);
|
|
459
|
+
}
|
|
460
|
+
auto attributes = props.getEffectiveTextAttributes(1);
|
|
461
|
+
PangoFontDescription *font = fontOf(attributes);
|
|
462
|
+
g_object_set(tag, "font-desc", font, nullptr);
|
|
463
|
+
pango_font_description_free(font);
|
|
464
|
+
if (attributes.foregroundColor) {
|
|
465
|
+
auto c = colorComponentsFromColor(attributes.foregroundColor);
|
|
466
|
+
GdkRGBA rgba{c.red, c.green, c.blue, c.alpha};
|
|
467
|
+
g_object_set(tag, "foreground-rgba", &rgba, nullptr);
|
|
468
|
+
}
|
|
469
|
+
GtkTextIter start, end;
|
|
470
|
+
gtk_text_buffer_get_bounds(buffer, &start, &end);
|
|
471
|
+
gtk_text_buffer_apply_tag(buffer, tag, &start, &end);
|
|
472
|
+
alignParagraphs(field);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
void restyle(GtkTextBuffer *, gpointer user) {
|
|
476
|
+
auto *field = static_cast<TextField *>(user);
|
|
477
|
+
if (auto props = field->view ? propsOf(*field->view) : nullptr) {
|
|
478
|
+
styleTextView(*field, *props);
|
|
479
|
+
}
|
|
480
|
+
redrawPlaceholder(*field);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// A multiline field's text view and scrolled window draw no background of their own: the view
|
|
484
|
+
// paints the field's.
|
|
485
|
+
void plainTextViews() {
|
|
486
|
+
static bool installed = false;
|
|
487
|
+
if (installed || !gdk_display_get_default()) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
installed = true;
|
|
491
|
+
GtkCssProvider *css = gtk_css_provider_new();
|
|
492
|
+
gtk_css_provider_load_from_data(css, "textview.ngn-plain, textview.ngn-plain > text { background: none; }", -1);
|
|
493
|
+
gtk_style_context_add_provider_for_display(gdk_display_get_default(), GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
494
|
+
g_object_unref(css);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
void watch(TextField *field, GtkWidget *editor) {
|
|
498
|
+
g_object_set_data(G_OBJECT(editor), "ngn-text-field", field);
|
|
499
|
+
g_signal_connect(editor, "preedit-changed", G_CALLBACK(preeditChanged), field);
|
|
500
|
+
// Backspace over a mark Pango lets go of alone deletes its whole character and puts the base
|
|
501
|
+
// back (GtkText and GtkTextBuffer alike): text the field had, not fitted again.
|
|
502
|
+
g_signal_connect_swapped(editor, "backspace", G_CALLBACK(+[](TextField *f) { f->replaying = true; }), field);
|
|
503
|
+
g_signal_connect_data(editor, "backspace", G_CALLBACK(+[](TextField *f) { f->replaying = false; }), field, nullptr,
|
|
504
|
+
static_cast<GConnectFlags>(G_CONNECT_SWAPPED | G_CONNECT_AFTER));
|
|
505
|
+
GtkEventController *focus = gtk_event_controller_focus_new();
|
|
506
|
+
g_signal_connect(focus, "enter", G_CALLBACK(focused), field);
|
|
507
|
+
g_signal_connect(focus, "leave", G_CALLBACK(unfocused), field);
|
|
508
|
+
gtk_widget_add_controller(editor, focus);
|
|
509
|
+
GtkEventController *keys = gtk_event_controller_key_new();
|
|
510
|
+
gtk_event_controller_set_propagation_phase(keys, GTK_PHASE_CAPTURE);
|
|
511
|
+
g_signal_connect(keys, "key-pressed", G_CALLBACK(keyPressed), field);
|
|
512
|
+
gtk_widget_add_controller(editor, keys);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
} // namespace
|
|
516
|
+
|
|
517
|
+
int utf16Units(const std::string &utf8, int characters) {
|
|
518
|
+
int units = 0;
|
|
519
|
+
const char *p = utf8.c_str();
|
|
520
|
+
const char *end = p + utf8.size();
|
|
521
|
+
for (int i = 0; i < characters && p < end; ++i) {
|
|
522
|
+
gunichar c = g_utf8_get_char(p);
|
|
523
|
+
units += c > 0xFFFF ? 2 : 1;
|
|
524
|
+
p = g_utf8_next_char(p);
|
|
525
|
+
}
|
|
526
|
+
return units;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
int charactersIn(const std::string &utf8, int units) {
|
|
530
|
+
int characters = 0;
|
|
531
|
+
const char *p = utf8.c_str();
|
|
532
|
+
const char *end = p + utf8.size();
|
|
533
|
+
while (units > 0 && p < end) {
|
|
534
|
+
gunichar c = g_utf8_get_char(p);
|
|
535
|
+
units -= c > 0xFFFF ? 2 : 1;
|
|
536
|
+
// A unit count that falls inside a surrogate pair keeps the whole character before it.
|
|
537
|
+
if (units < 0) {
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
++characters;
|
|
541
|
+
p = g_utf8_next_char(p);
|
|
542
|
+
}
|
|
543
|
+
return characters;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
std::string fieldText(const TextField &field) {
|
|
547
|
+
if (multiline(field)) {
|
|
548
|
+
GtkTextIter start, end;
|
|
549
|
+
gtk_text_buffer_get_bounds(bufferOf(field), &start, &end);
|
|
550
|
+
gchar *text = gtk_text_buffer_get_text(bufferOf(field), &start, &end, FALSE);
|
|
551
|
+
std::string out = text ? text : "";
|
|
552
|
+
g_free(text);
|
|
553
|
+
return out;
|
|
554
|
+
}
|
|
555
|
+
const char *text = gtk_editable_get_text(GTK_EDITABLE(field.text));
|
|
556
|
+
return text ? text : "";
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
GtkWidget *fieldWidget(const TextField &field) {
|
|
560
|
+
return field.scroll ? field.scroll : field.text;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
void makeTextField(GtkWidget *widget, ViewData &view) {
|
|
564
|
+
auto *field = new TextField();
|
|
565
|
+
field->view = &view;
|
|
566
|
+
auto props = propsOf(view);
|
|
567
|
+
if (props && props->multiline) {
|
|
568
|
+
field->text = gtk_text_view_new();
|
|
569
|
+
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(field->text), GTK_WRAP_WORD_CHAR);
|
|
570
|
+
// The view's border and background are the field's: the text view draws neither.
|
|
571
|
+
plainTextViews();
|
|
572
|
+
gtk_widget_add_css_class(field->text, "ngn-plain");
|
|
573
|
+
field->scroll = gtk_scrolled_window_new();
|
|
574
|
+
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(field->scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
|
|
575
|
+
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(field->scroll), field->text);
|
|
576
|
+
GtkTextBuffer *buffer = bufferOf(*field);
|
|
577
|
+
g_signal_connect_swapped(buffer, "changed", G_CALLBACK(changed), field);
|
|
578
|
+
g_signal_connect_after(buffer, "changed", G_CALLBACK(restyle), field);
|
|
579
|
+
g_signal_connect(buffer, "insert-text", G_CALLBACK(insertingText), field);
|
|
580
|
+
for (const char *signal : {"undo", "redo"}) {
|
|
581
|
+
g_signal_connect_swapped(buffer, signal, G_CALLBACK(+[](TextField *f) { f->replaying = true; }), field);
|
|
582
|
+
g_signal_connect_data(buffer, signal, G_CALLBACK(+[](TextField *f) { f->replaying = false; }), field, nullptr,
|
|
583
|
+
static_cast<GConnectFlags>(G_CONNECT_SWAPPED | G_CONNECT_AFTER));
|
|
584
|
+
}
|
|
585
|
+
// The cursor, whether set or moved by an edit, and the selection's other end, which only a set moves.
|
|
586
|
+
g_signal_connect_swapped(buffer, "notify::cursor-position", G_CALLBACK(selectionMoved), field);
|
|
587
|
+
g_signal_connect(buffer, "mark-set", G_CALLBACK(+[](GtkTextBuffer *buffer, GtkTextIter *, GtkTextMark *mark, gpointer user) {
|
|
588
|
+
if (mark == gtk_text_buffer_get_selection_bound(buffer)) {
|
|
589
|
+
selectionMoved(static_cast<TextField *>(user));
|
|
590
|
+
}
|
|
591
|
+
}), field);
|
|
592
|
+
} else {
|
|
593
|
+
// A GtkText is no accessible element (its role is none: it is made to be the delegate of a
|
|
594
|
+
// GtkEditable that is one, as a GtkEntry is), so it is made a text box, which takes the label
|
|
595
|
+
// and relations and exposes the text.
|
|
596
|
+
field->text = GTK_WIDGET(g_object_new(GTK_TYPE_TEXT, "accessible-role", GTK_ACCESSIBLE_ROLE_TEXT_BOX, nullptr));
|
|
597
|
+
g_signal_connect_swapped(field->text, "changed", G_CALLBACK(changed), field);
|
|
598
|
+
g_signal_connect_swapped(field->text, "notify::cursor-position", G_CALLBACK(selectionMoved), field);
|
|
599
|
+
g_signal_connect_swapped(field->text, "notify::selection-bound", G_CALLBACK(selectionMoved), field);
|
|
600
|
+
g_signal_connect(field->text, "activate", G_CALLBACK(activated), field);
|
|
601
|
+
g_signal_connect(field->text, "insert-text", G_CALLBACK(insertingLine), field);
|
|
602
|
+
}
|
|
603
|
+
watch(field, field->text);
|
|
604
|
+
// Freed with the editor, after its last signal. A multiline field's buffer, which can outlive it
|
|
605
|
+
// (a paste still to arrive), is disconnected from the field when its view goes.
|
|
606
|
+
g_object_set_data_full(G_OBJECT(field->text), "ngn-text-field-owner", field, [](gpointer p) {
|
|
607
|
+
auto *field = static_cast<TextField *>(p);
|
|
608
|
+
cancelPendingApply(*field);
|
|
609
|
+
delete field;
|
|
610
|
+
});
|
|
611
|
+
view.field = field;
|
|
612
|
+
gtk_widget_set_parent(fieldWidget(*field), widget);
|
|
613
|
+
if (props && props->text.empty() && !props->defaultValue.empty()) {
|
|
614
|
+
setText(*field, props->defaultValue);
|
|
615
|
+
}
|
|
616
|
+
applyTextFieldProps(view);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
TextField *textFieldOf(GtkWidget *widget) {
|
|
620
|
+
return static_cast<TextField *>(g_object_get_data(G_OBJECT(widget), "ngn-text-field"));
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
void destroyTextField(ViewData &view) {
|
|
624
|
+
if (view.field) {
|
|
625
|
+
// The editor may still signal as it goes (its focus leaving): the field hears nothing more.
|
|
626
|
+
view.field->view = nullptr;
|
|
627
|
+
if (multiline(*view.field)) {
|
|
628
|
+
g_signal_handlers_disconnect_by_data(bufferOf(*view.field), view.field);
|
|
629
|
+
}
|
|
630
|
+
view.field = nullptr;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
void applyTextFieldProps(ViewData &view) {
|
|
635
|
+
auto props = propsOf(view);
|
|
636
|
+
if (!props || !view.field) {
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
TextField &field = *view.field;
|
|
640
|
+
auto attributes = props->getEffectiveTextAttributes(1);
|
|
641
|
+
bool editable = props->traits.editable && props->editable && !props->readOnly;
|
|
642
|
+
bool rightToLeft = view.layout.layoutDirection == LayoutDirection::RightToLeft;
|
|
643
|
+
float xalign = 0;
|
|
644
|
+
switch (attributes.alignment.value_or(TextAlignment::Natural)) {
|
|
645
|
+
case TextAlignment::Center: xalign = 0.5f; break;
|
|
646
|
+
case TextAlignment::Right: xalign = 1; break;
|
|
647
|
+
case TextAlignment::Left: xalign = 0; break;
|
|
648
|
+
default: xalign = rightToLeft ? 1 : 0; break;
|
|
649
|
+
}
|
|
650
|
+
gtk_widget_set_direction(field.text, rightToLeft ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR);
|
|
651
|
+
if (multiline(field)) {
|
|
652
|
+
GtkTextView *text = GTK_TEXT_VIEW(field.text);
|
|
653
|
+
// Left in a right-to-left field is its start, so the natural alignment is GTK's left.
|
|
654
|
+
gtk_text_view_set_justification(
|
|
655
|
+
text, xalign == 0.5f ? GTK_JUSTIFY_CENTER : (xalign == 1) != rightToLeft ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT);
|
|
656
|
+
styleTextView(field, *props);
|
|
657
|
+
gtk_text_view_set_editable(text, editable);
|
|
658
|
+
gtk_text_view_set_input_hints(text, capitalizeHints(textInputOptions(*props).capitalize));
|
|
659
|
+
gtk_text_view_set_cursor_visible(text, editable);
|
|
660
|
+
} else {
|
|
661
|
+
GtkText *text = GTK_TEXT(field.text);
|
|
662
|
+
gtk_text_set_placeholder_text(text, props->placeholder.c_str());
|
|
663
|
+
PangoAttrList *list = textAttributes(*props);
|
|
664
|
+
gtk_text_set_attributes(text, list);
|
|
665
|
+
pango_attr_list_unref(list);
|
|
666
|
+
// The placeholder in the text's font, and in placeholderTextColor when set, else in the
|
|
667
|
+
// theme's text, dimmed: libadwaita dims a placeholder only inside an entry, and a bare
|
|
668
|
+
// GtkText's would read as typed text. Dimmed as it is drawn, over whatever the theme's text is
|
|
669
|
+
// then, translucent or not, light or dark, and however the theme changes after.
|
|
670
|
+
if (GtkWidget *label = placeholderLabel(field.text)) {
|
|
671
|
+
PangoAttrList *placeholder = textAttributes(*props, true);
|
|
672
|
+
gtk_label_set_attributes(GTK_LABEL(label), placeholder);
|
|
673
|
+
pango_attr_list_unref(placeholder);
|
|
674
|
+
gtk_widget_set_opacity(label, props->placeholderTextColor ? 1 : kPlaceholderAlpha);
|
|
675
|
+
}
|
|
676
|
+
gtk_text_set_input_hints(text, capitalizeHints(textInputOptions(*props).capitalize));
|
|
677
|
+
gtk_editable_set_editable(GTK_EDITABLE(text), editable);
|
|
678
|
+
gtk_text_set_visibility(text, !props->traits.secureTextEntry);
|
|
679
|
+
gtk_text_set_max_length(text, props->maxLength == std::numeric_limits<int>::max() ? 0 : std::max(0, props->maxLength));
|
|
680
|
+
gtk_editable_set_alignment(GTK_EDITABLE(text), xalign);
|
|
681
|
+
}
|
|
682
|
+
// A controlled value JavaScript set, when it has seen every change the user made, held while an
|
|
683
|
+
// input method composes. Once per props: a layout update applies the same props again, which
|
|
684
|
+
// must not undo a setTextAndSelection.
|
|
685
|
+
if (field.applied != view.props) {
|
|
686
|
+
// Stale props, which cannot supersede a held value, leave it alone.
|
|
687
|
+
if (controlledTextIsCurrent(props->mostRecentEventCount, field.eventCount, false) &&
|
|
688
|
+
(!props->text.empty() || props->defaultValue.empty() || field.eventCount > 0)) {
|
|
689
|
+
auto previous = std::dynamic_pointer_cast<const DesktopTextInputProps>(field.applied);
|
|
690
|
+
if (field.pendingText && previous && previous->text == props->text &&
|
|
691
|
+
field.pendingText->seen >= props->mostRecentEventCount) {
|
|
692
|
+
// The text is the same as the props before it, which answer no newer edit: a held
|
|
693
|
+
// setTextAndSelection stays.
|
|
694
|
+
} else if (field.composing) {
|
|
695
|
+
cancelPendingApply(field);
|
|
696
|
+
field.pendingText.emplace(TextField::Pending{props->text, props->mostRecentEventCount, std::nullopt});
|
|
697
|
+
} else {
|
|
698
|
+
field.pendingText.reset();
|
|
699
|
+
cancelPendingApply(field);
|
|
700
|
+
if (props->text != fieldText(field)) {
|
|
701
|
+
setText(field, props->text);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
auto before = std::dynamic_pointer_cast<const DesktopTextInputProps>(field.applied);
|
|
707
|
+
if (auto range = newSelection(before.get(), *props)) {
|
|
708
|
+
// Against the text it accompanies: a held text takes it when it is applied.
|
|
709
|
+
if (field.pendingText) {
|
|
710
|
+
field.pendingText->selection = *range;
|
|
711
|
+
} else {
|
|
712
|
+
selectUnits(field, range->first, range->second);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
field.applied = view.props;
|
|
716
|
+
redrawPlaceholder(field);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
void allocateTextField(ViewData &view, int width, int height) {
|
|
720
|
+
GtkWidget *child = fieldWidget(*view.field);
|
|
721
|
+
const auto &insets = view.layout.contentInsets;
|
|
722
|
+
int cw = std::max(0, static_cast<int>(width - insets.left - insets.right));
|
|
723
|
+
int ch = std::max(0, static_cast<int>(height - insets.top - insets.bottom));
|
|
724
|
+
if (multiline(*view.field)) {
|
|
725
|
+
GtkAllocation all{static_cast<int>(insets.left), static_cast<int>(insets.top), cw, ch};
|
|
726
|
+
gtk_widget_size_allocate(child, &all, -1);
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
int minH = 0, natH = 0;
|
|
730
|
+
gtk_widget_measure(child, GTK_ORIENTATION_VERTICAL, cw, &minH, &natH, nullptr, nullptr);
|
|
731
|
+
int h = std::min(ch, std::max(minH, natH));
|
|
732
|
+
int minW = 0;
|
|
733
|
+
gtk_widget_measure(child, GTK_ORIENTATION_HORIZONTAL, h, &minW, nullptr, nullptr, nullptr);
|
|
734
|
+
GtkAllocation all{static_cast<int>(insets.left), static_cast<int>(insets.top) + (ch - h) / 2, std::max(cw, minW), h};
|
|
735
|
+
gtk_widget_size_allocate(child, &all, -1);
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
void snapshotPlaceholder(const ViewData &view, GtkSnapshot *snapshot) {
|
|
739
|
+
const TextField *field = view.field;
|
|
740
|
+
auto props = propsOf(view);
|
|
741
|
+
if (!field || !multiline(*field) || !props || props->placeholder.empty() || field->composing ||
|
|
742
|
+
gtk_text_buffer_get_char_count(bufferOf(*field)) > 0) {
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
PangoLayout *layout = gtk_widget_create_pango_layout(field->text, props->placeholder.c_str());
|
|
746
|
+
PangoFontDescription *font = fontOf(props->getEffectiveTextAttributes(1));
|
|
747
|
+
pango_layout_set_font_description(layout, font);
|
|
748
|
+
pango_font_description_free(font);
|
|
749
|
+
const auto &insets = view.layout.contentInsets;
|
|
750
|
+
pango_layout_set_width(layout, static_cast<int>(std::max<Float>(0, view.layout.frame.size.width - insets.left - insets.right) * PANGO_SCALE));
|
|
751
|
+
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
|
|
752
|
+
// Aligned as the typed text is.
|
|
753
|
+
switch (gtk_text_view_get_justification(GTK_TEXT_VIEW(field->text))) {
|
|
754
|
+
case GTK_JUSTIFY_CENTER: pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); break;
|
|
755
|
+
case GTK_JUSTIFY_RIGHT: pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); break;
|
|
756
|
+
default: pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT); break;
|
|
757
|
+
}
|
|
758
|
+
// The placeholder colour, or the theme's text dimmed, light or dark.
|
|
759
|
+
GdkRGBA color;
|
|
760
|
+
gtk_widget_get_color(field->text, &color);
|
|
761
|
+
color.alpha *= kPlaceholderAlpha;
|
|
762
|
+
if (props->placeholderTextColor) {
|
|
763
|
+
auto c = colorComponentsFromColor(props->placeholderTextColor);
|
|
764
|
+
color = {c.red, c.green, c.blue, c.alpha};
|
|
765
|
+
}
|
|
766
|
+
// Within the content box, however many lines it wraps to.
|
|
767
|
+
graphene_rect_t box;
|
|
768
|
+
graphene_rect_init(&box, static_cast<float>(insets.left), static_cast<float>(insets.top),
|
|
769
|
+
static_cast<float>(std::max<Float>(0, view.layout.frame.size.width - insets.left - insets.right)),
|
|
770
|
+
static_cast<float>(std::max<Float>(0, view.layout.frame.size.height - insets.top - insets.bottom)));
|
|
771
|
+
gtk_snapshot_push_clip(snapshot, &box);
|
|
772
|
+
gtk_snapshot_save(snapshot);
|
|
773
|
+
graphene_point_t at;
|
|
774
|
+
graphene_point_init(&at, static_cast<float>(insets.left), static_cast<float>(insets.top));
|
|
775
|
+
gtk_snapshot_translate(snapshot, &at);
|
|
776
|
+
gtk_snapshot_append_layout(snapshot, layout, &color);
|
|
777
|
+
gtk_snapshot_restore(snapshot);
|
|
778
|
+
gtk_snapshot_pop(snapshot);
|
|
779
|
+
g_object_unref(layout);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
void textFieldCommand(ViewData &view, const std::string &name, const folly::dynamic &args) {
|
|
783
|
+
if (!view.field) {
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
TextField &field = *view.field;
|
|
787
|
+
if (name == "focus") {
|
|
788
|
+
gtk_widget_grab_focus(field.text);
|
|
789
|
+
} else if (name == "blur") {
|
|
790
|
+
if (GtkRoot *root = gtk_widget_get_root(field.text); root && gtk_widget_has_focus(field.text)) {
|
|
791
|
+
gtk_root_set_focus(root, nullptr);
|
|
792
|
+
}
|
|
793
|
+
} else if (name == "setTextAndSelection" && args.isArray() && args.size() >= 4) {
|
|
794
|
+
// [mostRecentEventCount, value, start, end]: a value only when JavaScript has seen every change,
|
|
795
|
+
// held while an input method composes.
|
|
796
|
+
// The count is a JavaScript number, which can arrive as a double.
|
|
797
|
+
if (args[0].isNumber() && args[0].asDouble() < field.eventCount) {
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
if (args[1].isString() && field.composing) {
|
|
801
|
+
TextField::Pending pending{
|
|
802
|
+
args[1].getString(), args[0].isNumber() ? static_cast<int>(args[0].asDouble()) : field.eventCount, std::nullopt};
|
|
803
|
+
if (args[2].isNumber() && args[3].isNumber()) {
|
|
804
|
+
pending.selection.emplace(static_cast<int>(args[2].asDouble()), static_cast<int>(args[3].asDouble()));
|
|
805
|
+
}
|
|
806
|
+
field.pendingText.emplace(std::move(pending));
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
if (args[1].isString()) {
|
|
810
|
+
field.pendingText.reset();
|
|
811
|
+
cancelPendingApply(field);
|
|
812
|
+
setText(field, args[1].getString());
|
|
813
|
+
}
|
|
814
|
+
if (args[2].isNumber() && args[3].isNumber()) {
|
|
815
|
+
int start = static_cast<int>(args[2].asDouble());
|
|
816
|
+
int end = static_cast<int>(args[3].asDouble());
|
|
817
|
+
if (field.pendingText) {
|
|
818
|
+
// Newer than a range held with the text, which it is applied to.
|
|
819
|
+
field.pendingText->selection.emplace(start, end);
|
|
820
|
+
} else {
|
|
821
|
+
selectUnits(field, start, end);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
} // namespace ngn::gtk
|