Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui_linux: Fix Wayland activation requests (#64058)
Lex Berezhny4d847859f550
diff --git a/crates/gpui_linux/src/linux/wayland/client.rs b/crates/gpui_linux/src/linux/wayland/client.rsindex 2d40cf7fa94..fa007ef00c7 100644--- a/crates/gpui_linux/src/linux/wayland/client.rs+++ b/crates/gpui_linux/src/linux/wayland/client.rs@@ -92,7 +92,7 @@ use crate::linux::{wayland::{clipboard::{Clipboard, DataOffer, FILE_LIST_MIME_TYPE, TEXT_MIME_TYPES},cursor::Cursor,- serial::{Serial, SerialKind, SerialTracker},+ serial::{ActivationContext, Serial, SerialKind, SerialTracker},to_shape,window::WaylandWindow,},@@ -406,7 +406,6 @@ pub(crate) struct WaylandClientState {data_offers: Vec<DataOffer<WlDataOffer>>,primary_data_offer: Option<DataOffer<ZwpPrimarySelectionOfferV1>>,cursor: Cursor,- pending_activation: Option<PendingActivation>,startup_activation_token: Option<String>,/// The environment this connection was made in, without its activation token.graphical_environment: GraphicalEnvironment,@@ -657,6 +656,14 @@ impl WaylandClientStatePtr {self.0.upgrade().unwrap().borrow().serial_tracker.get(kind)}+ pub fn activation_context(&self) -> Option<ActivationContext> {+ self.0+ .upgrade()?+ .borrow()+ .serial_tracker+ .activation_context()+ }+pub fn start_external_drag(&self,surface: &wl_surface::WlSurface,@@ -697,11 +704,6 @@ impl WaylandClientStatePtr {true}- pub fn set_pending_activation(&self, window: ObjectId) {- self.0.upgrade().unwrap().borrow_mut().pending_activation =- Some(PendingActivation::Window(window));- }-pub fn enable_ime(&self) {let client = self.get_client();let mut state = client.borrow_mut();@@ -1127,7 +1129,6 @@ impl WaylandConnection {data_offers: Vec::new(),primary_data_offer: None,cursor,- pending_activation: None,startup_activation_token: activation_token,graphical_environment: GraphicalEnvironment {activation_token: None,@@ -1394,16 +1395,15 @@ impl WaylandConnection {}pub(crate) fn open_uri(&self, uri: &str) {- let mut state = self.0.borrow_mut();- if let (Some(activation), Some(window)) = (+ let state = self.0.borrow();+ if let (Some(activation), Some(context)) = (state.globals.activation.clone(),- state.mouse_focused_window.clone(),+ state.serial_tracker.activation_context(),) {- state.pending_activation = Some(PendingActivation::Uri(uri.to_string()));- let token = activation.get_activation_token(&state.globals.qh, ());- let serial = state.serial_tracker.get(SerialKind::MousePress);- token.set_serial(serial.as_raw(), &state.wl_seat);- token.set_surface(&window.surface());+ let token = activation+ .get_activation_token(&state.globals.qh, PendingActivation::Uri(uri.to_string()));+ token.set_serial(context.serial.as_raw(), &state.wl_seat);+ token.set_surface(&context.requesting_surface);token.commit();} else {let executor = state.common.borrow().background_executor.clone();@@ -1412,16 +1412,15 @@ impl WaylandConnection {}pub(crate) fn reveal_path(&self, path: PathBuf) {- let mut state = self.0.borrow_mut();- if let (Some(activation), Some(window)) = (+ let state = self.0.borrow();+ if let (Some(activation), Some(context)) = (state.globals.activation.clone(),- state.mouse_focused_window.clone(),+ state.serial_tracker.activation_context(),) {- state.pending_activation = Some(PendingActivation::Path(path));- let token = activation.get_activation_token(&state.globals.qh, ());- let serial = state.serial_tracker.get(SerialKind::MousePress);- token.set_serial(serial.as_raw(), &state.wl_seat);- token.set_surface(&window.surface());+ let token =+ activation.get_activation_token(&state.globals.qh, PendingActivation::Path(path));+ token.set_serial(context.serial.as_raw(), &state.wl_seat);+ token.set_surface(&context.requesting_surface);token.commit();} else {let executor = state.common.borrow().background_executor.clone();@@ -1932,12 +1931,14 @@ impl Dispatch<xdg_wm_base::XdgWmBase, ()> for WaylandClientStatePtr {}}-impl Dispatch<xdg_activation_token_v1::XdgActivationTokenV1, ()> for WaylandClientStatePtr {+impl Dispatch<xdg_activation_token_v1::XdgActivationTokenV1, PendingActivation>+ for WaylandClientStatePtr+{fn event(this: &mut Self,token: &xdg_activation_token_v1::XdgActivationTokenV1,event: <xdg_activation_token_v1::XdgActivationTokenV1 as Proxy>::Event,- _: &(),+ pending_activation: &PendingActivation,_: &Connection,_: &QueueHandle<Self>,) {@@ -1947,21 +1948,21 @@ impl Dispatch<xdg_activation_token_v1::XdgActivationTokenV1, ()> for WaylandClieif let xdg_activation_token_v1::Event::Done { token } = event {let executor = state.common.borrow().background_executor.clone();let environment = state.graphical_environment.clone();- match state.pending_activation.take() {- Some(PendingActivation::Uri(uri)) => {- open_uri_internal(executor, &uri, Some(token), environment)+ match pending_activation {+ PendingActivation::Uri(uri) => {+ open_uri_internal(executor, uri, Some(token), environment)}- Some(PendingActivation::Path(path)) => {- reveal_path_internal(executor, path, Some(token), environment)+ PendingActivation::Path(path) => {+ reveal_path_internal(executor, path.clone(), Some(token), environment)}- Some(PendingActivation::Window(window)) => {- let Some(window) = get_window(&mut state, &window) else {- return;- };- let activation = state.globals.activation.as_ref().unwrap();- activation.activate(token, &window.surface());+ PendingActivation::Window(window) => {+ let activation = state.globals.activation.clone();+ if let (Some(window), Some(activation)) =+ (get_window(&mut state, window), activation)+ {+ activation.activate(token, &window.surface());+ }}- None => log::error!("activation token received with no pending activation"),}}@@ -2150,7 +2151,15 @@ impl Dispatch<wl_keyboard::WlKeyboard, ()> for WaylandClientStatePtr {..} => {if key_state == wl_keyboard::KeyState::Pressed {- state.serial_tracker.update(SerialKind::KeyPress, serial);+ let focused_surface = state+ .keyboard_focused_window+ .as_ref()+ .map(|window| window.surface());+ state.serial_tracker.update_with_focused_surface(+ SerialKind::KeyPress,+ serial,+ focused_surface,+ );}let focused_window = state.keyboard_focused_window.clone();@@ -2510,7 +2519,15 @@ impl Dispatch<wl_pointer::WlPointer, ()> for WaylandClientStatePtr {// Record presses only. Requests referencing this serial (popup grabs,// interactive moves) are declined when given a release serial.if button_state == wl_pointer::ButtonState::Pressed {- state.serial_tracker.update(SerialKind::MousePress, serial);+ let focused_surface = state+ .mouse_focused_window+ .as_ref()+ .map(|window| window.surface());+ state.serial_tracker.update_with_focused_surface(+ SerialKind::MousePress,+ serial,+ focused_surface,+ );}let button = linux_button_to_gpui(button);let Some(button) = button else { return };diff --git a/crates/gpui_linux/src/linux/wayland/serial.rs b/crates/gpui_linux/src/linux/wayland/serial.rsindex 9f5372a8a67..1bc4f001dea 100644--- a/crates/gpui_linux/src/linux/wayland/serial.rs+++ b/crates/gpui_linux/src/linux/wayland/serial.rs@@ -1,4 +1,5 @@use collections::HashMap;+use wayland_client::{Proxy, protocol::wl_surface};#[derive(Debug, Hash, PartialEq, Eq)]pub(crate) enum SerialKind {@@ -27,26 +28,53 @@ impl SelectionSerial {}}+/// The user interaction an `xdg_activation_v1` token request must reference.+#[derive(Clone, Debug)]+pub(crate) struct ActivationContext {+ pub(crate) serial: Serial,+ pub(crate) requesting_surface: wl_surface::WlSurface,+}++#[derive(Debug)]+struct Press {+ serial: Serial,+ focused_surface: Option<wl_surface::WlSurface>,+}+#[derive(Debug)]/// Helper for tracking of different serial kinds.pub(crate) struct SerialTracker {serials: HashMap<SerialKind, Serial>,- selection_serial: Option<SelectionSerial>,+ latest_press: Option<Press>,}impl SerialTracker {pub fn new() -> Self {Self {serials: HashMap::default(),- selection_serial: None,+ latest_press: None,}}pub fn update(&mut self, kind: SerialKind, value: u32) {+ self.update_with_focused_surface(kind, value, None);+ }++ /// Like [`Self::update`], but for key and pointer presses also records the surface+ /// that had focus, which activation token requests must name alongside the serial.+ pub fn update_with_focused_surface(+ &mut self,+ kind: SerialKind,+ value: u32,+ focused_surface: Option<wl_surface::WlSurface>,+ ) {let serial = Serial(value);if matches!(&kind, SerialKind::KeyPress | SerialKind::MousePress) {- self.selection_serial = Some(SelectionSerial(serial));+ self.latest_press = Some(Press {+ serial,+ focused_surface,+ });}self.serials.insert(kind, serial);@@ -60,7 +88,22 @@ impl SerialTracker {}pub fn selection_serial(&self) -> Option<SelectionSerial> {- self.selection_serial+ self.latest_press+ .as_ref()+ .map(|press| SelectionSerial(press.serial))+ }++ /// Returns the latest press if a still-alive surface had focus when it arrived.+ pub fn activation_context(&self) -> Option<ActivationContext> {+ let press = self.latest_press.as_ref()?;+ let surface = press+ .focused_surface+ .as_ref()+ .filter(|surface| surface.is_alive())?;+ Some(ActivationContext {+ serial: press.serial,+ requesting_surface: surface.clone(),+ })}}diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/src/linux/wayland/window.rsindex 8d92234ef1e..f7eca602e37 100644--- a/crates/gpui_linux/src/linux/wayland/window.rs+++ b/crates/gpui_linux/src/linux/wayland/window.rs@@ -32,7 +32,7 @@ use wayland_protocols_plasma::blur::client::org_kde_kwin_blur;use wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1;use crate::linux::wayland::{display::WaylandDisplay, serial::SerialKind};-use crate::linux::{Globals, Output, WaylandClientStatePtr, get_window};+use crate::linux::{Globals, Output, PendingActivation, WaylandClientStatePtr, get_window};use gpui::{AnyWindowHandle, Bounds, Capslock, Decorations, DevicePixels, ExternalDragPayload,FrameRequestSource, GpuSpecs, Modifiers, Pixels, PlatformAtlas, PlatformDisplay,@@ -1857,18 +1857,23 @@ impl PlatformWindow for WaylandWindow {}fn activate(&self) {- // Try to request an activation token. Even though the activation is likely going to be rejected,- // KWin and Mutter can use the app_id to visually indicate we're requesting attention.let state = self.borrow();if let (Some(activation), Some(app_id)) = (&state.globals.activation, state.app_id.clone()){- state.client.set_pending_activation(state.surface.id());- let token = activation.get_activation_token(&state.globals.qh, ());- // The serial isn't exactly important here, since the activation is probably going to be rejected anyway.- let serial = state.client.get_serial(SerialKind::MousePress);+ let token = activation.get_activation_token(+ &state.globals.qh,+ PendingActivation::Window(state.surface.id()),+ );token.set_app_id(app_id);- token.set_serial(serial.as_raw(), &state.globals.seat);- token.set_surface(&state.surface);+ // Without a recent press the compositor won't grant focus, but still request a+ // token: KWin and Mutter mark the window as demanding attention and end their+ // launch feedback once the token is passed to `activate`.+ if let Some(context) = state.client.activation_context() {+ token.set_serial(context.serial.as_raw(), &state.globals.seat);+ token.set_surface(&context.requesting_surface);+ } else {+ token.set_surface(&state.surface);+ }token.commit();}}