1use crate::compose::{
4 AdaptStrategy, PaneAdaptedValue, ThreePaneScaffoldAdaptStrategies, ThreePaneScaffoldRole,
5};
6
7#[derive(Clone, Debug, PartialEq)]
9pub struct ThreePaneScaffoldDestinationItem<T> {
10 pub pane: ThreePaneScaffoldRole,
12 pub content_key: Option<T>,
14}
15
16impl<T> ThreePaneScaffoldDestinationItem<T> {
17 pub fn new(
19 pane: ThreePaneScaffoldRole,
20 content_key: Option<T>,
21 ) -> ThreePaneScaffoldDestinationItem<T> {
22 ThreePaneScaffoldDestinationItem { pane, content_key }
23 }
24}
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
30pub struct ThreePaneScaffoldValue {
31 pub primary: PaneAdaptedValue,
33 pub secondary: PaneAdaptedValue,
35 pub tertiary: PaneAdaptedValue,
37}
38
39impl ThreePaneScaffoldValue {
40 pub fn get(&self, role: ThreePaneScaffoldRole) -> PaneAdaptedValue {
42 match role {
43 ThreePaneScaffoldRole::Primary => self.primary,
44 ThreePaneScaffoldRole::Secondary => self.secondary,
45 ThreePaneScaffoldRole::Tertiary => self.tertiary,
46 }
47 }
48
49 pub fn pane_count(&self) -> usize {
51 [self.primary, self.secondary, self.tertiary]
52 .into_iter()
53 .filter(|value| *value != PaneAdaptedValue::Hidden)
54 .count()
55 }
56
57 pub fn calculate<T>(
63 max_horizontal_partitions: usize,
64 max_vertical_partitions: usize,
65 adapt_strategies: &ThreePaneScaffoldAdaptStrategies,
66 destination_history: &[ThreePaneScaffoldDestinationItem<T>],
67 ) -> ThreePaneScaffoldValue {
68 ThreePaneScaffoldValue::from_pane_history(
69 max_horizontal_partitions,
70 max_vertical_partitions,
71 adapt_strategies,
72 destination_history.iter().map(|item| item.pane),
73 )
74 }
75
76 pub(crate) fn from_pane_history(
78 max_horizontal_partitions: usize,
79 max_vertical_partitions: usize,
80 adapt_strategies: &ThreePaneScaffoldAdaptStrategies,
81 pane_history: impl DoubleEndedIterator<Item = ThreePaneScaffoldRole>,
82 ) -> ThreePaneScaffoldValue {
83 let mut values = [None; 3];
84 let mut expanded_count = 0;
85 let mut check_reflowed_pane = max_horizontal_partitions == 1
86 && max_vertical_partitions > 1
87 && ThreePaneScaffoldRole::ALL
88 .into_iter()
89 .any(|role| matches!(adapt_strategies.get(role), AdaptStrategy::Reflow { .. }));
90
91 let panes_by_priority = pane_history.rev().chain(ThreePaneScaffoldRole::ALL);
92 for pane in panes_by_priority {
93 let has_available_partition = expanded_count < max_horizontal_partitions;
94 if !has_available_partition && !check_reflowed_pane {
95 break;
96 }
97 if values[pane.index()].is_some() {
98 continue;
99 }
100
101 let mut reflowed_pane = None;
102 let mut anchor_pane = pane;
103 let mut anchor_value = None;
104 if check_reflowed_pane
105 && let AdaptStrategy::Reflow { reflow_under } = adapt_strategies.get(pane)
106 {
107 reflowed_pane = Some(pane);
108 anchor_pane = reflow_under;
109 anchor_value = values[anchor_pane.index()];
110 }
111
112 match anchor_value {
113 None => {
114 if has_available_partition {
115 values[anchor_pane.index()] = Some(PaneAdaptedValue::Expanded);
116 expanded_count += 1;
117 } else {
118 continue;
119 }
120 }
121 Some(PaneAdaptedValue::Expanded) => {}
122 Some(_) => continue,
123 }
124
125 if let Some(pane) = reflowed_pane {
126 values[pane.index()] = Some(PaneAdaptedValue::Reflowed {
127 reflow_under: anchor_pane,
128 });
129 check_reflowed_pane = false;
130 }
131 }
132
133 ThreePaneScaffoldValue {
134 primary: values[0].unwrap_or(PaneAdaptedValue::Hidden),
135 secondary: values[1].unwrap_or(PaneAdaptedValue::Hidden),
136 tertiary: values[2].unwrap_or(PaneAdaptedValue::Hidden),
137 }
138 }
139}
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144
145 fn destination(pane: ThreePaneScaffoldRole) -> ThreePaneScaffoldDestinationItem<()> {
146 ThreePaneScaffoldDestinationItem::new(pane, None)
147 }
148
149 fn strategies(
150 primary: AdaptStrategy,
151 secondary: AdaptStrategy,
152 tertiary: AdaptStrategy,
153 ) -> ThreePaneScaffoldAdaptStrategies {
154 ThreePaneScaffoldAdaptStrategies {
155 primary,
156 secondary,
157 tertiary,
158 }
159 }
160
161 #[test]
162 fn one_partition_without_history_expands_primary() {
163 let value = ThreePaneScaffoldValue::calculate(
164 1,
165 1,
166 &strategies(
167 AdaptStrategy::Hide,
168 AdaptStrategy::Hide,
169 AdaptStrategy::Hide,
170 ),
171 &[] as &[ThreePaneScaffoldDestinationItem<()>],
172 );
173 assert_eq!(value.primary, PaneAdaptedValue::Expanded);
174 assert_eq!(value.secondary, PaneAdaptedValue::Hidden);
175 assert_eq!(value.tertiary, PaneAdaptedValue::Hidden);
176 }
177
178 #[test]
179 fn the_latest_destination_takes_the_partition() {
180 let value = ThreePaneScaffoldValue::calculate(
181 1,
182 1,
183 &strategies(
184 AdaptStrategy::Hide,
185 AdaptStrategy::Hide,
186 AdaptStrategy::Hide,
187 ),
188 &[
189 destination(ThreePaneScaffoldRole::Tertiary),
190 destination(ThreePaneScaffoldRole::Secondary),
191 ],
192 );
193 assert_eq!(value.primary, PaneAdaptedValue::Hidden);
194 assert_eq!(value.secondary, PaneAdaptedValue::Expanded);
195 assert_eq!(value.tertiary, PaneAdaptedValue::Hidden);
196 }
197
198 #[test]
199 fn two_partitions_expand_primary_and_secondary() {
200 let value = ThreePaneScaffoldValue::calculate(
201 2,
202 1,
203 &strategies(
204 AdaptStrategy::Hide,
205 AdaptStrategy::Hide,
206 AdaptStrategy::Hide,
207 ),
208 &[] as &[ThreePaneScaffoldDestinationItem<()>],
209 );
210 assert_eq!(value.primary, PaneAdaptedValue::Expanded);
211 assert_eq!(value.secondary, PaneAdaptedValue::Expanded);
212 assert_eq!(value.tertiary, PaneAdaptedValue::Hidden);
213 }
214
215 #[test]
216 fn three_partitions_expand_every_pane() {
217 let value = ThreePaneScaffoldValue::calculate(
218 3,
219 1,
220 &strategies(
221 AdaptStrategy::Hide,
222 AdaptStrategy::Hide,
223 AdaptStrategy::Hide,
224 ),
225 &[] as &[ThreePaneScaffoldDestinationItem<()>],
226 );
227 assert_eq!(value.pane_count(), 3);
228 }
229
230 #[test]
231 fn a_reflowed_pane_stacks_under_its_anchor_on_a_tall_single_column() {
232 let value = ThreePaneScaffoldValue::calculate(
233 1,
234 2,
235 &strategies(
236 AdaptStrategy::Hide,
237 AdaptStrategy::Reflow {
238 reflow_under: ThreePaneScaffoldRole::Tertiary,
239 },
240 AdaptStrategy::Hide,
241 ),
242 &[destination(ThreePaneScaffoldRole::Secondary)],
243 );
244 assert_eq!(value.primary, PaneAdaptedValue::Hidden);
245 assert_eq!(
246 value.secondary,
247 PaneAdaptedValue::Reflowed {
248 reflow_under: ThreePaneScaffoldRole::Tertiary
249 }
250 );
251 assert_eq!(value.tertiary, PaneAdaptedValue::Expanded);
252 }
253
254 #[test]
255 fn an_expanded_anchor_takes_the_reflowed_pane() {
256 let value = ThreePaneScaffoldValue::calculate(
257 1,
258 2,
259 &strategies(
260 AdaptStrategy::Hide,
261 AdaptStrategy::Hide,
262 AdaptStrategy::Reflow {
263 reflow_under: ThreePaneScaffoldRole::Primary,
264 },
265 ),
266 &[
267 destination(ThreePaneScaffoldRole::Tertiary),
268 destination(ThreePaneScaffoldRole::Secondary),
269 destination(ThreePaneScaffoldRole::Primary),
270 ],
271 );
272 assert_eq!(value.primary, PaneAdaptedValue::Expanded);
273 assert_eq!(value.secondary, PaneAdaptedValue::Hidden);
274 assert_eq!(
275 value.tertiary,
276 PaneAdaptedValue::Reflowed {
277 reflow_under: ThreePaneScaffoldRole::Primary
278 }
279 );
280 }
281
282 #[test]
283 fn a_reflow_needs_a_second_vertical_partition() {
284 let value = ThreePaneScaffoldValue::calculate(
285 1,
286 1,
287 &strategies(
288 AdaptStrategy::Reflow {
289 reflow_under: ThreePaneScaffoldRole::Tertiary,
290 },
291 AdaptStrategy::Hide,
292 AdaptStrategy::Hide,
293 ),
294 &[] as &[ThreePaneScaffoldDestinationItem<()>],
295 );
296 assert_eq!(value.primary, PaneAdaptedValue::Expanded);
297 assert_eq!(value.secondary, PaneAdaptedValue::Hidden);
298 assert_eq!(value.tertiary, PaneAdaptedValue::Hidden);
299 }
300
301 #[test]
302 fn two_columns_never_reflow() {
303 let value = ThreePaneScaffoldValue::calculate(
304 2,
305 2,
306 &strategies(
307 AdaptStrategy::Hide,
308 AdaptStrategy::Reflow {
309 reflow_under: ThreePaneScaffoldRole::Primary,
310 },
311 AdaptStrategy::Hide,
312 ),
313 &[
314 destination(ThreePaneScaffoldRole::Primary),
315 destination(ThreePaneScaffoldRole::Tertiary),
316 destination(ThreePaneScaffoldRole::Secondary),
317 ],
318 );
319 assert_eq!(value.primary, PaneAdaptedValue::Hidden);
320 assert_eq!(value.secondary, PaneAdaptedValue::Expanded);
321 assert_eq!(value.tertiary, PaneAdaptedValue::Expanded);
322 }
323}