From 66fb7ef585b4340401d3aca14eaa10e48775582f Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 15:16:36 -0700 Subject: [PATCH] feat(core): update_shared has a begin_pass hook whose answer the hooks of the pass see WorkspacePlatform::begin_pass(workspace) is called once per placement pass, after the layout is computed and before the containers are placed, and what it returns is handed to adjust_layout and place_container as Placement::pass (Placement now carries that borrow). A platform that has something to read once for the whole pass takes it here, so that two containers of a pass cannot see two different values. The default returns None. A test with the stub platform pins the order (once per pass, before the placements) and that the hooks see the answer. For reviewer finding F3 (S/review/core.md): the Windows title-bar tables were read once per pass and the shared update read them once per container; the Windows revision that follows uses this hook. Verified: cargo test -p komorebi-core --features schemars (native): 30 passed, 0 failed. Not run: the daemons (the next revision builds them). --- crates/komorebi-core/src/workspace.rs | 38 +++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/crates/komorebi-core/src/workspace.rs b/crates/komorebi-core/src/workspace.rs index 6863d74b..67e8f689 100644 --- a/crates/komorebi-core/src/workspace.rs +++ b/crates/komorebi-core/src/workspace.rs @@ -59,7 +59,7 @@ pub enum Pass { /// What the hooks of an update know about the pass that is running. #[derive(Clone, Copy, Debug)] -pub struct Placement { +pub struct Placement<'a> { /// The work area less the monitor's and the workspace's offsets and padding. pub adjusted_work_area: Rect, /// The work area of the monitor. @@ -76,6 +76,8 @@ pub struct Placement { pub resize_dimensions_is_empty: bool, /// Whether a window is maximized on this workspace. pub managed_maximized_window: bool, + /// What [`WorkspacePlatform::begin_pass`] returned for this pass. + pub pass: Option<&'a dyn std::any::Any>, } /// What names a window of the platform `P`: the id the shared code compares. @@ -224,12 +226,20 @@ pub trait WorkspacePlatform: Sized + Clone + std::fmt::Debug + PartialEq + 'stat (vec![], vec![]) } + /// Called once per placement pass, after the layout is computed and before the containers are + /// placed: whatever the platform needs to read once for the whole pass (Windows takes a snapshot + /// of the title-bar rules here, so that two containers of a pass see the same one). The answer + /// is handed to the hooks of the pass as [`Placement::pass`]. + fn begin_pass(_workspace: &Workspace) -> Option> { + None + } + /// Adjusts the tile of one container before its windows are placed: the room a border takes, /// and whatever else the platform needs (the clamp to the work area, the stackbar). fn adjust_layout( _container: &mut Container, _layout: &mut Rect, - _placement: &Placement, + _placement: &Placement<'_>, ) { } @@ -238,7 +248,7 @@ pub trait WorkspacePlatform: Sized + Clone + std::fmt::Debug + PartialEq + 'stat _container: &mut Container, _layout: &Rect, _hidden: bool, - _placement: &Placement, + _placement: &Placement<'_>, ) -> eyre::Result<()> { Err(eyre::eyre!("this platform has no shared update")) } @@ -1799,6 +1809,8 @@ impl Workspace

{ ) }; + let pass_state = P::begin_pass(self); + let placement = Placement { adjusted_work_area, work_area, @@ -1808,6 +1820,7 @@ impl Workspace

{ is_scrolling, resize_dimensions_is_empty: self.resize_dimensions.is_empty(), managed_maximized_window, + pass: pass_state.as_deref(), }; for (i, container) in self.containers_mut().iter_mut().enumerate() { @@ -1999,6 +2012,11 @@ mod tests { Ok(Flow::Run(None)) } + fn begin_pass(_: &Workspace) -> Option> { + LOG.with(|log| log.borrow_mut().push("begin_pass".to_string())); + Some(Box::new(7u8)) + } + fn containers_pruned(_: &mut Workspace, focused_before: usize) { LOG.with(|log| log.borrow_mut().push(format!("pruned {focused_before}"))); } @@ -2011,14 +2029,15 @@ mod tests { container: &mut Container, layout: &Rect, hidden: bool, - _: &Placement, + placement: &Placement, ) -> eyre::Result<()> { LOG.with(|log| { log.borrow_mut().push(format!( - "place {} {} {}", + "place {} {} {} {:?}", container.windows()[0].0, layout.left, - hidden + hidden, + placement.pass.and_then(|pass| pass.downcast_ref::()) )) }); Ok(()) @@ -2092,8 +2111,11 @@ mod tests { assert_eq!(log[0], "begin"); assert_eq!(log[1], "pruned 1"); assert_eq!(log.iter().filter(|l| l.starts_with("place ")).count(), 4); - assert_eq!(log[4], "end 0"); - assert_eq!(log[7], "end 1"); + assert_eq!(log[2], "begin_pass"); + assert_eq!(log[3], "place 1 1 false Some(7)"); + assert_eq!(log[5], "end 0"); + assert_eq!(log[6], "begin_pass"); + assert_eq!(log[9], "end 1"); assert_eq!(workspace.latest_layout.len(), 2); assert_eq!(workspace.resize_dimensions.len(), 2); }