@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,447 @@
1
+ #include "menus.h"
2
+
3
+ #include "app-windows.h"
4
+ #include "screens.h"
5
+
6
+ #include <dlfcn.h>
7
+
8
+ #include <algorithm>
9
+ #include <cstring>
10
+ #include <map>
11
+ #include <string>
12
+
13
+ namespace ngn::gtk {
14
+
15
+ namespace {
16
+
17
+ // GDK's names for the keys the web names otherwise, both ways.
18
+ const std::map<std::string, guint> &namedKeys() {
19
+ static const std::map<std::string, guint> keys{
20
+ {"ArrowLeft", GDK_KEY_Left}, {"ArrowRight", GDK_KEY_Right}, {"ArrowUp", GDK_KEY_Up}, {"ArrowDown", GDK_KEY_Down},
21
+ {"Enter", GDK_KEY_Return}, {"Escape", GDK_KEY_Escape}, {"Backspace", GDK_KEY_BackSpace}, {"Delete", GDK_KEY_Delete},
22
+ {"Tab", GDK_KEY_Tab}, {"Insert", GDK_KEY_Insert}, {"Home", GDK_KEY_Home}, {"End", GDK_KEY_End}, {"PageUp", GDK_KEY_Page_Up},
23
+ {"PageDown", GDK_KEY_Page_Down}, {"F1", GDK_KEY_F1}, {"F2", GDK_KEY_F2}, {"F3", GDK_KEY_F3}, {"F4", GDK_KEY_F4},
24
+ {"F5", GDK_KEY_F5}, {"F6", GDK_KEY_F6}, {"F7", GDK_KEY_F7}, {"F8", GDK_KEY_F8}, {"F9", GDK_KEY_F9},
25
+ {"F10", GDK_KEY_F10}, {"F11", GDK_KEY_F11}, {"F12", GDK_KEY_F12},
26
+ };
27
+ return keys;
28
+ }
29
+
30
+ const char *webName(guint keyval) {
31
+ switch (keyval) {
32
+ case GDK_KEY_KP_Enter:
33
+ return "Enter";
34
+ case GDK_KEY_ISO_Left_Tab:
35
+ return "Tab";
36
+ }
37
+ for (const auto &[name, value] : namedKeys()) {
38
+ if (value == keyval) {
39
+ return name.c_str();
40
+ }
41
+ }
42
+ return nullptr;
43
+ }
44
+
45
+ std::string actionName(int index) {
46
+ return "item-" + std::to_string(index);
47
+ }
48
+
49
+ struct Chosen {
50
+ MenuItem item;
51
+ std::function<void(const MenuItem &)> chosen;
52
+ };
53
+
54
+ // `disabled` when a menu above is: its items are too, as findMenuItem skips the whole branch.
55
+ void addItems(GMenu *menu, const std::vector<MenuItem> &items, GSimpleActionGroup *group, const std::function<void(const MenuItem &)> &chosen, int &next,
56
+ bool disabled) {
57
+ GMenu *section = g_menu_new();
58
+ auto flush = [&] {
59
+ if (g_menu_model_get_n_items(G_MENU_MODEL(section)) > 0) {
60
+ g_menu_append_section(menu, nullptr, G_MENU_MODEL(section));
61
+ g_object_unref(section);
62
+ section = g_menu_new();
63
+ }
64
+ };
65
+ for (const auto &item : items) {
66
+ if (item.separator) {
67
+ flush();
68
+ continue;
69
+ }
70
+ // A menu with nothing in it, such as the Window menu AppKit fills, which GTK has no list for.
71
+ if (item.items.empty() && item.command == MenuCommand::None) {
72
+ continue;
73
+ }
74
+ if (!item.items.empty()) {
75
+ GMenu *sub = g_menu_new();
76
+ addItems(sub, item.items, group, chosen, next, disabled || item.disabled);
77
+ if (item.inlineItems) {
78
+ flush();
79
+ g_menu_append_section(menu, nullptr, G_MENU_MODEL(sub));
80
+ } else {
81
+ g_menu_append_submenu(section, item.title.c_str(), G_MENU_MODEL(sub));
82
+ }
83
+ g_object_unref(sub);
84
+ continue;
85
+ }
86
+ std::string name = actionName(next++);
87
+ GSimpleAction *action = item.checked ? g_simple_action_new_stateful(name.c_str(), nullptr, g_variant_new_boolean(TRUE))
88
+ : g_simple_action_new(name.c_str(), nullptr);
89
+ g_simple_action_set_enabled(action, !disabled && !item.disabled);
90
+ g_signal_connect_data(
91
+ action, item.checked ? "change-state" : "activate", G_CALLBACK(+[](GSimpleAction *, GVariant *, gpointer data) {
92
+ auto *c = static_cast<Chosen *>(data);
93
+ c->chosen(c->item);
94
+ }),
95
+ new Chosen{item, chosen}, [](gpointer data, GClosure *) { delete static_cast<Chosen *>(data); }, GConnectFlags(0));
96
+ g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(action));
97
+ g_object_unref(action);
98
+ GMenuItem *entry = g_menu_item_new(item.title.c_str(), ("menu." + name).c_str());
99
+ if (auto accel = acceleratorOf(item); !accel.empty()) {
100
+ g_menu_item_set_attribute(entry, "accel", "s", accel.c_str());
101
+ }
102
+ g_menu_append_item(section, entry);
103
+ g_object_unref(entry);
104
+ }
105
+ flush();
106
+ g_object_unref(section);
107
+ }
108
+
109
+ // A button kind's role for the window's keys, set by native-controls.cpp as "default" or "cancel".
110
+ GtkWidget *findKeyButton(GtkWidget *widget, const char *role) {
111
+ if (!gtk_widget_get_mapped(widget) || !gtk_widget_is_sensitive(widget)) {
112
+ return nullptr;
113
+ }
114
+ if (const char *kind = static_cast<const char *>(g_object_get_data(G_OBJECT(widget), "ngn-key-role")); kind && std::strcmp(kind, role) == 0) {
115
+ return widget;
116
+ }
117
+ for (GtkWidget *child = gtk_widget_get_first_child(widget); child; child = gtk_widget_get_next_sibling(child)) {
118
+ if (GtkWidget *found = findKeyButton(child, role)) {
119
+ return found;
120
+ }
121
+ }
122
+ return nullptr;
123
+ }
124
+
125
+ } // namespace
126
+
127
+ KeyChord chordOf(guint keyval, GdkModifierType state) {
128
+ KeyChord chord;
129
+ if (const char *name = webName(keyval)) {
130
+ chord.key = name;
131
+ } else if (gunichar c = gdk_keyval_to_unicode(keyval); c != 0) {
132
+ char text[8] = {};
133
+ g_unichar_to_utf8(c, text);
134
+ chord.key = text;
135
+ }
136
+ if (state & GDK_CONTROL_MASK) chord.modifiers |= Control;
137
+ if (state & GDK_SHIFT_MASK) chord.modifiers |= Shift;
138
+ if (state & GDK_ALT_MASK) chord.modifiers |= Option;
139
+ return chord;
140
+ }
141
+
142
+ std::string acceleratorOf(const MenuItem &item) {
143
+ // None on Command either: Super is the desktop's, so no press here chooses the item, and a Ctrl
144
+ // label would name a press that does not.
145
+ if (item.key.empty() || (item.modifiers & Command)) {
146
+ return {};
147
+ }
148
+ guint keyval = 0;
149
+ if (auto named = namedKeys().find(item.key); named != namedKeys().end()) {
150
+ keyval = named->second;
151
+ } else if (g_utf8_strlen(item.key.c_str(), -1) == 1) {
152
+ keyval = gdk_unicode_to_keyval(g_utf8_get_char(item.key.c_str()));
153
+ } else {
154
+ // A key named some other way, such as "Pause": shown as its first letter, it would be another key.
155
+ return {};
156
+ }
157
+ unsigned state = 0;
158
+ if (item.modifiers & Control) state |= GDK_CONTROL_MASK;
159
+ if (item.modifiers & Shift) state |= GDK_SHIFT_MASK;
160
+ if (item.modifiers & Option) state |= GDK_ALT_MASK;
161
+ gchar *name = gtk_accelerator_name(gdk_keyval_to_lower(keyval), static_cast<GdkModifierType>(state));
162
+ std::string out = name ? name : "";
163
+ g_free(name);
164
+ return out;
165
+ }
166
+
167
+ namespace {
168
+
169
+ // Whether the libadwaita running has AdwShortcutsDialog (1.8), past the floor the host builds
170
+ // against: GtkBuilder finds the type by the same function.
171
+ bool haveShortcutsDialog() {
172
+ static bool have = dlsym(RTLD_DEFAULT, "adw_shortcuts_dialog_get_type") != nullptr;
173
+ return have;
174
+ }
175
+
176
+ } // namespace
177
+
178
+ GtkWidget *keyboardShortcutsWindow(const std::vector<ShortcutGroup> &groups, GtkWindow *parent) {
179
+ auto escaped = [](const std::string &text) {
180
+ gchar *out = g_markup_escape_text(text.c_str(), -1);
181
+ std::string result = out;
182
+ g_free(out);
183
+ return result;
184
+ };
185
+ bool dialog = haveShortcutsDialog();
186
+ // A group is an AdwShortcutsSection of AdwShortcutsItems in the dialog, a GtkShortcutsGroup of
187
+ // GtkShortcutsShortcuts in the window's one section.
188
+ const char *groupClass = dialog ? "AdwShortcutsSection" : "GtkShortcutsGroup";
189
+ const char *itemClass = dialog ? "AdwShortcutsItem" : "GtkShortcutsShortcut";
190
+ std::string ui = dialog ? "<interface><object class=\"AdwShortcutsDialog\" id=\"window\">"
191
+ : "<interface><object class=\"GtkShortcutsWindow\" id=\"window\"><property name=\"modal\">1</property>"
192
+ "<child><object class=\"GtkShortcutsSection\"><property name=\"section-name\">shortcuts</property>";
193
+ for (const auto &group : groups) {
194
+ ui += std::string("<child><object class=\"") + groupClass + "\"><property name=\"title\">" + escaped(group.title) + "</property>";
195
+ for (const auto &entry : group.entries) {
196
+ std::string accelerator = acceleratorOf(MenuItem{.key = entry.chord.key, .modifiers = entry.chord.modifiers});
197
+ if (accelerator.empty()) {
198
+ continue;
199
+ }
200
+ ui += std::string("<child><object class=\"") + itemClass + "\"><property name=\"title\">" + escaped(entry.title) +
201
+ "</property><property name=\"accelerator\">" + escaped(accelerator) + "</property></object></child>";
202
+ }
203
+ ui += "</object></child>";
204
+ }
205
+ ui += dialog ? "</object></interface>" : "</object></child></object></interface>";
206
+ GtkBuilder *builder = gtk_builder_new_from_string(ui.c_str(), -1);
207
+ // The window stays in GTK's list of toplevels once the builder goes, until it is closed; the
208
+ // dialog is the caller's reference until presentKeyboardShortcuts shows it.
209
+ GtkWidget *window = GTK_WIDGET(g_object_ref(gtk_builder_get_object(builder, "window")));
210
+ g_object_unref(builder);
211
+ if (!dialog) {
212
+ g_object_unref(window);
213
+ if (parent) {
214
+ gtk_window_set_transient_for(GTK_WINDOW(window), parent);
215
+ }
216
+ }
217
+ return window;
218
+ }
219
+
220
+ void presentKeyboardShortcuts(GtkWidget *shortcuts, GtkWindow *parent) {
221
+ if (GTK_IS_WINDOW(shortcuts)) {
222
+ gtk_window_present(GTK_WINDOW(shortcuts));
223
+ return;
224
+ }
225
+ // adw_dialog_present (libadwaita 1.5): the parent holds the dialog while it shows.
226
+ using Present = void (*)(GtkWidget *, GtkWidget *);
227
+ static auto present = reinterpret_cast<Present>(dlsym(RTLD_DEFAULT, "adw_dialog_present"));
228
+ if (present) {
229
+ present(shortcuts, parent ? GTK_WIDGET(parent) : nullptr);
230
+ }
231
+ g_object_unref(shortcuts);
232
+ }
233
+
234
+ GMenuModel *buildMenu(const std::vector<MenuItem> &items, GSimpleActionGroup *group, std::function<void(const MenuItem &)> chosen) {
235
+ GMenu *menu = g_menu_new();
236
+ int next = 0;
237
+ addItems(menu, items, group, chosen, next, false);
238
+ return G_MENU_MODEL(menu);
239
+ }
240
+
241
+ GtkWidget *nestedPopoverMenu(GMenuModel *model) {
242
+ return gtk_popover_menu_new_from_model_full(model, GTK_POPOVER_MENU_NESTED);
243
+ }
244
+
245
+ GtkMenus::GtkMenus(std::function<void(const MenuItem &item)> chosen) : chosen_(std::move(chosen)) {}
246
+
247
+ GtkMenus::~GtkMenus() {
248
+ for (auto &slot : slots_) {
249
+ if (*slot) {
250
+ g_object_remove_weak_pointer(G_OBJECT(*slot), reinterpret_cast<gpointer *>(slot.get()));
251
+ }
252
+ }
253
+ g_clear_object(&model_);
254
+ g_clear_object(&actions_);
255
+ }
256
+
257
+ void GtkMenus::setMenuBar(std::vector<MenuItem> bar) {
258
+ onMainContext([this, alive = std::weak_ptr<void>(alive_), bar = std::move(bar)]() mutable {
259
+ // Queued, so these menus may have gone by the time it runs.
260
+ if (alive.expired()) {
261
+ return;
262
+ }
263
+ bar_ = std::move(bar);
264
+ g_clear_object(&model_);
265
+ g_clear_object(&actions_);
266
+ actions_ = g_simple_action_group_new();
267
+ model_ = buildMenu(bar_, actions_, chosen_);
268
+ for (auto &slot : slots_) {
269
+ if (*slot) {
270
+ show(*slot);
271
+ }
272
+ }
273
+ });
274
+ }
275
+
276
+ void GtkMenus::attach(GtkWidget *surface) {
277
+ const WindowChrome *chrome = chromeOf(surface);
278
+ if (!chrome) {
279
+ return;
280
+ }
281
+ GtkWidget *slot = chrome->menuBar ? chrome->menuBar : chrome->primaryMenu;
282
+ if (!slot) {
283
+ return;
284
+ }
285
+ // The slots of windows that have gone.
286
+ std::erase_if(slots_, [](const std::unique_ptr<GtkWidget *> &held) { return *held == nullptr; });
287
+ auto held = std::make_unique<GtkWidget *>(slot);
288
+ g_object_add_weak_pointer(G_OBJECT(slot), reinterpret_cast<gpointer *>(held.get()));
289
+ slots_.push_back(std::move(held));
290
+ if (model_) {
291
+ show(slot);
292
+ }
293
+ }
294
+
295
+ void GtkMenus::show(GtkWidget *slot) {
296
+ if (GTK_IS_POPOVER_MENU_BAR(slot)) {
297
+ gtk_popover_menu_bar_set_menu_model(GTK_POPOVER_MENU_BAR(slot), model_);
298
+ } else {
299
+ // Nested, as the menu bar's are.
300
+ gtk_menu_button_set_popover(GTK_MENU_BUTTON(slot), model_ ? nestedPopoverMenu(model_) : nullptr);
301
+ }
302
+ // The items' actions are the window's `menu` group, so each window's slot reaches them.
303
+ if (GtkRoot *root = gtk_widget_get_root(slot)) {
304
+ gtk_widget_insert_action_group(GTK_WIDGET(root), "menu", G_ACTION_GROUP(actions_));
305
+ }
306
+ gtk_widget_set_visible(slot, model_ != nullptr && g_menu_model_get_n_items(model_) > 0);
307
+ }
308
+
309
+ bool popUpContextMenu(GtkWidget *widget, facebook::react::Point point, const std::vector<MenuItem> &items, std::function<void(const MenuItem &)> chosen) {
310
+ if (items.empty()) {
311
+ return false;
312
+ }
313
+ GSimpleActionGroup *group = g_simple_action_group_new();
314
+ GMenuModel *model = buildMenu(items, group, std::move(chosen));
315
+ // Only separators and empty menus: nothing to draw, so the view's own menu instead.
316
+ if (g_menu_model_get_n_items(model) == 0) {
317
+ g_object_unref(model);
318
+ g_object_unref(group);
319
+ return false;
320
+ }
321
+ GtkWidget *popover = nestedPopoverMenu(model);
322
+ g_object_unref(model);
323
+ gtk_widget_insert_action_group(popover, "menu", G_ACTION_GROUP(group));
324
+ g_object_unref(group);
325
+ gtk_widget_set_parent(popover, widget);
326
+ gtk_popover_set_has_arrow(GTK_POPOVER(popover), FALSE);
327
+ gtk_widget_set_halign(popover, gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL ? GTK_ALIGN_END : GTK_ALIGN_START);
328
+ GdkRectangle at{static_cast<int>(point.x), static_cast<int>(point.y), 1, 1};
329
+ gtk_popover_set_pointing_to(GTK_POPOVER(popover), &at);
330
+ // It goes once closed, after the item chosen has run.
331
+ // Held until then, so a view that goes first takes nothing from under it.
332
+ g_signal_connect(popover, "closed", G_CALLBACK(+[](GtkPopover *p, gpointer) {
333
+ g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, [](gpointer data) -> gboolean {
334
+ if (gtk_widget_get_parent(GTK_WIDGET(data))) {
335
+ gtk_widget_unparent(GTK_WIDGET(data));
336
+ }
337
+ return G_SOURCE_REMOVE;
338
+ }, g_object_ref(p), g_object_unref);
339
+ }), nullptr);
340
+ gtk_popover_popup(GTK_POPOVER(popover));
341
+ return true;
342
+ }
343
+
344
+ bool textInputFocused(GtkWindow *window) {
345
+ GtkWidget *focus = gtk_root_get_focus(GTK_ROOT(window));
346
+ for (GtkWidget *w = focus; w; w = gtk_widget_get_parent(w)) {
347
+ if (GTK_IS_EDITABLE(w) || GTK_IS_TEXT_VIEW(w)) {
348
+ return true;
349
+ }
350
+ }
351
+ return false;
352
+ }
353
+
354
+ GtkWidget *keyButton(GtkWindow *window, bool cancel) {
355
+ GtkWidget *content = gtk_window_get_child(window);
356
+ return content ? findKeyButton(content, cancel ? "cancel" : "default") : nullptr;
357
+ }
358
+
359
+ void routeKeys(GtkWindow *window, std::shared_ptr<KeyRouting> routing) {
360
+ // Before the focused widget, as an app's shortcuts and the menus' keys come first.
361
+ GtkEventController *first = gtk_event_controller_key_new();
362
+ gtk_event_controller_set_propagation_phase(first, GTK_PHASE_CAPTURE);
363
+ g_signal_connect_data(
364
+ first, "key-pressed", G_CALLBACK(+[](GtkEventControllerKey *c, guint keyval, guint, GdkModifierType state, gpointer data) -> gboolean {
365
+ auto *routing = static_cast<std::shared_ptr<KeyRouting> *>(data)->get();
366
+ auto *window = GTK_WINDOW(gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(c)));
367
+ // The browser keys some keyboards have go back and forward too.
368
+ if (keyval == GDK_KEY_Back || keyval == GDK_KEY_Forward) {
369
+ routing->navigate(keyval == GDK_KEY_Back ? NavigationDirection::Back : NavigationDirection::Forward, window);
370
+ return TRUE;
371
+ }
372
+ // Super is the desktop's, never an app's.
373
+ if (state & GDK_SUPER_MASK) {
374
+ return FALSE;
375
+ }
376
+ auto chord = chordOf(keyval, static_cast<GdkModifierType>(state & gtk_accelerator_get_default_mod_mask()));
377
+ if (chord.key.empty()) {
378
+ // No chord for the shortcuts and menus to match, but the keypad's arrows, Home, End and
379
+ // Tab are the focus's as their plain keys are, which have a chord. The chord the others
380
+ // read stays empty, so no binding of Home answers keypad Home.
381
+ return routing->focus && routing->focus(window, keyval, state);
382
+ }
383
+ bool text = textInputFocused(window);
384
+ if (routing->shortcut && routing->shortcut(chord, text)) {
385
+ return TRUE;
386
+ }
387
+ if (routing->focus && routing->focus(window, keyval, state)) {
388
+ return TRUE;
389
+ }
390
+ if (auto direction = routing->navigation ? routing->navigation(chord) : std::nullopt) {
391
+ routing->navigate(*direction, window);
392
+ return TRUE;
393
+ }
394
+ const MenuItem *item = routing->menuKey ? routing->menuKey(chord) : nullptr;
395
+ // A focused text input keeps its editing keys, from the app's items as from the OS's.
396
+ if (!item || (text && (isEditingCommand(item->command) || (routing->editingKey && routing->editingKey(chord))))) {
397
+ return FALSE;
398
+ }
399
+ routing->choose(*item, window);
400
+ return TRUE;
401
+ }),
402
+ new std::shared_ptr<KeyRouting>(routing), [](gpointer data, GClosure *) { delete static_cast<std::shared_ptr<KeyRouting> *>(data); },
403
+ GConnectFlags(0));
404
+ gtk_widget_add_controller(GTK_WIDGET(window), first);
405
+
406
+ // After it, for a press nothing in the window used: Return and Escape on the window's buttons.
407
+ GtkEventController *last = gtk_event_controller_key_new();
408
+ g_signal_connect(last, "key-pressed", G_CALLBACK(+[](GtkEventControllerKey *c, guint keyval, guint, GdkModifierType state, gpointer) -> gboolean {
409
+ if ((state & gtk_accelerator_get_default_mod_mask()) != 0) {
410
+ return FALSE;
411
+ }
412
+ bool cancel = keyval == GDK_KEY_Escape;
413
+ if (!cancel && keyval != GDK_KEY_Return && keyval != GDK_KEY_KP_Enter) {
414
+ return FALSE;
415
+ }
416
+ GtkWidget *button = keyButton(GTK_WINDOW(gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(c))), cancel);
417
+ if (!button) {
418
+ return FALSE;
419
+ }
420
+ // As a press of it: GTK shows it pressed, then clicks it.
421
+ gtk_widget_activate(button);
422
+ return TRUE;
423
+ }), nullptr);
424
+ gtk_widget_add_controller(GTK_WIDGET(window), last);
425
+
426
+ // Mouse buttons 8 and 9, GDK's numbers for the side buttons, go back and forward.
427
+ GtkGesture *side = gtk_gesture_click_new();
428
+ gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(side), 0);
429
+ gtk_event_controller_set_propagation_phase(GTK_EVENT_CONTROLLER(side), GTK_PHASE_CAPTURE);
430
+ g_signal_connect_data(
431
+ side, "pressed", G_CALLBACK(+[](GtkGestureClick *g, int, double, double, gpointer data) {
432
+ guint button = gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(g));
433
+ if (button != 8 && button != 9) {
434
+ gtk_gesture_set_state(GTK_GESTURE(g), GTK_EVENT_SEQUENCE_DENIED);
435
+ return;
436
+ }
437
+ auto *routing = static_cast<std::shared_ptr<KeyRouting> *>(data)->get();
438
+ auto *window = GTK_WINDOW(gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(g)));
439
+ routing->navigate(*navigationFor(button == 8 ? NavigationInput::MouseButton4 : NavigationInput::MouseButton5), window);
440
+ gtk_gesture_set_state(GTK_GESTURE(g), GTK_EVENT_SEQUENCE_CLAIMED);
441
+ }),
442
+ new std::shared_ptr<KeyRouting>(routing), [](gpointer data, GClosure *) { delete static_cast<std::shared_ptr<KeyRouting> *>(data); },
443
+ GConnectFlags(0));
444
+ gtk_widget_add_controller(GTK_WIDGET(window), GTK_EVENT_CONTROLLER(side));
445
+ }
446
+
447
+ } // namespace ngn::gtk
@@ -0,0 +1,100 @@
1
+ // The app's menus on GTK (app-menus.h's MenuBackend), from the core's one menu model: a
2
+ // GtkPopoverMenuBar under the header bar on KDE Plasma, and the header bar's primary menu
3
+ // everywhere else (screens.h lays both slots). Each item is an action of the window's `menu` group,
4
+ // which calls `chosen` with the item. GTK's popover menus take no keys of their own, so a key
5
+ // press is looked up in the bar (default-menu.h's findMenuItem) by the window's key handling
6
+ // (keyPressed), after the app's shortcuts and the back and forward keys, as AppKit's menus come
7
+ // after them. The same builder draws a view's context menu.
8
+ #pragma once
9
+
10
+ #include "ngn/app-menus.h"
11
+ #include "ngn/default-menu.h"
12
+ #include "ngn/input.h"
13
+
14
+ #include <gtk/gtk.h>
15
+
16
+ #include <functional>
17
+ #include <memory>
18
+ #include <mutex>
19
+ #include <optional>
20
+ #include <vector>
21
+
22
+ namespace ngn::gtk {
23
+
24
+ // A key press as the web names it ("q", "Z" with Shift, "ArrowLeft", "Enter", "F5"), with the
25
+ // modifiers held: Ctrl is MenuModifier's Control, Alt its Option, and Super none.
26
+ KeyChord chordOf(guint keyval, GdkModifierType state);
27
+ // An item's key in GTK's accelerator form ("<Control>q", "<Alt>Left"), for the menu to show; empty
28
+ // when it has none.
29
+ std::string acceleratorOf(const MenuItem &item);
30
+
31
+ // `items` as a GMenu whose items are actions of `group`, each named for its place, calling
32
+ // `chosen` with its item. Disabled items, and every item in a disabled menu, are disabled actions;
33
+ // checked ones are stateful.
34
+ GMenuModel *buildMenu(const std::vector<MenuItem> &items, GSimpleActionGroup *group, std::function<void(const MenuItem &)> chosen);
35
+
36
+ // A popover menu drawing `model`, its submenus nested rather than sliding: a sliding menu opens a
37
+ // submenu by its label, so of two submenus with one label the second opened the first.
38
+ GtkWidget *nestedPopoverMenu(GMenuModel *model);
39
+
40
+ class GtkMenus final : public MenuBackend {
41
+ public:
42
+ // `chosen` runs on the UI thread with each item chosen from a menu or by its key.
43
+ explicit GtkMenus(std::function<void(const MenuItem &item)> chosen);
44
+ ~GtkMenus() override;
45
+
46
+ void setMenuBar(std::vector<MenuItem> bar) override;
47
+ // A window's chrome (by its surface widget) shows the bar from now on. The UI thread.
48
+ void attach(GtkWidget *surface);
49
+ // The bar as drawn, for a key press to look up. The UI thread.
50
+ const std::vector<MenuItem> &bar() const {
51
+ return bar_;
52
+ }
53
+
54
+ private:
55
+ void show(GtkWidget *slot);
56
+
57
+ std::function<void(const MenuItem &)> chosen_;
58
+ std::vector<MenuItem> bar_;
59
+ GMenuModel *model_{nullptr};
60
+ GSimpleActionGroup *actions_{nullptr};
61
+ // The menu bars and primary menu buttons showing the bar, each cleared as its window goes.
62
+ std::vector<std::unique_ptr<GtkWidget *>> slots_;
63
+ // Expires as these menus go, for a bar update queued before then.
64
+ std::shared_ptr<void> alive_{std::make_shared<int>(0)};
65
+ };
66
+
67
+ // Linux's Keyboard Shortcuts (MenuHost::keyboardShortcuts's groups), over `parent` when given,
68
+ // built through GtkBuilder: libadwaita 1.8's AdwShortcutsDialog, a section a group, where the
69
+ // libadwaita running has it, and GTK's GtkShortcutsWindow below 1.8. Not yet shown.
70
+ GtkWidget *keyboardShortcutsWindow(const std::vector<ShortcutGroup> &groups, GtkWindow *parent);
71
+ // Shows what keyboardShortcutsWindow made: a dialog over its parent, a window on its own.
72
+ void presentKeyboardShortcuts(GtkWidget *shortcuts, GtkWindow *parent);
73
+
74
+ // A view's context menu at `point` in `widget`: chosen items call `chosen`. True once shown.
75
+ bool popUpContextMenu(GtkWidget *widget, facebook::react::Point point, const std::vector<MenuItem> &items, std::function<void(const MenuItem &)> chosen);
76
+
77
+ // The window's key handling, in the order AppKit's has it, for a window holding the app's
78
+ // surfaces: the app's shortcuts (`shortcut`, true when one took the press), then the focus keys
79
+ // (`focus`: Tab, Shift-Tab and a composite widget's arrows, focus.h), then the back and
80
+ // forward keys (`navigate`), then a menu item's key (`menuKey`, the item for the press or null).
81
+ // A text input that has the focus keeps its own editing keys: an editing command's item's, and any
82
+ // item's on a key `editingKey` names (app-menus.h's editingChord). Then, as the press bubbles back up
83
+ // unused, Return presses the window's `default` native button and Escape its `cancel` one.
84
+ struct KeyRouting {
85
+ std::function<bool(const KeyChord &chord, bool textInputFocused)> shortcut;
86
+ std::function<bool(GtkWindow *window, guint keyval, GdkModifierType state)> focus;
87
+ std::function<std::optional<NavigationDirection>(const KeyChord &chord)> navigation;
88
+ std::function<bool(const KeyChord &chord)> editingKey;
89
+ std::function<void(NavigationDirection direction, GtkWindow *window)> navigate;
90
+ std::function<const MenuItem *(const KeyChord &chord)> menuKey;
91
+ std::function<void(const MenuItem &item, GtkWindow *window)> choose;
92
+ };
93
+ void routeKeys(GtkWindow *window, std::shared_ptr<KeyRouting> routing);
94
+ // Whether the focus in `window` is a text input's, which owns the editing keys.
95
+ bool textInputFocused(GtkWindow *window);
96
+ // The `default` (Return) or `cancel` (Escape) native button of `window` that is shown and enabled,
97
+ // or null.
98
+ GtkWidget *keyButton(GtkWindow *window, bool cancel);
99
+
100
+ } // namespace ngn::gtk