@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,338 @@
1
+ #include "printing.h"
2
+
3
+ #include "app-windows.h"
4
+ #include "portal.h"
5
+
6
+ #include <gio/gunixfdlist.h>
7
+ #ifdef NGN_UNIX_PRINT
8
+ #include <gtk/gtkunixprint.h>
9
+ #endif
10
+
11
+ #include <fcntl.h>
12
+ #include <unistd.h>
13
+
14
+ #include <algorithm>
15
+ #include <cstdio>
16
+ #include <memory>
17
+
18
+ namespace ngn::gtk {
19
+
20
+ namespace {
21
+
22
+ constexpr const char *kPrintPortal = "org.freedesktop.portal.Print";
23
+
24
+ // Draws the content into a page's printable area, `width` by `height` points.
25
+ using Drawer = std::function<void(cairo_t *cr, double width, double height)>;
26
+
27
+ // What `widget` draws now, as GSK's nodes, which cairo draws as vectors into the page: scaled down
28
+ // to fit it, never up.
29
+ std::optional<Drawer> widgetDrawer(GtkWidget *widget) {
30
+ int width = gtk_widget_get_width(widget), height = gtk_widget_get_height(widget);
31
+ if (width <= 0 || height <= 0) {
32
+ return std::nullopt;
33
+ }
34
+ GdkPaintable *paintable = gtk_widget_paintable_new(widget);
35
+ GtkSnapshot *snapshot = gtk_snapshot_new();
36
+ gdk_paintable_snapshot(paintable, snapshot, width, height);
37
+ g_object_unref(paintable);
38
+ GskRenderNode *node = gtk_snapshot_free_to_node(snapshot);
39
+ if (!node) {
40
+ return std::nullopt;
41
+ }
42
+ auto held = std::shared_ptr<GskRenderNode>(node, gsk_render_node_unref);
43
+ return [held, width, height](cairo_t *cr, double pageWidth, double pageHeight) {
44
+ double scale = std::min({pageWidth / width, pageHeight / height, 1.0});
45
+ cairo_scale(cr, scale, scale);
46
+ gsk_render_node_draw(held.get(), cr);
47
+ };
48
+ }
49
+
50
+ // An image's pixels, scaled to fit the page either way.
51
+ std::optional<Drawer> imageDrawer(const std::string &bytes, std::string &error) {
52
+ GBytes *data = g_bytes_new(bytes.data(), bytes.size());
53
+ GError *problem = nullptr;
54
+ GdkTexture *texture = gdk_texture_new_from_bytes(data, &problem);
55
+ g_bytes_unref(data);
56
+ if (!texture) {
57
+ error = problem ? std::string("the image could not be read: ") + problem->message : "the image could not be read";
58
+ g_clear_error(&problem);
59
+ return std::nullopt;
60
+ }
61
+ int width = gdk_texture_get_width(texture), height = gdk_texture_get_height(texture);
62
+ cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
63
+ gdk_texture_download(texture, cairo_image_surface_get_data(surface), cairo_image_surface_get_stride(surface));
64
+ cairo_surface_mark_dirty(surface);
65
+ g_object_unref(texture);
66
+ auto held = std::shared_ptr<cairo_surface_t>(surface, cairo_surface_destroy);
67
+ return [held, width, height](cairo_t *cr, double pageWidth, double pageHeight) {
68
+ double scale = std::min(pageWidth / width, pageHeight / height);
69
+ cairo_scale(cr, scale, scale);
70
+ cairo_set_source_surface(cr, held.get(), 0, 0);
71
+ cairo_paint(cr);
72
+ };
73
+ }
74
+
75
+ struct Operation {
76
+ Drawer draw;
77
+ PrintDone done;
78
+ bool answered{false};
79
+ void finish(const std::string &error) {
80
+ if (!answered) {
81
+ answered = true;
82
+ done(error);
83
+ }
84
+ }
85
+ };
86
+
87
+ // One page through GtkPrintOperation: its dialog on `parent`, or written to `exportPath` with none.
88
+ // Answers once the operation is done.
89
+ void runOperation(Drawer draw, bool landscape, GtkWindow *parent, const std::string &exportPath, PrintDone done) {
90
+ GtkPrintOperation *operation = gtk_print_operation_new();
91
+ gtk_print_operation_set_n_pages(operation, 1);
92
+ gtk_print_operation_set_unit(operation, GTK_UNIT_POINTS);
93
+ gtk_print_operation_set_allow_async(operation, exportPath.empty());
94
+ GtkPageSetup *page = gtk_page_setup_new();
95
+ gtk_page_setup_set_orientation(page, landscape ? GTK_PAGE_ORIENTATION_LANDSCAPE : GTK_PAGE_ORIENTATION_PORTRAIT);
96
+ gtk_print_operation_set_default_page_setup(operation, page);
97
+ g_object_unref(page);
98
+ auto *state = new Operation{std::move(draw), std::move(done)};
99
+ g_signal_connect(operation, "draw-page", G_CALLBACK(+[](GtkPrintOperation *, GtkPrintContext *context, gint, gpointer data) {
100
+ static_cast<Operation *>(data)->draw(gtk_print_context_get_cairo_context(context), gtk_print_context_get_width(context),
101
+ gtk_print_context_get_height(context));
102
+ }), state);
103
+ g_signal_connect_data(operation, "done", G_CALLBACK(+[](GtkPrintOperation *operation, GtkPrintOperationResult result, gpointer data) {
104
+ std::string error;
105
+ if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
106
+ GError *problem = nullptr;
107
+ gtk_print_operation_get_error(operation, &problem);
108
+ error = problem ? problem->message : "printing failed";
109
+ g_clear_error(&problem);
110
+ }
111
+ static_cast<Operation *>(data)->finish(error);
112
+ }), state, +[](gpointer data, GClosure *) { delete static_cast<Operation *>(data); }, GConnectFlags(0));
113
+ auto action = GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG;
114
+ if (!exportPath.empty()) {
115
+ gtk_print_operation_set_export_filename(operation, exportPath.c_str());
116
+ action = GTK_PRINT_OPERATION_ACTION_EXPORT;
117
+ }
118
+ GError *error = nullptr;
119
+ auto result = gtk_print_operation_run(operation, action, parent, &error);
120
+ if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
121
+ state->finish(error ? error->message : "printing failed");
122
+ } else if (result != GTK_PRINT_OPERATION_RESULT_IN_PROGRESS) {
123
+ state->finish("");
124
+ }
125
+ g_clear_error(&error);
126
+ g_object_unref(operation);
127
+ }
128
+
129
+ GtkWindow *windowOf(GtkWidget *widget) {
130
+ GtkRoot *root = widget ? gtk_widget_get_root(widget) : nullptr;
131
+ return root && GTK_IS_WINDOW(root) ? GTK_WINDOW(root) : portal::frontWindow();
132
+ }
133
+
134
+ // `bytes` in a file of its own, already unlinked, open for reading: the fd the Print portal reads.
135
+ int pdfFd(const std::string &bytes) {
136
+ gchar *path = nullptr;
137
+ int fd = g_file_open_tmp("ngn-print-XXXXXX.pdf", &path, nullptr);
138
+ if (fd < 0) {
139
+ return -1;
140
+ }
141
+ unlink(path);
142
+ g_free(path);
143
+ if (write(fd, bytes.data(), bytes.size()) != static_cast<ssize_t>(bytes.size()) || lseek(fd, 0, SEEK_SET) != 0) {
144
+ close(fd);
145
+ return -1;
146
+ }
147
+ return fd;
148
+ }
149
+
150
+ #ifdef NGN_UNIX_PRINT
151
+ // A job sent to a printer, and the file it prints, removed once sent.
152
+ using SentJob = std::pair<PrintDone, std::string>;
153
+
154
+ // GTK's print dialog, where no portal answers: the PDF goes as it is to a printer that takes PDF.
155
+ void printPdfWithGtk(const std::string &bytes, bool landscape, PrintDone done) {
156
+ GtkWidget *dialog = gtk_print_unix_dialog_new("Print", portal::frontWindow());
157
+ gtk_print_unix_dialog_set_manual_capabilities(GTK_PRINT_UNIX_DIALOG(dialog), GTK_PRINT_CAPABILITY_GENERATE_PDF);
158
+ GtkPageSetup *page = gtk_page_setup_new();
159
+ gtk_page_setup_set_orientation(page, landscape ? GTK_PAGE_ORIENTATION_LANDSCAPE : GTK_PAGE_ORIENTATION_PORTRAIT);
160
+ gtk_print_unix_dialog_set_page_setup(GTK_PRINT_UNIX_DIALOG(dialog), page);
161
+ g_object_unref(page);
162
+ struct Pending {
163
+ std::string bytes;
164
+ PrintDone done;
165
+ };
166
+ g_signal_connect_data(dialog, "response", G_CALLBACK(+[](GtkDialog *dialog, gint response, gpointer data) {
167
+ auto *pending = static_cast<Pending *>(data);
168
+ auto done = std::move(pending->done);
169
+ if (response != GTK_RESPONSE_OK) {
170
+ gtk_window_destroy(GTK_WINDOW(dialog));
171
+ done("");
172
+ return;
173
+ }
174
+ auto *chooser = GTK_PRINT_UNIX_DIALOG(dialog);
175
+ GtkPrinter *printer = gtk_print_unix_dialog_get_selected_printer(chooser);
176
+ if (!printer || !gtk_printer_accepts_pdf(printer)) {
177
+ gtk_window_destroy(GTK_WINDOW(dialog));
178
+ done("the chosen printer does not take PDF");
179
+ return;
180
+ }
181
+ GtkPrintSettings *settings = gtk_print_unix_dialog_get_settings(chooser);
182
+ GtkPrintJob *job = gtk_print_job_new("Print", printer, settings, gtk_print_unix_dialog_get_page_setup(chooser));
183
+ g_object_unref(settings);
184
+ gchar *path = nullptr;
185
+ int fd = g_file_open_tmp("ngn-print-XXXXXX.pdf", &path, nullptr);
186
+ bool written = fd >= 0 && write(fd, pending->bytes.data(), pending->bytes.size()) == static_cast<ssize_t>(pending->bytes.size());
187
+ if (fd >= 0) {
188
+ close(fd);
189
+ }
190
+ GError *error = nullptr;
191
+ if (!written || !gtk_print_job_set_source_file(job, path, &error)) {
192
+ done(error ? error->message : "the PDF could not be written for the printer");
193
+ g_clear_error(&error);
194
+ } else {
195
+ auto *heap = new SentJob(std::move(done), path);
196
+ gtk_print_job_send(job, +[](GtkPrintJob *, gpointer data, const GError *error) {
197
+ std::unique_ptr<SentJob> sent(static_cast<SentJob *>(data));
198
+ unlink(sent->second.c_str());
199
+ sent->first(error ? error->message : "");
200
+ }, heap, nullptr);
201
+ }
202
+ g_free(path);
203
+ g_object_unref(job);
204
+ gtk_window_destroy(GTK_WINDOW(dialog));
205
+ }), new Pending{bytes, std::move(done)}, +[](gpointer data, GClosure *) { delete static_cast<Pending *>(data); }, GConnectFlags(0));
206
+ gtk_window_present(GTK_WINDOW(dialog));
207
+ }
208
+ #endif
209
+
210
+ // PreparePrint asks the user for a printer and settings; Print then sends the file with the token
211
+ // it answered.
212
+ void printPdf(const std::string &bytes, bool landscape, PrintDone done) {
213
+ portal::parentHandle(portal::frontWindow(), [bytes, landscape, done = std::move(done)](const std::string &parent, std::function<void()> drop) mutable {
214
+ GVariantBuilder settings, page;
215
+ g_variant_builder_init(&settings, G_VARIANT_TYPE_VARDICT);
216
+ g_variant_builder_init(&page, G_VARIANT_TYPE_VARDICT);
217
+ g_variant_builder_add(&page, "{sv}", "Orientation", g_variant_new_string(landscape ? "landscape" : "portrait"));
218
+ auto shared = std::make_shared<std::function<void()>>(std::move(drop));
219
+ portal::request(
220
+ kPrintPortal, "PreparePrint",
221
+ g_variant_new("(ss@a{sv}@a{sv})", parent.c_str(), "Print", g_variant_builder_end(&settings), g_variant_builder_end(&page)),
222
+ g_variant_builder_new(G_VARIANT_TYPE_VARDICT), nullptr,
223
+ [bytes, landscape, parent, shared, done = std::move(done)](portal::Outcome outcome, GVariant *results) mutable {
224
+ if (outcome == portal::Outcome::Absent) {
225
+ (*shared)();
226
+ #ifdef NGN_UNIX_PRINT
227
+ printPdfWithGtk(bytes, landscape, std::move(done));
228
+ #else
229
+ done("printing a PDF needs the desktop's print portal");
230
+ #endif
231
+ return;
232
+ }
233
+ guint32 token = 0;
234
+ if (outcome != portal::Outcome::Done || !results || !g_variant_lookup(results, "token", "u", &token)) {
235
+ (*shared)();
236
+ done(outcome == portal::Outcome::Failed ? "the print portal could not print" : "");
237
+ return;
238
+ }
239
+ int fd = pdfFd(bytes);
240
+ if (fd < 0) {
241
+ (*shared)();
242
+ done("the PDF could not be written for the printer");
243
+ return;
244
+ }
245
+ GUnixFDList *fds = g_unix_fd_list_new_from_array(&fd, 1);
246
+ GVariantBuilder *options = g_variant_builder_new(G_VARIANT_TYPE_VARDICT);
247
+ g_variant_builder_add(options, "{sv}", "token", g_variant_new_uint32(token));
248
+ portal::request(kPrintPortal, "Print", g_variant_new("(ssh)", parent.c_str(), "Print", 0), options, fds,
249
+ [shared, done = std::move(done)](portal::Outcome outcome, GVariant *) {
250
+ (*shared)();
251
+ done(outcome == portal::Outcome::Failed ? "the print portal could not print" : "");
252
+ });
253
+ g_object_unref(fds);
254
+ });
255
+ });
256
+ }
257
+
258
+ class GtkPrintSystem final : public PrintSystem {
259
+ public:
260
+ explicit GtkPrintSystem(std::function<GtkWidget *(facebook::react::SurfaceId)> surfaceWidget) : surfaceWidget_(std::move(surfaceWidget)) {}
261
+
262
+ void print(Document document, bool landscape, PrintDone done) override {
263
+ onMainContext([document = std::move(document), landscape, done = std::move(done)]() mutable {
264
+ switch (document.kind) {
265
+ case Document::Kind::Html:
266
+ done("printing HTML is not available on Linux");
267
+ return;
268
+ case Document::Kind::Pdf:
269
+ printPdf(document.content, landscape, std::move(done));
270
+ return;
271
+ case Document::Kind::Image: {
272
+ std::string error;
273
+ auto draw = imageDrawer(document.content, error);
274
+ if (!draw) {
275
+ done(error);
276
+ return;
277
+ }
278
+ runOperation(std::move(*draw), landscape, portal::frontWindow(), "", std::move(done));
279
+ return;
280
+ }
281
+ }
282
+ });
283
+ }
284
+
285
+ void printWindow(facebook::react::SurfaceId surface, bool landscape, PrintDone done) override {
286
+ onMainContext([this, surface, landscape, done = std::move(done)]() mutable {
287
+ GtkWidget *widget = surfaceWidget_ ? surfaceWidget_(surface) : nullptr;
288
+ if (!widget) {
289
+ done("no window shows that surface");
290
+ return;
291
+ }
292
+ printWidget(widget, landscape, std::move(done));
293
+ });
294
+ }
295
+
296
+ void renderPdf(std::string, PageSetup, std::string, std::function<void(int, const std::string &)> done) override {
297
+ done(0, "printing HTML to a PDF is not available on Linux");
298
+ }
299
+
300
+ private:
301
+ std::function<GtkWidget *(facebook::react::SurfaceId)> surfaceWidget_;
302
+ };
303
+
304
+ } // namespace
305
+
306
+ void printWidget(GtkWidget *widget, bool landscape, PrintDone done) {
307
+ auto draw = widgetDrawer(widget);
308
+ if (!draw) {
309
+ done("the window has nothing drawn yet");
310
+ return;
311
+ }
312
+ runOperation(std::move(*draw), landscape, windowOf(widget), "", std::move(done));
313
+ }
314
+
315
+ bool exportWidget(GtkWidget *widget, bool landscape, const std::string &path) {
316
+ auto draw = widgetDrawer(widget);
317
+ std::optional<std::string> error;
318
+ if (draw) {
319
+ runOperation(std::move(*draw), landscape, nullptr, path, [&error](const std::string &e) { error = e; });
320
+ }
321
+ return error && error->empty();
322
+ }
323
+
324
+ bool exportImage(const std::string &bytes, bool landscape, const std::string &path) {
325
+ std::string problem;
326
+ auto draw = imageDrawer(bytes, problem);
327
+ std::optional<std::string> error;
328
+ if (draw) {
329
+ runOperation(std::move(*draw), landscape, nullptr, path, [&error](const std::string &e) { error = e; });
330
+ }
331
+ return error && error->empty();
332
+ }
333
+
334
+ std::shared_ptr<PrintSystem> makePrintSystem(std::function<GtkWidget *(facebook::react::SurfaceId)> surfaceWidget) {
335
+ return std::make_shared<GtkPrintSystem>(std::move(surfaceWidget));
336
+ }
337
+
338
+ } // namespace ngn::gtk
@@ -0,0 +1,34 @@
1
+ // Printing on GTK (ngn/printing.h). A window's content and an image go through GtkPrintOperation,
2
+ // whose dialog is the Print portal's inside a sandbox, drawn onto one page scaled to fit it. A PDF
3
+ // goes to the Print portal (PreparePrint, then Print with the file), which takes the file as it is;
4
+ // where no portal answers, to GTK's print dialog and a printer that takes PDF. HTML is absent: the
5
+ // host has no web engine. File > Print with no print item of the app's prints the window
6
+ // (MenuHost::printWindow).
7
+ #pragma once
8
+
9
+ #include "ngn/printing.h"
10
+
11
+ #include <gtk/gtk.h>
12
+
13
+ #include <functional>
14
+ #include <memory>
15
+ #include <string>
16
+
17
+ namespace ngn::gtk {
18
+
19
+ using PrintDone = std::function<void(const std::string &error)>;
20
+
21
+ // The print dialog for what `widget` (a surface) draws, on its window. Answers on the main
22
+ // context, empty once the dialog has closed, printed or cancelled.
23
+ void printWidget(GtkWidget *widget, bool landscape, PrintDone done);
24
+
25
+ // `surfaceWidget` names the widget a surface draws in, null for none.
26
+ std::shared_ptr<PrintSystem> makePrintSystem(std::function<GtkWidget *(facebook::react::SurfaceId)> surfaceWidget);
27
+
28
+ // What printWidget and an image print draw, written to a PDF at `path` with no dialog: one page of
29
+ // US Letter (A4 in a locale that uses it), the content scaled to fit inside its margins. For the
30
+ // tests, which cannot press Print.
31
+ bool exportWidget(GtkWidget *widget, bool landscape, const std::string &path);
32
+ bool exportImage(const std::string &bytes, bool landscape, const std::string &path);
33
+
34
+ } // namespace ngn::gtk
@@ -0,0 +1,74 @@
1
+ #include "proxy-resolver.h"
2
+
3
+ #include <atomic>
4
+
5
+ namespace ngn::gtk {
6
+
7
+ namespace {
8
+
9
+ // How long a lookup waits for the portal, which may itself evaluate a PAC script.
10
+ constexpr int kPortalTimeoutMs = 5000;
11
+
12
+ // Set once the session has shown it has no proxy portal: not asked again.
13
+ std::atomic<bool> gNoPortal{false};
14
+
15
+ std::vector<std::string> strings(const gchar *const *list) {
16
+ std::vector<std::string> out;
17
+ for (auto *item = list; item && *item; ++item) {
18
+ out.emplace_back(*item);
19
+ }
20
+ return out;
21
+ }
22
+
23
+ // No portal on the bus, or none with this interface: not a failure of this one lookup.
24
+ bool portalMissing(const GError *error) {
25
+ return g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN) ||
26
+ g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER) ||
27
+ g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD) ||
28
+ g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_INTERFACE) ||
29
+ g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_OBJECT);
30
+ }
31
+
32
+ } // namespace
33
+
34
+ std::vector<std::string> desktopProxies(const std::string &url, GDBusConnection *bus, GProxyResolver *fallback) {
35
+ if (bus) {
36
+ GError *error = nullptr;
37
+ // A message bus routes to the portal by name; a connection straight to one has no names.
38
+ const char *name = g_dbus_connection_get_unique_name(bus) ? "org.freedesktop.portal.Desktop" : nullptr;
39
+ GVariant *reply = g_dbus_connection_call_sync(
40
+ bus, name, "/org/freedesktop/portal/desktop", "org.freedesktop.portal.ProxyResolver", "Lookup",
41
+ g_variant_new("(s)", url.c_str()), G_VARIANT_TYPE("(as)"), G_DBUS_CALL_FLAGS_NONE, kPortalTimeoutMs, nullptr,
42
+ &error);
43
+ if (reply) {
44
+ const gchar **list = nullptr;
45
+ g_variant_get(reply, "(^a&s)", &list);
46
+ auto out = strings(list);
47
+ g_free(list);
48
+ g_variant_unref(reply);
49
+ return out;
50
+ }
51
+ if (portalMissing(error)) {
52
+ gNoPortal = true;
53
+ }
54
+ g_error_free(error);
55
+ }
56
+ if (!fallback) {
57
+ return {};
58
+ }
59
+ gchar **list = g_proxy_resolver_lookup(fallback, url.c_str(), nullptr, nullptr);
60
+ auto out = strings(list);
61
+ g_strfreev(list);
62
+ return out;
63
+ }
64
+
65
+ std::vector<std::string> desktopProxies(const std::string &url) {
66
+ GDBusConnection *bus = gNoPortal ? nullptr : g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr);
67
+ auto out = desktopProxies(url, bus, g_proxy_resolver_get_default());
68
+ if (bus) {
69
+ g_object_unref(bus);
70
+ }
71
+ return out;
72
+ }
73
+
74
+ } // namespace ngn::gtk
@@ -0,0 +1,20 @@
1
+ // The desktop's proxy settings for the libcurl client (curl-network.h's desktopProxies): the proxy
2
+ // portal's answer (org.freedesktop.portal.ProxyResolver, which a Flatpak app must use) when a portal
3
+ // answers, else GLib's own GProxyResolver's, which reads GNOME's or KDE's settings through
4
+ // glib-networking and answers `direct://` without it. Portal first, as for every portal. Blocking:
5
+ // for the network thread, any thread.
6
+ #pragma once
7
+
8
+ #include <gio/gio.h>
9
+
10
+ #include <string>
11
+ #include <vector>
12
+
13
+ namespace ngn::gtk {
14
+
15
+ std::vector<std::string> desktopProxies(const std::string &url);
16
+
17
+ // The same over `bus` (null: no portal to ask) and `fallback`.
18
+ std::vector<std::string> desktopProxies(const std::string &url, GDBusConnection *bus, GProxyResolver *fallback);
19
+
20
+ } // namespace ngn::gtk