1use crate::*;
3
4#[derive(Clone, Copy, Debug)]
6pub struct ScaffoldOptions {
7 pub top_bar_height: f32,
8 pub bottom_bar_height: f32,
9 pub rail_width: f32,
10 pub compact_breakpoint: f32,
12}
13
14impl Default for ScaffoldOptions {
15 fn default() -> Self {
16 Self {
17 top_bar_height: 64.0,
18 bottom_bar_height: 80.0,
19 rail_width: 80.0,
20 compact_breakpoint: 600.0,
21 }
22 }
23}
24
25#[derive(Clone, Copy, Debug, PartialEq)]
27pub struct ScaffoldSlots {
28 pub compact: bool,
29 pub top_bar: Rect,
30 pub nav_rail: Option<Rect>,
31 pub bottom_bar: Option<Rect>,
32 pub body: Rect,
33 pub fab: Rect,
34}
35
36impl Frame<'_> {
37 pub fn scaffold_slots(&self, bounds: Rect, options: ScaffoldOptions) -> ScaffoldSlots {
39 let compact = bounds.width < options.compact_breakpoint;
40 let top_h = options.top_bar_height.max(0.0).min(bounds.height);
41 let rail_w = if compact { 0.0 } else { options.rail_width.max(0.0).min(bounds.width) };
42 let bottom_h = if compact {
43 options.bottom_bar_height.max(0.0).min((bounds.height - top_h).max(0.0))
44 } else {
45 0.0
46 };
47 let top_bar = Rect { x: bounds.x + rail_w, y: bounds.y, width: (bounds.width - rail_w).max(0.0), height: top_h };
48 let nav_rail = (!compact && rail_w > 0.0).then_some(Rect { x: bounds.x, y: bounds.y, width: rail_w, height: bounds.height });
49 let bottom_bar = (compact && bottom_h > 0.0).then_some(Rect {
50 x: bounds.x,
51 y: bounds.y + bounds.height - bottom_h,
52 width: bounds.width,
53 height: bottom_h,
54 });
55 let body = Rect {
56 x: bounds.x + rail_w,
57 y: bounds.y + top_h,
58 width: (bounds.width - rail_w).max(0.0),
59 height: (bounds.height - top_h - bottom_h).max(0.0),
60 };
61 let fab = Rect {
62 x: (body.x + body.width - 72.0).max(body.x),
63 y: (body.y + body.height - 72.0).max(body.y),
64 width: 56.0_f32.min(body.width),
65 height: 56.0_f32.min(body.height),
66 };
67 ScaffoldSlots { compact, top_bar, nav_rail, bottom_bar, body, fab }
68 }
69
70 pub fn scaffold(
73 &mut self,
74 bounds: Rect,
75 options: ScaffoldOptions,
76 top_bar: impl FnOnce(&mut Self, Rect),
77 mut navigation: impl FnMut(&mut Self, Rect, bool),
78 body: impl FnOnce(&mut Self, Rect),
79 fab: impl FnOnce(&mut Self, Rect),
80 ) -> ScaffoldSlots {
81 let slots = self.scaffold_slots(bounds, options);
82 if slots.top_bar.height > 0.0 {
83 self.within(slots.top_bar, |f| top_bar(f, slots.top_bar));
84 }
85 if let Some(rail) = slots.nav_rail {
86 self.within(rail, |f| navigation(f, rail, false));
87 }
88 self.within(slots.body, |f| body(f, slots.body));
89 if let Some(bar) = slots.bottom_bar {
90 self.within(bar, |f| navigation(f, bar, true));
91 }
92 fab(self, slots.fab);
93 slots
94 }
95
96 #[allow(clippy::too_many_arguments)]
99 pub fn scaffold_adaptive(
100 &mut self,
101 id: u64,
102 bounds: Rect,
103 options: ScaffoldAdaptiveOptions,
104 rail_expanded: bool,
105 drawer_open: &mut bool,
106 top_bar: impl FnOnce(&mut Self, Rect),
107 mut navigation: impl FnMut(&mut Self, Rect, f32, bool),
108 body: impl FnOnce(&mut Self, Rect),
109 fab: impl FnOnce(&mut Self, Rect),
110 ) -> ScaffoldSlots {
111 let compact = bounds.width < options.compact_breakpoint;
112 let collapsed_w = options.collapsed_rail_width.max(56.0);
113 let expanded_w = options.expanded_rail_width.max(collapsed_w);
114 let target_exp = f32::from(!compact && rail_expanded);
115 let expansion = self
116 .transition(
117 id,
118 70,
119 target_exp,
120 target_exp,
121 0.3,
122 crate::timing::Easing::Standard,
123 )
124 .clamp(0.0, 1.0);
125 let rail_w = if compact {
126 0.0
127 } else {
128 collapsed_w + (expanded_w - collapsed_w) * expansion
129 };
130 let drawer_prog = self
131 .transition(
132 id,
133 71,
134 f32::from(compact && *drawer_open),
135 0.0,
136 0.3,
137 crate::timing::Easing::Standard,
138 )
139 .clamp(0.0, 1.0);
140 let slots = self.scaffold_slots(
141 bounds,
142 ScaffoldOptions {
143 top_bar_height: options.top_bar_height,
144 bottom_bar_height: options.bottom_bar_height,
145 rail_width: rail_w,
146 compact_breakpoint: options.compact_breakpoint,
147 },
148 );
149 let blocked = compact && *drawer_open;
150 self.without_input(blocked, |f| {
151 if slots.top_bar.height > 0.0 {
152 f.within(slots.top_bar, |f| top_bar(f, slots.top_bar));
153 }
154 if let Some(rail) = slots.nav_rail {
155 f.within(rail, |f| navigation(f, rail, expansion, false));
156 }
157 f.within(slots.body, |f| body(f, slots.body));
158 if let Some(bar) = slots.bottom_bar {
159 f.within(bar, |f| navigation(f, bar, 0.0, true));
160 }
161 fab(f, slots.fab);
162 });
163 if compact && drawer_prog > 0.005 {
164 let scrim_r = self.target(id.wrapping_add(2), bounds, "Close navigation drawer", *drawer_open, Role::Button);
165 self.paint_rect(bounds, 0.0, Color(0, 0, 0, (0.38 * drawer_prog * 255.0) as u8));
166 let dw = options.drawer_width.min(bounds.width);
167 let drawer_rect = Rect {
168 x: bounds.x - dw * (1.0 - drawer_prog),
169 y: bounds.y,
170 width: dw,
171 height: bounds.height,
172 };
173 self.elevation(drawer_rect, 16.0, 6);
174 self.paint_rect(drawer_rect, 16.0, self.theme().surface_low);
175 self.within(drawer_rect, |f| navigation(f, drawer_rect, 1.0, false));
176 if (scrim_r.clicked && !self.pointer_in(drawer_rect)) || self.pressed_key(Key::Escape) {
177 *drawer_open = false;
178 }
179 }
180 slots
181 }
182}
183
184#[derive(Clone, Copy, Debug)]
186pub struct ScaffoldAdaptiveOptions {
187 pub top_bar_height: f32,
188 pub bottom_bar_height: f32,
189 pub collapsed_rail_width: f32,
190 pub expanded_rail_width: f32,
191 pub drawer_width: f32,
192 pub compact_breakpoint: f32,
193}
194
195impl Default for ScaffoldAdaptiveOptions {
196 fn default() -> Self {
197 Self {
198 top_bar_height: 64.0,
199 bottom_bar_height: 80.0,
200 collapsed_rail_width: 88.0,
201 expanded_rail_width: 256.0,
202 drawer_width: 280.0,
203 compact_breakpoint: 600.0,
204 }
205 }
206}
207
208#[cfg(test)]
209#[path = "scaffold.spec.rs"]
210mod tests;