9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 1 | use monolith_planner::Rect; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 2 | use slotmap::{SlotMap, new_key_type}; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 3 | use std::collections::HashMap; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 4 | use std::hash::Hash; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 5 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 6 | new_key_type! { pub struct NodeId; } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 7 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 8 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
b22c0511d5af | atagen | 2026-10-17 03:48 UTC | 9 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
b22c0511d5af | atagen | 2026-10-17 03:48 UTC | 10 | #[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 11 | pub enum Axis { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 12 | Horizontal, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 13 | Vertical, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 14 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 15 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 16 | impl Axis { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 17 | pub fn other(self) -> Self { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 18 | match self { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 19 | Self::Horizontal => Self::Vertical, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 20 | Self::Vertical => Self::Horizontal, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 21 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 22 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 23 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 24 | fn index(self) -> usize { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 25 | match self { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 26 | Self::Horizontal => 0, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 27 | Self::Vertical => 1, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 28 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 29 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 30 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 31 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 32 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
b22c0511d5af | atagen | 2026-10-17 03:48 UTC | 33 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
b22c0511d5af | atagen | 2026-10-17 03:48 UTC | 34 | #[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 35 | pub enum Dir { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 36 | Left, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 37 | Right, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 38 | Up, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 39 | Down, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 40 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 41 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 42 | impl Dir { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 43 | pub fn axis(self) -> Axis { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 44 | match self { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 45 | Self::Left | Self::Right => Axis::Horizontal, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 46 | Self::Up | Self::Down => Axis::Vertical, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 47 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 48 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 49 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 50 | pub fn forward(self) -> bool { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 51 | matches!(self, Self::Right | Self::Down) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 52 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 53 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 54 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 55 | const MIN_SHARE: f64 = 0.05; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 56 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 57 | /// A window's width and height relative to its neighbours'. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 58 | type Size = [f64; 2]; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 59 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 60 | #[derive(Clone, Debug)] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 61 | enum Kind<W> { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 62 | Leaf(W, Size), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 63 | Container { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 64 | axis: Axis, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 65 | children: Vec<NodeId>, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 66 | expanded: Option<NodeId>, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 67 | }, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 68 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 69 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 70 | #[derive(Clone, Debug)] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 71 | struct Node<W> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 72 | parent: Option<NodeId>, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 73 | kind: Kind<W>, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 74 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 75 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 76 | /// One output's tiling tree. Every tiled window of every tag lives here, the |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 77 | /// caller's visibility filter decides which ones take space. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 78 | /// |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 79 | /// Children run on the axis opposite their parent. Containers hold at least |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 80 | /// two children, hidden ones included, and a container left with one child |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 81 | /// is replaced by it. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 82 | #[derive(Clone, Debug)] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 83 | pub struct Tree<W> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 84 | nodes: SlotMap<NodeId, Node<W>>, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 85 | root: Option<NodeId>, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 86 | leaves: HashMap<W, NodeId>, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 87 | stack: Axis, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 88 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 89 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 90 | impl<W: Copy + Eq + Hash> Default for Tree<W> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 91 | fn default() -> Self { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 92 | Self { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 93 | nodes: SlotMap::with_key(), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 94 | root: None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 95 | leaves: HashMap::new(), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 96 | stack: Axis::Vertical, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 97 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 98 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 99 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 100 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 101 | impl<W: Copy + Eq + Hash> Tree<W> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 102 | /// The axis tag rows stack along. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 103 | pub fn stack_axis(&self) -> Axis { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 104 | self.stack |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 105 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 106 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 107 | pub fn contains(&self, w: W) -> bool { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 108 | self.leaves.contains_key(&w) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 109 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 110 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 111 | pub fn is_empty(&self) -> bool { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 112 | self.root.is_none() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 113 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 114 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 115 | pub fn windows(&self) -> impl Iterator<Item = W> + '_ { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 116 | self.leaves.keys().copied() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 117 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 118 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 119 | /// Inserts `w` as the next sibling of `anchor`, or as a new row if |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 120 | /// `anchor` is not in the tree. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 121 | pub fn insert_after(&mut self, w: W, anchor: W) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 122 | let Some(&a) = self.leaves.get(&anchor) else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 123 | return self.insert_row(w); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 124 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 125 | match self.position(a) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 126 | Some((p, i)) if Some(p) == self.root && self.axis(p) == Some(self.stack) => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 127 | let n = self.new_leaf(w, self.size(a)); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 128 | self.unlink(p, i); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 129 | let c = self.new_container(self.stack.other()); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 130 | self.attach(c, 0, a); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 131 | self.attach(c, 1, n); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 132 | self.attach(p, i, c); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 133 | self.reveal(n, true); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 134 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 135 | Some((p, i)) => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 136 | let n = self.new_leaf(w, self.size(p)); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 137 | self.attach(p, i + 1, n); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 138 | self.reveal(n, true); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 139 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 140 | None => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 141 | let n = self.new_leaf(w, self.size(a)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 142 | let c = self.new_container(self.stack.other()); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 143 | self.attach(c, 0, a); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 144 | self.attach(c, 1, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 145 | self.root = Some(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 146 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 147 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 148 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 149 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 150 | /// Appends `w` as a row of its own along the stacking axis. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 151 | pub fn insert_row(&mut self, w: W) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 152 | let size = self.root.map_or([1.0; 2], |r| self.size(r)); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 153 | let n = self.new_leaf(w, size); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 154 | match self.root { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 155 | None => self.root = Some(n), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 156 | Some(r) if self.axis(r) == Some(self.stack) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 157 | let end = self.children(r).len(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 158 | self.attach(r, end, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 159 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 160 | Some(r) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 161 | let c = self.new_container(self.stack); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 162 | self.attach(c, 0, r); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 163 | self.attach(c, 1, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 164 | self.root = Some(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 165 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 166 | } |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 167 | self.reveal(n, true); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 168 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 169 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 170 | pub fn remove(&mut self, w: W) -> bool { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 171 | let Some(n) = self.leaves.remove(&w) else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 172 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 173 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 174 | self.detach(n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 175 | self.nodes.remove(n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 176 | true |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 177 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 178 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 179 | pub fn retain(&mut self, mut keep: impl FnMut(W) -> bool) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 180 | let gone: Vec<W> = self.windows().filter(|&w| !keep(w)).collect(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 181 | for w in gone { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 182 | self.remove(w); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 183 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 184 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 185 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 186 | /// Moves a window one step in `dir`. Leaves swap slots with the next |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 187 | /// visible sibling, enter a sibling container, or step out beside their |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 188 | /// container. Hidden windows never move. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 189 | pub fn move_window(&mut self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 190 | let Some(&n) = self.leaves.get(&w).filter(|_| vis(w)) else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 191 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 192 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 193 | self.until_seen(&vis, |t| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 194 | let moved = t.move_node(n, dir, &vis, true); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 195 | t.reveal(n, moved) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 196 | }) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 197 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 198 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 199 | /// Moves the container holding `w` as a rigid block. A window sitting |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 200 | /// directly in the root is its own block. |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 201 | pub fn move_container(&mut self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 202 | let Some(&n) = self.leaves.get(&w).filter(|_| vis(w)) else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 203 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 204 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 205 | self.until_seen(&vis, |t| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 206 | let block = t.nodes[n] |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 207 | .parent |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 208 | .filter(|&p| Some(p) != t.root) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 209 | .unwrap_or(n); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 210 | let moved = t.move_node(block, dir, &vis, false); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 211 | t.reveal(n, moved) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 212 | }) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 213 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 214 | |
605c44c8c5fc | atagen | 2026-10-17 09:57 UTC | 215 | /// Moves a window to the end of the next visible lane in `dir` among its |
605c44c8c5fc | atagen | 2026-10-17 09:57 UTC | 216 | /// siblings. A lone window there becomes a stack with it. A window with no |
605c44c8c5fc | atagen | 2026-10-17 09:57 UTC | 217 | /// sibling lane that way leaves its stack as `move_window` would, so it |
605c44c8c5fc | atagen | 2026-10-17 09:57 UTC | 218 | /// takes one press to step out and another to merge. |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 219 | pub fn merge_window(&mut self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 220 | let Some(&n) = self.leaves.get(&w).filter(|_| vis(w)) else { |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 221 | return false; |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 222 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 223 | self.until_seen(&vis, |t| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 224 | if let Some((p, i)) = t.position(n) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 225 | && t.axis(p) == Some(dir.axis()) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 226 | && let Some(j) = t.next_shown(p, i, dir, &vis) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 227 | { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 228 | t.join_lane(n, p, j); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 229 | return t.reveal(n, true); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 230 | } |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 231 | let moved = t.move_node(n, dir, &vis, true); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 232 | t.reveal(n, moved) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 233 | }) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 234 | } |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 235 | |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 236 | /// Repeats `step` until the visible layout changes, or undoes it all when |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 237 | /// it never does. Steps around hidden windows otherwise ate keypresses |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 238 | /// and rearranged windows the user could not see. |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 239 | fn until_seen( |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 240 | &mut self, |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 241 | vis: &impl Fn(W) -> bool, |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 242 | mut step: impl FnMut(&mut Self) -> bool, |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 243 | ) -> bool { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 244 | let seen = |t: &Self| -> Vec<(W, Rect)> { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 245 | t.arrange(Rect::new(0.0, 0.0, 1000.0, 1000.0), 0.0, vis) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 246 | .into_iter() |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 247 | .filter(|p| vis(p.0)) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 248 | .collect() |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 249 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 250 | let same = |a: &[(W, Rect)], b: &[(W, Rect)]| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 251 | a.len() == b.len() |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 252 | && a.iter().all(|(v, r)| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 253 | b.iter().any(|(w, s)| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 254 | v == w |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 255 | && [r.x - s.x, r.y - s.y, r.w - s.w, r.h - s.h] |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 256 | .iter() |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 257 | .all(|d| d.abs() < 1e-6) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 258 | }) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 259 | }) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 260 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 261 | let before = self.clone(); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 262 | let start = seen(self); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 263 | for _ in 0..=before.nodes.len() { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 264 | if !step(self) { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 265 | break; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 266 | } |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 267 | if !same(&start, &seen(self)) { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 268 | return true; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 269 | } |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 270 | } |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 271 | *self = before; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 272 | false |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 273 | } |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 274 | |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 275 | /// Undoes any expansion along `leaf`'s ancestry that favours another |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 276 | /// child, once it has `moved` or arrived. An expanded container gives its |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 277 | /// other children no room, so a window moved into one vanished while |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 278 | /// staying focused. |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 279 | fn reveal(&mut self, leaf: NodeId, moved: bool) -> bool { |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 280 | let mut cur = leaf; |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 281 | while moved && let Some(p) = self.nodes[cur].parent { |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 282 | if let Kind::Container { expanded, .. } = &mut self.nodes[p].kind |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 283 | && expanded.is_some_and(|e| e != cur) |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 284 | { |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 285 | *expanded = None; |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 286 | } |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 287 | cur = p; |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 288 | } |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 289 | moved |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 290 | } |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 291 | |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 292 | /// Whether every container above `w` is expanded towards it. |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 293 | pub fn maximized(&self, w: W, vis: impl Fn(W) -> bool) -> bool { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 294 | let path = self.shown_path(w, &vis); |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 295 | !path.is_empty() && path.iter().all(|&(p, c)| self.expanded(p) == Some(c)) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 296 | } |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 297 | |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 298 | /// `w`'s ancestry as (container, child) pairs, leaving out containers |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 299 | /// where the child has no visible sibling. Those look like nothing on |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 300 | /// screen, so expanding there would do nothing the user could see. |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 301 | fn shown_path(&self, w: W, vis: &impl Fn(W) -> bool) -> Vec<(NodeId, NodeId)> { |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 302 | let Some(&leaf) = self.leaves.get(&w) else { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 303 | return Vec::new(); |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 304 | }; |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 305 | let mut path = Vec::new(); |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 306 | let mut cur = leaf; |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 307 | while let Some(p) = self.nodes[cur].parent { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 308 | let shared = self |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 309 | .children(p) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 310 | .iter() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 311 | .any(|&c| c != cur && self.visible(c, vis)); |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 312 | if shared { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 313 | path.push((p, cur)); |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 314 | } |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 315 | cur = p; |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 316 | } |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 317 | path |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 318 | } |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 319 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 320 | /// Transposes the whole tree. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 321 | pub fn flip(&mut self) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 322 | self.stack = self.stack.other(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 323 | for node in self.nodes.values_mut() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 324 | match &mut node.kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 325 | Kind::Container { axis, .. } => *axis = axis.other(), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 326 | Kind::Leaf(_, size) => size.reverse(), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 327 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 328 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 329 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 330 | |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 331 | /// Toggles expansion for `count` levels of `w`'s visible ancestry, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 332 | /// starting `skip` levels up. Level 0 expands the window inside the |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 333 | /// nearest container where it has a visible sibling. |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 334 | pub fn toggle_expand( |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 335 | &mut self, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 336 | w: W, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 337 | skip: usize, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 338 | count: usize, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 339 | vis: impl Fn(W) -> bool, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 340 | ) -> bool { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 341 | let path: Vec<_> = self |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 342 | .shown_path(w, &vis) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 343 | .into_iter() |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 344 | .skip(skip) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 345 | .take(count) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 346 | .collect(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 347 | if path.is_empty() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 348 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 349 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 350 | let all = path.iter().all(|&(p, c)| self.expanded(p) == Some(c)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 351 | for (p, c) in path { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 352 | if let Kind::Container { expanded, .. } = &mut self.nodes[p].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 353 | *expanded = (!all).then_some(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 354 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 355 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 356 | true |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 357 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 358 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 359 | /// Grows `w` by `delta`, a fraction of the visible length of the nearest |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 360 | /// container along `axis` where it has a visible sibling. Hidden siblings |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 361 | /// keep their sizes, and with them their share. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 362 | pub fn resize(&mut self, w: W, axis: Axis, delta: f64, vis: impl Fn(W) -> bool) -> bool { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 363 | let Some(&leaf) = self.leaves.get(&w) else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 364 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 365 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 366 | let mut cur = leaf; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 367 | while let Some(p) = self.nodes[cur].parent { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 368 | if self.axis(p) == Some(axis) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 369 | let shown: Vec<(NodeId, f64)> = self |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 370 | .children(p) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 371 | .iter() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 372 | .filter(|&&c| self.visible(c, &vis)) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 373 | .map(|&c| (c, self.extent(c, axis, &vis))) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 374 | .collect(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 375 | if shown.len() >= 2 { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 376 | let sum: f64 = shown.iter().map(|s| s.1).sum(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 377 | let old = self.extent(cur, axis, &vis); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 378 | let max = 1.0 - MIN_SHARE * (shown.len() - 1) as f64; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 379 | let new = (old / sum + delta).clamp(MIN_SHARE, max) * sum; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 380 | let scale = (sum - new) / (sum - old); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 381 | for (c, _) in shown { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 382 | self.scale(c, axis, if c == cur { new / old } else { scale }); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 383 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 384 | return true; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 385 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 386 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 387 | cur = p; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 388 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 389 | false |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 390 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 391 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 392 | /// Scales every window under `n` along `axis`, hidden ones too, so they |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 393 | /// keep their proportions for when they are shown again. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 394 | fn scale(&mut self, n: NodeId, axis: Axis, by: f64) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 395 | match &mut self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 396 | Kind::Leaf(_, size) => size[axis.index()] *= by, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 397 | Kind::Container { children, .. } => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 398 | for c in children.clone() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 399 | self.scale(c, axis, by); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 400 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 401 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 402 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 403 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 404 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 405 | /// How much room `n` asks for along `axis` among its siblings. A |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 406 | /// container asks for the mean of what its visible children ask for, so |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 407 | /// windows of other tags sharing a lane change nothing on screen, and a |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 408 | /// window carries its own size wherever it moves. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 409 | fn extent(&self, n: NodeId, axis: Axis, vis: &impl Fn(W) -> bool) -> f64 { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 410 | match &self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 411 | Kind::Leaf(_, size) => size[axis.index()], |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 412 | Kind::Container { children, .. } => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 413 | let shown: Vec<f64> = children |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 414 | .iter() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 415 | .filter(|&&c| self.visible(c, vis)) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 416 | .map(|&c| self.extent(c, axis, vis)) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 417 | .collect(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 418 | if shown.is_empty() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 419 | children |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 420 | .iter() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 421 | .map(|&c| self.extent(c, axis, vis)) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 422 | .sum::<f64>() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 423 | / children.len() as f64 |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 424 | } else { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 425 | shown.iter().sum::<f64>() / shown.len() as f64 |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 426 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 427 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 428 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 429 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 430 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 431 | /// The size a window placed in `n`'s stead would take, ignoring |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 432 | /// visibility since new windows land on any tag. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 433 | fn size(&self, n: NodeId) -> Size { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 434 | let all = |_: W| true; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 435 | [ |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 436 | self.extent(n, Axis::Horizontal, &all), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 437 | self.extent(n, Axis::Vertical, &all), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 438 | ] |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 439 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 440 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 441 | /// Lays the tree out in `area`. Hidden windows come back as zero-size |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 442 | /// rects on the boundary where they would reappear. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 443 | pub fn arrange(&self, area: Rect, gap: f64, vis: impl Fn(W) -> bool) -> Vec<(W, Rect)> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 444 | let mut out = Vec::with_capacity(self.leaves.len()); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 445 | if let Some(r) = self.root { |
1c8e1b4009f5 | atagen | 2026-10-16 23:19 UTC | 446 | let area = if self.visible(r, &vis) { |
1c8e1b4009f5 | atagen | 2026-10-16 23:19 UTC | 447 | area |
1c8e1b4009f5 | atagen | 2026-10-16 23:19 UTC | 448 | } else { |
1c8e1b4009f5 | atagen | 2026-10-16 23:19 UTC | 449 | Rect::new(area.x + area.w / 2.0, area.y, 0.0, area.h) |
1c8e1b4009f5 | atagen | 2026-10-16 23:19 UTC | 450 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 451 | self.place(r, area, gap, &vis, &mut out); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 452 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 453 | out |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 454 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 455 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 456 | fn place( |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 457 | &self, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 458 | n: NodeId, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 459 | r: Rect, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 460 | gap: f64, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 461 | vis: &impl Fn(W) -> bool, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 462 | out: &mut Vec<(W, Rect)>, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 463 | ) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 464 | let (axis, children, expanded) = match &self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 465 | Kind::Leaf(w, _) => return out.push((*w, r)), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 466 | Kind::Container { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 467 | axis, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 468 | children, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 469 | expanded, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 470 | } => (*axis, children, *expanded), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 471 | }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 472 | let shown: Vec<bool> = children.iter().map(|&c| self.visible(c, vis)).collect(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 473 | let (start, len) = span(r, axis); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 474 | if let Some(e) = expanded.and_then(|e| children.iter().position(|&c| c == e)) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 475 | && shown[e] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 476 | { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 477 | for (i, &c) in children.iter().enumerate() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 478 | let sub = match i.cmp(&e) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 479 | std::cmp::Ordering::Equal => r, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 480 | std::cmp::Ordering::Less => with_span(r, axis, start, 0.0), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 481 | std::cmp::Ordering::Greater => with_span(r, axis, start + len, 0.0), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 482 | }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 483 | self.place(c, sub, gap, vis, out); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 484 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 485 | return; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 486 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 487 | let count = shown.iter().filter(|s| **s).count(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 488 | let extents: Vec<f64> = children |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 489 | .iter() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 490 | .zip(&shown) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 491 | .map(|(&c, &s)| if s { self.extent(c, axis, vis) } else { 0.0 }) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 492 | .collect(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 493 | let total: f64 = extents.iter().sum(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 494 | let gaps = count.saturating_sub(1) as f64; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 495 | let gap = if gaps > 0.0 { gap.min(len / gaps) } else { gap }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 496 | let avail = len - gap * gaps; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 497 | let last = shown.iter().rposition(|s| *s); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 498 | let mut pos = start; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 499 | let mut seen = false; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 500 | for (i, &c) in children.iter().enumerate() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 501 | if shown[i] { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 502 | if seen { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 503 | pos += gap; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 504 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 505 | let l = avail * extents[i] / total; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 506 | self.place(c, with_span(r, axis, pos, l), gap, vis, out); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 507 | pos += l; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 508 | seen = true; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 509 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 510 | let between = seen && last.is_some_and(|l| l > i); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 511 | let at = if between { pos + gap / 2.0 } else { pos }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 512 | self.place(c, with_span(r, axis, at, 0.0), gap, vis, out); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 513 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 514 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 515 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 516 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 517 | fn move_node(&mut self, n: NodeId, dir: Dir, vis: &impl Fn(W) -> bool, enter: bool) -> bool { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 518 | let mut cur = n; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 519 | while let Some((p, i)) = self.position(cur) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 520 | if self.axis(p) == Some(dir.axis()) { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 521 | let hidden_lane = self |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 522 | .adjacent(p, i, dir) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 523 | .filter(|&j| enter && !self.visible(self.children(p)[j], vis)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 524 | if cur != n { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 525 | if let Some(j) = hidden_lane { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 526 | self.join_lane(n, p, j); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 527 | } else { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 528 | let at = if dir.forward() { i + 1 } else { i }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 529 | self.relocate(n, p, at); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 530 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 531 | return true; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 532 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 533 | if let Some(j) = self.next_shown(p, i, dir, vis) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 534 | let sib = self.children(p)[j]; |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 535 | if enter && self.shown_windows(sib, vis) > 1 { |
9c0e9500c151 | atagen | 2026-10-18 09:09 UTC | 536 | self.join_lane(n, p, j); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 537 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 538 | let children = self.children_mut(p); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 539 | children.swap(i, j); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 540 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 541 | return true; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 542 | } |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 543 | if let Some(j) = hidden_lane { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 544 | self.join_lane(n, p, j); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 545 | return true; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 546 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 547 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 548 | cur = p; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 549 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 550 | if cur == n || self.axis(cur) == Some(dir.axis()) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 551 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 552 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 553 | let (old, i) = self.position(n).expect("n is below the root"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 554 | self.unlink(old, i); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 555 | self.normalize(old); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 556 | let r = self.root.expect("the rest of the tree survives"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 557 | if self.axis(r) == Some(dir.axis()) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 558 | let at = if dir.forward() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 559 | self.children(r).len() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 560 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 561 | 0 |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 562 | }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 563 | self.attach(r, at, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 564 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 565 | let c = self.new_container(dir.axis()); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 566 | self.attach(c, 0, r); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 567 | let at = if dir.forward() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 568 | self.children(c).len() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 569 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 570 | 0 |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 571 | }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 572 | self.attach(c, at, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 573 | self.root = Some(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 574 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 575 | true |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 576 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 577 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 578 | fn next_shown(&self, p: NodeId, i: usize, dir: Dir, vis: &impl Fn(W) -> bool) -> Option<usize> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 579 | let children = self.children(p); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 580 | if dir.forward() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 581 | (i + 1..children.len()).find(|&j| self.visible(children[j], vis)) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 582 | } else { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 583 | (0..i).rev().find(|&j| self.visible(children[j], vis)) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 584 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 585 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 586 | |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 587 | fn adjacent(&self, p: NodeId, i: usize, dir: Dir) -> Option<usize> { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 588 | let j = if dir.forward() { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 589 | i + 1 |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 590 | } else { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 591 | i.checked_sub(1)? |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 592 | }; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 593 | (j < self.children(p).len()).then_some(j) |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 594 | } |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 595 | |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 596 | /// Moves `n` to the end of the lane at `p`'s child `j`. A lone window |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 597 | /// there becomes a lane of its own, perpendicular to `p`. |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 598 | fn join_lane(&mut self, n: NodeId, p: NodeId, j: usize) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 599 | let lane = self.children(p)[j]; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 600 | let lane = if self.axis(lane).is_some() { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 601 | lane |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 602 | } else { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 603 | let c = self.new_container(self.axis(p).expect("p is a container").other()); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 604 | self.children_mut(p)[j] = c; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 605 | self.nodes[c].parent = Some(p); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 606 | if let Kind::Container { expanded, .. } = &mut self.nodes[p].kind |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 607 | && *expanded == Some(lane) |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 608 | { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 609 | *expanded = Some(c); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 610 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 611 | self.attach(c, 0, lane); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 612 | c |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 613 | }; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 614 | let end = self.children(lane).len(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 615 | self.relocate(n, lane, end); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 616 | } |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 617 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 618 | fn relocate(&mut self, n: NodeId, p: NodeId, at: usize) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 619 | let (old, i) = self.position(n).expect("relocated nodes have a parent"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 620 | let at = if old == p && i < at { at - 1 } else { at }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 621 | self.unlink(old, i); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 622 | self.attach(p, at, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 623 | self.normalize(old); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 624 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 625 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 626 | /// Places `node` among `p`'s children. A container running along `p`'s |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 627 | /// axis is dissolved into `p` so axes keep alternating. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 628 | fn attach(&mut self, p: NodeId, at: usize, node: NodeId) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 629 | if self.axis(node).is_some() && self.axis(node) == self.axis(p) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 630 | let children = self.children(node).to_vec(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 631 | self.nodes.remove(node); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 632 | for (k, &c) in children.iter().enumerate() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 633 | self.nodes[c].parent = Some(p); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 634 | self.children_mut(p).insert(at + k, c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 635 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 636 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 637 | self.nodes[node].parent = Some(p); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 638 | self.children_mut(p).insert(at, node); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 639 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 640 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 641 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 642 | fn unlink(&mut self, p: NodeId, i: usize) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 643 | let child = self.unlink_dead(p, i); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 644 | self.nodes[child].parent = None; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 645 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 646 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 647 | fn detach(&mut self, n: NodeId) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 648 | match self.position(n) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 649 | Some((p, i)) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 650 | self.unlink(p, i); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 651 | self.normalize(p); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 652 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 653 | None => self.root = None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 654 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 655 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 656 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 657 | /// Repairs a container that lost a child. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 658 | fn normalize(&mut self, c: NodeId) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 659 | if self.children(c).len() >= 2 { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 660 | return; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 661 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 662 | let pos = self.position(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 663 | let was_expanded = pos.is_some_and(|(p, _)| self.expanded(p) == Some(c)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 664 | let Some(Node { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 665 | kind: Kind::Container { children, .. }, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 666 | .. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 667 | }) = self.nodes.remove(c) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 668 | else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 669 | unreachable!("only containers are normalized"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 670 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 671 | let only = children.into_iter().next(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 672 | match (only, pos) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 673 | (None, Some((p, i))) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 674 | self.unlink_dead(p, i); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 675 | self.normalize(p); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 676 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 677 | (None, None) => self.root = None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 678 | (Some(only), Some((p, i))) => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 679 | self.unlink_dead(p, i); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 680 | let leaf_like = self.axis(only) != self.axis(p); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 681 | self.attach(p, i, only); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 682 | if was_expanded |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 683 | && leaf_like |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 684 | && let Kind::Container { expanded, .. } = &mut self.nodes[p].kind |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 685 | { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 686 | *expanded = Some(only); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 687 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 688 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 689 | (Some(only), None) => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 690 | self.nodes[only].parent = None; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 691 | self.root = Some(only); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 692 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 693 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 694 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 695 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 696 | /// Like `unlink`, for a child whose node is already gone. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 697 | fn unlink_dead(&mut self, p: NodeId, i: usize) -> NodeId { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 698 | let Kind::Container { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 699 | children, expanded, .. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 700 | } = &mut self.nodes[p].kind |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 701 | else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 702 | unreachable!("parents are containers"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 703 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 704 | let child = children.remove(i); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 705 | if *expanded == Some(child) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 706 | *expanded = None; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 707 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 708 | child |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 709 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 710 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 711 | fn new_leaf(&mut self, w: W, size: Size) -> NodeId { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 712 | let n = self.nodes.insert(Node { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 713 | parent: None, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 714 | kind: Kind::Leaf(w, size), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 715 | }); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 716 | self.leaves.insert(w, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 717 | n |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 718 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 719 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 720 | fn new_container(&mut self, axis: Axis) -> NodeId { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 721 | self.nodes.insert(Node { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 722 | parent: None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 723 | kind: Kind::Container { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 724 | axis, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 725 | children: Vec::new(), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 726 | expanded: None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 727 | }, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 728 | }) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 729 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 730 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 731 | fn position(&self, n: NodeId) -> Option<(NodeId, usize)> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 732 | let p = self.nodes[n].parent?; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 733 | let i = self.children(p).iter().position(|&c| c == n)?; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 734 | Some((p, i)) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 735 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 736 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 737 | fn axis(&self, n: NodeId) -> Option<Axis> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 738 | match &self.nodes[n].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 739 | Kind::Container { axis, .. } => Some(*axis), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 740 | Kind::Leaf(..) => None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 741 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 742 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 743 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 744 | fn expanded(&self, n: NodeId) -> Option<NodeId> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 745 | match &self.nodes[n].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 746 | Kind::Container { expanded, .. } => *expanded, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 747 | Kind::Leaf(..) => None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 748 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 749 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 750 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 751 | fn children(&self, n: NodeId) -> &[NodeId] { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 752 | match &self.nodes[n].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 753 | Kind::Container { children, .. } => children, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 754 | Kind::Leaf(..) => &[], |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 755 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 756 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 757 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 758 | fn children_mut(&mut self, n: NodeId) -> &mut Vec<NodeId> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 759 | match &mut self.nodes[n].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 760 | Kind::Container { children, .. } => children, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 761 | Kind::Leaf(..) => unreachable!("leaves have no children"), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 762 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 763 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 764 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 765 | fn visible(&self, n: NodeId, vis: &impl Fn(W) -> bool) -> bool { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 766 | match &self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 767 | Kind::Leaf(w, _) => vis(*w), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 768 | Kind::Container { children, .. } => children.iter().any(|&c| self.visible(c, vis)), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 769 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 770 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 771 | |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 772 | fn shown_windows(&self, n: NodeId, vis: &impl Fn(W) -> bool) -> usize { |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 773 | match &self.nodes[n].kind { |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 774 | Kind::Leaf(w, _) => usize::from(vis(*w)), |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 775 | Kind::Container { children, .. } => { |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 776 | children.iter().map(|&c| self.shown_windows(c, vis)).sum() |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 777 | } |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 778 | } |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 779 | } |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 780 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 781 | #[cfg(test)] |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 782 | pub(crate) fn shape(&self) -> String |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 783 | where |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 784 | W: std::fmt::Debug, |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 785 | { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 786 | fn go<W: Copy + Eq + Hash + std::fmt::Debug>(t: &Tree<W>, n: NodeId) -> String { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 787 | match &t.nodes[n].kind { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 788 | Kind::Leaf(w, [x, y]) => format!("{w:?}[{x:.6},{y:.6}]"), |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 789 | Kind::Container { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 790 | axis, |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 791 | children, |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 792 | expanded, |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 793 | } => { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 794 | let inner: Vec<String> = children.iter().map(|&c| go(t, c)).collect(); |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 795 | let e = expanded.and_then(|e| children.iter().position(|&c| c == e)); |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 796 | format!("{axis:?}{e:?}({})", inner.join(" ")) |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 797 | } |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 798 | } |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 799 | } |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 800 | self.root.map(|r| go(self, r)).unwrap_or_default() |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 801 | } |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 802 | |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 803 | #[cfg(test)] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 804 | pub(crate) fn check(&self) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 805 | let reachable = match self.root { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 806 | Some(r) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 807 | assert!(self.nodes[r].parent.is_none()); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 808 | self.check_node(r) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 809 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 810 | None => 0, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 811 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 812 | assert_eq!(reachable, self.nodes.len(), "orphaned nodes"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 813 | for (&w, &n) in &self.leaves { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 814 | assert!(matches!(self.nodes[n].kind, Kind::Leaf(x, _) if x == w)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 815 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 816 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 817 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 818 | #[cfg(test)] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 819 | fn check_node(&self, n: NodeId) -> usize { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 820 | match &self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 821 | Kind::Leaf(_, size) => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 822 | assert!( |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 823 | size.iter().all(|s| *s > 0.0 && s.is_finite()), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 824 | "size {size:?}" |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 825 | ); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 826 | 1 |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 827 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 828 | Kind::Container { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 829 | axis, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 830 | children, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 831 | expanded, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 832 | } => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 833 | assert!( |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 834 | children.len() >= 2, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 835 | "container with {} children", |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 836 | children.len() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 837 | ); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 838 | if let Some(e) = expanded { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 839 | assert!(children.contains(e)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 840 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 841 | let mut count = 1; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 842 | for &c in children { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 843 | assert_eq!(self.nodes[c].parent, Some(n)); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 844 | assert_ne!(self.axis(c), Some(*axis), "axes must alternate"); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 845 | count += self.check_node(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 846 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 847 | count |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 848 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 849 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 850 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 851 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 852 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 853 | fn span(r: Rect, axis: Axis) -> (f64, f64) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 854 | match axis { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 855 | Axis::Horizontal => (r.x, r.w), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 856 | Axis::Vertical => (r.y, r.h), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 857 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 858 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 859 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 860 | fn with_span(r: Rect, axis: Axis, start: f64, len: f64) -> Rect { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 861 | match axis { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 862 | Axis::Horizontal => Rect::new(start, r.y, len, r.h), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 863 | Axis::Vertical => Rect::new(r.x, start, r.w, len), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 864 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 865 | } |