zng_view_api/
drag_drop.rs

1//! Drag&drop types.
2
3use std::{fmt, path::PathBuf};
4
5use zng_task::channel::IpcBytes;
6use zng_txt::Txt;
7
8use bitflags::bitflags;
9
10use crate::image::ImageId;
11
12/// Drag&drop data payload.
13#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
14#[non_exhaustive]
15pub enum DragDropData {
16    /// Text string.
17    ///
18    /// View-process can convert between [`String`] and the text formats of the platform.
19    Text(Txt),
20    /// Image data.
21    ///
22    /// View-process reads from clipboard in any format supported and starts an image decode task
23    /// for the data, the [`ImageId`] may still be decoding when received. For writing the
24    /// view-process will expect the image to already be loaded, the image will be encoded in
25    /// a format compatible with the platform clipboard.
26    Image(ImageId),
27    /// List of paths.
28    Paths(Vec<PathBuf>),
29    /// Any data format specific for the view-process implementation.
30    ///
31    /// The view-process implementation may also pass this to the operating system as binary data.
32    Extension {
33        /// Type key, must be in a format defined by the view-process.
34        data_type: Txt,
35        /// The raw data.
36        data: IpcBytes,
37    },
38}
39
40bitflags! {
41    /// Drag&drop drop effect on the data source.
42    #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
43    pub struct DragDropEffect: u8 {
44        /// Indicates that the dragged data will be copied from its present location to the drop location.
45        const COPY = 0b001;
46        /// Indicates that the dragged data will be moved from its present location to the drop location.
47        const MOVE = 0b010;
48        /// Indicates that some form of relationship or connection will be created between the source and drop locations.
49        const LINK = 0b100;
50    }
51}
52impl DragDropEffect {
53    /// Count effects flagged.
54    pub fn len(&self) -> u8 {
55        [DragDropEffect::COPY, DragDropEffect::MOVE, DragDropEffect::LINK]
56            .into_iter()
57            .filter(|&f| self.contains(f))
58            .count() as u8
59    }
60}
61
62/// Error for drag start or cancel error.
63#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
64#[non_exhaustive]
65pub enum DragDropError {
66    /// View-process implementer does not support any of the provided data types.
67    NotSupported,
68    /// Cannot start dragging.
69    CannotStart(Txt),
70}
71impl fmt::Display for DragDropError {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        match self {
74            DragDropError::NotSupported => write!(f, "not supported"),
75            DragDropError::CannotStart(txt) => write!(f, "cannot start, {txt}"),
76        }
77    }
78}
79impl std::error::Error for DragDropError {
80    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
81        None
82    }
83}