zng_view_api/
clipboard.rs

1//! Clipboard types.
2
3use std::{fmt, path::PathBuf};
4
5use zng_task::channel::IpcBytes;
6use zng_txt::Txt;
7
8use crate::image::ImageId;
9
10/// Clipboard data.
11#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
12#[non_exhaustive]
13pub enum ClipboardData {
14    /// Text string.
15    ///
16    /// View-process can convert between [`String`] and the text formats of the platform.
17    Text(Txt),
18    /// Image data.
19    ///
20    /// View-process reads from clipboard in any format supported and starts an image decode task
21    /// for the data, the [`ImageId`] may still be decoding when received. For writing the
22    /// view-process will expect the image to already be loaded, the image will be encoded in
23    /// a format compatible with the platform clipboard.
24    Image(ImageId),
25    /// List of paths.
26    FileList(Vec<PathBuf>),
27    /// Any data format supported only by the specific view-process implementation.
28    Extension {
29        /// Type key, must be in a format defined by the view-process.
30        data_type: Txt,
31        /// The raw data.
32        data: IpcBytes,
33    },
34}
35
36/// Clipboard data type.
37#[derive(Debug, Clone, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
38#[non_exhaustive]
39pub enum ClipboardType {
40    /// A [`ClipboardData::Text`].
41    Text,
42    /// A [`ClipboardData::Image`].
43    Image,
44    /// A [`ClipboardData::FileList`].
45    FileList,
46    /// A [`ClipboardData::Extension`].
47    Extension(Txt),
48}
49
50/// Clipboard read/write error.
51#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
52#[non_exhaustive]
53pub enum ClipboardError {
54    /// Requested format is not set on the clipboard.
55    NotFound,
56    /// View-process or operating system does not support the data type.
57    NotSupported,
58    /// Other error.
59    ///
60    /// The string can be a debug description of the error, only suitable for logging.
61    Other(Txt),
62}
63impl fmt::Display for ClipboardError {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        match self {
66            ClipboardError::NotFound => write!(f, "clipboard does not contain the requested format"),
67            ClipboardError::NotSupported => write!(f, "clipboard implementation does not support the format"),
68            ClipboardError::Other(_) => write!(f, "internal error"),
69        }
70    }
71}
72impl std::error::Error for ClipboardError {}