Skip to main content

waft_winui/
scroll_behavior.rs

1//! WinUI's scrolling: a thin overlay scroll bar in the theme's scrollbar colours, and no
2//! overscroll glow or stretch — a `ScrollViewer` shows no indicator at the edge.
3
4use waft_embedder::Radius;
5use waft_foundation::App;
6use waft_widgets::{
7    BuildContext, IntoWidget, RawScrollbar, ScrollBehavior, ScrollableDetails, WidgetRef,
8};
9
10use crate::theme::ThemeResources;
11
12/// How a Fluent app's scrollables behave: WinUI's scroll bar and edge.
13///
14/// The scrollbar is [`RawScrollbar`] at the kit's two-point thickness in the theme's
15/// `ScrollBarForeground`, shown while a scroll is under way and hidden after it, as WinUI's
16/// auto-hiding bar is. No glow or stretch marks the edge, as none does a `ScrollViewer`'s. The
17/// physics are the platform's, as [`ScrollBehavior`]'s default gives them: bouncing on iOS and
18/// macOS, clamping elsewhere.
19#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
20pub struct FluentScrollBehavior;
21
22impl FluentScrollBehavior {
23    /// WinUI's scrolling for the app.
24    pub const fn new() -> FluentScrollBehavior {
25        FluentScrollBehavior
26    }
27
28    /// The bar's thickness: WinUI's collapsed scrollbar is 2 effective pixels across.
29    const THICKNESS: f64 = 2.0;
30}
31
32impl ScrollBehavior for FluentScrollBehavior {
33    fn as_any(&self) -> &dyn std::any::Any {
34        self
35    }
36
37    fn build_scrollbar(
38        &self,
39        app: &mut App,
40        context: BuildContext,
41        child: WidgetRef,
42        details: &ScrollableDetails,
43    ) -> WidgetRef {
44        let resources = ThemeResources::of(app, context);
45        let Some(controller) = details.controller.clone() else {
46            return child;
47        };
48        RawScrollbar::new(child)
49            .controller(controller)
50            .thickness(Self::THICKNESS)
51            .radius(Radius::circular(Self::THICKNESS / 2.0))
52            .thumb_color(resources.scroll_bar_foreground)
53            .into_widget()
54    }
55
56    fn build_overscroll_indicator(
57        &self,
58        _app: &mut App,
59        _context: BuildContext,
60        child: WidgetRef,
61        _details: &ScrollableDetails,
62    ) -> WidgetRef {
63        child
64    }
65}