From d7710f7264a45147467c7a1043bcfcef0368a725 Mon Sep 17 00:00:00 2001 From: Kirill Chibisov Date: Tue, 31 Dec 2024 06:08:18 +0300 Subject: [PATCH] api: add `ActivationToken::{from,into}_raw` This is needed when passing and getting token from the IPC to activate the window. --- src/changelog/unreleased.md | 1 + src/platform/startup_notify.rs | 6 ++-- .../linux/wayland/types/xdg_activation.rs | 2 +- src/platform_impl/linux/wayland/window/mod.rs | 2 +- src/platform_impl/linux/x11/mod.rs | 2 +- src/platform_impl/linux/x11/window.rs | 2 +- src/window.rs | 29 +++++++++++++++++-- 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index ca9e513531..84e9e766fc 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -42,6 +42,7 @@ changelog entry. ### Added +- `ActivationToken::from_raw` and `ActivationToken::into_raw`. - On X11, add a workaround for disabling IME on GNOME. ### Fixed diff --git a/src/platform/startup_notify.rs b/src/platform/startup_notify.rs index c9047a03b2..6fab284e1a 100644 --- a/src/platform/startup_notify.rs +++ b/src/platform/startup_notify.rs @@ -64,7 +64,7 @@ impl EventLoopExtStartupNotify for ActiveEventLoop { crate::platform_impl::ActiveEventLoop::X(_) => env::var(X11_VAR), } .ok() - .map(ActivationToken::_new) + .map(ActivationToken::from_raw) } } @@ -94,6 +94,6 @@ pub fn reset_activation_token_env() { /// /// This could be used before running daemon processes. pub fn set_activation_token_env(token: ActivationToken) { - env::set_var(X11_VAR, &token._token); - env::set_var(WAYLAND_VAR, token._token); + env::set_var(X11_VAR, &token.token); + env::set_var(WAYLAND_VAR, token.token); } diff --git a/src/platform_impl/linux/wayland/types/xdg_activation.rs b/src/platform_impl/linux/wayland/types/xdg_activation.rs index 8bd21d0abb..9efc75da34 100644 --- a/src/platform_impl/linux/wayland/types/xdg_activation.rs +++ b/src/platform_impl/linux/wayland/types/xdg_activation.rs @@ -80,7 +80,7 @@ impl Dispatch for XdgA state.events_sink.push_window_event( crate::event::WindowEvent::ActivationTokenDone { serial: *serial, - token: ActivationToken::_new(token), + token: ActivationToken::from_raw(token), }, *window_id, ); diff --git a/src/platform_impl/linux/wayland/window/mod.rs b/src/platform_impl/linux/wayland/window/mod.rs index 08e4504a83..83d6f80328 100644 --- a/src/platform_impl/linux/wayland/window/mod.rs +++ b/src/platform_impl/linux/wayland/window/mod.rs @@ -168,7 +168,7 @@ impl Window { if let (Some(xdg_activation), Some(token)) = (xdg_activation.as_ref(), attributes.platform_specific.activation_token) { - xdg_activation.activate(token._token, &surface); + xdg_activation.activate(token.token, &surface); } // XXX Do initial commit. diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 8b9b690fb2..4042da0d09 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -531,7 +531,7 @@ impl EventLoop { window_id: crate::window::WindowId(window_id), event: WindowEvent::ActivationTokenDone { serial, - token: crate::window::ActivationToken::_new(token), + token: crate::window::ActivationToken::from_raw(token), }, }; callback(event, &self.event_processor.target) diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 1777f904df..ec1b0420b4 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -559,7 +559,7 @@ impl UnownedWindow { // Remove the startup notification if we have one. if let Some(startup) = window_attrs.platform_specific.activation_token.as_ref() { - leap!(xconn.remove_activation_token(xwindow, &startup._token)); + leap!(xconn.remove_activation_token(xwindow, &startup.token)); } // We never want to give the user a broken window, since by then, it's too late to handle. diff --git a/src/window.rs b/src/window.rs index 9bd002ab4c..f59273ed49 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1850,11 +1850,34 @@ impl Default for ImePurpose { /// [`Window`]: crate::window::Window #[derive(Debug, PartialEq, Eq, Clone)] pub struct ActivationToken { - pub(crate) _token: String, + pub(crate) token: String, } impl ActivationToken { - pub(crate) fn _new(_token: String) -> Self { - Self { _token } + /// Make an [`ActivationToken`] from a string. + /// + /// This method should be used to wrap tokens passed by side channels to your application, like + /// dbus. + /// + /// The validity of the token is ensured by the windowing system. Using the invalid token will + /// only result in the side effect of the operation involving it being ignored (e.g. window + /// won't get focused automatically), but won't yield any errors. + /// + /// To obtain a valid token, use + #[cfg_attr(any(x11_platform, wayland_platform, docsrs), doc = " [`request_activation_token`].")] + #[cfg_attr( + not(any(x11_platform, wayland_platform, docsrs)), + doc = " `request_activation_token`." + )] + /// + #[rustfmt::skip] + /// [`request_activation_token`]: crate::platform::startup_notify::WindowExtStartupNotify::request_activation_token + pub fn from_raw(token: String) -> Self { + Self { token } + } + + /// Convert the token to its string representation to later pass via IPC. + pub fn into_raw(self) -> String { + self.token } }