floem/views/
drag_window_area.rs

1use ui_events::pointer::{PointerButton, PointerButtonEvent, PointerEvent};
2
3use crate::{
4    action::{drag_window, toggle_window_maximized},
5    event::{Event, EventListener},
6    id::ViewId,
7    view::{IntoView, View},
8};
9
10use super::Decorators;
11
12/// A view that will move the window when the mouse is dragged. See [`drag_window_area`].
13pub struct DragWindowArea {
14    id: ViewId,
15}
16
17/// A view that will move the window when the mouse is dragged.
18///
19/// This can be used to allow dragging the window when the title bar is disabled.
20pub fn drag_window_area<V: IntoView + 'static>(child: V) -> DragWindowArea {
21    let id = ViewId::new();
22    id.set_children([child]);
23    DragWindowArea { id }
24        .on_event_stop(EventListener::PointerDown, |e| {
25            if let Event::Pointer(PointerEvent::Down(PointerButtonEvent { button, .. })) = e {
26                if button.is_some_and(|b| b == PointerButton::Primary) {
27                    drag_window();
28                }
29            }
30        })
31        .on_double_click_stop(|_| toggle_window_maximized())
32}
33impl View for DragWindowArea {
34    fn id(&self) -> ViewId {
35        self.id
36    }
37
38    fn debug_name(&self) -> std::borrow::Cow<'static, str> {
39        "Drag Window Area".into()
40    }
41}