@ng-native/desktop 0.26.1 → 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,134 @@
1
+ // The GTK 4 backend: the core's mounting and desktop props interfaces over one NgnView per Fabric
2
+ // view (view-widget.h), on the GTK thread.
3
+ #pragma once
4
+
5
+ #include "screens.h"
6
+ #include "split-view.h"
7
+ #include "view-widget.h"
8
+
9
+ #include "ngn/accessibility.h"
10
+ #include "ngn/desktop-view-props.h"
11
+ #include "ngn/mounting-backend.h"
12
+
13
+ #include <gtk/gtk.h>
14
+
15
+ #include <deque>
16
+ #include <functional>
17
+ #include <mutex>
18
+ #include <thread>
19
+ #include <unordered_map>
20
+
21
+ namespace ngn::gtk {
22
+
23
+ class GtkBackend final : public MountingBackend, public DesktopPropsSink {
24
+ public:
25
+ // On the thread that runs GLib's default main context, which is the UI thread from then on.
26
+ GtkBackend();
27
+ ~GtkBackend() override;
28
+
29
+ // The widget `surfaceId`'s root view mounts into (makeSurfaceWidget).
30
+ void addSurface(SurfaceId surfaceId, GtkWidget *surface);
31
+ void removeSurface(SurfaceId surfaceId);
32
+ // The view's widget, or null.
33
+ GtkWidget *view(Tag tag) const;
34
+
35
+ // Told once each transaction has mounted.
36
+ std::function<void(SurfaceId)> onMount;
37
+
38
+ // What the screens (screens.h) need of the host: a view whose subtree a container of its own
39
+ // hits (the tree's container roots), and the pointer into a slot as that container.
40
+ struct Hooks {
41
+ std::function<void(Tag, bool)> containerRoot = [](Tag, bool) {};
42
+ std::function<void(GtkWidget *, SurfaceId, Tag)> attachInput = [](GtkWidget *, SurfaceId, Tag) {};
43
+ std::function<void(Tag)> detachInput = [](Tag) {};
44
+ // A press a screen reader sends a view that has no handler of its own for it: a click, as
45
+ // Space on the focused view is (InputDispatcher::activate). Whether it reached the view.
46
+ std::function<bool(SurfaceId, Tag)> activate = [](SurfaceId, Tag) { return false; };
47
+ // The user chose the tab `screen` in the sidebar of the tabs host `host` (MountingManager::chooseTab).
48
+ std::function<void(SurfaceId, Tag host, Tag screen)> chooseTab = [](SurfaceId, Tag, Tag) {};
49
+ } hooks;
50
+
51
+ // The widget a surface's root mounts into, or null.
52
+ GtkWidget *surfaceWidget(SurfaceId surfaceId) const;
53
+ // A view's data, or null.
54
+ ViewData *data(Tag tag) const;
55
+ // Moves the view's widget into `slot`, leaving an empty view in its place, so its siblings keep
56
+ // their places and Fabric's indices; unhostView puts it back. A view Fabric moves or removes
57
+ // comes back first.
58
+ void hostView(Tag tag, GtkWidget *slot);
59
+ void unhostView(Tag tag);
60
+ // Whether the view is still in the slot it was hosted in.
61
+ bool isHosted(Tag tag) const;
62
+ // `window`'s scale changed (window-scale.h): each Image in it picks its source again, as one
63
+ // does on macOS when its window's backing scale changes.
64
+ void windowRescaled(GtkWidget *window);
65
+ // A RNSSearchBar's field, or null.
66
+ GtkWidget *searchEntry(Tag tag) const;
67
+ Headers &headers() {
68
+ return *headers_;
69
+ }
70
+ Modals &modals() {
71
+ return *modals_;
72
+ }
73
+ SplitViews &splitViews() {
74
+ return *splitViews_;
75
+ }
76
+
77
+ void runOnUiThread(std::function<void()> task) override;
78
+ bool onUiThread() const override;
79
+ void serviceUiThread() override;
80
+ void createView(const ShadowView &view) override;
81
+ void deleteView(SurfaceId surfaceId, Tag tag) override;
82
+ void insertChild(SurfaceId surfaceId, Tag parent, Tag child, int index) override;
83
+ void removeChild(SurfaceId surfaceId, Tag parent, Tag child, int index) override;
84
+ void updateProps(const ShadowView &view) override;
85
+ void updateState(const ShadowView &view) override;
86
+ void updateLayout(const ShadowView &view) override;
87
+ void updateEventEmitter(const ShadowView &view) override;
88
+ void dispatchCommand(Tag tag, const std::string &name, const folly::dynamic &args) override;
89
+ void setCovered(SurfaceId surfaceId, Tag tag, bool covered) override;
90
+ void setPresented(SurfaceId surfaceId, Tag screen, bool presented) override;
91
+ void setFrontHeader(SurfaceId surfaceId, const std::optional<ScreenStacks::FrontHeader> &header) override;
92
+ void didMount(SurfaceId surfaceId) override;
93
+ void updateDesktopProps(SurfaceId surfaceId, Tag tag, const DesktopProps &props) override;
94
+ void setSplitView(SurfaceId surfaceId, Tag split, bool window) override;
95
+ void setSourceLists(SurfaceId surfaceId, Tag parent, const std::vector<SourceList> &lists) override;
96
+ void announce(SurfaceId surfaceId, const Announcement &announcement) override;
97
+ void setAccessibilityRelations(SurfaceId surfaceId, Tag tag, const AxRelations &relations) override;
98
+ void setSvgDrawing(SurfaceId surfaceId, Tag svg, std::shared_ptr<const SvgDocument> document) override;
99
+ std::vector<facebook::react::ComponentDescriptorProvider> componentProviders() const override;
100
+
101
+ private:
102
+ void drain();
103
+ // Replaces the view's widget when its GTK role is no longer the one the widget was made with,
104
+ // which GTK fixes for good: the new widget takes the old one's place, children, view data and
105
+ // focus. Making the view never replaces it.
106
+ void followRole(const ShadowView &view);
107
+ // The view's accessible actions (accessible.h applyAccessibleActions), sent through the core.
108
+ void applyActions(ViewData &d, const AxInfo &info);
109
+ // Points the accessible widget of `tag` at the accessible widgets of the views it is labelled
110
+ // and described by, as relations_ has them.
111
+ void applyRelations(Tag tag);
112
+
113
+ std::thread::id uiThread_;
114
+ std::mutex tasksMutex_;
115
+ std::deque<std::function<void()>> tasks_;
116
+ guint drainSource_{0};
117
+ std::unordered_map<Tag, GtkWidget *> views_;
118
+ // Each view's relations while it has any: set again on its new widget, and on the widgets that
119
+ // point at it, when a role change replaces a widget.
120
+ std::unordered_map<Tag, AxRelations> relations_;
121
+ static void surfaceGone(gpointer self, GObject *surface);
122
+ std::unordered_map<SurfaceId, GtkWidget *> surfaces_;
123
+ struct Hosted {
124
+ GtkWidget *placeholder;
125
+ GtkWidget *slot;
126
+ };
127
+ std::unordered_map<Tag, Hosted> hosted_;
128
+ std::unordered_map<Tag, GtkWidget *> searchEntries_;
129
+ std::unique_ptr<Headers> headers_;
130
+ std::unique_ptr<Modals> modals_;
131
+ std::unique_ptr<SplitViews> splitViews_;
132
+ };
133
+
134
+ } // namespace ngn::gtk
@@ -0,0 +1,354 @@
1
+ #include "host-sources.h"
2
+
3
+ #include "accessible.h"
4
+ #include "alerts.h"
5
+ #include "app-info.h"
6
+ #include "app-windows.h"
7
+ #include "locales.h"
8
+ #include "native-controls.h"
9
+ #include "portal.h"
10
+ #include "system-access.h"
11
+
12
+ #include "ngn/posix-locale.h"
13
+
14
+ #include <adwaita.h>
15
+ #if __has_include(<gio/gdesktopappinfo.h>)
16
+ #include <gio/gdesktopappinfo.h>
17
+ #define NGN_DESKTOP_APP_INFO 1
18
+ #endif
19
+ #include <gtk/gtk.h>
20
+
21
+ #include <atomic>
22
+ #include <memory>
23
+ #include <cstdio>
24
+ #include <mutex>
25
+
26
+ namespace ngn::gtk {
27
+
28
+ namespace {
29
+
30
+ class KeyFileStore final : public StringStore {
31
+ public:
32
+ explicit KeyFileStore(std::string path) : path_(std::move(path)), file_(g_key_file_new()) {
33
+ g_key_file_load_from_file(file_, path_.c_str(), G_KEY_FILE_NONE, nullptr);
34
+ }
35
+ ~KeyFileStore() override {
36
+ g_key_file_free(file_);
37
+ }
38
+ std::optional<std::string> get(const std::string &key) override {
39
+ std::lock_guard lock(mutex_);
40
+ gchar *value = g_key_file_get_string(file_, kGroup, key.c_str(), nullptr);
41
+ if (!value) {
42
+ return std::nullopt;
43
+ }
44
+ std::string out = value;
45
+ g_free(value);
46
+ return out;
47
+ }
48
+ void set(const std::string &key, const std::string &value) override {
49
+ std::lock_guard lock(mutex_);
50
+ g_key_file_set_string(file_, kGroup, key.c_str(), value.c_str());
51
+ save();
52
+ }
53
+ void remove(const std::string &key) override {
54
+ std::lock_guard lock(mutex_);
55
+ g_key_file_remove_key(file_, kGroup, key.c_str(), nullptr);
56
+ save();
57
+ }
58
+
59
+ private:
60
+ static constexpr const char *kGroup = "Preferences";
61
+ void save() {
62
+ gchar *dir = g_path_get_dirname(path_.c_str());
63
+ g_mkdir_with_parents(dir, 0700);
64
+ g_free(dir);
65
+ GError *error = nullptr;
66
+ if (!g_key_file_save_to_file(file_, path_.c_str(), &error)) {
67
+ fprintf(stderr, "[ngn] preferences: %s\n", error->message);
68
+ g_error_free(error);
69
+ }
70
+ }
71
+ std::string path_;
72
+ GKeyFile *file_;
73
+ std::mutex mutex_;
74
+ };
75
+
76
+ using ImageSize = std::optional<std::pair<double, double>>;
77
+
78
+ void sizePrepared(GdkPixbufLoader *, int width, int height, gpointer out) {
79
+ *static_cast<ImageSize *>(out) = std::make_pair<double, double>(width, height);
80
+ }
81
+
82
+ // Inside a Flatpak or a Snap, where the desktop portal opens a uri and no handler is visible.
83
+ bool sandboxed() {
84
+ return g_file_test("/.flatpak-info", G_FILE_TEST_EXISTS) || g_getenv("SNAP") != nullptr;
85
+ }
86
+
87
+ // The desktop's own settings app, GNOME's or KDE Plasma's. None in the dev loop on a Mac, whose
88
+ // GLib has no desktop files.
89
+ bool launchSettingsApp() {
90
+ #ifndef NGN_DESKTOP_APP_INFO
91
+ return false;
92
+ #else
93
+ for (const char *id : {"org.gnome.Settings.desktop", "gnome-control-center.desktop", "systemsettings.desktop", "org.kde.systemsettings.desktop"}) {
94
+ if (GDesktopAppInfo *app = g_desktop_app_info_new(id)) {
95
+ bool launched = g_app_info_launch(G_APP_INFO(app), nullptr, nullptr, nullptr);
96
+ g_object_unref(app);
97
+ if (launched) {
98
+ return true;
99
+ }
100
+ }
101
+ }
102
+ return false;
103
+ #endif
104
+ }
105
+
106
+ std::string systemScheme() {
107
+ return adw_style_manager_get_dark(adw_style_manager_get_default()) ? "dark" : "light";
108
+ }
109
+
110
+ class GtkSystem final : public System {
111
+ public:
112
+ GtkSystem(const std::string &appId, std::string appData, std::function<void(int64_t)> clipboardChanged,
113
+ std::function<void(const std::string &)> colorSchemeChanged)
114
+ : appData_(std::move(appData)),
115
+ preferences_(makeKeyFileStore(appData_ + "/preferences.ini"), appId, false),
116
+ scheme_(systemScheme()),
117
+ clipboardChanged_(std::move(clipboardChanged)),
118
+ colorSchemeChanged_(std::move(colorSchemeChanged)) {
119
+ g_signal_connect(adw_style_manager_get_default(), "notify::dark", G_CALLBACK(+[](GObject *, GParamSpec *, gpointer data) {
120
+ auto *self = static_cast<GtkSystem *>(data);
121
+ std::string drawn = systemScheme();
122
+ self->scheme_.systemChanged(drawn);
123
+ // What GTK draws now, which is what the app sees: not the newest override, which may still
124
+ // wait on the main context behind the one being applied.
125
+ if (self->colorSchemeChanged_) {
126
+ self->colorSchemeChanged_(drawn);
127
+ }
128
+ }), this);
129
+ if (GdkDisplay *display = gdk_display_get_default()) {
130
+ clipboard_ = GDK_CLIPBOARD(g_object_ref(gdk_display_get_clipboard(display)));
131
+ g_signal_connect(clipboard_, "changed", G_CALLBACK(+[](GdkClipboard *, gpointer self) {
132
+ static_cast<GtkSystem *>(self)->readClipboard(true);
133
+ }), this);
134
+ readClipboard(false);
135
+ }
136
+ }
137
+ ~GtkSystem() override {
138
+ g_signal_handlers_disconnect_by_data(adw_style_manager_get_default(), this);
139
+ if (clipboard_) {
140
+ g_signal_handlers_disconnect_by_data(clipboard_, this);
141
+ g_object_unref(clipboard_);
142
+ }
143
+ *alive_ = false;
144
+ }
145
+
146
+ DeviceFacts facts() override {
147
+ // The distribution's name and version, not the kernel's: Platform.Version is "24.04" on
148
+ // Ubuntu 24.04, as OS_VERSION reads it.
149
+ auto release = distroRelease();
150
+ // The most preferred language, as Localization's first locale.
151
+ auto languages = preferredLanguageTags(getenv("LANGUAGE") ? getenv("LANGUAGE") : "", localeOf("LC_MESSAGES"));
152
+ std::string locale = languages.empty() ? "en" : languages.front();
153
+ return {.osVersion = release.versionId,
154
+ .systemName = release.name,
155
+ .interfaceIdiom = "desktop",
156
+ .locale = locale,
157
+ .rightToLeft = gtk_widget_get_default_direction() == GTK_TEXT_DIR_RTL};
158
+ }
159
+ void openUrl(const std::string &url, std::function<void(bool)> done) override {
160
+ openUri(url, std::move(done));
161
+ }
162
+ bool canOpenUrl(const std::string &url) override {
163
+ return canOpenUri(url, sandboxed(), [](const std::string &scheme) {
164
+ GAppInfo *app = g_app_info_get_default_for_uri_scheme(scheme.c_str());
165
+ if (app) {
166
+ g_object_unref(app);
167
+ }
168
+ return app != nullptr;
169
+ });
170
+ }
171
+ void openSettings(std::function<void(bool)> done) override {
172
+ // A Linux app has no page of its own in the desktop's settings: this opens them, as macOS opens
173
+ // System Settings.
174
+ onMainContext([done = std::move(done)] { done(launchSettingsApp()); });
175
+ }
176
+ std::string colorScheme() override {
177
+ return scheme_.current();
178
+ }
179
+ void setColorScheme(std::optional<std::string> scheme, std::function<void(std::string)> applied) override {
180
+ scheme_.set(scheme);
181
+ onMainContext([this, scheme, applied = std::move(applied)] {
182
+ AdwColorScheme adw = !scheme ? ADW_COLOR_SCHEME_DEFAULT : *scheme == "dark" ? ADW_COLOR_SCHEME_FORCE_DARK : ADW_COLOR_SCHEME_FORCE_LIGHT;
183
+ adw_style_manager_set_color_scheme(adw_style_manager_get_default(), adw);
184
+ scheme_.systemChanged(systemScheme());
185
+ applied(scheme_.applied(scheme));
186
+ });
187
+ }
188
+ void showAlert(const AlertRequest &request, std::function<void(int, std::string, std::string)> done) override {
189
+ onMainContext([request, done = std::move(done)]() mutable { ngn::gtk::showAlert(request, std::move(done), activeWindow()); });
190
+ }
191
+ // What the clipboard held as it last changed: GDK reads it asynchronously, which System's
192
+ // callers do not, so another app's change counts once its text has been read.
193
+ std::optional<std::string> clipboardText() override {
194
+ std::lock_guard lock(mutex_);
195
+ return text_;
196
+ }
197
+ void setClipboardText(const std::string &text) override {
198
+ {
199
+ std::lock_guard lock(mutex_);
200
+ text_ = text;
201
+ ++changes_;
202
+ }
203
+ onMainContext([this, alive = alive_, text] {
204
+ if (*alive && clipboard_) {
205
+ gdk_clipboard_set_text(clipboard_, text.c_str());
206
+ }
207
+ });
208
+ }
209
+ int64_t clipboardChangeCount() override {
210
+ return changes_;
211
+ }
212
+ std::optional<folly::dynamic> preference(const std::string &key) override {
213
+ return preferences_.get(key);
214
+ }
215
+ void setPreference(const std::string &key, std::optional<folly::dynamic> value) override {
216
+ preferences_.set(key, value);
217
+ }
218
+ std::optional<std::pair<double, double>> imageSize(const std::string &bytes) override {
219
+ GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
220
+ ImageSize size;
221
+ g_signal_connect(loader, "size-prepared", G_CALLBACK(sizePrepared), &size);
222
+ gdk_pixbuf_loader_write(loader, reinterpret_cast<const guchar *>(bytes.data()), bytes.size(), nullptr);
223
+ gdk_pixbuf_loader_close(loader, nullptr);
224
+ g_object_unref(loader);
225
+ return size;
226
+ }
227
+ void announce(const Announcement &announcement) override {
228
+ // Through the window in front, as macOS posts it on the application: GTK announces through a
229
+ // widget, and reads it out from its window.
230
+ onMainContext([announcement] { announceOn(GTK_WIDGET(portal::frontWindow()), announcement); });
231
+ }
232
+ std::string appDataDirectory() override {
233
+ g_mkdir_with_parents(appData_.c_str(), 0700);
234
+ return appData_;
235
+ }
236
+
237
+ private:
238
+ // Another app's text, read as the clipboard changes (`announce`), or at launch. Text the app set
239
+ // itself is already kept and counted. Only the latest read counts: one change can come as more
240
+ // than one signal, and reads can finish out of order.
241
+ void readClipboard(bool announce) {
242
+ if (gdk_clipboard_is_local(clipboard_)) {
243
+ return;
244
+ }
245
+ struct Read {
246
+ GtkSystem *self;
247
+ std::shared_ptr<bool> alive;
248
+ bool announce;
249
+ uint64_t number;
250
+ };
251
+ gdk_clipboard_read_text_async(clipboard_, nullptr, +[](GObject *source, GAsyncResult *result, gpointer data) {
252
+ std::unique_ptr<Read> read(static_cast<Read *>(data));
253
+ GError *error = nullptr;
254
+ gchar *text = gdk_clipboard_read_text_finish(GDK_CLIPBOARD(source), result, &error);
255
+ g_clear_error(&error);
256
+ if (!*read->alive || read->number != read->self->reads_) {
257
+ g_free(text);
258
+ return;
259
+ }
260
+ int64_t count = 0;
261
+ {
262
+ std::lock_guard lock(read->self->mutex_);
263
+ read->self->text_ = text ? std::optional<std::string>(text) : std::nullopt;
264
+ count = read->announce ? ++read->self->changes_ : read->self->changes_.load();
265
+ }
266
+ g_free(text);
267
+ if (read->announce && read->self->clipboardChanged_) {
268
+ read->self->clipboardChanged_(count);
269
+ }
270
+ }, new Read{this, alive_, announce, ++reads_});
271
+ }
272
+
273
+ std::string appData_;
274
+ JsonPreferences preferences_;
275
+ ColorSchemeOverride scheme_;
276
+ std::function<void(int64_t)> clipboardChanged_;
277
+ std::function<void(const std::string &)> colorSchemeChanged_;
278
+ GdkClipboard *clipboard_{nullptr};
279
+ std::shared_ptr<bool> alive_ = std::make_shared<bool>(true);
280
+ // Reads started, on the main context.
281
+ uint64_t reads_{0};
282
+ std::mutex mutex_;
283
+ std::optional<std::string> text_;
284
+ std::atomic<int64_t> changes_{1};
285
+ };
286
+
287
+ class NoMenus final : public MenuBackend {
288
+ public:
289
+ void setMenuBar(std::vector<MenuItem>) override {}
290
+ };
291
+
292
+ class GtkUndo final : public UndoManager {
293
+ public:
294
+ void registerAction(const std::string &label, std::function<void()> action) override {
295
+ auto record = [stacks = stacks_, label, action = std::move(action)]() mutable { stacks->registerAction(label, std::move(action)); };
296
+ // Inline on the UI thread (the loop's dispatch owns the main context there): an undo registers
297
+ // its redo while it runs, and a queued registration would find undoing() false by then.
298
+ if (g_main_context_is_owner(g_main_context_default())) {
299
+ record();
300
+ return;
301
+ }
302
+ onMainContext(std::move(record));
303
+ }
304
+ bool undoing() const override {
305
+ return stacks_->undoing();
306
+ }
307
+ void undo() override {
308
+ onMainContext([stacks = stacks_] { stacks->undo(); });
309
+ }
310
+ void redo() override {
311
+ onMainContext([stacks = stacks_] { stacks->redo(); });
312
+ }
313
+ void clear() override {
314
+ onMainContext([stacks = stacks_] { stacks->clear(); });
315
+ }
316
+
317
+ private:
318
+ std::shared_ptr<UndoStacks> stacks_ = std::make_shared<UndoStacks>();
319
+ };
320
+
321
+ } // namespace
322
+
323
+ bool canOpenUri(const std::string &uri, bool sandboxed, const std::function<bool(const std::string &scheme)> &hasHandler) {
324
+ gchar *scheme = g_uri_parse_scheme(uri.c_str());
325
+ if (!scheme) {
326
+ return false;
327
+ }
328
+ std::string name = scheme;
329
+ g_free(scheme);
330
+ return sandboxed || hasHandler(name);
331
+ }
332
+
333
+ std::shared_ptr<System> makeSystem(const std::string &appId, const std::string &appDataDirectory, std::function<void(int64_t)> clipboardChanged,
334
+ std::function<void(const std::string &)> colorSchemeChanged) {
335
+ return std::make_shared<GtkSystem>(appId, appDataDirectory, std::move(clipboardChanged), std::move(colorSchemeChanged));
336
+ }
337
+
338
+ std::shared_ptr<StringStore> makeKeyFileStore(const std::string &path) {
339
+ return std::make_shared<KeyFileStore>(path);
340
+ }
341
+
342
+ std::shared_ptr<MenuBackend> makeNoMenus() {
343
+ return std::make_shared<NoMenus>();
344
+ }
345
+
346
+ std::shared_ptr<UndoManager> makeUndoManager() {
347
+ return std::make_shared<GtkUndo>();
348
+ }
349
+
350
+ std::shared_ptr<ControlMeasurer> makeControlMeasurer() {
351
+ return std::make_shared<GtkControlMeasurer>();
352
+ }
353
+
354
+ } // namespace ngn::gtk
@@ -0,0 +1,46 @@
1
+ // What the GTK host passes the core for the OS, as far as this backend reaches it yet: the System
2
+ // over GLib and libadwaita, and stand-ins for what later steps add, each of which refuses or does
3
+ // nothing rather than pretend. Networking is the core's libcurl client (curl-network.h).
4
+ #pragma once
5
+
6
+ #include "ngn/app-menus.h"
7
+ #include "ngn/app-windows.h"
8
+ #include "ngn/native-controls.h"
9
+ #include "ngn/system.h"
10
+ #include "ngn/undo.h"
11
+
12
+ #include <functional>
13
+ #include <memory>
14
+ #include <string>
15
+
16
+ namespace ngn::gtk {
17
+
18
+ // Preferences in `<appData>/preferences.ini`, the colour scheme from libadwaita's style manager,
19
+ // the display's GdkClipboard, alerts logged and cancelled until the dialogs come. GDK reads the
20
+ // clipboard asynchronously and System does not, so the system keeps its text as it changes:
21
+ // `clipboardChanged` hears the new change count once another app's text has been read, on the main
22
+ // context. With no display, the clipboard is the app's own, in memory. `colorSchemeChanged` hears
23
+ // the scheme GTK draws whenever libadwaita's dark changes: the desktop's, through the settings
24
+ // portal, or an override as it is applied; on the main context. While a later override still
25
+ // waits to apply, colorScheme() already answers that one.
26
+ std::shared_ptr<System> makeSystem(const std::string &appId, const std::string &appDataDirectory,
27
+ std::function<void(int64_t count)> clipboardChanged = {},
28
+ std::function<void(const std::string &scheme)> colorSchemeChanged = {});
29
+
30
+ // Whether Linking can open `uri`: one with a scheme the desktop has an app for. Inside a sandbox the
31
+ // desktop portal decides as it opens it, so any scheme can be tried there.
32
+ bool canOpenUri(const std::string &uri, bool sandboxed, const std::function<bool(const std::string &scheme)> &hasHandler);
33
+
34
+ // Strings in a key file at `path`, written on each change.
35
+ std::shared_ptr<StringStore> makeKeyFileStore(const std::string &path);
36
+
37
+ std::shared_ptr<MenuBackend> makeNoMenus();
38
+
39
+ // The app's undo history (`UndoStack`): the core's UndoStacks, which GTK has no manager of its own
40
+ // for, kept on the UI thread whichever thread asks, as AppKit's NSUndoManager is.
41
+ std::shared_ptr<UndoManager> makeUndoManager();
42
+
43
+ // How big GTK draws each native control (native-controls.h).
44
+ std::shared_ptr<ControlMeasurer> makeControlMeasurer();
45
+
46
+ } // namespace ngn::gtk