1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! [`ScaffoldInfo`]: **what the shell knows and its slots do not.**
//!
//! An [`AppBar`](crate::AppBar) is handed to a [`Scaffold`](crate::Scaffold) already
//! built, so it cannot see the shell it is about to stand in. The reference's does, through
//! the context: `Scaffold.of(context)` tells a bar whether the screen has a drawer, which
//! is how a bar with no `leading` grows a menu button on a screen that has a menu and
//! stays empty on one that does not (`app_bar.dart:1010`).
//!
//! This is that context, in the shape frus already uses twice: an ambient value the walk
//! installs on the way down, like [`MediaQuery`](crate::MediaQuery) and the theme, and a
//! [`Widget`] hook — [`Widget::scaffold_override`] — for the node that imposes one.
//!
//! It carries **messages**, which is the part that needed care: the shell knows what opens
//! its drawer and the bar does not, and the two are generic over different type parameters
//! as far as the ambient is concerned. The message is held as `Rc<dyn Any>` and handed back
//! only to a caller that names the type it was put in with. A bar whose `Msg` is not the
//! shell's asks and is told nothing, which is the honest answer: it could not have sent
//! that message anyway.
//!
//! ```ignore
//! ScaffoldScope::new(ScaffoldInfo::default().with_drawer(Msg::ToggleMenu), shell)
//! ```
use std::any::Any;
use std::cell::RefCell;
use std::hash::{Hash, Hasher};
use std::rc::Rc;
use crate::widget::Widget;
/// What the [`Scaffold`](crate::Scaffold) around a widget knows about itself.
///
/// Empty outside a shell — which is not the same as "a shell with nothing in it", but the
/// two want the same answers here, and a bar outside a shell has no drawer to open either
/// way.
#[derive(Clone, Default)]
pub struct ScaffoldInfo {
/// The message that opens or closes the leading drawer, if the shell has one.
drawer: Option<Rc<dyn Any>>,
/// The same for the trailing drawer.
end_drawer: Option<Rc<dyn Any>>,
}
impl ScaffoldInfo {
/// The one in force here — empty outside every [`ScaffoldInfo::scope`].
#[must_use]
pub fn of() -> Self {
AMBIENT.with(|a| a.borrow().clone())
}
/// Records a leading drawer and the message that toggles it.
#[must_use]
pub fn with_drawer<Msg: Clone + 'static>(mut self, toggle: Msg) -> Self {
self.drawer = Some(Rc::new(toggle));
self
}
/// Records a trailing drawer and the message that toggles it.
#[must_use]
pub fn with_end_drawer<Msg: Clone + 'static>(mut self, toggle: Msg) -> Self {
self.end_drawer = Some(Rc::new(toggle));
self
}
/// Whether the shell has a drawer on the leading edge.
#[must_use]
pub fn has_drawer(&self) -> bool {
self.drawer.is_some()
}
/// Whether the shell has a drawer on the trailing edge.
#[must_use]
pub fn has_end_drawer(&self) -> bool {
self.end_drawer.is_some()
}
/// The message that toggles the leading drawer, for a widget whose message type it
/// **is**. `None` for anyone else: a widget cannot send a message of another tree's
/// type, so being told about it would only invite a cast.
#[must_use]
pub fn drawer_toggle<Msg: Clone + 'static>(&self) -> Option<Msg> {
self.drawer.as_ref()?.downcast_ref::<Msg>().cloned()
}
/// The same for the trailing drawer.
#[must_use]
pub fn end_drawer_toggle<Msg: Clone + 'static>(&self) -> Option<Msg> {
self.end_drawer.as_ref()?.downcast_ref::<Msg>().cloned()
}
/// Puts this in force until the guard is dropped.
#[must_use]
pub fn install(self) -> ScaffoldGuard {
ScaffoldGuard(AMBIENT.with(|a| a.replace(self)))
}
/// Runs `body` with this in force, and puts back what was there.
pub fn scope<R>(self, body: impl FnOnce() -> R) -> R {
let _guard = self.install();
body()
}
/// The part of this that changes what a slot **composes**, for the relayout
/// fingerprint.
///
/// Whether there is a drawer decides whether a bar grows a button, which is a
/// different row and a different geometry. *Which* message opens it does not: two
/// shells with different messages and the same drawers lay out identically, and
/// hashing the message would mean hashing a pointer that moves every frame.
pub(crate) fn shape_hash<H: Hasher>(&self, hasher: &mut H) {
self.has_drawer().hash(hasher);
self.has_end_drawer().hash(hasher);
}
}
/// Puts back the previous ambient shell when dropped — including while a panic unwinds.
pub struct ScaffoldGuard(ScaffoldInfo);
impl Drop for ScaffoldGuard {
fn drop(&mut self) {
AMBIENT.with(|a| a.replace(std::mem::take(&mut self.0)));
}
}
thread_local! {
/// The shell in force on this thread. A `RefCell` rather than a `Cell` because the
/// value is not `Copy`: it holds the messages, and a message is whatever the
/// application says it is.
static AMBIENT: RefCell<ScaffoldInfo> = const { RefCell::new(ScaffoldInfo {
drawer: None,
end_drawer: None,
}) };
}
/// A **shell for one subtree**: the counterpart of [`MediaScope`](crate::MediaScope), one
/// milestone later and for the other thing a shell knows.
///
/// A **transparent wrapper**: it lays out, paints and answers exactly
/// as its child does. The only thing it adds is what the subtree below it reads from
/// [`ScaffoldInfo::of`] — including a subtree deferred until
/// [`Widget::build_themed`](crate::Widget::build_themed), which is where an
/// [`AppBar`](crate::AppBar) is composed and therefore the whole point.
pub struct ScaffoldScope<Msg = crate::callback::Callback> {
info: ScaffoldInfo,
inner: Box<dyn Widget<Msg>>,
}
impl<Msg> ScaffoldScope<Msg> {
/// Tells `child`'s subtree what shell it is in.
pub fn new(info: ScaffoldInfo, child: impl Widget<Msg> + 'static) -> Self {
Self {
info,
inner: Box::new(child),
}
}
/// Transparent: it does not change the box its child is.
fn restyle(&self, base: frus_layout::Style) -> frus_layout::Style {
base
}
}
crate::transparent::forward_transparent!(ScaffoldScope {
fn key(&self) -> Option<u64> {
self.inner.key()
}
fn positioned(&self) -> Option<crate::positioned::Positioning> {
self.inner.positioned()
}
fn theme_override(&self, inherited: &crate::theme::Theme) -> Option<Box<crate::theme::Theme>> {
self.inner.theme_override(inherited)
}
fn media_override(&self, inherited: crate::MediaQuery) -> Option<crate::MediaQuery> {
self.inner.media_override(inherited)
}
/// The shell this scope names — unless the child is itself a scope, in which case the
/// **inner** one wins, exactly as a nested `Scaffold` wins in the reference.
fn scaffold_override(&self) -> Option<ScaffoldInfo> {
Some(
self.inner
.scaffold_override()
.unwrap_or_else(|| self.info.clone()),
)
}
fn scroll_behavior_override(&self) -> Option<crate::ScrollBehavior> {
self.inner.scroll_behavior_override()
}
/// Forwarded: a scope around a form is still that form (milestone 512).
fn autofill_group(&self) -> bool {
self.inner.autofill_group()
}
/// Forwarded: the area's bar is the area's, whatever wraps it.
fn area_toolbar(
&self,
context: crate::ToolbarContext,
) -> Option<Option<&dyn crate::widget::Widget<Msg>>> {
self.inner.area_toolbar(context)
}
});
#[cfg(test)]
mod tests {
use super::*;
use crate::{build_ui, Container, Runtime, Size, Theme};
#[derive(Clone, Debug, PartialEq)]
enum Msg {
Menu,
}
/// Another tree's message type, which the shell's message is not.
#[derive(Clone, Debug, PartialEq)]
enum Other {
#[allow(dead_code)]
Menu,
}
/// Outside a shell there is no shell, and the answer says so rather than inventing one.
#[test]
fn there_is_no_shell_until_one_says_so() {
let bare = ScaffoldInfo::of();
assert!(!bare.has_drawer() && !bare.has_end_drawer());
assert_eq!(bare.drawer_toggle::<Msg>(), None);
}
/// The message comes back to the tree it was put in for, and to nobody else.
#[test]
fn a_message_is_handed_back_only_to_the_type_it_was_put_in_with() {
ScaffoldInfo::default().with_drawer(Msg::Menu).scope(|| {
let info = ScaffoldInfo::of();
assert!(info.has_drawer());
assert_eq!(info.drawer_toggle::<Msg>(), Some(Msg::Menu));
// A tree of another message type is told nothing, because it could not
// have sent this message anyway.
assert_eq!(info.drawer_toggle::<Other>(), None);
});
// And the scope is closed behind it.
assert!(!ScaffoldInfo::of().has_drawer());
}
/// The scope reaches the subtree through the walk, not only the call that installs it —
/// which is what an already-built app bar needs.
#[test]
fn a_scope_reaches_the_subtree_the_walk_visits() {
let seen = std::rc::Rc::new(std::cell::Cell::new(false));
let probe = {
let seen = seen.clone();
crate::ThemeBuilder::<Msg>::boxed(move |_| {
seen.set(ScaffoldInfo::of().has_drawer());
Box::new(Container::new().color(frus_core::Color::WHITE))
})
};
let tree = ScaffoldScope::new(ScaffoldInfo::default().with_drawer(Msg::Menu), probe);
build_ui(
&tree,
Size::new(100.0, 100.0),
&Runtime::default(),
&Theme::default(),
);
assert!(seen.get(), "the deferred build did not see the shell");
}
/// Two shells that differ in what they hold must not share a layout: a drawer is a
/// button in the bar, and a button is a different row. The cache keys on a fingerprint
/// of the walk, so the shell has to be in it — the same trap `theme_override` and
/// `media_override` each document beside themselves.
#[test]
fn the_shell_is_part_of_the_relayout_fingerprint() {
// One runtime across both builds, so the second really does consult the first's
// cache rather than starting from nothing.
let runtime = Runtime::default();
let height = |drawer: bool| {
let probe = crate::ThemeBuilder::<Msg>::boxed(move |_| {
// A shell with a drawer composes a taller box, the way a bar with a menu
// button composes a different row.
let tall = if ScaffoldInfo::of().has_drawer() {
40.0
} else {
10.0
};
Box::new(
Container::new()
.width(10.0)
.height(tall)
.color(frus_core::Color::rgb8(255, 0, 128)),
)
});
let info = if drawer {
ScaffoldInfo::default().with_drawer(Msg::Menu)
} else {
ScaffoldInfo::default()
};
let tree = ScaffoldScope::new(info, probe);
build_ui(&tree, Size::new(100.0, 100.0), &runtime, &Theme::default())
.scene()
.primitives()
.iter()
.find_map(|p| match p {
frus_core::Primitive::Rect { rect, .. } => Some(rect.height),
_ => None,
})
};
let with = height(true);
let without = height(false);
assert_ne!(
with, without,
"a shell with a drawer was served from the cache of one without"
);
assert_eq!(height(true), with, "and back again");
}
}