diff --git a/komorebi/src/animation/animator.rs b/komorebi/src/animation/animator.rs index 4fd152d7..58e4152e 100644 --- a/komorebi/src/animation/animator.rs +++ b/komorebi/src/animation/animator.rs @@ -39,6 +39,7 @@ use crate::accessibility::attribute_constants::kAXSizeAttribute; use crate::accessibility::enhanced_ui; use crate::accessibility::enhanced_ui::EnhancedUiGuard; use crate::accessibility::error::AccessibilityError; +use crate::arrival; use crate::border_manager; use crate::core::Rect; use crate::macos_api::MacosApi; @@ -877,6 +878,22 @@ fn perform_write(window_id: WindowId, write: &Merged) -> WriteOutcome { /// How far (in pixels) a window may be from where it was sent without being sent there again const SETTLE_TOLERANCE: u32 = 2; +/// Compare where a window turned out to be with where it was sent, and say what it turned out to be +/// +/// Whether the position and the size are right is returned. Whether the window took the width it +/// was given is said to [`arrival`], every time and whatever the answer, as it forgets a window that +/// has taken its size since it last refused. +fn compare(window_id: WindowId, actual: Rect, target: Rect) -> (bool, bool) { + arrival::report(window_id, target, actual); + + let position_is_right = actual.left.abs_diff(target.left) <= SETTLE_TOLERANCE + && actual.top.abs_diff(target.top) <= SETTLE_TOLERANCE; + let size_is_right = actual.right.abs_diff(target.right) <= SETTLE_TOLERANCE + && actual.bottom.abs_diff(target.bottom) <= SETTLE_TOLERANCE; + + (position_is_right, size_is_right) +} + fn perform_verify(verify: &Verify) -> VerifyOutcome { let window_id = verify.window_id; let element = &verify.handle.element; @@ -891,10 +908,7 @@ fn perform_verify(verify: &Verify) -> VerifyOutcome { } }; - let position_is_right = actual.left.abs_diff(target.left) <= SETTLE_TOLERANCE - && actual.top.abs_diff(target.top) <= SETTLE_TOLERANCE; - let size_is_right = actual.right.abs_diff(target.right) <= SETTLE_TOLERANCE - && actual.bottom.abs_diff(target.bottom) <= SETTLE_TOLERANCE; + let (position_is_right, size_is_right) = compare(window_id, actual, target); if position_is_right && size_is_right { // the animation may have been cut short by a slow app, so make sure the border ends up @@ -927,6 +941,14 @@ fn perform_verify(verify: &Verify) -> VerifyOutcome { ); border_manager::follow(window_id, target); + // there is no verification after the last attempt, so look for the last time at what + // came of it, as that is the last word on where the window ended up + if verify.last_attempt + && let Ok(rect) = MacosApi::window_rect(element) + { + compare(window_id, Rect::from(rect), target); + } + VerifyOutcome::Reissued } Err(_) => VerifyOutcome::Failed { @@ -967,6 +989,65 @@ mod tests { } } + fn frame(width: i32) -> Rect { + Rect { + left: 100, + top: 50, + right: width, + bottom: 800, + } + } + + #[test] + fn a_window_that_would_not_get_as_narrow_is_reported_with_what_it_turned_out_to_be() { + // ids that nothing else in the tests uses, as what is reported is shared by all of them + let window_id = 4_100_000_001; + + let (position_is_right, size_is_right) = compare(window_id, frame(600), frame(400)); + + assert!(position_is_right); + assert!(!size_is_right); + + let arrival = arrival::take(window_id).expect("the refusal should have been reported"); + assert_eq!(arrival.target, frame(400)); + assert_eq!(arrival.actual, frame(600)); + } + + #[test] + fn a_window_that_took_its_size_is_not_a_refusal_and_is_forgotten() { + let window_id = 4_100_000_002; + + // it refused, and then it did not + compare(window_id, frame(600), frame(400)); + let (position_is_right, size_is_right) = compare(window_id, frame(401), frame(400)); + + assert!(position_is_right && size_is_right); + assert_eq!(arrival::take(window_id), None); + } + + #[test] + fn a_window_that_is_narrower_than_it_was_told_to_be_is_not_a_refusal() { + let window_id = 4_100_000_003; + + let (_, size_is_right) = compare(window_id, frame(300), frame(400)); + + assert!(!size_is_right); + assert_eq!(arrival::take(window_id), None); + } + + #[test] + fn a_window_that_is_in_the_wrong_place_but_the_right_size_is_not_a_refusal() { + let window_id = 4_100_000_004; + let mut actual = frame(400); + actual.left += 300; + + let (position_is_right, size_is_right) = compare(window_id, actual, frame(400)); + + assert!(!position_is_right); + assert!(size_is_right); + assert_eq!(arrival::take(window_id), None); + } + #[test] fn writes_to_one_window_are_folded_into_one() { let jobs = coalesce(vec![ diff --git a/komorebi/src/arrival.rs b/komorebi/src/arrival.rs new file mode 100644 index 00000000..1a410f79 --- /dev/null +++ b/komorebi/src/arrival.rs @@ -0,0 +1,166 @@ +//! What a window turned out to be once it got where it was sent. +//! +//! An app is free to refuse a size, and many do: a window that will not get narrower than some +//! width stays that wide however it is asked. A layout that does not know about this places the +//! next column as if the window had the width that it was given, and so places it on top of the +//! window that did not. +//! +//! Whatever finds out that a window is wider than it was told to be, which is the animator when a +//! journey ends and the layout when it writes a size without animating, says so here. Nothing in +//! this module waits for anything or takes the lock of the window manager, so it can be called +//! from any thread. The layout picks up what has been said the next time that it runs. + +use crate::core::Rect; +use parking_lot::Mutex; +use std::collections::HashMap; +use std::sync::LazyLock; + +/// How much wider than it was told to be a window can be before it counts as refusing, since +/// windows are placed in whole pixels and some apps round. +pub const TOLERANCE: i32 = 2; + +/// Where a window was sent, and what it turned out to be +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Arrival { + pub target: Rect, + pub actual: Rect, +} + +impl Arrival { + /// How much wider than it was told to be the window is, or zero if it is not by more than the + /// tolerance + pub fn extra_width(&self) -> i32 { + let extra = self.actual.right - self.target.right; + + if extra > TOLERANCE { extra } else { 0 } + } +} + +/// The latest word on every window that was not what it was told to be +static ARRIVALS: LazyLock>> = + LazyLock::new(|| Mutex::new(HashMap::new())); + +/// Say what a window turned out to be when it was sent to `target`. +/// +/// Only a window that did not take the width that it was given is kept. The latest word is the one +/// that counts, so a window that has taken its size since is forgotten. +pub fn report(window_id: u32, target: Rect, actual: Rect) { + let arrival = Arrival { target, actual }; + + let mut arrivals = ARRIVALS.lock(); + if arrival.extra_width() > 0 { + arrivals.insert(window_id, arrival); + } else { + arrivals.remove(&window_id); + } +} + +/// What a window was last said to have turned out to be, if that was wider than it was told to be, +/// and forget it. +pub fn take(window_id: u32) -> Option { + ARRIVALS.lock().remove(&window_id) +} + +/// How wide a column has to be for a window that turned out as `arrival` says, given what the +/// column takes up around the window on either side, and that no column is wider than the +/// viewport. +/// +/// `None` is for a window that took the size it was given. +pub fn column_width_for(arrival: &Arrival, around_window: i32, viewport_width: i32) -> Option { + (arrival.extra_width() > 0) + .then(|| (arrival.actual.right + 2 * around_window).min(viewport_width)) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn rect(width: i32) -> Rect { + Rect { + left: 0, + top: 0, + right: width, + bottom: 800, + } + } + + #[test] + fn a_window_that_took_its_size_is_not_a_refusal() { + assert_eq!( + column_width_for( + &Arrival { + target: rect(400), + actual: rect(400) + }, + 10, + 1200 + ), + None + ); + + // nor is rounding + assert_eq!( + column_width_for( + &Arrival { + target: rect(400), + actual: rect(402) + }, + 10, + 1200 + ), + None + ); + + // nor is a window that is narrower than it was told to be + assert_eq!( + column_width_for( + &Arrival { + target: rect(400), + actual: rect(300) + }, + 10, + 1200 + ), + None + ); + } + + #[test] + fn a_window_that_is_wider_needs_a_column_wide_enough_for_it_and_what_is_around_it() { + let arrival = Arrival { + target: rect(400), + actual: rect(700), + }; + + assert_eq!(column_width_for(&arrival, 0, 1200), Some(700)); + assert_eq!(column_width_for(&arrival, 12, 1200), Some(724)); + } + + #[test] + fn no_column_is_wider_than_the_viewport() { + let arrival = Arrival { + target: rect(400), + actual: rect(1300), + }; + + assert_eq!(column_width_for(&arrival, 12, 1200), Some(1200)); + } + + #[test] + fn the_latest_word_on_a_window_is_the_one_that_counts() { + // ids that no other test uses, since this is shared by every test that runs at once + let id = 4_000_001; + + report(id, rect(400), rect(700)); + assert_eq!(take(id).map(|a| a.actual.right), Some(700)); + assert_eq!(take(id), None, "it is taken once"); + + report(id, rect(400), rect(700)); + report(id, rect(400), rect(401)); + assert_eq!(take(id), None, "the window took its size afterwards"); + + report(id, rect(400), rect(700)); + report(id, rect(400), rect(800)); + assert_eq!(take(id).map(|a| a.actual.right), Some(800)); + } +} diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 66eba391..4b322b76 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -52,6 +52,7 @@ pub mod accessibility; pub mod animation; pub mod app_kit_notification_constants; pub mod application; +pub mod arrival; pub mod border_manager; pub mod container; pub mod core;