@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,58 @@
1
+ // The native controls of native-controls.h as GTK and libadwaita draw them, each a widget filling
2
+ // its view, with the events and commands the AppKit host sends and takes:
3
+ //
4
+ // <native-button> GtkButton: `default` is a suggested action, `destructive` a destructive
5
+ // one, `plain` flat, and one with no kind a plain push button
6
+ // <switch> GtkSwitch
7
+ // <activity-indicator> AdwSpinner on libadwaita 1.6 and later, found at run time, else GtkSpinner
8
+ // <segmented-control> AdwToggleGroup on libadwaita 1.7 and later, found at run time, else a
9
+ // linked row of GtkToggleButtons
10
+ // <date-time-picker> a GtkMenuButton showing the date, with a GtkCalendar in its popover; the
11
+ // time modes show the time, and pick nothing yet
12
+ // <search-field> GtkSearchEntry
13
+ //
14
+ // `controlSize` and `sfSymbol` are AppKit's and do nothing here, as on Android.
15
+ #pragma once
16
+
17
+ #include "view-widget.h"
18
+
19
+ #include "ngn/native-controls.h"
20
+
21
+ #include <folly/dynamic.h>
22
+
23
+ #include <gtk/gtk.h>
24
+
25
+ #include <functional>
26
+ #include <optional>
27
+ #include <string>
28
+
29
+ namespace ngn::gtk {
30
+
31
+ // The desktop's clock format, as GNOME's clock-format gives it (clock-format.h): a 12-hour clock
32
+ // or a 24-hour one, or nothing for the locale's time format to decide. Date pickers shown follow a
33
+ // change. The UI thread.
34
+ void useClockFormat(std::optional<bool> twelveHour);
35
+
36
+ // Where the controls' events go in place of each view's emitter, for tests, which have no event
37
+ // dispatcher to read them from; null for the emitters.
38
+ using ControlEventSink = std::function<void(facebook::react::Tag tag, const std::string &name, const folly::dynamic &payload)>;
39
+ void setControlEventSink(ControlEventSink sink);
40
+
41
+ // Makes the control of `view`, child of `widget`, when its component is one; else does nothing.
42
+ void makeControl(GtkWidget *widget, ViewData &view);
43
+ // Applies `view.props` to its control, if it has one.
44
+ void applyControlProps(ViewData &view);
45
+ // The switch's setValue and setNativeValue, and the search field's focus and blur.
46
+ void controlCommand(ViewData &view, const std::string &name, const folly::dynamic &args);
47
+
48
+ // Whether libadwaita has AdwSpinner (1.6) and AdwToggleGroup (1.7) at run time.
49
+ bool haveAdwSpinner();
50
+ bool haveAdwToggleGroup();
51
+
52
+ // Sizes the controls as GTK lays them out, on the UI thread only (ControlSizes calls it there).
53
+ class GtkControlMeasurer final : public ControlMeasurer {
54
+ public:
55
+ facebook::react::Size measure(ControlKind kind, const NativeControlProps &props) const override;
56
+ };
57
+
58
+ } // namespace ngn::gtk
@@ -0,0 +1,72 @@
1
+ #include "network-state.h"
2
+
3
+ #include "dbus-properties.h"
4
+
5
+ #include <set>
6
+
7
+ namespace ngn::gtk {
8
+
9
+ namespace {
10
+
11
+ // GLib's monitor for the route, NetworkManager's PrimaryConnectionType for its kind. Both report
12
+ // on the main context, so one change at a time reaches the watch, in order.
13
+ class NetworkManagerMonitor final : public PathMonitor {
14
+ public:
15
+ explicit NetworkManagerMonitor(GDBusConnection *connection)
16
+ : manager_(G_BUS_TYPE_SYSTEM, connection, "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager",
17
+ "org.freedesktop.NetworkManager") {}
18
+
19
+ std::function<void()> watch(std::function<void(const NetworkPath &)> changed) override {
20
+ auto heap = std::make_shared<std::function<void(const NetworkPath &)>>(std::move(changed));
21
+ GNetworkMonitor *monitor = g_network_monitor_get_default();
22
+ auto report = [this, heap, monitor] { (*heap)(networkPathOf(g_network_monitor_get_network_available(monitor), connectionType())); };
23
+ report();
24
+ auto *listener = new std::function<void()>(report);
25
+ gulong handler = g_signal_connect_data(
26
+ monitor, "network-changed", G_CALLBACK(+[](GNetworkMonitor *, gboolean, gpointer data) { (*static_cast<std::function<void()> *>(data))(); }),
27
+ listener, [](gpointer data, GClosure *) { delete static_cast<std::function<void()> *>(data); }, GConnectFlags(0));
28
+ auto stop = manager_.watch(report);
29
+ return [monitor, handler, stop] {
30
+ stop();
31
+ g_signal_handler_disconnect(monitor, handler);
32
+ };
33
+ }
34
+
35
+ private:
36
+ std::string connectionType() {
37
+ GVariant *type = manager_.get("PrimaryConnectionType");
38
+ std::string out = type && g_variant_is_of_type(type, G_VARIANT_TYPE_STRING) ? g_variant_get_string(type, nullptr) : "";
39
+ if (type) {
40
+ g_variant_unref(type);
41
+ }
42
+ return out;
43
+ }
44
+
45
+ DBusProperties manager_;
46
+ };
47
+
48
+ } // namespace
49
+
50
+ NetworkPath networkPathOf(bool available, const std::string &connectionType) {
51
+ NetworkPath path{.satisfied = available};
52
+ if (!available) {
53
+ return path;
54
+ }
55
+ static const std::set<std::string> cellular{"gsm", "cdma"};
56
+ if (connectionType == "802-11-wireless") {
57
+ path.wifi = true;
58
+ } else if (connectionType == "802-3-ethernet") {
59
+ path.ethernet = true;
60
+ } else if (cellular.count(connectionType)) {
61
+ path.cellular = true;
62
+ } else {
63
+ path.other = true;
64
+ }
65
+ return path;
66
+ }
67
+
68
+ std::shared_ptr<PathMonitor> makePathMonitor(GDBusConnection *connection) {
69
+ return std::make_shared<NetworkManagerMonitor>(connection);
70
+ }
71
+
72
+ } // namespace ngn::gtk
@@ -0,0 +1,24 @@
1
+ // The OS half of NgNativeNetwork on Linux (the core's network-state.h): whether a route exists from
2
+ // GLib's GNetworkMonitor (the NetworkMonitor portal inside a sandbox), and its type from
3
+ // NetworkManager's primary connection. Without NetworkManager (a sandbox that may not reach it, or
4
+ // a system without it) a route's type is OTHER.
5
+ #pragma once
6
+
7
+ #include "ngn/network-state.h"
8
+
9
+ #include <gio/gio.h>
10
+
11
+ #include <memory>
12
+ #include <string>
13
+
14
+ namespace ngn::gtk {
15
+
16
+ // The path for a route that is there or not, of NetworkManager's connection type
17
+ // ("802-11-wireless", "802-3-ethernet", "gsm"), empty where it is not known.
18
+ NetworkPath networkPathOf(bool available, const std::string &connectionType);
19
+
20
+ // Made on the main context. `connection`, in the tests, is a peer-to-peer connection to a stand-in
21
+ // NetworkManager, in place of the system bus.
22
+ std::shared_ptr<PathMonitor> makePathMonitor(GDBusConnection *connection = nullptr);
23
+
24
+ } // namespace ngn::gtk
@@ -0,0 +1,235 @@
1
+ #include "notifications.h"
2
+
3
+ #include "portal.h"
4
+ #include "system-access.h"
5
+
6
+ #include "ngn/notification-schedule.h"
7
+
8
+ #include <atomic>
9
+ #include <cstdio>
10
+ #include <optional>
11
+
12
+ namespace ngn::gtk {
13
+
14
+ namespace {
15
+
16
+ constexpr const char *kPortal = "org.freedesktop.portal.Notification";
17
+
18
+ // What GNotification and the portal show as the body: the subtitle, if any, on a line above it, as
19
+ // neither has a subtitle of its own.
20
+ std::string bodyOf(const NotificationRequest &request) {
21
+ if (request.subtitle.empty()) {
22
+ return request.body;
23
+ }
24
+ return request.body.empty() ? request.subtitle : request.subtitle + "\n" + request.body;
25
+ }
26
+
27
+ NotificationSchedule::Clock glibClock() {
28
+ return {
29
+ .now = [] { return g_get_real_time() / 1000.0; },
30
+ .after =
31
+ [](double ms, std::function<void()> fn) {
32
+ auto cancelled = std::make_shared<std::atomic<bool>>(false);
33
+ struct Timer {
34
+ std::function<void()> fn;
35
+ std::shared_ptr<std::atomic<bool>> cancelled;
36
+ };
37
+ g_timeout_add_full(
38
+ G_PRIORITY_DEFAULT, static_cast<guint>(ms),
39
+ +[](gpointer data) -> gboolean {
40
+ auto *timer = static_cast<Timer *>(data);
41
+ if (!*timer->cancelled) {
42
+ timer->fn();
43
+ }
44
+ return G_SOURCE_REMOVE;
45
+ },
46
+ new Timer{std::move(fn), cancelled}, +[](gpointer data) { delete static_cast<Timer *>(data); });
47
+ return std::function<void()>([cancelled] { *cancelled = true; });
48
+ },
49
+ };
50
+ }
51
+
52
+ class GtkNotificationCenter final : public NotificationCenter, public std::enable_shared_from_this<GtkNotificationCenter> {
53
+ public:
54
+ GtkNotificationCenter(GApplication *application, std::shared_ptr<NotificationEvents> events, std::function<void()> raise,
55
+ std::function<void(int)> showBadge)
56
+ : application_(G_APPLICATION(g_object_ref(application))),
57
+ events_(std::move(events)),
58
+ raise_(std::move(raise)),
59
+ showBadge_(std::move(showBadge)) {}
60
+
61
+ ~GtkNotificationCenter() override {
62
+ if (unsubscribe_) {
63
+ unsubscribe_();
64
+ }
65
+ g_action_map_remove_action(G_ACTION_MAP(application_), kNotificationAction);
66
+ g_object_unref(application_);
67
+ }
68
+
69
+ void start(const std::string &appData, NotificationSchedule::Clock clock) {
70
+ std::weak_ptr<GtkNotificationCenter> weak = weak_from_this();
71
+ g_mkdir_with_parents(appData.c_str(), 0700);
72
+ schedule_ = std::make_unique<NotificationSchedule>(appData + "/notifications.json", std::move(clock), [weak](const NotificationRequest &request) {
73
+ if (auto self = weak.lock()) {
74
+ self->show(request);
75
+ }
76
+ });
77
+ // A click on a GNotification, and on one shown before a launch, which D-Bus activation runs as
78
+ // the app's action.
79
+ GSimpleAction *action = g_simple_action_new(kNotificationAction, G_VARIANT_TYPE_STRING);
80
+ g_signal_connect_data(
81
+ action, "activate", G_CALLBACK(+[](GSimpleAction *, GVariant *parameter, gpointer data) {
82
+ if (auto self = static_cast<std::weak_ptr<GtkNotificationCenter> *>(data)->lock()) {
83
+ self->clicked(g_variant_get_string(parameter, nullptr));
84
+ }
85
+ }),
86
+ new std::weak_ptr<GtkNotificationCenter>(weak), +[](gpointer data, GClosure *) { delete static_cast<std::weak_ptr<GtkNotificationCenter> *>(data); },
87
+ GConnectFlags(0));
88
+ g_action_map_add_action(G_ACTION_MAP(application_), G_ACTION(action));
89
+ g_object_unref(action);
90
+ // A click on one the portal showed, for an app it does not activate: ActionInvoked(id, action,
91
+ // parameter).
92
+ unsubscribe_ = portal::subscribe(kPortal, "ActionInvoked", [weak](GVariant *parameters) {
93
+ if (!g_variant_is_of_type(parameters, G_VARIANT_TYPE("(ssav)"))) {
94
+ return;
95
+ }
96
+ const gchar *id = nullptr, *name = nullptr;
97
+ g_variant_get(parameters, "(&s&s@av)", &id, &name, nullptr);
98
+ std::string action = name;
99
+ if (auto self = weak.lock(); self && (action == kNotificationAction || action == std::string("app.") + kNotificationAction)) {
100
+ self->clicked(id);
101
+ }
102
+ });
103
+ schedule_->start();
104
+ }
105
+
106
+ bool canPost() override {
107
+ return g_application_get_application_id(application_) != nullptr;
108
+ }
109
+ void authorization(std::function<void(NotificationAuthorization)> done) override {
110
+ done(NotificationAuthorization::Authorized);
111
+ }
112
+ void requestAuthorization(std::function<void(NotificationAuthorization)> done) override {
113
+ done(NotificationAuthorization::Authorized);
114
+ }
115
+ void add(const NotificationRequest &request, std::function<void(std::optional<std::string>)> done) override {
116
+ if (!schedule_->add(request)) {
117
+ done("The notification schedule could not be saved");
118
+ return;
119
+ }
120
+ done(std::nullopt);
121
+ }
122
+ void pending(std::function<void(std::vector<NotificationRequest>)> done) override {
123
+ done(schedule_->pending());
124
+ }
125
+ void delivered(std::function<void(std::vector<std::pair<double, NotificationRequest>>)> done) override {
126
+ done(schedule_->delivered());
127
+ }
128
+ void removePending(const std::vector<std::string> &identifiers) override {
129
+ schedule_->removePending(identifiers);
130
+ }
131
+ void removeAllPending() override {
132
+ schedule_->removeAllPending();
133
+ }
134
+ void removeDelivered(const std::vector<std::string> &identifiers) override {
135
+ withdraw(schedule_->removeDelivered(identifiers));
136
+ }
137
+ void removeAllDelivered() override {
138
+ withdraw(schedule_->removeAllDelivered());
139
+ }
140
+ std::optional<double> nextDate(const NotificationTrigger &trigger) override {
141
+ return firstFireDate(trigger, g_get_real_time() / 1000.0);
142
+ }
143
+ int badge() override {
144
+ return badge_;
145
+ }
146
+ void setBadge(int count) override {
147
+ badge_ = count;
148
+ if (showBadge_) {
149
+ showBadge_(count);
150
+ }
151
+ }
152
+
153
+ private:
154
+ // On the main context, as the schedule's timer fires.
155
+ void show(const NotificationRequest &request) {
156
+ if (request.badge) {
157
+ setBadge(*request.badge);
158
+ }
159
+ auto shown = schedule_->deliveredOne(request.identifier);
160
+ events_->received(shown ? shown->first : g_get_real_time() / 1000.0, request);
161
+ GVariantBuilder *options = g_variant_builder_new(G_VARIANT_TYPE_VARDICT);
162
+ g_variant_builder_add(options, "{sv}", "title", g_variant_new_string(request.title.c_str()));
163
+ g_variant_builder_add(options, "{sv}", "body", g_variant_new_string(bodyOf(request).c_str()));
164
+ // Inside a sandbox the portal knows the app's id, and runs an `app.` action on the app through
165
+ // D-Bus activation, launching it if it must. Outside one it may know none (xdg-desktop-portal
166
+ // 1.12 does not), so a plain action, which it sends back as ActionInvoked.
167
+ std::string action = insideFlatpak() ? std::string("app.") + kNotificationAction : kNotificationAction;
168
+ g_variant_builder_add(options, "{sv}", "default-action", g_variant_new_string(action.c_str()));
169
+ g_variant_builder_add(options, "{sv}", "default-action-target", g_variant_new_string(request.identifier.c_str()));
170
+ std::weak_ptr<GtkNotificationCenter> weak = weak_from_this();
171
+ portal::request(kPortal, "AddNotification", g_variant_new("(s)", request.identifier.c_str()), options, nullptr,
172
+ [weak, request](portal::Outcome outcome, GVariant *) {
173
+ if (auto self = weak.lock(); self && outcome != portal::Outcome::Done) {
174
+ self->sendNotification(request);
175
+ }
176
+ }, false);
177
+ }
178
+
179
+ // GLib's own: the portal inside a sandbox, else org.gtk.Notifications or
180
+ // org.freedesktop.Notifications.
181
+ void sendNotification(const NotificationRequest &request) {
182
+ GNotification *notification = g_notification_new(request.title.c_str());
183
+ auto body = bodyOf(request);
184
+ if (!body.empty()) {
185
+ g_notification_set_body(notification, body.c_str());
186
+ }
187
+ g_notification_set_default_action_and_target(notification, (std::string("app.") + kNotificationAction).c_str(), "s",
188
+ request.identifier.c_str());
189
+ g_application_send_notification(application_, request.identifier.c_str(), notification);
190
+ g_object_unref(notification);
191
+ }
192
+
193
+ // From the portal, or from GLib's notifications where it does not answer: GLib picks its server
194
+ // once for the process, as it first sends or withdraws one.
195
+ void withdraw(const std::vector<std::string> &identifiers) {
196
+ std::weak_ptr<GtkNotificationCenter> weak = weak_from_this();
197
+ for (const auto &id : identifiers) {
198
+ portal::request(kPortal, "RemoveNotification", g_variant_new("(s)", id.c_str()), nullptr, nullptr,
199
+ [weak, id](portal::Outcome outcome, GVariant *) {
200
+ if (auto self = weak.lock(); self && outcome != portal::Outcome::Done) {
201
+ g_application_withdraw_notification(self->application_, id.c_str());
202
+ }
203
+ }, false);
204
+ }
205
+ }
206
+
207
+ void clicked(const std::string &identifier) {
208
+ if (auto shown = schedule_->deliveredOne(identifier)) {
209
+ events_->responded(shown->first, shown->second);
210
+ }
211
+ if (raise_) {
212
+ raise_();
213
+ }
214
+ }
215
+
216
+ GApplication *application_;
217
+ std::shared_ptr<NotificationEvents> events_;
218
+ std::function<void()> raise_;
219
+ std::function<void(int)> showBadge_;
220
+ std::unique_ptr<NotificationSchedule> schedule_;
221
+ std::function<void()> unsubscribe_;
222
+ std::atomic<int> badge_{0};
223
+ };
224
+
225
+ } // namespace
226
+
227
+ std::shared_ptr<NotificationCenter> makeNotificationCenter(GApplication *application, const std::string &appData,
228
+ std::shared_ptr<NotificationEvents> events, std::function<void()> raise,
229
+ std::function<void(int)> showBadge) {
230
+ auto center = std::make_shared<GtkNotificationCenter>(application, std::move(events), std::move(raise), std::move(showBadge));
231
+ center->start(appData, glibClock());
232
+ return center;
233
+ }
234
+
235
+ } // namespace ngn::gtk
@@ -0,0 +1,30 @@
1
+ // Local notifications on GTK (ngn/notifications.h): each shown through the Notification portal,
2
+ // or by GApplication's own GNotification (org.gtk.Notifications or org.freedesktop.Notifications)
3
+ // where no portal answers. Linux has no scheduler, so triggers fire from the core's
4
+ // NotificationSchedule, kept in the app's data folder. A click on one, by the portal's
5
+ // ActionInvoked or the app's `ngn-notification` action (which D-Bus activation runs on a launch),
6
+ // raises the app and is the response the app hears. There is no permission to ask for: an app with
7
+ // an application id posts, and the desktop's settings decide whether it shows. No push.
8
+ #pragma once
9
+
10
+ #include "ngn/notifications.h"
11
+
12
+ #include <gio/gio.h>
13
+
14
+ #include <functional>
15
+ #include <memory>
16
+ #include <string>
17
+
18
+ namespace ngn::gtk {
19
+
20
+ // The app's action a notification's click runs, with the notification's identifier.
21
+ inline constexpr const char *kNotificationAction = "ngn-notification";
22
+
23
+ // `raise` brings the app to the front, and `showBadge`, when given, draws the badge count where the
24
+ // desktop shows one (the launcher's). The schedule starts at once, so what came due while the app
25
+ // was closed shows as it launches.
26
+ std::shared_ptr<NotificationCenter> makeNotificationCenter(GApplication *application, const std::string &appData,
27
+ std::shared_ptr<NotificationEvents> events, std::function<void()> raise,
28
+ std::function<void(int count)> showBadge = {});
29
+
30
+ } // namespace ngn::gtk