@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.
Files changed (256) hide show
  1. package/README.md +6 -3
  2. package/bin/app-config.mjs +1 -0
  3. package/bin/archive.mjs +166 -0
  4. package/bin/autolink.mjs +61 -30
  5. package/bin/cli.mjs +127 -27
  6. package/bin/host.mjs +255 -58
  7. package/bin/package-linux.mjs +1394 -0
  8. package/bin/package.mjs +171 -35
  9. package/bin/plist.mjs +6 -2
  10. package/bin/png-icon.mjs +315 -0
  11. package/bin/prebuilt.mjs +147 -26
  12. package/bin/toolchain.mjs +258 -19
  13. package/native/CMakeLists.txt +64 -6
  14. package/native/appkit/CMakeLists.txt +2 -1
  15. package/native/appkit/appkit-backend.h +1 -0
  16. package/native/appkit/appkit-backend.mm +13 -0
  17. package/native/appkit/header-toolbar.mm +28 -25
  18. package/native/appkit/host-view.h +10 -0
  19. package/native/appkit/host-view.mm +146 -40
  20. package/native/appkit/input-replay.h +6 -0
  21. package/native/appkit/input-replay.mm +21 -12
  22. package/native/appkit/main.mm +11 -7
  23. package/native/appkit/native-controls.mm +13 -1
  24. package/native/appkit/split-view.mm +7 -13
  25. package/native/appkit/text-input-field.h +4 -0
  26. package/native/appkit/text-input-field.mm +20 -1
  27. package/native/cmake/curl.cmake +79 -0
  28. package/native/cmake/libraries/react-native-reanimated.cmake +5 -0
  29. package/native/cmake/libraries/react-native-worklets.cmake +5 -0
  30. package/native/cmake/linked-modules.cmake +20 -4
  31. package/native/cmake/react-native.cmake +18 -1
  32. package/native/core/include/ngn/accent-colors.h +38 -0
  33. package/native/core/include/ngn/accessibility-relations.h +65 -0
  34. package/native/core/include/ngn/accessibility.h +26 -0
  35. package/native/core/include/ngn/animated-frame-loop.h +56 -0
  36. package/native/core/include/ngn/app-info.h +24 -8
  37. package/native/core/include/ngn/app-menus.h +14 -1
  38. package/native/core/include/ngn/app-windows.h +15 -2
  39. package/native/core/include/ngn/aria-role.h +96 -0
  40. package/native/core/include/ngn/audio.h +2 -0
  41. package/native/core/include/ngn/camera.h +9 -0
  42. package/native/core/include/ngn/control-sizes.h +7 -2
  43. package/native/core/include/ngn/crash-report.h +31 -0
  44. package/native/core/include/ngn/curl-network.h +93 -0
  45. package/native/core/include/ngn/dbus-menu.h +39 -0
  46. package/native/core/include/ngn/default-menu.h +36 -0
  47. package/native/core/include/ngn/desktop-expo.h +7 -1
  48. package/native/core/include/ngn/desktop-modules.h +22 -0
  49. package/native/core/include/ngn/desktop-props.h +21 -1
  50. package/native/core/include/ngn/desktop-view-props.h +6 -0
  51. package/native/core/include/ngn/focus-order.h +10 -0
  52. package/native/core/include/ngn/header-bar.h +22 -0
  53. package/native/core/include/ngn/host-os.h +6 -0
  54. package/native/core/include/ngn/host-reload.h +21 -0
  55. package/native/core/include/ngn/host-state.h +23 -0
  56. package/native/core/include/ngn/image-source.h +26 -1
  57. package/native/core/include/ngn/input-script.h +3 -3
  58. package/native/core/include/ngn/input.h +4 -1
  59. package/native/core/include/ngn/launch-frame.h +21 -0
  60. package/native/core/include/ngn/launch-settings.h +23 -5
  61. package/native/core/include/ngn/mounting-backend.h +5 -0
  62. package/native/core/include/ngn/mounting-manager.h +1 -0
  63. package/native/core/include/ngn/native-controls.h +26 -5
  64. package/native/core/include/ngn/notification-schedule.h +74 -0
  65. package/native/core/include/ngn/notifications.h +2 -1
  66. package/native/core/include/ngn/openssl-update-crypto.h +15 -0
  67. package/native/core/include/ngn/photo-library.h +60 -20
  68. package/native/core/include/ngn/posix-locale.h +43 -0
  69. package/native/core/include/ngn/runtime.h +5 -0
  70. package/native/core/include/ngn/safe-areas.h +14 -2
  71. package/native/core/include/ngn/split-view.h +78 -0
  72. package/native/core/include/ngn/surface-metrics.h +7 -0
  73. package/native/core/include/ngn/system.h +14 -0
  74. package/native/core/include/ngn/text-input.h +13 -0
  75. package/native/core/include/ngn/text-units.h +38 -0
  76. package/native/core/include/ngn/updates.h +14 -2
  77. package/native/core/include/ngn/view-box.h +40 -0
  78. package/native/core/include/ngn/view-transform.h +15 -0
  79. package/native/core/include/ngn/web-browser.h +46 -0
  80. package/native/core/src/accent-colors.cpp +88 -0
  81. package/native/core/src/accessibility-relations.cpp +138 -0
  82. package/native/core/src/accessibility.cpp +50 -0
  83. package/native/core/src/animated-frame-loop.cpp +166 -0
  84. package/native/core/src/app-menus.cpp +26 -9
  85. package/native/core/src/app-windows.cpp +27 -6
  86. package/native/core/src/aria-role.cpp +127 -0
  87. package/native/core/src/control-sizes.cpp +9 -5
  88. package/native/core/src/crash-report.cpp +196 -0
  89. package/native/core/src/curl-network.cpp +1562 -0
  90. package/native/core/src/dbus-menu.cpp +71 -0
  91. package/native/core/src/default-menu.cpp +134 -0
  92. package/native/core/src/desktop-view-props.cpp +14 -0
  93. package/native/core/src/focus-order.cpp +19 -0
  94. package/native/core/src/header-bar.cpp +20 -0
  95. package/native/core/src/host-os.cpp +19 -0
  96. package/native/core/src/host-reload.cpp +45 -0
  97. package/native/core/src/host-state.cpp +8 -0
  98. package/native/core/src/image-source.cpp +90 -0
  99. package/native/core/src/input-dispatcher.cpp +27 -5
  100. package/native/core/src/input-script.cpp +3 -2
  101. package/native/core/src/launch-settings.cpp +76 -0
  102. package/native/core/src/libraries/react-native-reanimated.cpp +6 -1
  103. package/native/core/src/modules/app-info.cpp +55 -6
  104. package/native/core/src/modules/app-modules.cpp +1 -1
  105. package/native/core/src/modules/audio.cpp +4 -2
  106. package/native/core/src/modules/camera.cpp +8 -11
  107. package/native/core/src/modules/desktop-expo.cpp +10 -5
  108. package/native/core/src/modules/desktop-modules.cpp +106 -41
  109. package/native/core/src/modules/file-photo-picker.cpp +408 -0
  110. package/native/core/src/modules/notifications.cpp +13 -17
  111. package/native/core/src/modules/photo-library.cpp +42 -13
  112. package/native/core/src/modules/web-browser.cpp +89 -0
  113. package/native/core/src/mounting-manager.cpp +12 -0
  114. package/native/core/src/native-controls.cpp +42 -1
  115. package/native/core/src/notification-schedule.cpp +429 -0
  116. package/native/core/src/openssl-update-crypto.cpp +83 -0
  117. package/native/core/src/posix-locale.cpp +192 -0
  118. package/native/core/src/runtime.cpp +30 -1
  119. package/native/core/src/safe-areas.cpp +19 -4
  120. package/native/core/src/spawn-rules.cpp +4 -1
  121. package/native/core/src/split-view.cpp +93 -0
  122. package/native/core/src/system.cpp +19 -0
  123. package/native/core/src/text-input.cpp +18 -1
  124. package/native/core/src/text-units.cpp +99 -0
  125. package/native/core/src/updates.cpp +76 -17
  126. package/native/core/src/view-box.cpp +75 -40
  127. package/native/core/src/view-transform.cpp +28 -0
  128. package/native/docker/Dockerfile +38 -0
  129. package/native/docker/Dockerfile.fedora +36 -0
  130. package/native/docker/glibc-floor.sh +49 -0
  131. package/native/gtk/CMakeLists.txt +137 -0
  132. package/native/gtk/accent-colour.cpp +57 -0
  133. package/native/gtk/accent-colour.h +41 -0
  134. package/native/gtk/accessibility-settings.cpp +159 -0
  135. package/native/gtk/accessibility-settings.h +62 -0
  136. package/native/gtk/accessible-text.cpp +151 -0
  137. package/native/gtk/accessible-text.h +49 -0
  138. package/native/gtk/accessible.cpp +306 -0
  139. package/native/gtk/accessible.h +75 -0
  140. package/native/gtk/alerts.cpp +267 -0
  141. package/native/gtk/alerts.h +30 -0
  142. package/native/gtk/animation-frames.cpp +159 -0
  143. package/native/gtk/animation-frames.h +20 -0
  144. package/native/gtk/app-info.cpp +91 -0
  145. package/native/gtk/app-info.h +25 -0
  146. package/native/gtk/app-windows.cpp +439 -0
  147. package/native/gtk/app-windows.h +150 -0
  148. package/native/gtk/audio.cpp +370 -0
  149. package/native/gtk/audio.h +54 -0
  150. package/native/gtk/auth-session.cpp +73 -0
  151. package/native/gtk/auth-session.h +16 -0
  152. package/native/gtk/battery.cpp +110 -0
  153. package/native/gtk/battery.h +19 -0
  154. package/native/gtk/box-paint.cpp +262 -0
  155. package/native/gtk/box-paint.h +32 -0
  156. package/native/gtk/cairo-canvas.cpp +755 -0
  157. package/native/gtk/cairo-canvas.h +49 -0
  158. package/native/gtk/camera.cpp +909 -0
  159. package/native/gtk/camera.h +69 -0
  160. package/native/gtk/capture.cpp +46 -0
  161. package/native/gtk/capture.h +19 -0
  162. package/native/gtk/clock-format.cpp +151 -0
  163. package/native/gtk/clock-format.h +36 -0
  164. package/native/gtk/crypto.cpp +75 -0
  165. package/native/gtk/crypto.h +14 -0
  166. package/native/gtk/dbus-properties.cpp +131 -0
  167. package/native/gtk/dbus-properties.h +61 -0
  168. package/native/gtk/drag-and-drop.cpp +339 -0
  169. package/native/gtk/drag-and-drop.h +30 -0
  170. package/native/gtk/file-dialogs.cpp +239 -0
  171. package/native/gtk/file-dialogs.h +21 -0
  172. package/native/gtk/file-system.cpp +212 -0
  173. package/native/gtk/file-system.h +25 -0
  174. package/native/gtk/focus.cpp +282 -0
  175. package/native/gtk/focus.h +37 -0
  176. package/native/gtk/fonts.cpp +89 -0
  177. package/native/gtk/fonts.h +22 -0
  178. package/native/gtk/gst-frames.cpp +150 -0
  179. package/native/gtk/gst-frames.h +47 -0
  180. package/native/gtk/gst-playback.cpp +496 -0
  181. package/native/gtk/gst-playback.h +81 -0
  182. package/native/gtk/gtk-backend.cpp +690 -0
  183. package/native/gtk/gtk-backend.h +134 -0
  184. package/native/gtk/host-sources.cpp +354 -0
  185. package/native/gtk/host-sources.h +46 -0
  186. package/native/gtk/image-manipulator.cpp +254 -0
  187. package/native/gtk/image-manipulator.h +30 -0
  188. package/native/gtk/image-view.cpp +256 -0
  189. package/native/gtk/image-view.h +54 -0
  190. package/native/gtk/input-replay.cpp +299 -0
  191. package/native/gtk/input-replay.h +36 -0
  192. package/native/gtk/launcher-badge.cpp +29 -0
  193. package/native/gtk/launcher-badge.h +21 -0
  194. package/native/gtk/locales.cpp +131 -0
  195. package/native/gtk/locales.h +23 -0
  196. package/native/gtk/location.cpp +274 -0
  197. package/native/gtk/location.h +19 -0
  198. package/native/gtk/main.cpp +1092 -0
  199. package/native/gtk/menus.cpp +447 -0
  200. package/native/gtk/menus.h +100 -0
  201. package/native/gtk/native-controls.cpp +831 -0
  202. package/native/gtk/native-controls.h +58 -0
  203. package/native/gtk/network-state.cpp +72 -0
  204. package/native/gtk/network-state.h +24 -0
  205. package/native/gtk/notifications.cpp +235 -0
  206. package/native/gtk/notifications.h +30 -0
  207. package/native/gtk/pango-text.cpp +274 -0
  208. package/native/gtk/pango-text.h +73 -0
  209. package/native/gtk/portal.cpp +451 -0
  210. package/native/gtk/portal.h +71 -0
  211. package/native/gtk/printing.cpp +338 -0
  212. package/native/gtk/printing.h +34 -0
  213. package/native/gtk/proxy-resolver.cpp +74 -0
  214. package/native/gtk/proxy-resolver.h +20 -0
  215. package/native/gtk/screens.cpp +1461 -0
  216. package/native/gtk/screens.h +274 -0
  217. package/native/gtk/scroll-view.cpp +414 -0
  218. package/native/gtk/scroll-view.h +82 -0
  219. package/native/gtk/secret-store.cpp +120 -0
  220. package/native/gtk/secret-store.h +27 -0
  221. package/native/gtk/settings-portal.cpp +129 -0
  222. package/native/gtk/settings-portal.h +47 -0
  223. package/native/gtk/split-view.cpp +1105 -0
  224. package/native/gtk/split-view.h +195 -0
  225. package/native/gtk/status-notifier.cpp +487 -0
  226. package/native/gtk/status-notifier.h +43 -0
  227. package/native/gtk/surface-input.cpp +251 -0
  228. package/native/gtk/surface-input.h +57 -0
  229. package/native/gtk/system-access.cpp +295 -0
  230. package/native/gtk/system-access.h +37 -0
  231. package/native/gtk/text-field.cpp +827 -0
  232. package/native/gtk/text-field.h +78 -0
  233. package/native/gtk/video.cpp +527 -0
  234. package/native/gtk/video.h +47 -0
  235. package/native/gtk/view-widget.cpp +431 -0
  236. package/native/gtk/view-widget.h +105 -0
  237. package/native/gtk/window-scale.cpp +95 -0
  238. package/native/gtk/window-scale.h +35 -0
  239. package/native/scripts/atspi-check.py +310 -0
  240. package/native/scripts/atspi-check.test.py +48 -0
  241. package/native/scripts/build.sh +20 -3
  242. package/native/scripts/compilers.sh +17 -0
  243. package/native/scripts/deb-depends-check.sh +22 -0
  244. package/native/scripts/fetch.sh +23 -2
  245. package/native/scripts/launch-cell.sh +157 -0
  246. package/native/scripts/launch-probe.sh +14 -3
  247. package/native/scripts/make-prebuilt.mjs +133 -44
  248. package/native/scripts/prebuilt-host-cache.mjs +71 -40
  249. package/native/scripts/reset-for-compilers.sh +44 -0
  250. package/native/scripts/start-xvfb.sh +35 -0
  251. package/native/shim/paragraph-platform/react/renderer/components/text/HostPlatformParagraphProps.h +35 -0
  252. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +5 -2
  253. package/native/shim/worklets/linux-reanimated-props.h +14 -0
  254. package/native/shim/worklets/linux-worklets.h +23 -0
  255. package/package.json +6 -3
  256. package/prebuilt.json +70 -6
@@ -0,0 +1,212 @@
1
+ // GFileMonitor reports a rename within a watched folder as one event naming both paths, and a move
2
+ // across its edge as a move in or out; these become the core's events (FSEvents' shape: a rename
3
+ // is its old path, then its new one).
4
+ #include "file-system.h"
5
+
6
+ #include "app-windows.h"
7
+
8
+ #include <gio/gio.h>
9
+
10
+ #include <cstdio>
11
+ #include <cstdlib>
12
+ #include <functional>
13
+ #include <memory>
14
+ #include <map>
15
+ #include <mutex>
16
+ #include <sstream>
17
+
18
+ namespace ngn::gtk {
19
+
20
+ namespace {
21
+
22
+ std::string pathOf(GFile *file) {
23
+ gchar *path = file ? g_file_get_path(file) : nullptr;
24
+ std::string out = path ? path : "";
25
+ g_free(path);
26
+ return out;
27
+ }
28
+
29
+ // One watch: its monitor, made on the main context, and its events' handler, which runs only while
30
+ // the watch lasts. The lock is held across each batch, so once stop has taken it none follows.
31
+ struct Watch {
32
+ std::recursive_mutex lock;
33
+ bool live{true};
34
+ GFileMonitor *monitor{nullptr};
35
+ gulong handler{0};
36
+ std::function<void(std::vector<FileWatcher::Event>)> events;
37
+ };
38
+
39
+ void changed(GFileMonitor *, GFile *file, GFile *other, GFileMonitorEvent type, gpointer data) {
40
+ auto *watch = static_cast<Watch *>(data);
41
+ std::vector<FileWatcher::Event> batch;
42
+ auto path = pathOf(file);
43
+ switch (type) {
44
+ case G_FILE_MONITOR_EVENT_CREATED:
45
+ case G_FILE_MONITOR_EVENT_MOVED_IN:
46
+ batch.push_back({.path = path, .created = true});
47
+ break;
48
+ case G_FILE_MONITOR_EVENT_DELETED:
49
+ case G_FILE_MONITOR_EVENT_MOVED_OUT:
50
+ batch.push_back({.path = path, .removed = true});
51
+ break;
52
+ case G_FILE_MONITOR_EVENT_CHANGED:
53
+ case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
54
+ batch.push_back({.path = path, .modified = true});
55
+ break;
56
+ case G_FILE_MONITOR_EVENT_RENAMED:
57
+ batch.push_back({.path = path, .renamed = true});
58
+ batch.push_back({.path = pathOf(other), .renamed = true});
59
+ break;
60
+ case G_FILE_MONITOR_EVENT_UNMOUNTED:
61
+ batch.push_back({.path = path, .rescan = true});
62
+ break;
63
+ default:
64
+ // CHANGES_DONE_HINT follows the CHANGED already reported; PRE_UNMOUNT comes before UNMOUNTED.
65
+ return;
66
+ }
67
+ std::lock_guard hold(watch->lock);
68
+ if (watch->live) {
69
+ watch->events(std::move(batch));
70
+ }
71
+ }
72
+
73
+
74
+ class MonitorWatcher final : public FileWatcher {
75
+ public:
76
+ int start(const std::string &path, std::function<void(std::vector<Event>)> events) override {
77
+ auto watch = std::make_shared<Watch>();
78
+ watch->events = std::move(events);
79
+ int handle;
80
+ {
81
+ std::lock_guard lock(mutex_);
82
+ handle = ++next_;
83
+ watches_[handle] = watch;
84
+ }
85
+ // On the main context, so its events are read there whichever thread asked.
86
+ onMainContext([watch, path] {
87
+ std::lock_guard hold(watch->lock);
88
+ if (!watch->live) {
89
+ return;
90
+ }
91
+ GFile *file = g_file_new_for_path(path.c_str());
92
+ GError *error = nullptr;
93
+ watch->monitor = g_file_monitor(file, G_FILE_MONITOR_WATCH_MOVES, nullptr, &error);
94
+ g_object_unref(file);
95
+ if (!watch->monitor) {
96
+ fprintf(stderr, "[ngn] could not watch %s: %s\n", path.c_str(), error->message);
97
+ g_error_free(error);
98
+ return;
99
+ }
100
+ // FSEvents' latency: GIO's default holds a file's changes back for most of a second.
101
+ g_file_monitor_set_rate_limit(watch->monitor, 100);
102
+ watch->handler = g_signal_connect(watch->monitor, "changed", G_CALLBACK(changed), watch.get());
103
+ });
104
+ return handle;
105
+ }
106
+
107
+ void stop(int handle) override {
108
+ std::shared_ptr<Watch> watch;
109
+ {
110
+ std::lock_guard lock(mutex_);
111
+ auto it = watches_.find(handle);
112
+ if (it == watches_.end()) {
113
+ return;
114
+ }
115
+ watch = std::move(it->second);
116
+ watches_.erase(it);
117
+ }
118
+ {
119
+ std::lock_guard hold(watch->lock);
120
+ watch->live = false;
121
+ }
122
+ onMainContext([watch] {
123
+ if (watch->monitor) {
124
+ g_signal_handler_disconnect(watch->monitor, watch->handler);
125
+ g_file_monitor_cancel(watch->monitor);
126
+ g_clear_object(&watch->monitor);
127
+ }
128
+ });
129
+ }
130
+
131
+ private:
132
+ std::mutex mutex_;
133
+ std::map<int, std::shared_ptr<Watch>> watches_;
134
+ int next_{0};
135
+ };
136
+
137
+ std::string realOrSame(const std::string &path) {
138
+ if (path.empty()) {
139
+ return path;
140
+ }
141
+ char *resolved = realpath(path.c_str(), nullptr);
142
+ std::string out = resolved ? resolved : path;
143
+ free(resolved);
144
+ return out;
145
+ }
146
+
147
+ } // namespace
148
+
149
+ std::shared_ptr<FileWatcher> makeFileWatcher() {
150
+ return std::make_shared<MonitorWatcher>();
151
+ }
152
+
153
+ std::string xdgUserDir(const std::string &contents, const std::string &name, const std::string &home) {
154
+ std::istringstream lines(contents);
155
+ std::string line;
156
+ std::string key = "XDG_" + name + "_DIR=";
157
+ std::string found;
158
+ while (std::getline(lines, line)) {
159
+ auto start = line.find_first_not_of(" \t");
160
+ if (start == std::string::npos || line.compare(start, key.size(), key) != 0) {
161
+ continue;
162
+ }
163
+ auto value = line.substr(start + key.size());
164
+ auto open = value.find('"');
165
+ auto close = value.rfind('"');
166
+ if (open == std::string::npos || close == open) {
167
+ continue;
168
+ }
169
+ value = value.substr(open + 1, close - open - 1);
170
+ if (value.rfind("$HOME", 0) == 0) {
171
+ value = home + value.substr(5);
172
+ } else if (value.empty() || value[0] != '/') {
173
+ // The file allows only $HOME-relative and absolute paths.
174
+ continue;
175
+ }
176
+ while (value.size() > 1 && value.back() == '/') {
177
+ value.pop_back();
178
+ }
179
+ // The last line for a name wins, as xdg-user-dirs reads it.
180
+ found = value;
181
+ }
182
+ auto bare = home;
183
+ while (bare.size() > 1 && bare.back() == '/') {
184
+ bare.pop_back();
185
+ }
186
+ return found == bare ? "" : found;
187
+ }
188
+
189
+ FileAccess::Folders fileFolders(const std::string &appData, const std::string &cache, const std::string &temp) {
190
+ std::string home = g_get_home_dir();
191
+ std::string contents;
192
+ gchar *file = g_build_filename(g_get_user_config_dir(), "user-dirs.dirs", nullptr);
193
+ gchar *text = nullptr;
194
+ if (g_file_get_contents(file, &text, nullptr, nullptr)) {
195
+ contents = text;
196
+ g_free(text);
197
+ }
198
+ g_free(file);
199
+ auto document = appData + "/Documents";
200
+ g_mkdir_with_parents(document.c_str(), 0700);
201
+ return {
202
+ .cache = realOrSame(cache),
203
+ .document = realOrSame(document),
204
+ .appData = realOrSame(appData),
205
+ .temp = realOrSame(temp),
206
+ .home = realOrSame(home),
207
+ .desktop = realOrSame(xdgUserDir(contents, "DESKTOP", home)),
208
+ .downloads = realOrSame(xdgUserDir(contents, "DOWNLOAD", home)),
209
+ };
210
+ }
211
+
212
+ } // namespace ngn::gtk
@@ -0,0 +1,25 @@
1
+ // The OS half of NgNativeFileSystem on Linux (the core's file-system.h): GFileMonitor, which is
2
+ // inotify, and the XDG folders.
3
+ #pragma once
4
+
5
+ #include "ngn/file-system.h"
6
+
7
+ #include <memory>
8
+ #include <string>
9
+
10
+ namespace ngn::gtk {
11
+
12
+ // Watches with one GFileMonitor per watch, made and read on the main context.
13
+ std::shared_ptr<FileWatcher> makeFileWatcher();
14
+
15
+ // The folder `name` (DESKTOP, DOWNLOAD) names in the XDG user-dirs file's `contents`, with $HOME
16
+ // expanded; empty when it names none, or names the home folder itself, which the file uses to
17
+ // say the user has no such folder.
18
+ std::string xdgUserDir(const std::string &contents, const std::string &name, const std::string &home);
19
+
20
+ // The app's folders under `appData` and `cache`, `temp`, and the user's: home, and the desktop and
21
+ // downloads folders from $XDG_CONFIG_HOME/user-dirs.dirs, absent when the user has none. A Flatpak
22
+ // sets XDG_DATA_HOME and XDG_CACHE_HOME under ~/.var/app/<id>, so the app's own follow them.
23
+ FileAccess::Folders fileFolders(const std::string &appData, const std::string &cache, const std::string &temp);
24
+
25
+ } // namespace ngn::gtk
@@ -0,0 +1,282 @@
1
+ #include "focus.h"
2
+
3
+ #include "gtk-backend.h"
4
+ #include "text-field.h"
5
+ #include "view-widget.h"
6
+
7
+ #include "ngn/focus-order.h"
8
+
9
+ #include <react/renderer/core/RawEvent.h>
10
+
11
+ #include <optional>
12
+
13
+ namespace ngn::gtk {
14
+
15
+ namespace {
16
+
17
+ // Whether the focus moving now was moved by a key, which makes it focus-visible.
18
+ bool gKeyMove = false;
19
+
20
+ // The view the focus is on, or in: the nearest view at or above the focused widget.
21
+ std::optional<Tag> focusedView(GtkWidget *focus) {
22
+ for (GtkWidget *w = focus; w; w = gtk_widget_get_parent(w)) {
23
+ if (const ViewData *d = viewData(w); d && d->tag >= 0 && !d->surface) {
24
+ return d->tag;
25
+ }
26
+ }
27
+ return std::nullopt;
28
+ }
29
+
30
+ // The root view of the window's surface: the first surface widget in it.
31
+ std::optional<Tag> surfaceRoot(GtkWidget *widget) {
32
+ if (const ViewData *d = viewData(widget); d && d->surface) {
33
+ return d->surfaceId;
34
+ }
35
+ for (GtkWidget *c = gtk_widget_get_first_child(widget); c; c = gtk_widget_get_next_sibling(c)) {
36
+ if (auto root = surfaceRoot(c)) {
37
+ return root;
38
+ }
39
+ }
40
+ return std::nullopt;
41
+ }
42
+
43
+ struct Context {
44
+ std::optional<Tag> root;
45
+ std::optional<Tag> focused;
46
+ // A presentation's, whose Tab order Tab never leaves.
47
+ bool contained{false};
48
+ };
49
+
50
+ Context contextOf(GtkWindow *window, GtkBackend &backend, const ViewTree &tree) {
51
+ Context c;
52
+ GtkWidget *focus = gtk_root_get_focus(GTK_ROOT(window));
53
+ c.focused = focus ? focusedView(focus) : std::nullopt;
54
+ c.root = c.focused ? std::optional(focusContainer(tree, *c.focused)) : surfaceRoot(GTK_WIDGET(window));
55
+ // The presentation in front of the window keeps the focus inside it.
56
+ if (auto front = backend.modals().front(window); front && c.root != front) {
57
+ bool inside = false;
58
+ for (Tag at = c.focused.value_or(-1); const auto *node = tree.find(at); at = node->parent) {
59
+ inside = inside || at == *front;
60
+ }
61
+ if (!inside) {
62
+ c.root = front;
63
+ c.focused.reset();
64
+ }
65
+ }
66
+ c.contained = c.root && backend.modals().slotOf(*c.root) != nullptr;
67
+ return c;
68
+ }
69
+
70
+ bool grab(const ViewData &view, bool backwards) {
71
+ GtkWidget *widget = focusWidget(view);
72
+ // A key moved it: GTK draws its focus ring, as AppKit does for a focus from the keyboard.
73
+ if (GtkRoot *root = gtk_widget_get_root(widget); root && GTK_IS_WINDOW(root)) {
74
+ gtk_window_set_focus_visible(GTK_WINDOW(root), TRUE);
75
+ }
76
+ gKeyMove = true;
77
+ bool taken = false;
78
+ if (gtk_widget_is_sensitive(widget)) {
79
+ // A control that is a container (a row of segments) takes no focus itself, and grab_focus on
80
+ // it finds nothing: its first (or, backwards, last) segment that does is the stop.
81
+ taken = widget == view.control && !gtk_widget_get_focusable(widget) && gtk_widget_get_first_child(widget)
82
+ ? gtk_widget_child_focus(widget, backwards ? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD)
83
+ : gtk_widget_grab_focus(widget);
84
+ }
85
+ gKeyMove = false;
86
+ return taken;
87
+ }
88
+
89
+ std::optional<CompositeKey> compositeKey(guint keyval) {
90
+ switch (keyval) {
91
+ case GDK_KEY_Left:
92
+ case GDK_KEY_KP_Left:
93
+ return CompositeKey::Left;
94
+ case GDK_KEY_Right:
95
+ case GDK_KEY_KP_Right:
96
+ return CompositeKey::Right;
97
+ case GDK_KEY_Up:
98
+ case GDK_KEY_KP_Up:
99
+ return CompositeKey::Up;
100
+ case GDK_KEY_Down:
101
+ case GDK_KEY_KP_Down:
102
+ return CompositeKey::Down;
103
+ case GDK_KEY_Home:
104
+ case GDK_KEY_KP_Home:
105
+ return CompositeKey::Home;
106
+ case GDK_KEY_End:
107
+ case GDK_KEY_KP_End:
108
+ return CompositeKey::End;
109
+ default:
110
+ return std::nullopt;
111
+ }
112
+ }
113
+
114
+ // Whether a click at (x, y) in `view` is on the view itself, its own box or content that takes no
115
+ // focus, rather than on something inside it that does (a view or a control that took the press as
116
+ // it came up).
117
+ bool clickIsOn(GtkWidget *view, double x, double y) {
118
+ GtkWidget *hit = gtk_widget_pick(view, x, y, GTK_PICK_DEFAULT);
119
+ for (GtkWidget *w = hit; w && w != view; w = gtk_widget_get_parent(w)) {
120
+ if (gtk_widget_get_focusable(w)) {
121
+ return false;
122
+ }
123
+ }
124
+ return true;
125
+ }
126
+
127
+ // Whether the focus is in a popover (a date picker's calendar, a menu): GTK's own focus keeps it
128
+ // inside until it closes.
129
+ bool inPopover(GtkWidget *focus) {
130
+ return focus && gtk_widget_get_ancestor(focus, GTK_TYPE_POPOVER) != nullptr;
131
+ }
132
+
133
+ void send(GtkWidget *widget, bool focused) {
134
+ const ViewData *d = viewData(widget);
135
+ if (!d || !d->emitter || d->tag < 0) {
136
+ return;
137
+ }
138
+ folly::dynamic payload = folly::dynamic::object("target", d->tag);
139
+ if (focused) {
140
+ payload["focusVisible"] = gKeyMove;
141
+ }
142
+ d->emitter->dispatchEvent(focused ? "focus" : "blur", std::move(payload), facebook::react::RawEvent::Category::Discrete);
143
+ }
144
+
145
+ } // namespace
146
+
147
+ GtkWidget *focusWidget(const ViewData &view) {
148
+ if (view.field) {
149
+ return fieldWidget(*view.field);
150
+ }
151
+ return view.control ? view.control : view.widget;
152
+ }
153
+
154
+ void applyFocusable(GtkWidget *widget, const ViewData &view) {
155
+ if (!view.field && !view.control) {
156
+ gtk_widget_set_focusable(widget, viewTakesFocus(view.desktop));
157
+ }
158
+ }
159
+
160
+ void watchFocus(GtkWidget *widget) {
161
+ // A plain view's focus ring, drawn while the focus came from the keyboard.
162
+ static bool styled = false;
163
+ if (!styled && gdk_display_get_default()) {
164
+ styled = true;
165
+ GtkCssProvider *css = gtk_css_provider_new();
166
+ gtk_css_provider_load_from_data(
167
+ css, "ngn-view.ngn-focused:focus-visible { outline: 2px solid alpha(@accent_bg_color, 0.5); outline-offset: 1px; }", -1);
168
+ gtk_style_context_add_provider_for_display(gdk_display_get_default(), GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
169
+ g_object_unref(css);
170
+ }
171
+ // A click focuses the innermost view under it that takes the focus, as AppKit's first responder
172
+ // follows a click: an inner one took it already as the press bubbled up.
173
+ GtkGesture *click = gtk_gesture_click_new();
174
+ gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(click), GDK_BUTTON_PRIMARY);
175
+ g_signal_connect(click, "pressed", G_CALLBACK(+[](GtkGestureClick *g, int, double x, double y, gpointer) {
176
+ GtkWidget *view = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(g));
177
+ GtkRoot *root = gtk_widget_get_root(view);
178
+ GtkWidget *focus = root ? gtk_root_get_focus(root) : nullptr;
179
+ if (gtk_widget_get_focusable(view) && focus != view && clickIsOn(view, x, y)) {
180
+ gtk_widget_grab_focus(view);
181
+ }
182
+ gtk_gesture_set_state(GTK_GESTURE(g), GTK_EVENT_SEQUENCE_DENIED);
183
+ }), nullptr);
184
+ gtk_widget_add_controller(widget, GTK_EVENT_CONTROLLER(click));
185
+ }
186
+
187
+ void trackFocus(GtkWindow *window) {
188
+ if (g_object_get_data(G_OBJECT(window), "ngn-focus")) {
189
+ return;
190
+ }
191
+ // The view that had the focus, held weakly, which hears its blur as the focus moves on.
192
+ auto *last = g_new0(GWeakRef, 1);
193
+ g_weak_ref_init(last, nullptr);
194
+ g_object_set_data_full(G_OBJECT(window), "ngn-focus", last, [](gpointer p) {
195
+ g_weak_ref_clear(static_cast<GWeakRef *>(p));
196
+ g_free(p);
197
+ });
198
+ // The window's focus widget changes however the focus moved, whether or not the window is
199
+ // active, which GTK's focus controllers wait for.
200
+ g_signal_connect(window, "notify::focus-widget", G_CALLBACK(+[](GtkWindow *w, GParamSpec *, gpointer) {
201
+ auto *held = static_cast<GWeakRef *>(g_object_get_data(G_OBJECT(w), "ngn-focus"));
202
+ GtkWidget *focus = gtk_root_get_focus(GTK_ROOT(w));
203
+ GtkWidget *view = focus && viewData(focus) ? focus : nullptr;
204
+ auto *previous = static_cast<GtkWidget *>(g_weak_ref_get(held));
205
+ if (previous == view) {
206
+ g_clear_object(&previous);
207
+ return;
208
+ }
209
+ // A view whose widget was replaced as its role changed keeps the focus: it hears neither.
210
+ bool same = previous && view && g_object_get_data(G_OBJECT(previous), "ngn-replaced-by") == view;
211
+ if (previous) {
212
+ gtk_widget_remove_css_class(previous, "ngn-focused");
213
+ if (!same) {
214
+ send(previous, false);
215
+ }
216
+ g_object_unref(previous);
217
+ }
218
+ g_weak_ref_set(held, view);
219
+ if (view) {
220
+ // The ring is the focused view's own, not its ancestors', which GTK marks focus-visible too.
221
+ gtk_widget_add_css_class(view, "ngn-focused");
222
+ if (!same) {
223
+ send(view, true);
224
+ }
225
+ }
226
+ }), nullptr);
227
+ }
228
+
229
+ bool moveFocusByKey(GtkWindow *window, guint keyval, GdkModifierType state, GtkBackend &backend, const ViewTree &tree) {
230
+ auto mods = state & (GDK_CONTROL_MASK | GDK_ALT_MASK | GDK_SUPER_MASK | GDK_SHIFT_MASK);
231
+ bool tab = keyval == GDK_KEY_Tab || keyval == GDK_KEY_ISO_Left_Tab || keyval == GDK_KEY_KP_Tab;
232
+ if (tab && (mods & ~GDK_SHIFT_MASK) == 0) {
233
+ // In an open popover Tab is GTK's: it walks the popover's own widgets and stays in them, where
234
+ // the core's Tab order would step out to the view after the one the popover belongs to.
235
+ if (inPopover(gtk_root_get_focus(GTK_ROOT(window)))) {
236
+ return false;
237
+ }
238
+ auto c = contextOf(window, backend, tree);
239
+ if (!c.root) {
240
+ return false;
241
+ }
242
+ // Shift+Tab arrives as ISO_Left_Tab.
243
+ bool backwards = keyval == GDK_KEY_ISO_Left_Tab || (mods & GDK_SHIFT_MASK);
244
+ std::optional<Tag> from = c.focused;
245
+ auto count = tabOrder(tree, *c.root, TabReach::All, from).size();
246
+ // A view whose widget takes no focus (a disabled control) passes Tab on.
247
+ for (size_t tries = 0; tries < count; tries++) {
248
+ auto next = nextInTabOrder(tree, *c.root, from, backwards, TabReach::All);
249
+ if (!next) {
250
+ break;
251
+ }
252
+ if (const ViewData *d = backend.data(*next); d && grab(*d, backwards)) {
253
+ return true;
254
+ }
255
+ from = next;
256
+ }
257
+ return c.contained;
258
+ }
259
+ auto key = compositeKey(keyval);
260
+ if (!key || (mods & ~GDK_CONTROL_MASK) != 0) {
261
+ return false;
262
+ }
263
+ auto c = contextOf(window, backend, tree);
264
+ GtkWidget *focus = gtk_root_get_focus(GTK_ROOT(window));
265
+ const ViewData *focused = c.focused ? backend.data(*c.focused) : nullptr;
266
+ // On the view itself: a text field or a control inside it keeps its arrows.
267
+ if (!c.root || !focused || focus != focused->widget) {
268
+ return false;
269
+ }
270
+ auto next = compositeMove(tree, *c.root, *c.focused, *key, (mods & GDK_CONTROL_MASK) != 0);
271
+ if (!next) {
272
+ return false;
273
+ }
274
+ if (*next != *c.focused) {
275
+ if (const ViewData *d = backend.data(*next)) {
276
+ grab(*d, false);
277
+ }
278
+ }
279
+ return true;
280
+ }
281
+
282
+ } // namespace ngn::gtk
@@ -0,0 +1,37 @@
1
+ // The focus on GTK, which the core owns (focus-order.h). A window's capture-phase key controller
2
+ // (menus.h's KeyRouting) gives Tab and Shift-Tab to the core's Tab order, and the arrow keys, Home
3
+ // and End to the composite widget the focus is in, before GTK's own focus moves see them; each move
4
+ // grabs real GTK focus on the view's widget, so an assistive technology hears it. Tab stays in the
5
+ // container the focus is in (focusContainer), and in the presentation in front of the window.
6
+ //
7
+ // A plain view's widget is focusable as its desktop props say (viewTakesFocus), takes the focus on
8
+ // a click, and reports its GTK focus back as the view's `focus` and `blur`, however it came: a key,
9
+ // a click, or an assistive technology moving it. A key's focus is focus-visible.
10
+ #pragma once
11
+
12
+ #include "ngn/view-tree.h"
13
+
14
+ #include <gtk/gtk.h>
15
+
16
+ namespace ngn::gtk {
17
+
18
+ class GtkBackend;
19
+ struct ViewData;
20
+
21
+ // Tab, Shift-Tab and the composite keys pressed in `window`. True when the key was used.
22
+ bool moveFocusByKey(GtkWindow *window, guint keyval, GdkModifierType state, GtkBackend &backend, const ViewTree &tree);
23
+
24
+ // The widget that takes the focus for a view: its text field, its native control, or the view.
25
+ GtkWidget *focusWidget(const ViewData &view);
26
+
27
+ // Whether the view's own widget takes the focus, from its desktop props; set as they change.
28
+ void applyFocusable(GtkWidget *widget, const ViewData &view);
29
+
30
+ // Focuses the view's widget on a click, when it takes the focus; set once, as its widget is made.
31
+ void watchFocus(GtkWidget *widget);
32
+
33
+ // Reports the focus moving between views in `window` as their `focus` and `blur`, and marks the
34
+ // focused one for its ring; once per window.
35
+ void trackFocus(GtkWindow *window);
36
+
37
+ } // namespace ngn::gtk
@@ -0,0 +1,89 @@
1
+ #include "fonts.h"
2
+
3
+ #include <fontconfig/fontconfig.h>
4
+ #include <glib.h>
5
+
6
+ #include <atomic>
7
+ #include <mutex>
8
+ #include <stdexcept>
9
+
10
+ namespace ngn::gtk {
11
+
12
+ namespace {
13
+
14
+ std::atomic<uint64_t> gAdded{0};
15
+ std::mutex gLibraryMutex;
16
+ std::shared_ptr<const FontLibrary> gLibrary;
17
+
18
+ // The first face fontconfig's application fonts have from the file at `path`.
19
+ std::optional<RegisteredFont> applicationFontAt(const std::string &path) {
20
+ FcFontSet *fonts = FcConfigGetFonts(nullptr, FcSetApplication);
21
+ for (int i = 0; fonts && i < fonts->nfont; ++i) {
22
+ FcChar8 *file = nullptr;
23
+ int index = 0;
24
+ if (FcPatternGetString(fonts->fonts[i], FC_FILE, 0, &file) != FcResultMatch || path != reinterpret_cast<const char *>(file)) {
25
+ continue;
26
+ }
27
+ if (FcPatternGetInteger(fonts->fonts[i], FC_INDEX, 0, &index) == FcResultMatch && index != 0) {
28
+ continue;
29
+ }
30
+ FcChar8 *family = nullptr, *postScript = nullptr;
31
+ RegisteredFont font;
32
+ if (FcPatternGetString(fonts->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch) {
33
+ font.family = reinterpret_cast<const char *>(family);
34
+ }
35
+ if (FcPatternGetString(fonts->fonts[i], FC_POSTSCRIPT_NAME, 0, &postScript) == FcResultMatch) {
36
+ font.postScriptName = reinterpret_cast<const char *>(postScript);
37
+ }
38
+ return font;
39
+ }
40
+ return std::nullopt;
41
+ }
42
+
43
+ class FontconfigRegistrar final : public FontRegistrar {
44
+ public:
45
+ RegisteredFont registerFile(const std::string &path) override {
46
+ std::lock_guard lock(mutex_);
47
+ // Registered before, by this process: it is there to use.
48
+ if (auto font = applicationFontAt(path)) {
49
+ return *font;
50
+ }
51
+ if (!g_file_test(path.c_str(), G_FILE_TEST_IS_REGULAR)) {
52
+ throw std::runtime_error("there is no such file");
53
+ }
54
+ if (!FcConfigAppFontAddFile(nullptr, reinterpret_cast<const FcChar8 *>(path.c_str()))) {
55
+ throw std::runtime_error("the file is not a font fontconfig reads");
56
+ }
57
+ auto font = applicationFontAt(path);
58
+ if (!font || font->family.empty()) {
59
+ throw std::runtime_error("the file is not a font fontconfig reads");
60
+ }
61
+ ++gAdded;
62
+ return *font;
63
+ }
64
+
65
+ private:
66
+ std::mutex mutex_;
67
+ };
68
+
69
+ } // namespace
70
+
71
+ std::shared_ptr<FontRegistrar> makeFontRegistrar() {
72
+ return std::make_shared<FontconfigRegistrar>();
73
+ }
74
+
75
+ void useFontLibrary(std::shared_ptr<const FontLibrary> library) {
76
+ std::lock_guard lock(gLibraryMutex);
77
+ gLibrary = std::move(library);
78
+ }
79
+
80
+ std::shared_ptr<const FontLibrary> fontLibrary() {
81
+ std::lock_guard lock(gLibraryMutex);
82
+ return gLibrary;
83
+ }
84
+
85
+ uint64_t fontsAdded() {
86
+ return gAdded;
87
+ }
88
+
89
+ } // namespace ngn::gtk
@@ -0,0 +1,22 @@
1
+ // The OS half of NgNativeFont on Linux (the core's fonts.h): a font file added to fontconfig's
2
+ // application fonts for this process, and Pango's font maps told to look again. Text names a
3
+ // loaded font by the name it was loaded under (useFontLibrary), as on macOS.
4
+ #pragma once
5
+
6
+ #include "ngn/fonts.h"
7
+
8
+ #include <cstdint>
9
+ #include <memory>
10
+
11
+ namespace ngn::gtk {
12
+
13
+ std::shared_ptr<FontRegistrar> makeFontRegistrar();
14
+
15
+ // The names text resolves loaded fonts by: the core's library, which fontOf (pango-text.h) reads.
16
+ void useFontLibrary(std::shared_ptr<const FontLibrary> library);
17
+ std::shared_ptr<const FontLibrary> fontLibrary();
18
+
19
+ // How many times fonts have been added: a thread whose Pango font map saw fewer reloads it.
20
+ uint64_t fontsAdded();
21
+
22
+ } // namespace ngn::gtk