Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 39 additions & 5 deletions src/shojiwm_lib/src/plasma_window_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,9 @@ fn send_changes(resources: &[OrgKdePlasmaWindow], old: &PlasmaWindowInfo, new: &

pub struct PlasmaWindowManagementState {
managers: Vec<OrgKdePlasmaWindowManagement>,
/// 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<PlasmaWindowHandle>,
next_internal_id: u32,
}
Expand Down Expand Up @@ -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<PlasmaWindowHandle> = self
.windows_top_to_bottom()
.into_iter()
.rev()
.filter_map(|window| window.user_data().get::<PlasmaWindowHandle>().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
Expand All @@ -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;
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/shojiwm_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
81 changes: 81 additions & 0 deletions src/shojiwm_rs/tests/plasma_stacking_order.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<String>> = const { RefCell::new(Vec::new()) };
/// The last stacking order sent, bottom to top.
static STACKING: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
}

impl Dispatch<OrgKdePlasmaWindowManagement, ()> for common::Client {
fn event(
_: &mut Self,
_: &OrgKdePlasmaWindowManagement,
event: org_kde_plasma_window_management::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
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<String> {
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");
}