@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.
- package/README.md +6 -3
- package/bin/app-config.mjs +1 -0
- package/bin/archive.mjs +166 -0
- package/bin/autolink.mjs +61 -30
- package/bin/cli.mjs +127 -27
- package/bin/host.mjs +255 -58
- package/bin/package-linux.mjs +1394 -0
- package/bin/package.mjs +171 -35
- package/bin/plist.mjs +6 -2
- package/bin/png-icon.mjs +315 -0
- package/bin/prebuilt.mjs +147 -26
- package/bin/toolchain.mjs +258 -19
- package/native/CMakeLists.txt +64 -6
- package/native/appkit/CMakeLists.txt +2 -1
- package/native/appkit/appkit-backend.h +1 -0
- package/native/appkit/appkit-backend.mm +13 -0
- package/native/appkit/header-toolbar.mm +28 -25
- package/native/appkit/host-view.h +10 -0
- package/native/appkit/host-view.mm +146 -40
- package/native/appkit/input-replay.h +6 -0
- package/native/appkit/input-replay.mm +21 -12
- package/native/appkit/main.mm +11 -7
- package/native/appkit/native-controls.mm +13 -1
- package/native/appkit/split-view.mm +7 -13
- package/native/appkit/text-input-field.h +4 -0
- package/native/appkit/text-input-field.mm +20 -1
- package/native/cmake/curl.cmake +79 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +5 -0
- package/native/cmake/libraries/react-native-worklets.cmake +5 -0
- package/native/cmake/linked-modules.cmake +20 -4
- package/native/cmake/react-native.cmake +18 -1
- package/native/core/include/ngn/accent-colors.h +38 -0
- package/native/core/include/ngn/accessibility-relations.h +65 -0
- package/native/core/include/ngn/accessibility.h +26 -0
- package/native/core/include/ngn/animated-frame-loop.h +56 -0
- package/native/core/include/ngn/app-info.h +24 -8
- package/native/core/include/ngn/app-menus.h +14 -1
- package/native/core/include/ngn/app-windows.h +15 -2
- package/native/core/include/ngn/aria-role.h +96 -0
- package/native/core/include/ngn/audio.h +2 -0
- package/native/core/include/ngn/camera.h +9 -0
- package/native/core/include/ngn/control-sizes.h +7 -2
- package/native/core/include/ngn/crash-report.h +31 -0
- package/native/core/include/ngn/curl-network.h +93 -0
- package/native/core/include/ngn/dbus-menu.h +39 -0
- package/native/core/include/ngn/default-menu.h +36 -0
- package/native/core/include/ngn/desktop-expo.h +7 -1
- package/native/core/include/ngn/desktop-modules.h +22 -0
- package/native/core/include/ngn/desktop-props.h +21 -1
- package/native/core/include/ngn/desktop-view-props.h +6 -0
- package/native/core/include/ngn/focus-order.h +10 -0
- package/native/core/include/ngn/header-bar.h +22 -0
- package/native/core/include/ngn/host-os.h +6 -0
- package/native/core/include/ngn/host-reload.h +21 -0
- package/native/core/include/ngn/host-state.h +23 -0
- package/native/core/include/ngn/image-source.h +26 -1
- package/native/core/include/ngn/input-script.h +3 -3
- package/native/core/include/ngn/input.h +4 -1
- package/native/core/include/ngn/launch-frame.h +21 -0
- package/native/core/include/ngn/launch-settings.h +23 -5
- package/native/core/include/ngn/mounting-backend.h +5 -0
- package/native/core/include/ngn/mounting-manager.h +1 -0
- package/native/core/include/ngn/native-controls.h +26 -5
- package/native/core/include/ngn/notification-schedule.h +74 -0
- package/native/core/include/ngn/notifications.h +2 -1
- package/native/core/include/ngn/openssl-update-crypto.h +15 -0
- package/native/core/include/ngn/photo-library.h +60 -20
- package/native/core/include/ngn/posix-locale.h +43 -0
- package/native/core/include/ngn/runtime.h +5 -0
- package/native/core/include/ngn/safe-areas.h +14 -2
- package/native/core/include/ngn/split-view.h +78 -0
- package/native/core/include/ngn/surface-metrics.h +7 -0
- package/native/core/include/ngn/system.h +14 -0
- package/native/core/include/ngn/text-input.h +13 -0
- package/native/core/include/ngn/text-units.h +38 -0
- package/native/core/include/ngn/updates.h +14 -2
- package/native/core/include/ngn/view-box.h +40 -0
- package/native/core/include/ngn/view-transform.h +15 -0
- package/native/core/include/ngn/web-browser.h +46 -0
- package/native/core/src/accent-colors.cpp +88 -0
- package/native/core/src/accessibility-relations.cpp +138 -0
- package/native/core/src/accessibility.cpp +50 -0
- package/native/core/src/animated-frame-loop.cpp +166 -0
- package/native/core/src/app-menus.cpp +26 -9
- package/native/core/src/app-windows.cpp +27 -6
- package/native/core/src/aria-role.cpp +127 -0
- package/native/core/src/control-sizes.cpp +9 -5
- package/native/core/src/crash-report.cpp +196 -0
- package/native/core/src/curl-network.cpp +1562 -0
- package/native/core/src/dbus-menu.cpp +71 -0
- package/native/core/src/default-menu.cpp +134 -0
- package/native/core/src/desktop-view-props.cpp +14 -0
- package/native/core/src/focus-order.cpp +19 -0
- package/native/core/src/header-bar.cpp +20 -0
- package/native/core/src/host-os.cpp +19 -0
- package/native/core/src/host-reload.cpp +45 -0
- package/native/core/src/host-state.cpp +8 -0
- package/native/core/src/image-source.cpp +90 -0
- package/native/core/src/input-dispatcher.cpp +27 -5
- package/native/core/src/input-script.cpp +3 -2
- package/native/core/src/launch-settings.cpp +76 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +6 -1
- package/native/core/src/modules/app-info.cpp +55 -6
- package/native/core/src/modules/app-modules.cpp +1 -1
- package/native/core/src/modules/audio.cpp +4 -2
- package/native/core/src/modules/camera.cpp +8 -11
- package/native/core/src/modules/desktop-expo.cpp +10 -5
- package/native/core/src/modules/desktop-modules.cpp +106 -41
- package/native/core/src/modules/file-photo-picker.cpp +408 -0
- package/native/core/src/modules/notifications.cpp +13 -17
- package/native/core/src/modules/photo-library.cpp +42 -13
- package/native/core/src/modules/web-browser.cpp +89 -0
- package/native/core/src/mounting-manager.cpp +12 -0
- package/native/core/src/native-controls.cpp +42 -1
- package/native/core/src/notification-schedule.cpp +429 -0
- package/native/core/src/openssl-update-crypto.cpp +83 -0
- package/native/core/src/posix-locale.cpp +192 -0
- package/native/core/src/runtime.cpp +30 -1
- package/native/core/src/safe-areas.cpp +19 -4
- package/native/core/src/spawn-rules.cpp +4 -1
- package/native/core/src/split-view.cpp +93 -0
- package/native/core/src/system.cpp +19 -0
- package/native/core/src/text-input.cpp +18 -1
- package/native/core/src/text-units.cpp +99 -0
- package/native/core/src/updates.cpp +76 -17
- package/native/core/src/view-box.cpp +75 -40
- package/native/core/src/view-transform.cpp +28 -0
- package/native/docker/Dockerfile +38 -0
- package/native/docker/Dockerfile.fedora +36 -0
- package/native/docker/glibc-floor.sh +49 -0
- package/native/gtk/CMakeLists.txt +137 -0
- package/native/gtk/accent-colour.cpp +57 -0
- package/native/gtk/accent-colour.h +41 -0
- package/native/gtk/accessibility-settings.cpp +159 -0
- package/native/gtk/accessibility-settings.h +62 -0
- package/native/gtk/accessible-text.cpp +151 -0
- package/native/gtk/accessible-text.h +49 -0
- package/native/gtk/accessible.cpp +306 -0
- package/native/gtk/accessible.h +75 -0
- package/native/gtk/alerts.cpp +267 -0
- package/native/gtk/alerts.h +30 -0
- package/native/gtk/animation-frames.cpp +159 -0
- package/native/gtk/animation-frames.h +20 -0
- package/native/gtk/app-info.cpp +91 -0
- package/native/gtk/app-info.h +25 -0
- package/native/gtk/app-windows.cpp +439 -0
- package/native/gtk/app-windows.h +150 -0
- package/native/gtk/audio.cpp +370 -0
- package/native/gtk/audio.h +54 -0
- package/native/gtk/auth-session.cpp +73 -0
- package/native/gtk/auth-session.h +16 -0
- package/native/gtk/battery.cpp +110 -0
- package/native/gtk/battery.h +19 -0
- package/native/gtk/box-paint.cpp +262 -0
- package/native/gtk/box-paint.h +32 -0
- package/native/gtk/cairo-canvas.cpp +755 -0
- package/native/gtk/cairo-canvas.h +49 -0
- package/native/gtk/camera.cpp +909 -0
- package/native/gtk/camera.h +69 -0
- package/native/gtk/capture.cpp +46 -0
- package/native/gtk/capture.h +19 -0
- package/native/gtk/clock-format.cpp +151 -0
- package/native/gtk/clock-format.h +36 -0
- package/native/gtk/crypto.cpp +75 -0
- package/native/gtk/crypto.h +14 -0
- package/native/gtk/dbus-properties.cpp +131 -0
- package/native/gtk/dbus-properties.h +61 -0
- package/native/gtk/drag-and-drop.cpp +339 -0
- package/native/gtk/drag-and-drop.h +30 -0
- package/native/gtk/file-dialogs.cpp +239 -0
- package/native/gtk/file-dialogs.h +21 -0
- package/native/gtk/file-system.cpp +212 -0
- package/native/gtk/file-system.h +25 -0
- package/native/gtk/focus.cpp +282 -0
- package/native/gtk/focus.h +37 -0
- package/native/gtk/fonts.cpp +89 -0
- package/native/gtk/fonts.h +22 -0
- package/native/gtk/gst-frames.cpp +150 -0
- package/native/gtk/gst-frames.h +47 -0
- package/native/gtk/gst-playback.cpp +496 -0
- package/native/gtk/gst-playback.h +81 -0
- package/native/gtk/gtk-backend.cpp +690 -0
- package/native/gtk/gtk-backend.h +134 -0
- package/native/gtk/host-sources.cpp +354 -0
- package/native/gtk/host-sources.h +46 -0
- package/native/gtk/image-manipulator.cpp +254 -0
- package/native/gtk/image-manipulator.h +30 -0
- package/native/gtk/image-view.cpp +256 -0
- package/native/gtk/image-view.h +54 -0
- package/native/gtk/input-replay.cpp +299 -0
- package/native/gtk/input-replay.h +36 -0
- package/native/gtk/launcher-badge.cpp +29 -0
- package/native/gtk/launcher-badge.h +21 -0
- package/native/gtk/locales.cpp +131 -0
- package/native/gtk/locales.h +23 -0
- package/native/gtk/location.cpp +274 -0
- package/native/gtk/location.h +19 -0
- package/native/gtk/main.cpp +1092 -0
- package/native/gtk/menus.cpp +447 -0
- package/native/gtk/menus.h +100 -0
- package/native/gtk/native-controls.cpp +831 -0
- package/native/gtk/native-controls.h +58 -0
- package/native/gtk/network-state.cpp +72 -0
- package/native/gtk/network-state.h +24 -0
- package/native/gtk/notifications.cpp +235 -0
- package/native/gtk/notifications.h +30 -0
- package/native/gtk/pango-text.cpp +274 -0
- package/native/gtk/pango-text.h +73 -0
- package/native/gtk/portal.cpp +451 -0
- package/native/gtk/portal.h +71 -0
- package/native/gtk/printing.cpp +338 -0
- package/native/gtk/printing.h +34 -0
- package/native/gtk/proxy-resolver.cpp +74 -0
- package/native/gtk/proxy-resolver.h +20 -0
- package/native/gtk/screens.cpp +1461 -0
- package/native/gtk/screens.h +274 -0
- package/native/gtk/scroll-view.cpp +414 -0
- package/native/gtk/scroll-view.h +82 -0
- package/native/gtk/secret-store.cpp +120 -0
- package/native/gtk/secret-store.h +27 -0
- package/native/gtk/settings-portal.cpp +129 -0
- package/native/gtk/settings-portal.h +47 -0
- package/native/gtk/split-view.cpp +1105 -0
- package/native/gtk/split-view.h +195 -0
- package/native/gtk/status-notifier.cpp +487 -0
- package/native/gtk/status-notifier.h +43 -0
- package/native/gtk/surface-input.cpp +251 -0
- package/native/gtk/surface-input.h +57 -0
- package/native/gtk/system-access.cpp +295 -0
- package/native/gtk/system-access.h +37 -0
- package/native/gtk/text-field.cpp +827 -0
- package/native/gtk/text-field.h +78 -0
- package/native/gtk/video.cpp +527 -0
- package/native/gtk/video.h +47 -0
- package/native/gtk/view-widget.cpp +431 -0
- package/native/gtk/view-widget.h +105 -0
- package/native/gtk/window-scale.cpp +95 -0
- package/native/gtk/window-scale.h +35 -0
- package/native/scripts/atspi-check.py +310 -0
- package/native/scripts/atspi-check.test.py +48 -0
- package/native/scripts/build.sh +20 -3
- package/native/scripts/compilers.sh +17 -0
- package/native/scripts/deb-depends-check.sh +22 -0
- package/native/scripts/fetch.sh +23 -2
- package/native/scripts/launch-cell.sh +157 -0
- package/native/scripts/launch-probe.sh +14 -3
- package/native/scripts/make-prebuilt.mjs +133 -44
- package/native/scripts/prebuilt-host-cache.mjs +71 -40
- package/native/scripts/reset-for-compilers.sh +44 -0
- package/native/scripts/start-xvfb.sh +35 -0
- package/native/shim/paragraph-platform/react/renderer/components/text/HostPlatformParagraphProps.h +35 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +5 -2
- package/native/shim/worklets/linux-reanimated-props.h +14 -0
- package/native/shim/worklets/linux-worklets.h +23 -0
- package/package.json +6 -3
- package/prebuilt.json +70 -6
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
#include "accessible.h"
|
|
2
|
+
|
|
3
|
+
#include "text-field.h"
|
|
4
|
+
#include "view-widget.h"
|
|
5
|
+
|
|
6
|
+
#include "ngn/native-controls.h"
|
|
7
|
+
|
|
8
|
+
#include <react/renderer/components/view/ViewProps.h>
|
|
9
|
+
|
|
10
|
+
#include <dlfcn.h>
|
|
11
|
+
|
|
12
|
+
#include <algorithm>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <string_view>
|
|
15
|
+
#include <utility>
|
|
16
|
+
#include <vector>
|
|
17
|
+
|
|
18
|
+
namespace ngn::gtk {
|
|
19
|
+
|
|
20
|
+
std::optional<AxInfo> accessibilityOf(const facebook::react::ShadowView &view) {
|
|
21
|
+
auto props = std::dynamic_pointer_cast<const facebook::react::ViewProps>(view.props);
|
|
22
|
+
if (!props) {
|
|
23
|
+
return std::nullopt;
|
|
24
|
+
}
|
|
25
|
+
if (view.componentName == std::string("Paragraph")) {
|
|
26
|
+
return paragraphAccessibilityInfo(*props, paragraphText(view.state));
|
|
27
|
+
}
|
|
28
|
+
return accessibilityInfo(*props);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
GtkAccessibleRole accessibleRole(const facebook::react::ShadowView &view) {
|
|
32
|
+
auto props = std::dynamic_pointer_cast<const facebook::react::ViewProps>(view.props);
|
|
33
|
+
auto info = accessibilityOf(view);
|
|
34
|
+
// A native control's GTK widget is the accessible element, with its own role and state, as
|
|
35
|
+
// AppKit's control is on macOS: the view holding it is not one too. So is a text input's field,
|
|
36
|
+
// a text box with the label.
|
|
37
|
+
if (!props || !info || controlKind(view.componentName) || std::string_view(view.componentName) == "TextInput") {
|
|
38
|
+
return GTK_ACCESSIBLE_ROLE_GENERIC;
|
|
39
|
+
}
|
|
40
|
+
return gtkRole(ariaRole(roleName(*props), *info));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
GtkAccessibleRole gtkRole(AriaRole role) {
|
|
44
|
+
switch (role) {
|
|
45
|
+
case AriaRole::Generic: return GTK_ACCESSIBLE_ROLE_GENERIC;
|
|
46
|
+
case AriaRole::None: return GTK_ACCESSIBLE_ROLE_NONE;
|
|
47
|
+
case AriaRole::Presentation: return GTK_ACCESSIBLE_ROLE_PRESENTATION;
|
|
48
|
+
case AriaRole::Alert: return GTK_ACCESSIBLE_ROLE_ALERT;
|
|
49
|
+
case AriaRole::AlertDialog: return GTK_ACCESSIBLE_ROLE_ALERT_DIALOG;
|
|
50
|
+
case AriaRole::Banner: return GTK_ACCESSIBLE_ROLE_BANNER;
|
|
51
|
+
case AriaRole::Button: return GTK_ACCESSIBLE_ROLE_BUTTON;
|
|
52
|
+
case AriaRole::Cell: return GTK_ACCESSIBLE_ROLE_CELL;
|
|
53
|
+
case AriaRole::Checkbox: return GTK_ACCESSIBLE_ROLE_CHECKBOX;
|
|
54
|
+
case AriaRole::ColumnHeader: return GTK_ACCESSIBLE_ROLE_COLUMN_HEADER;
|
|
55
|
+
case AriaRole::ComboBox: return GTK_ACCESSIBLE_ROLE_COMBO_BOX;
|
|
56
|
+
case AriaRole::Dialog: return GTK_ACCESSIBLE_ROLE_DIALOG;
|
|
57
|
+
case AriaRole::Document: return GTK_ACCESSIBLE_ROLE_DOCUMENT;
|
|
58
|
+
case AriaRole::Feed: return GTK_ACCESSIBLE_ROLE_FEED;
|
|
59
|
+
case AriaRole::Form: return GTK_ACCESSIBLE_ROLE_FORM;
|
|
60
|
+
case AriaRole::Grid: return GTK_ACCESSIBLE_ROLE_GRID;
|
|
61
|
+
case AriaRole::GridCell: return GTK_ACCESSIBLE_ROLE_GRID_CELL;
|
|
62
|
+
case AriaRole::Group: return GTK_ACCESSIBLE_ROLE_GROUP;
|
|
63
|
+
case AriaRole::Heading: return GTK_ACCESSIBLE_ROLE_HEADING;
|
|
64
|
+
case AriaRole::Img: return GTK_ACCESSIBLE_ROLE_IMG;
|
|
65
|
+
case AriaRole::Label: return GTK_ACCESSIBLE_ROLE_LABEL;
|
|
66
|
+
case AriaRole::Link: return GTK_ACCESSIBLE_ROLE_LINK;
|
|
67
|
+
case AriaRole::List: return GTK_ACCESSIBLE_ROLE_LIST;
|
|
68
|
+
case AriaRole::ListBox: return GTK_ACCESSIBLE_ROLE_LIST_BOX;
|
|
69
|
+
case AriaRole::ListItem: return GTK_ACCESSIBLE_ROLE_LIST_ITEM;
|
|
70
|
+
case AriaRole::Log: return GTK_ACCESSIBLE_ROLE_LOG;
|
|
71
|
+
case AriaRole::Main: return GTK_ACCESSIBLE_ROLE_MAIN;
|
|
72
|
+
case AriaRole::Marquee: return GTK_ACCESSIBLE_ROLE_MARQUEE;
|
|
73
|
+
case AriaRole::Math: return GTK_ACCESSIBLE_ROLE_MATH;
|
|
74
|
+
case AriaRole::Menu: return GTK_ACCESSIBLE_ROLE_MENU;
|
|
75
|
+
case AriaRole::MenuBar: return GTK_ACCESSIBLE_ROLE_MENU_BAR;
|
|
76
|
+
case AriaRole::MenuItem: return GTK_ACCESSIBLE_ROLE_MENU_ITEM;
|
|
77
|
+
case AriaRole::MenuItemCheckbox: return GTK_ACCESSIBLE_ROLE_MENU_ITEM_CHECKBOX;
|
|
78
|
+
case AriaRole::MenuItemRadio: return GTK_ACCESSIBLE_ROLE_MENU_ITEM_RADIO;
|
|
79
|
+
case AriaRole::Meter: return GTK_ACCESSIBLE_ROLE_METER;
|
|
80
|
+
case AriaRole::Navigation: return GTK_ACCESSIBLE_ROLE_NAVIGATION;
|
|
81
|
+
case AriaRole::Note: return GTK_ACCESSIBLE_ROLE_NOTE;
|
|
82
|
+
case AriaRole::Option: return GTK_ACCESSIBLE_ROLE_OPTION;
|
|
83
|
+
case AriaRole::ProgressBar: return GTK_ACCESSIBLE_ROLE_PROGRESS_BAR;
|
|
84
|
+
case AriaRole::Radio: return GTK_ACCESSIBLE_ROLE_RADIO;
|
|
85
|
+
case AriaRole::RadioGroup: return GTK_ACCESSIBLE_ROLE_RADIO_GROUP;
|
|
86
|
+
case AriaRole::Region: return GTK_ACCESSIBLE_ROLE_REGION;
|
|
87
|
+
case AriaRole::Row: return GTK_ACCESSIBLE_ROLE_ROW;
|
|
88
|
+
case AriaRole::RowGroup: return GTK_ACCESSIBLE_ROLE_ROW_GROUP;
|
|
89
|
+
case AriaRole::RowHeader: return GTK_ACCESSIBLE_ROLE_ROW_HEADER;
|
|
90
|
+
case AriaRole::Scrollbar: return GTK_ACCESSIBLE_ROLE_SCROLLBAR;
|
|
91
|
+
case AriaRole::SearchBox: return GTK_ACCESSIBLE_ROLE_SEARCH_BOX;
|
|
92
|
+
case AriaRole::Separator: return GTK_ACCESSIBLE_ROLE_SEPARATOR;
|
|
93
|
+
case AriaRole::Slider: return GTK_ACCESSIBLE_ROLE_SLIDER;
|
|
94
|
+
case AriaRole::SpinButton: return GTK_ACCESSIBLE_ROLE_SPIN_BUTTON;
|
|
95
|
+
case AriaRole::Status: return GTK_ACCESSIBLE_ROLE_STATUS;
|
|
96
|
+
case AriaRole::Switch: return GTK_ACCESSIBLE_ROLE_SWITCH;
|
|
97
|
+
case AriaRole::Tab: return GTK_ACCESSIBLE_ROLE_TAB;
|
|
98
|
+
case AriaRole::TabList: return GTK_ACCESSIBLE_ROLE_TAB_LIST;
|
|
99
|
+
case AriaRole::TabPanel: return GTK_ACCESSIBLE_ROLE_TAB_PANEL;
|
|
100
|
+
case AriaRole::Table: return GTK_ACCESSIBLE_ROLE_TABLE;
|
|
101
|
+
case AriaRole::Timer: return GTK_ACCESSIBLE_ROLE_TIMER;
|
|
102
|
+
case AriaRole::ToggleButton: return GTK_ACCESSIBLE_ROLE_TOGGLE_BUTTON;
|
|
103
|
+
case AriaRole::Toolbar: return GTK_ACCESSIBLE_ROLE_TOOLBAR;
|
|
104
|
+
case AriaRole::Tooltip: return GTK_ACCESSIBLE_ROLE_TOOLTIP;
|
|
105
|
+
case AriaRole::Tree: return GTK_ACCESSIBLE_ROLE_TREE;
|
|
106
|
+
case AriaRole::TreeGrid: return GTK_ACCESSIBLE_ROLE_TREE_GRID;
|
|
107
|
+
case AriaRole::TreeItem: return GTK_ACCESSIBLE_ROLE_TREE_ITEM;
|
|
108
|
+
// GTK 4.14's ARTICLE is past the 4.10 floor the host builds against.
|
|
109
|
+
case AriaRole::Article: return GTK_ACCESSIBLE_ROLE_GROUP;
|
|
110
|
+
}
|
|
111
|
+
return GTK_ACCESSIBLE_ROLE_GENERIC;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
GtkWidget *accessibleWidget(const ViewData &view) {
|
|
115
|
+
// The editor, not a multiline field's scrolled window: that one is only a group.
|
|
116
|
+
return view.field ? view.field->text : view.widget;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
GtkWidget *relationWidget(const ViewData &view) {
|
|
120
|
+
return view.control ? view.control : accessibleWidget(view);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
namespace {
|
|
124
|
+
|
|
125
|
+
struct AxActions {
|
|
126
|
+
GSimpleActionGroup *group{nullptr};
|
|
127
|
+
GSimpleActionGroup *custom{nullptr};
|
|
128
|
+
AccessibleActionSinks sinks;
|
|
129
|
+
// The declared actions in `custom`: each GAction's name, and the name the app declared.
|
|
130
|
+
std::vector<std::pair<std::string, std::string>> customNames;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
constexpr std::pair<const char *, AxVerb> kVerbs[] = {
|
|
134
|
+
{"press", AxVerb::Press}, {"increment", AxVerb::Increment}, {"decrement", AxVerb::Decrement}, {"cancel", AxVerb::Cancel}};
|
|
135
|
+
|
|
136
|
+
AxActions *actionsOf(GtkWidget *widget) {
|
|
137
|
+
return static_cast<AxActions *>(g_object_get_data(G_OBJECT(widget), "ngn-ax-actions"));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
void performVerb(GSimpleAction *self, GVariant *, gpointer data) {
|
|
141
|
+
auto *widget = static_cast<GtkWidget *>(data);
|
|
142
|
+
AxActions *actions = actionsOf(widget);
|
|
143
|
+
const char *name = g_action_get_name(G_ACTION(self));
|
|
144
|
+
for (const auto &[verbName, verb] : kVerbs) {
|
|
145
|
+
if (actions && std::string_view(name) == verbName) {
|
|
146
|
+
actions->sinks.perform(widget, verb);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
void sendCustom(GSimpleAction *self, GVariant *, gpointer data) {
|
|
152
|
+
auto *widget = static_cast<GtkWidget *>(data);
|
|
153
|
+
AxActions *actions = actionsOf(widget);
|
|
154
|
+
const char *name = g_action_get_name(G_ACTION(self));
|
|
155
|
+
for (size_t i = 0; actions && i < actions->customNames.size(); i++) {
|
|
156
|
+
if (actions->customNames[i].first == name) {
|
|
157
|
+
// A copy: the app's handler can change the view's actions, which replaces the list.
|
|
158
|
+
std::string declared = actions->customNames[i].second;
|
|
159
|
+
actions->sinks.send(widget, declared);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
} // namespace
|
|
166
|
+
|
|
167
|
+
std::vector<std::pair<std::string, std::string>> customActionNames(const std::vector<AxAction> &declared) {
|
|
168
|
+
std::vector<std::pair<std::string, std::string>> names;
|
|
169
|
+
for (const auto &action : declared) {
|
|
170
|
+
std::string name;
|
|
171
|
+
for (char c : action.name) {
|
|
172
|
+
name += g_ascii_isalnum(c) || c == '-' || c == '.' ? c : '-';
|
|
173
|
+
}
|
|
174
|
+
if (name.empty()) {
|
|
175
|
+
name = "action";
|
|
176
|
+
}
|
|
177
|
+
std::string unique = name;
|
|
178
|
+
for (int n = 2; std::any_of(names.begin(), names.end(), [&unique](const auto &taken) { return taken.first == unique; }); n++) {
|
|
179
|
+
unique = name + "-" + std::to_string(n);
|
|
180
|
+
}
|
|
181
|
+
names.emplace_back(unique, action.name);
|
|
182
|
+
}
|
|
183
|
+
return names;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
void applyAccessibleActions(GtkWidget *widget, const AxInfo &info, AccessibleActionSinks sinks) {
|
|
187
|
+
AxActions *actions = actionsOf(widget);
|
|
188
|
+
bool offers = info.element && !info.disabled &&
|
|
189
|
+
(info.offersPress || info.offersIncrement || info.offersCancel || !info.customActions.empty());
|
|
190
|
+
if (!actions && !offers) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (!actions) {
|
|
194
|
+
actions = new AxActions{g_simple_action_group_new(), g_simple_action_group_new(), std::move(sinks)};
|
|
195
|
+
for (const auto &[name, verb] : kVerbs) {
|
|
196
|
+
GSimpleAction *action = g_simple_action_new(name, nullptr);
|
|
197
|
+
g_signal_connect(action, "activate", G_CALLBACK(performVerb), widget);
|
|
198
|
+
g_action_map_add_action(G_ACTION_MAP(actions->group), G_ACTION(action));
|
|
199
|
+
g_object_unref(action);
|
|
200
|
+
}
|
|
201
|
+
gtk_widget_insert_action_group(widget, "accessibility", G_ACTION_GROUP(actions->group));
|
|
202
|
+
gtk_widget_insert_action_group(widget, "custom", G_ACTION_GROUP(actions->custom));
|
|
203
|
+
g_object_set_data_full(G_OBJECT(widget), "ngn-ax-actions", actions, +[](gpointer data) {
|
|
204
|
+
auto *a = static_cast<AxActions *>(data);
|
|
205
|
+
g_object_unref(a->group);
|
|
206
|
+
g_object_unref(a->custom);
|
|
207
|
+
delete a;
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
auto enable = [actions](const char *name, bool on) {
|
|
211
|
+
g_simple_action_set_enabled(G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(actions->group), name)), on);
|
|
212
|
+
};
|
|
213
|
+
// GTK checks the widget's sensitivity, not the accessible disabled state, so a disabled view
|
|
214
|
+
// offers no action of its own accord.
|
|
215
|
+
bool live = info.element && !info.disabled;
|
|
216
|
+
enable("press", live && info.offersPress);
|
|
217
|
+
enable("increment", live && info.offersIncrement);
|
|
218
|
+
enable("decrement", live && info.offersIncrement);
|
|
219
|
+
enable("cancel", live && info.offersCancel);
|
|
220
|
+
// The declared actions as they are now, those gone removed, as AppKit asks for its custom actions
|
|
221
|
+
// afresh each time.
|
|
222
|
+
auto names = live ? customActionNames(info.customActions) : std::vector<std::pair<std::string, std::string>>{};
|
|
223
|
+
if (names != actions->customNames) {
|
|
224
|
+
for (const auto &[name, declared] : actions->customNames) {
|
|
225
|
+
g_action_map_remove_action(G_ACTION_MAP(actions->custom), name.c_str());
|
|
226
|
+
}
|
|
227
|
+
actions->customNames = std::move(names);
|
|
228
|
+
for (const auto &[name, declared] : actions->customNames) {
|
|
229
|
+
GSimpleAction *action = g_simple_action_new(name.c_str(), nullptr);
|
|
230
|
+
g_signal_connect(action, "activate", G_CALLBACK(sendCustom), widget);
|
|
231
|
+
g_action_map_add_action(G_ACTION_MAP(actions->custom), G_ACTION(action));
|
|
232
|
+
g_object_unref(action);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
bool announceOn(GtkWidget *widget, const Announcement &announcement) {
|
|
238
|
+
using Announce = void (*)(GtkAccessible *, const char *, int);
|
|
239
|
+
static auto announce = reinterpret_cast<Announce>(dlsym(RTLD_DEFAULT, "gtk_accessible_announce"));
|
|
240
|
+
if (!announce || !widget || announcement.text.empty()) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
// GtkAccessibleAnnouncementPriority: LOW, MEDIUM, HIGH, one to one.
|
|
244
|
+
int priority = announcement.priority == AnnouncementPriority::Low ? 0 : announcement.priority == AnnouncementPriority::Medium ? 1 : 2;
|
|
245
|
+
announce(GTK_ACCESSIBLE(widget), announcement.text.c_str(), priority);
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
void applyAccessibility(GtkWidget *widget, const AxInfo &info) {
|
|
250
|
+
GtkAccessible *accessible = GTK_ACCESSIBLE(widget);
|
|
251
|
+
auto text = [accessible](GtkAccessibleProperty property, const std::string &value) {
|
|
252
|
+
if (value.empty()) {
|
|
253
|
+
gtk_accessible_reset_property(accessible, property);
|
|
254
|
+
} else {
|
|
255
|
+
gtk_accessible_update_property(accessible, property, value.c_str(), -1);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
// Static text is named by what it says, which the core reads as its value (as VoiceOver reads a
|
|
259
|
+
// text's): AT-SPI's name is what Orca speaks, and a label has no value.
|
|
260
|
+
bool namedByValue = info.role == AxRole::StaticText && info.label.empty();
|
|
261
|
+
text(GTK_ACCESSIBLE_PROPERTY_LABEL, namedByValue ? info.value : info.label);
|
|
262
|
+
text(GTK_ACCESSIBLE_PROPERTY_DESCRIPTION, info.hint);
|
|
263
|
+
text(GTK_ACCESSIBLE_PROPERTY_VALUE_TEXT, namedByValue ? std::string() : info.value);
|
|
264
|
+
text(GTK_ACCESSIBLE_PROPERTY_ROLE_DESCRIPTION, info.roleDescription);
|
|
265
|
+
if (info.level) {
|
|
266
|
+
gtk_accessible_update_property(accessible, GTK_ACCESSIBLE_PROPERTY_LEVEL, *info.level, -1);
|
|
267
|
+
} else {
|
|
268
|
+
gtk_accessible_reset_property(accessible, GTK_ACCESSIBLE_PROPERTY_LEVEL);
|
|
269
|
+
}
|
|
270
|
+
gtk_accessible_update_state(accessible, GTK_ACCESSIBLE_STATE_DISABLED, info.disabled, GTK_ACCESSIBLE_STATE_SELECTED, info.selected, -1);
|
|
271
|
+
if (info.checked) {
|
|
272
|
+
auto state = *info.checked == 2 ? GTK_ACCESSIBLE_TRISTATE_MIXED : *info.checked == 1 ? GTK_ACCESSIBLE_TRISTATE_TRUE : GTK_ACCESSIBLE_TRISTATE_FALSE;
|
|
273
|
+
gtk_accessible_update_state(accessible, GTK_ACCESSIBLE_STATE_CHECKED, state, -1);
|
|
274
|
+
} else {
|
|
275
|
+
gtk_accessible_reset_state(accessible, GTK_ACCESSIBLE_STATE_CHECKED);
|
|
276
|
+
}
|
|
277
|
+
if (info.expanded) {
|
|
278
|
+
gtk_accessible_update_state(accessible, GTK_ACCESSIBLE_STATE_EXPANDED, *info.expanded ? TRUE : FALSE, -1);
|
|
279
|
+
} else {
|
|
280
|
+
gtk_accessible_reset_state(accessible, GTK_ACCESSIBLE_STATE_EXPANDED);
|
|
281
|
+
}
|
|
282
|
+
if (info.required) {
|
|
283
|
+
gtk_accessible_update_property(accessible, GTK_ACCESSIBLE_PROPERTY_REQUIRED, TRUE, -1);
|
|
284
|
+
} else {
|
|
285
|
+
gtk_accessible_reset_property(accessible, GTK_ACCESSIBLE_PROPERTY_REQUIRED);
|
|
286
|
+
}
|
|
287
|
+
if (info.invalid) {
|
|
288
|
+
gtk_accessible_update_state(accessible, GTK_ACCESSIBLE_STATE_INVALID, GTK_ACCESSIBLE_INVALID_TRUE, -1);
|
|
289
|
+
} else {
|
|
290
|
+
gtk_accessible_reset_state(accessible, GTK_ACCESSIBLE_STATE_INVALID);
|
|
291
|
+
}
|
|
292
|
+
// GTK has an item's place in its set and a grid's counts as integer relations.
|
|
293
|
+
auto count = [accessible](GtkAccessibleRelation relation, std::optional<int> value) {
|
|
294
|
+
if (value) {
|
|
295
|
+
gtk_accessible_update_relation(accessible, relation, *value, -1);
|
|
296
|
+
} else {
|
|
297
|
+
gtk_accessible_reset_relation(accessible, relation);
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
count(GTK_ACCESSIBLE_RELATION_POS_IN_SET, info.positionInSet);
|
|
301
|
+
count(GTK_ACCESSIBLE_RELATION_SET_SIZE, info.setSize);
|
|
302
|
+
count(GTK_ACCESSIBLE_RELATION_ROW_COUNT, info.rowCount);
|
|
303
|
+
count(GTK_ACCESSIBLE_RELATION_COL_COUNT, info.columnCount);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
} // namespace ngn::gtk
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// A view's accessibility through GTK's own: the core's ARIA role for it (aria-role.h) as the
|
|
2
|
+
// widget's accessible role, set as GTK makes the widget, and the core's AxInfo (accessibility.h)
|
|
3
|
+
// as its label, description, level and states, updated as the props change. GTK then exposes the
|
|
4
|
+
// widget over AT-SPI.
|
|
5
|
+
//
|
|
6
|
+
// GTK fixes a widget's role when it makes it, so the backend replaces the widget of a view whose
|
|
7
|
+
// role changes (GtkBackend). Orca's structural navigation (headings and landmarks) needs AT-SPI
|
|
8
|
+
// Collection, which GTK does not implement, so it finds nothing in a GTK app: a documented gap.
|
|
9
|
+
#pragma once
|
|
10
|
+
|
|
11
|
+
#include "ngn/accessibility.h"
|
|
12
|
+
#include "ngn/aria-role.h"
|
|
13
|
+
#include "ngn/system.h"
|
|
14
|
+
|
|
15
|
+
#include <react/renderer/mounting/ShadowView.h>
|
|
16
|
+
|
|
17
|
+
#include <gtk/gtk.h>
|
|
18
|
+
|
|
19
|
+
#include <functional>
|
|
20
|
+
#include <string>
|
|
21
|
+
#include <utility>
|
|
22
|
+
#include <vector>
|
|
23
|
+
|
|
24
|
+
namespace ngn::gtk {
|
|
25
|
+
|
|
26
|
+
struct ViewData;
|
|
27
|
+
|
|
28
|
+
// The view's accessibility as the core reads it: a Paragraph's from its text, any other's from its
|
|
29
|
+
// props. Nothing for a view without View props.
|
|
30
|
+
std::optional<AxInfo> accessibilityOf(const facebook::react::ShadowView &view);
|
|
31
|
+
|
|
32
|
+
// GTK's role for the view: its ARIA role, `generic` for one that is no element of its own (read
|
|
33
|
+
// through), has no View props, or holds the element (a native control, a text input's field).
|
|
34
|
+
GtkAccessibleRole accessibleRole(const facebook::react::ShadowView &view);
|
|
35
|
+
|
|
36
|
+
// GTK's name for an ARIA role.
|
|
37
|
+
GtkAccessibleRole gtkRole(AriaRole role);
|
|
38
|
+
|
|
39
|
+
// Sets what the widget says beyond its role, and resets what `info` no longer has.
|
|
40
|
+
void applyAccessibility(GtkWidget *widget, const AxInfo &info);
|
|
41
|
+
|
|
42
|
+
// The widget that carries the view's label, description and states: a text input's editor (the
|
|
43
|
+
// GtkText, or a multiline field's GtkTextView, which is a text box where its scrolled window is a
|
|
44
|
+
// group), else the view's own.
|
|
45
|
+
GtkWidget *accessibleWidget(const ViewData &view);
|
|
46
|
+
|
|
47
|
+
// Where a view's accessible actions go: `perform` sends a press, increment, decrement or cancel,
|
|
48
|
+
// and `send` a declared action by the name the app gave it.
|
|
49
|
+
struct AccessibleActionSinks {
|
|
50
|
+
std::function<bool(GtkWidget *, AxVerb)> perform;
|
|
51
|
+
std::function<bool(GtkWidget *, const std::string &)> send;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// The view's press, increment, decrement and cancel as the widget's `accessibility` actions, which
|
|
55
|
+
// GTK lists on AT-SPI's Action interface for a screen reader to invoke, each enabled while `info`
|
|
56
|
+
// offers it, as AppKit's perform methods are; and its declared actions (`accessibilityActions`) as
|
|
57
|
+
// its `custom` actions, as AppKit lists custom actions. GTK gives AT-SPI an action's name and no
|
|
58
|
+
// label, so a screen reader reads `custom.<name>`. A native control and a text input's field are
|
|
59
|
+
// GTK's own widgets with GTK's own actions, and get none.
|
|
60
|
+
void applyAccessibleActions(GtkWidget *widget, const AxInfo &info, AccessibleActionSinks sinks);
|
|
61
|
+
|
|
62
|
+
// The declared actions' GAction names, with the names the app declared: a GAction name takes only
|
|
63
|
+
// letters, digits, '-' and '.', so each other character becomes '-', and a name already taken gets
|
|
64
|
+
// a number ("delete-2").
|
|
65
|
+
std::vector<std::pair<std::string, std::string>> customActionNames(const std::vector<AxAction> &declared);
|
|
66
|
+
|
|
67
|
+
// The widget that is the view's end of a relation, labelled-by and described-by: the native
|
|
68
|
+
// control that fills it, which assistive technology operates, else accessibleWidget.
|
|
69
|
+
GtkWidget *relationWidget(const ViewData &view);
|
|
70
|
+
|
|
71
|
+
// Says `announcement` through `widget`'s window, at its priority, with gtk_accessible_announce,
|
|
72
|
+
// found at run time: GTK 4.14 and later. Below it nothing is said. Whether it was.
|
|
73
|
+
bool announceOn(GtkWidget *widget, const Announcement &announcement);
|
|
74
|
+
|
|
75
|
+
} // namespace ngn::gtk
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
#include "alerts.h"
|
|
2
|
+
|
|
3
|
+
#include "screens.h"
|
|
4
|
+
|
|
5
|
+
#include <adwaita.h>
|
|
6
|
+
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
namespace ngn::gtk {
|
|
11
|
+
|
|
12
|
+
namespace {
|
|
13
|
+
|
|
14
|
+
// What an alert answers with, freed with its dialog.
|
|
15
|
+
struct Pending {
|
|
16
|
+
std::function<void(int, std::string, std::string)> done;
|
|
17
|
+
int escape{-1};
|
|
18
|
+
GtkWidget *field{nullptr};
|
|
19
|
+
GtkWidget *password{nullptr};
|
|
20
|
+
bool answered{false};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
GtkWidget *gLast = nullptr;
|
|
24
|
+
|
|
25
|
+
// libadwaita 1.5's AdwAlertDialog, whose functions take the same arguments as AdwMessageDialog's.
|
|
26
|
+
struct AlertDialogApi {
|
|
27
|
+
GtkWidget *(*make)(const char *, const char *);
|
|
28
|
+
void (*addResponse)(GtkWidget *, const char *, const char *);
|
|
29
|
+
void (*setAppearance)(GtkWidget *, const char *, AdwResponseAppearance);
|
|
30
|
+
void (*setDefault)(GtkWidget *, const char *);
|
|
31
|
+
void (*setClose)(GtkWidget *, const char *);
|
|
32
|
+
void (*setExtra)(GtkWidget *, GtkWidget *);
|
|
33
|
+
void (*present)(GtkWidget *, GtkWidget *);
|
|
34
|
+
void (*close)(GtkWidget *);
|
|
35
|
+
bool available() const {
|
|
36
|
+
return make && addResponse && setAppearance && setDefault && setClose && setExtra && present && close;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const AlertDialogApi &alertDialogApi() {
|
|
41
|
+
static AlertDialogApi api{
|
|
42
|
+
adwFunction<GtkWidget *(*)(const char *, const char *)>("adw_alert_dialog_new"),
|
|
43
|
+
adwFunction<void (*)(GtkWidget *, const char *, const char *)>("adw_alert_dialog_add_response"),
|
|
44
|
+
adwFunction<void (*)(GtkWidget *, const char *, AdwResponseAppearance)>("adw_alert_dialog_set_response_appearance"),
|
|
45
|
+
adwFunction<void (*)(GtkWidget *, const char *)>("adw_alert_dialog_set_default_response"),
|
|
46
|
+
adwFunction<void (*)(GtkWidget *, const char *)>("adw_alert_dialog_set_close_response"),
|
|
47
|
+
adwFunction<void (*)(GtkWidget *, GtkWidget *)>("adw_alert_dialog_set_extra_child"),
|
|
48
|
+
adwFunction<void (*)(GtkWidget *, GtkWidget *)>("adw_dialog_present"),
|
|
49
|
+
adwFunction<void (*)(GtkWidget *)>("adw_dialog_force_close"),
|
|
50
|
+
};
|
|
51
|
+
return api;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// The prompt's fields: a text or password entry, or a login above a password.
|
|
55
|
+
GtkWidget *fieldsFor(const AlertRequest &request, Pending &pending) {
|
|
56
|
+
auto entry = [&](bool secure, const char *placeholder) {
|
|
57
|
+
GtkWidget *e = secure ? gtk_password_entry_new() : gtk_entry_new();
|
|
58
|
+
g_object_set(e, "activates-default", TRUE, nullptr);
|
|
59
|
+
if (placeholder) {
|
|
60
|
+
g_object_set(e, "placeholder-text", placeholder, nullptr);
|
|
61
|
+
}
|
|
62
|
+
return e;
|
|
63
|
+
};
|
|
64
|
+
switch (request.input) {
|
|
65
|
+
case AlertInput::None:
|
|
66
|
+
return nullptr;
|
|
67
|
+
case AlertInput::PlainText:
|
|
68
|
+
case AlertInput::SecureText:
|
|
69
|
+
pending.field = entry(request.input == AlertInput::SecureText, nullptr);
|
|
70
|
+
gtk_editable_set_text(GTK_EDITABLE(pending.field), request.defaultValue.c_str());
|
|
71
|
+
return pending.field;
|
|
72
|
+
case AlertInput::LoginPassword: {
|
|
73
|
+
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
|
|
74
|
+
pending.field = entry(false, "Login");
|
|
75
|
+
gtk_editable_set_text(GTK_EDITABLE(pending.field), request.defaultValue.c_str());
|
|
76
|
+
pending.password = entry(true, "Password");
|
|
77
|
+
gtk_box_append(GTK_BOX(box), pending.field);
|
|
78
|
+
gtk_box_append(GTK_BOX(box), pending.password);
|
|
79
|
+
return box;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return nullptr;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
void responseButtons(GtkWidget *root, GtkWidget *skip, std::vector<GtkWidget *> &out) {
|
|
86
|
+
for (GtkWidget *child = gtk_widget_get_first_child(root); child; child = gtk_widget_get_next_sibling(child)) {
|
|
87
|
+
if (child == skip) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (GTK_IS_BUTTON(child)) {
|
|
91
|
+
out.push_back(child);
|
|
92
|
+
} else {
|
|
93
|
+
responseButtons(child, skip, out);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// More than three buttons stack, one above another, as libadwaita stacks them when they do not fit
|
|
99
|
+
// side by side: each asks for a width that four never fit in, even in a wide dialog. The responses
|
|
100
|
+
// are the dialog's buttons outside `extra`; with any other count found, libadwaita's own layout
|
|
101
|
+
// stands.
|
|
102
|
+
void stackResponses(GtkWidget *dialog, GtkWidget *extra, size_t count) {
|
|
103
|
+
constexpr int kStackedWidth = 200;
|
|
104
|
+
std::vector<GtkWidget *> buttons;
|
|
105
|
+
responseButtons(dialog, extra, buttons);
|
|
106
|
+
if (count <= 3 || buttons.size() != count) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
for (GtkWidget *button : buttons) {
|
|
110
|
+
gtk_widget_set_size_request(button, kStackedWidth, -1);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
std::string textOf(GtkWidget *field) {
|
|
115
|
+
return field ? gtk_editable_get_text(GTK_EDITABLE(field)) : "";
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
void answer(GtkWidget *dialog, const char *response) {
|
|
119
|
+
auto *pending = static_cast<Pending *>(g_object_get_data(G_OBJECT(dialog), "ngn-alert"));
|
|
120
|
+
if (!pending || pending->answered) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
pending->answered = true;
|
|
124
|
+
std::string id = response ? response : "";
|
|
125
|
+
int index = id.size() > 1 && id[0] == 'b' ? std::stoi(id.substr(1)) : pending->escape;
|
|
126
|
+
pending->done(index, textOf(pending->field), textOf(pending->password));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// An alert that refuses Escape (`can-close` false, with no cancel button) refuses its own buttons'
|
|
130
|
+
// close too: libadwaita's AdwAlertDialog closes before it responds, and that close keeps the
|
|
131
|
+
// property. Once a button has answered, it is closed by force, as a pressed button closes it.
|
|
132
|
+
void closeRefused(GtkWidget *dialog) {
|
|
133
|
+
if (!g_object_class_find_property(G_OBJECT_GET_CLASS(dialog), "can-close")) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
gboolean canClose = TRUE;
|
|
137
|
+
g_object_get(dialog, "can-close", &canClose, nullptr);
|
|
138
|
+
if (!canClose) {
|
|
139
|
+
if (const auto &api = alertDialogApi(); api.close) {
|
|
140
|
+
api.close(dialog);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
} // namespace
|
|
146
|
+
|
|
147
|
+
GtkWindow *activeWindow() {
|
|
148
|
+
GListModel *windows = gtk_window_get_toplevels();
|
|
149
|
+
GtkWindow *visible = nullptr;
|
|
150
|
+
for (guint i = 0; i < g_list_model_get_n_items(windows); i++) {
|
|
151
|
+
auto *window = GTK_WINDOW(g_list_model_get_item(windows, i));
|
|
152
|
+
g_object_unref(window);
|
|
153
|
+
if (!gtk_widget_get_visible(GTK_WIDGET(window)) || g_object_get_data(G_OBJECT(window), "ngn-alert")) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (gtk_window_is_active(window)) {
|
|
157
|
+
return window;
|
|
158
|
+
}
|
|
159
|
+
visible = visible ? visible : window;
|
|
160
|
+
}
|
|
161
|
+
// No window manager says which is active (a headless session): the first one shown.
|
|
162
|
+
return visible;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
void showAlert(const AlertRequest &request, std::function<void(int, std::string, std::string)> done, GtkWindow *parent) {
|
|
166
|
+
auto layout = gnomeAlertLayout(request);
|
|
167
|
+
auto *pending = new Pending{.done = std::move(done), .escape = layout.escapeIndex};
|
|
168
|
+
GtkWidget *extra = fieldsFor(request, *pending);
|
|
169
|
+
const auto &api = alertDialogApi();
|
|
170
|
+
bool inWindow = parent && api.available();
|
|
171
|
+
GtkWidget *dialog = inWindow
|
|
172
|
+
? api.make(request.title.c_str(), request.message.c_str())
|
|
173
|
+
: adw_message_dialog_new(parent, request.title.c_str(), request.message.c_str());
|
|
174
|
+
auto addResponse = inWindow ? api.addResponse : [](GtkWidget *d, const char *id, const char *label) {
|
|
175
|
+
adw_message_dialog_add_response(ADW_MESSAGE_DIALOG(d), id, label);
|
|
176
|
+
};
|
|
177
|
+
auto setAppearance = inWindow ? api.setAppearance : [](GtkWidget *d, const char *id, AdwResponseAppearance a) {
|
|
178
|
+
adw_message_dialog_set_response_appearance(ADW_MESSAGE_DIALOG(d), id, a);
|
|
179
|
+
};
|
|
180
|
+
for (int index : layout.order) {
|
|
181
|
+
std::string id = "b" + std::to_string(index);
|
|
182
|
+
addResponse(dialog, id.c_str(), request.buttons[static_cast<size_t>(index)].label.c_str());
|
|
183
|
+
if (request.buttons[static_cast<size_t>(index)].role == AlertButtonRole::Destructive) {
|
|
184
|
+
setAppearance(dialog, id.c_str(), ADW_RESPONSE_DESTRUCTIVE);
|
|
185
|
+
} else if (index == layout.returnIndex) {
|
|
186
|
+
setAppearance(dialog, id.c_str(), ADW_RESPONSE_SUGGESTED);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
std::string returnId = layout.returnIndex >= 0 ? "b" + std::to_string(layout.returnIndex) : "";
|
|
190
|
+
// Escape answers the cancel button, or "close", which answers with no button.
|
|
191
|
+
std::string escapeId = layout.escapeIndex >= 0 ? "b" + std::to_string(layout.escapeIndex) : "close";
|
|
192
|
+
if (inWindow) {
|
|
193
|
+
api.setDefault(dialog, returnId.empty() ? nullptr : returnId.c_str());
|
|
194
|
+
api.setClose(dialog, escapeId.c_str());
|
|
195
|
+
// With no cancel button Escape presses nothing, as on macOS: closed with no answer, the app's
|
|
196
|
+
// Alert callback would never run, and a Dialogs promise never settle.
|
|
197
|
+
if (layout.escapeIndex < 0) {
|
|
198
|
+
g_object_set(dialog, "can-close", FALSE, nullptr);
|
|
199
|
+
}
|
|
200
|
+
if (extra) {
|
|
201
|
+
api.setExtra(dialog, extra);
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
adw_message_dialog_set_default_response(ADW_MESSAGE_DIALOG(dialog), returnId.empty() ? nullptr : returnId.c_str());
|
|
205
|
+
adw_message_dialog_set_close_response(ADW_MESSAGE_DIALOG(dialog), escapeId.c_str());
|
|
206
|
+
if (extra) {
|
|
207
|
+
adw_message_dialog_set_extra_child(ADW_MESSAGE_DIALOG(dialog), extra);
|
|
208
|
+
}
|
|
209
|
+
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
|
|
210
|
+
}
|
|
211
|
+
g_object_set_data_full(G_OBJECT(dialog), "ngn-alert", pending, [](gpointer p) { delete static_cast<Pending *>(p); });
|
|
212
|
+
g_signal_connect(dialog, "response", G_CALLBACK(+[](GtkWidget *d, const char *response, gpointer) {
|
|
213
|
+
answer(d, response);
|
|
214
|
+
closeRefused(d);
|
|
215
|
+
}), nullptr);
|
|
216
|
+
// The answer arrives however it goes, even closed with no response. Over a window, after the
|
|
217
|
+
// closing call returns: libadwaita's AdwAlertDialog closes before it emits a clicked button's
|
|
218
|
+
// response, and with no close animation (animations off, as with reduced motion) "closed" comes
|
|
219
|
+
// first, so answering at once would answer the cancel button for a click on any other.
|
|
220
|
+
if (inWindow) {
|
|
221
|
+
g_signal_connect(dialog, "closed", G_CALLBACK(+[](GtkWidget *d, gpointer) {
|
|
222
|
+
if (gLast == d) {
|
|
223
|
+
gLast = nullptr;
|
|
224
|
+
}
|
|
225
|
+
g_idle_add_full(G_PRIORITY_HIGH, +[](gpointer data) -> gboolean {
|
|
226
|
+
answer(GTK_WIDGET(data), "close");
|
|
227
|
+
return G_SOURCE_REMOVE;
|
|
228
|
+
}, g_object_ref(d), g_object_unref);
|
|
229
|
+
}), nullptr);
|
|
230
|
+
} else {
|
|
231
|
+
// A message dialog responds before it goes, and lets go of its fields, which the answer
|
|
232
|
+
// reads, once it is destroyed: it is answered as it is.
|
|
233
|
+
g_signal_connect(dialog, "destroy", G_CALLBACK(+[](GtkWidget *d, gpointer) {
|
|
234
|
+
if (gLast == d) {
|
|
235
|
+
gLast = nullptr;
|
|
236
|
+
}
|
|
237
|
+
answer(d, "close");
|
|
238
|
+
}), nullptr);
|
|
239
|
+
}
|
|
240
|
+
gLast = dialog;
|
|
241
|
+
if (inWindow) {
|
|
242
|
+
api.present(dialog, GTK_WIDGET(parent));
|
|
243
|
+
} else {
|
|
244
|
+
gtk_window_present(GTK_WINDOW(dialog));
|
|
245
|
+
}
|
|
246
|
+
// After presenting: an AdwDialog keeps its content unparented until then (libadwaita 1.5), so its
|
|
247
|
+
// buttons are not reachable from the dialog before.
|
|
248
|
+
stackResponses(dialog, extra, layout.order.size());
|
|
249
|
+
if (pending->field) {
|
|
250
|
+
gtk_widget_grab_focus(pending->field);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
GtkWidget *shownAlert() {
|
|
255
|
+
return gLast;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
void respondToAlert(GtkWidget *alert, const char *response) {
|
|
259
|
+
g_signal_emit_by_name(alert, "response", response);
|
|
260
|
+
if (ADW_IS_MESSAGE_DIALOG(alert)) {
|
|
261
|
+
gtk_window_destroy(GTK_WINDOW(alert));
|
|
262
|
+
} else if (const auto &api = alertDialogApi(); api.close) {
|
|
263
|
+
api.close(alert);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
} // namespace ngn::gtk
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Alerts on GTK (System::showAlert, behind Alert.alert, Alert.prompt and Dialogs): an AdwAlertDialog
|
|
2
|
+
// in the active window on libadwaita 1.5 and later, found at run time since the build targets 1.4's
|
|
3
|
+
// API, and otherwise an AdwMessageDialog, a modal window of its own, which also shows when no window
|
|
4
|
+
// is active, so the answer always arrives. Both take the prompt's text fields. The buttons follow
|
|
5
|
+
// GNOME's order (gnomeAlertLayout): Cancel first and on Escape, the affirmative last and on Return
|
|
6
|
+
// unless destructive.
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "ngn/system.h"
|
|
10
|
+
|
|
11
|
+
#include <gtk/gtk.h>
|
|
12
|
+
|
|
13
|
+
#include <functional>
|
|
14
|
+
#include <string>
|
|
15
|
+
|
|
16
|
+
namespace ngn::gtk {
|
|
17
|
+
|
|
18
|
+
// Shows `request` over `parent`, or on its own with none; `done` gets the button's index (the
|
|
19
|
+
// request's), the text and the password. On the GTK thread.
|
|
20
|
+
void showAlert(const AlertRequest &request, std::function<void(int, std::string, std::string)> done, GtkWindow *parent);
|
|
21
|
+
|
|
22
|
+
// The application's active window, which an alert shows over; null for none.
|
|
23
|
+
GtkWindow *activeWindow();
|
|
24
|
+
|
|
25
|
+
// The alert showing last, for tests: the dialog, and how it answers a response id ("b<index>", or
|
|
26
|
+
// "close" for Escape). Null for none.
|
|
27
|
+
GtkWidget *shownAlert();
|
|
28
|
+
void respondToAlert(GtkWidget *alert, const char *response);
|
|
29
|
+
|
|
30
|
+
} // namespace ngn::gtk
|