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