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 Paths(Vec<PathBuf>),
27 /// Any data format supported only by the specific view-process implementation.
28 ///
29 /// The view-process implementation may also pass this to the operating system as binary data.
30 Extension {
31 /// Type key, must be in a format defined by the view-process.
32 data_type: Txt,
33 /// The raw data.
34 data: IpcBytes,
35 },
36}
37
38/// Clipboard data type.
39#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
40#[non_exhaustive]
41pub enum ClipboardType {
42 /// A [`ClipboardData::Text`].
43 Text,
44 /// A [`ClipboardData::Image`].
45 Image,
46 /// A [`ClipboardData::Paths`].
47 Paths,
48 /// A [`ClipboardData::Extension`].
49 Extension(Txt),
50}
51
52/// Clipboard read/write error.
53#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
54#[non_exhaustive]
55pub enum ClipboardError {
56 /// Requested format is not set on the clipboard.
57 NotFound,
58 /// View-process or operating system does not support the data type.
59 NotSupported,
60 /// Other error.
61 ///
62 /// The string can be a debug description of the error, only suitable for logging.
63 Other(Txt),
64}
65impl fmt::Display for ClipboardError {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 match self {
68 ClipboardError::NotFound => write!(f, "clipboard does not contain the requested format"),
69 ClipboardError::NotSupported => write!(f, "clipboard implementation does not support the format"),
70 ClipboardError::Other(_) => write!(f, "internal error"),
71 }
72 }
73}
74impl std::error::Error for ClipboardError {}
75
76/// Clipboard types and operations implemented by the view-process.
77#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
78#[non_exhaustive]
79pub struct ClipboardTypes {
80 /// Data formats that the implementation can read.
81 pub read: Vec<ClipboardType>,
82
83 /// Data formats that the implementation can write.
84 pub write: Vec<ClipboardType>,
85 /// Implementation can put multiple data on the clipboard at the same time.
86 pub write_multi: bool,
87}
88impl ClipboardTypes {
89 /// New.
90 pub const fn new(read: Vec<ClipboardType>, write: Vec<ClipboardType>, write_multi: bool) -> Self {
91 Self { read, write, write_multi }
92 }
93}