zng_view_api/
drag_drop.rsuse std::{fmt, path::PathBuf};
use zng_txt::Txt;
use crate::ipc::IpcBytes;
use bitflags::bitflags;
#[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum DragDropData {
Text {
format: Txt,
data: Txt,
},
Path(PathBuf),
Binary {
format: Txt,
data: IpcBytes,
},
}
impl fmt::Debug for DragDropData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Text { format, data } => write!(f, "Text {{ format: {:?}, data: {} bytes }}", format, data.len()),
Self::Path(data) => write!(f, "Path({})", data.display()),
Self::Binary { format, data } => write!(f, "Binary {{ format: {:?}, data: {} bytes }}", format, data.len()),
}
}
}
#[cfg(feature = "var")]
zng_var::impl_from_and_into_var! {
fn from(plain: Txt) -> DragDropData {
DragDropData::Text {
format: "text/plain".into(),
data: plain,
}
}
fn from(plain: String) -> DragDropData {
Txt::from(plain).into()
}
fn from(plain: &'static str) -> DragDropData {
Txt::from(plain).into()
}
fn from(path: PathBuf) -> DragDropData {
DragDropData::Path(path)
}
}
bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DragDropEffect: u8 {
const COPY = 0b001;
const MOVE = 0b010;
const LINK = 0b100;
}
}
impl DragDropEffect {
pub fn len(&self) -> u8 {
[DragDropEffect::COPY, DragDropEffect::MOVE, DragDropEffect::LINK]
.into_iter()
.filter(|&f| self.contains(f))
.count() as u8
}
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum DragDropError {
NotSupported,
CannotStart(Txt),
}
impl fmt::Display for DragDropError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DragDropError::NotSupported => write!(f, "not supported"),
DragDropError::CannotStart(txt) => write!(f, "cannot start, {txt}"),
}
}
}
impl std::error::Error for DragDropError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}