@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,1461 @@
|
|
|
1
|
+
#include "screens.h"
|
|
2
|
+
|
|
3
|
+
#include "gtk-backend.h"
|
|
4
|
+
#include "menus.h"
|
|
5
|
+
#include "view-widget.h"
|
|
6
|
+
|
|
7
|
+
#include "ngn/screen-presentation.h"
|
|
8
|
+
#include "ngn/split-view.h"
|
|
9
|
+
|
|
10
|
+
#include <react/renderer/components/FBReactNativeSpec/EventEmitters.h>
|
|
11
|
+
#include <react/renderer/components/modal/ModalHostViewState.h>
|
|
12
|
+
#include <react/renderer/core/ConcreteState.h>
|
|
13
|
+
#include <react/renderer/core/RawEvent.h>
|
|
14
|
+
|
|
15
|
+
#include <adwaita.h>
|
|
16
|
+
#include <dlfcn.h>
|
|
17
|
+
|
|
18
|
+
#include <algorithm>
|
|
19
|
+
#include <cmath>
|
|
20
|
+
#include <cstdio>
|
|
21
|
+
#include <set>
|
|
22
|
+
|
|
23
|
+
using namespace facebook::react;
|
|
24
|
+
|
|
25
|
+
G_BEGIN_DECLS
|
|
26
|
+
#define NGN_TYPE_HOST_SLOT (ngn_host_slot_get_type())
|
|
27
|
+
G_DECLARE_FINAL_TYPE(NgnHostSlot, ngn_host_slot, NGN, HOST_SLOT, GtkWidget)
|
|
28
|
+
G_END_DECLS
|
|
29
|
+
|
|
30
|
+
struct _NgnHostSlot {
|
|
31
|
+
GtkWidget parent_instance;
|
|
32
|
+
int width;
|
|
33
|
+
int height;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
G_DEFINE_TYPE(NgnHostSlot, ngn_host_slot, GTK_TYPE_WIDGET)
|
|
37
|
+
|
|
38
|
+
namespace {
|
|
39
|
+
|
|
40
|
+
// The size set on the slot, else its view's frame.
|
|
41
|
+
std::pair<int, int> slotSize(NgnHostSlot *slot) {
|
|
42
|
+
if (slot->width >= 0) {
|
|
43
|
+
return {slot->width, slot->height};
|
|
44
|
+
}
|
|
45
|
+
GtkWidget *child = gtk_widget_get_first_child(GTK_WIDGET(slot));
|
|
46
|
+
auto *d = ngn::gtk::viewData(child);
|
|
47
|
+
if (!d) {
|
|
48
|
+
return {0, 0};
|
|
49
|
+
}
|
|
50
|
+
const auto &size = d->layout.frame.size;
|
|
51
|
+
return {static_cast<int>(std::ceil(size.width)), static_cast<int>(std::ceil(size.height))};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
void slotMeasure(GtkWidget *widget, GtkOrientation orientation, int, int *minimum, int *natural, int *, int *) {
|
|
55
|
+
auto [w, h] = slotSize(NGN_HOST_SLOT(widget));
|
|
56
|
+
*minimum = *natural = orientation == GTK_ORIENTATION_HORIZONTAL ? w : h;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
void slotAllocate(GtkWidget *widget, int width, int height, int) {
|
|
60
|
+
// The view a slot holds fills it; one replacing it (a role change) may be beside it for a moment.
|
|
61
|
+
for (GtkWidget *child = gtk_widget_get_first_child(widget); child; child = gtk_widget_get_next_sibling(child)) {
|
|
62
|
+
GtkAllocation all{0, 0, width, height};
|
|
63
|
+
gtk_widget_size_allocate(child, &all, -1);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
void slotDispose(GObject *object) {
|
|
68
|
+
while (GtkWidget *child = gtk_widget_get_first_child(GTK_WIDGET(object))) {
|
|
69
|
+
gtk_widget_unparent(child);
|
|
70
|
+
}
|
|
71
|
+
G_OBJECT_CLASS(ngn_host_slot_parent_class)->dispose(object);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const char *kChrome = "ngn-window-chrome";
|
|
75
|
+
|
|
76
|
+
// A header config's subviews still under it: those not in the header bar.
|
|
77
|
+
std::vector<ngn::HeaderSubview> subviewsOf(const ngn::gtk::ViewData *config) {
|
|
78
|
+
std::vector<ngn::HeaderSubview> subviews;
|
|
79
|
+
for (GtkWidget *c = config ? gtk_widget_get_first_child(config->widget) : nullptr; c; c = gtk_widget_get_next_sibling(c)) {
|
|
80
|
+
if (const auto *d = ngn::gtk::viewData(c); d && d->props) {
|
|
81
|
+
if (auto type = ngn::headerItemType(*d->props)) {
|
|
82
|
+
subviews.push_back({d->tag, *type});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return subviews;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The sheet's card and the dimmed backdrop of libadwaita 1.4's drawn sheet.
|
|
90
|
+
void installSheetStyle() {
|
|
91
|
+
static bool installed = false;
|
|
92
|
+
if (installed || !gdk_display_get_default()) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
installed = true;
|
|
96
|
+
GtkCssProvider *css = gtk_css_provider_new();
|
|
97
|
+
gtk_css_provider_load_from_data(
|
|
98
|
+
css,
|
|
99
|
+
".ngn-backdrop { background-color: alpha(black, 0.3); }"
|
|
100
|
+
".ngn-sheet { background-color: @window_bg_color; border-radius: 12px;"
|
|
101
|
+
" box-shadow: 0 2px 8px 2px alpha(black, 0.2), 0 0 0 1px alpha(black, 0.05); }",
|
|
102
|
+
-1);
|
|
103
|
+
gtk_style_context_add_provider_for_display(gdk_display_get_default(), GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
104
|
+
g_object_unref(css);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
} // namespace
|
|
108
|
+
|
|
109
|
+
namespace ngn::gtk {
|
|
110
|
+
|
|
111
|
+
std::string colorRule(const char *name, const char *property, float red, float green, float blue, float alpha) {
|
|
112
|
+
auto byte = [](float v) { return static_cast<unsigned>(std::lround(std::clamp(v, 0.0f, 1.0f) * 255)); };
|
|
113
|
+
// CSS wants a point, whatever the user's locale writes a decimal as.
|
|
114
|
+
char opacity[G_ASCII_DTOSTR_BUF_SIZE];
|
|
115
|
+
g_ascii_formatd(opacity, sizeof opacity, "%.3f", static_cast<double>(alpha));
|
|
116
|
+
char rule[200];
|
|
117
|
+
std::snprintf(rule, sizeof rule, ".%s { %s: rgba(%u, %u, %u, %s); }", name, property, byte(red), byte(green), byte(blue), opacity);
|
|
118
|
+
return rule;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
std::string colorClass(const char *property, const SharedColor &color) {
|
|
122
|
+
if (!color || !gdk_display_get_default()) {
|
|
123
|
+
return {};
|
|
124
|
+
}
|
|
125
|
+
auto c = colorComponentsFromColor(color);
|
|
126
|
+
auto byte = [](float v) { return static_cast<unsigned>(std::lround(std::clamp(v, 0.0f, 1.0f) * 255)); };
|
|
127
|
+
char name[48];
|
|
128
|
+
std::snprintf(
|
|
129
|
+
name, sizeof name, "ngn-%s-%02x%02x%02x%02x", std::string_view(property) == "color" ? "fg" : "bg", byte(c.red),
|
|
130
|
+
byte(c.green), byte(c.blue), byte(c.alpha));
|
|
131
|
+
static std::set<std::string> added;
|
|
132
|
+
if (added.insert(name).second) {
|
|
133
|
+
std::string rule = colorRule(name, property, c.red, c.green, c.blue, c.alpha);
|
|
134
|
+
GtkCssProvider *css = gtk_css_provider_new();
|
|
135
|
+
gtk_css_provider_load_from_data(css, rule.c_str(), -1);
|
|
136
|
+
gtk_style_context_add_provider_for_display(
|
|
137
|
+
gdk_display_get_default(), GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
138
|
+
g_object_unref(css);
|
|
139
|
+
}
|
|
140
|
+
return name;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
} // namespace ngn::gtk
|
|
144
|
+
|
|
145
|
+
namespace {
|
|
146
|
+
|
|
147
|
+
void addClass(GtkWidget *widget, const std::string &name) {
|
|
148
|
+
if (!name.empty()) {
|
|
149
|
+
gtk_widget_add_css_class(widget, name.c_str());
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
} // namespace
|
|
154
|
+
|
|
155
|
+
static void ngn_host_slot_class_init(NgnHostSlotClass *klass) {
|
|
156
|
+
GtkWidgetClass *widget = GTK_WIDGET_CLASS(klass);
|
|
157
|
+
widget->measure = slotMeasure;
|
|
158
|
+
widget->size_allocate = slotAllocate;
|
|
159
|
+
gtk_widget_class_set_css_name(widget, "ngn-slot");
|
|
160
|
+
G_OBJECT_CLASS(klass)->dispose = slotDispose;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static void ngn_host_slot_init(NgnHostSlot *self) {
|
|
164
|
+
self->width = -1;
|
|
165
|
+
self->height = -1;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
namespace ngn::gtk {
|
|
169
|
+
|
|
170
|
+
GtkWidget *wrapInChrome(GtkWidget *surface, const ChromeOptions &options) {
|
|
171
|
+
GtkWidget *toolbar = adw_toolbar_view_new();
|
|
172
|
+
GtkWidget *header = adw_header_bar_new();
|
|
173
|
+
GtkWidget *overlay = gtk_overlay_new();
|
|
174
|
+
gtk_overlay_set_child(GTK_OVERLAY(overlay), surface);
|
|
175
|
+
adw_toolbar_view_add_top_bar(ADW_TOOLBAR_VIEW(toolbar), header);
|
|
176
|
+
adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(toolbar), overlay);
|
|
177
|
+
auto *chrome = new WindowChrome{.toolbarView = toolbar, .headerBar = header, .overlay = overlay, .titleBar = options.titleBar};
|
|
178
|
+
if (options.menus == MenuPlacement::MenuBar) {
|
|
179
|
+
chrome->menuBar = gtk_popover_menu_bar_new_from_model(nullptr);
|
|
180
|
+
gtk_widget_set_visible(chrome->menuBar, FALSE);
|
|
181
|
+
adw_toolbar_view_add_top_bar(ADW_TOOLBAR_VIEW(toolbar), chrome->menuBar);
|
|
182
|
+
} else {
|
|
183
|
+
chrome->primaryMenu = gtk_menu_button_new();
|
|
184
|
+
gtk_menu_button_set_icon_name(GTK_MENU_BUTTON(chrome->primaryMenu), "open-menu-symbolic");
|
|
185
|
+
gtk_menu_button_set_primary(GTK_MENU_BUTTON(chrome->primaryMenu), TRUE);
|
|
186
|
+
gtk_widget_set_tooltip_text(chrome->primaryMenu, "Main Menu");
|
|
187
|
+
gtk_widget_set_visible(chrome->primaryMenu, FALSE);
|
|
188
|
+
adw_header_bar_pack_end(ADW_HEADER_BAR(header), chrome->primaryMenu);
|
|
189
|
+
}
|
|
190
|
+
if (runsUnderHeaderBar(*chrome)) {
|
|
191
|
+
// A flat bar over the content, which shows through it.
|
|
192
|
+
adw_toolbar_view_set_top_bar_style(ADW_TOOLBAR_VIEW(toolbar), ADW_TOOLBAR_FLAT);
|
|
193
|
+
adw_toolbar_view_set_extend_content_to_top_edge(ADW_TOOLBAR_VIEW(toolbar), TRUE);
|
|
194
|
+
adw_header_bar_set_show_title(ADW_HEADER_BAR(header), options.titleBar != TitleBar::Hidden);
|
|
195
|
+
} else {
|
|
196
|
+
// The bar above the content, with its edge, as the content scrolls under nothing.
|
|
197
|
+
adw_toolbar_view_set_top_bar_style(ADW_TOOLBAR_VIEW(toolbar), ADW_TOOLBAR_RAISED);
|
|
198
|
+
}
|
|
199
|
+
g_object_set_data_full(G_OBJECT(surface), kChrome, chrome, [](gpointer p) { delete static_cast<WindowChrome *>(p); });
|
|
200
|
+
return toolbar;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const WindowChrome *chromeOf(GtkWidget *surface) {
|
|
204
|
+
return surface ? static_cast<const WindowChrome *>(g_object_get_data(G_OBJECT(surface), kChrome)) : nullptr;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
bool runsUnderHeaderBar(const WindowChrome &chrome) {
|
|
208
|
+
return chrome.titleBar != TitleBar::Default;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
WindowChrome::~WindowChrome() {
|
|
212
|
+
for (const Held &h : held) {
|
|
213
|
+
g_object_unref(h.widget);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
namespace {
|
|
218
|
+
|
|
219
|
+
bool holdsWindowControls(GtkWidget *widget) {
|
|
220
|
+
if (GTK_IS_WINDOW_CONTROLS(widget)) {
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
for (GtkWidget *child = gtk_widget_get_first_child(widget); child; child = gtk_widget_get_next_sibling(child)) {
|
|
224
|
+
if (holdsWindowControls(child)) {
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
void holdWidget(WindowChrome &chrome, GtkWidget *widget, bool target, bool focus, bool sensitive = true) {
|
|
232
|
+
for (const auto &h : chrome.held) {
|
|
233
|
+
if (h.widget == widget) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
bool primary = GTK_IS_MENU_BUTTON(widget) && gtk_menu_button_get_primary(GTK_MENU_BUTTON(widget)) != FALSE;
|
|
238
|
+
chrome.held.push_back({GTK_WIDGET(g_object_ref(widget)), gtk_widget_get_can_target(widget) != FALSE,
|
|
239
|
+
gtk_widget_get_can_focus(widget) != FALSE, gtk_widget_get_sensitive(widget) != FALSE, primary});
|
|
240
|
+
if (!target) {
|
|
241
|
+
gtk_widget_set_can_target(widget, FALSE);
|
|
242
|
+
}
|
|
243
|
+
if (!focus) {
|
|
244
|
+
gtk_widget_set_can_focus(widget, FALSE);
|
|
245
|
+
}
|
|
246
|
+
if (!sensitive) {
|
|
247
|
+
gtk_widget_set_sensitive(widget, FALSE);
|
|
248
|
+
}
|
|
249
|
+
if (primary) {
|
|
250
|
+
// F10 opens a primary menu from anywhere in the window.
|
|
251
|
+
gtk_menu_button_set_primary(GTK_MENU_BUTTON(widget), FALSE);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// A header bar's items, its title among them, and not its window controls: the window still
|
|
256
|
+
// moves, closes and resizes from them.
|
|
257
|
+
void holdHeaderBar(WindowChrome &chrome, GtkWidget *widget) {
|
|
258
|
+
for (GtkWidget *child = gtk_widget_get_first_child(widget); child; child = gtk_widget_get_next_sibling(child)) {
|
|
259
|
+
if (GTK_IS_WINDOW_CONTROLS(child)) {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (holdsWindowControls(child)) {
|
|
263
|
+
holdHeaderBar(chrome, child);
|
|
264
|
+
} else {
|
|
265
|
+
holdWidget(chrome, child, false, false);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// The header bars in `widget`'s subtree outside `front`: the window's, wherever a split view put
|
|
271
|
+
// it, and a full-height sidebar's.
|
|
272
|
+
void findHeaderBars(GtkWidget *widget, GtkWidget *front, std::vector<GtkWidget *> &found) {
|
|
273
|
+
for (GtkWidget *child = gtk_widget_get_first_child(widget); child; child = gtk_widget_get_next_sibling(child)) {
|
|
274
|
+
if (child == front) {
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (ADW_IS_HEADER_BAR(child)) {
|
|
278
|
+
found.push_back(child);
|
|
279
|
+
} else {
|
|
280
|
+
findHeaderBars(child, front, found);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
} // namespace
|
|
286
|
+
|
|
287
|
+
void holdChrome(GtkWidget *surface, GtkWidget *front) {
|
|
288
|
+
auto *chrome = surface ? static_cast<WindowChrome *>(g_object_get_data(G_OBJECT(surface), kChrome)) : nullptr;
|
|
289
|
+
if (!chrome) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
// What was held is given back first: a header laid out again has items of its own.
|
|
293
|
+
for (const auto &h : chrome->held) {
|
|
294
|
+
gtk_widget_set_can_target(h.widget, h.target);
|
|
295
|
+
gtk_widget_set_can_focus(h.widget, h.focus);
|
|
296
|
+
gtk_widget_set_sensitive(h.widget, h.sensitive);
|
|
297
|
+
if (h.primary) {
|
|
298
|
+
gtk_menu_button_set_primary(GTK_MENU_BUTTON(h.widget), TRUE);
|
|
299
|
+
}
|
|
300
|
+
g_object_unref(h.widget);
|
|
301
|
+
}
|
|
302
|
+
chrome->held.clear();
|
|
303
|
+
if (!front) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
// The layer takes the pointer over the content already: the content keeps its own, so the window
|
|
307
|
+
// controls of a header bar a split view moved into it still work.
|
|
308
|
+
GtkWidget *content = gtk_overlay_get_child(GTK_OVERLAY(chrome->overlay));
|
|
309
|
+
for (GtkWidget *child = gtk_widget_get_first_child(chrome->overlay); child; child = gtk_widget_get_next_sibling(child)) {
|
|
310
|
+
if (child != front) {
|
|
311
|
+
holdWidget(*chrome, child, child == content, false);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
std::vector<GtkWidget *> bars;
|
|
315
|
+
findHeaderBars(chrome->toolbarView, front, bars);
|
|
316
|
+
for (GtkWidget *bar : bars) {
|
|
317
|
+
holdHeaderBar(*chrome, bar);
|
|
318
|
+
}
|
|
319
|
+
if (chrome->menuBar) {
|
|
320
|
+
// F10 opens the menu bar from anywhere in the window, so it is insensitive.
|
|
321
|
+
holdWidget(*chrome, chrome->menuBar, true, true, false);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
namespace {
|
|
326
|
+
|
|
327
|
+
// The window controls in `widget`'s subtree that show a button: the start side's and the end's,
|
|
328
|
+
// as gtk-decoration-layout splits them at its colon.
|
|
329
|
+
void findWindowControls(GtkWidget *widget, std::vector<GtkWidget *> &found) {
|
|
330
|
+
for (GtkWidget *child = gtk_widget_get_first_child(widget); child; child = gtk_widget_get_next_sibling(child)) {
|
|
331
|
+
if (GTK_IS_WINDOW_CONTROLS(child)) {
|
|
332
|
+
if (!gtk_window_controls_get_empty(GTK_WINDOW_CONTROLS(child)) && gtk_widget_get_visible(child)) {
|
|
333
|
+
found.push_back(child);
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
findWindowControls(child, found);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
} // namespace
|
|
342
|
+
|
|
343
|
+
ContainerInsets chromeInsets(GtkWidget *surface) {
|
|
344
|
+
ContainerInsets insets{.size = {static_cast<Float>(gtk_widget_get_width(surface)), static_cast<Float>(gtk_widget_get_height(surface))}};
|
|
345
|
+
const WindowChrome *chrome = chromeOf(surface);
|
|
346
|
+
if (!chrome || !runsUnderHeaderBar(*chrome)) {
|
|
347
|
+
return insets;
|
|
348
|
+
}
|
|
349
|
+
insets.insets.top = static_cast<Float>(adw_toolbar_view_get_top_bar_height(ADW_TOOLBAR_VIEW(chrome->toolbarView)));
|
|
350
|
+
bool rightToLeft = gtk_widget_get_direction(surface) == GTK_TEXT_DIR_RTL;
|
|
351
|
+
std::vector<GtkWidget *> controls;
|
|
352
|
+
findWindowControls(chrome->headerBar, controls);
|
|
353
|
+
for (GtkWidget *side : controls) {
|
|
354
|
+
graphene_rect_t bounds;
|
|
355
|
+
if (!gtk_widget_compute_bounds(side, surface, &bounds)) {
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
double minX = bounds.origin.x, maxX = bounds.origin.x + bounds.size.width;
|
|
359
|
+
if (gtk_window_controls_get_side(GTK_WINDOW_CONTROLS(side)) == GTK_PACK_START) {
|
|
360
|
+
insets.titlebarLeading = std::max(insets.titlebarLeading, titlebarLeading(minX, maxX, insets.size.width, rightToLeft));
|
|
361
|
+
} else {
|
|
362
|
+
insets.titlebarTrailing = std::max(insets.titlebarTrailing, titlebarTrailing(minX, maxX, insets.size.width, rightToLeft));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return insets;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
GtkWidget *makeHostSlot() {
|
|
369
|
+
return GTK_WIDGET(g_object_new(NGN_TYPE_HOST_SLOT, nullptr));
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
void setSlotSize(GtkWidget *slot, std::optional<std::pair<int, int>> size) {
|
|
373
|
+
auto *s = NGN_HOST_SLOT(slot);
|
|
374
|
+
s->width = size ? size->first : -1;
|
|
375
|
+
s->height = size ? size->second : -1;
|
|
376
|
+
gtk_widget_queue_resize(slot);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
bool haveAdwDialog() {
|
|
380
|
+
return g_type_from_name("AdwDialog") != 0 && adwFunction<void (*)(GtkWidget *, GtkWidget *)>("adw_dialog_present") &&
|
|
381
|
+
adwFunction<void (*)(GtkWidget *)>("adw_dialog_force_close");
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// --- Search fields ----------------------------------------------------------------------------
|
|
385
|
+
|
|
386
|
+
namespace {
|
|
387
|
+
|
|
388
|
+
// What a bar button, the back button and a dialog's close attempt reach when pressed.
|
|
389
|
+
using PressTarget = std::pair<GtkBackend *, std::pair<Tag, std::string>>;
|
|
390
|
+
struct ChevronTarget {
|
|
391
|
+
Headers *headers;
|
|
392
|
+
SurfaceId surfaceId;
|
|
393
|
+
NavigationDirection direction;
|
|
394
|
+
};
|
|
395
|
+
using CloseTarget = std::pair<Modals *, Tag>;
|
|
396
|
+
|
|
397
|
+
// The menu entries of a bar button's menu, as a GMenu whose actions are `group`'s: choosing one
|
|
398
|
+
// sends its menu id, as AppKit's menu does. An `on` entry is a stateful action, which GTK draws
|
|
399
|
+
// with a check.
|
|
400
|
+
void fillMenu(
|
|
401
|
+
GMenu *menu, const HeaderMenuItem &entry, GSimpleActionGroup *group, int &next, GtkBackend &backend, Tag config,
|
|
402
|
+
bool disabled = false) {
|
|
403
|
+
for (const auto &child : entry.items) {
|
|
404
|
+
if (child.inlineSection) {
|
|
405
|
+
GMenu *section = g_menu_new();
|
|
406
|
+
for (const auto &inner : child.items) {
|
|
407
|
+
HeaderMenuItem one;
|
|
408
|
+
one.items.push_back(inner);
|
|
409
|
+
fillMenu(section, one, group, next, backend, config, disabled);
|
|
410
|
+
}
|
|
411
|
+
g_menu_append_section(menu, nullptr, G_MENU_MODEL(section));
|
|
412
|
+
g_object_unref(section);
|
|
413
|
+
} else if (!child.items.empty() || child.menuId.empty()) {
|
|
414
|
+
GMenu *submenu = g_menu_new();
|
|
415
|
+
// A disabled submenu has no entry that can be chosen.
|
|
416
|
+
fillMenu(submenu, child, group, next, backend, config, disabled || child.disabled);
|
|
417
|
+
g_menu_append_submenu(menu, child.title.c_str(), G_MENU_MODEL(submenu));
|
|
418
|
+
g_object_unref(submenu);
|
|
419
|
+
} else {
|
|
420
|
+
std::string name = "item" + std::to_string(next++);
|
|
421
|
+
GSimpleAction *action = child.on ? g_simple_action_new_stateful(name.c_str(), nullptr, g_variant_new_boolean(TRUE))
|
|
422
|
+
: g_simple_action_new(name.c_str(), nullptr);
|
|
423
|
+
g_simple_action_set_enabled(action, !disabled && !child.disabled);
|
|
424
|
+
g_signal_connect_data(
|
|
425
|
+
action, "activate", G_CALLBACK(+[](GSimpleAction *, GVariant *, gpointer data) {
|
|
426
|
+
auto *p = static_cast<PressTarget *>(data);
|
|
427
|
+
if (const ViewData *d = p->first->data(p->second.first); d && d->emitter) {
|
|
428
|
+
d->emitter->dispatchEvent(
|
|
429
|
+
"pressHeaderBarButtonMenuItem", folly::dynamic::object("menuId", p->second.second),
|
|
430
|
+
RawEvent::Category::Discrete);
|
|
431
|
+
}
|
|
432
|
+
}),
|
|
433
|
+
new PressTarget(&backend, {config, child.menuId}),
|
|
434
|
+
[](gpointer data, GClosure *) { delete static_cast<PressTarget *>(data); }, GConnectFlags(0));
|
|
435
|
+
g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(action));
|
|
436
|
+
g_object_unref(action);
|
|
437
|
+
g_menu_append(menu, child.title.c_str(), ("ngn." + name).c_str());
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// A bar button with a menu: a GtkMenuButton opening it.
|
|
443
|
+
GtkWidget *menuButton(GtkBackend &backend, Tag config, const BarButton &button) {
|
|
444
|
+
GtkWidget *widget = gtk_menu_button_new();
|
|
445
|
+
gtk_menu_button_set_label(GTK_MENU_BUTTON(widget), (button.title.empty() ? button.accessibilityLabel : button.title).c_str());
|
|
446
|
+
GMenu *menu = g_menu_new();
|
|
447
|
+
GSimpleActionGroup *group = g_simple_action_group_new();
|
|
448
|
+
int next = 0;
|
|
449
|
+
fillMenu(menu, *button.menu, group, next, backend, config);
|
|
450
|
+
// Nested, as the window's menus are, so two submenus with one title each open their own.
|
|
451
|
+
gtk_menu_button_set_popover(GTK_MENU_BUTTON(widget), nestedPopoverMenu(G_MENU_MODEL(menu)));
|
|
452
|
+
gtk_widget_insert_action_group(widget, "ngn", G_ACTION_GROUP(group));
|
|
453
|
+
// GTK 4.10 cannot read an inserted group back; tests read it here.
|
|
454
|
+
g_object_set_data_full(G_OBJECT(widget), "ngn-menu-actions", g_object_ref(group), g_object_unref);
|
|
455
|
+
g_object_unref(menu);
|
|
456
|
+
g_object_unref(group);
|
|
457
|
+
return widget;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
struct SearchField {
|
|
461
|
+
GtkBackend *backend;
|
|
462
|
+
Tag tag;
|
|
463
|
+
// A change the host made, which JavaScript already knows of.
|
|
464
|
+
bool setting{false};
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
SearchField *searchFieldOf(GtkWidget *entry) {
|
|
468
|
+
return static_cast<SearchField *>(g_object_get_data(G_OBJECT(entry), "ngn-search"));
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
void emitSearch(GtkWidget *entry, SearchBarInput input) {
|
|
472
|
+
auto *f = searchFieldOf(entry);
|
|
473
|
+
const ViewData *d = f ? f->backend->data(f->tag) : nullptr;
|
|
474
|
+
if (!d || !d->emitter) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
const char *text = gtk_editable_get_text(GTK_EDITABLE(entry));
|
|
478
|
+
for (auto &event : searchBarEvents(input, text ? text : "")) {
|
|
479
|
+
d->emitter->dispatchEvent(event.name, std::move(event.payload), RawEvent::Category::Discrete);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
void setSearchText(GtkWidget *entry, const std::string &text) {
|
|
484
|
+
auto *f = searchFieldOf(entry);
|
|
485
|
+
f->setting = true;
|
|
486
|
+
gtk_editable_set_text(GTK_EDITABLE(entry), text.c_str());
|
|
487
|
+
f->setting = false;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
} // namespace
|
|
491
|
+
|
|
492
|
+
GtkWidget *makeSearchEntry(GtkBackend &backend, Tag tag) {
|
|
493
|
+
GtkWidget *entry = gtk_search_entry_new();
|
|
494
|
+
gtk_widget_set_size_request(entry, 220, -1);
|
|
495
|
+
g_object_set_data_full(G_OBJECT(entry), "ngn-search", new SearchField{&backend, tag}, [](gpointer p) {
|
|
496
|
+
delete static_cast<SearchField *>(p);
|
|
497
|
+
});
|
|
498
|
+
// Each change as it happens: `search-changed` comes after a delay, when a change the host made
|
|
499
|
+
// can no longer be told from the user's.
|
|
500
|
+
g_signal_connect(entry, "changed", G_CALLBACK(+[](GtkEditable *e, gpointer) {
|
|
501
|
+
if (!searchFieldOf(GTK_WIDGET(e))->setting) {
|
|
502
|
+
emitSearch(GTK_WIDGET(e), SearchBarInput::Typed);
|
|
503
|
+
}
|
|
504
|
+
}), nullptr);
|
|
505
|
+
g_signal_connect(entry, "activate", G_CALLBACK(+[](GtkSearchEntry *e, gpointer) { emitSearch(GTK_WIDGET(e), SearchBarInput::Submitted); }), nullptr);
|
|
506
|
+
// Escape is the Cancel button: the field empties first, as on iOS.
|
|
507
|
+
g_signal_connect(entry, "stop-search", G_CALLBACK(+[](GtkSearchEntry *e, gpointer) {
|
|
508
|
+
setSearchText(GTK_WIDGET(e), "");
|
|
509
|
+
emitSearch(GTK_WIDGET(e), SearchBarInput::Cancelled);
|
|
510
|
+
}), nullptr);
|
|
511
|
+
GtkEventController *focus = gtk_event_controller_focus_new();
|
|
512
|
+
g_signal_connect(focus, "enter", G_CALLBACK(+[](GtkEventControllerFocus *c, gpointer) {
|
|
513
|
+
emitSearch(gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(c)), SearchBarInput::Began);
|
|
514
|
+
}), nullptr);
|
|
515
|
+
g_signal_connect(focus, "leave", G_CALLBACK(+[](GtkEventControllerFocus *c, gpointer) {
|
|
516
|
+
emitSearch(gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(c)), SearchBarInput::Ended);
|
|
517
|
+
}), nullptr);
|
|
518
|
+
gtk_widget_add_controller(entry, focus);
|
|
519
|
+
return entry;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
void applySearchBar(GtkWidget *entry, const Props &props) {
|
|
523
|
+
if (auto config = searchBarConfig(props)) {
|
|
524
|
+
gtk_search_entry_set_placeholder_text(GTK_SEARCH_ENTRY(entry), config->placeholder.c_str());
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
void runSearchBarCommand(GtkWidget *entry, const std::string &name, const folly::dynamic &args) {
|
|
529
|
+
auto command = searchBarCommand(name, args);
|
|
530
|
+
if (!command) {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
switch (command->kind) {
|
|
534
|
+
case SearchBarCommand::Focus:
|
|
535
|
+
gtk_widget_grab_focus(entry);
|
|
536
|
+
break;
|
|
537
|
+
case SearchBarCommand::Cancel:
|
|
538
|
+
setSearchText(entry, "");
|
|
539
|
+
[[fallthrough]];
|
|
540
|
+
case SearchBarCommand::Blur:
|
|
541
|
+
if (GtkRoot *root = gtk_widget_get_root(entry); root && gtk_root_get_focus(root) &&
|
|
542
|
+
(gtk_root_get_focus(root) == entry || gtk_widget_is_ancestor(gtk_root_get_focus(root), entry))) {
|
|
543
|
+
gtk_root_set_focus(root, nullptr);
|
|
544
|
+
}
|
|
545
|
+
break;
|
|
546
|
+
case SearchBarCommand::ClearText:
|
|
547
|
+
setSearchText(entry, "");
|
|
548
|
+
break;
|
|
549
|
+
case SearchBarCommand::SetText:
|
|
550
|
+
setSearchText(entry, command->text);
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// --- Headers ----------------------------------------------------------------------------------
|
|
556
|
+
|
|
557
|
+
Headers::~Headers() {
|
|
558
|
+
for (auto &[surfaceId, shown] : shown_) {
|
|
559
|
+
clear(surfaceId, shown);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
GtkWidget *Headers::headerBarOf(SurfaceId surfaceId) const {
|
|
564
|
+
const auto *chrome = chromeOf(backend_.surfaceWidget(surfaceId));
|
|
565
|
+
return chrome ? chrome->headerBar : nullptr;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
GtkWidget *Headers::backButton(SurfaceId surfaceId) const {
|
|
569
|
+
auto it = shown_.find(surfaceId);
|
|
570
|
+
return it == shown_.end() ? nullptr : it->second.back;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
GtkWidget *Headers::forwardButton(SurfaceId surfaceId) const {
|
|
574
|
+
auto it = shown_.find(surfaceId);
|
|
575
|
+
return it == shown_.end() ? nullptr : it->second.forward;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
void Headers::setHistory(SurfaceId surfaceId, const std::optional<WindowHistory> &history) {
|
|
579
|
+
auto &shown = shown_[surfaceId];
|
|
580
|
+
if (shown.history == history) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
shown.history = history;
|
|
584
|
+
layOut(surfaceId, shown);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
void Headers::pressChevron(SurfaceId surfaceId, NavigationDirection direction) {
|
|
588
|
+
auto it = shown_.find(surfaceId);
|
|
589
|
+
if (it == shown_.end()) {
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
const Shown &shown = it->second;
|
|
593
|
+
if (shown.buttons.fromHistory) {
|
|
594
|
+
if (navigate) {
|
|
595
|
+
navigate(surfaceId, direction, 1);
|
|
596
|
+
}
|
|
597
|
+
} else if (direction == NavigationDirection::Back && shown.header) {
|
|
598
|
+
if (const ViewData *d = backend_.data(shown.header->screen); d && d->emitter) {
|
|
599
|
+
d->emitter->dispatchEvent("headerBackButtonClicked");
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
bool Headers::showHistoryMenu(SurfaceId surfaceId, NavigationDirection direction) {
|
|
605
|
+
auto it = shown_.find(surfaceId);
|
|
606
|
+
if (it == shown_.end() || !it->second.history || !it->second.buttons.fromHistory) {
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
GtkWidget *chevron = direction == NavigationDirection::Back ? it->second.back : it->second.forward;
|
|
610
|
+
if (!chevron || !gtk_widget_get_sensitive(chevron)) {
|
|
611
|
+
return false;
|
|
612
|
+
}
|
|
613
|
+
std::vector<MenuItem> items;
|
|
614
|
+
for (const auto &entry : historyMenu(*it->second.history, direction)) {
|
|
615
|
+
items.push_back({
|
|
616
|
+
.title = entry.title,
|
|
617
|
+
.command = direction == NavigationDirection::Back ? MenuCommand::Back : MenuCommand::Forward,
|
|
618
|
+
.id = std::to_string(entry.steps),
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
// Under the chevron, where a menu button's list opens.
|
|
622
|
+
Point at{0, static_cast<Float>(gtk_widget_get_height(chevron))};
|
|
623
|
+
return popUpContextMenu(chevron, at, items, [this, surfaceId, direction](const MenuItem &item) {
|
|
624
|
+
if (navigate) {
|
|
625
|
+
navigate(surfaceId, direction, std::stoi(item.id));
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
void Headers::show(SurfaceId surfaceId, const std::optional<ScreenStacks::FrontHeader> &header) {
|
|
631
|
+
auto &shown = shown_[surfaceId];
|
|
632
|
+
shown.header = header;
|
|
633
|
+
layOut(surfaceId, shown);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
void Headers::refresh() {
|
|
637
|
+
for (auto &[surfaceId, shown] : shown_) {
|
|
638
|
+
const ViewData *config = shown.header ? backend_.data(shown.header->config) : nullptr;
|
|
639
|
+
auto bar = config && config->props ? headerBar(*config->props) : std::nullopt;
|
|
640
|
+
auto subviews = subviewsOf(config);
|
|
641
|
+
// The hosted subviews are the config's still, unless Fabric moved them since.
|
|
642
|
+
bool moved = false;
|
|
643
|
+
for (Tag tag : shown.hosted) {
|
|
644
|
+
if (!backend_.isHosted(tag)) {
|
|
645
|
+
moved = true;
|
|
646
|
+
} else if (const ViewData *d = backend_.data(tag); d && d->props) {
|
|
647
|
+
subviews.push_back({tag, headerItemType(*d->props).value_or("")});
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
auto layout = headerLayout(bar.value_or(HeaderBar{}), subviews, shown.header && shown.header->canGoBack);
|
|
651
|
+
if (moved || bar != shown.bar || layout != shown.layout) {
|
|
652
|
+
layOut(surfaceId, shown);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
void Headers::refreshChrome(SurfaceId surfaceId) {
|
|
658
|
+
layOut(surfaceId, shown_[surfaceId]);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
void Headers::toggleSidebar(SurfaceId surfaceId) {
|
|
662
|
+
backend_.splitViews().toggle(surfaceId);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
GtkWidget *Headers::sidebarButton(SurfaceId surfaceId) const {
|
|
666
|
+
auto it = shown_.find(surfaceId);
|
|
667
|
+
return it == shown_.end() ? nullptr : it->second.sidebarButton;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
void Headers::removeSurface(SurfaceId surfaceId) {
|
|
671
|
+
if (auto it = shown_.find(surfaceId); it != shown_.end()) {
|
|
672
|
+
clear(surfaceId, it->second);
|
|
673
|
+
shown_.erase(it);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
void Headers::forget(Tag tag) {
|
|
678
|
+
for (auto &[surfaceId, shown] : shown_) {
|
|
679
|
+
if (auto it = std::find(shown.hosted.begin(), shown.hosted.end(), tag); it != shown.hosted.end()) {
|
|
680
|
+
// Let go of as clear() lets go of it, which will no longer see it.
|
|
681
|
+
backend_.unhostView(tag);
|
|
682
|
+
backend_.hooks.detachInput(tag);
|
|
683
|
+
backend_.hooks.containerRoot(tag, false);
|
|
684
|
+
shown.hosted.erase(it);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
namespace {
|
|
690
|
+
|
|
691
|
+
// Takes a widget packed into a header bar (the window's, or the sidebar's) back out, and lets it go.
|
|
692
|
+
void unpack(GtkWidget *widget) {
|
|
693
|
+
if (GtkWidget *owner = gtk_widget_get_ancestor(widget, ADW_TYPE_HEADER_BAR)) {
|
|
694
|
+
adw_header_bar_remove(ADW_HEADER_BAR(owner), widget);
|
|
695
|
+
} else if (gtk_widget_get_parent(widget)) {
|
|
696
|
+
gtk_widget_unparent(widget);
|
|
697
|
+
}
|
|
698
|
+
g_object_unref(widget);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
} // namespace
|
|
702
|
+
|
|
703
|
+
void Headers::clear(SurfaceId surfaceId, Shown &shown, bool keepSearches) {
|
|
704
|
+
GtkWidget *bar = headerBarOf(surfaceId);
|
|
705
|
+
if (!keepSearches) {
|
|
706
|
+
for (GtkWidget *entry : shown.searches) {
|
|
707
|
+
unpack(entry);
|
|
708
|
+
}
|
|
709
|
+
shown.searches.clear();
|
|
710
|
+
}
|
|
711
|
+
for (Tag tag : shown.hosted) {
|
|
712
|
+
backend_.unhostView(tag);
|
|
713
|
+
backend_.hooks.detachInput(tag);
|
|
714
|
+
backend_.hooks.containerRoot(tag, false);
|
|
715
|
+
}
|
|
716
|
+
shown.hosted.clear();
|
|
717
|
+
if (bar) {
|
|
718
|
+
adw_header_bar_set_title_widget(ADW_HEADER_BAR(bar), nullptr);
|
|
719
|
+
}
|
|
720
|
+
// A chevron's list still open goes before the chevron does.
|
|
721
|
+
for (GtkWidget *chevron : {shown.back, shown.forward}) {
|
|
722
|
+
for (GtkWidget *c = chevron ? gtk_widget_get_first_child(chevron) : nullptr; c;) {
|
|
723
|
+
GtkWidget *next = gtk_widget_get_next_sibling(c);
|
|
724
|
+
if (GTK_IS_POPOVER(c)) {
|
|
725
|
+
gtk_widget_unparent(c);
|
|
726
|
+
}
|
|
727
|
+
c = next;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
// What was packed into the bar comes out of it; the title's own children go with the title. A
|
|
731
|
+
// search field is its search bar view's, which keeps it for the next header.
|
|
732
|
+
for (GtkWidget *widget : shown.packed) {
|
|
733
|
+
unpack(widget);
|
|
734
|
+
}
|
|
735
|
+
shown.packed.clear();
|
|
736
|
+
shown.back = nullptr;
|
|
737
|
+
shown.forward = nullptr;
|
|
738
|
+
shown.sidebarButton = nullptr;
|
|
739
|
+
shown.buttons = {};
|
|
740
|
+
if (bar && !shown.background.empty()) {
|
|
741
|
+
gtk_widget_remove_css_class(bar, shown.background.c_str());
|
|
742
|
+
}
|
|
743
|
+
shown.background.clear();
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
void Headers::layOut(SurfaceId surfaceId, Shown &shown) {
|
|
747
|
+
layOutBar(surfaceId, shown);
|
|
748
|
+
// A sheet or cover in front holds the new items still too.
|
|
749
|
+
backend_.modals().hold(surfaceId);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
void Headers::layOutBar(SurfaceId surfaceId, Shown &shown) {
|
|
753
|
+
// The search fields stay packed when they stay the same: one taken out loses the focus, and
|
|
754
|
+
// typing into it stops.
|
|
755
|
+
clear(surfaceId, shown, true);
|
|
756
|
+
GtkWidget *bar = headerBarOf(surfaceId);
|
|
757
|
+
const ViewData *config = shown.header ? backend_.data(shown.header->config) : nullptr;
|
|
758
|
+
shown.bar = config && config->props ? headerBar(*config->props) : std::nullopt;
|
|
759
|
+
auto subviews = subviewsOf(config);
|
|
760
|
+
shown.layout = headerLayout(shown.bar.value_or(HeaderBar{}), subviews, shown.header && shown.header->canGoBack);
|
|
761
|
+
// A window with a split view of its own keeps its sidebar button, and the pair, with no header
|
|
762
|
+
// in front, as the macOS toolbar does.
|
|
763
|
+
bool header = shown.bar && !shown.bar->hidden;
|
|
764
|
+
bool sidebar = backend_.splitViews().windowSplit(surfaceId).has_value();
|
|
765
|
+
bool showing = bar && header;
|
|
766
|
+
std::vector<GtkWidget *> searches;
|
|
767
|
+
for (Tag tag : showing ? shown.layout.searchBars : std::vector<Tag>{}) {
|
|
768
|
+
const ViewData *subview = backend_.data(tag);
|
|
769
|
+
for (GtkWidget *c = subview ? gtk_widget_get_first_child(subview->widget) : nullptr; c; c = gtk_widget_get_next_sibling(c)) {
|
|
770
|
+
if (const ViewData *d = viewData(c); d && d->component == "RNSSearchBar") {
|
|
771
|
+
if (GtkWidget *entry = backend_.searchEntry(d->tag)) {
|
|
772
|
+
searches.push_back(entry);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
if (searches != shown.searches) {
|
|
778
|
+
clear(surfaceId, shown);
|
|
779
|
+
// The end packs from the window's edge inward: the search fields are outermost.
|
|
780
|
+
for (GtkWidget *entry : searches) {
|
|
781
|
+
adw_header_bar_pack_end(ADW_HEADER_BAR(bar), entry);
|
|
782
|
+
shown.searches.push_back(GTK_WIDGET(g_object_ref(entry)));
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
if (!bar || (!header && !sidebar)) {
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
const HeaderLayout &layout = shown.layout;
|
|
789
|
+
auto keep = [&shown](GtkWidget *widget) {
|
|
790
|
+
shown.packed.push_back(GTK_WIDGET(g_object_ref(widget)));
|
|
791
|
+
return widget;
|
|
792
|
+
};
|
|
793
|
+
if (sidebar) {
|
|
794
|
+
GtkWidget *toggle = keep(gtk_button_new_from_icon_name("sidebar-show-symbolic"));
|
|
795
|
+
gtk_widget_set_tooltip_text(toggle, "Show Sidebar");
|
|
796
|
+
gtk_accessible_update_property(GTK_ACCESSIBLE(toggle), GTK_ACCESSIBLE_PROPERTY_LABEL, "Show Sidebar", -1);
|
|
797
|
+
auto *where = new ChevronTarget{this, surfaceId, NavigationDirection::Back};
|
|
798
|
+
g_signal_connect_data(toggle, "clicked", G_CALLBACK(+[](GtkButton *, gpointer data) {
|
|
799
|
+
auto *t = static_cast<ChevronTarget *>(data);
|
|
800
|
+
t->headers->toggleSidebar(t->surfaceId);
|
|
801
|
+
}), where, [](gpointer data, GClosure *) { delete static_cast<ChevronTarget *>(data); }, GConnectFlags(0));
|
|
802
|
+
// Over the sidebar, at its end, while the full-height sidebar shows beside the content.
|
|
803
|
+
if (GtkWidget *overSidebar = backend_.splitViews().sidebarHeaderShown(surfaceId)) {
|
|
804
|
+
adw_header_bar_pack_end(ADW_HEADER_BAR(overSidebar), toggle);
|
|
805
|
+
} else {
|
|
806
|
+
adw_header_bar_pack_start(ADW_HEADER_BAR(bar), toggle);
|
|
807
|
+
}
|
|
808
|
+
shown.sidebarButton = toggle;
|
|
809
|
+
}
|
|
810
|
+
auto slotFor = [&](Tag tag) {
|
|
811
|
+
GtkWidget *slot = makeHostSlot();
|
|
812
|
+
// Centred in the bar's height, as a toolbar item is.
|
|
813
|
+
gtk_widget_set_valign(slot, GTK_ALIGN_CENTER);
|
|
814
|
+
backend_.hostView(tag, slot);
|
|
815
|
+
backend_.hooks.containerRoot(tag, true);
|
|
816
|
+
backend_.hooks.attachInput(slot, surfaceId, tag);
|
|
817
|
+
shown.hosted.push_back(tag);
|
|
818
|
+
return keep(slot);
|
|
819
|
+
};
|
|
820
|
+
// The header's tint on its buttons, the back button's included, unless a button has its own; its
|
|
821
|
+
// title colour on the title; its background colour on the bar.
|
|
822
|
+
std::string tint = header ? colorClass("color", shown.bar->color) : std::string();
|
|
823
|
+
if (header) {
|
|
824
|
+
shown.background = colorClass("background-color", shown.bar->backgroundColor);
|
|
825
|
+
addClass(bar, shown.background);
|
|
826
|
+
}
|
|
827
|
+
auto button = [&](const BarButton &b) {
|
|
828
|
+
if (b.spacing) {
|
|
829
|
+
GtkWidget *widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
|
830
|
+
gtk_widget_set_size_request(widget, static_cast<int>(*b.spacing), -1);
|
|
831
|
+
return keep(widget);
|
|
832
|
+
}
|
|
833
|
+
GtkWidget *widget = b.menu ? menuButton(backend_, shown.header->config, b) : gtk_button_new_with_label(b.title.c_str());
|
|
834
|
+
addClass(widget, b.tint ? colorClass("color", b.tint) : tint);
|
|
835
|
+
if (b.menu) {
|
|
836
|
+
gtk_widget_set_sensitive(widget, !b.disabled);
|
|
837
|
+
return keep(widget);
|
|
838
|
+
}
|
|
839
|
+
gtk_widget_set_sensitive(widget, !b.disabled);
|
|
840
|
+
if (!b.accessibilityLabel.empty()) {
|
|
841
|
+
gtk_accessible_update_property(GTK_ACCESSIBLE(widget), GTK_ACCESSIBLE_PROPERTY_LABEL, b.accessibilityLabel.c_str(), -1);
|
|
842
|
+
}
|
|
843
|
+
Tag configTag = shown.header->config;
|
|
844
|
+
auto *pressed = new PressTarget(&backend_, {configTag, b.id});
|
|
845
|
+
g_signal_connect_data(widget, "clicked", G_CALLBACK(+[](GtkButton *, gpointer data) {
|
|
846
|
+
auto *p = static_cast<PressTarget *>(data);
|
|
847
|
+
if (const ViewData *d = p->first->data(p->second.first); d && d->emitter) {
|
|
848
|
+
d->emitter->dispatchEvent("pressHeaderBarButtonItem", folly::dynamic::object("buttonId", p->second.second));
|
|
849
|
+
}
|
|
850
|
+
}), pressed, [](gpointer data, GClosure *) { delete static_cast<PressTarget *>(data); }, GConnectFlags(0));
|
|
851
|
+
return keep(widget);
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
// The pair leads the bar while the window has history, or, for an app that reports none, while
|
|
855
|
+
// the stack can go back; the header's hideBackButton and its own left items replace it. The
|
|
856
|
+
// layout is asked as though the screen could go back, so the history has its say as well.
|
|
857
|
+
// With no header, only the history has its say.
|
|
858
|
+
bool backAllowed = header && headerLayout(*shown.bar, subviews, true).back;
|
|
859
|
+
shown.buttons = header ? historyButtons(shown.history, shown.header && shown.header->canGoBack, !backAllowed)
|
|
860
|
+
: historyButtons(shown.history, false, false);
|
|
861
|
+
if (shown.buttons.shown) {
|
|
862
|
+
GtkWidget *pair = keep(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
|
|
863
|
+
gtk_widget_add_css_class(pair, "linked");
|
|
864
|
+
auto chevron = [&](NavigationDirection direction) {
|
|
865
|
+
bool back = direction == NavigationDirection::Back;
|
|
866
|
+
GtkWidget *button = gtk_button_new_from_icon_name(back ? "go-previous-symbolic" : "go-next-symbolic");
|
|
867
|
+
addClass(button, tint);
|
|
868
|
+
gtk_widget_set_tooltip_text(button, back ? "Back" : "Forward");
|
|
869
|
+
gtk_accessible_update_property(GTK_ACCESSIBLE(button), GTK_ACCESSIBLE_PROPERTY_LABEL, back ? "Back" : "Forward", -1);
|
|
870
|
+
gtk_widget_set_sensitive(button, back ? shown.buttons.backEnabled : shown.buttons.forwardEnabled);
|
|
871
|
+
ChevronTarget target{this, surfaceId, direction};
|
|
872
|
+
g_signal_connect_data(button, "clicked", G_CALLBACK(+[](GtkButton *, gpointer data) {
|
|
873
|
+
auto *t = static_cast<ChevronTarget *>(data);
|
|
874
|
+
t->headers->pressChevron(t->surfaceId, t->direction);
|
|
875
|
+
}), new ChevronTarget(target), [](gpointer data, GClosure *) { delete static_cast<ChevronTarget *>(data); }, GConnectFlags(0));
|
|
876
|
+
// A secondary click or a long press lists the entries that way, as AppKit's segments do.
|
|
877
|
+
GtkGesture *secondary = gtk_gesture_click_new();
|
|
878
|
+
gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(secondary), GDK_BUTTON_SECONDARY);
|
|
879
|
+
g_signal_connect_data(secondary, "pressed", G_CALLBACK(+[](GtkGestureClick *g, int, double, double, gpointer data) {
|
|
880
|
+
auto *t = static_cast<ChevronTarget *>(data);
|
|
881
|
+
if (t->headers->showHistoryMenu(t->surfaceId, t->direction)) {
|
|
882
|
+
gtk_gesture_set_state(GTK_GESTURE(g), GTK_EVENT_SEQUENCE_CLAIMED);
|
|
883
|
+
}
|
|
884
|
+
}), new ChevronTarget(target), [](gpointer data, GClosure *) { delete static_cast<ChevronTarget *>(data); }, GConnectFlags(0));
|
|
885
|
+
gtk_widget_add_controller(button, GTK_EVENT_CONTROLLER(secondary));
|
|
886
|
+
GtkGesture *hold = gtk_gesture_long_press_new();
|
|
887
|
+
g_signal_connect_data(hold, "pressed", G_CALLBACK(+[](GtkGestureLongPress *g, double, double, gpointer data) {
|
|
888
|
+
auto *t = static_cast<ChevronTarget *>(data);
|
|
889
|
+
if (t->headers->showHistoryMenu(t->surfaceId, t->direction)) {
|
|
890
|
+
gtk_gesture_set_state(GTK_GESTURE(g), GTK_EVENT_SEQUENCE_CLAIMED);
|
|
891
|
+
}
|
|
892
|
+
}), new ChevronTarget(target), [](gpointer data, GClosure *) { delete static_cast<ChevronTarget *>(data); }, GConnectFlags(0));
|
|
893
|
+
gtk_widget_add_controller(button, GTK_EVENT_CONTROLLER(hold));
|
|
894
|
+
gtk_box_append(GTK_BOX(pair), button);
|
|
895
|
+
return button;
|
|
896
|
+
};
|
|
897
|
+
shown.back = chevron(NavigationDirection::Back);
|
|
898
|
+
shown.forward = chevron(NavigationDirection::Forward);
|
|
899
|
+
adw_header_bar_pack_start(ADW_HEADER_BAR(bar), pair);
|
|
900
|
+
}
|
|
901
|
+
if (!header) {
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
for (Tag tag : layout.left) {
|
|
905
|
+
adw_header_bar_pack_start(ADW_HEADER_BAR(bar), slotFor(tag));
|
|
906
|
+
}
|
|
907
|
+
for (const auto &b : shown.bar->left) {
|
|
908
|
+
adw_header_bar_pack_start(ADW_HEADER_BAR(bar), button(b));
|
|
909
|
+
}
|
|
910
|
+
if (!layout.center.empty()) {
|
|
911
|
+
GtkWidget *center = keep(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6));
|
|
912
|
+
for (Tag tag : layout.center) {
|
|
913
|
+
gtk_box_append(GTK_BOX(center), slotFor(tag));
|
|
914
|
+
}
|
|
915
|
+
adw_header_bar_set_title_widget(ADW_HEADER_BAR(bar), center);
|
|
916
|
+
} else {
|
|
917
|
+
GtkWidget *title = keep(adw_window_title_new(shown.bar->title.c_str(), ""));
|
|
918
|
+
addClass(title, colorClass("color", shown.bar->titleColor));
|
|
919
|
+
adw_header_bar_set_title_widget(ADW_HEADER_BAR(bar), title);
|
|
920
|
+
}
|
|
921
|
+
// Inward of the search fields, the last piece in reading order first.
|
|
922
|
+
for (auto it = layout.right.rbegin(); it != layout.right.rend(); ++it) {
|
|
923
|
+
adw_header_bar_pack_end(ADW_HEADER_BAR(bar), slotFor(*it));
|
|
924
|
+
}
|
|
925
|
+
for (auto it = shown.bar->right.rbegin(); it != shown.bar->right.rend(); ++it) {
|
|
926
|
+
adw_header_bar_pack_end(ADW_HEADER_BAR(bar), button(*it));
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// --- Modals -----------------------------------------------------------------------------------
|
|
931
|
+
|
|
932
|
+
namespace {
|
|
933
|
+
|
|
934
|
+
// The header config under a presented screen, whose header the sheet draws.
|
|
935
|
+
const ViewData *headerConfigOf(const GtkBackend &backend, Tag screen) {
|
|
936
|
+
const ViewData *d = backend.data(screen);
|
|
937
|
+
for (GtkWidget *c = d ? gtk_widget_get_first_child(d->widget) : nullptr; c; c = gtk_widget_get_next_sibling(c)) {
|
|
938
|
+
if (const ViewData *child = viewData(c); child && child->component == "RNSScreenStackHeaderConfig") {
|
|
939
|
+
return child;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
return nullptr;
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
// A shortcut on `widget`, anywhere in its window: `fn` says whether it acted.
|
|
946
|
+
void onKey(GtkWidget *widget, const char *keys, std::function<bool()> fn) {
|
|
947
|
+
GtkEventController *controller = gtk_shortcut_controller_new();
|
|
948
|
+
gtk_shortcut_controller_set_scope(GTK_SHORTCUT_CONTROLLER(controller), GTK_SHORTCUT_SCOPE_GLOBAL);
|
|
949
|
+
auto *heap = new std::function<bool()>(std::move(fn));
|
|
950
|
+
GtkShortcutAction *action = gtk_callback_action_new(
|
|
951
|
+
[](GtkWidget *, GVariant *, gpointer data) -> gboolean { return (*static_cast<std::function<bool()> *>(data))(); },
|
|
952
|
+
heap, [](gpointer data) { delete static_cast<std::function<bool()> *>(data); });
|
|
953
|
+
gtk_shortcut_controller_add_shortcut(
|
|
954
|
+
GTK_SHORTCUT_CONTROLLER(controller), gtk_shortcut_new(gtk_shortcut_trigger_parse_string(keys), action));
|
|
955
|
+
gtk_widget_add_controller(widget, controller);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
int naturalHeight(GtkWidget *widget) {
|
|
959
|
+
int minimum = 0, natural = 0;
|
|
960
|
+
gtk_widget_measure(widget, GTK_ORIENTATION_VERTICAL, -1, &minimum, &natural, nullptr, nullptr);
|
|
961
|
+
return natural;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
} // namespace
|
|
965
|
+
|
|
966
|
+
Modals::~Modals() {
|
|
967
|
+
while (!presented_.empty()) {
|
|
968
|
+
dismiss(presented_.begin()->first);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
void Modals::mounted(SurfaceId surfaceId, Tag tag) {
|
|
973
|
+
if (!presented_.count(tag)) {
|
|
974
|
+
pending_.insert({surfaceId, tag});
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
void Modals::setPresented(SurfaceId surfaceId, Tag screen, bool presented) {
|
|
979
|
+
if (presented) {
|
|
980
|
+
mounted(surfaceId, screen);
|
|
981
|
+
} else {
|
|
982
|
+
unmounted(screen);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
void Modals::unmounted(Tag tag) {
|
|
987
|
+
for (auto it = pending_.begin(); it != pending_.end();) {
|
|
988
|
+
it = it->second == tag ? pending_.erase(it) : std::next(it);
|
|
989
|
+
}
|
|
990
|
+
dismiss(tag);
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
void Modals::settle() {
|
|
994
|
+
auto pending = std::move(pending_);
|
|
995
|
+
pending_.clear();
|
|
996
|
+
for (auto [surfaceId, tag] : pending) {
|
|
997
|
+
present(surfaceId, tag);
|
|
998
|
+
}
|
|
999
|
+
for (auto &[tag, presented] : presented_) {
|
|
1000
|
+
dress(tag, presented);
|
|
1001
|
+
fit(tag, presented);
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
GtkWidget *Modals::slotOf(Tag tag) const {
|
|
1006
|
+
auto it = presented_.find(tag);
|
|
1007
|
+
return it == presented_.end() ? nullptr : it->second.slot;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
bool Modals::inDialog(Tag tag) const {
|
|
1011
|
+
auto it = presented_.find(tag);
|
|
1012
|
+
return it != presented_.end() && it->second.dialog != nullptr;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
GtkWidget *Modals::popoverOf(Tag tag) const {
|
|
1016
|
+
auto it = presented_.find(tag);
|
|
1017
|
+
return it == presented_.end() ? nullptr : it->second.popover;
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
GtkWidget *Modals::sheetHeaderOf(Tag tag) const {
|
|
1021
|
+
auto it = presented_.find(tag);
|
|
1022
|
+
return it == presented_.end() ? nullptr : it->second.header;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
void Modals::press(Tag tag, const std::string &buttonId) {
|
|
1026
|
+
if (const ViewData *config = headerConfigOf(backend_, tag); config && config->emitter) {
|
|
1027
|
+
config->emitter->dispatchEvent(
|
|
1028
|
+
"pressHeaderBarButtonItem", folly::dynamic::object("buttonId", buttonId), RawEvent::Category::Discrete);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
void Modals::requestClose(Tag tag) {
|
|
1033
|
+
auto it = presented_.find(tag);
|
|
1034
|
+
if (it != presented_.end() && it->second.screen) {
|
|
1035
|
+
// A sheet's Cancel takes Escape, as its key equivalent does on macOS; otherwise the screen is
|
|
1036
|
+
// dismissed natively, and the router follows.
|
|
1037
|
+
for (const auto &button : it->second.chrome.buttons) {
|
|
1038
|
+
if (button.cancel && !button.disabled) {
|
|
1039
|
+
press(tag, button.id);
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
if (const ViewData *d = backend_.data(tag); d && d->emitter) {
|
|
1044
|
+
d->emitter->dispatchEvent("dismissed", folly::dynamic::object("dismissCount", 1), RawEvent::Category::Discrete);
|
|
1045
|
+
}
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
1048
|
+
if (const ViewData *d = backend_.data(tag)) {
|
|
1049
|
+
if (auto emitter = std::dynamic_pointer_cast<const ModalHostViewEventEmitter>(d->emitter)) {
|
|
1050
|
+
emitter->onRequestClose({});
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
std::optional<Tag> Modals::front(GtkWindow *window) const {
|
|
1056
|
+
std::optional<Tag> front;
|
|
1057
|
+
uint64_t latest = 0;
|
|
1058
|
+
for (const auto &[tag, presented] : presented_) {
|
|
1059
|
+
GtkWidget *surface = backend_.surfaceWidget(presented.surfaceId);
|
|
1060
|
+
if (surface && gtk_widget_get_root(surface) == GTK_ROOT(window) && presented.order > latest) {
|
|
1061
|
+
latest = presented.order;
|
|
1062
|
+
front = tag;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
return front;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
// Whether a popover on `anchor` would show: it is in a window, and nothing between it and the
|
|
1069
|
+
// window is hidden, as a screen the stack covers is (setCovered). Not whether it is mapped yet, nor
|
|
1070
|
+
// whether the window is shown: the first mount presents before the window is (GtkWindows::didMount).
|
|
1071
|
+
static bool anchorOnScreen(GtkWidget *anchor) {
|
|
1072
|
+
GtkWidget *root = GTK_WIDGET(gtk_widget_get_root(anchor));
|
|
1073
|
+
if (!root) {
|
|
1074
|
+
return false;
|
|
1075
|
+
}
|
|
1076
|
+
for (GtkWidget *w = anchor; w != root; w = gtk_widget_get_parent(w)) {
|
|
1077
|
+
if (!gtk_widget_get_visible(w) || !gtk_widget_get_child_visible(w)) {
|
|
1078
|
+
return false;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
return true;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
bool Modals::pressDefault(Tag tag) {
|
|
1085
|
+
auto it = presented_.find(tag);
|
|
1086
|
+
if (it == presented_.end()) {
|
|
1087
|
+
return false;
|
|
1088
|
+
}
|
|
1089
|
+
for (const auto &button : it->second.chrome.buttons) {
|
|
1090
|
+
if (button.isDefault && !button.disabled) {
|
|
1091
|
+
press(tag, button.id);
|
|
1092
|
+
return true;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
return false;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
Tag Modals::frontOf(SurfaceId surfaceId) const {
|
|
1099
|
+
const std::pair<const Tag, Presented> *front = nullptr;
|
|
1100
|
+
for (const auto &entry : presented_) {
|
|
1101
|
+
if (entry.second.surfaceId == surfaceId && (!front || entry.second.order > front->second.order)) {
|
|
1102
|
+
front = &entry;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
return front ? front->first : 0;
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
void Modals::requestCloseFront(SurfaceId surfaceId) {
|
|
1109
|
+
if (Tag front = frontOf(surfaceId)) {
|
|
1110
|
+
requestClose(front);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
bool Modals::pressDefaultFront(SurfaceId surfaceId) {
|
|
1115
|
+
Tag front = frontOf(surfaceId);
|
|
1116
|
+
return front && pressDefault(front);
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
void Modals::present(SurfaceId surfaceId, Tag tag) {
|
|
1120
|
+
const ViewData *d = backend_.data(tag);
|
|
1121
|
+
GtkWidget *surface = backend_.surfaceWidget(surfaceId);
|
|
1122
|
+
const WindowChrome *chrome = chromeOf(surface);
|
|
1123
|
+
if (!d || !d->props || !chrome || presented_.count(tag)) {
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
1126
|
+
installSheetStyle();
|
|
1127
|
+
Presented p{.surfaceId = surfaceId, .presentation = d->desktop.presentation, .screen = d->component == "RNSScreen", .order = ++presentations_};
|
|
1128
|
+
std::optional<Tag> anchor = d->desktop.anchor;
|
|
1129
|
+
if (p.screen) {
|
|
1130
|
+
p.presentation = screenPresentation(*d->props).value_or(ModalPresentation::Sheet);
|
|
1131
|
+
if (const auto *props = dynamic_cast<const ScreenProps *>(d->props.get()); props && props->anchor != 0) {
|
|
1132
|
+
anchor = props->anchor;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
// A popover points at its anchor; an anchor that is not on screen leaves it a sheet, as on macOS.
|
|
1136
|
+
GtkWidget *anchorWidget = anchor ? backend_.view(*anchor) : nullptr;
|
|
1137
|
+
if (anchorWidget && !anchorOnScreen(anchorWidget)) {
|
|
1138
|
+
anchorWidget = nullptr;
|
|
1139
|
+
}
|
|
1140
|
+
if (p.presentation == ModalPresentation::Popover && !anchorWidget) {
|
|
1141
|
+
p.presentation = ModalPresentation::Sheet;
|
|
1142
|
+
} else if (anchorWidget) {
|
|
1143
|
+
p.presentation = ModalPresentation::Popover;
|
|
1144
|
+
}
|
|
1145
|
+
GtkRoot *root = gtk_widget_get_root(surface);
|
|
1146
|
+
if (GtkWidget *focus = root ? gtk_root_get_focus(root) : nullptr) {
|
|
1147
|
+
p.opener = GTK_WIDGET(g_object_ref(focus));
|
|
1148
|
+
}
|
|
1149
|
+
p.slot = GTK_WIDGET(g_object_ref_sink(makeHostSlot()));
|
|
1150
|
+
backend_.hostView(tag, p.slot);
|
|
1151
|
+
// libadwaita presents a dialog in a window of its own outside an AdwWindow, or over one that is
|
|
1152
|
+
// not resizable: the sheet is drawn over the window there instead.
|
|
1153
|
+
bool inWindow = root && (ADW_IS_WINDOW(root) || ADW_IS_APPLICATION_WINDOW(root)) && gtk_window_get_resizable(GTK_WINDOW(root));
|
|
1154
|
+
if (p.presentation == ModalPresentation::Sheet) {
|
|
1155
|
+
// What a sheet shows: the screen's header, when it has one (dress), over the content.
|
|
1156
|
+
p.card = GTK_WIDGET(g_object_ref_sink(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0)));
|
|
1157
|
+
gtk_box_append(GTK_BOX(p.card), p.slot);
|
|
1158
|
+
}
|
|
1159
|
+
if (p.presentation == ModalPresentation::Popover) {
|
|
1160
|
+
GtkWidget *popover = gtk_popover_new();
|
|
1161
|
+
gtk_popover_set_child(GTK_POPOVER(popover), p.slot);
|
|
1162
|
+
gtk_popover_set_autohide(GTK_POPOVER(popover), TRUE);
|
|
1163
|
+
gtk_widget_set_parent(popover, anchorWidget);
|
|
1164
|
+
// GTK moves the focus into an autohide popover as it shows. With nothing in it to take the
|
|
1165
|
+
// focus and nothing focused in the window, as at launch, GTK passes the window's null focus to
|
|
1166
|
+
// gtk_widget_is_ancestor, a critical: the popover takes the focus itself first then, and a
|
|
1167
|
+
// field in it still takes it from there.
|
|
1168
|
+
gtk_widget_set_focusable(popover, TRUE);
|
|
1169
|
+
g_signal_connect(popover, "map", G_CALLBACK(+[](GtkWidget *shown, gpointer) {
|
|
1170
|
+
if (GtkRoot *root = gtk_widget_get_root(shown); root && !gtk_root_get_focus(root)) {
|
|
1171
|
+
gtk_widget_grab_focus(shown);
|
|
1172
|
+
}
|
|
1173
|
+
}), nullptr);
|
|
1174
|
+
GdkRectangle rect{0, 0, std::max(1, gtk_widget_get_width(anchorWidget)), std::max(1, gtk_widget_get_height(anchorWidget))};
|
|
1175
|
+
gtk_popover_set_pointing_to(GTK_POPOVER(popover), &rect);
|
|
1176
|
+
// Closed by GTK (a click outside, or Escape): the app decides, as for Escape on a sheet.
|
|
1177
|
+
g_signal_connect_data(
|
|
1178
|
+
popover, "closed", G_CALLBACK(+[](GtkPopover *, gpointer data) {
|
|
1179
|
+
auto *self = static_cast<CloseTarget *>(data);
|
|
1180
|
+
if (self->first->presented_.count(self->second)) {
|
|
1181
|
+
self->first->requestClose(self->second);
|
|
1182
|
+
// GTK hid it to emit this. Until the app unmounts the modal, it is still presented.
|
|
1183
|
+
g_idle_add_full(
|
|
1184
|
+
G_PRIORITY_DEFAULT_IDLE, +[](gpointer data) -> gboolean {
|
|
1185
|
+
auto *again = static_cast<CloseTarget *>(data);
|
|
1186
|
+
auto it = again->first->presented_.find(again->second);
|
|
1187
|
+
if (it != again->first->presented_.end() && it->second.popover) {
|
|
1188
|
+
gtk_popover_popup(GTK_POPOVER(it->second.popover));
|
|
1189
|
+
}
|
|
1190
|
+
return G_SOURCE_REMOVE;
|
|
1191
|
+
},
|
|
1192
|
+
new CloseTarget(*self), [](gpointer data) { delete static_cast<CloseTarget *>(data); });
|
|
1193
|
+
}
|
|
1194
|
+
}),
|
|
1195
|
+
new CloseTarget(this, tag), [](gpointer data, GClosure *) { delete static_cast<CloseTarget *>(data); },
|
|
1196
|
+
GConnectFlags(0));
|
|
1197
|
+
p.popover = GTK_WIDGET(g_object_ref(popover));
|
|
1198
|
+
} else if (p.presentation == ModalPresentation::Sheet && inWindow && haveAdwDialog()) {
|
|
1199
|
+
// AdwDialog by its type and properties, which 1.4's headers do not declare. It asks before it
|
|
1200
|
+
// closes, and the app decides, as Escape on macOS asks.
|
|
1201
|
+
GtkWidget *dialog = GTK_WIDGET(g_object_new(g_type_from_name("AdwDialog"), "can-close", FALSE, "child", p.card, nullptr));
|
|
1202
|
+
// Floating, never libadwaita's bottom sheet.
|
|
1203
|
+
if (g_object_class_find_property(G_OBJECT_GET_CLASS(dialog), "presentation-mode")) {
|
|
1204
|
+
g_object_set(dialog, "presentation-mode", 1, nullptr);
|
|
1205
|
+
}
|
|
1206
|
+
g_signal_connect_data(
|
|
1207
|
+
dialog, "close-attempt", G_CALLBACK(+[](GtkWidget *, gpointer data) {
|
|
1208
|
+
auto *self = static_cast<CloseTarget *>(data);
|
|
1209
|
+
self->first->requestClose(self->second);
|
|
1210
|
+
}),
|
|
1211
|
+
new CloseTarget(this, tag), [](gpointer data, GClosure *) { delete static_cast<CloseTarget *>(data); },
|
|
1212
|
+
GConnectFlags(0));
|
|
1213
|
+
p.dialog = GTK_WIDGET(g_object_ref(dialog));
|
|
1214
|
+
} else if (p.presentation == ModalPresentation::Sheet) {
|
|
1215
|
+
// libadwaita 1.4: a dimmed backdrop over the window's content with the card in its middle.
|
|
1216
|
+
GtkWidget *backdrop = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
1217
|
+
gtk_widget_add_css_class(backdrop, "ngn-backdrop");
|
|
1218
|
+
gtk_widget_set_halign(p.card, GTK_ALIGN_CENTER);
|
|
1219
|
+
gtk_widget_set_valign(p.card, GTK_ALIGN_CENTER);
|
|
1220
|
+
gtk_widget_set_vexpand(p.card, TRUE);
|
|
1221
|
+
gtk_widget_add_css_class(p.card, "ngn-sheet");
|
|
1222
|
+
gtk_widget_set_overflow(p.card, GTK_OVERFLOW_HIDDEN);
|
|
1223
|
+
gtk_box_append(GTK_BOX(backdrop), p.card);
|
|
1224
|
+
p.layer = backdrop;
|
|
1225
|
+
} else {
|
|
1226
|
+
p.layer = p.slot;
|
|
1227
|
+
}
|
|
1228
|
+
if (p.layer) {
|
|
1229
|
+
// Each layer's Escape reaches the presentation in front: GTK runs the first one registered.
|
|
1230
|
+
onKey(p.layer, "Escape", [this, surfaceId] {
|
|
1231
|
+
requestCloseFront(surfaceId);
|
|
1232
|
+
return true;
|
|
1233
|
+
});
|
|
1234
|
+
gtk_overlay_add_overlay(GTK_OVERLAY(chrome->overlay), p.layer);
|
|
1235
|
+
}
|
|
1236
|
+
if (GtkWidget *keys = p.dialog ? p.dialog : p.layer) {
|
|
1237
|
+
// As for Escape, whichever layer's shortcut runs first, the presentation in front takes Return.
|
|
1238
|
+
// With no enabled default button it is still consumed, so it does not reach the window's own
|
|
1239
|
+
// default button behind the sheet.
|
|
1240
|
+
onKey(keys, "Return|KP_Enter", [this, surfaceId] {
|
|
1241
|
+
bool pressed = pressDefaultFront(surfaceId);
|
|
1242
|
+
return pressed || frontOf(surfaceId) != 0;
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
backend_.hooks.containerRoot(tag, true);
|
|
1246
|
+
backend_.hooks.attachInput(p.slot, surfaceId, tag);
|
|
1247
|
+
auto &presented = presented_[tag] = p;
|
|
1248
|
+
dress(tag, presented);
|
|
1249
|
+
fit(tag, presented);
|
|
1250
|
+
if (presented.dialog) {
|
|
1251
|
+
adwFunction<void (*)(GtkWidget *, GtkWidget *)>("adw_dialog_present")(presented.dialog, surface);
|
|
1252
|
+
}
|
|
1253
|
+
if (presented.popover) {
|
|
1254
|
+
gtk_popover_popup(GTK_POPOVER(presented.popover));
|
|
1255
|
+
}
|
|
1256
|
+
hold(surfaceId);
|
|
1257
|
+
if (!gtk_widget_child_focus(presented.slot, GTK_DIR_TAB_FORWARD) && !presented.popover) {
|
|
1258
|
+
// Nothing in it takes focus (a drawn sheet of text): the slot does, so a background field that
|
|
1259
|
+
// had focus does not keep the keys, which GTK does not take back when it stops being focusable.
|
|
1260
|
+
// Not a popover: GTK moves focus into that as it shows, and one with nothing to focus leaves
|
|
1261
|
+
// the window's own focus alone (the popover takes it itself only when nothing had it).
|
|
1262
|
+
gtk_widget_set_focusable(presented.slot, TRUE);
|
|
1263
|
+
gtk_widget_grab_focus(presented.slot);
|
|
1264
|
+
}
|
|
1265
|
+
if (auto emitter = std::dynamic_pointer_cast<const ModalHostViewEventEmitter>(d->emitter)) {
|
|
1266
|
+
emitter->onShow({});
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
void Modals::hold(SurfaceId surfaceId) {
|
|
1271
|
+
const Presented *front = nullptr;
|
|
1272
|
+
for (const auto &[tag, presented] : presented_) {
|
|
1273
|
+
if (presented.surfaceId == surfaceId && presented.layer && (!front || presented.order > front->order)) {
|
|
1274
|
+
front = &presented;
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
holdChrome(backend_.surfaceWidget(surfaceId), front ? front->layer : nullptr);
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
void Modals::dress(Tag tag, Presented &p) {
|
|
1281
|
+
const ViewData *d = backend_.data(tag);
|
|
1282
|
+
const ViewData *config = p.screen ? headerConfigOf(backend_, tag) : nullptr;
|
|
1283
|
+
auto bar = config && config->props ? headerBar(*config->props) : std::nullopt;
|
|
1284
|
+
SheetChrome chrome = bar ? sheetChrome(*bar, p.presentation) : SheetChrome{};
|
|
1285
|
+
// Named by the view's own accessibility label, or the screen's title.
|
|
1286
|
+
std::string label;
|
|
1287
|
+
if (auto props = d ? std::dynamic_pointer_cast<const ViewProps>(d->props) : nullptr) {
|
|
1288
|
+
label = presentationLabel(props->accessibilityLabel, chrome.label);
|
|
1289
|
+
}
|
|
1290
|
+
GtkWidget *named = p.popover ? p.popover : p.dialog ? p.dialog : p.layer;
|
|
1291
|
+
if (named && (label != p.label || !p.labelled)) {
|
|
1292
|
+
p.label = label;
|
|
1293
|
+
p.labelled = true;
|
|
1294
|
+
gtk_accessible_update_property(GTK_ACCESSIBLE(named), GTK_ACCESSIBLE_PROPERTY_LABEL, label.c_str(), -1);
|
|
1295
|
+
}
|
|
1296
|
+
if (chrome == p.chrome && (p.header || (chrome.heading.empty() && chrome.buttons.empty()))) {
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
p.chrome = chrome;
|
|
1300
|
+
if (p.header) {
|
|
1301
|
+
gtk_box_remove(GTK_BOX(p.card), p.header);
|
|
1302
|
+
g_object_unref(p.header);
|
|
1303
|
+
p.header = nullptr;
|
|
1304
|
+
}
|
|
1305
|
+
if (!p.card || (chrome.heading.empty() && chrome.buttons.empty())) {
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
// A sheet's header is the dialog's own header bar, as GNOME's dialogs have it: the title in the
|
|
1309
|
+
// middle, the left items (Cancel) at the start, the right ones at the end, the last of them the
|
|
1310
|
+
// default on Return. Escape presses Cancel (requestClose).
|
|
1311
|
+
GtkWidget *header = adw_header_bar_new();
|
|
1312
|
+
adw_header_bar_set_show_start_title_buttons(ADW_HEADER_BAR(header), FALSE);
|
|
1313
|
+
adw_header_bar_set_show_end_title_buttons(ADW_HEADER_BAR(header), FALSE);
|
|
1314
|
+
adw_header_bar_set_title_widget(ADW_HEADER_BAR(header), adw_window_title_new(chrome.heading.c_str(), ""));
|
|
1315
|
+
// sheetChrome keeps the left items first: those of the bar's left items that became buttons.
|
|
1316
|
+
size_t left = static_cast<size_t>(std::count_if(bar->left.begin(), bar->left.end(), [](const BarButton &b) {
|
|
1317
|
+
return !b.spacing && !(b.title.empty() && b.accessibilityLabel.empty());
|
|
1318
|
+
}));
|
|
1319
|
+
std::vector<GtkWidget *> end;
|
|
1320
|
+
for (size_t i = 0; i < chrome.buttons.size(); ++i) {
|
|
1321
|
+
const SheetButton &b = chrome.buttons[i];
|
|
1322
|
+
GtkWidget *button = gtk_button_new_with_label(b.title.c_str());
|
|
1323
|
+
gtk_widget_set_sensitive(button, !b.disabled);
|
|
1324
|
+
if (b.isDefault) {
|
|
1325
|
+
gtk_widget_add_css_class(button, "suggested-action");
|
|
1326
|
+
}
|
|
1327
|
+
auto *target = new PressTarget(nullptr, {tag, b.id});
|
|
1328
|
+
g_object_set_data(G_OBJECT(button), "ngn-modals", this);
|
|
1329
|
+
g_signal_connect_data(
|
|
1330
|
+
button, "clicked", G_CALLBACK(+[](GtkButton *button, gpointer data) {
|
|
1331
|
+
auto *self = static_cast<Modals *>(g_object_get_data(G_OBJECT(button), "ngn-modals"));
|
|
1332
|
+
auto *p = static_cast<PressTarget *>(data);
|
|
1333
|
+
self->press(p->second.first, p->second.second);
|
|
1334
|
+
}),
|
|
1335
|
+
target, [](gpointer data, GClosure *) { delete static_cast<PressTarget *>(data); }, GConnectFlags(0));
|
|
1336
|
+
if (i < left) {
|
|
1337
|
+
adw_header_bar_pack_start(ADW_HEADER_BAR(header), button);
|
|
1338
|
+
} else {
|
|
1339
|
+
end.push_back(button);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
for (auto it = end.rbegin(); it != end.rend(); ++it) {
|
|
1343
|
+
adw_header_bar_pack_end(ADW_HEADER_BAR(header), *it);
|
|
1344
|
+
}
|
|
1345
|
+
p.header = GTK_WIDGET(g_object_ref_sink(header));
|
|
1346
|
+
gtk_box_prepend(GTK_BOX(p.card), p.header);
|
|
1347
|
+
p.size = {0, 0};
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
namespace {
|
|
1351
|
+
|
|
1352
|
+
// Where a cover starts: below the top bars over the window's content, and below the header bars a
|
|
1353
|
+
// full-height split view has, the sidebar's beside the window's, which stay the window's as they do
|
|
1354
|
+
// over a cover on macOS.
|
|
1355
|
+
int coverTop(GtkWidget *surface) {
|
|
1356
|
+
int top = static_cast<int>(chromeInsets(surface).insets.top);
|
|
1357
|
+
const WindowChrome *chrome = chromeOf(surface);
|
|
1358
|
+
graphene_rect_t bounds;
|
|
1359
|
+
if (chrome && gtk_widget_is_ancestor(chrome->headerBar, surface) && gtk_widget_compute_bounds(chrome->headerBar, surface, &bounds)) {
|
|
1360
|
+
top = std::max(top, static_cast<int>(std::lround(bounds.origin.y + bounds.size.height)));
|
|
1361
|
+
}
|
|
1362
|
+
return top;
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
} // namespace
|
|
1366
|
+
|
|
1367
|
+
void Modals::fit(Tag tag, Presented &p) {
|
|
1368
|
+
GtkWidget *surface = backend_.surfaceWidget(p.surfaceId);
|
|
1369
|
+
const ViewData *d = backend_.data(tag);
|
|
1370
|
+
if (!surface || !d) {
|
|
1371
|
+
return;
|
|
1372
|
+
}
|
|
1373
|
+
Size window{static_cast<Float>(gtk_widget_get_width(surface)), static_cast<Float>(gtk_widget_get_height(surface))};
|
|
1374
|
+
Size size = window;
|
|
1375
|
+
auto node = sheetNodes_ ? sheetNodes_(p.surfaceId, tag) : std::nullopt;
|
|
1376
|
+
if (p.presentation == ModalPresentation::Cover) {
|
|
1377
|
+
// Below the header bar, which the window's content may run under: the cover starts where the
|
|
1378
|
+
// bar ends.
|
|
1379
|
+
int top = coverTop(surface);
|
|
1380
|
+
gtk_widget_set_margin_top(p.layer, top);
|
|
1381
|
+
size.height = std::max<Float>(0, window.height - static_cast<Float>(top));
|
|
1382
|
+
} else if (p.presentation == ModalPresentation::Sheet) {
|
|
1383
|
+
// The content at the core's size, below the header, which the sheet stays within the window with.
|
|
1384
|
+
Float header = p.header ? static_cast<Float>(naturalHeight(p.header)) : 0;
|
|
1385
|
+
size = sheetSize(node.value_or(SheetNode{}), window);
|
|
1386
|
+
size.height = std::max<Float>(0, std::min(size.height + header, window.height) - header);
|
|
1387
|
+
} else if (p.presentation == ModalPresentation::Popover) {
|
|
1388
|
+
// Its content's size once that has laid out; until then as much as a popover takes.
|
|
1389
|
+
Size measured = node ? popoverSize(*node, window) : Size{0, 0};
|
|
1390
|
+
size = measured.width >= 1 && measured.height >= 1
|
|
1391
|
+
? measured
|
|
1392
|
+
: Size{std::min(window.width, kPopoverMax), std::min(window.height, kPopoverMax)};
|
|
1393
|
+
}
|
|
1394
|
+
std::pair<int, int> pixels{static_cast<int>(std::lround(size.width)), static_cast<int>(std::lround(size.height))};
|
|
1395
|
+
if (pixels == p.size) {
|
|
1396
|
+
return;
|
|
1397
|
+
}
|
|
1398
|
+
p.size = pixels;
|
|
1399
|
+
if (p.presentation == ModalPresentation::Cover) {
|
|
1400
|
+
// A cover fills the overlay, whatever its size.
|
|
1401
|
+
setSlotSize(p.slot, std::nullopt);
|
|
1402
|
+
} else {
|
|
1403
|
+
setSlotSize(p.slot, pixels);
|
|
1404
|
+
}
|
|
1405
|
+
if (p.dialog) {
|
|
1406
|
+
int header = p.header ? naturalHeight(p.header) : 0;
|
|
1407
|
+
g_object_set(p.dialog, "content-width", pixels.first, "content-height", pixels.second + header, nullptr);
|
|
1408
|
+
}
|
|
1409
|
+
if (auto state = std::dynamic_pointer_cast<const ConcreteState<ModalHostViewState>>(d->state)) {
|
|
1410
|
+
state->updateState(ModalHostViewState(size));
|
|
1411
|
+
} else if (auto pane = std::dynamic_pointer_cast<const ConcreteState<SplitPaneState>>(d->state)) {
|
|
1412
|
+
if (pane->getData().size != size) {
|
|
1413
|
+
pane->updateState(SplitPaneState{.size = size});
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
void Modals::dismiss(Tag tag) {
|
|
1419
|
+
auto it = presented_.find(tag);
|
|
1420
|
+
if (it == presented_.end()) {
|
|
1421
|
+
return;
|
|
1422
|
+
}
|
|
1423
|
+
Presented p = it->second;
|
|
1424
|
+
presented_.erase(it);
|
|
1425
|
+
backend_.unhostView(tag);
|
|
1426
|
+
backend_.hooks.detachInput(tag);
|
|
1427
|
+
backend_.hooks.containerRoot(tag, false);
|
|
1428
|
+
if (p.dialog) {
|
|
1429
|
+
adwFunction<void (*)(GtkWidget *)>("adw_dialog_force_close")(p.dialog);
|
|
1430
|
+
g_object_unref(p.dialog);
|
|
1431
|
+
}
|
|
1432
|
+
if (p.popover) {
|
|
1433
|
+
gtk_popover_popdown(GTK_POPOVER(p.popover));
|
|
1434
|
+
if (gtk_widget_get_parent(p.popover)) {
|
|
1435
|
+
gtk_widget_unparent(p.popover);
|
|
1436
|
+
}
|
|
1437
|
+
g_object_unref(p.popover);
|
|
1438
|
+
}
|
|
1439
|
+
if (p.layer) {
|
|
1440
|
+
if (const WindowChrome *chrome = chromeOf(backend_.surfaceWidget(p.surfaceId)); chrome && gtk_widget_get_parent(p.layer)) {
|
|
1441
|
+
gtk_overlay_remove_overlay(GTK_OVERLAY(chrome->overlay), p.layer);
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
if (p.header) {
|
|
1445
|
+
g_object_unref(p.header);
|
|
1446
|
+
}
|
|
1447
|
+
if (p.card) {
|
|
1448
|
+
g_object_unref(p.card);
|
|
1449
|
+
}
|
|
1450
|
+
g_object_unref(p.slot);
|
|
1451
|
+
// Before the focus goes back, which a held window would refuse.
|
|
1452
|
+
hold(p.surfaceId);
|
|
1453
|
+
if (p.opener) {
|
|
1454
|
+
if (gtk_widget_get_root(p.opener)) {
|
|
1455
|
+
gtk_widget_grab_focus(p.opener);
|
|
1456
|
+
}
|
|
1457
|
+
g_object_unref(p.opener);
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
} // namespace ngn::gtk
|