Skip to main content

freya_components/
window_drag_area.rs

1use dioxus::prelude::*;
2use freya_core::platform::MouseButton;
3use freya_elements::{
4    self as dioxus_elements,
5    events::MouseEvent,
6};
7use freya_hooks::use_platform;
8
9/// Allow dragging the window when the cursor drag this component with a left mouse click.
10///
11/// # Example
12///
13/// ```no_run
14/// # use freya::prelude::*;
15/// fn app() -> Element {
16///     rsx!(
17///         WindowDragArea {
18///             label {
19///                 height: "100%",
20///                 width: "100%",
21///                 "Drag Me"
22///             }
23///         }
24///     )
25/// }
26/// ```
27#[allow(non_snake_case)]
28#[component]
29pub fn WindowDragArea(
30    /// The inner children for the WindowDragArea
31    children: Element,
32) -> Element {
33    let platform = use_platform();
34
35    let onmousedown = move |e: MouseEvent| {
36        if let Some(MouseButton::Left) = e.trigger_button {
37            e.stop_propagation();
38            platform.drag_window();
39        }
40    };
41
42    rsx!(
43        rect {
44            onmousedown,
45            {children}
46        }
47    )
48}