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