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
312
313
//! Flutter counterpart: `widgets/primary_scroll_controller.dart`.
use std::collections::HashSet;
use waft_embedder::TargetPlatform;
use waft_foundation::App;
use waft_painting::Axis;
use crate::framework::{BuildContext, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
use crate::widgets::scroll_configuration::ScrollConfiguration;
use crate::widgets::scroll_controller::ScrollControllerRef;
/// Dart's `_kMobilePlatforms`, a function because a `HashSet` has no `const` constructor.
fn k_mobile_platforms() -> HashSet<TargetPlatform> {
HashSet::from([
TargetPlatform::Android,
TargetPlatform::IOS,
TargetPlatform::Fuchsia,
])
}
/// Associates a `ScrollController` with a subtree.
///
/// When a `ScrollView` has `primary` set to true, the `ScrollView`
/// uses [`of`](Self::of) to inherit the [`PrimaryScrollController`] associated with its
/// subtree.
///
/// A scroll view that doesn't have a controller or the primary flag set will
/// inherit the primary scroll controller, if [`should_inherit`](Self::should_inherit) allows
/// it. By default [`should_inherit`](Self::should_inherit) is true for mobile platforms when
/// the scroll view has a scroll direction of [`Axis::Vertical`]. This automatic inheritance
/// can be configured with
/// [`automatically_inherit_for_platforms`](Self::automatically_inherit_for_platforms) and
/// [`scroll_direction`](Self::scroll_direction).
///
/// Inheriting this scroll controller can provide default behavior for scroll
/// views in a subtree. For example, the `Scaffold` uses this mechanism to
/// implement the scroll-to-top gesture on iOS.
///
/// Another default behavior handled by the [`PrimaryScrollController`] is default
/// [`crate::ScrollAction`]s. If a scroll action is not handled by an otherwise focused
/// part of the application, the scroll action will be evaluated using the scroll
/// view associated with a [`PrimaryScrollController`], for example, when executing
/// `Shortcuts` key events like page up and down.
///
/// See also:
/// * [`crate::ScrollAction`], an [`crate::Action`] that scrolls the `Scrollable` that
/// encloses the current primary focus or is attached to the primary scroll controller.
/// * `Shortcuts`, a widget that establishes a `ShortcutManager` to be used
/// by its descendants when invoking an [`crate::Action`] via a keyboard key
/// combination that maps to an [`crate::Intent`].
#[derive(Debug)]
pub struct PrimaryScrollController {
pub key: Option<KeyRef>,
/// The `ScrollController` associated with the subtree.
///
/// See also:
///
/// * `ScrollView.controller`, which discusses the purpose of specifying a
/// scroll controller.
pub controller: Option<ScrollControllerRef>,
/// The [`Axis`] this controller is configured for scroll views to
/// automatically inherit.
///
/// Used in conjunction with
/// [`automatically_inherit_for_platforms`](Self::automatically_inherit_for_platforms). If
/// the current [`TargetPlatform`] is not included there, this is ignored.
///
/// When `None`, no scroll view in any axis will automatically inherit this
/// controller. This is dissimilar to [`PrimaryScrollController::none`]. When a
/// primary scroll controller is inherited, `ScrollView` will insert
/// [`PrimaryScrollController::none`] into the tree to prevent further descendant
/// scroll views from inheriting the current primary scroll controller.
///
/// For the direction in which active scrolling may be occurring, see
/// [`waft_rendering::ScrollDirection`].
///
/// Defaults to [`Axis::Vertical`].
pub scroll_direction: Option<Axis>,
/// The [`TargetPlatform`]s this controller is configured for scroll views to
/// automatically inherit.
///
/// Used in conjunction with [`scroll_direction`](Self::scroll_direction). If the [`Axis`]
/// provided to [`should_inherit`](Self::should_inherit) is not
/// [`scroll_direction`](Self::scroll_direction), this is ignored.
///
/// When empty, no scroll view in any axis will automatically inherit this
/// controller. Defaults to the mobile platforms.
pub automatically_inherit_for_platforms: HashSet<TargetPlatform>,
/// The widget below this widget in the tree.
pub child: WidgetRef,
}
impl PrimaryScrollController {
/// Creates a widget that associates a `ScrollController` with a subtree.
pub fn new<K>(
controller: ScrollControllerRef,
child: impl IntoWidget<K>,
) -> PrimaryScrollController {
PrimaryScrollController {
key: None,
controller: Some(controller),
scroll_direction: Some(Axis::Vertical),
automatically_inherit_for_platforms: k_mobile_platforms(),
child: child.into_widget(),
}
}
/// Creates a subtree without an associated `ScrollController`.
pub fn none<K>(child: impl IntoWidget<K>) -> PrimaryScrollController {
PrimaryScrollController {
key: None,
controller: None,
scroll_direction: None,
automatically_inherit_for_platforms: HashSet::new(),
child: child.into_widget(),
}
}
/// Dart `PrimaryScrollController(key:)`.
pub fn key(mut self, key: KeyRef) -> PrimaryScrollController {
self.key = Some(key);
self
}
/// Dart `PrimaryScrollController(scrollDirection:)`.
pub fn scroll_direction(mut self, scroll_direction: Axis) -> PrimaryScrollController {
self.scroll_direction = Some(scroll_direction);
self
}
/// Dart `PrimaryScrollController(automaticallyInheritForPlatforms:)`.
pub fn automatically_inherit_for_platforms(
mut self,
automatically_inherit_for_platforms: HashSet<TargetPlatform>,
) -> PrimaryScrollController {
self.automatically_inherit_for_platforms = automatically_inherit_for_platforms;
self
}
/// Returns true if this [`PrimaryScrollController`] is configured to be
/// automatically inherited for the current [`TargetPlatform`] and the given
/// [`Axis`].
///
/// This method is typically not called directly. `ScrollView` will call this
/// method if it has not been provided a `ScrollController` and
/// `ScrollView.primary` is unset.
pub fn should_inherit(app: &mut App, context: BuildContext, scroll_direction: Axis) -> bool {
let Some(result) =
context.find_ancestor_widget_of_exact_type::<PrimaryScrollController>(app)
else {
return false;
};
let inherit_for_platforms = result.automatically_inherit_for_platforms.clone();
let inherited_direction = result.scroll_direction;
let platform = ScrollConfiguration::of(app, context).get_platform(app, context);
if inherit_for_platforms.contains(&platform) {
return inherited_direction == Some(scroll_direction);
}
false
}
/// Returns the `ScrollController` most closely associated with the given
/// context.
///
/// Returns `None` if there is no `ScrollController` associated with the given
/// context.
///
/// Calling this method will create a dependency on the closest
/// [`PrimaryScrollController`] in the `context`, if there is one.
///
/// See also:
///
/// * [`PrimaryScrollController::of`], which is similar to this method, but
/// panics if no [`PrimaryScrollController`] ancestor is found.
pub fn maybe_of(app: &mut App, context: BuildContext) -> Option<ScrollControllerRef> {
context
.depend_on_inherited_widget_of_exact_type::<PrimaryScrollController>(app)
.and_then(|result| result.controller.clone())
}
/// Returns the `ScrollController` most closely associated with the given
/// context.
///
/// Calling this method will create a dependency on the closest
/// [`PrimaryScrollController`] in the `context`.
///
/// # Panics
///
/// If no [`PrimaryScrollController`] ancestor is found.
///
/// See also:
///
/// * [`PrimaryScrollController::maybe_of`], which is similar to this method, but
/// returns `None` if no [`PrimaryScrollController`] ancestor is found.
pub fn of(app: &mut App, context: BuildContext) -> ScrollControllerRef {
PrimaryScrollController::maybe_of(app, context).expect(
"PrimaryScrollController.of() was called with a context that does not contain a \
PrimaryScrollController widget.\n\
No PrimaryScrollController widget ancestor could be found starting from the \
context that was passed to PrimaryScrollController.of(). This can happen because \
you are using a widget that looks for a PrimaryScrollController ancestor, but no \
such ancestor exists.",
)
}
}
impl InheritedWidget for PrimaryScrollController {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &PrimaryScrollController) -> bool {
self.controller != old_widget.controller
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::framework::{AnyElement, Element, IntoWidget};
use crate::test_harness::Harness;
use crate::widgets::basic::SizedBox;
use crate::widgets::scroll_controller::ScrollController;
use waft_foundation::AppCell;
/// The element under the test root at `depth` levels down.
fn descendant(harness: &Harness, app: &App, depth: usize) -> AnyElement {
let mut element = harness.root.as_element();
for _ in 0..=depth {
element = element.children(app)[0];
}
element
}
fn mount(app: &mut App, widget: crate::framework::WidgetRef) -> Harness {
let harness = Harness::mount(app, widget);
harness.pump(app);
harness
}
#[test]
fn of_finds_the_controller_the_subtree_is_associated_with() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let controller = ScrollController::default(&mut app);
let harness = mount(
&mut app,
PrimaryScrollController::new(controller.erase(), SizedBox::shrink()).into_widget(),
);
let inner = descendant(&harness, &app, 1);
assert_eq!(
PrimaryScrollController::of(&mut app, inner),
controller.erase()
);
assert_eq!(
PrimaryScrollController::maybe_of(&mut app, inner),
Some(controller.erase())
);
}
#[test]
fn none_hides_the_controller_from_its_subtree() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let controller = ScrollController::default(&mut app);
let harness = mount(
&mut app,
PrimaryScrollController::new(
controller.erase(),
PrimaryScrollController::none(SizedBox::shrink()),
)
.into_widget(),
);
let inner = descendant(&harness, &app, 2);
assert_eq!(PrimaryScrollController::maybe_of(&mut app, inner), None);
}
#[test]
fn should_inherit_follows_the_platform_and_the_axis() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let controller = ScrollController::default(&mut app);
let harness = mount(
&mut app,
PrimaryScrollController::new(controller.erase(), SizedBox::shrink()).into_widget(),
);
let inner = descendant(&harness, &app, 1);
// `App::new` runs on the host's platform; the default set covers the mobile ones.
let mobile = k_mobile_platforms().contains(&app.platform().target_platform());
assert_eq!(
PrimaryScrollController::should_inherit(&mut app, inner, Axis::Vertical),
mobile
);
assert!(!PrimaryScrollController::should_inherit(
&mut app,
inner,
Axis::Horizontal
));
}
}