use dioxus::prelude::*;
use freya_elements::{
elements as dioxus_elements,
events::{
keyboard::Key,
KeyboardEvent,
MouseEvent,
},
};
use freya_hooks::{
use_applied_theme,
use_platform,
TileTheme,
TileThemeWith,
};
use winit::window::CursorIcon;
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub enum TileStatus {
#[default]
Idle,
Hovering,
}
#[allow(non_snake_case)]
#[component]
pub fn Tile(
children: Element,
leading: Option<Element>,
onselect: Option<EventHandler<()>>,
theme: Option<TileThemeWith>,
) -> Element {
let mut status = use_signal(TileStatus::default);
let platform = use_platform();
let TileTheme { padding } = use_applied_theme!(&theme, tile);
use_drop(move || {
if *status.read() == TileStatus::Hovering {
platform.set_cursor(CursorIcon::default());
}
});
let onkeydown = move |e: KeyboardEvent| {
if e.key == Key::Enter {
if let Some(onselect) = &onselect {
e.stop_propagation();
onselect.call(())
}
}
};
let onclick = move |e: MouseEvent| {
if let Some(onselect) = &onselect {
e.stop_propagation();
onselect.call(())
}
};
let onmouseenter = move |_| {
platform.set_cursor(CursorIcon::Pointer);
status.set(TileStatus::Hovering);
};
let onmouseleave = move |_| {
platform.set_cursor(CursorIcon::default());
status.set(TileStatus::default());
};
rsx!(
rect {
onkeydown,
onclick,
onmouseenter,
onmouseleave,
direction: "horizontal",
padding: "{padding}",
cross_align: "center",
if let Some(leading) = leading {
rect {
padding: "4",
{leading}
}
}
{children}
}
)
}