zng_view_api/
clipboard.rs

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