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