@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,1092 @@
|
|
|
1
|
+
// ngn-linux: the desktop host on GTK 4. Loads a bundle into one runtime and starts the app, composed
|
|
2
|
+
// as every desktop host is (makeHostViews, addCoreModules and addDesktopModules) over Linux's
|
|
3
|
+
// HostOs. The app's windows each have a surface (app-windows.h); the app's own surface gets a
|
|
4
|
+
// window only when the app renders into it.
|
|
5
|
+
//
|
|
6
|
+
// ngn-linux <bundle> [--module <name>] [--title <title>] [--app-id <id>]
|
|
7
|
+
// ngn-linux (a packaged app: what the metadata.json beside it names; argv and env
|
|
8
|
+
// are ignored)
|
|
9
|
+
//
|
|
10
|
+
// NGN_CAPTURE=<file.png> saves the first window's content, drawn in-process (capture.h), a second
|
|
11
|
+
// after its first frame, or with an input script (NGN_INPUT_SCRIPT) a second after the script has
|
|
12
|
+
// played.
|
|
13
|
+
#include "accessibility-settings.h"
|
|
14
|
+
#include "animation-frames.h"
|
|
15
|
+
#include "app-info.h"
|
|
16
|
+
#include "app-windows.h"
|
|
17
|
+
#if NGN_GSTREAMER
|
|
18
|
+
#include "audio.h"
|
|
19
|
+
#include "camera.h"
|
|
20
|
+
#include "video.h"
|
|
21
|
+
#endif
|
|
22
|
+
#include "auth-session.h"
|
|
23
|
+
#include "battery.h"
|
|
24
|
+
#include "capture.h"
|
|
25
|
+
#include "clock-format.h"
|
|
26
|
+
#include "crypto.h"
|
|
27
|
+
#include "file-dialogs.h"
|
|
28
|
+
#include "file-system.h"
|
|
29
|
+
#include "focus.h"
|
|
30
|
+
#include "fonts.h"
|
|
31
|
+
#include "gtk-backend.h"
|
|
32
|
+
#include "host-sources.h"
|
|
33
|
+
#include "image-manipulator.h"
|
|
34
|
+
#include "image-view.h"
|
|
35
|
+
#include "input-replay.h"
|
|
36
|
+
#include "launcher-badge.h"
|
|
37
|
+
#include "locales.h"
|
|
38
|
+
#include "location.h"
|
|
39
|
+
#include "menus.h"
|
|
40
|
+
#include "native-controls.h"
|
|
41
|
+
#include "network-state.h"
|
|
42
|
+
#include "notifications.h"
|
|
43
|
+
#include "pango-text.h"
|
|
44
|
+
#include "printing.h"
|
|
45
|
+
#include "proxy-resolver.h"
|
|
46
|
+
#include "scroll-view.h"
|
|
47
|
+
#include "secret-store.h"
|
|
48
|
+
#include "status-notifier.h"
|
|
49
|
+
#include "surface-input.h"
|
|
50
|
+
#include "system-access.h"
|
|
51
|
+
#include "view-widget.h"
|
|
52
|
+
#include "window-scale.h"
|
|
53
|
+
|
|
54
|
+
#include "ngn/core-modules.h"
|
|
55
|
+
#include "ngn/crash-report.h"
|
|
56
|
+
#include "ngn/curl-network.h"
|
|
57
|
+
#include "ngn/desktop-modules.h"
|
|
58
|
+
#include "ngn/dev-server.h"
|
|
59
|
+
#include "ngn/file-system.h"
|
|
60
|
+
#include "ngn/host-os.h"
|
|
61
|
+
#include "accent-colour.h"
|
|
62
|
+
#include "ngn/host-state.h"
|
|
63
|
+
#include "ngn/host-views.h"
|
|
64
|
+
#include "ngn/launch-settings.h"
|
|
65
|
+
#include "ngn/launch-frame.h"
|
|
66
|
+
#include "ngn/layout-animations.h"
|
|
67
|
+
#include "ngn/openssl-update-crypto.h"
|
|
68
|
+
#include "ngn/processes.h"
|
|
69
|
+
#include "ngn/runtime.h"
|
|
70
|
+
|
|
71
|
+
#include <folly/json.h>
|
|
72
|
+
|
|
73
|
+
#include <adwaita.h>
|
|
74
|
+
#include <gtk/gtk.h>
|
|
75
|
+
#include <unistd.h>
|
|
76
|
+
|
|
77
|
+
#include <atomic>
|
|
78
|
+
#include <cstdio>
|
|
79
|
+
#include <map>
|
|
80
|
+
#include <memory>
|
|
81
|
+
#include <mutex>
|
|
82
|
+
#include <set>
|
|
83
|
+
|
|
84
|
+
using facebook::react::Float;
|
|
85
|
+
using facebook::react::SurfaceId;
|
|
86
|
+
|
|
87
|
+
extern char **environ;
|
|
88
|
+
|
|
89
|
+
namespace {
|
|
90
|
+
|
|
91
|
+
constexpr SurfaceId kAppSurface = 1;
|
|
92
|
+
|
|
93
|
+
struct Host {
|
|
94
|
+
std::shared_ptr<ngn::gtk::GtkBackend> backend;
|
|
95
|
+
std::shared_ptr<ngn::MountingManager> mounting;
|
|
96
|
+
std::unique_ptr<ngn::InputDispatcher> input;
|
|
97
|
+
std::unique_ptr<ngn::Runtime> runtime;
|
|
98
|
+
std::shared_ptr<ngn::HostState> state;
|
|
99
|
+
// The desktop's accessibility settings, into the host state as they change.
|
|
100
|
+
std::unique_ptr<ngn::gtk::AccessibilitySettingsWatcher> accessibility;
|
|
101
|
+
std::shared_ptr<ngn::gtk::GtkWindows> windows;
|
|
102
|
+
std::shared_ptr<ngn::MenuHost> menus;
|
|
103
|
+
std::shared_ptr<ngn::gtk::GtkMenus> gtkMenus;
|
|
104
|
+
// The app's undo history, which Edit > Undo and Redo run while no text input has the focus.
|
|
105
|
+
std::shared_ptr<ngn::UndoManager> undo = ngn::gtk::makeUndoManager();
|
|
106
|
+
ngn::HostOs os;
|
|
107
|
+
std::unique_ptr<ngn::gtk::AccentFollower> accent;
|
|
108
|
+
std::string bundle;
|
|
109
|
+
// The app's own surface and the window it gets once the app renders into it.
|
|
110
|
+
GtkWidget *appWindow{nullptr};
|
|
111
|
+
GtkWidget *appSurface{nullptr};
|
|
112
|
+
// Its window once the app renders into it, and once that window is shown, past the splash.
|
|
113
|
+
bool appWindowAsked{false};
|
|
114
|
+
bool appWindowShown{false};
|
|
115
|
+
std::set<SurfaceId> surfaces;
|
|
116
|
+
// What each surface last reported of its size and its chrome's insets.
|
|
117
|
+
std::map<SurfaceId, ngn::ContainerInsets> reported;
|
|
118
|
+
GtkApplication *application{nullptr};
|
|
119
|
+
gint64 launched{0};
|
|
120
|
+
bool firstFrame{false};
|
|
121
|
+
// NGN_LOG_COMMITS: each frame's time is logged too, with each commit's.
|
|
122
|
+
bool logFrames{false};
|
|
123
|
+
bool quit{false};
|
|
124
|
+
guint animationTimer{0};
|
|
125
|
+
// The Expo Updates client (updates.h), and which launch owes its first frame on screen.
|
|
126
|
+
std::shared_ptr<ngn::UpdatesClient> updates;
|
|
127
|
+
ngn::LaunchFrame launchFrame;
|
|
128
|
+
} gHost;
|
|
129
|
+
|
|
130
|
+
// The splash screen (NgNativeSplashScreen): the first windows wait for the app to hide it. The gate
|
|
131
|
+
// is the process's, so a reload, which lets it go, never leaves it behind a window it shows.
|
|
132
|
+
std::shared_ptr<ngn::SplashGate> gSplash = std::make_shared<ngn::SplashGate>();
|
|
133
|
+
|
|
134
|
+
// WebBrowser's sign-in (auth-session.h): the link back to the app ends it, and only it hears that.
|
|
135
|
+
std::shared_ptr<ngn::LinkAuthSession> gAuthSession = ngn::gtk::makeAuthSession();
|
|
136
|
+
|
|
137
|
+
// Runs a window's first show once the splash lets it, on the UI thread: the gate lets go on the
|
|
138
|
+
// JavaScript thread, or on the one a reload ends the module on.
|
|
139
|
+
void showPastSplash(std::function<void()> show) {
|
|
140
|
+
gSplash->show([show = std::move(show)] { ngn::gtk::onMainContext(show); });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// The bundle the runtime runs now, for SourceCode's scriptURL: set at launch and before a reload
|
|
144
|
+
// into an update, read on the JavaScript thread.
|
|
145
|
+
std::mutex gBundleMutex;
|
|
146
|
+
std::string gBundle;
|
|
147
|
+
|
|
148
|
+
void setRunningBundle(const std::string &bundle) {
|
|
149
|
+
std::lock_guard lock(gBundleMutex);
|
|
150
|
+
gBundle = bundle;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// The event beat: React Native's RunLoopObserverManager wants a tick as the UI thread is about to
|
|
154
|
+
// go idle. GLib calls a source's prepare before each poll, which is that moment.
|
|
155
|
+
gboolean beatPrepare(GSource *, gint *timeout) {
|
|
156
|
+
*timeout = -1;
|
|
157
|
+
if (gHost.runtime) {
|
|
158
|
+
gHost.runtime->beat();
|
|
159
|
+
}
|
|
160
|
+
return FALSE;
|
|
161
|
+
}
|
|
162
|
+
GSourceFuncs gBeatFuncs = {beatPrepare, nullptr, nullptr, nullptr, nullptr, nullptr};
|
|
163
|
+
|
|
164
|
+
ngn::SurfaceMetrics metricsFor(int width, int height, double scale) {
|
|
165
|
+
return {.size = {static_cast<Float>(width), static_cast<Float>(height)}, .pointScale = static_cast<float>(scale), .fontScale = ngn::gtk::desktopFontScale()};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
GtkWidget *surfaceWidgetOf(SurfaceId surface) {
|
|
169
|
+
return surface == kAppSurface ? gHost.appSurface : gHost.windows ? gHost.windows->surfaceWidget(surface) : nullptr;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// The text scaling changed: every surface lays out again at it, and Dimensions reports it.
|
|
173
|
+
void followFontScale() {
|
|
174
|
+
float fontScale = ngn::gtk::desktopFontScale();
|
|
175
|
+
auto metrics = gHost.state->metrics.get();
|
|
176
|
+
metrics.window.fontScale = metrics.screen.fontScale = fontScale;
|
|
177
|
+
gHost.state->metrics.set(metrics);
|
|
178
|
+
for (SurfaceId surface : gHost.surfaces) {
|
|
179
|
+
GtkWidget *widget = surfaceWidgetOf(surface);
|
|
180
|
+
if (widget && gHost.runtime) {
|
|
181
|
+
gHost.runtime->updateSurface(
|
|
182
|
+
surface, metricsFor(gtk_widget_get_width(widget), gtk_widget_get_height(widget), ngn::gtk::windowScale(widget)));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// What Dimensions reports: the app window's content size and scale, and its monitor's size (the
|
|
188
|
+
// first monitor's before the window is on one; the window's own size with no monitor at all).
|
|
189
|
+
ngn::WindowMetrics windowMetrics(int width, int height) {
|
|
190
|
+
double scale = ngn::gtk::windowScale(gHost.appWindow);
|
|
191
|
+
double screenWidth = width, screenHeight = height;
|
|
192
|
+
GdkDisplay *display = gtk_widget_get_display(gHost.appWindow);
|
|
193
|
+
GdkSurface *surface = gtk_native_get_surface(GTK_NATIVE(gHost.appWindow));
|
|
194
|
+
GdkMonitor *monitor = surface ? gdk_display_get_monitor_at_surface(display, surface) : nullptr;
|
|
195
|
+
GListModel *monitors = gdk_display_get_monitors(display);
|
|
196
|
+
GdkMonitor *first = !monitor && g_list_model_get_n_items(monitors) > 0 ? GDK_MONITOR(g_list_model_get_item(monitors, 0)) : nullptr;
|
|
197
|
+
if (GdkMonitor *m = monitor ? monitor : first) {
|
|
198
|
+
GdkRectangle geometry;
|
|
199
|
+
gdk_monitor_get_geometry(m, &geometry);
|
|
200
|
+
screenWidth = geometry.width;
|
|
201
|
+
screenHeight = geometry.height;
|
|
202
|
+
}
|
|
203
|
+
if (first) {
|
|
204
|
+
g_object_unref(first);
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
.window = {.width = static_cast<double>(width), .height = static_cast<double>(height), .scale = scale, .fontScale = ngn::gtk::desktopFontScale()},
|
|
208
|
+
.screen = {.width = screenWidth, .height = screenHeight, .scale = scale, .fontScale = ngn::gtk::desktopFontScale()},
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// A dev or test run's input script (NGN_INPUT_SCRIPT), played into the first window once it shows.
|
|
213
|
+
std::string gInputScript;
|
|
214
|
+
|
|
215
|
+
// Whether it plays; `done` runs once it has.
|
|
216
|
+
bool playInputScript(GtkWidget *window, std::function<void()> done) {
|
|
217
|
+
gchar *text = nullptr;
|
|
218
|
+
if (!g_file_get_contents(gInputScript.c_str(), &text, nullptr, nullptr)) {
|
|
219
|
+
fprintf(stderr, "[ngn] input script %s: cannot be read\n", gInputScript.c_str());
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
auto script = ngn::parseInputScript(text);
|
|
223
|
+
g_free(text);
|
|
224
|
+
return ngn::gtk::replayInputScript(window, std::move(script), std::move(done));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// NGN_CAPTURE's screenshot of `surface`, a second from now, unless its window has gone.
|
|
228
|
+
void captureSoon(GtkWidget *surface, std::string file) {
|
|
229
|
+
auto *alive = new GtkWidget *(surface);
|
|
230
|
+
g_object_add_weak_pointer(G_OBJECT(surface), reinterpret_cast<gpointer *>(alive));
|
|
231
|
+
ngn::gtk::runAfter(1, [file, alive] {
|
|
232
|
+
if (*alive) {
|
|
233
|
+
bool saved = ngn::gtk::captureWidget(*alive, file);
|
|
234
|
+
fprintf(stderr, saved ? "[ngn] captured %s\n" : "[ngn] could not capture %s\n", file.c_str());
|
|
235
|
+
g_object_remove_weak_pointer(G_OBJECT(*alive), reinterpret_cast<gpointer *>(alive));
|
|
236
|
+
}
|
|
237
|
+
delete alive;
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// The first window's first frame, once drawn: logged, given the run's input script, and captured
|
|
242
|
+
// when asked, a second after the script has played (or after the frame, without one).
|
|
243
|
+
void firstFrameDrawn(GtkWidget *surface) {
|
|
244
|
+
fprintf(stderr, "[ngn] first frame %.0f ms after launch\n", (g_get_monotonic_time() - gHost.launched) / 1000.0);
|
|
245
|
+
const char *path = getenv("NGN_CAPTURE");
|
|
246
|
+
std::string file = path ? path : "";
|
|
247
|
+
std::function<void()> capture;
|
|
248
|
+
if (!file.empty()) {
|
|
249
|
+
capture = [surface, file] { captureSoon(surface, file); };
|
|
250
|
+
}
|
|
251
|
+
bool playing = !gInputScript.empty() && playInputScript(GTK_WIDGET(gtk_widget_get_root(surface)), capture);
|
|
252
|
+
if (capture && !playing) {
|
|
253
|
+
capture();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
void watchFirstFrame(GtkWidget *window, GtkWidget *surface) {
|
|
258
|
+
GdkFrameClock *clock = gtk_widget_get_frame_clock(window);
|
|
259
|
+
if (!clock || gHost.firstFrame) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
gHost.firstFrame = true;
|
|
263
|
+
g_signal_connect(clock, "after-paint", G_CALLBACK(+[](GdkFrameClock *, gpointer surface) {
|
|
264
|
+
static bool logged = false;
|
|
265
|
+
if (logged) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
logged = true;
|
|
269
|
+
firstFrameDrawn(static_cast<GtkWidget *>(surface));
|
|
270
|
+
}), surface);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// A launch running an update counts once its first frame has been up kLaunchSuccessSeconds
|
|
274
|
+
// (launch-frame.h); until then a crash rolls the update back at the next launch.
|
|
275
|
+
void markLaunchSucceededLater(uint64_t generation) {
|
|
276
|
+
if (!gHost.updates || !gHost.updates->running()) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
// For this launch only: a reload since begins another, with a timer of its own.
|
|
280
|
+
ngn::gtk::runAfter(ngn::kLaunchSuccessSeconds, [generation] { gHost.updates->markLaunchSucceeded(generation); });
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// `window` shows `surface`'s content: the launch's first, mounted by its own JavaScript instance
|
|
284
|
+
// while it is still the launch running, starts the launch's success timer. UI thread.
|
|
285
|
+
void launchFrameShown(SurfaceId surface, GtkWidget *window) {
|
|
286
|
+
const auto *root = gHost.mounting->tree().find(surface);
|
|
287
|
+
if (!gHost.updates || !window || !gtk_widget_get_visible(window) || !root || root->children.empty()) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (auto generation = gHost.launchFrame.shown(gHost.mounting->mountedBy(surface), gHost.updates->launchGeneration())) {
|
|
291
|
+
markLaunchSucceededLater(*generation);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// A reload's bundle (the launch `generation`) loaded into the JavaScript instance `instance`: its
|
|
296
|
+
// launch counts from its first frame on screen, which may already be there or come with a later
|
|
297
|
+
// mount. UI thread.
|
|
298
|
+
void reloadLaunched(uint64_t generation, uint64_t instance) {
|
|
299
|
+
gHost.launchFrame.loaded(generation, instance);
|
|
300
|
+
launchFrameShown(kAppSurface, gHost.appWindowShown ? gHost.appWindow : nullptr);
|
|
301
|
+
for (SurfaceId surface : gHost.surfaces) {
|
|
302
|
+
if (surface != kAppSurface) {
|
|
303
|
+
launchFrameShown(surface, gHost.windows->window(surface));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// The menu's reload: the same launch, in a new instance. Its first frame, if still owed, comes from
|
|
309
|
+
// that instance once the bundle loads; the instance it replaces owes nothing from now.
|
|
310
|
+
void reloadSameLaunch() {
|
|
311
|
+
uint64_t generation = gHost.updates ? gHost.updates->launchGeneration() : 0;
|
|
312
|
+
gHost.launchFrame.reloadBegan();
|
|
313
|
+
gHost.launchFrame.bundleLoading();
|
|
314
|
+
gHost.runtime->reload([generation](bool loaded) {
|
|
315
|
+
gHost.launchFrame.bundleLoadEnded(loaded);
|
|
316
|
+
if (!loaded) {
|
|
317
|
+
fprintf(stderr, "ngn-linux: the reload could not load the bundle\n");
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
uint64_t instance = gHost.mounting->instance();
|
|
321
|
+
gHost.backend->runOnUiThread([generation, instance] { reloadLaunched(generation, instance); });
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// With NGN_LOG_COMMITS, a window's frames, each logged as `[ngn] frame N ms`: the time from the start
|
|
326
|
+
// of the frame clock's update, where animations and layout run, to the end of its paint, which GTK's
|
|
327
|
+
// cairo renderer does in process. `pnpm bench:linux` reads them. The start is kept on the clock, which
|
|
328
|
+
// the window's destruction takes with the handlers.
|
|
329
|
+
void logFrameTimes(GtkWidget *window) {
|
|
330
|
+
GdkFrameClock *clock = window ? gtk_widget_get_frame_clock(window) : nullptr;
|
|
331
|
+
if (!clock || g_object_get_data(G_OBJECT(clock), "ngn-frame-start")) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
g_object_set_data_full(G_OBJECT(clock), "ngn-frame-start", g_new0(gint64, 1), g_free);
|
|
335
|
+
g_signal_connect(clock, "update", G_CALLBACK(+[](GdkFrameClock *clock, gpointer) {
|
|
336
|
+
*static_cast<gint64 *>(g_object_get_data(G_OBJECT(clock), "ngn-frame-start")) = g_get_monotonic_time();
|
|
337
|
+
}), nullptr);
|
|
338
|
+
g_signal_connect(clock, "after-paint", G_CALLBACK(+[](GdkFrameClock *clock, gpointer) {
|
|
339
|
+
gint64 *start = static_cast<gint64 *>(g_object_get_data(G_OBJECT(clock), "ngn-frame-start"));
|
|
340
|
+
if (*start != 0) {
|
|
341
|
+
fprintf(stderr, "[ngn] frame %.1f ms\n", (g_get_monotonic_time() - *start) / 1000.0);
|
|
342
|
+
*start = 0;
|
|
343
|
+
}
|
|
344
|
+
}), nullptr);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// A surface's size and the safe area its window's chrome leaves it (chromeInsets), each time
|
|
348
|
+
// either changes: as it resizes, and as the header bar or the window controls move over content
|
|
349
|
+
// that runs under them.
|
|
350
|
+
void reportContainer(SurfaceId surface) {
|
|
351
|
+
GtkWidget *widget = surfaceWidgetOf(surface);
|
|
352
|
+
if (!widget || !gHost.mounting) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
auto insets = ngn::gtk::chromeInsets(widget);
|
|
356
|
+
if (auto it = gHost.reported.find(surface); it != gHost.reported.end() && it->second == insets) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
gHost.reported[surface] = insets;
|
|
360
|
+
gHost.backend->modals().settle();
|
|
361
|
+
gHost.mounting->setContainerInsets(surface, insets);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// A surface's widget joins the backend and the input dispatcher.
|
|
365
|
+
// The surface whose window `window` is, if any.
|
|
366
|
+
std::optional<SurfaceId> surfaceOfWindow(GtkWindow *window) {
|
|
367
|
+
for (SurfaceId surface : gHost.surfaces) {
|
|
368
|
+
if (GtkWidget *widget = surfaceWidgetOf(surface); widget && gtk_widget_get_root(widget) == GTK_ROOT(window)) {
|
|
369
|
+
return surface;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return std::nullopt;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
void navigate(ngn::NavigationDirection direction, GtkWindow *window) {
|
|
376
|
+
gHost.menus->navigate(direction, window ? surfaceOfWindow(window) : std::nullopt);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// What a text input does itself, by GTK's action for it on the focused widget.
|
|
380
|
+
const char *textAction(ngn::MenuCommand command) {
|
|
381
|
+
switch (command) {
|
|
382
|
+
case ngn::MenuCommand::Undo: return "text.undo";
|
|
383
|
+
case ngn::MenuCommand::Redo: return "text.redo";
|
|
384
|
+
case ngn::MenuCommand::Cut: return "clipboard.cut";
|
|
385
|
+
case ngn::MenuCommand::Copy: return "clipboard.copy";
|
|
386
|
+
case ngn::MenuCommand::Paste:
|
|
387
|
+
case ngn::MenuCommand::PasteAndMatchStyle: return "clipboard.paste";
|
|
388
|
+
case ngn::MenuCommand::Delete: return "selection.delete";
|
|
389
|
+
case ngn::MenuCommand::SelectAll: return "selection.select-all";
|
|
390
|
+
default: return nullptr;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// A menu item chosen, from a menu or by its key, in `window` (the active one when null). The host
|
|
395
|
+
// runs the OS's commands; the rest go to the core (MenuHost::choose), which sends the app's.
|
|
396
|
+
void runMenuItem(const ngn::MenuItem &item, GtkWindow *window) {
|
|
397
|
+
if (!window) {
|
|
398
|
+
window = gtk_application_get_active_window(gHost.application);
|
|
399
|
+
}
|
|
400
|
+
switch (item.command) {
|
|
401
|
+
case ngn::MenuCommand::Reload:
|
|
402
|
+
reloadSameLaunch();
|
|
403
|
+
return;
|
|
404
|
+
case ngn::MenuCommand::OpenDevTools:
|
|
405
|
+
gHost.runtime->openDebugger();
|
|
406
|
+
return;
|
|
407
|
+
case ngn::MenuCommand::Quit:
|
|
408
|
+
gHost.windows->beginQuit();
|
|
409
|
+
return;
|
|
410
|
+
case ngn::MenuCommand::ToggleSidebar:
|
|
411
|
+
// The window's split view (split-view.h); a window without one leaves it to the app.
|
|
412
|
+
if (auto surface = window ? surfaceOfWindow(window) : std::nullopt; surface && gHost.backend->splitViews().toggle(*surface)) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
break;
|
|
416
|
+
case ngn::MenuCommand::CloseWindow:
|
|
417
|
+
// The sheet, cover or popover in front first, as Escape asks it to close; then the window, as
|
|
418
|
+
// the close button does: the app decides (close-request).
|
|
419
|
+
if (auto front = window ? gHost.backend->modals().front(window) : std::nullopt) {
|
|
420
|
+
gHost.backend->modals().requestClose(*front);
|
|
421
|
+
} else if (window) {
|
|
422
|
+
gtk_window_close(window);
|
|
423
|
+
}
|
|
424
|
+
return;
|
|
425
|
+
case ngn::MenuCommand::ToggleFullScreen:
|
|
426
|
+
if (window) {
|
|
427
|
+
gtk_window_is_fullscreen(window) ? gtk_window_unfullscreen(window) : gtk_window_fullscreen(window);
|
|
428
|
+
}
|
|
429
|
+
return;
|
|
430
|
+
case ngn::MenuCommand::KeyboardShortcuts:
|
|
431
|
+
ngn::gtk::presentKeyboardShortcuts(
|
|
432
|
+
ngn::gtk::keyboardShortcutsWindow(gHost.menus->keyboardShortcuts(gHost.gtkMenus->bar()), window), window);
|
|
433
|
+
return;
|
|
434
|
+
case ngn::MenuCommand::About: {
|
|
435
|
+
GtkWidget *about = adw_about_window_new();
|
|
436
|
+
adw_about_window_set_application_name(ADW_ABOUT_WINDOW(about), gHost.menus->options().appName.c_str());
|
|
437
|
+
if (const char *id = g_application_get_application_id(G_APPLICATION(gHost.application))) {
|
|
438
|
+
adw_about_window_set_application_icon(ADW_ABOUT_WINDOW(about), id);
|
|
439
|
+
}
|
|
440
|
+
if (window) {
|
|
441
|
+
gtk_window_set_transient_for(GTK_WINDOW(about), window);
|
|
442
|
+
}
|
|
443
|
+
gtk_window_present(GTK_WINDOW(about));
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
default:
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
// Undo and Redo are a focused text input's own, else the app's, as on macOS.
|
|
450
|
+
bool history = item.command == ngn::MenuCommand::Undo || item.command == ngn::MenuCommand::Redo;
|
|
451
|
+
if (history && !(window && ngn::gtk::textInputFocused(window))) {
|
|
452
|
+
item.command == ngn::MenuCommand::Undo ? gHost.undo->undo() : gHost.undo->redo();
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
if (const char *action = textAction(item.command)) {
|
|
456
|
+
if (GtkWidget *focus = window ? gtk_root_get_focus(GTK_ROOT(window)) : nullptr) {
|
|
457
|
+
gtk_widget_activate_action(focus, action, nullptr);
|
|
458
|
+
}
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
gHost.menus->choose(item, window ? surfaceOfWindow(window) : std::nullopt);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// A window holding the app's surfaces: its menus, and its keys and side buttons routed as
|
|
465
|
+
// menus.h's KeyRouting orders them.
|
|
466
|
+
void routeWindow(GtkWidget *surfaceWidget) {
|
|
467
|
+
auto *window = GTK_WINDOW(gtk_widget_get_root(surfaceWidget));
|
|
468
|
+
if (!window || g_object_get_data(G_OBJECT(window), "ngn-keys")) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
g_object_set_data(G_OBJECT(window), "ngn-keys", GINT_TO_POINTER(1));
|
|
472
|
+
ngn::gtk::trackFocus(window);
|
|
473
|
+
gHost.gtkMenus->attach(surfaceWidget);
|
|
474
|
+
ngn::gtk::routeKeys(window, std::make_shared<ngn::gtk::KeyRouting>(ngn::gtk::KeyRouting{
|
|
475
|
+
.shortcut = [](const ngn::KeyChord &chord, bool text) { return gHost.menus->keyDown(chord, text); },
|
|
476
|
+
.focus = [](GtkWindow *window, guint keyval, GdkModifierType state) {
|
|
477
|
+
return ngn::gtk::moveFocusByKey(window, keyval, state, *gHost.backend, gHost.mounting->tree());
|
|
478
|
+
},
|
|
479
|
+
.navigation = [](const ngn::KeyChord &chord) { return ngn::navigationFor(chord, gHost.os); },
|
|
480
|
+
.editingKey = [](const ngn::KeyChord &chord) { return ngn::editingChord(chord, gHost.os); },
|
|
481
|
+
.navigate = navigate,
|
|
482
|
+
.menuKey = [](const ngn::KeyChord &chord) { return ngn::findMenuItem(gHost.gtkMenus->bar(), chord); },
|
|
483
|
+
.choose = runMenuItem,
|
|
484
|
+
}));
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
void addSurface(SurfaceId surface, GtkWidget *widget) {
|
|
488
|
+
gHost.backend->addSurface(surface, widget);
|
|
489
|
+
ngn::gtk::attachPointerInput(widget, surface, surface, gHost.input.get());
|
|
490
|
+
gHost.input->addContainer(surface, surface, surface);
|
|
491
|
+
gHost.surfaces.insert(surface);
|
|
492
|
+
if (gHost.gtkMenus) {
|
|
493
|
+
routeWindow(widget);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// The app's own surface gets its window once the app renders into it.
|
|
498
|
+
void showAppSurfaceIfUsed() {
|
|
499
|
+
const auto *root = gHost.mounting->tree().find(kAppSurface);
|
|
500
|
+
if (gHost.appWindowAsked || !root || root->children.empty()) {
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
gHost.appWindowAsked = true;
|
|
504
|
+
showPastSplash([] {
|
|
505
|
+
gHost.appWindowShown = true;
|
|
506
|
+
gtk_window_present(GTK_WINDOW(gHost.appWindow));
|
|
507
|
+
watchFirstFrame(gHost.appWindow, gHost.appSurface);
|
|
508
|
+
if (gHost.logFrames) {
|
|
509
|
+
logFrameTimes(gHost.appWindow);
|
|
510
|
+
}
|
|
511
|
+
// Past the splash, which may be after the app's last mount: its launch's first frame is up now.
|
|
512
|
+
launchFrameShown(kAppSurface, gHost.appWindow);
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// A second launch or the bus asks for the app: the window the app draws into without AppWindows,
|
|
517
|
+
// when it has one, else the app's windows (GtkWindows::raise).
|
|
518
|
+
void raiseApp() {
|
|
519
|
+
if (gHost.appWindowShown) {
|
|
520
|
+
gtk_window_present(GTK_WINDOW(gHost.appWindow));
|
|
521
|
+
} else if (gHost.appWindowAsked) {
|
|
522
|
+
// Held by the splash screen: it shows, in front, once the app hides that.
|
|
523
|
+
} else {
|
|
524
|
+
gHost.windows->raise();
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
std::string joined(const std::string &a, const std::string &b) {
|
|
529
|
+
gchar *path = g_build_filename(a.c_str(), b.c_str(), nullptr);
|
|
530
|
+
std::string out = path;
|
|
531
|
+
g_free(path);
|
|
532
|
+
return out;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
std::string directoryOf(const std::string &path) {
|
|
536
|
+
gchar *dir = g_path_get_dirname(path.c_str());
|
|
537
|
+
std::string out = dir;
|
|
538
|
+
g_free(dir);
|
|
539
|
+
return out;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
} // namespace
|
|
543
|
+
|
|
544
|
+
int main(int argc, char *argv[]) {
|
|
545
|
+
gHost.launched = g_get_monotonic_time();
|
|
546
|
+
ngn::HostOs os = ngn::linuxOs();
|
|
547
|
+
std::vector<std::string> args(argv + 1, argv + argc);
|
|
548
|
+
// A package (bin/package-linux.mjs) puts metadata.json beside the host, which links from bin/ to
|
|
549
|
+
// lib/<name>/: the folder of the executable itself, not of the link.
|
|
550
|
+
std::string folder;
|
|
551
|
+
if (gchar *self = g_file_read_link("/proc/self/exe", nullptr)) {
|
|
552
|
+
folder = directoryOf(self);
|
|
553
|
+
g_free(self);
|
|
554
|
+
}
|
|
555
|
+
auto metadata = folder.empty() ? std::variant<ngn::LaunchSources::Lookup, std::string>() : ngn::packagedMetadata(folder);
|
|
556
|
+
if (auto *problem = std::get_if<std::string>(&metadata)) {
|
|
557
|
+
fprintf(stderr, "ngn-linux: %s\n", problem->c_str());
|
|
558
|
+
return 2;
|
|
559
|
+
}
|
|
560
|
+
auto launch = ngn::launchSettings(
|
|
561
|
+
{
|
|
562
|
+
.args = args,
|
|
563
|
+
.env = [](const std::string &name) -> std::optional<std::string> {
|
|
564
|
+
const char *value = getenv(name.c_str());
|
|
565
|
+
return value ? std::optional<std::string>(value) : std::nullopt;
|
|
566
|
+
},
|
|
567
|
+
.plist = std::get<ngn::LaunchSources::Lookup>(metadata),
|
|
568
|
+
.resourcePath = folder,
|
|
569
|
+
},
|
|
570
|
+
os);
|
|
571
|
+
if (auto *usage = std::get_if<std::string>(&launch)) {
|
|
572
|
+
fprintf(stderr, "%s\n", usage->c_str());
|
|
573
|
+
return 2;
|
|
574
|
+
}
|
|
575
|
+
static const auto settings = std::get<ngn::LaunchSettings>(launch);
|
|
576
|
+
std::string bundle = settings.bundle;
|
|
577
|
+
gHost.bundle = bundle;
|
|
578
|
+
gHost.os = os;
|
|
579
|
+
std::string appId = settings.appId.empty() ? settings.title : settings.appId;
|
|
580
|
+
ngn::installCrashReport(
|
|
581
|
+
ngn::crashReportPath(
|
|
582
|
+
appId, settings.test ? std::optional<std::string>(settings.test->stateDir) : std::nullopt,
|
|
583
|
+
[](const std::string &name) -> std::optional<std::string> {
|
|
584
|
+
const char *value = getenv(name.c_str());
|
|
585
|
+
return value ? std::optional<std::string>(value) : std::nullopt;
|
|
586
|
+
}),
|
|
587
|
+
os.program);
|
|
588
|
+
// As on macOS, only a run from a dev server plays one, and a test launch, which screens-check's
|
|
589
|
+
// Linux mode makes on an exported bundle.
|
|
590
|
+
if (!settings.inputScript.empty() && (settings.test || ngn::DevServer::fromBundleUrl(bundle))) {
|
|
591
|
+
gInputScript = settings.inputScript;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
gtk_init();
|
|
595
|
+
adw_init();
|
|
596
|
+
// The desktop's accent, which libadwaita below 1.6 does not follow itself.
|
|
597
|
+
gHost.accent = std::make_unique<ngn::gtk::AccentFollower>();
|
|
598
|
+
// Back and forward swap arrows in a right-to-left locale, which GTK knows once it is initialised.
|
|
599
|
+
os = ngn::linuxOs(gtk_widget_get_default_direction() == GTK_TEXT_DIR_RTL);
|
|
600
|
+
gHost.os = os;
|
|
601
|
+
// One instance of the app, as a Linux app has one: a test launch is its own, beside any other.
|
|
602
|
+
gHost.application = ngn::gtk::startApplication(appId, !settings.test, settings.dbusService);
|
|
603
|
+
if (g_application_get_is_remote(G_APPLICATION(gHost.application))) {
|
|
604
|
+
auto args = settings.openArgs;
|
|
605
|
+
if (!settings.initialUrl.empty()) {
|
|
606
|
+
args.push_back(settings.initialUrl);
|
|
607
|
+
}
|
|
608
|
+
ngn::gtk::forwardLaunch(gHost.application, args);
|
|
609
|
+
fprintf(stderr, "[ngn] %s is already running: this launch went to it\n", appId.c_str());
|
|
610
|
+
return 0;
|
|
611
|
+
}
|
|
612
|
+
// GNOME's 12 or 24-hour clock, which the date pickers' time follows.
|
|
613
|
+
static ngn::gtk::ClockFormatWatcher clockFormat(ngn::gtk::useClockFormat);
|
|
614
|
+
const char *desktop = getenv("XDG_CURRENT_DESKTOP");
|
|
615
|
+
const auto menuPlacement = ngn::linuxMenuPlacement(desktop ? desktop : "");
|
|
616
|
+
|
|
617
|
+
gHost.backend = std::make_shared<ngn::gtk::GtkBackend>();
|
|
618
|
+
// The mounting manager, the input dispatcher and the gesture recognisers over the backend, the
|
|
619
|
+
// recognisers' timers on GLib's.
|
|
620
|
+
auto views = ngn::makeHostViews(gHost.backend, gHost.backend, [](double ms, std::function<void()> fn) -> std::function<void()> {
|
|
621
|
+
auto cancelled = std::make_shared<std::atomic<bool>>(false);
|
|
622
|
+
ngn::gtk::runAfter(ms / 1000, [cancelled, fn = std::move(fn)] {
|
|
623
|
+
if (!*cancelled) {
|
|
624
|
+
fn();
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
return [cancelled] { *cancelled = true; };
|
|
628
|
+
});
|
|
629
|
+
gHost.mounting = views.mounting;
|
|
630
|
+
gHost.input = std::move(views.input);
|
|
631
|
+
ngn::gtk::installWorkletHost(gHost.mounting, gHost.backend);
|
|
632
|
+
gHost.backend->hooks = {
|
|
633
|
+
.containerRoot =
|
|
634
|
+
[](facebook::react::Tag tag, bool root) {
|
|
635
|
+
if (!gHost.mounting) {
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
if (root) {
|
|
639
|
+
gHost.mounting->tree().addContainerRoot(tag);
|
|
640
|
+
} else {
|
|
641
|
+
gHost.mounting->tree().removeContainerRoot(tag);
|
|
642
|
+
}
|
|
643
|
+
},
|
|
644
|
+
.attachInput =
|
|
645
|
+
[](GtkWidget *slot, SurfaceId surface, facebook::react::Tag tag) {
|
|
646
|
+
ngn::gtk::attachPointerInput(slot, surface, static_cast<ngn::ContainerId>(tag), gHost.input.get());
|
|
647
|
+
gHost.input->addContainer(static_cast<ngn::ContainerId>(tag), surface, tag);
|
|
648
|
+
},
|
|
649
|
+
.detachInput =
|
|
650
|
+
[](facebook::react::Tag tag) {
|
|
651
|
+
if (gHost.input) {
|
|
652
|
+
gHost.input->removeContainer(static_cast<ngn::ContainerId>(tag));
|
|
653
|
+
}
|
|
654
|
+
},
|
|
655
|
+
.activate = [](SurfaceId surface, facebook::react::Tag tag) { return gHost.input && gHost.input->activate(surface, tag); },
|
|
656
|
+
.chooseTab = [](SurfaceId surface, facebook::react::Tag host, facebook::react::Tag screen) { gHost.mounting->chooseTab(surface, host, screen); },
|
|
657
|
+
};
|
|
658
|
+
gHost.backend->modals().setSheetNodes([](SurfaceId surface, facebook::react::Tag tag) -> std::optional<ngn::SheetNode> {
|
|
659
|
+
return gHost.runtime ? gHost.runtime->sheetNode(surface, tag) : std::nullopt;
|
|
660
|
+
});
|
|
661
|
+
gHost.logFrames = settings.logCommits;
|
|
662
|
+
if (settings.logCommits) {
|
|
663
|
+
gHost.mounting->onCommit = [](SurfaceId, double ms) { fprintf(stderr, "[ngn] commit %.1f ms\n", ms); };
|
|
664
|
+
}
|
|
665
|
+
gHost.backend->onMount = [](SurfaceId surfaceId) {
|
|
666
|
+
gHost.input->didMount(surfaceId);
|
|
667
|
+
if (surfaceId == kAppSurface) {
|
|
668
|
+
showAppSurfaceIfUsed();
|
|
669
|
+
launchFrameShown(surfaceId, gHost.appWindowShown ? gHost.appWindow : nullptr);
|
|
670
|
+
} else {
|
|
671
|
+
gHost.windows->didMount(surfaceId);
|
|
672
|
+
if (GtkWidget *window = gHost.windows->window(surfaceId)) {
|
|
673
|
+
launchFrameShown(surfaceId, window);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
// The cursor the views under the pointer ask for, on each surface: only the one under the
|
|
678
|
+
// pointer shows it.
|
|
679
|
+
gHost.input->onCursor = [](ngn::Cursor cursor) {
|
|
680
|
+
for (SurfaceId surface : gHost.surfaces) {
|
|
681
|
+
if (GtkWidget *widget = surfaceWidgetOf(surface)) {
|
|
682
|
+
GdkCursor *shown = ngn::gtk::makeCursor(cursor);
|
|
683
|
+
gtk_widget_set_cursor(widget, shown);
|
|
684
|
+
g_clear_object(&shown);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
// A press that became a drag out of a `[dragItem]` view: the move that crossed the threshold
|
|
689
|
+
// starts the drag, which has the pointer from there.
|
|
690
|
+
gHost.input->onDragOut = [](facebook::react::Tag tag, const ngn::PointerInput &, const ngn::DragOutItem &item) {
|
|
691
|
+
ngn::gtk::dragOutOfPointer(gHost.backend->view(tag), item);
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
int width = static_cast<int>(ngn::kDefaultWindowWidth), height = static_cast<int>(ngn::kDefaultWindowHeight);
|
|
695
|
+
gHost.appWindow = adw_application_window_new(gHost.application);
|
|
696
|
+
gtk_window_set_title(GTK_WINDOW(gHost.appWindow), settings.title.c_str());
|
|
697
|
+
gtk_window_set_default_size(GTK_WINDOW(gHost.appWindow), width, height);
|
|
698
|
+
gHost.appSurface = ngn::gtk::makeSurfaceWidget(kAppSurface, [](int w, int h) {
|
|
699
|
+
reportContainer(kAppSurface);
|
|
700
|
+
// Each allocation, a new size or a new scale: Dimensions follows the window, as on macOS.
|
|
701
|
+
if (gHost.state) {
|
|
702
|
+
gHost.state->metrics.set(windowMetrics(w, h));
|
|
703
|
+
}
|
|
704
|
+
if (gHost.runtime) {
|
|
705
|
+
gHost.runtime->updateSurface(kAppSurface, metricsFor(w, h, ngn::gtk::windowScale(gHost.appWindow)));
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
adw_application_window_set_content(ADW_APPLICATION_WINDOW(gHost.appWindow), ngn::gtk::wrapInChrome(gHost.appSurface, {.menus = menuPlacement}));
|
|
709
|
+
addSurface(kAppSurface, gHost.appSurface);
|
|
710
|
+
g_signal_connect(gHost.appWindow, "close-request", G_CALLBACK(+[](GtkWindow *, gpointer) -> gboolean {
|
|
711
|
+
gHost.quit = true;
|
|
712
|
+
return FALSE;
|
|
713
|
+
}), nullptr);
|
|
714
|
+
gtk_widget_realize(gHost.appWindow);
|
|
715
|
+
ngn::gtk::afterLayout(gHost.appWindow, [] { reportContainer(kAppSurface); });
|
|
716
|
+
// A move to a monitor of another scale, or a new fractional scale, changes no size, so the
|
|
717
|
+
// surface would not hear of it: the layout rounds to the new pixels, and Dimensions and
|
|
718
|
+
// PixelRatio report them.
|
|
719
|
+
ngn::gtk::followWindowScale(gHost.appWindow, [] {
|
|
720
|
+
int w = gtk_widget_get_width(gHost.appSurface), h = gtk_widget_get_height(gHost.appSurface);
|
|
721
|
+
if (gHost.state) {
|
|
722
|
+
gHost.state->metrics.set(windowMetrics(w, h));
|
|
723
|
+
}
|
|
724
|
+
if (gHost.runtime) {
|
|
725
|
+
gHost.runtime->updateSurface(kAppSurface, metricsFor(w, h, ngn::gtk::windowScale(gHost.appWindow)));
|
|
726
|
+
}
|
|
727
|
+
gHost.backend->windowRescaled(gHost.appWindow);
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
// The app's folders: a test launch's own, else the XDG ones named for the app.
|
|
731
|
+
std::string appData = settings.test ? settings.test->applicationSupport() : joined(g_get_user_data_dir(), appId);
|
|
732
|
+
std::string caches = settings.test ? settings.test->caches() : joined(g_get_user_cache_dir(), appId);
|
|
733
|
+
g_mkdir_with_parents(caches.c_str(), 0700);
|
|
734
|
+
// Another app's copy reaches Clipboard's listeners once its text has been read.
|
|
735
|
+
auto system = ngn::gtk::makeSystem(
|
|
736
|
+
appId, appData,
|
|
737
|
+
[](int64_t count) {
|
|
738
|
+
if (gHost.state) {
|
|
739
|
+
gHost.state->clipboardChangeCount.set(count);
|
|
740
|
+
}
|
|
741
|
+
},
|
|
742
|
+
// Appearance and prefers-color-scheme follow the desktop's scheme as it changes.
|
|
743
|
+
[](const std::string &scheme) {
|
|
744
|
+
if (gHost.state) {
|
|
745
|
+
gHost.state->colorScheme.set(scheme);
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
// A sidebar's collapsed state autosaves among the app's preferences, as window frames do.
|
|
749
|
+
gHost.backend->splitViews().readPreference = [system](const std::string &key) { return system->preference(key); };
|
|
750
|
+
gHost.backend->splitViews().writePreference = [system](const std::string &key, const folly::dynamic &value) {
|
|
751
|
+
system->setPreference(key, value);
|
|
752
|
+
};
|
|
753
|
+
// The state and the watcher of the desktop's accessibility settings, which only the running app has.
|
|
754
|
+
auto watched = ngn::gtk::makeWatchedHostState();
|
|
755
|
+
gHost.state = std::move(watched.state);
|
|
756
|
+
gHost.accessibility = std::move(watched.accessibility);
|
|
757
|
+
gHost.state->metrics.set(windowMetrics(width, height));
|
|
758
|
+
// The scheme the app starts in: the desktop's, which HostState would otherwise leave light.
|
|
759
|
+
gHost.state->colorScheme.set(system->colorScheme());
|
|
760
|
+
if (!settings.initialUrl.empty()) {
|
|
761
|
+
gHost.state->setInitialUrl(settings.initialUrl);
|
|
762
|
+
}
|
|
763
|
+
// The links and files a launcher opened the app on: the first is its initial URL.
|
|
764
|
+
for (const auto &url : ngn::gtk::openUrls(settings.openArgs)) {
|
|
765
|
+
gHost.state->receiveUrl(url, false);
|
|
766
|
+
}
|
|
767
|
+
// Cookies last between launches, in the app's data folder, as macOS keeps them. A proxy comes
|
|
768
|
+
// from the desktop's settings unless libcurl's environment variables name one.
|
|
769
|
+
auto curl = ngn::makeCurlNetwork({
|
|
770
|
+
.cookieFile = joined(appData, "cookies.txt"),
|
|
771
|
+
.desktopProxies = [](const std::string &url) { return ngn::gtk::desktopProxies(url); },
|
|
772
|
+
.appName = settings.title,
|
|
773
|
+
.appVersion = settings.setting("CFBundleShortVersionString", "NGN_APP_VERSION"),
|
|
774
|
+
});
|
|
775
|
+
ngn::NetworkClients network{curl.http, curl.webSockets};
|
|
776
|
+
// Images load through the same client, and are kept in memory for the process.
|
|
777
|
+
ngn::gtk::useImageLoading(network.http, std::make_shared<ngn::ImageCache>("", 64 << 20));
|
|
778
|
+
ngn::gtk::onScrollOffset([](facebook::react::Tag tag, facebook::react::Point offset) {
|
|
779
|
+
gHost.mounting->tree().setScrollOffset(tag, offset);
|
|
780
|
+
});
|
|
781
|
+
|
|
782
|
+
static ngn::ProcessesSetup processes = ngn::gtk::processesSetup(os, settings.capabilitiesJson, ngn::gtk::insideFlatpak());
|
|
783
|
+
auto folders = ngn::gtk::fileFolders(appData, caches, settings.test ? settings.test->temp() : g_get_tmp_dir());
|
|
784
|
+
if (auto path = ngn::pathOfUri(bundle)) {
|
|
785
|
+
// An exported bundle on disk: its assets are beside it.
|
|
786
|
+
folders.bundle = directoryOf(*path);
|
|
787
|
+
}
|
|
788
|
+
static auto fileAccess = std::make_shared<ngn::FileAccess>(folders, processes.capabilities.paths);
|
|
789
|
+
ngn::useViewFileAccess(fileAccess);
|
|
790
|
+
|
|
791
|
+
// Kept for the process: ReactHost asks it for modules on every reload.
|
|
792
|
+
static auto modules = std::make_shared<ngn::ModuleRegistry>();
|
|
793
|
+
ngn::addCoreModules(
|
|
794
|
+
*modules,
|
|
795
|
+
{.system = system,
|
|
796
|
+
.state = gHost.state,
|
|
797
|
+
.http = network.http,
|
|
798
|
+
.webSockets = network.webSockets,
|
|
799
|
+
.files = fileAccess,
|
|
800
|
+
.reactNativeVersion = NGN_REACT_NATIVE_VERSION,
|
|
801
|
+
.bundle = [] {
|
|
802
|
+
std::lock_guard lock(gBundleMutex);
|
|
803
|
+
return gBundle;
|
|
804
|
+
},
|
|
805
|
+
.fakeSystemUI = settings.test && settings.test->fakeSystemUI,
|
|
806
|
+
.gestures = views.gestures,
|
|
807
|
+
.runOnUiThread = [](std::function<void()> task) { gHost.backend->runOnUiThread(std::move(task)); }});
|
|
808
|
+
|
|
809
|
+
gHost.windows = std::make_shared<ngn::gtk::GtkWindows>(ngn::gtk::GtkWindows::Hooks{
|
|
810
|
+
.addSurface = addSurface,
|
|
811
|
+
.startSurface = [](SurfaceId surface, int w, int h, double scale) { gHost.runtime->startSurface(surface, "", metricsFor(w, h, scale)); },
|
|
812
|
+
.resizeSurface =
|
|
813
|
+
[](SurfaceId surface, int w, int h, double scale) {
|
|
814
|
+
reportContainer(surface);
|
|
815
|
+
if (gHost.runtime) {
|
|
816
|
+
gHost.runtime->updateSurface(surface, metricsFor(w, h, scale));
|
|
817
|
+
}
|
|
818
|
+
},
|
|
819
|
+
.rescaled = [](GtkWidget *window) { gHost.backend->windowRescaled(window); },
|
|
820
|
+
.stopSurface =
|
|
821
|
+
[](SurfaceId surface) {
|
|
822
|
+
if (gHost.menus) {
|
|
823
|
+
gHost.menus->forgetHistory(surface);
|
|
824
|
+
}
|
|
825
|
+
gHost.runtime->stopSurface(surface);
|
|
826
|
+
gHost.mounting->removeContainerInsets(surface);
|
|
827
|
+
gHost.reported.erase(surface);
|
|
828
|
+
gHost.input->removeContainer(surface);
|
|
829
|
+
gHost.backend->removeSurface(surface);
|
|
830
|
+
gHost.surfaces.erase(surface);
|
|
831
|
+
},
|
|
832
|
+
.laidOut = reportContainer,
|
|
833
|
+
.quit = [] { gHost.quit = true; },
|
|
834
|
+
// The app's own window, once the app draws into it (shown, or held by the splash), keeps the app
|
|
835
|
+
// running; closing it quits.
|
|
836
|
+
.lastWindowClosed =
|
|
837
|
+
[] {
|
|
838
|
+
if (!gHost.appWindowAsked) {
|
|
839
|
+
gHost.quit = true;
|
|
840
|
+
}
|
|
841
|
+
},
|
|
842
|
+
.whenShown = showPastSplash,
|
|
843
|
+
// A window just shown, at its mount or once the splash let it. The first frame is the first
|
|
844
|
+
// window that draws, which a splash screen or a close can keep from being the first one
|
|
845
|
+
// mounted; and a launch's first frame counts from here, which may be after its last mount.
|
|
846
|
+
.presented =
|
|
847
|
+
[](SurfaceId surface) {
|
|
848
|
+
if (GtkWidget *window = gHost.windows->window(surface)) {
|
|
849
|
+
watchFirstFrame(window, gHost.windows->surfaceWidget(surface));
|
|
850
|
+
if (gHost.logFrames) {
|
|
851
|
+
logFrameTimes(window);
|
|
852
|
+
}
|
|
853
|
+
launchFrameShown(surface, window);
|
|
854
|
+
}
|
|
855
|
+
},
|
|
856
|
+
}, gHost.application, menuPlacement);
|
|
857
|
+
gHost.windows->appName = settings.title;
|
|
858
|
+
auto windowHost = std::make_shared<ngn::WindowHost>(gHost.windows, system);
|
|
859
|
+
gHost.windows->setHost(windowHost);
|
|
860
|
+
// A second launch, or the session bus: activate raises the app, and open hands it links and
|
|
861
|
+
// files, which DeepLinks hears (the initial URL until the app has drawn).
|
|
862
|
+
g_signal_connect(gHost.application, "activate", G_CALLBACK(+[](GApplication *, gpointer) { raiseApp(); }), nullptr);
|
|
863
|
+
g_signal_connect(gHost.application, "open", G_CALLBACK(+[](GApplication *, GFile **files, gint count, const gchar *, gpointer) {
|
|
864
|
+
for (gint i = 0; i < count; ++i) {
|
|
865
|
+
gchar *uri = g_file_get_uri(files[i]);
|
|
866
|
+
if (!gAuthSession->receive(uri)) {
|
|
867
|
+
gHost.state->receiveUrl(uri, gHost.firstFrame);
|
|
868
|
+
}
|
|
869
|
+
g_free(uri);
|
|
870
|
+
}
|
|
871
|
+
raiseApp();
|
|
872
|
+
}), nullptr);
|
|
873
|
+
gHost.gtkMenus = std::make_shared<ngn::gtk::GtkMenus>([](const ngn::MenuItem &item) { runMenuItem(item, nullptr); });
|
|
874
|
+
auto menus = std::make_shared<ngn::MenuHost>(
|
|
875
|
+
gHost.gtkMenus, ngn::DefaultMenuOptions{.appName = settings.title, .developer = ngn::DevServer::fromBundleUrl(bundle).has_value(), .os = os});
|
|
876
|
+
gHost.menus = menus;
|
|
877
|
+
// File > Print with no print item of the app's: what the window shows (printing.h).
|
|
878
|
+
menus->printWindow = [](std::optional<SurfaceId> surface) {
|
|
879
|
+
GtkWidget *widget = surface ? surfaceWidgetOf(*surface) : nullptr;
|
|
880
|
+
if (!widget) {
|
|
881
|
+
auto *active = gtk_application_get_active_window(gHost.application);
|
|
882
|
+
auto found = active ? surfaceOfWindow(active) : std::nullopt;
|
|
883
|
+
widget = found ? surfaceWidgetOf(*found) : nullptr;
|
|
884
|
+
}
|
|
885
|
+
if (!widget) {
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
ngn::gtk::printWidget(widget, false, [](const std::string &error) {
|
|
889
|
+
if (!error.empty()) {
|
|
890
|
+
fprintf(stderr, "[ngn] print: %s\n", error.c_str());
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
};
|
|
894
|
+
// Each window's history, as the app's router reports it, for its header bar's back and forward
|
|
895
|
+
// pair, which goes through it.
|
|
896
|
+
menus->historyChanged = [](SurfaceId surface) {
|
|
897
|
+
gHost.backend->runOnUiThread([surface] {
|
|
898
|
+
if (gHost.menus) {
|
|
899
|
+
gHost.backend->headers().setHistory(surface, gHost.menus->history(surface));
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
};
|
|
903
|
+
gHost.backend->headers().navigate = [](SurfaceId surface, ngn::NavigationDirection direction, int steps) {
|
|
904
|
+
gHost.menus->navigate(direction, surface, steps);
|
|
905
|
+
};
|
|
906
|
+
routeWindow(gHost.appSurface);
|
|
907
|
+
// A view's context menu (its `contextMenu` prop), drawn where the secondary click was.
|
|
908
|
+
ngn::gtk::setContextMenuShower([](GtkWidget *widget, const ngn::PointerInput &input) {
|
|
909
|
+
auto target = gHost.input->contextMenuAt(input);
|
|
910
|
+
if (!target) {
|
|
911
|
+
return false;
|
|
912
|
+
}
|
|
913
|
+
auto tag = target->tag;
|
|
914
|
+
auto *window = GTK_WINDOW(gtk_widget_get_root(widget));
|
|
915
|
+
return ngn::gtk::popUpContextMenu(widget, input.point, target->items, [tag, window](const ngn::MenuItem &item) {
|
|
916
|
+
if (item.command == ngn::MenuCommand::Back || item.command == ngn::MenuCommand::Forward) {
|
|
917
|
+
navigate(item.command == ngn::MenuCommand::Back ? ngn::NavigationDirection::Back : ngn::NavigationDirection::Forward, window);
|
|
918
|
+
} else {
|
|
919
|
+
gHost.input->contextMenuSelect(tag, item.id);
|
|
920
|
+
}
|
|
921
|
+
});
|
|
922
|
+
});
|
|
923
|
+
// The tray only while a StatusNotifierWatcher runs, as the launch finds it; Linux has no Dock.
|
|
924
|
+
std::shared_ptr<ngn::TrayHost> tray;
|
|
925
|
+
if (ngn::gtk::statusNotifierWatcherRunning()) {
|
|
926
|
+
auto host = std::make_shared<std::weak_ptr<ngn::TrayHost>>();
|
|
927
|
+
tray = std::make_shared<ngn::TrayHost>(ngn::gtk::makeStatusNotifierTray(appId, {
|
|
928
|
+
.press = [host](const std::string &icon) {
|
|
929
|
+
if (auto tray = host->lock()) {
|
|
930
|
+
tray->press(icon);
|
|
931
|
+
}
|
|
932
|
+
},
|
|
933
|
+
.menuSelect = [host](const std::string &icon, const std::string &id) {
|
|
934
|
+
if (auto tray = host->lock()) {
|
|
935
|
+
tray->menuSelect(icon, id);
|
|
936
|
+
}
|
|
937
|
+
},
|
|
938
|
+
.command = [](ngn::MenuCommand command) { runMenuItem({.command = command}, nullptr); },
|
|
939
|
+
}));
|
|
940
|
+
*host = tray;
|
|
941
|
+
}
|
|
942
|
+
// A bundle on disk runs the newest update the Expo Updates client keeps, in the app's data
|
|
943
|
+
// folder ($XDG_DATA_HOME/<app-id>/updates: the install tree is read-only), when the app has an
|
|
944
|
+
// updates URL; one from a dev server never does.
|
|
945
|
+
bool fromDevServer = ngn::DevServer::fromBundleUrl(bundle).has_value();
|
|
946
|
+
gHost.updates = std::make_shared<ngn::UpdatesClient>(
|
|
947
|
+
ngn::updatesConfigFrom(settings, os, appData, bundle), network.http, ngn::makeOpenSslUpdateCrypto());
|
|
948
|
+
if (!fromDevServer && gHost.updates->enabled()) {
|
|
949
|
+
bundle = gHost.updates->launch();
|
|
950
|
+
if (auto running = gHost.updates->running()) {
|
|
951
|
+
fprintf(stderr, "[ngn] running update %s\n", running->id.c_str());
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
setRunningBundle(bundle);
|
|
955
|
+
auto fileDialogs = ngn::gtk::makeFileDialogs();
|
|
956
|
+
#if NGN_GSTREAMER
|
|
957
|
+
// The default microphone, through PulseAudio or PipeWire, and the camera through the Camera
|
|
958
|
+
// portal, with the microphone a recording's sound comes from.
|
|
959
|
+
auto microphone = ngn::gtk::makeAudioInput();
|
|
960
|
+
auto cameraHardware = ngn::gtk::makeCameraHardware({.microphone = microphone});
|
|
961
|
+
#endif
|
|
962
|
+
// The desktop host's own modules (desktop-modules.h). What this backend has no source for yet
|
|
963
|
+
// is left empty, and its module is not registered: JavaScript reads it as absent (NgNativeHost).
|
|
964
|
+
auto desktopModules = ngn::addDesktopModules(
|
|
965
|
+
*modules,
|
|
966
|
+
{.os = os,
|
|
967
|
+
.system = system,
|
|
968
|
+
.state = gHost.state,
|
|
969
|
+
.http = network.http,
|
|
970
|
+
.files = fileAccess,
|
|
971
|
+
.appId = appId,
|
|
972
|
+
.appName = settings.title,
|
|
973
|
+
.caches = caches,
|
|
974
|
+
.noPrompts = settings.noPrompts,
|
|
975
|
+
.windows = windowHost,
|
|
976
|
+
.menus = menus,
|
|
977
|
+
.tray = tray,
|
|
978
|
+
.processes = processes,
|
|
979
|
+
.shell = ngn::gtk::makeShellOpener(),
|
|
980
|
+
.secrets = ngn::gtk::makeSecretStore(appId, ngn::gtk::insideFlatpak()),
|
|
981
|
+
.sleep = ngn::gtk::makeSleepPreventer(gHost.application),
|
|
982
|
+
.notifications =
|
|
983
|
+
[appData, appId](std::shared_ptr<ngn::NotificationEvents> events) {
|
|
984
|
+
return ngn::gtk::makeNotificationCenter(G_APPLICATION(gHost.application), appData, std::move(events), raiseApp,
|
|
985
|
+
ngn::gtk::makeLauncherBadge(appId));
|
|
986
|
+
},
|
|
987
|
+
.undo = gHost.undo,
|
|
988
|
+
.fileDialogs = fileDialogs,
|
|
989
|
+
.fileWatcher = ngn::gtk::makeFileWatcher(),
|
|
990
|
+
.location = ngn::gtk::makeLocationSource(),
|
|
991
|
+
.bundle = ngn::bundleFactsOf(settings.plist),
|
|
992
|
+
.machine = ngn::gtk::machineFacts(),
|
|
993
|
+
.pathMonitor = ngn::gtk::makePathMonitor(),
|
|
994
|
+
.power = ngn::gtk::makePowerSource(),
|
|
995
|
+
.fontRegistrar = ngn::gtk::makeFontRegistrar(),
|
|
996
|
+
.authSession = gAuthSession,
|
|
997
|
+
#if NGN_GSTREAMER
|
|
998
|
+
// Playback and recording through GStreamer, each left out where it is not installed.
|
|
999
|
+
.audioOutput = ngn::gtk::makeAudioOutput(),
|
|
1000
|
+
.audioInput = microphone,
|
|
1001
|
+
.videoOutput = ngn::gtk::makeVideoOutput(),
|
|
1002
|
+
#endif
|
|
1003
|
+
.splash = gSplash,
|
|
1004
|
+
// ImagePicker: the FileChooser portal over the user's files, and the camera's own window.
|
|
1005
|
+
.photoPicker = ngn::makeFilePhotoPicker(fileDialogs, caches + "/ImagePicker", ngn::gtk::imagePixelSize),
|
|
1006
|
+
#if NGN_GSTREAMER
|
|
1007
|
+
.cameraHardware = cameraHardware,
|
|
1008
|
+
.cameraSheet = cameraHardware ? ngn::gtk::makeCameraSheet() : nullptr,
|
|
1009
|
+
#endif
|
|
1010
|
+
.imageCodec = ngn::gtk::makeImageCodec(),
|
|
1011
|
+
.printSystem = ngn::gtk::makePrintSystem(surfaceWidgetOf),
|
|
1012
|
+
.crypto = ngn::gtk::makeCryptoSource(),
|
|
1013
|
+
.locales = ngn::gtk::makeLocaleSource(),
|
|
1014
|
+
.regionCode = ngn::gtk::systemRegionCode,
|
|
1015
|
+
.updates = gHost.updates,
|
|
1016
|
+
.fromDevServer = fromDevServer,
|
|
1017
|
+
.reload =
|
|
1018
|
+
[](const std::string &next, std::function<void(bool)> done) {
|
|
1019
|
+
setRunningBundle(next);
|
|
1020
|
+
// The launch the module's beginReload just began.
|
|
1021
|
+
uint64_t generation = gHost.updates->launchGeneration();
|
|
1022
|
+
gHost.launchFrame.bundleLoading();
|
|
1023
|
+
gHost.runtime->reload(next, [next, done, generation](bool loaded) {
|
|
1024
|
+
gHost.launchFrame.bundleLoadEnded(loaded);
|
|
1025
|
+
// A bundle that did not load never counts as a launch that succeeded; one that did
|
|
1026
|
+
// counts from the first frame its own instance mounts on screen.
|
|
1027
|
+
if (loaded) {
|
|
1028
|
+
uint64_t instance = gHost.mounting->instance();
|
|
1029
|
+
gHost.backend->runOnUiThread([generation, instance] { reloadLaunched(generation, instance); });
|
|
1030
|
+
} else {
|
|
1031
|
+
fprintf(stderr, "ngn-linux: the update's bundle %s did not load\n", next.c_str());
|
|
1032
|
+
}
|
|
1033
|
+
done(loaded);
|
|
1034
|
+
});
|
|
1035
|
+
}});
|
|
1036
|
+
#if NGN_GSTREAMER
|
|
1037
|
+
ngn::gtk::useCameraHardware(cameraHardware, desktopModules.cameraViews, settings.noPrompts);
|
|
1038
|
+
#endif
|
|
1039
|
+
// The names fonts are loaded under, which text resolves (fonts.h).
|
|
1040
|
+
ngn::gtk::useFontLibrary(desktopModules.fonts);
|
|
1041
|
+
|
|
1042
|
+
gHost.runtime = std::make_unique<ngn::Runtime>(
|
|
1043
|
+
gHost.mounting,
|
|
1044
|
+
std::make_shared<ngn::gtk::PangoTextMeasurer>(),
|
|
1045
|
+
ngn::gtk::makeControlMeasurer(),
|
|
1046
|
+
modules, network, bundle,
|
|
1047
|
+
ngn::DebuggerIdentity{.appId = appId, .deviceName = g_get_host_name(), .os = os.id});
|
|
1048
|
+
gHost.runtime->rightToLeft = [system] { return ngn::appRightToLeft(*system); };
|
|
1049
|
+
// The first launch owes its first frame from the instance the runtime just began.
|
|
1050
|
+
gHost.launchFrame.loaded(gHost.updates->launchGeneration(), gHost.mounting->instance());
|
|
1051
|
+
|
|
1052
|
+
ngn::onLayoutAnimationsRunning([](bool running) {
|
|
1053
|
+
gHost.backend->runOnUiThread([running] {
|
|
1054
|
+
if (running && gHost.animationTimer == 0) {
|
|
1055
|
+
gHost.animationTimer = g_timeout_add(8, [](gpointer) -> gboolean {
|
|
1056
|
+
if (gHost.runtime) {
|
|
1057
|
+
gHost.runtime->animationTick();
|
|
1058
|
+
}
|
|
1059
|
+
return G_SOURCE_CONTINUE;
|
|
1060
|
+
}, nullptr);
|
|
1061
|
+
} else if (!running && gHost.animationTimer != 0) {
|
|
1062
|
+
g_source_remove(gHost.animationTimer);
|
|
1063
|
+
gHost.animationTimer = 0;
|
|
1064
|
+
}
|
|
1065
|
+
});
|
|
1066
|
+
});
|
|
1067
|
+
|
|
1068
|
+
ngn::gtk::watchFontScale(followFontScale);
|
|
1069
|
+
|
|
1070
|
+
GSource *beat = g_source_new(&gBeatFuncs, sizeof(GSource));
|
|
1071
|
+
g_source_set_priority(beat, G_PRIORITY_LOW);
|
|
1072
|
+
g_source_attach(beat, nullptr);
|
|
1073
|
+
|
|
1074
|
+
if (!gHost.runtime->loadBundle()) {
|
|
1075
|
+
fprintf(stderr, "ngn-linux: could not load %s\n", bundle.c_str());
|
|
1076
|
+
return 1;
|
|
1077
|
+
}
|
|
1078
|
+
gHost.runtime->startSurface(kAppSurface, settings.module, metricsFor(width, height, ngn::gtk::windowScale(gHost.appWindow)));
|
|
1079
|
+
|
|
1080
|
+
while (!gHost.quit) {
|
|
1081
|
+
g_main_context_iteration(nullptr, TRUE);
|
|
1082
|
+
}
|
|
1083
|
+
// A clean quit is no crash: the update running is not rolled back, however soon it quit. Not so
|
|
1084
|
+
// while its reload is pending or its bundle did not load: that launch never ran.
|
|
1085
|
+
if (gHost.launchFrame.bundleLoaded()) {
|
|
1086
|
+
gHost.updates->markLaunchSucceeded();
|
|
1087
|
+
}
|
|
1088
|
+
gHost.runtime->shutdown();
|
|
1089
|
+
gHost.runtime.reset();
|
|
1090
|
+
gHost.accent.reset();
|
|
1091
|
+
return 0;
|
|
1092
|
+
}
|