freya-components 0.4.0-alpha.4

Components for Freya apps
Documentation
use freya_core::prelude::*;
use torin::size::Size;
enum ScrollThumbState {
    Idle,
    Hovering,
}

use crate::{
    get_theme,
    scrollviews::shared::Axis,
    theming::component_themes::ScrollBarThemePartial,
};

#[derive(Clone, PartialEq)]
pub struct ScrollThumb {
    pub(crate) theme: Option<ScrollBarThemePartial>,
    pub clicking_scrollbar: State<Option<(Axis, f64)>>,
    pub axis: Axis,
    pub size: f32,
}

impl RenderOwned for ScrollThumb {
    fn render(mut self) -> impl IntoElement {
        let scrollbar_theme = get_theme!(&self.theme, scrollbar);
        let mut state = use_state(|| ScrollThumbState::Idle);

        let (width, height) = match self.axis {
            Axis::X => (Size::px(self.size), Size::fill()),
            Axis::Y => (Size::fill(), Size::px(self.size)),
        };
        let thumb_background = match *state.read() {
            _ if self.clicking_scrollbar.read().is_some() => {
                scrollbar_theme.active_thumb_background
            }
            ScrollThumbState::Idle => scrollbar_theme.thumb_background,
            ScrollThumbState::Hovering => scrollbar_theme.hover_thumb_background,
        };

        let on_pointer_enter = move |_| state.set(ScrollThumbState::Hovering);
        let on_pointer_leave = move |_| state.set(ScrollThumbState::Idle);

        rect()
            .width(width)
            .height(height)
            .padding(4.)
            .on_pointer_enter(on_pointer_enter)
            .on_pointer_leave(on_pointer_leave)
            .on_pointer_down(move |e: Event<PointerEventData>| {
                if self.axis == Axis::X {
                    self.clicking_scrollbar
                        .set(Some((self.axis, e.element_location().x)));
                } else {
                    self.clicking_scrollbar
                        .set(Some((self.axis, e.element_location().y)));
                }
            })
            .child(
                rect()
                    .width(Size::fill())
                    .height(Size::fill())
                    .background(thumb_background)
                    .corner_radius(8.),
            )
    }
}