@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,339 @@
1
+ #include "drag-and-drop.h"
2
+
3
+ #include <memory>
4
+ #include <optional>
5
+ #include <string>
6
+ #include <vector>
7
+
8
+ namespace ngn::gtk {
9
+
10
+ namespace {
11
+
12
+ // A drag over the surface: where it is, and what it carries once both reads have come. A drop
13
+ // made before then is answered as they do.
14
+ struct DropState {
15
+ ContainerId container;
16
+ InputSink *input;
17
+ double x{0}, y{0};
18
+ bool over{false};
19
+ // The drag being read, and what it carries once read: one at a time.
20
+ GdkDrop *drop{nullptr};
21
+ std::optional<DragItems> items;
22
+ // The drag whose every read failed: it takes nothing, as a GtkDropTarget's failed load did.
23
+ GdkDrop *failed{nullptr};
24
+ // Drops made before their items came, with where each was made.
25
+ std::vector<std::pair<GdkDrop *, PointerInput>> waiting;
26
+ // Reads still running, which hold this state.
27
+ int reading{0};
28
+ bool detached{false};
29
+ };
30
+
31
+ PointerInput dropInput(const DropState &s) {
32
+ PointerInput input;
33
+ input.container = s.container;
34
+ input.point = {static_cast<float>(s.x), static_cast<float>(s.y)};
35
+ input.screenPoint = input.point;
36
+ input.buttons = 1;
37
+ return input;
38
+ }
39
+
40
+ // What a drop offers, with what GDK makes of it: a uri list as a GdkFileList, text as a string.
41
+ std::pair<bool, bool> offers(GdkDrop *drop) {
42
+ GdkContentFormats *formats = gdk_content_formats_union_deserialize_gtypes(gdk_content_formats_ref(gdk_drop_get_formats(drop)));
43
+ std::pair<bool, bool> out{gdk_content_formats_contain_gtype(formats, GDK_TYPE_FILE_LIST), gdk_content_formats_contain_gtype(formats, G_TYPE_STRING)};
44
+ gdk_content_formats_unref(formats);
45
+ return out;
46
+ }
47
+
48
+ void forget(DropState *s) {
49
+ if (--s->reading == 0 && s->detached) {
50
+ delete s;
51
+ }
52
+ }
53
+
54
+ // Both reads came: the drop waiting for them is answered, and the drag still over the surface
55
+ // carries them.
56
+ void itemsCame(DropState *s, GdkDrop *drop, DragItems items) {
57
+ for (auto it = s->waiting.begin(); it != s->waiting.end(); ++it) {
58
+ if (it->first == drop) {
59
+ bool taken = s->input->drop(it->second, items);
60
+ gdk_drop_finish(drop, taken ? GDK_ACTION_COPY : GdkDragAction(0));
61
+ g_object_unref(drop);
62
+ s->waiting.erase(it);
63
+ return;
64
+ }
65
+ }
66
+ if (drop == s->drop) {
67
+ s->items = items;
68
+ if (s->over) {
69
+ s->input->dragOver(dropInput(*s), items);
70
+ }
71
+ }
72
+ }
73
+
74
+ // Every read of the drag failed: a drop made on it is refused, and the drag is not dropped on.
75
+ void readsFailed(DropState *s, GdkDrop *drop) {
76
+ for (auto it = s->waiting.begin(); it != s->waiting.end(); ++it) {
77
+ if (it->first == drop) {
78
+ gdk_drop_finish(drop, GdkDragAction(0));
79
+ g_object_unref(drop);
80
+ s->waiting.erase(it);
81
+ return;
82
+ }
83
+ }
84
+ if (drop == s->drop) {
85
+ s->failed = drop;
86
+ }
87
+ }
88
+
89
+ struct Read {
90
+ DropState *state;
91
+ GdkDrop *drop;
92
+ bool text;
93
+ std::vector<std::string> urls;
94
+ // A read came back with a value, even an empty string.
95
+ bool read{false};
96
+ };
97
+
98
+ void finishRead(std::unique_ptr<Read> read, std::optional<std::string> text) {
99
+ if (!read->state->detached) {
100
+ if (read->read) {
101
+ itemsCame(read->state, read->drop, dragItems(read->urls, std::move(text)));
102
+ } else {
103
+ readsFailed(read->state, read->drop);
104
+ }
105
+ }
106
+ forget(read->state);
107
+ g_object_unref(read->drop);
108
+ }
109
+
110
+ void textRead(GObject *source, GAsyncResult *result, gpointer data) {
111
+ std::unique_ptr<Read> read(static_cast<Read *>(data));
112
+ const GValue *value = gdk_drop_read_value_finish(GDK_DROP(source), result, nullptr);
113
+ std::optional<std::string> text;
114
+ if (value && G_VALUE_HOLDS_STRING(value) && g_value_get_string(value)) {
115
+ text = g_value_get_string(value);
116
+ read->read = true;
117
+ }
118
+ finishRead(std::move(read), std::move(text));
119
+ }
120
+
121
+ void readText(std::unique_ptr<Read> read) {
122
+ if (!read->text) {
123
+ finishRead(std::move(read), std::nullopt);
124
+ return;
125
+ }
126
+ GdkDrop *drop = read->drop;
127
+ gdk_drop_read_value_async(drop, G_TYPE_STRING, G_PRIORITY_DEFAULT, nullptr, textRead, read.release());
128
+ }
129
+
130
+ void filesRead(GObject *source, GAsyncResult *result, gpointer data) {
131
+ std::unique_ptr<Read> read(static_cast<Read *>(data));
132
+ const GValue *value = gdk_drop_read_value_finish(GDK_DROP(source), result, nullptr);
133
+ if (value) {
134
+ read->read = true;
135
+ auto items = dropItems(value);
136
+ for (const auto &file : items.files) {
137
+ read->urls.push_back(file.uri);
138
+ }
139
+ read->urls.insert(read->urls.end(), items.urls.begin(), items.urls.end());
140
+ }
141
+ // The target went while the files were read: its drop is finished, and a read on it would never
142
+ // answer.
143
+ if (read->state->detached) {
144
+ finishRead(std::move(read), std::nullopt);
145
+ return;
146
+ }
147
+ readText(std::move(read));
148
+ }
149
+
150
+ // Reads the drop's file list, then its text, as macOS hands a view both.
151
+ void readDrop(DropState *s, GdkDrop *drop) {
152
+ auto [files, text] = offers(drop);
153
+ ++s->reading;
154
+ auto read = std::make_unique<Read>(Read{.state = s, .drop = GDK_DROP(g_object_ref(drop)), .text = text});
155
+ if (!files) {
156
+ readText(std::move(read));
157
+ return;
158
+ }
159
+ gdk_drop_read_value_async(drop, GDK_TYPE_FILE_LIST, G_PRIORITY_DEFAULT, nullptr, filesRead, read.release());
160
+ }
161
+
162
+ gboolean accepts(GtkDropTargetAsync *, GdkDrop *drop, gpointer) {
163
+ auto [files, text] = offers(drop);
164
+ return files || text;
165
+ }
166
+
167
+ GdkDragAction over(DropState *s, GdkDrop *drop, double x, double y) {
168
+ s->x = x;
169
+ s->y = y;
170
+ s->over = true;
171
+ if (drop != s->drop) {
172
+ s->drop = drop;
173
+ s->items.reset();
174
+ s->failed = nullptr;
175
+ readDrop(s, drop);
176
+ }
177
+ if (drop == s->failed) {
178
+ return GdkDragAction(0);
179
+ }
180
+ // Pending until the drag's data has come: the view under it is hit tested, and hears the enter
181
+ // with what is dragged once itemsCame hands it on.
182
+ DragItems items = s->items.value_or(DragItems{});
183
+ items.pending = !s->items;
184
+ return s->input->dragOver(dropInput(*s), items) ? GDK_ACTION_COPY : GdkDragAction(0);
185
+ }
186
+
187
+ GdkDragAction dragEntered(GtkDropTargetAsync *, GdkDrop *drop, double x, double y, gpointer user) {
188
+ return over(static_cast<DropState *>(user), drop, x, y);
189
+ }
190
+
191
+ void dragLeft(GtkDropTargetAsync *, GdkDrop *, gpointer user) {
192
+ auto *s = static_cast<DropState *>(user);
193
+ s->over = false;
194
+ s->drop = nullptr;
195
+ s->items.reset();
196
+ s->failed = nullptr;
197
+ s->input->dragExit(s->container);
198
+ }
199
+
200
+ gboolean dropped(GtkDropTargetAsync *, GdkDrop *drop, double x, double y, gpointer user) {
201
+ auto *s = static_cast<DropState *>(user);
202
+ s->x = x;
203
+ s->y = y;
204
+ s->over = false;
205
+ auto input = dropInput(*s);
206
+ if (drop == s->failed) {
207
+ gdk_drop_finish(drop, GdkDragAction(0));
208
+ s->failed = nullptr;
209
+ return TRUE;
210
+ }
211
+ if (drop == s->drop && s->items) {
212
+ bool taken = s->input->drop(input, *s->items);
213
+ gdk_drop_finish(drop, taken ? GDK_ACTION_COPY : GdkDragAction(0));
214
+ return TRUE;
215
+ }
216
+ // Its items have not come yet: answered as they do.
217
+ if (drop != s->drop) {
218
+ s->drop = drop;
219
+ readDrop(s, drop);
220
+ }
221
+ s->waiting.emplace_back(GDK_DROP(g_object_ref(drop)), input);
222
+ return TRUE;
223
+ }
224
+
225
+ using Uses = std::vector<std::shared_ptr<FileAccess::Use>>;
226
+
227
+ // The drag is over, dropped or cancelled: its reference goes, and with it the file uses it held.
228
+ void dragEnded(GdkDrag *drag, gpointer) {
229
+ g_signal_handlers_disconnect_by_data(drag, nullptr);
230
+ g_object_unref(drag);
231
+ }
232
+
233
+ void dragCancelled(GdkDrag *drag, GdkDragCancelReason, gpointer) {
234
+ dragEnded(drag, nullptr);
235
+ }
236
+
237
+ } // namespace
238
+
239
+ DragItems dropItems(const GValue *value) {
240
+ std::vector<std::string> urls;
241
+ std::optional<std::string> text;
242
+ if (value && G_VALUE_HOLDS(value, GDK_TYPE_FILE_LIST)) {
243
+ if (auto *list = static_cast<GdkFileList *>(g_value_get_boxed(value))) {
244
+ GSList *files = gdk_file_list_get_files(list);
245
+ for (GSList *at = files; at; at = at->next) {
246
+ gchar *uri = g_file_get_uri(G_FILE(at->data));
247
+ urls.emplace_back(uri);
248
+ g_free(uri);
249
+ }
250
+ g_slist_free(files);
251
+ }
252
+ } else if (value && G_VALUE_HOLDS_STRING(value) && g_value_get_string(value)) {
253
+ text = g_value_get_string(value);
254
+ }
255
+ return dragItems(urls, std::move(text));
256
+ }
257
+
258
+ void attachDropTarget(GtkWidget *widget, ContainerId container, InputSink *input) {
259
+ auto *state = new DropState{.container = container, .input = input};
260
+ // Async, so that a drag's file list and its text are both read: a GtkDropTarget takes one.
261
+ GtkDropTargetAsync *target = gtk_drop_target_async_new(nullptr, GDK_ACTION_COPY);
262
+ g_signal_connect(target, "accept", G_CALLBACK(accepts), state);
263
+ g_signal_connect(target, "drag-enter", G_CALLBACK(dragEntered), state);
264
+ g_signal_connect(target, "drag-motion", G_CALLBACK(dragEntered), state);
265
+ g_signal_connect(target, "drag-leave", G_CALLBACK(dragLeft), state);
266
+ g_signal_connect(target, "drop", G_CALLBACK(dropped), state);
267
+ // Reads still running hold the state until they finish.
268
+ g_object_set_data_full(G_OBJECT(target), "ngn-drop", state, [](gpointer p) {
269
+ auto *s = static_cast<DropState *>(p);
270
+ for (auto &[drop, input] : s->waiting) {
271
+ gdk_drop_finish(drop, GdkDragAction(0));
272
+ g_object_unref(drop);
273
+ }
274
+ s->waiting.clear();
275
+ s->detached = true;
276
+ if (s->reading == 0) {
277
+ delete s;
278
+ }
279
+ });
280
+ gtk_widget_add_controller(widget, GTK_EVENT_CONTROLLER(target));
281
+ }
282
+
283
+ GdkContentProvider *dragContent(const DragOutItem &item) {
284
+ std::vector<GdkContentProvider *> parts;
285
+ std::string uriList;
286
+ if (!item.files.empty()) {
287
+ GSList *files = nullptr;
288
+ for (const auto &uri : item.files) {
289
+ files = g_slist_append(files, g_file_new_for_uri(uri.c_str()));
290
+ }
291
+ GdkFileList *list = gdk_file_list_new_from_list(files);
292
+ g_slist_free_full(files, g_object_unref);
293
+ // GDK serializes it as text/uri-list, and for a sandboxed taker through the file transfer portal.
294
+ parts.push_back(gdk_content_provider_new_typed(GDK_TYPE_FILE_LIST, list));
295
+ g_boxed_free(GDK_TYPE_FILE_LIST, list);
296
+ } else if (item.url) {
297
+ uriList = *item.url + "\r\n";
298
+ GBytes *bytes = g_bytes_new(uriList.data(), uriList.size());
299
+ parts.push_back(gdk_content_provider_new_for_bytes("text/uri-list", bytes));
300
+ g_bytes_unref(bytes);
301
+ }
302
+ if (auto text = item.text ? item.text : item.url) {
303
+ parts.push_back(gdk_content_provider_new_typed(G_TYPE_STRING, text->c_str()));
304
+ }
305
+ return gdk_content_provider_new_union(parts.data(), parts.size());
306
+ }
307
+
308
+ GdkDrag *beginDragOut(GtkWidget *view, GdkEvent *event, const DragOutItem &item) {
309
+ GtkNative *native = view ? gtk_widget_get_native(view) : nullptr;
310
+ GdkDevice *device = event ? gdk_event_get_device(event) : nullptr;
311
+ if (!native || !device) {
312
+ return nullptr;
313
+ }
314
+ GdkSurface *surface = gtk_native_get_surface(native);
315
+ GdkContentProvider *content = dragContent(item);
316
+ GdkDrag *drag = gdk_drag_begin(surface, device, content, GDK_ACTION_COPY, 0, 0);
317
+ g_object_unref(content);
318
+ if (!drag) {
319
+ return nullptr;
320
+ }
321
+ // The view's look under the pointer, held where the press took it.
322
+ double x = 0, y = 0, tx = 0, ty = 0;
323
+ gdk_event_get_position(event, &x, &y);
324
+ gtk_native_get_surface_transform(native, &tx, &ty);
325
+ graphene_point_t in, at;
326
+ graphene_point_init(&in, static_cast<float>(x - tx), static_cast<float>(y - ty));
327
+ if (!gtk_widget_compute_point(GTK_WIDGET(native), view, &in, &at)) {
328
+ graphene_point_init(&at, 0, 0);
329
+ }
330
+ GdkPaintable *look = gtk_widget_paintable_new(view);
331
+ gtk_drag_icon_set_from_paintable(drag, look, static_cast<int>(at.x), static_cast<int>(at.y));
332
+ g_object_unref(look);
333
+ g_object_set_data_full(G_OBJECT(drag), "ngn-uses", new Uses(item.uses), [](gpointer p) { delete static_cast<Uses *>(p); });
334
+ g_signal_connect(drag, "dnd-finished", G_CALLBACK(dragEnded), nullptr);
335
+ g_signal_connect(drag, "cancel", G_CALLBACK(dragCancelled), nullptr);
336
+ return drag;
337
+ }
338
+
339
+ } // namespace ngn::gtk
@@ -0,0 +1,30 @@
1
+ // Drag and drop on GTK, as the core's input dispatcher decides it (input-dispatcher.h): a
2
+ // GtkDropTargetAsync on each surface reads a drag's file list and its text, both, and hands its
3
+ // files, urls and text to the dispatcher, which picks the `fileDrop` view under it; and a press the dispatcher turns into a drag out of a
4
+ // `[dragItem]` view starts a GDK drag with that item. Inside a Flatpak, GTK reads a dropped file
5
+ // through the document portal, so the path the app gets is one it can open.
6
+ #pragma once
7
+
8
+ #include "ngn/input-dispatcher.h"
9
+ #include "ngn/input.h"
10
+
11
+ #include <gtk/gtk.h>
12
+
13
+ namespace ngn::gtk {
14
+
15
+ // A drop's value (a GdkFileList, whose files may be any uri, or a string) as the dispatcher's items.
16
+ DragItems dropItems(const GValue *value);
17
+
18
+ // Hands drags over `widget` to `input` as the container `container`: over, out and dropped.
19
+ void attachDropTarget(GtkWidget *widget, ContainerId container, InputSink *input);
20
+
21
+ // What a drag out offers: its files as a GdkFileList and a uri list, and its text (else its url)
22
+ // as plain text. The caller owns it.
23
+ GdkContentProvider *dragContent(const DragOutItem &item);
24
+
25
+ // Starts dragging `item` out of `view`, from the pointer event that crossed the drag threshold,
26
+ // with the view's look as the drag icon; the item's file uses are held until the drag ends. Null
27
+ // when GDK would not start one.
28
+ GdkDrag *beginDragOut(GtkWidget *view, GdkEvent *event, const DragOutItem &item);
29
+
30
+ } // namespace ngn::gtk
@@ -0,0 +1,239 @@
1
+ #include "file-dialogs.h"
2
+
3
+ #include "app-windows.h"
4
+ #include "portal.h"
5
+
6
+ #include <gtk/gtk.h>
7
+
8
+ #include <cstdio>
9
+
10
+ namespace ngn::gtk {
11
+
12
+ namespace {
13
+
14
+ constexpr const char *kFileChooser = "org.freedesktop.portal.FileChooser";
15
+
16
+ using Paths = std::function<void(std::optional<std::vector<std::string>>)>;
17
+ using Path = std::function<void(std::optional<std::string>)>;
18
+
19
+ std::string joined(const std::vector<std::string> &types) {
20
+ std::string out;
21
+ for (const auto &type : types) {
22
+ out += (out.empty() ? "" : ", ") + type;
23
+ }
24
+ return out;
25
+ }
26
+
27
+ // One filter of every type, as the portal takes it: (name, [(1, mime type)]), named `name` or the
28
+ // types listed.
29
+ GVariant *portalFilter(const std::vector<std::string> &types, const std::string &name) {
30
+ GVariantBuilder patterns;
31
+ g_variant_builder_init(&patterns, G_VARIANT_TYPE("a(us)"));
32
+ for (const auto &type : types) {
33
+ g_variant_builder_add(&patterns, "(us)", 1u, type.c_str());
34
+ }
35
+ return g_variant_new("(s@a(us))", (name.empty() ? joined(types) : name).c_str(), g_variant_builder_end(&patterns));
36
+ }
37
+
38
+ void addFilter(GVariantBuilder *options, const std::vector<std::string> &types, const std::string &name = "") {
39
+ if (types.empty()) {
40
+ return;
41
+ }
42
+ GVariant *filter = g_variant_ref_sink(portalFilter(types, name));
43
+ GVariantBuilder filters;
44
+ g_variant_builder_init(&filters, G_VARIANT_TYPE("a(sa(us))"));
45
+ g_variant_builder_add_value(&filters, filter);
46
+ g_variant_builder_add(options, "{sv}", "filters", g_variant_builder_end(&filters));
47
+ g_variant_builder_add(options, "{sv}", "current_filter", filter);
48
+ g_variant_unref(filter);
49
+ }
50
+
51
+ std::vector<std::string> urisOf(GVariant *results) {
52
+ std::vector<std::string> out;
53
+ GVariant *uris = results ? g_variant_lookup_value(results, "uris", G_VARIANT_TYPE_STRING_ARRAY) : nullptr;
54
+ if (!uris) {
55
+ return out;
56
+ }
57
+ gsize count = 0;
58
+ const gchar **each = g_variant_get_strv(uris, &count);
59
+ for (gsize i = 0; i < count; ++i) {
60
+ out.emplace_back(each[i]);
61
+ }
62
+ g_free(each);
63
+ g_variant_unref(uris);
64
+ return out;
65
+ }
66
+
67
+ // Asks the FileChooser portal, against the front window: `fallBack` runs when no portal answers.
68
+ void askPortal(const char *method, std::string title, GVariantBuilder *options, Paths done, std::function<void(Paths)> fallBack) {
69
+ portal::parentHandle(portal::frontWindow(), [method, title = std::move(title), options, done = std::move(done), fallBack = std::move(fallBack)](
70
+ const std::string &parent, std::function<void()> drop) mutable {
71
+ portal::request(kFileChooser, method, g_variant_new("(ss)", parent.c_str(), title.c_str()), options, nullptr,
72
+ [done = std::move(done), fallBack = std::move(fallBack), drop = std::move(drop)](portal::Outcome outcome, GVariant *results) mutable {
73
+ drop();
74
+ if (outcome == portal::Outcome::Absent) {
75
+ fallBack(std::move(done));
76
+ } else if (outcome == portal::Outcome::Done) {
77
+ done(pathsOfUris(urisOf(results)));
78
+ } else {
79
+ done(std::nullopt);
80
+ }
81
+ });
82
+ });
83
+ }
84
+
85
+ // GTK's own dialog, where no portal answers.
86
+ GtkFileDialog *gtkDialog(const char *title, const std::vector<std::string> &types, const std::string &name = "") {
87
+ GtkFileDialog *dialog = gtk_file_dialog_new();
88
+ gtk_file_dialog_set_title(dialog, title);
89
+ gtk_file_dialog_set_modal(dialog, TRUE);
90
+ if (!types.empty()) {
91
+ GtkFileFilter *filter = gtk_file_filter_new();
92
+ gtk_file_filter_set_name(filter, (name.empty() ? joined(types) : name).c_str());
93
+ for (const auto &type : types) {
94
+ gtk_file_filter_add_mime_type(filter, type.c_str());
95
+ }
96
+ GListStore *filters = g_list_store_new(GTK_TYPE_FILE_FILTER);
97
+ g_list_store_append(filters, filter);
98
+ gtk_file_dialog_set_filters(dialog, G_LIST_MODEL(filters));
99
+ gtk_file_dialog_set_default_filter(dialog, filter);
100
+ g_object_unref(filters);
101
+ g_object_unref(filter);
102
+ }
103
+ return dialog;
104
+ }
105
+
106
+ std::optional<std::string> pathOf(GFile *file) {
107
+ if (!file) {
108
+ return std::nullopt;
109
+ }
110
+ gchar *path = g_file_get_path(file);
111
+ g_object_unref(file);
112
+ if (!path) {
113
+ return std::nullopt;
114
+ }
115
+ std::string out = path;
116
+ g_free(path);
117
+ return out;
118
+ }
119
+
120
+ // A dialog's finish, given its result: the files, or nothing when the user dismissed it.
121
+ using Finish = std::function<std::optional<std::vector<std::string>>(GtkFileDialog *, GAsyncResult *, GError **)>;
122
+
123
+ void finished(GObject *source, GAsyncResult *result, gpointer data) {
124
+ auto *pair = static_cast<std::pair<Finish, Paths> *>(data);
125
+ GError *error = nullptr;
126
+ auto paths = pair->first(GTK_FILE_DIALOG(source), result, &error);
127
+ if (error) {
128
+ if (!g_error_matches(error, GTK_DIALOG_ERROR, GTK_DIALOG_ERROR_DISMISSED)) {
129
+ fprintf(stderr, "[ngn] file dialog: %s\n", error->message);
130
+ }
131
+ g_error_free(error);
132
+ }
133
+ pair->second(paths);
134
+ g_object_unref(source);
135
+ delete pair;
136
+ }
137
+
138
+ gpointer later(Finish finish, Paths done) {
139
+ return new std::pair<Finish, Paths>(std::move(finish), std::move(done));
140
+ }
141
+
142
+ class PortalFileDialogs final : public FileDialogs {
143
+ public:
144
+ void openFiles(const OpenRequest &request, Paths done) override {
145
+ onMainContext([request, done = std::move(done)]() mutable {
146
+ GVariantBuilder *options = g_variant_builder_new(G_VARIANT_TYPE_VARDICT);
147
+ g_variant_builder_add(options, "{sv}", "modal", g_variant_new_boolean(TRUE));
148
+ g_variant_builder_add(options, "{sv}", "multiple", g_variant_new_boolean(request.multiple));
149
+ addFilter(options, request.types, request.filterName);
150
+ std::string title = !request.title.empty() ? request.title : request.multiple ? "Open Files" : "Open File";
151
+ askPortal("OpenFile", title, options, std::move(done), [request, title](Paths done) {
152
+ GtkFileDialog *dialog = gtkDialog(title.c_str(), request.types, request.filterName);
153
+ if (request.multiple) {
154
+ gtk_file_dialog_open_multiple(dialog, portal::frontWindow(), nullptr, finished, later([](GtkFileDialog *d, GAsyncResult *r, GError **e) {
155
+ std::optional<std::vector<std::string>> out;
156
+ if (GListModel *files = gtk_file_dialog_open_multiple_finish(d, r, e)) {
157
+ out.emplace();
158
+ for (guint i = 0, n = g_list_model_get_n_items(files); i < n; ++i) {
159
+ if (auto path = pathOf(G_FILE(g_list_model_get_item(files, i)))) {
160
+ out->push_back(*path);
161
+ }
162
+ }
163
+ g_object_unref(files);
164
+ }
165
+ return out;
166
+ }, std::move(done)));
167
+ } else {
168
+ gtk_file_dialog_open(dialog, portal::frontWindow(), nullptr, finished, later([](GtkFileDialog *d, GAsyncResult *r, GError **e) {
169
+ auto path = pathOf(gtk_file_dialog_open_finish(d, r, e));
170
+ return path ? std::optional<std::vector<std::string>>({*path}) : std::nullopt;
171
+ }, std::move(done)));
172
+ }
173
+ });
174
+ });
175
+ }
176
+
177
+ void saveFile(const SaveRequest &request, Path done) override {
178
+ onMainContext([request, done = std::move(done)]() mutable {
179
+ GVariantBuilder *options = g_variant_builder_new(G_VARIANT_TYPE_VARDICT);
180
+ g_variant_builder_add(options, "{sv}", "modal", g_variant_new_boolean(TRUE));
181
+ if (!request.suggestedName.empty()) {
182
+ g_variant_builder_add(options, "{sv}", "current_name", g_variant_new_string(request.suggestedName.c_str()));
183
+ }
184
+ addFilter(options, request.types);
185
+ askPortal("SaveFile", "Save File", options, first(std::move(done)), [request](Paths done) {
186
+ GtkFileDialog *dialog = gtkDialog("Save File", request.types);
187
+ if (!request.suggestedName.empty()) {
188
+ gtk_file_dialog_set_initial_name(dialog, request.suggestedName.c_str());
189
+ }
190
+ gtk_file_dialog_save(dialog, portal::frontWindow(), nullptr, finished, later([](GtkFileDialog *d, GAsyncResult *r, GError **e) {
191
+ auto path = pathOf(gtk_file_dialog_save_finish(d, r, e));
192
+ return path ? std::optional<std::vector<std::string>>({*path}) : std::nullopt;
193
+ }, std::move(done)));
194
+ });
195
+ });
196
+ }
197
+
198
+ void chooseFolder(Path done) override {
199
+ onMainContext([done = std::move(done)]() mutable {
200
+ GVariantBuilder *options = g_variant_builder_new(G_VARIANT_TYPE_VARDICT);
201
+ g_variant_builder_add(options, "{sv}", "modal", g_variant_new_boolean(TRUE));
202
+ g_variant_builder_add(options, "{sv}", "directory", g_variant_new_boolean(TRUE));
203
+ askPortal("OpenFile", "Choose Folder", options, first(std::move(done)), [](Paths done) {
204
+ GtkFileDialog *dialog = gtkDialog("Choose Folder", {});
205
+ gtk_file_dialog_select_folder(dialog, portal::frontWindow(), nullptr, finished, later([](GtkFileDialog *d, GAsyncResult *r, GError **e) {
206
+ auto path = pathOf(gtk_file_dialog_select_folder_finish(d, r, e));
207
+ return path ? std::optional<std::vector<std::string>>({*path}) : std::nullopt;
208
+ }, std::move(done)));
209
+ });
210
+ });
211
+ }
212
+
213
+ private:
214
+ // A one-path answer from a list of them.
215
+ static Paths first(Path done) {
216
+ return [done = std::move(done)](std::optional<std::vector<std::string>> paths) {
217
+ done(paths && !paths->empty() ? std::optional<std::string>(paths->front()) : std::nullopt);
218
+ };
219
+ }
220
+ };
221
+
222
+ } // namespace
223
+
224
+ std::optional<std::vector<std::string>> pathsOfUris(const std::vector<std::string> &uris) {
225
+ std::vector<std::string> out;
226
+ for (const auto &uri : uris) {
227
+ if (gchar *path = g_filename_from_uri(uri.c_str(), nullptr, nullptr)) {
228
+ out.emplace_back(path);
229
+ g_free(path);
230
+ }
231
+ }
232
+ return out.empty() ? std::nullopt : std::optional<std::vector<std::string>>(std::move(out));
233
+ }
234
+
235
+ std::shared_ptr<FileDialogs> makeFileDialogs() {
236
+ return std::make_shared<PortalFileDialogs>();
237
+ }
238
+
239
+ } // namespace ngn::gtk
@@ -0,0 +1,21 @@
1
+ // DocumentPicker's dialogs on GTK (FileDialogs in ngn/desktop-expo.h): the FileChooser portal's
2
+ // OpenFile and SaveFile, placed against the front window, and GtkFileDialog where no portal
3
+ // answers. A MIME type list becomes one filter of them all.
4
+ #pragma once
5
+
6
+ #include "ngn/desktop-expo.h"
7
+
8
+ #include <memory>
9
+ #include <optional>
10
+ #include <string>
11
+ #include <vector>
12
+
13
+ namespace ngn::gtk {
14
+
15
+ std::shared_ptr<FileDialogs> makeFileDialogs();
16
+
17
+ // The local paths of a FileChooser answer's `uris` (file:// only), or nothing when the user
18
+ // chose none.
19
+ std::optional<std::vector<std::string>> pathsOfUris(const std::vector<std::string> &uris);
20
+
21
+ } // namespace ngn::gtk