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
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_hooks::{
    use_activable_route,
    use_applied_theme,
    use_platform,
    SidebarItemTheme,
    SidebarItemThemeWith,
    SidebarTheme,
    SidebarThemeWith,
};
use winit::window::CursorIcon;

use crate::{
    ButtonStatus,
    ScrollView,
};

#[allow(non_snake_case)]
#[component]
pub fn Sidebar(
    /// Theme override.
    theme: Option<SidebarThemeWith>,
    /// This is what is rendered next to the sidebar.
    children: Element,
    /// This is what is rendered in the sidebar.
    sidebar: Element,
) -> Element {
    let SidebarTheme {
        spacing,
        font_theme,
        background,
    } = use_applied_theme!(&theme, sidebar);

    rsx!(
        rect {
            width: "100%",
            height: "100%",
            direction: "horizontal",
            rect {
                overflow: "clip",
                width: "180",
                height: "100%",
                background: "{background}",
                color: "{font_theme.color}",
                shadow: "2 0 5 0 rgb(0, 0, 0, 30)",
                ScrollView {
                    padding: "8",
                    spacing,
                    {sidebar}
                }
            }
            rect {
                overflow: "clip",
                width: "fill",
                height: "100%",
                color: "{font_theme.color}",
                {children}
            }
        }
    )
}

#[allow(non_snake_case)]
#[component]
pub fn SidebarItem(
    /// Theme override.
    theme: Option<SidebarItemThemeWith>,
    /// Inner content for the SidebarItem.
    children: Element,
    /// Optionally handle the `onclick` event in the SidebarItem.
    onclick: Option<EventHandler<()>>,
) -> Element {
    let SidebarItemTheme {
        margin,
        hover_background,
        background,
        font_theme,
    } = use_applied_theme!(&theme, sidebar_item);
    let mut status = use_signal(ButtonStatus::default);
    let platform = use_platform();
    let is_active = use_activable_route();

    use_drop(move || {
        if *status.read() == ButtonStatus::Hovering {
            platform.set_cursor(CursorIcon::default());
        }
    });

    let onclick = move |_| {
        if let Some(onclick) = &onclick {
            onclick.call(());
        }
    };

    let onmouseenter = move |_| {
        platform.set_cursor(CursorIcon::Pointer);
        status.set(ButtonStatus::Hovering);
    };

    let onmouseleave = move |_| {
        platform.set_cursor(CursorIcon::default());
        status.set(ButtonStatus::default());
    };

    let background = match *status.read() {
        _ if is_active => hover_background,
        ButtonStatus::Hovering => hover_background,
        ButtonStatus::Idle => background,
    };

    rsx!(
        rect {
            overflow: "clip",
            margin: "{margin}",
            onclick,
            onmouseenter,
            onmouseleave,
            width: "100%",
            height: "auto",
            color: "{font_theme.color}",
            corner_radius: "99",
            padding: "8 10",
            background: "{background}",
            {children}
        }
    )
}