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 | |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 150 | /// Inserts `w` after `anchor` by splitting `anchor` along `axis`. They |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 151 | /// share the space `anchor` had, and its siblings keep theirs. |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 152 | pub fn insert_split(&mut self, w: W, anchor: W, axis: Axis) { |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 153 | let Some(&a) = self.leaves.get(&anchor) else { |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 154 | return self.insert_row(w); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 155 | }; |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 156 | let size = self.size(a); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 157 | let n = self.new_leaf(w, size); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 158 | match self.position(a) { |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 159 | Some((p, i)) if self.axis(p) == Some(axis) => { |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 160 | self.halve(a, axis); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 161 | self.halve(n, axis); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 162 | self.attach(p, i + 1, n); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 163 | } |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 164 | Some((p, i)) => { |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 165 | self.unlink(p, i); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 166 | let c = self.new_container(axis); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 167 | self.attach(c, 0, a); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 168 | self.attach(c, 1, n); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 169 | self.attach(p, i, c); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 170 | } |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 171 | None => { |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 172 | let c = self.new_container(axis); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 173 | self.attach(c, 0, a); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 174 | self.attach(c, 1, n); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 175 | self.root = Some(c); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 176 | } |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 177 | } |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 178 | self.reveal(n, true); |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 179 | } |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 180 | |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 181 | fn halve(&mut self, leaf: NodeId, axis: Axis) { |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 182 | if let Kind::Leaf(_, size) = &mut self.nodes[leaf].kind { |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 183 | size[axis.index()] /= 2.0; |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 184 | } |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 185 | } |
86ef802692f5 | atagen | 2026-10-19 07:43 UTC | 186 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 187 | /// Appends `w` as a row of its own along the stacking axis. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 188 | pub fn insert_row(&mut self, w: W) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 189 | let size = self.root.map_or([1.0; 2], |r| self.size(r)); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 190 | let n = self.new_leaf(w, size); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 191 | match self.root { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 192 | None => self.root = Some(n), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 193 | Some(r) if self.axis(r) == Some(self.stack) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 194 | let end = self.children(r).len(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 195 | self.attach(r, end, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 196 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 197 | Some(r) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 198 | let c = self.new_container(self.stack); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 199 | self.attach(c, 0, r); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 200 | self.attach(c, 1, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 201 | self.root = Some(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 202 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 203 | } |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 204 | self.reveal(n, true); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 205 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 206 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 207 | pub fn remove(&mut self, w: W) -> bool { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 208 | let Some(n) = self.leaves.remove(&w) else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 209 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 210 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 211 | self.detach(n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 212 | self.nodes.remove(n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 213 | true |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 214 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 215 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 216 | pub fn retain(&mut self, mut keep: impl FnMut(W) -> bool) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 217 | let gone: Vec<W> = self.windows().filter(|&w| !keep(w)).collect(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 218 | for w in gone { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 219 | self.remove(w); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 220 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 221 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 222 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 223 | /// Moves a window one step in `dir`. Leaves swap slots with the next |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 224 | /// visible sibling, enter a sibling container, or step out beside their |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 225 | /// container. Hidden windows never move. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 226 | pub fn move_window(&mut self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 227 | let Some(&n) = self.leaves.get(&w).filter(|_| vis(w)) else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 228 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 229 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 230 | self.until_seen(&vis, |t| { |
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 | }) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 234 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 235 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 236 | /// Moves the container holding `w` as a rigid block. A window sitting |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 237 | /// directly in the root is its own block. |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 238 | pub fn move_container(&mut self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 239 | let Some(&n) = self.leaves.get(&w).filter(|_| vis(w)) else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 240 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 241 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 242 | self.until_seen(&vis, |t| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 243 | let block = t.nodes[n] |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 244 | .parent |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 245 | .filter(|&p| Some(p) != t.root) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 246 | .unwrap_or(n); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 247 | let moved = t.move_node(block, dir, &vis, false); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 248 | t.reveal(n, moved) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 249 | }) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 250 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 251 | |
605c44c8c5fc | atagen | 2026-10-17 09:57 UTC | 252 | /// Moves a window to the end of the next visible lane in `dir` among its |
605c44c8c5fc | atagen | 2026-10-17 09:57 UTC | 253 | /// siblings. A lone window there becomes a stack with it. A window with no |
605c44c8c5fc | atagen | 2026-10-17 09:57 UTC | 254 | /// sibling lane that way leaves its stack as `move_window` would, so it |
605c44c8c5fc | atagen | 2026-10-17 09:57 UTC | 255 | /// takes one press to step out and another to merge. |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 256 | pub fn merge_window(&mut self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 257 | let Some(&n) = self.leaves.get(&w).filter(|_| vis(w)) else { |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 258 | return false; |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 259 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 260 | self.until_seen(&vis, |t| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 261 | if let Some((p, i)) = t.position(n) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 262 | && t.axis(p) == Some(dir.axis()) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 263 | && let Some(j) = t.next_shown(p, i, dir, &vis) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 264 | { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 265 | t.join_lane(n, p, j); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 266 | return t.reveal(n, true); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 267 | } |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 268 | let moved = t.move_node(n, dir, &vis, true); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 269 | t.reveal(n, moved) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 270 | }) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 271 | } |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 272 | |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 273 | /// Repeats `step` until the visible layout changes, or undoes it all when |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 274 | /// it never does. Steps around hidden windows otherwise ate keypresses |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 275 | /// and rearranged windows the user could not see. |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 276 | fn until_seen( |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 277 | &mut self, |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 278 | vis: &impl Fn(W) -> bool, |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 279 | mut step: impl FnMut(&mut Self) -> bool, |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 280 | ) -> bool { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 281 | let seen = |t: &Self| -> Vec<(W, Rect)> { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 282 | t.arrange(Rect::new(0.0, 0.0, 1000.0, 1000.0), 0.0, vis) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 283 | .into_iter() |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 284 | .filter(|p| vis(p.0)) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 285 | .collect() |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 286 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 287 | let same = |a: &[(W, Rect)], b: &[(W, Rect)]| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 288 | a.len() == b.len() |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 289 | && a.iter().all(|(v, r)| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 290 | b.iter().any(|(w, s)| { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 291 | v == w |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 292 | && [r.x - s.x, r.y - s.y, r.w - s.w, r.h - s.h] |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 293 | .iter() |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 294 | .all(|d| d.abs() < 1e-6) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 295 | }) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 296 | }) |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 297 | }; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 298 | let before = self.clone(); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 299 | let start = seen(self); |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 300 | for _ in 0..=before.nodes.len() { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 301 | if !step(self) { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 302 | break; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 303 | } |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 304 | if !same(&start, &seen(self)) { |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 305 | return true; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 306 | } |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 307 | } |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 308 | *self = before; |
8f1f743a2f6b | atagen | 2026-10-19 03:43 UTC | 309 | false |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 310 | } |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 311 | |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 312 | /// Undoes any expansion along `leaf`'s ancestry that favours another |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 313 | /// child, once it has `moved` or arrived. An expanded container gives its |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 314 | /// other children no room, so a window moved into one vanished while |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 315 | /// staying focused. |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 316 | fn reveal(&mut self, leaf: NodeId, moved: bool) -> bool { |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 317 | let mut cur = leaf; |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 318 | while moved && let Some(p) = self.nodes[cur].parent { |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 319 | if let Kind::Container { expanded, .. } = &mut self.nodes[p].kind |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 320 | && expanded.is_some_and(|e| e != cur) |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 321 | { |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 322 | *expanded = None; |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 323 | } |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 324 | cur = p; |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 325 | } |
3b1cf5ee2ea2 | atagen | 2026-10-17 13:53 UTC | 326 | moved |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 327 | } |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 328 | |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 329 | /// Whether every container above `w` is expanded towards it. |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 330 | pub fn maximized(&self, w: W, vis: impl Fn(W) -> bool) -> bool { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 331 | let path = self.shown_path(w, &vis); |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 332 | !path.is_empty() && path.iter().all(|&(p, c)| self.expanded(p) == Some(c)) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 333 | } |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 334 | |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 335 | /// `w`'s ancestry as (container, child) pairs, leaving out containers |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 336 | /// where the child has no visible sibling. Those look like nothing on |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 337 | /// screen, so expanding there would do nothing the user could see. |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 338 | fn shown_path(&self, w: W, vis: &impl Fn(W) -> bool) -> Vec<(NodeId, NodeId)> { |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 339 | let Some(&leaf) = self.leaves.get(&w) else { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 340 | return Vec::new(); |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 341 | }; |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 342 | let mut path = Vec::new(); |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 343 | let mut cur = leaf; |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 344 | while let Some(p) = self.nodes[cur].parent { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 345 | let shared = self |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 346 | .children(p) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 347 | .iter() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 348 | .any(|&c| c != cur && self.visible(c, vis)); |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 349 | if shared { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 350 | path.push((p, cur)); |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 351 | } |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 352 | cur = p; |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 353 | } |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 354 | path |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 355 | } |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 356 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 357 | /// Transposes the whole tree. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 358 | pub fn flip(&mut self) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 359 | self.stack = self.stack.other(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 360 | for node in self.nodes.values_mut() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 361 | match &mut node.kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 362 | Kind::Container { axis, .. } => *axis = axis.other(), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 363 | Kind::Leaf(_, size) => size.reverse(), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 364 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 365 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 366 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 367 | |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 368 | /// Toggles expansion for `count` levels of `w`'s visible ancestry, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 369 | /// starting `skip` levels up. Level 0 expands the window inside the |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 370 | /// nearest container where it has a visible sibling. |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 371 | pub fn toggle_expand( |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 372 | &mut self, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 373 | w: W, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 374 | skip: usize, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 375 | count: usize, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 376 | vis: impl Fn(W) -> bool, |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 377 | ) -> bool { |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 378 | let path: Vec<_> = self |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 379 | .shown_path(w, &vis) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 380 | .into_iter() |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 381 | .skip(skip) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 382 | .take(count) |
54559daf11c3 | atagen | 2026-10-17 13:38 UTC | 383 | .collect(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 384 | if path.is_empty() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 385 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 386 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 387 | let all = path.iter().all(|&(p, c)| self.expanded(p) == Some(c)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 388 | for (p, c) in path { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 389 | if let Kind::Container { expanded, .. } = &mut self.nodes[p].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 390 | *expanded = (!all).then_some(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 391 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 392 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 393 | true |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 394 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 395 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 396 | /// Grows `w` by `delta`, a fraction of the visible length of the nearest |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 397 | /// container along `axis` where it has a visible sibling. Hidden siblings |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 398 | /// keep their sizes, and with them their share. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 399 | 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 | 400 | let Some(&leaf) = self.leaves.get(&w) else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 401 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 402 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 403 | let mut cur = leaf; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 404 | while let Some(p) = self.nodes[cur].parent { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 405 | if self.axis(p) == Some(axis) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 406 | let shown: Vec<(NodeId, f64)> = self |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 407 | .children(p) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 408 | .iter() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 409 | .filter(|&&c| self.visible(c, &vis)) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 410 | .map(|&c| (c, self.extent(c, axis, &vis))) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 411 | .collect(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 412 | if shown.len() >= 2 { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 413 | let sum: f64 = shown.iter().map(|s| s.1).sum(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 414 | let old = self.extent(cur, axis, &vis); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 415 | let max = 1.0 - MIN_SHARE * (shown.len() - 1) as f64; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 416 | let new = (old / sum + delta).clamp(MIN_SHARE, max) * sum; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 417 | let scale = (sum - new) / (sum - old); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 418 | for (c, _) in shown { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 419 | self.scale(c, axis, if c == cur { new / old } else { scale }); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 420 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 421 | return true; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 422 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 423 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 424 | cur = p; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 425 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 426 | false |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 427 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 428 | |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 429 | /// The lane a window's split on side `dir` lives in, as the child holding |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 430 | /// the window, its parent and the index of the visible sibling across |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 431 | /// the split. |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 432 | fn split_across( |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 433 | &self, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 434 | w: W, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 435 | dir: Dir, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 436 | vis: &impl Fn(W) -> bool, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 437 | ) -> Option<(NodeId, NodeId, usize)> { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 438 | let mut cur = *self.leaves.get(&w)?; |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 439 | if !self.visible(cur, vis) { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 440 | return None; |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 441 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 442 | while let Some((p, i)) = self.position(cur) { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 443 | if self.axis(p) == Some(dir.axis()) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 444 | && let Some(j) = self.next_shown(p, i, dir, vis) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 445 | { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 446 | return Some((cur, p, j)); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 447 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 448 | cur = p; |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 449 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 450 | None |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 451 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 452 | |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 453 | /// Whether the split on side `dir` of `w` has a visible window across it. |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 454 | pub fn has_split(&self, w: W, dir: Dir, vis: impl Fn(W) -> bool) -> bool { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 455 | self.split_across(w, dir, &vis).is_some() |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 456 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 457 | |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 458 | /// Moves the split on side `dir` of `w` by `px` along that side's axis, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 459 | /// positive toward the right or bottom. Only the two children meeting at |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 460 | /// the split change, so the split follows the pointer. |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 461 | pub fn move_split( |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 462 | &mut self, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 463 | w: W, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 464 | dir: Dir, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 465 | px: f64, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 466 | area: Rect, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 467 | gap: f64, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 468 | vis: impl Fn(W) -> bool, |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 469 | ) -> bool { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 470 | let Some((cur, p, j)) = self.split_across(w, dir, &vis) else { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 471 | return false; |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 472 | }; |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 473 | let axis = dir.axis(); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 474 | let other = self.children(p)[j]; |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 475 | let (first, second) = if dir.forward() { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 476 | (cur, other) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 477 | } else { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 478 | (other, cur) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 479 | }; |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 480 | let mut leaves = Vec::new(); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 481 | self.shown_leaves(first, &vis, &mut leaves); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 482 | let (start, end) = self |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 483 | .arrange(area, gap, &vis) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 484 | .iter() |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 485 | .filter(|(l, _)| leaves.contains(l)) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 486 | .map(|(_, r)| span(*r, axis)) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 487 | .fold((f64::MAX, f64::MIN), |(s, e), (rs, rl)| { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 488 | (s.min(rs), e.max(rs + rl)) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 489 | }); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 490 | let ef = self.extent(first, axis, &vis); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 491 | let es = self.extent(second, axis, &vis); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 492 | if end <= start || ef <= 0.0 || es <= 0.0 { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 493 | return true; |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 494 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 495 | let sum: f64 = self |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 496 | .children(p) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 497 | .iter() |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 498 | .filter(|&&c| self.visible(c, &vis)) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 499 | .map(|&c| self.extent(c, axis, &vis)) |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 500 | .sum(); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 501 | let min = MIN_SHARE * sum; |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 502 | let du = (px * ef / (end - start)).clamp(-(ef - min).max(0.0), (es - min).max(0.0)); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 503 | self.scale(first, axis, (ef + du) / ef); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 504 | self.scale(second, axis, (es - du) / es); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 505 | true |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 506 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 507 | |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 508 | fn shown_leaves(&self, n: NodeId, vis: &impl Fn(W) -> bool, out: &mut Vec<W>) { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 509 | match &self.nodes[n].kind { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 510 | Kind::Leaf(w, _) => { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 511 | if vis(*w) { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 512 | out.push(*w); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 513 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 514 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 515 | Kind::Container { children, .. } => { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 516 | for &c in children { |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 517 | self.shown_leaves(c, vis, out); |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 518 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 519 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 520 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 521 | } |
abe5e42a95d6 | atagen | 2026-10-19 07:52 UTC | 522 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 523 | /// Scales every window under `n` along `axis`, hidden ones too, so they |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 524 | /// keep their proportions for when they are shown again. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 525 | fn scale(&mut self, n: NodeId, axis: Axis, by: f64) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 526 | match &mut self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 527 | Kind::Leaf(_, size) => size[axis.index()] *= by, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 528 | Kind::Container { children, .. } => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 529 | for c in children.clone() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 530 | self.scale(c, axis, by); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 531 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 532 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 533 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 534 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 535 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 536 | /// How much room `n` asks for along `axis` among its siblings. A |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 537 | /// container asks for the mean of what its visible children ask for, so |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 538 | /// windows of other tags sharing a lane change nothing on screen, and a |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 539 | /// window carries its own size wherever it moves. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 540 | fn extent(&self, n: NodeId, axis: Axis, vis: &impl Fn(W) -> bool) -> f64 { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 541 | match &self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 542 | Kind::Leaf(_, size) => size[axis.index()], |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 543 | Kind::Container { children, .. } => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 544 | let shown: Vec<f64> = children |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 545 | .iter() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 546 | .filter(|&&c| self.visible(c, vis)) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 547 | .map(|&c| self.extent(c, axis, vis)) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 548 | .collect(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 549 | if shown.is_empty() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 550 | children |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 551 | .iter() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 552 | .map(|&c| self.extent(c, axis, vis)) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 553 | .sum::<f64>() |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 554 | / children.len() as f64 |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 555 | } else { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 556 | shown.iter().sum::<f64>() / shown.len() as f64 |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 557 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 558 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 559 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 560 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 561 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 562 | /// The size a window placed in `n`'s stead would take, ignoring |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 563 | /// visibility since new windows land on any tag. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 564 | fn size(&self, n: NodeId) -> Size { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 565 | let all = |_: W| true; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 566 | [ |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 567 | self.extent(n, Axis::Horizontal, &all), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 568 | self.extent(n, Axis::Vertical, &all), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 569 | ] |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 570 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 571 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 572 | /// Lays the tree out in `area`. Hidden windows come back as zero-size |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 573 | /// rects on the boundary where they would reappear. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 574 | pub fn arrange(&self, area: Rect, gap: f64, vis: impl Fn(W) -> bool) -> Vec<(W, Rect)> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 575 | let mut out = Vec::with_capacity(self.leaves.len()); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 576 | if let Some(r) = self.root { |
1c8e1b4009f5 | atagen | 2026-10-16 23:19 UTC | 577 | let area = if self.visible(r, &vis) { |
1c8e1b4009f5 | atagen | 2026-10-16 23:19 UTC | 578 | area |
6d227f43182f | atagen | 2026-10-19 08:40 UTC | 579 | } else if area.w >= area.h { |
6d227f43182f | atagen | 2026-10-19 08:40 UTC | 580 | Rect::new(area.right(), area.y, 0.0, area.h) |
1c8e1b4009f5 | atagen | 2026-10-16 23:19 UTC | 581 | } else { |
6d227f43182f | atagen | 2026-10-19 08:40 UTC | 582 | Rect::new(area.x, area.bottom(), area.w, 0.0) |
1c8e1b4009f5 | atagen | 2026-10-16 23:19 UTC | 583 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 584 | self.place(r, area, gap, &vis, &mut out); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 585 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 586 | out |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 587 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 588 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 589 | fn place( |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 590 | &self, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 591 | n: NodeId, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 592 | r: Rect, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 593 | gap: f64, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 594 | vis: &impl Fn(W) -> bool, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 595 | out: &mut Vec<(W, Rect)>, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 596 | ) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 597 | let (axis, children, expanded) = match &self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 598 | Kind::Leaf(w, _) => return out.push((*w, r)), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 599 | Kind::Container { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 600 | axis, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 601 | children, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 602 | expanded, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 603 | } => (*axis, children, *expanded), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 604 | }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 605 | let shown: Vec<bool> = children.iter().map(|&c| self.visible(c, vis)).collect(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 606 | let (start, len) = span(r, axis); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 607 | if let Some(e) = expanded.and_then(|e| children.iter().position(|&c| c == e)) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 608 | && shown[e] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 609 | { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 610 | for (i, &c) in children.iter().enumerate() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 611 | let sub = match i.cmp(&e) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 612 | std::cmp::Ordering::Equal => r, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 613 | std::cmp::Ordering::Less => with_span(r, axis, start, 0.0), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 614 | std::cmp::Ordering::Greater => with_span(r, axis, start + len, 0.0), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 615 | }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 616 | self.place(c, sub, gap, vis, out); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 617 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 618 | return; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 619 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 620 | let count = shown.iter().filter(|s| **s).count(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 621 | let extents: Vec<f64> = children |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 622 | .iter() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 623 | .zip(&shown) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 624 | .map(|(&c, &s)| if s { self.extent(c, axis, vis) } else { 0.0 }) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 625 | .collect(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 626 | let total: f64 = extents.iter().sum(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 627 | let gaps = count.saturating_sub(1) as f64; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 628 | let gap = if gaps > 0.0 { gap.min(len / gaps) } else { gap }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 629 | let avail = len - gap * gaps; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 630 | let last = shown.iter().rposition(|s| *s); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 631 | let mut pos = start; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 632 | let mut seen = false; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 633 | for (i, &c) in children.iter().enumerate() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 634 | if shown[i] { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 635 | if seen { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 636 | pos += gap; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 637 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 638 | let l = avail * extents[i] / total; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 639 | self.place(c, with_span(r, axis, pos, l), gap, vis, out); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 640 | pos += l; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 641 | seen = true; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 642 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 643 | let between = seen && last.is_some_and(|l| l > i); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 644 | let at = if between { pos + gap / 2.0 } else { pos }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 645 | self.place(c, with_span(r, axis, at, 0.0), gap, vis, out); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 646 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 647 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 648 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 649 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 650 | 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 | 651 | let mut cur = n; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 652 | while let Some((p, i)) = self.position(cur) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 653 | if self.axis(p) == Some(dir.axis()) { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 654 | let hidden_lane = self |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 655 | .adjacent(p, i, dir) |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 656 | .filter(|&j| enter && !self.visible(self.children(p)[j], vis)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 657 | if cur != n { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 658 | if let Some(j) = hidden_lane { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 659 | self.join_lane(n, p, j); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 660 | } else { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 661 | let at = if dir.forward() { i + 1 } else { i }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 662 | self.relocate(n, p, at); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 663 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 664 | return true; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 665 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 666 | if let Some(j) = self.next_shown(p, i, dir, vis) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 667 | let sib = self.children(p)[j]; |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 668 | if enter && self.shown_windows(sib, vis) > 1 { |
9c0e9500c151 | atagen | 2026-10-18 09:09 UTC | 669 | self.join_lane(n, p, j); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 670 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 671 | let children = self.children_mut(p); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 672 | children.swap(i, j); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 673 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 674 | return true; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 675 | } |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 676 | if let Some(j) = hidden_lane { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 677 | self.join_lane(n, p, j); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 678 | return true; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 679 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 680 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 681 | cur = p; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 682 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 683 | if cur == n || self.axis(cur) == Some(dir.axis()) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 684 | return false; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 685 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 686 | let (old, i) = self.position(n).expect("n is below the root"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 687 | self.unlink(old, i); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 688 | self.normalize(old); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 689 | let r = self.root.expect("the rest of the tree survives"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 690 | if self.axis(r) == Some(dir.axis()) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 691 | let at = if dir.forward() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 692 | self.children(r).len() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 693 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 694 | 0 |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 695 | }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 696 | self.attach(r, at, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 697 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 698 | let c = self.new_container(dir.axis()); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 699 | self.attach(c, 0, r); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 700 | let at = if dir.forward() { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 701 | self.children(c).len() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 702 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 703 | 0 |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 704 | }; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 705 | self.attach(c, at, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 706 | self.root = Some(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 707 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 708 | true |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 709 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 710 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 711 | 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 | 712 | let children = self.children(p); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 713 | if dir.forward() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 714 | (i + 1..children.len()).find(|&j| self.visible(children[j], vis)) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 715 | } else { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 716 | (0..i).rev().find(|&j| self.visible(children[j], vis)) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 717 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 718 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 719 | |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 720 | fn adjacent(&self, p: NodeId, i: usize, dir: Dir) -> Option<usize> { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 721 | let j = if dir.forward() { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 722 | i + 1 |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 723 | } else { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 724 | i.checked_sub(1)? |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 725 | }; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 726 | (j < self.children(p).len()).then_some(j) |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 727 | } |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 728 | |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 729 | /// Moves `n` to the end of the lane at `p`'s child `j`. A lone window |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 730 | /// there becomes a lane of its own, perpendicular to `p`. |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 731 | fn join_lane(&mut self, n: NodeId, p: NodeId, j: usize) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 732 | let lane = self.children(p)[j]; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 733 | let lane = if self.axis(lane).is_some() { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 734 | lane |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 735 | } else { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 736 | let c = self.new_container(self.axis(p).expect("p is a container").other()); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 737 | self.children_mut(p)[j] = c; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 738 | self.nodes[c].parent = Some(p); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 739 | if let Kind::Container { expanded, .. } = &mut self.nodes[p].kind |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 740 | && *expanded == Some(lane) |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 741 | { |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 742 | *expanded = Some(c); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 743 | } |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 744 | self.attach(c, 0, lane); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 745 | c |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 746 | }; |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 747 | let end = self.children(lane).len(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 748 | self.relocate(n, lane, end); |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 749 | } |
60f00dd4f2be | atagen | 2026-10-16 11:50 UTC | 750 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 751 | fn relocate(&mut self, n: NodeId, p: NodeId, at: usize) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 752 | let (old, i) = self.position(n).expect("relocated nodes have a parent"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 753 | let at = if old == p && i < at { at - 1 } else { at }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 754 | self.unlink(old, i); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 755 | self.attach(p, at, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 756 | self.normalize(old); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 757 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 758 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 759 | /// Places `node` among `p`'s children. A container running along `p`'s |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 760 | /// axis is dissolved into `p` so axes keep alternating. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 761 | fn attach(&mut self, p: NodeId, at: usize, node: NodeId) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 762 | if self.axis(node).is_some() && self.axis(node) == self.axis(p) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 763 | let children = self.children(node).to_vec(); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 764 | self.nodes.remove(node); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 765 | for (k, &c) in children.iter().enumerate() { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 766 | self.nodes[c].parent = Some(p); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 767 | self.children_mut(p).insert(at + k, c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 768 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 769 | } else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 770 | self.nodes[node].parent = Some(p); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 771 | self.children_mut(p).insert(at, node); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 772 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 773 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 774 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 775 | fn unlink(&mut self, p: NodeId, i: usize) { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 776 | let child = self.unlink_dead(p, i); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 777 | self.nodes[child].parent = None; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 778 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 779 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 780 | fn detach(&mut self, n: NodeId) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 781 | match self.position(n) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 782 | Some((p, i)) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 783 | self.unlink(p, i); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 784 | self.normalize(p); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 785 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 786 | None => self.root = None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 787 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 788 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 789 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 790 | /// Repairs a container that lost a child. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 791 | fn normalize(&mut self, c: NodeId) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 792 | if self.children(c).len() >= 2 { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 793 | return; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 794 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 795 | let pos = self.position(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 796 | let was_expanded = pos.is_some_and(|(p, _)| self.expanded(p) == Some(c)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 797 | let Some(Node { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 798 | kind: Kind::Container { children, .. }, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 799 | .. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 800 | }) = self.nodes.remove(c) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 801 | else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 802 | unreachable!("only containers are normalized"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 803 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 804 | let only = children.into_iter().next(); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 805 | match (only, pos) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 806 | (None, Some((p, i))) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 807 | self.unlink_dead(p, i); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 808 | self.normalize(p); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 809 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 810 | (None, None) => self.root = None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 811 | (Some(only), Some((p, i))) => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 812 | self.unlink_dead(p, i); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 813 | let leaf_like = self.axis(only) != self.axis(p); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 814 | self.attach(p, i, only); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 815 | if was_expanded |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 816 | && leaf_like |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 817 | && let Kind::Container { expanded, .. } = &mut self.nodes[p].kind |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 818 | { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 819 | *expanded = Some(only); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 820 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 821 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 822 | (Some(only), None) => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 823 | self.nodes[only].parent = None; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 824 | self.root = Some(only); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 825 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 826 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 827 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 828 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 829 | /// Like `unlink`, for a child whose node is already gone. |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 830 | fn unlink_dead(&mut self, p: NodeId, i: usize) -> NodeId { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 831 | let Kind::Container { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 832 | children, expanded, .. |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 833 | } = &mut self.nodes[p].kind |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 834 | else { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 835 | unreachable!("parents are containers"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 836 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 837 | let child = children.remove(i); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 838 | if *expanded == Some(child) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 839 | *expanded = None; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 840 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 841 | child |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 842 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 843 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 844 | fn new_leaf(&mut self, w: W, size: Size) -> NodeId { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 845 | let n = self.nodes.insert(Node { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 846 | parent: None, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 847 | kind: Kind::Leaf(w, size), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 848 | }); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 849 | self.leaves.insert(w, n); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 850 | n |
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 new_container(&mut self, axis: Axis) -> NodeId { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 854 | self.nodes.insert(Node { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 855 | parent: None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 856 | kind: Kind::Container { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 857 | axis, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 858 | children: Vec::new(), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 859 | expanded: None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 860 | }, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 861 | }) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 862 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 863 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 864 | fn position(&self, n: NodeId) -> Option<(NodeId, usize)> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 865 | let p = self.nodes[n].parent?; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 866 | let i = self.children(p).iter().position(|&c| c == n)?; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 867 | Some((p, i)) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 868 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 869 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 870 | fn axis(&self, n: NodeId) -> Option<Axis> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 871 | match &self.nodes[n].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 872 | Kind::Container { axis, .. } => Some(*axis), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 873 | Kind::Leaf(..) => None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 874 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 875 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 876 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 877 | fn expanded(&self, n: NodeId) -> Option<NodeId> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 878 | match &self.nodes[n].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 879 | Kind::Container { expanded, .. } => *expanded, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 880 | Kind::Leaf(..) => None, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 881 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 882 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 883 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 884 | fn children(&self, n: NodeId) -> &[NodeId] { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 885 | match &self.nodes[n].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 886 | Kind::Container { children, .. } => children, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 887 | Kind::Leaf(..) => &[], |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 888 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 889 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 890 | |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 891 | fn children_mut(&mut self, n: NodeId) -> &mut Vec<NodeId> { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 892 | match &mut self.nodes[n].kind { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 893 | Kind::Container { children, .. } => children, |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 894 | Kind::Leaf(..) => unreachable!("leaves have no children"), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 895 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 896 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 897 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 898 | fn visible(&self, n: NodeId, vis: &impl Fn(W) -> bool) -> bool { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 899 | match &self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 900 | Kind::Leaf(w, _) => vis(*w), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 901 | Kind::Container { children, .. } => children.iter().any(|&c| self.visible(c, vis)), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 902 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 903 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 904 | |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 905 | fn shown_windows(&self, n: NodeId, vis: &impl Fn(W) -> bool) -> usize { |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 906 | match &self.nodes[n].kind { |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 907 | Kind::Leaf(w, _) => usize::from(vis(*w)), |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 908 | Kind::Container { children, .. } => { |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 909 | children.iter().map(|&c| self.shown_windows(c, vis)).sum() |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 910 | } |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 911 | } |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 912 | } |
00c7b3db8a3f | atagen | 2026-10-18 10:19 UTC | 913 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 914 | #[cfg(test)] |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 915 | pub(crate) fn shape(&self) -> String |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 916 | where |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 917 | W: std::fmt::Debug, |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 918 | { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 919 | fn go<W: Copy + Eq + Hash + std::fmt::Debug>(t: &Tree<W>, n: NodeId) -> String { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 920 | match &t.nodes[n].kind { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 921 | Kind::Leaf(w, [x, y]) => format!("{w:?}[{x:.6},{y:.6}]"), |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 922 | Kind::Container { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 923 | axis, |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 924 | children, |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 925 | expanded, |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 926 | } => { |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 927 | let inner: Vec<String> = children.iter().map(|&c| go(t, c)).collect(); |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 928 | let e = expanded.and_then(|e| children.iter().position(|&c| c == e)); |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 929 | format!("{axis:?}{e:?}({})", inner.join(" ")) |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 930 | } |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 931 | } |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 932 | } |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 933 | self.root.map(|r| go(self, r)).unwrap_or_default() |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 934 | } |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 935 | |
ae47743b5745 | atagen | 2026-10-19 03:49 UTC | 936 | #[cfg(test)] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 937 | pub(crate) fn check(&self) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 938 | let reachable = match self.root { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 939 | Some(r) => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 940 | assert!(self.nodes[r].parent.is_none()); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 941 | self.check_node(r) |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 942 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 943 | None => 0, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 944 | }; |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 945 | assert_eq!(reachable, self.nodes.len(), "orphaned nodes"); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 946 | for (&w, &n) in &self.leaves { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 947 | assert!(matches!(self.nodes[n].kind, Kind::Leaf(x, _) if x == w)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 948 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 949 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 950 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 951 | #[cfg(test)] |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 952 | fn check_node(&self, n: NodeId) -> usize { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 953 | match &self.nodes[n].kind { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 954 | Kind::Leaf(_, size) => { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 955 | assert!( |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 956 | size.iter().all(|s| *s > 0.0 && s.is_finite()), |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 957 | "size {size:?}" |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 958 | ); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 959 | 1 |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 960 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 961 | Kind::Container { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 962 | axis, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 963 | children, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 964 | expanded, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 965 | } => { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 966 | assert!( |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 967 | children.len() >= 2, |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 968 | "container with {} children", |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 969 | children.len() |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 970 | ); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 971 | if let Some(e) = expanded { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 972 | assert!(children.contains(e)); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 973 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 974 | let mut count = 1; |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 975 | for &c in children { |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 976 | assert_eq!(self.nodes[c].parent, Some(n)); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 977 | assert_ne!(self.axis(c), Some(*axis), "axes must alternate"); |
e2443dec4dcd | atagen | 2026-10-18 09:58 UTC | 978 | count += self.check_node(c); |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 979 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 980 | count |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 981 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 982 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 983 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 984 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 985 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 986 | fn span(r: Rect, axis: Axis) -> (f64, f64) { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 987 | match axis { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 988 | Axis::Horizontal => (r.x, r.w), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 989 | Axis::Vertical => (r.y, r.h), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 990 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 991 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 992 | |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 993 | fn with_span(r: Rect, axis: Axis, start: f64, len: f64) -> Rect { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 994 | match axis { |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 995 | Axis::Horizontal => Rect::new(start, r.y, len, r.h), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 996 | Axis::Vertical => Rect::new(r.x, start, r.w, len), |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 997 | } |
9baaafa4b167 | atagen | 2026-10-16 10:04 UTC | 998 | } |