Skip to main content

waft_material/compose/
pane_scaffold_directive.rs

1//! How many panes a window has room for, and how they are spaced.
2
3use crate::compose::{WindowHeightSizeClass, WindowSizeClass, WindowWidthSizeClass};
4
5/// How many panes the window has room for, and how they are spaced.
6///
7/// The values are Compose's `PaneScaffoldDirective` without the posture-derived bounds: a hinge
8/// waits for the embedders to report one.
9#[derive(Clone, Debug, PartialEq)]
10pub struct PaneScaffoldDirective {
11    /// The maximum number of panes side by side.
12    pub max_horizontal_partitions: usize,
13    /// The spacer between two panes side by side, in logical pixels.
14    pub horizontal_partition_spacer_size: f64,
15    /// The maximum number of panes stacked.
16    pub max_vertical_partitions: usize,
17    /// The spacer between two stacked panes, in logical pixels.
18    pub vertical_partition_spacer_size: f64,
19    /// The width a pane asks for when it names none.
20    pub default_pane_preferred_width: f64,
21    /// The height a reflowed pane asks for when it names none.
22    pub default_pane_preferred_height: f64,
23}
24
25impl PaneScaffoldDirective {
26    /// Compose's default preferred pane width, in logical pixels.
27    pub const DEFAULT_PANE_PREFERRED_WIDTH: f64 = 360.0;
28    /// Compose's default preferred pane width on a large window, in logical pixels.
29    pub const DEFAULT_PANE_PREFERRED_WIDTH_XL: f64 = 412.0;
30    /// Compose's default preferred pane height, in logical pixels.
31    pub const DEFAULT_PANE_PREFERRED_HEIGHT: f64 = 420.0;
32
33    /// One partition below Expanded, two at Expanded and three from Large; 24-pixel spacers.
34    pub fn from_window_size_class(size_class: WindowSizeClass) -> PaneScaffoldDirective {
35        let (max_horizontal_partitions, horizontal_partition_spacer_size, width) =
36            match size_class.width {
37                WindowWidthSizeClass::Compact | WindowWidthSizeClass::Medium => {
38                    (1, 0.0, Self::DEFAULT_PANE_PREFERRED_WIDTH)
39                }
40                WindowWidthSizeClass::Expanded => (2, 24.0, Self::DEFAULT_PANE_PREFERRED_WIDTH),
41                WindowWidthSizeClass::Large | WindowWidthSizeClass::ExtraLarge => {
42                    (3, 24.0, Self::DEFAULT_PANE_PREFERRED_WIDTH_XL)
43                }
44            };
45        // A single column on a tall window stacks a reflowed pane under its anchor.
46        let (max_vertical_partitions, vertical_partition_spacer_size) = if max_horizontal_partitions
47            == 1
48            && size_class.height == WindowHeightSizeClass::Expanded
49        {
50            (2, 24.0)
51        } else {
52            (1, 0.0)
53        };
54        PaneScaffoldDirective {
55            max_horizontal_partitions,
56            horizontal_partition_spacer_size,
57            max_vertical_partitions,
58            vertical_partition_spacer_size,
59            default_pane_preferred_width: width,
60            default_pane_preferred_height: Self::DEFAULT_PANE_PREFERRED_HEIGHT,
61        }
62    }
63
64    /// The same, with two panes already at Medium width.
65    pub fn with_two_panes_on_medium_width(size_class: WindowSizeClass) -> PaneScaffoldDirective {
66        let mut directive = PaneScaffoldDirective::from_window_size_class(size_class);
67        if size_class.width == WindowWidthSizeClass::Medium {
68            directive.max_horizontal_partitions = 2;
69            directive.horizontal_partition_spacer_size = 24.0;
70            directive.max_vertical_partitions = 1;
71            directive.vertical_partition_spacer_size = 0.0;
72        }
73        directive
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    fn size(width: WindowWidthSizeClass, height: WindowHeightSizeClass) -> WindowSizeClass {
82        WindowSizeClass { width, height }
83    }
84
85    #[test]
86    fn a_compact_window_has_one_pane_and_no_spacers() {
87        let directive = PaneScaffoldDirective::from_window_size_class(size(
88            WindowWidthSizeClass::Compact,
89            WindowHeightSizeClass::Medium,
90        ));
91        assert_eq!(directive.max_horizontal_partitions, 1);
92        assert_eq!(directive.max_vertical_partitions, 1);
93        assert_eq!(directive.horizontal_partition_spacer_size, 0.0);
94        assert_eq!(directive.vertical_partition_spacer_size, 0.0);
95        assert_eq!(directive.default_pane_preferred_width, 360.0);
96        assert_eq!(directive.default_pane_preferred_height, 420.0);
97    }
98
99    #[test]
100    fn a_tall_single_column_stacks_reflowed_panes() {
101        let directive = PaneScaffoldDirective::from_window_size_class(size(
102            WindowWidthSizeClass::Medium,
103            WindowHeightSizeClass::Expanded,
104        ));
105        assert_eq!(directive.max_horizontal_partitions, 1);
106        assert_eq!(directive.max_vertical_partitions, 2);
107        assert_eq!(directive.vertical_partition_spacer_size, 24.0);
108    }
109
110    #[test]
111    fn an_expanded_window_has_two_panes_with_a_24_pixel_spacer() {
112        let directive = PaneScaffoldDirective::from_window_size_class(size(
113            WindowWidthSizeClass::Expanded,
114            WindowHeightSizeClass::Expanded,
115        ));
116        assert_eq!(directive.max_horizontal_partitions, 2);
117        assert_eq!(directive.max_vertical_partitions, 1);
118        assert_eq!(directive.horizontal_partition_spacer_size, 24.0);
119        assert_eq!(directive.default_pane_preferred_width, 360.0);
120    }
121
122    #[test]
123    fn an_extra_large_window_has_three_panes_and_the_wide_default() {
124        let directive = PaneScaffoldDirective::from_window_size_class(size(
125            WindowWidthSizeClass::ExtraLarge,
126            WindowHeightSizeClass::Expanded,
127        ));
128        assert_eq!(directive.max_horizontal_partitions, 3);
129        assert_eq!(directive.horizontal_partition_spacer_size, 24.0);
130        assert_eq!(directive.default_pane_preferred_width, 412.0);
131    }
132
133    #[test]
134    fn a_medium_window_can_take_two_panes_when_asked() {
135        let directive = PaneScaffoldDirective::with_two_panes_on_medium_width(size(
136            WindowWidthSizeClass::Medium,
137            WindowHeightSizeClass::Expanded,
138        ));
139        assert_eq!(directive.max_horizontal_partitions, 2);
140        assert_eq!(directive.horizontal_partition_spacer_size, 24.0);
141        assert_eq!(directive.max_vertical_partitions, 1);
142        assert_eq!(directive.vertical_partition_spacer_size, 0.0);
143    }
144
145    #[test]
146    fn two_panes_on_medium_leaves_other_classes_alone() {
147        let directive = PaneScaffoldDirective::with_two_panes_on_medium_width(size(
148            WindowWidthSizeClass::Expanded,
149            WindowHeightSizeClass::Expanded,
150        ));
151        assert_eq!(
152            directive,
153            PaneScaffoldDirective::from_window_size_class(size(
154                WindowWidthSizeClass::Expanded,
155                WindowHeightSizeClass::Expanded,
156            ))
157        );
158    }
159}