@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,43 @@
1
+ // The tray on Linux (tray-and-dock.h's TrayBackend): each `<tray-icon>` a StatusNotifierItem with
2
+ // its menu as com.canonical.dbusmenu (dbus-menu.h), over GDBus, as KDE Plasma, the AppIndicator
3
+ // extension for GNOME and the other StatusNotifierWatcher hosts draw them. Each icon has a
4
+ // session-bus connection of its own, its item at /StatusNotifierItem and its menu at /MenuBar,
5
+ // registered with the watcher by that path, so closing the connection takes the icon away. An icon
6
+ // with a menu is ItemIsMenu, which a click opens; one without is pressed by Activate.
7
+ //
8
+ // The icon is `image` as an icon theme's name, else the app's own icon (its application id, as
9
+ // the package installs it); an SF Symbol has no Linux equivalent and is not drawn. The title is
10
+ // the item's Title, and the tooltip its ToolTip. A watcher that restarts (Plasma's shell, say) is
11
+ // given every icon again.
12
+ #pragma once
13
+
14
+ #include "ngn/tray-and-dock.h"
15
+
16
+ #include <gio/gio.h>
17
+
18
+ #include <functional>
19
+ #include <memory>
20
+ #include <string>
21
+
22
+ namespace ngn::gtk {
23
+
24
+ // The watcher's well-known name on the session bus.
25
+ inline constexpr const char *kStatusNotifierWatcher = "org.kde.StatusNotifierWatcher";
26
+
27
+ // Whether a StatusNotifierWatcher runs on the session bus: without one (stock GNOME, Fedora without
28
+ // the AppIndicator extension) the tray is absent.
29
+ bool statusNotifierWatcherRunning();
30
+
31
+ struct TrayEvents {
32
+ // A click on an icon with no menu.
33
+ std::function<void(const std::string &icon)> press;
34
+ // An item of an icon's menu chosen, by the app's id for it.
35
+ std::function<void(const std::string &icon, const std::string &id)> menuSelect;
36
+ // An item with the OS's command chosen (a `menuRole` such as quit), which the host runs.
37
+ std::function<void(MenuCommand command)> command;
38
+ };
39
+
40
+ // The icons, each `<appId>-<icon id>` to the watcher. Events come on the UI thread.
41
+ std::shared_ptr<TrayBackend> makeStatusNotifierTray(const std::string &appId, TrayEvents events);
42
+
43
+ } // namespace ngn::gtk
@@ -0,0 +1,251 @@
1
+ #include "surface-input.h"
2
+
3
+ #include "drag-and-drop.h"
4
+
5
+ #include <cmath>
6
+ #include <optional>
7
+
8
+ using facebook::react::Cursor;
9
+ using facebook::react::SurfaceId;
10
+
11
+ namespace ngn::gtk {
12
+
13
+ int ClickCounter::press(uint32_t timeMs, double x, double y, unsigned button) {
14
+ bool continues = count_ > 0 && button == lastButton_ && timeMs - lastTime_ <= static_cast<uint32_t>(intervalMs_) &&
15
+ std::abs(x - lastX_) <= distance_ && std::abs(y - lastY_) <= distance_;
16
+ count_ = continues ? count_ + 1 : 1;
17
+ lastTime_ = timeMs;
18
+ lastX_ = x;
19
+ lastY_ = y;
20
+ lastButton_ = button;
21
+ return count_;
22
+ }
23
+
24
+ int buttonBit(unsigned gdkButton) {
25
+ switch (gdkButton) {
26
+ case 1: return 1;
27
+ case 2: return 4;
28
+ case 3: return 2;
29
+ case 8: return 8;
30
+ case 9: return 16;
31
+ default: return 0;
32
+ }
33
+ }
34
+
35
+ const char *cursorName(Cursor cursor) {
36
+ switch (cursor) {
37
+ case Cursor::Auto: return nullptr;
38
+ case Cursor::Alias: return "alias";
39
+ case Cursor::AllScroll: return "all-scroll";
40
+ case Cursor::Cell: return "cell";
41
+ case Cursor::ColResize: return "col-resize";
42
+ case Cursor::ContextMenu: return "context-menu";
43
+ case Cursor::Copy: return "copy";
44
+ case Cursor::Crosshair: return "crosshair";
45
+ case Cursor::Default: return "default";
46
+ case Cursor::EResize: return "e-resize";
47
+ case Cursor::EWResize: return "ew-resize";
48
+ case Cursor::Grab: return "grab";
49
+ case Cursor::Grabbing: return "grabbing";
50
+ case Cursor::Help: return "help";
51
+ case Cursor::Move: return "move";
52
+ case Cursor::NEResize: return "ne-resize";
53
+ case Cursor::NESWResize: return "nesw-resize";
54
+ case Cursor::NResize: return "n-resize";
55
+ case Cursor::NSResize: return "ns-resize";
56
+ case Cursor::NWResize: return "nw-resize";
57
+ case Cursor::NWSEResize: return "nwse-resize";
58
+ case Cursor::NoDrop: return "no-drop";
59
+ case Cursor::None: return "none";
60
+ case Cursor::NotAllowed: return "not-allowed";
61
+ case Cursor::Pointer: return "pointer";
62
+ case Cursor::Progress: return "progress";
63
+ case Cursor::RowResize: return "row-resize";
64
+ case Cursor::SResize: return "s-resize";
65
+ case Cursor::SEResize: return "se-resize";
66
+ case Cursor::SWResize: return "sw-resize";
67
+ case Cursor::Text: return "text";
68
+ // GTK has no `url` cursor: the pointing hand, as CSS's own fallback for a link.
69
+ case Cursor::Url: return "pointer";
70
+ case Cursor::WResize: return "w-resize";
71
+ case Cursor::Wait: return "wait";
72
+ case Cursor::ZoomIn: return "zoom-in";
73
+ case Cursor::ZoomOut: return "zoom-out";
74
+ }
75
+ return nullptr;
76
+ }
77
+
78
+ GdkCursor *makeCursor(Cursor cursor) {
79
+ const char *name = cursorName(cursor);
80
+ if (!name) {
81
+ return nullptr;
82
+ }
83
+ GdkCursor *fallback = gdk_cursor_new_from_name("default", nullptr);
84
+ GdkCursor *out = gdk_cursor_new_from_name(name, fallback);
85
+ g_clear_object(&fallback);
86
+ return out;
87
+ }
88
+
89
+ namespace {
90
+
91
+ struct PointerState {
92
+ GtkWidget *widget{nullptr};
93
+ SurfaceId surfaceId{-1};
94
+ ContainerId container{0};
95
+ InputSink *input{nullptr};
96
+ int buttons{0};
97
+ ClickCounter clicks{400, 5};
98
+ };
99
+
100
+ // The motion event the input sink is hearing, while it hears it.
101
+ struct Moving {
102
+ PointerState *state{nullptr};
103
+ GdkEvent *event{nullptr};
104
+ };
105
+ Moving gMoving;
106
+
107
+ // The event's point in the widget's coordinates: GDK gives it in the surface's.
108
+ std::optional<facebook::react::Point> pointIn(GtkWidget *widget, GdkEvent *event) {
109
+ double x = 0, y = 0;
110
+ if (!gdk_event_get_position(event, &x, &y)) {
111
+ return std::nullopt;
112
+ }
113
+ GtkNative *native = gtk_widget_get_native(widget);
114
+ if (!native) {
115
+ return std::nullopt;
116
+ }
117
+ double tx = 0, ty = 0;
118
+ gtk_native_get_surface_transform(native, &tx, &ty);
119
+ graphene_point_t in, out;
120
+ graphene_point_init(&in, static_cast<float>(x - tx), static_cast<float>(y - ty));
121
+ if (!gtk_widget_compute_point(GTK_WIDGET(native), widget, &in, &out)) {
122
+ return std::nullopt;
123
+ }
124
+ return facebook::react::Point{out.x, out.y};
125
+ }
126
+
127
+ std::optional<WheelDelta> wheelDelta(GdkEvent *event) {
128
+ WheelDelta delta;
129
+ switch (gdk_scroll_event_get_direction(event)) {
130
+ case GDK_SCROLL_UP: delta.y = -1; delta.mode = 1; break;
131
+ case GDK_SCROLL_DOWN: delta.y = 1; delta.mode = 1; break;
132
+ case GDK_SCROLL_LEFT: delta.x = -1; delta.mode = 1; break;
133
+ case GDK_SCROLL_RIGHT: delta.x = 1; delta.mode = 1; break;
134
+ case GDK_SCROLL_SMOOTH: {
135
+ gdk_scroll_event_get_deltas(event, &delta.x, &delta.y);
136
+ // A wheel's clicks in lines; a touchpad's movement in pixels.
137
+ delta.mode = gdk_scroll_event_get_unit(event) == GDK_SCROLL_UNIT_WHEEL ? 1 : 0;
138
+ if (gdk_scroll_event_is_stop(event)) {
139
+ delta.phase = GesturePhase::Ended;
140
+ }
141
+ break;
142
+ }
143
+ }
144
+ if (delta.x == 0 && delta.y == 0 && !delta.phase) {
145
+ return std::nullopt;
146
+ }
147
+ return delta;
148
+ }
149
+
150
+ std::function<bool(GtkWidget *, const PointerInput &)> &contextMenuShower() {
151
+ static std::function<bool(GtkWidget *, const PointerInput &)> shower;
152
+ return shower;
153
+ }
154
+
155
+ gboolean pointerEvent(GtkEventControllerLegacy *, GdkEvent *event, gpointer user) {
156
+ auto *s = static_cast<PointerState *>(user);
157
+ GdkEventType type = gdk_event_get_event_type(event);
158
+ if (type != GDK_BUTTON_PRESS && type != GDK_BUTTON_RELEASE && type != GDK_MOTION_NOTIFY && type != GDK_SCROLL) {
159
+ return FALSE;
160
+ }
161
+ auto point = pointIn(s->widget, event);
162
+ if (!point) {
163
+ return FALSE;
164
+ }
165
+ PointerInput input;
166
+ input.container = s->container;
167
+ input.point = *point;
168
+ // Wayland gives no position on the screen: the window's own stands in for it.
169
+ input.screenPoint = *point;
170
+ input.timestamp = gdk_event_get_time(event) / 1000.0;
171
+ switch (type) {
172
+ case GDK_BUTTON_PRESS: {
173
+ guint button = gdk_button_event_get_button(event);
174
+ s->buttons |= buttonBit(button);
175
+ input.buttons = s->buttons;
176
+ input.pressure = 0.5f;
177
+ input.clickCount = s->clicks.press(gdk_event_get_time(event), point->x, point->y, button);
178
+ if (button == 3 && ((contextMenuShower() && contextMenuShower()(s->widget, input)) || s->input->contextMenu(input))) {
179
+ return FALSE;
180
+ }
181
+ s->input->pointerDown(input);
182
+ return FALSE;
183
+ }
184
+ case GDK_BUTTON_RELEASE:
185
+ s->buttons &= ~buttonBit(gdk_button_event_get_button(event));
186
+ input.buttons = s->buttons;
187
+ s->input->pointerUp(input);
188
+ return FALSE;
189
+ case GDK_MOTION_NOTIFY:
190
+ input.buttons = s->buttons;
191
+ input.pressure = s->buttons ? 0.5f : 0;
192
+ // A move that crosses the drag threshold on a `[dragItem]` view starts a drag out from
193
+ // this event (dragOutOfPointer).
194
+ gMoving = {s, event};
195
+ s->input->pointerMove(input);
196
+ gMoving = {};
197
+ return FALSE;
198
+ default: {
199
+ auto delta = wheelDelta(event);
200
+ if (!delta) {
201
+ return FALSE;
202
+ }
203
+ // One a gesture handler took is not the view under it to scroll as well.
204
+ return s->input->wheel(input, *delta).taken;
205
+ }
206
+ }
207
+ }
208
+
209
+ void pointerLeft(GtkEventControllerMotion *, gpointer user) {
210
+ auto *s = static_cast<PointerState *>(user);
211
+ PointerInput input;
212
+ input.container = s->container;
213
+ input.buttons = s->buttons;
214
+ s->input->pointerLeave(s->surfaceId, input);
215
+ }
216
+
217
+ } // namespace
218
+
219
+ void setContextMenuShower(std::function<bool(GtkWidget *widget, const PointerInput &input)> shower) {
220
+ contextMenuShower() = std::move(shower);
221
+ }
222
+
223
+ void attachPointerInput(GtkWidget *widget, SurfaceId surfaceId, ContainerId container, InputSink *input) {
224
+ auto *state = new PointerState{.widget = widget, .surfaceId = surfaceId, .container = container, .input = input};
225
+ int interval = 400, distance = 5;
226
+ g_object_get(gtk_widget_get_settings(widget), "gtk-double-click-time", &interval, "gtk-double-click-distance", &distance, nullptr);
227
+ state->clicks = ClickCounter(interval, distance);
228
+ g_object_set_data_full(G_OBJECT(widget), "ngn-pointer", state, [](gpointer p) { delete static_cast<PointerState *>(p); });
229
+
230
+ GtkEventController *legacy = gtk_event_controller_legacy_new();
231
+ gtk_event_controller_set_propagation_phase(legacy, GTK_PHASE_CAPTURE);
232
+ g_signal_connect(legacy, "event", G_CALLBACK(pointerEvent), state);
233
+ gtk_widget_add_controller(widget, legacy);
234
+
235
+ GtkEventController *motion = gtk_event_controller_motion_new();
236
+ g_signal_connect(motion, "leave", G_CALLBACK(pointerLeft), state);
237
+ gtk_widget_add_controller(widget, motion);
238
+
239
+ attachDropTarget(widget, container, input);
240
+ }
241
+
242
+ bool dragOutOfPointer(GtkWidget *view, const DragOutItem &item) {
243
+ if (!gMoving.event || !beginDragOut(view, gMoving.event, item)) {
244
+ return false;
245
+ }
246
+ // The drag has the pointer to its release, which this widget never sees.
247
+ gMoving.state->buttons = 0;
248
+ return true;
249
+ }
250
+
251
+ } // namespace ngn::gtk
@@ -0,0 +1,57 @@
1
+ // The pointer on a surface, to the core's InputSink, which hit-tests the view tree itself and
2
+ // decides press, hover and the cursor. A capture-phase legacy controller on the surface's widget
3
+ // sees each event before any widget under it, and never claims one, so a text field under the
4
+ // pointer still takes its own click.
5
+ #pragma once
6
+
7
+ #include "ngn/input-dispatcher.h"
8
+ #include "ngn/input.h"
9
+
10
+ #include <react/renderer/components/view/primitives.h>
11
+
12
+ #include <gtk/gtk.h>
13
+
14
+ #include <cstdint>
15
+ #include <functional>
16
+
17
+ namespace ngn::gtk {
18
+
19
+ // Which press of a run of quick ones a press is, by GTK's double-click time and distance: GDK
20
+ // gives a raw press no count of its own.
21
+ class ClickCounter {
22
+ public:
23
+ ClickCounter(int intervalMs, int distance) : intervalMs_(intervalMs), distance_(distance) {}
24
+ int press(uint32_t timeMs, double x, double y, unsigned button);
25
+
26
+ private:
27
+ int intervalMs_;
28
+ int distance_;
29
+ int count_{0};
30
+ uint32_t lastTime_{0};
31
+ double lastX_{0}, lastY_{0};
32
+ unsigned lastButton_{0};
33
+ };
34
+
35
+ // A DOM `buttons` bit for a GDK button: 1 primary, 2 secondary, 4 middle, 8 and 16 back and
36
+ // forward (GDK's 8 and 9).
37
+ int buttonBit(unsigned gdkButton);
38
+
39
+ // The CSS name GTK's cursors go by, or null for the default.
40
+ const char *cursorName(facebook::react::Cursor cursor);
41
+ // The cursor GTK shows for `cursor`, falling back to `default` where the cursor theme has none of
42
+ // that name; null for `auto`, which is the widget's own. The caller owns it.
43
+ GdkCursor *makeCursor(facebook::react::Cursor cursor);
44
+
45
+ // Shows a view's context menu for a secondary click at `input` in `widget`; true when it did, and
46
+ // the click goes no further. Set once by the host.
47
+ void setContextMenuShower(std::function<bool(GtkWidget *widget, const PointerInput &input)> shower);
48
+
49
+ // Feeds `widget`'s pointer events to `input` as the container `container` of `surfaceId`, and
50
+ // drags over it (drag-and-drop.h).
51
+ void attachPointerInput(GtkWidget *widget, facebook::react::SurfaceId surfaceId, ContainerId container, InputSink *input);
52
+
53
+ // Starts dragging `item` out of `view` from the pointer move the input sink is hearing, as its
54
+ // onDragOut asks; false outside one, or when GDK would not start a drag.
55
+ bool dragOutOfPointer(GtkWidget *view, const DragOutItem &item);
56
+
57
+ } // namespace ngn::gtk
@@ -0,0 +1,295 @@
1
+ #include "system-access.h"
2
+
3
+ #include "app-windows.h"
4
+ #include "portal.h"
5
+
6
+ #include <folly/json.h>
7
+ #include <gio/gunixfdlist.h>
8
+
9
+ #include <fcntl.h>
10
+ #include <sys/stat.h>
11
+ #include <unistd.h>
12
+
13
+ #include <cstdio>
14
+ #include <memory>
15
+
16
+ extern char **environ;
17
+
18
+ namespace ngn::gtk {
19
+
20
+ namespace {
21
+
22
+ constexpr const char *kOpenUri = "org.freedesktop.portal.OpenURI";
23
+
24
+ using Done = std::function<void(bool)>;
25
+
26
+ // The portal's answer as Done's: opened, or not; `fallBack` when no portal is there to ask.
27
+ portal::Answer answered(Done done, std::function<void(Done)> fallBack) {
28
+ return [done = std::move(done), fallBack = std::move(fallBack)](portal::Outcome outcome, GVariant *) mutable {
29
+ if (outcome == portal::Outcome::Absent) {
30
+ fallBack(std::move(done));
31
+ } else {
32
+ done(outcome == portal::Outcome::Done);
33
+ }
34
+ };
35
+ }
36
+
37
+ // Calls OpenURI's `method` for the front window, with `argument` (the uri, or the fd's index in
38
+ // `fds`) after the window's handle.
39
+ void openThroughPortal(const char *method, GVariant *argument, GUnixFDList *fds, portal::Answer answer) {
40
+ g_variant_ref_sink(argument);
41
+ portal::parentHandle(portal::frontWindow(), [method, argument, fds, answer = std::move(answer)](const std::string &parent,
42
+ std::function<void()> drop) mutable {
43
+ GVariantBuilder *options = g_variant_builder_new(G_VARIANT_TYPE_VARDICT);
44
+ portal::request(kOpenUri, method, g_variant_new("(s@*)", parent.c_str(), argument), options, fds,
45
+ [answer = std::move(answer), drop = std::move(drop)](portal::Outcome outcome, GVariant *results) {
46
+ drop();
47
+ answer(outcome, results);
48
+ });
49
+ g_variant_unref(argument);
50
+ g_clear_object(&fds);
51
+ });
52
+ }
53
+
54
+ // An fd for `path` that the portal reads the file through, in a list of its own; null when it
55
+ // cannot be opened.
56
+ GUnixFDList *fdFor(const std::string &path) {
57
+ #ifdef O_PATH
58
+ int fd = open(path.c_str(), O_PATH | O_CLOEXEC);
59
+ #else
60
+ int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
61
+ #endif
62
+ if (fd < 0) {
63
+ return nullptr;
64
+ }
65
+ GUnixFDList *fds = g_unix_fd_list_new_from_array(&fd, 1);
66
+ return fds;
67
+ }
68
+
69
+ void launchUriWithGtk(const std::string &uri, Done done) {
70
+ GtkUriLauncher *launcher = gtk_uri_launcher_new(uri.c_str());
71
+ gtk_uri_launcher_launch(launcher, portal::frontWindow(), nullptr, +[](GObject *source, GAsyncResult *result, gpointer data) {
72
+ std::unique_ptr<Done> done(static_cast<Done *>(data));
73
+ GError *error = nullptr;
74
+ bool launched = gtk_uri_launcher_launch_finish(GTK_URI_LAUNCHER(source), result, &error);
75
+ if (error) {
76
+ fprintf(stderr, "[ngn] could not open a url: %s\n", error->message);
77
+ g_error_free(error);
78
+ }
79
+ (*done)(launched);
80
+ }, new Done(std::move(done)));
81
+ g_object_unref(launcher);
82
+ }
83
+
84
+ void reportLaunch(void *data, bool launched, GError *error) {
85
+ std::unique_ptr<Done> done(static_cast<Done *>(data));
86
+ if (error) {
87
+ fprintf(stderr, "[ngn] could not open a file: %s\n", error->message);
88
+ g_error_free(error);
89
+ }
90
+ (*done)(launched);
91
+ }
92
+
93
+ void launchFileWithGtk(const std::string &path, bool containingFolder, Done done) {
94
+ GFile *file = g_file_new_for_path(path.c_str());
95
+ GtkFileLauncher *launcher = gtk_file_launcher_new(file);
96
+ g_object_unref(file);
97
+ if (containingFolder) {
98
+ gtk_file_launcher_open_containing_folder(launcher, portal::frontWindow(), nullptr, +[](GObject *source, GAsyncResult *result, gpointer data) {
99
+ GError *error = nullptr;
100
+ bool launched = gtk_file_launcher_open_containing_folder_finish(GTK_FILE_LAUNCHER(source), result, &error);
101
+ reportLaunch(data, launched, error);
102
+ }, new Done(std::move(done)));
103
+ } else {
104
+ gtk_file_launcher_launch(launcher, portal::frontWindow(), nullptr, +[](GObject *source, GAsyncResult *result, gpointer data) {
105
+ GError *error = nullptr;
106
+ bool launched = gtk_file_launcher_launch_finish(GTK_FILE_LAUNCHER(source), result, &error);
107
+ reportLaunch(data, launched, error);
108
+ }, new Done(std::move(done)));
109
+ }
110
+ g_object_unref(launcher);
111
+ }
112
+
113
+ bool exists(const std::string &path) {
114
+ struct stat info{};
115
+ return stat(path.c_str(), &info) == 0;
116
+ }
117
+
118
+ // The content types whose opening runs code: programs, libraries and scripts, desktop entries and
119
+ // AppImages, which launch what they name, and package and Flatpak installers. Not GLib's
120
+ // can_be_executable, which says yes for any text file.
121
+ const char *const kRunsCode[] = {
122
+ "application/x-executable", "application/x-pie-executable", "application/x-sharedlib", "application/x-shellscript",
123
+ "application/x-csh", "application/x-perl", "application/x-ruby", "text/x-python", "text/x-python3",
124
+ "application/x-desktop", "application/vnd.appimage", "application/x-iso9660-appimage", "application/vnd.flatpak",
125
+ "application/vnd.flatpak.ref", "application/vnd.flatpak.repo", "application/x-rpm",
126
+ "application/vnd.debian.binary-package", "application/x-ms-dos-executable", "application/x-msdownload",
127
+ "application/x-msi"};
128
+
129
+ bool runsCode(const std::string &path) {
130
+ // From the file's name and its first bytes: a script with no extension, an ELF named .txt.
131
+ GFile *file = g_file_new_for_path(path.c_str());
132
+ GFileInfo *info = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, G_FILE_QUERY_INFO_NONE, nullptr, nullptr);
133
+ g_object_unref(file);
134
+ gchar *type = info && g_file_info_get_content_type(info) ? g_strdup(g_file_info_get_content_type(info))
135
+ : g_content_type_guess(path.c_str(), nullptr, 0, nullptr);
136
+ g_clear_object(&info);
137
+ bool runs = false;
138
+ for (const char *code : kRunsCode) {
139
+ runs = runs || g_content_type_is_a(type, code);
140
+ }
141
+ g_free(type);
142
+ return runs;
143
+ }
144
+
145
+ class PortalOpener final : public ShellOpener {
146
+ public:
147
+ PathFacts facts(const std::string &path) override {
148
+ PathFacts facts;
149
+ struct stat info{};
150
+ facts.exists = stat(path.c_str(), &info) == 0;
151
+ if (!facts.exists) {
152
+ return facts;
153
+ }
154
+ facts.executable = S_ISREG(info.st_mode) && access(path.c_str(), X_OK) == 0;
155
+ facts.runsCode = !S_ISDIR(info.st_mode) && runsCode(path);
156
+ return facts;
157
+ }
158
+
159
+ void openUrl(const std::string &url, Done done) override {
160
+ openUri(url, std::move(done));
161
+ }
162
+
163
+ void openPath(const std::string &path, Done done) override {
164
+ openFile("OpenFile", path, false, std::move(done));
165
+ }
166
+
167
+ void showItemInFolder(const std::string &path, Done done) override {
168
+ openFile("OpenDirectory", path, true, std::move(done));
169
+ }
170
+
171
+ private:
172
+ static void openFile(const char *method, std::string path, bool containingFolder, Done done) {
173
+ onMainContext([method, path = std::move(path), containingFolder, done = std::move(done)]() mutable {
174
+ GUnixFDList *fds = exists(path) ? fdFor(path) : nullptr;
175
+ if (!fds) {
176
+ done(false);
177
+ return;
178
+ }
179
+ openThroughPortal(method, g_variant_new_handle(0), fds, answered(std::move(done), [path, containingFolder](Done done) {
180
+ launchFileWithGtk(path, containingFolder, std::move(done));
181
+ }));
182
+ });
183
+ }
184
+ };
185
+
186
+ class Inhibitor final : public SleepPreventer, public std::enable_shared_from_this<Inhibitor> {
187
+ public:
188
+ explicit Inhibitor(GtkApplication *application) : application_(G_APPLICATION(g_object_ref(application))) {}
189
+ ~Inhibitor() override {
190
+ g_object_unref(application_);
191
+ }
192
+
193
+ void hold(bool awake) override {
194
+ onMainContext([self = shared_from_this(), awake] { self->set(awake); });
195
+ }
196
+
197
+ private:
198
+ // On the main context. A request still being made is ended once it is open; only the latest
199
+ // hold counts.
200
+ void set(bool awake) {
201
+ if (awake == awake_) {
202
+ return;
203
+ }
204
+ awake_ = awake;
205
+ if (!awake) {
206
+ release();
207
+ return;
208
+ }
209
+ auto generation = ++generation_;
210
+ auto self = shared_from_this();
211
+ GVariantBuilder *options = g_variant_builder_new(G_VARIANT_TYPE_VARDICT);
212
+ g_variant_builder_add(options, "{sv}", "reason", g_variant_new_string("Kept awake by the app"));
213
+ // Idle is Inhibit's 8. No window: the hold is the app's, not one window's.
214
+ portal::request("org.freedesktop.portal.Inhibit", "Inhibit", g_variant_new("(su)", "", 8u), options, nullptr,
215
+ [self, generation](portal::Outcome outcome, GVariant *) {
216
+ if (outcome == portal::Outcome::Absent && self->awake_ && self->generation_ == generation) {
217
+ self->cookie_ = gtk_application_inhibit(GTK_APPLICATION(self->application_), nullptr,
218
+ GTK_APPLICATION_INHIBIT_IDLE, "Kept awake by the app");
219
+ }
220
+ },
221
+ true, [self, generation](const std::string &path) {
222
+ if (self->awake_ && self->generation_ == generation) {
223
+ self->request_ = path;
224
+ } else {
225
+ portal::close(path);
226
+ }
227
+ });
228
+ }
229
+
230
+ void release() {
231
+ ++generation_;
232
+ if (!request_.empty()) {
233
+ portal::close(request_);
234
+ request_.clear();
235
+ }
236
+ if (cookie_ != 0) {
237
+ gtk_application_uninhibit(GTK_APPLICATION(application_), cookie_);
238
+ cookie_ = 0;
239
+ }
240
+ }
241
+
242
+ GApplication *application_;
243
+ bool awake_{false};
244
+ unsigned generation_{0};
245
+ std::string request_;
246
+ guint cookie_{0};
247
+ };
248
+
249
+ } // namespace
250
+
251
+ bool insideFlatpak() {
252
+ return g_file_test("/.flatpak-info", G_FILE_TEST_EXISTS);
253
+ }
254
+
255
+ ProcessesSetup processesSetup(const HostOs &os, const std::string &capabilitiesJson, bool flatpak) {
256
+ ProcessesSetup setup;
257
+ for (char **e = environ; e && *e; ++e) {
258
+ std::string entry = *e;
259
+ if (auto eq = entry.find('='); eq != std::string::npos) {
260
+ setup.environment[entry.substr(0, eq)] = entry.substr(eq + 1);
261
+ }
262
+ }
263
+ setup.lookup = {
264
+ .isExecutable = [](const std::string &path) { return access(path.c_str(), X_OK) == 0; },
265
+ .searchPath = os.searchPath,
266
+ };
267
+ if (flatpak) {
268
+ setup.lookup.searchPath.insert(setup.lookup.searchPath.begin(), "/app/bin");
269
+ }
270
+ if (!capabilitiesJson.empty()) {
271
+ try {
272
+ setup.capabilities = parseCapabilities(folly::parseJson(capabilitiesJson));
273
+ } catch (const std::exception &error) {
274
+ fprintf(stderr, "[ngn] --capabilities is not JSON (%s): no capabilities\n", error.what());
275
+ }
276
+ }
277
+ return setup;
278
+ }
279
+
280
+ void openUri(const std::string &uri, Done done) {
281
+ onMainContext([uri, done = std::move(done)]() mutable {
282
+ openThroughPortal("OpenURI", g_variant_new_string(uri.c_str()), nullptr,
283
+ answered(std::move(done), [uri](Done done) { launchUriWithGtk(uri, std::move(done)); }));
284
+ });
285
+ }
286
+
287
+ std::shared_ptr<ShellOpener> makeShellOpener() {
288
+ return std::make_shared<PortalOpener>();
289
+ }
290
+
291
+ std::shared_ptr<SleepPreventer> makeSleepPreventer(GtkApplication *application) {
292
+ return std::make_shared<Inhibitor>(application);
293
+ }
294
+
295
+ } // namespace ngn::gtk
@@ -0,0 +1,37 @@
1
+ // What Processes, Shell and KeepAwake reach on Linux: the spawn setup, the desktop's launchers
2
+ // through the OpenURI portal, and the idle inhibitor.
3
+ #pragma once
4
+
5
+ #include "ngn/desktop-expo.h"
6
+ #include "ngn/host-os.h"
7
+ #include "ngn/processes.h"
8
+
9
+ #include <gtk/gtk.h>
10
+
11
+ #include <functional>
12
+ #include <memory>
13
+ #include <string>
14
+
15
+ namespace ngn::gtk {
16
+
17
+ // Inside a Flatpak sandbox, which /.flatpak-info marks.
18
+ bool insideFlatpak();
19
+
20
+ // Processes' setup: the environment the app started with, `os`'s search path (with the app's own
21
+ // /app/bin first inside a Flatpak, whose spawns stay in the sandbox), and the capabilities
22
+ // declared as `capabilitiesJson`, none when that is not JSON.
23
+ ProcessesSetup processesSetup(const HostOs &os, const std::string &capabilitiesJson, bool flatpak);
24
+
25
+ // Opens `uri` in the app the desktop chose for it, through the OpenURI portal, or GTK's launcher
26
+ // where no portal answers; `done` hears whether it opened, on the main context.
27
+ void openUri(const std::string &uri, std::function<void(bool)> done);
28
+
29
+ // Shell's opener: OpenURI's OpenURI, OpenFile and OpenDirectory, with GTK's launchers where no
30
+ // portal answers. What opening a path would run is read from its content type.
31
+ std::shared_ptr<ShellOpener> makeShellOpener();
32
+
33
+ // Holds the session idle while the app is awake: GtkApplication's inhibitor, which asks the Inhibit
34
+ // portal inside a sandbox and the session manager or the compositor outside one.
35
+ std::shared_ptr<SleepPreventer> makeSleepPreventer(GtkApplication *application);
36
+
37
+ } // namespace ngn::gtk