zng_view_api/
drag_drop.rs1use std::{fmt, path::PathBuf};
4
5use zng_txt::Txt;
6
7use crate::ipc::IpcBytes;
8
9use bitflags::bitflags;
10
11#[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)]
13#[non_exhaustive]
14pub enum DragDropData {
15 Text {
19 format: Txt,
23 data: Txt,
25 },
26 Path(PathBuf),
28 Binary {
32 format: Txt,
34 data: IpcBytes,
36 },
37}
38impl fmt::Debug for DragDropData {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 match self {
41 Self::Text { format, data } => write!(f, "Text {{ format: {:?}, data: {} bytes }}", format, data.len()),
42 Self::Path(data) => write!(f, "Path({})", data.display()),
43 Self::Binary { format, data } => write!(f, "Binary {{ format: {:?}, data: {} bytes }}", format, data.len()),
44 }
45 }
46}
47
48#[cfg(feature = "var")]
49zng_var::impl_from_and_into_var! {
50 fn from(plain: Txt) -> DragDropData {
51 DragDropData::Text {
52 format: "text/plain".into(),
53 data: plain,
54 }
55 }
56
57 fn from(plain: String) -> DragDropData {
58 Txt::from(plain).into()
59 }
60
61 fn from(plain: &'static str) -> DragDropData {
62 Txt::from(plain).into()
63 }
64
65 fn from(path: PathBuf) -> DragDropData {
66 DragDropData::Path(path)
67 }
68}
69
70bitflags! {
71 #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
73 pub struct DragDropEffect: u8 {
74 const COPY = 0b001;
76 const MOVE = 0b010;
78 const LINK = 0b100;
80 }
81}
82impl DragDropEffect {
83 pub fn len(&self) -> u8 {
85 [DragDropEffect::COPY, DragDropEffect::MOVE, DragDropEffect::LINK]
86 .into_iter()
87 .filter(|&f| self.contains(f))
88 .count() as u8
89 }
90}
91
92#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
94#[non_exhaustive]
95pub enum DragDropError {
96 NotSupported,
98 CannotStart(Txt),
100}
101impl fmt::Display for DragDropError {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 match self {
104 DragDropError::NotSupported => write!(f, "not supported"),
105 DragDropError::CannotStart(txt) => write!(f, "cannot start, {txt}"),
106 }
107 }
108}
109impl std::error::Error for DragDropError {
110 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
111 None
112 }
113}