diff --git a/Cargo.lock b/Cargo.lock index 0fbaa397..dabdd676 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5169,6 +5169,7 @@ dependencies = [ "wayland-client", "wayland-protocols", "wayland-protocols-misc", + "wayland-protocols-plasma", "wayland-protocols-wlr", ] diff --git a/src/shojiwm_lib/src/plasma_window_management.rs b/src/shojiwm_lib/src/plasma_window_management.rs index b373dadf..fad1fb8b 100644 --- a/src/shojiwm_lib/src/plasma_window_management.rs +++ b/src/shojiwm_lib/src/plasma_window_management.rs @@ -303,8 +303,9 @@ fn send_changes(resources: &[OrgKdePlasmaWindow], old: &PlasmaWindowInfo, new: & pub struct PlasmaWindowManagementState { managers: Vec, - /// Live windows, oldest first. This is also the stacking order the - /// clients get: ShojiWM's real stacking lives in the TypeScript config. + /// Live windows in stacking order, bottom to top: the order windows are + /// drawn in (`ShojiWM::windows_top_to_bottom`, reversed). A window not + /// in the space yet stays on top. Clients get this as the stacking order. handles: Vec, next_internal_id: u32, } @@ -378,8 +379,40 @@ impl ShojiWM { for manager in &state.managers { PlasmaWindowManagementState::announce(manager, &handle); } - state.windows_changed(); window.user_data().insert_if_missing(|| handle); + // (a new window always changes the list, even where it stacks last) + if !self.restack_plasma_windows() { + self.plasma_window_management_state.windows_changed(); + } + } + + /// Put the handles in the order the windows are drawn in, and tell the + /// clients when that changed (focus raises a window; the config's + /// z-index moves one). True: it changed and the clients were told. + fn restack_plasma_windows(&mut self) -> bool { + let mut order: Vec = self + .windows_top_to_bottom() + .into_iter() + .rev() + .filter_map(|window| window.user_data().get::().cloned()) + .collect(); + let state = &mut self.plasma_window_management_state; + for handle in &state.handles { + if !order.iter().any(|candidate| candidate.same_as(handle)) { + order.push(handle.clone()); + } + } + let same = order.len() == state.handles.len() + && order + .iter() + .zip(&state.handles) + .all(|(new, old)| new.same_as(old)); + if same { + return false; + } + state.handles = order; + state.windows_changed(); + true } /// Send the window's current title, state and geometry to the clients @@ -394,8 +427,8 @@ impl ShojiWM { handle.update(self.plasma_window_info(window)); } - /// [`Self::sync_plasma_window`] for every window: focus, minimize and - /// geometry changes land here. + /// [`Self::sync_plasma_window`] for every window: focus, minimize, + /// geometry and stacking changes land here. pub fn sync_plasma_window_states(&mut self) { if self.plasma_window_management_state.handles.is_empty() { return; @@ -404,6 +437,7 @@ impl ShojiWM { for window in windows { self.sync_plasma_window(&window); } + self.restack_plasma_windows(); } /// Send `unmapped` and forget the window. diff --git a/src/shojiwm_rs/Cargo.toml b/src/shojiwm_rs/Cargo.toml index b1bca3ce..f383bb3b 100644 --- a/src/shojiwm_rs/Cargo.toml +++ b/src/shojiwm_rs/Cargo.toml @@ -17,3 +17,4 @@ wayland-client = "0.31.14" wayland-protocols = { version = "0.32.12", features = ["client", "staging", "unstable"] } wayland-protocols-wlr = { version = "0.3.12", features = ["client"] } wayland-protocols-misc = { version = "0.3.12", features = ["client"] } +wayland-protocols-plasma = { version = "0.3.12", features = ["client"] } diff --git a/src/shojiwm_rs/tests/plasma_stacking_order.rs b/src/shojiwm_rs/tests/plasma_stacking_order.rs new file mode 100644 index 00000000..bc8f2740 --- /dev/null +++ b/src/shojiwm_rs/tests/plasma_stacking_order.rs @@ -0,0 +1,81 @@ +//! The stacking order that org_kde_plasma_window_management gives is the +//! order windows are drawn in: a raised window moves to the top, and the +//! clients are told. + +mod common; + +use std::cell::RefCell; + +use wayland_client::{Connection, Dispatch, QueueHandle}; +use wayland_protocols_plasma::plasma_window_management::client::org_kde_plasma_window_management::{ + self, OrgKdePlasmaWindowManagement, +}; + +thread_local! { + /// The windows announced, in the order they came. + static UUIDS: RefCell> = const { RefCell::new(Vec::new()) }; + /// The last stacking order sent, bottom to top. + static STACKING: RefCell> = const { RefCell::new(Vec::new()) }; +} + +impl Dispatch for common::Client { + fn event( + _: &mut Self, + _: &OrgKdePlasmaWindowManagement, + event: org_kde_plasma_window_management::Event, + _: &(), + _: &Connection, + _: &QueueHandle, + ) { + match event { + org_kde_plasma_window_management::Event::WindowWithUuid { uuid, .. } => { + UUIDS.with(|uuids| uuids.borrow_mut().push(uuid)); + } + org_kde_plasma_window_management::Event::StackingOrderUuidChanged { uuids } => { + STACKING.with(|stacking| { + *stacking.borrow_mut() = uuids.split(';').map(String::from).collect(); + }); + } + _ => {} + } + } +} + +fn setup() {} + +fn stacking() -> Vec { + STACKING.with(|stacking| stacking.borrow().clone()) +} + +#[test] +fn a_raised_window_is_on_top_of_the_stacking_order() { + let mut session = common::Session::new(setup); + // (version 16: the last that sends the order as a string) + let _manager: OrgKdePlasmaWindowManagement = + session.bind("org_kde_plasma_window_management", 16); + session.roundtrip(); + session.map_toplevel_with_buffer("a", 200, 200); + session.map_toplevel_with_buffer("b", 200, 200); + session.roundtrip(); + + let uuids = UUIDS.with(|uuids| uuids.borrow().clone()); + assert_eq!(uuids.len(), 2, "both windows are announced"); + let (a, b) = (uuids[0].clone(), uuids[1].clone()); + assert_eq!( + stacking(), + [a.clone(), b.clone()], + "the newest window is on top" + ); + + let bottom = session + .state + .space + .elements() + .next() + .cloned() + .expect("a window in the space"); + session.state.space.raise_element(&bottom, true); + session.state.sync_taskbar_window_states(); + session.roundtrip(); + assert_eq!(stacking(), [b, a], "the raised window is on top"); +}