@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,439 @@
1
+ #include "app-windows.h"
2
+
3
+ #include "screens.h"
4
+ #include "view-widget.h"
5
+ #include "window-scale.h"
6
+
7
+ #include <adwaita.h>
8
+
9
+ #include <algorithm>
10
+ #include <cmath>
11
+ #include <cstdio>
12
+ #include <utility>
13
+
14
+ namespace ngn::gtk {
15
+
16
+ void onMainContext(std::function<void()> task) {
17
+ auto *heap = new std::function<void()>(std::move(task));
18
+ // Queued for the loop to dispatch, always: g_main_context_invoke runs the task on the calling
19
+ // thread when it can take the context, which another thread can between two of the loop's turns.
20
+ g_idle_add_full(
21
+ G_PRIORITY_DEFAULT,
22
+ [](gpointer data) -> gboolean {
23
+ (*static_cast<std::function<void()> *>(data))();
24
+ return G_SOURCE_REMOVE;
25
+ },
26
+ heap, [](gpointer data) { delete static_cast<std::function<void()> *>(data); });
27
+ }
28
+
29
+ void runAfter(double seconds, std::function<void()> task) {
30
+ auto *heap = new std::function<void()>(std::move(task));
31
+ g_timeout_add_full(
32
+ G_PRIORITY_DEFAULT, static_cast<guint>(std::max(0.0, seconds) * 1000),
33
+ [](gpointer data) -> gboolean {
34
+ (*static_cast<std::function<void()> *>(data))();
35
+ return G_SOURCE_REMOVE;
36
+ },
37
+ heap, [](gpointer data) { delete static_cast<std::function<void()> *>(data); });
38
+ }
39
+
40
+ GtkApplication *startApplication(const std::string &appId, bool unique, bool service) {
41
+ bool named = g_application_id_is_valid(appId.c_str());
42
+ int flags = G_APPLICATION_HANDLES_OPEN;
43
+ // A single instance needs a name to find the first by; a service is one by its name.
44
+ flags |= unique && named ? 0 : G_APPLICATION_NON_UNIQUE;
45
+ if (service && named) {
46
+ flags = G_APPLICATION_HANDLES_OPEN | G_APPLICATION_IS_SERVICE;
47
+ }
48
+ AdwApplication *app = adw_application_new(named ? appId.c_str() : nullptr, static_cast<GApplicationFlags>(flags));
49
+ GError *error = nullptr;
50
+ if (!g_application_register(G_APPLICATION(app), nullptr, &error)) {
51
+ fprintf(stderr, "[ngn] could not register the application: %s\n", error ? error->message : "unknown");
52
+ g_clear_error(&error);
53
+ }
54
+ return GTK_APPLICATION(app);
55
+ }
56
+
57
+ void forwardLaunch(GtkApplication *application, const std::vector<std::string> &openArgs) {
58
+ std::vector<GFile *> files;
59
+ for (const auto &arg : openArgs) {
60
+ files.push_back(g_file_new_for_commandline_arg(arg.c_str()));
61
+ }
62
+ if (files.empty()) {
63
+ g_application_activate(G_APPLICATION(application));
64
+ } else {
65
+ g_application_open(G_APPLICATION(application), files.data(), static_cast<gint>(files.size()), "");
66
+ }
67
+ for (GFile *file : files) {
68
+ g_object_unref(file);
69
+ }
70
+ // The calls go out before the process does.
71
+ if (GDBusConnection *bus = g_application_get_dbus_connection(G_APPLICATION(application))) {
72
+ g_dbus_connection_flush_sync(bus, nullptr, nullptr);
73
+ }
74
+ }
75
+
76
+ std::vector<std::string> openUrls(const std::vector<std::string> &openArgs) {
77
+ std::vector<std::string> urls;
78
+ for (const auto &arg : openArgs) {
79
+ GFile *file = g_file_new_for_commandline_arg(arg.c_str());
80
+ gchar *uri = g_file_get_uri(file);
81
+ // A link GIO has no backend for keeps its own text, which g_file_get_uri also gives back.
82
+ urls.emplace_back(uri ? uri : arg);
83
+ g_free(uri);
84
+ g_object_unref(file);
85
+ }
86
+ return urls;
87
+ }
88
+
89
+ gulong afterLayout(GtkWidget *window, std::function<void()> laidOut) {
90
+ GdkFrameClock *clock = gtk_widget_get_frame_clock(window);
91
+ if (!clock) {
92
+ return 0;
93
+ }
94
+ return g_signal_connect_data(
95
+ clock, "paint", G_CALLBACK(+[](GdkFrameClock *, gpointer data) { (*static_cast<std::function<void()> *>(data))(); }),
96
+ new std::function<void()>(std::move(laidOut)), [](gpointer data, GClosure *) { delete static_cast<std::function<void()> *>(data); },
97
+ GConnectFlags(0));
98
+ }
99
+
100
+ namespace {
101
+
102
+ struct CloseRequest {
103
+ GtkWindows *windows;
104
+ SurfaceId surface;
105
+ };
106
+
107
+ // A window's own report of a change to the GtkWindows it belongs to.
108
+ struct WindowSignal {
109
+ GtkWindows *windows;
110
+ SurfaceId surface;
111
+ void (*report)(GtkWindows *, SurfaceId, GtkWindow *);
112
+ };
113
+
114
+ gulong connectWindow(GtkWidget *window, const char *signal, WindowSignal data) {
115
+ return g_signal_connect_data(
116
+ window, signal, G_CALLBACK(+[](GtkWindow *w, GParamSpec *, gpointer p) {
117
+ auto *s = static_cast<WindowSignal *>(p);
118
+ s->report(s->windows, s->surface, w);
119
+ }),
120
+ new WindowSignal(data), [](gpointer p, GClosure *) { delete static_cast<WindowSignal *>(p); }, GConnectFlags(0));
121
+ }
122
+
123
+ } // namespace
124
+
125
+ bool GtkWindows::fillsScreen(GtkWindow *window) {
126
+ return gtk_window_is_maximized(window) || gtk_window_is_fullscreen(window);
127
+ }
128
+
129
+ GtkWindows::GtkWindows(Hooks hooks, GtkApplication *application, MenuPlacement menus)
130
+ : hooks_(std::move(hooks)), application_(application), menus_(menus) {}
131
+
132
+ const char *GtkWindows::titleFor(const std::string &title) const {
133
+ return (title.empty() ? appName : title).c_str();
134
+ }
135
+
136
+ GtkWindows::~GtkWindows() {
137
+ // Handlers first, which call back into these, then the windows still open.
138
+ auto open = std::exchange(windows_, {});
139
+ for (auto &[surface, entry] : open) {
140
+ disconnect(entry);
141
+ gtk_window_destroy(GTK_WINDOW(entry.window));
142
+ }
143
+ }
144
+
145
+ void GtkWindows::disconnect(Window &entry) {
146
+ stopScaleFollow(entry.scaleFollow);
147
+ g_clear_signal_handler(&entry.closeRequested, entry.window);
148
+ for (gulong &id : entry.notified) {
149
+ g_clear_signal_handler(&id, entry.window);
150
+ }
151
+ entry.notified.clear();
152
+ if (entry.clock) {
153
+ g_clear_signal_handler(&entry.laidOut, entry.clock);
154
+ g_clear_object(&entry.clock);
155
+ }
156
+ }
157
+
158
+ void GtkWindows::setHost(std::weak_ptr<WindowHost> host) {
159
+ host_ = std::move(host);
160
+ }
161
+
162
+ GtkWidget *GtkWindows::window(SurfaceId surface) const {
163
+ auto it = windows_.find(surface);
164
+ return it == windows_.end() ? nullptr : it->second.window;
165
+ }
166
+
167
+ GtkWidget *GtkWindows::surfaceWidget(SurfaceId surface) const {
168
+ auto it = windows_.find(surface);
169
+ return it == windows_.end() ? nullptr : it->second.surface;
170
+ }
171
+
172
+ void GtkWindows::openWindow(SurfaceId surface, const WindowRequest &request, const WindowPlacement &placement, std::function<void()> opened) {
173
+ onMainContext([this, surface, request, placement, opened = std::move(opened)] { open(surface, request, placement, opened); });
174
+ }
175
+
176
+ void GtkWindows::open(SurfaceId surface, const WindowRequest &request, const WindowPlacement &placement, std::function<void()> opened) {
177
+ int width = static_cast<int>(std::lround(placement.frame.width > 0 ? placement.frame.width : request.width.value_or(kDefaultWindowWidth)));
178
+ int height = static_cast<int>(std::lround(placement.frame.height > 0 ? placement.frame.height : request.height.value_or(kDefaultWindowHeight)));
179
+ GtkWidget *window = adw_application_window_new(application_);
180
+ gtk_window_set_title(GTK_WINDOW(window), titleFor(request.title));
181
+ gtk_window_set_default_size(GTK_WINDOW(window), width, height);
182
+ gtk_window_set_resizable(GTK_WINDOW(window), request.resizable);
183
+ // Back as it was saved: at its size, and maximised or full screen. GTK places it.
184
+ if (placement.frame.maximized && request.resizable) {
185
+ gtk_window_maximize(GTK_WINDOW(window));
186
+ }
187
+ if (placement.frame.fullScreen || request.fullScreen) {
188
+ gtk_window_fullscreen(GTK_WINDOW(window));
189
+ }
190
+ GtkWidget *widget = makeSurfaceWidget(surface, [this, surface, window](int w, int h) {
191
+ if (hooks_.resizeSurface) {
192
+ hooks_.resizeSurface(surface, w, h, windowScale(window));
193
+ }
194
+ // A window that fills the screen has the size GTK allocates, which arrives after the
195
+ // notify::maximized or notify::fullscreened that reported the old one.
196
+ reportFrame(surface, true);
197
+ });
198
+ if (request.minWidth || request.minHeight) {
199
+ gtk_widget_set_size_request(widget, static_cast<int>(request.minWidth.value_or(-1)), static_cast<int>(request.minHeight.value_or(-1)));
200
+ }
201
+ adw_application_window_set_content(ADW_APPLICATION_WINDOW(window), wrapInChrome(widget, {.titleBar = request.titleBar, .menus = menus_}));
202
+ windows_[surface] = {.window = window, .surface = widget};
203
+ Window &entry = windows_[surface];
204
+ auto *request_ = new CloseRequest{this, surface};
205
+ entry.closeRequested = g_signal_connect_data(
206
+ window, "close-request", G_CALLBACK(+[](GtkWindow *, gpointer data) -> gboolean {
207
+ auto *r = static_cast<CloseRequest *>(data);
208
+ auto host = r->windows->host_.lock();
209
+ // The app decides, when it listens; otherwise the window closes as the OS would close it.
210
+ if (!host || host->closeRequested(r->surface)) {
211
+ r->windows->close(r->surface);
212
+ // Linux's convention: the app quits when the user closes its last window, unless an app
213
+ // listens, whose quitWhenLastWindowCloses decides instead (WindowHost::lastWindowClosed).
214
+ // Only here, not in close: a reload closes the app's windows too, before it opens their
215
+ // replacements.
216
+ if (r->windows->windows_.empty() && (!host || host->lastWindowClosed()) && r->windows->hooks_.lastWindowClosed) {
217
+ r->windows->hooks_.lastWindowClosed();
218
+ }
219
+ }
220
+ return TRUE;
221
+ }),
222
+ request_, [](gpointer data, GClosure *) { delete static_cast<CloseRequest *>(data); }, GConnectFlags(0));
223
+ for (const char *signal : {"notify::default-width", "notify::default-height", "notify::maximized", "notify::fullscreened"}) {
224
+ entry.notified.push_back(
225
+ connectWindow(window, signal, {this, surface, [](GtkWindows *windows, SurfaceId s, GtkWindow *) { windows->reportFrame(s); }}));
226
+ }
227
+ entry.notified.push_back(connectWindow(window, "notify::is-active", {this, surface, [](GtkWindows *windows, SurfaceId s, GtkWindow *w) {
228
+ windows->reportFocus(s, gtk_window_is_active(w));
229
+ }}));
230
+ if (hooks_.addSurface) {
231
+ hooks_.addSurface(surface, widget);
232
+ }
233
+ // Realized, so the surface knows its scale, but not shown until its first frame mounts.
234
+ gtk_widget_realize(window);
235
+ // A move to a monitor of another scale, or a new fractional scale, changes no size, so the
236
+ // surface widget does not report it.
237
+ entry.scaleFollow = followWindowScale(window, [this, surface, window] {
238
+ GtkWidget *content = surfaceWidget(surface);
239
+ if (content && hooks_.resizeSurface) {
240
+ hooks_.resizeSurface(surface, gtk_widget_get_width(content), gtk_widget_get_height(content), windowScale(window));
241
+ }
242
+ if (hooks_.rescaled) {
243
+ hooks_.rescaled(window);
244
+ }
245
+ reportFrame(surface);
246
+ });
247
+ entry.laidOut = afterLayout(window, [this, surface] {
248
+ if (hooks_.laidOut) {
249
+ hooks_.laidOut(surface);
250
+ }
251
+ });
252
+ // Held with the handler, which goes before the window: the clock is its surface's until then.
253
+ if (entry.laidOut) {
254
+ entry.clock = GDK_FRAME_CLOCK(g_object_ref(gtk_widget_get_frame_clock(window)));
255
+ }
256
+ if (hooks_.startSurface) {
257
+ hooks_.startSurface(surface, width, height, windowScale(window));
258
+ }
259
+ if (opened) {
260
+ opened();
261
+ }
262
+ }
263
+
264
+ void presentWithoutInitialFocus(GtkWindow *window) {
265
+ // GTK focuses the first widget it can as it shows a window with no focus, and the widget hears
266
+ // of it at once, active window or not: so for that moment nothing in it can take the focus.
267
+ GtkWidget *content = gtk_window_get_child(window);
268
+ bool hold = !gtk_root_get_focus(GTK_ROOT(window)) && content && gtk_widget_get_can_focus(content);
269
+ if (hold) {
270
+ gtk_widget_set_can_focus(content, FALSE);
271
+ }
272
+ gtk_window_present(window);
273
+ if (hold) {
274
+ gtk_widget_set_can_focus(content, TRUE);
275
+ }
276
+ }
277
+
278
+ void GtkWindows::didMount(SurfaceId surface) {
279
+ auto it = windows_.find(surface);
280
+ if (it == windows_.end() || it->second.mounted) {
281
+ return;
282
+ }
283
+ it->second.mounted = true;
284
+ if (!hooks_.whenShown) {
285
+ show(surface);
286
+ return;
287
+ }
288
+ // Held past the splash screen, which may let it go after these windows are gone. The hook
289
+ // answers on the main context, in one dispatch, so a window shows in the order it was ready.
290
+ hooks_.whenShown([this, surface, alive = std::weak_ptr<void>(alive_)] {
291
+ if (!alive.expired()) {
292
+ show(surface);
293
+ }
294
+ });
295
+ }
296
+
297
+ void GtkWindows::show(SurfaceId surface) {
298
+ auto it = windows_.find(surface);
299
+ // A window closed while the splash held it is not shown.
300
+ if (it == windows_.end() || it->second.shown) {
301
+ return;
302
+ }
303
+ it->second.shown = true;
304
+ presentWithoutInitialFocus(GTK_WINDOW(it->second.window));
305
+ if (hooks_.presented) {
306
+ hooks_.presented(surface);
307
+ }
308
+ // The app learns the size the window opened at, which a saved frame decides.
309
+ reportFrame(surface);
310
+ }
311
+
312
+ void GtkWindows::reportFrame(SurfaceId surface, bool onAllocation) {
313
+ auto it = windows_.find(surface);
314
+ auto host = host_.lock();
315
+ // A window still waiting for its first frame has not been sized by the user.
316
+ if (it == windows_.end() || !it->second.shown || !host) {
317
+ return;
318
+ }
319
+ GtkWindow *window = GTK_WINDOW(it->second.window);
320
+ bool maximized = gtk_window_is_maximized(window);
321
+ bool fullScreen = gtk_window_is_fullscreen(window);
322
+ bool fills = windowFills(window);
323
+ // An allocation of a window with a size of its own changes nothing the app was not told.
324
+ if (onAllocation && !fills) {
325
+ return;
326
+ }
327
+ // The default size is the one the window has, or goes back to when it fills the screen.
328
+ int width = 0, height = 0;
329
+ gtk_window_get_default_size(window, &width, &height);
330
+ WindowFrame saved{.width = static_cast<double>(width), .height = static_cast<double>(height), .maximized = maximized, .fullScreen = fullScreen};
331
+ WindowFrame now = saved;
332
+ if (fills && gtk_widget_get_width(it->second.surface) > 0) {
333
+ now.width = gtk_widget_get_width(it->second.surface);
334
+ now.height = gtk_widget_get_height(it->second.surface);
335
+ }
336
+ // Each allocation comes here, but the app hears a size once.
337
+ if (onAllocation && now.width == it->second.reportedWidth && now.height == it->second.reportedHeight) {
338
+ return;
339
+ }
340
+ if (width > 0 && height > 0) {
341
+ it->second.reportedWidth = now.width;
342
+ it->second.reportedHeight = now.height;
343
+ host->resized(surface, now, windowScale(it->second.window), saved);
344
+ }
345
+ }
346
+
347
+ void GtkWindows::reportFocus(SurfaceId surface, bool active) {
348
+ auto host = host_.lock();
349
+ // A window going, which close already reported, says nothing more.
350
+ if (!host || !windows_.contains(surface)) {
351
+ return;
352
+ }
353
+ if (active) {
354
+ host->focused(surface);
355
+ } else if (host->focusedSurface() == surface) {
356
+ host->focused(std::nullopt);
357
+ }
358
+ }
359
+
360
+ void GtkWindows::raise() {
361
+ // GTK lists an application's windows most recently active first.
362
+ for (GList *l = gtk_application_get_windows(application_); l; l = l->next) {
363
+ GtkWindow *window = GTK_WINDOW(l->data);
364
+ for (const auto &[surface, open] : windows_) {
365
+ if (open.window == GTK_WIDGET(window) && open.shown) {
366
+ gtk_window_present(window);
367
+ return;
368
+ }
369
+ }
370
+ }
371
+ if (auto host = host_.lock()) {
372
+ host->reopen(false);
373
+ }
374
+ }
375
+
376
+ void GtkWindows::closeWindow(SurfaceId surface) {
377
+ onMainContext([this, surface] { close(surface); });
378
+ }
379
+
380
+ void GtkWindows::close(SurfaceId surface) {
381
+ auto it = windows_.find(surface);
382
+ if (it == windows_.end()) {
383
+ return;
384
+ }
385
+ GtkWidget *window = it->second.window;
386
+ disconnect(it->second);
387
+ windows_.erase(it);
388
+ if (hooks_.stopSurface) {
389
+ hooks_.stopSurface(surface);
390
+ }
391
+ auto host = host_.lock();
392
+ if (host && host->focusedSurface() == surface) {
393
+ host->focused(std::nullopt);
394
+ }
395
+ gtk_window_destroy(GTK_WINDOW(window));
396
+ }
397
+
398
+ void GtkWindows::focusWindow(SurfaceId surface) {
399
+ onMainContext([this, surface] {
400
+ if (GtkWidget *w = window(surface)) {
401
+ gtk_window_present(GTK_WINDOW(w));
402
+ }
403
+ });
404
+ }
405
+
406
+ void GtkWindows::setWindowTitle(SurfaceId surface, const std::string &title) {
407
+ onMainContext([this, surface, title] {
408
+ if (GtkWidget *w = window(surface)) {
409
+ gtk_window_set_title(GTK_WINDOW(w), titleFor(title));
410
+ }
411
+ });
412
+ }
413
+
414
+ void GtkWindows::beginQuit() {
415
+ onMainContext([this] {
416
+ auto host = host_.lock();
417
+ if (!host || host->quitRequested() == QuitReply::Now) {
418
+ if (hooks_.quit) {
419
+ hooks_.quit();
420
+ }
421
+ }
422
+ });
423
+ }
424
+
425
+ void GtkWindows::replyToQuit(bool proceed) {
426
+ if (proceed) {
427
+ onMainContext([this] {
428
+ if (hooks_.quit) {
429
+ hooks_.quit();
430
+ }
431
+ });
432
+ }
433
+ }
434
+
435
+ void GtkWindows::after(double seconds, std::function<void()> task) {
436
+ runAfter(seconds, std::move(task));
437
+ }
438
+
439
+ } // namespace ngn::gtk
@@ -0,0 +1,150 @@
1
+ // The app's windows on GTK (app-windows.h's WindowBackend): an AdwApplicationWindow of the app's
2
+ // AdwApplication per window the app opens, each filled by its own surface in the chrome of its
3
+ // title-bar mode (screens.h), and shown once that surface's first frame mounts. Size autosave and
4
+ // focus reports come with the window lifecycle.
5
+ #pragma once
6
+
7
+ #include "ngn/app-windows.h"
8
+ #include "ngn/default-menu.h"
9
+ #include "window-scale.h"
10
+
11
+ #include <gtk/gtk.h>
12
+
13
+ #include <functional>
14
+ #include <map>
15
+ #include <memory>
16
+ #include <string>
17
+ #include <vector>
18
+
19
+ namespace ngn::gtk {
20
+
21
+ // Shows `window` without the focus GTK gives the first focusable widget of a window shown with
22
+ // none, which would be a header bar's search field: macOS gives a new window no field the focus,
23
+ // and a field's focus is the app's to give (autoFocus). A focus set before it shows stays.
24
+ void presentWithoutInitialFocus(GtkWindow *window);
25
+
26
+ // The app's AdwApplication, registered, which every window belongs to, named by `appId` when that
27
+ // is a valid application id. It handles open (links and files). A `unique` one is the app's single
28
+ // instance: a second launch finds the first registered (g_application_get_is_remote) and hands it
29
+ // its links (forwardLaunch). `service` starts it as the app's D-Bus service, as the session bus
30
+ // does for a DBusActivatable desktop entry.
31
+ GtkApplication *startApplication(const std::string &appId, bool unique = false, bool service = false);
32
+
33
+ // A second launch's work, in the instance already running: its links and files (as a launcher's
34
+ // %U gives them) to open, or nothing to raise the app's window. Sent over the session bus.
35
+ void forwardLaunch(GtkApplication *application, const std::vector<std::string> &openArgs);
36
+
37
+ // A launch's links and files as the URLs the app's DeepLinks hears: a path becomes a file:// URL.
38
+ std::vector<std::string> openUrls(const std::vector<std::string> &openArgs);
39
+
40
+ // Calls `laidOut` each time GTK has laid out the realized `window` and is about to draw it (its
41
+ // frame clock's paint phase), for what moves with its chrome. Returns the handler's id on that
42
+ // clock, for whoever must stop it before `laidOut`'s captures go; 0 when the window has no clock.
43
+ gulong afterLayout(GtkWidget *window, std::function<void()> laidOut);
44
+
45
+ class GtkWindows final : public WindowBackend {
46
+ public:
47
+ struct Hooks {
48
+ // A window's surface widget was made: the backend mounts into it and input reads it.
49
+ std::function<void(SurfaceId surface, GtkWidget *widget)> addSurface;
50
+ // Start and size the surface's app, with its size in points and its scale.
51
+ std::function<void(SurfaceId surface, int width, int height, double scale)> startSurface;
52
+ std::function<void(SurfaceId surface, int width, int height, double scale)> resizeSurface;
53
+ // The window's scale changed, a fractional step included (window-scale.h).
54
+ std::function<void(GtkWidget *window)> rescaled;
55
+ // Stop the surface's app.
56
+ std::function<void(SurfaceId surface)> stopSurface;
57
+ // GTK laid out the surface's window: its chrome may have moved.
58
+ std::function<void(SurfaceId surface)> laidOut;
59
+ // The app agreed to quit, or nothing was listening when it was asked to.
60
+ std::function<void()> quit;
61
+ // The user closed the last of these windows, and no app listens to decide (the core's
62
+ // WindowHost::lastWindowClosed). The host decides: the app's own window may still be open.
63
+ std::function<void()> lastWindowClosed;
64
+ // Runs a window's first show, now or later (once the app hides its splash screen, splash-screen.h),
65
+ // on the main context, in the order the windows were ready, and in the same one dispatch as any
66
+ // other show the host holds past the splash. Without it a window shows as its first frame mounts.
67
+ std::function<void(std::function<void()> show)> whenShown;
68
+ // A window was shown for the first time: not one closed while the splash held it.
69
+ std::function<void(SurfaceId surface)> presented;
70
+ };
71
+
72
+ // Windows of `application`, with the app's menus where `menus` says.
73
+ GtkWindows(Hooks hooks, GtkApplication *application, MenuPlacement menus);
74
+ // Disconnects the handlers of, then destroys, the windows still open.
75
+ ~GtkWindows() override;
76
+ GtkWindows(const GtkWindows &) = delete;
77
+ GtkWindows &operator=(const GtkWindows &) = delete;
78
+
79
+ // Whether a window fills the screen, so it has the size GTK gives it rather than its own. A seam for
80
+ // tests, which run without a window manager to maximise one.
81
+ std::function<bool(GtkWindow *)> windowFills = &GtkWindows::fillsScreen;
82
+ // The app's name (the host's --title): the title of a window the app gives none, as the main window
83
+ // is unless it says otherwise. GNOME's header bar, the taskbar, Alt+Tab and Orca name a window by
84
+ // its title, where macOS names the app in its menu bar and Dock. Set once, before any window opens.
85
+ std::string appName;
86
+
87
+ // The host the close button asks first. Set once, before any window opens.
88
+ void setHost(std::weak_ptr<WindowHost> host);
89
+ // The surface's first frame mounted: its window shows. The UI thread.
90
+ void didMount(SurfaceId surface);
91
+ // The app was launched again (GApplication's activate or open): the window last active comes
92
+ // to the front, or the app hears `reopen` when it has none shown. The UI thread.
93
+ void raise();
94
+ GtkWidget *window(SurfaceId surface) const;
95
+ GtkWidget *surfaceWidget(SurfaceId surface) const;
96
+
97
+ void openWindow(SurfaceId surface, const WindowRequest &request, const WindowPlacement &placement, std::function<void()> opened) override;
98
+ void closeWindow(SurfaceId surface) override;
99
+ void focusWindow(SurfaceId surface) override;
100
+ void setWindowTitle(SurfaceId surface, const std::string &title) override;
101
+ void beginQuit() override;
102
+ void replyToQuit(bool proceed) override;
103
+ void after(double seconds, std::function<void()> task) override;
104
+
105
+ private:
106
+ struct Window {
107
+ GtkWidget *window{nullptr};
108
+ GtkWidget *surface{nullptr};
109
+ // Its first frame mounted, and it is shown, or held by whenShown until it may be.
110
+ bool mounted{false};
111
+ bool shown{false};
112
+ // The handlers that call back into these windows, stopped when the window closes or these go.
113
+ ScaleFollow scaleFollow;
114
+ gulong closeRequested{0};
115
+ // The window's notify handlers (size, maximised, full screen, focus).
116
+ std::vector<gulong> notified;
117
+ GdkFrameClock *clock{nullptr};
118
+ gulong laidOut{0};
119
+ // The size the app last heard, so an allocation that changes nothing says nothing.
120
+ double reportedWidth{0};
121
+ double reportedHeight{0};
122
+ };
123
+ void show(SurfaceId surface);
124
+ // `title`, or the app's name where it is empty.
125
+ const char *titleFor(const std::string &title) const;
126
+ static void disconnect(Window &entry);
127
+ static bool fillsScreen(GtkWindow *window);
128
+ void open(SurfaceId surface, const WindowRequest &request, const WindowPlacement &placement, std::function<void()> opened);
129
+ void close(SurfaceId surface);
130
+ // The window's size, maximised and full-screen state changed: the app hears its size now, and
131
+ // its frame autosaves with the size it goes back to (never a place: GTK has none to give).
132
+ // `onAllocation`: GTK has just allocated the surface, which only a window filling the screen reports.
133
+ void reportFrame(SurfaceId surface, bool onAllocation = false);
134
+ void reportFocus(SurfaceId surface, bool active);
135
+
136
+ Hooks hooks_;
137
+ GtkApplication *application_;
138
+ MenuPlacement menus_;
139
+ std::weak_ptr<WindowHost> host_;
140
+ std::map<SurfaceId, Window> windows_;
141
+ // Gone with these windows: a show the splash let go after that does nothing.
142
+ std::shared_ptr<void> alive_ = std::make_shared<int>(0);
143
+ };
144
+
145
+ // Runs `task` on GLib's default main context, which is the UI thread.
146
+ void onMainContext(std::function<void()> task);
147
+ // Runs `task` on the UI thread after `seconds`.
148
+ void runAfter(double seconds, std::function<void()> task);
149
+
150
+ } // namespace ngn::gtk