waft-winui 0.18.2

WinUI 3 controls and Fluent styling for the Waft UI framework
Documentation
//! WinUI's scrolling: a thin overlay scroll bar in the theme's scrollbar colours, and no
//! overscroll glow or stretch — a `ScrollViewer` shows no indicator at the edge.

use waft_embedder::Radius;
use waft_foundation::App;
use waft_widgets::{
    BuildContext, IntoWidget, RawScrollbar, ScrollBehavior, ScrollableDetails, WidgetRef,
};

use crate::theme::ThemeResources;

/// How a Fluent app's scrollables behave: WinUI's scroll bar and edge.
///
/// The scrollbar is [`RawScrollbar`] at the kit's two-point thickness in the theme's
/// `ScrollBarForeground`, shown while a scroll is under way and hidden after it, as WinUI's
/// auto-hiding bar is. No glow or stretch marks the edge, as none does a `ScrollViewer`'s. The
/// physics are the platform's, as [`ScrollBehavior`]'s default gives them: bouncing on iOS and
/// macOS, clamping elsewhere.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FluentScrollBehavior;

impl FluentScrollBehavior {
    /// WinUI's scrolling for the app.
    pub const fn new() -> FluentScrollBehavior {
        FluentScrollBehavior
    }

    /// The bar's thickness: WinUI's collapsed scrollbar is 2 effective pixels across.
    const THICKNESS: f64 = 2.0;
}

impl ScrollBehavior for FluentScrollBehavior {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn build_scrollbar(
        &self,
        app: &mut App,
        context: BuildContext,
        child: WidgetRef,
        details: &ScrollableDetails,
    ) -> WidgetRef {
        let resources = ThemeResources::of(app, context);
        let Some(controller) = details.controller.clone() else {
            return child;
        };
        RawScrollbar::new(child)
            .controller(controller)
            .thickness(Self::THICKNESS)
            .radius(Radius::circular(Self::THICKNESS / 2.0))
            .thumb_color(resources.scroll_bar_foreground)
            .into_widget()
    }

    fn build_overscroll_indicator(
        &self,
        _app: &mut App,
        _context: BuildContext,
        child: WidgetRef,
        _details: &ScrollableDetails,
    ) -> WidgetRef {
        child
    }
}