Skip to main content

xrack_runtime/
drag.rs

1use crate::{KeyModifiers, Length};
2use std::sync::Arc;
3
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum DragAction {
6    #[default]
7    None,
8    Copy,
9    Move,
10    Link,
11}
12
13impl KeyModifiers {
14    #[doc(hidden)]
15    pub fn drag_action(self) -> Option<DragAction> {
16        match (self.control, self.shift) {
17            (false, false) => None,
18            (true, false) => Some(DragAction::Copy),
19            (false, true) => Some(DragAction::Move),
20            (true, true) => Some(DragAction::Link),
21        }
22    }
23}
24
25#[derive(Clone, Debug, PartialEq)]
26pub enum DragEvent {
27    Started,
28    Finished(DragAction),
29    Entered { data: String, action: DragAction },
30    FilesEntered(Arc<[String]>),
31    Moved { x: Length, y: Length },
32    Exited,
33    Dropped { data: String, action: DragAction },
34    FilesDropped(Arc<[String]>),
35    FilesFailed(String),
36}
37
38/// Borrowed arguments for a DSL drag handler.
39#[doc(hidden)]
40pub enum DragArguments<'a> {
41    None,
42    Action(DragAction),
43    Data(&'a str, DragAction),
44    Files(&'a [String]),
45    Message(&'a str),
46    Position(Length, Length),
47}
48
49impl DragEvent {
50    #[doc(hidden)]
51    pub fn notification(&self) -> (&'static str, DragArguments<'_>) {
52        use DragArguments::*;
53        match self {
54            Self::Started => ("drag-started", None),
55            Self::Finished(action) => ("drag-finished", Action(*action)),
56            Self::Entered { data, action } => ("entered", Data(data, *action)),
57            Self::Dropped { data, action } => ("dropped", Data(data, *action)),
58            Self::FilesEntered(paths) => ("files-entered", Files(paths)),
59            Self::FilesDropped(paths) => ("files-dropped", Files(paths)),
60            Self::FilesFailed(message) => ("files-failed", Message(message)),
61            Self::Moved { x, y } => ("position-changed", Position(*x, *y)),
62            Self::Exited => ("exited", None),
63        }
64    }
65
66    #[doc(hidden)]
67    pub fn target_kind(&self) -> &'static str {
68        match self {
69            Self::Started | Self::Finished(_) => "DragArea",
70            _ => "DropArea",
71        }
72    }
73
74    #[doc(hidden)]
75    pub fn is_terminal(&self) -> bool {
76        matches!(self, Self::Exited | Self::Finished(_))
77    }
78}
79
80pub fn is_drag_area(kind: &str) -> bool {
81    matches!(kind, "DragArea" | "DropArea")
82}