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
use dioxus::prelude::*;
use freya_elements::{
    self as dioxus_elements,
    events::MouseEvent,
};
use freya_hooks::{
    use_applied_theme,
    ScrollBarThemeWith,
};

/// Properties for the [`ScrollThumb`] component.
#[derive(Props, Clone, PartialEq)]
pub struct ScrollThumbProps {
    /// Theme override.
    pub theme: Option<ScrollBarThemeWith>,
    pub clicking_scrollbar: bool,
    pub onmousedown: EventHandler<MouseEvent>,
    #[props(into)]
    pub width: String,
    #[props(into)]
    pub height: String,
}

enum ScrollThumbState {
    Idle,
    Hovering,
}

/// Scroll thumb used for [`crate::ScrollView`] and [`crate::VirtualScrollView`].
#[allow(non_snake_case)]
pub fn ScrollThumb(
    ScrollThumbProps {
        theme,
        clicking_scrollbar,
        onmousedown,
        width,
        height,
    }: ScrollThumbProps,
) -> Element {
    let theme = use_applied_theme!(&theme, scroll_bar);
    let mut state = use_signal(|| ScrollThumbState::Idle);
    let thumb_background = match *state.read() {
        _ if clicking_scrollbar => theme.active_thumb_background,
        ScrollThumbState::Idle => theme.thumb_background,
        ScrollThumbState::Hovering => theme.hover_thumb_background,
    };

    rsx!(
        rect {
            onmouseenter: move |_: MouseEvent| { state.set(ScrollThumbState::Hovering) },
            onmouseleave: move |_| { state.set(ScrollThumbState::Idle) },
            onmousedown: move |e| {
                e.stop_propagation();
                onmousedown.call(e);
            },
            width: "{width}",
            height: "{height}",
            padding: "2",
            rect {
                width: "100%",
                height: "100%",
                corner_radius: "8",
                background: "{thumb_background}",
            }
        }
    )
}