Files
komorebi/komorebi/tests/placement_cases.rs
T
LGUG2Z 21017b573f feat(wm): initial window placement rules
A window that opens goes after the focused container. A workspace can now say
another place for its windows: the primary or the secondary tile, before or
after the focused container, the end of the list, or a place by number, counting
from 1. The place is for every window, or for the windows that match a rule on
their program, app id, title or path; the rules are the conditions the ignore
and manage rules are made of, and the first that matches in key order decides. A
place that does not exist, such as the secondary tile of a workspace with one
container, is the default place.

A place is read from a name, a number or text that is a number. The
documentation of the setting shows a number and a map's keys can only be text,
and komorebi for Windows (83c4e11d) reads only text, so the form its own page
shows is refused there.

A place that was reserved with preselect-direction is used before the rules are
asked, and a locked container keeps its index. A workspace whose
window_container_behaviour is Append puts a window in the focused container, and
the rules are not used.

A workspace takes the rules from the key initial_window_placement_rules of its
entry in komorebi.json. The key is in schema.json and so in the typed config
option of the module, and the page docs/initial-window-placement-rules.md says
how to write it. vm-placement opens windows in a headless KWin and checks where
they go, by program, app id and title.

The schema that schemars derives for a place is an object with a Placement or an
Index key, which is not what is read or written, so a check against it refuses a
file that the daemon reads. The schema of PlacementTarget is written by hand
instead: a name, a number, or text that is a number. A file with a place of each
form is checked against schema.json, and the module's config option takes them.
2026-10-28 07:20:57 -07:00

153 lines
5.4 KiB
Rust

//! What a placement rule does beside the other things that decide where a window goes: how a rule
//! names a program, an app id and a title, a place that was preselected, and a locked container.
//!
//! `placement.rs` has the forms of the setting and the places they name. Window ids are in the
//! 61_000s.
use komorebi::backend::fake::FakeBackend;
use komorebi::backend::with_backend;
use komorebi::core::InitialWindowPlacementRules;
use komorebi::core::OperationDirection;
use komorebi::core::SocketMessage;
use komorebi::window::Window;
use komorebi::window_manager::WindowManager;
use komorebi::window_manager_event::KWinNotification;
use komorebi::window_manager_event::SystemNotification;
use komorebi::window_manager_event::WindowManagerEvent;
use std::sync::Arc;
fn desktop(rules: &str) -> (Arc<FakeBackend>, WindowManager) {
let fake = Arc::new(FakeBackend::with_one_monitor());
let (_sender, receiver) = crossbeam_channel::bounded(1);
// unix socket paths are limited to ~104 bytes on macOS, which the temp dir alone can exceed
static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
let socket = std::path::PathBuf::from(format!(
"/tmp/kmq-{}-{}.sock",
std::process::id(),
NEXT.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
));
let mut wm = with_backend(fake.clone(), || {
let mut wm = WindowManager::new(receiver, Some(socket)).unwrap();
wm.init().unwrap();
wm
});
let rules: InitialWindowPlacementRules = serde_json::from_str(rules).unwrap();
wm.monitors_mut()[0].workspaces_mut()[0].initial_window_placement_rules = Some(rules);
(fake, wm)
}
fn open(fake: &Arc<FakeBackend>, wm: &mut WindowManager, id: u32, exe: &str, class: &str) {
fake.open_window(id, exe, class, &format!("{exe} {id}"));
with_backend(fake.clone(), || {
wm.process_event(WindowManagerEvent::Show(
SystemNotification::KWin(KWinNotification::WindowAdded),
Window::from(id),
))
})
.unwrap();
}
/// the ids of the windows of the containers, in order
fn order(fake: &Arc<FakeBackend>, wm: &WindowManager) -> Vec<u32> {
with_backend(fake.clone(), || {
wm.focused_workspace()
.unwrap()
.containers()
.iter()
.filter_map(|c| c.focused_window().map(|w| w.id))
.collect()
})
}
fn send(fake: &Arc<FakeBackend>, wm: &mut WindowManager, message: SocketMessage) {
with_backend(fake.clone(), || wm.process_command(message, Vec::new())).unwrap();
}
fn focus(fake: &Arc<FakeBackend>, wm: &mut WindowManager, id: u32) {
fake.set_foreground(Some(id));
with_backend(fake.clone(), || {
wm.process_event(WindowManagerEvent::FocusChange(
SystemNotification::KWin(KWinNotification::WindowActivated),
Window::from(id),
))
})
.unwrap();
}
#[test]
fn a_rule_may_name_the_program_the_way_windows_does() {
// `.exe` is ignored in the id, so a configuration written for Windows works here
let (fake, mut wm) = desktop(
r#"{ "Primary": { "kind": "Exe", "id": "firefox.exe", "matching_strategy": "Equals" } }"#,
);
open(&fake, &mut wm, 61_001, "foot", "foot");
open(&fake, &mut wm, 61_002, "foot", "foot");
open(&fake, &mut wm, 61_003, "firefox", "firefox");
assert_eq!(order(&fake, &wm), [61_003, 61_001, 61_002]);
}
#[test]
fn a_rule_may_name_the_app_id_of_the_window() {
let (fake, mut wm) = desktop(
r#"{ "Last": { "kind": "Class", "id": "special", "matching_strategy": "Equals" } }"#,
);
open(&fake, &mut wm, 61_011, "foot", "special");
open(&fake, &mut wm, 61_012, "foot", "foot");
open(&fake, &mut wm, 61_013, "foot", "foot");
// a window that no rule matches goes after the focused container, which is the last to open
assert_eq!(order(&fake, &wm), [61_011, 61_012, 61_013]);
open(&fake, &mut wm, 61_014, "foot", "special");
focus(&fake, &mut wm, 61_011);
open(&fake, &mut wm, 61_015, "foot", "foot");
open(&fake, &mut wm, 61_016, "foot", "special");
// 61_015 went after the focused 61_011; 61_016 matches and went to the end
assert_eq!(
order(&fake, &wm),
[61_011, 61_015, 61_012, 61_013, 61_014, 61_016]
);
}
#[test]
fn a_place_that_was_preselected_wins_over_the_rules() {
let (fake, mut wm) = desktop(r#""Last""#);
open(&fake, &mut wm, 61_021, "foot", "foot");
open(&fake, &mut wm, 61_022, "foot", "foot");
focus(&fake, &mut wm, 61_021);
send(
&fake,
&mut wm,
SocketMessage::PreselectDirection(OperationDirection::Right),
);
open(&fake, &mut wm, 61_023, "foot", "foot");
// the rule says the end of the list; the place that was reserved is the one in the middle
assert_eq!(order(&fake, &wm), [61_021, 61_023, 61_022]);
// and the rule is back for the next window
open(&fake, &mut wm, 61_024, "foot", "foot");
assert_eq!(order(&fake, &wm), [61_021, 61_023, 61_022, 61_024]);
}
#[test]
fn a_rule_that_asks_for_the_place_of_a_locked_container_gets_the_place_after_it() {
let (fake, mut wm) = desktop(r#""Primary""#);
open(&fake, &mut wm, 61_031, "foot", "foot");
send(&fake, &mut wm, SocketMessage::ToggleLock);
open(&fake, &mut wm, 61_032, "foot", "foot");
open(&fake, &mut wm, 61_033, "foot", "foot");
// the first place is the locked container's, so each window takes the one after it
assert_eq!(order(&fake, &wm), [61_031, 61_033, 61_032]);
}