|
1 | | -//! Provides a clipboard abstraction to access the target system's clipboard. |
| 1 | +mod use_clipboard; |
2 | 2 |
|
3 | | -use copypasta::{ClipboardContext, ClipboardProvider}; |
4 | | -use std::fmt; |
5 | | - |
6 | | -/// Contains the context for interacting with the clipboard. |
7 | | -/// |
8 | | -/// # Examples |
9 | | -/// |
10 | | -/// ``` |
11 | | -/// use dioxus_std; |
12 | | -/// |
13 | | -/// // Access the clipboard abstraction |
14 | | -/// let mut clipboard = dioxus_std::clipboard::Clipboard::new().unwrap(); |
15 | | -/// |
16 | | -/// // Get clipboard content |
17 | | -/// if let Ok(content) = clipboard.get_content() { |
18 | | -/// println!("{}", content); |
19 | | -/// } |
20 | | -/// |
21 | | -/// // Set clipboard content |
22 | | -/// clipboard.set_content("Hello, Dioxus!".to_string());; |
23 | | -/// |
24 | | -/// ``` |
25 | | -pub struct Clipboard { |
26 | | - ctx: ClipboardContext, |
27 | | -} |
28 | | - |
29 | | -impl Clipboard { |
30 | | - /// Creates a new struct to utilize the clipboard abstraction. |
31 | | - pub fn new() -> Result<Self, ClipboardError> { |
32 | | - let ctx = match ClipboardContext::new() { |
33 | | - Ok(ctx) => ctx, |
34 | | - Err(e) => return Err(ClipboardError::FailedToInit(e.to_string())), |
35 | | - }; |
36 | | - |
37 | | - Ok(Self { ctx }) |
38 | | - } |
39 | | - |
40 | | - /// Provides a [`String`] of the target system's current clipboard content. |
41 | | - pub fn get_content(&mut self) -> Result<String, ClipboardError> { |
42 | | - match self.ctx.get_contents() { |
43 | | - Ok(content) => Ok(content), |
44 | | - Err(e) => Err(ClipboardError::FailedToFetchContent(e.to_string())), |
45 | | - } |
46 | | - } |
47 | | - |
48 | | - /// Set the clipboard's content to the provided [`String`] |
49 | | - pub fn set_content(&mut self, value: String) -> Result<(), ClipboardError> { |
50 | | - match self.ctx.set_contents(value) { |
51 | | - Ok(()) => Ok(()), |
52 | | - Err(e) => Err(ClipboardError::FailedToSetContent(e.to_string())), |
53 | | - } |
54 | | - } |
55 | | -} |
56 | | - |
57 | | -/// Represents errors when utilizing the clipboard abstraction. |
58 | | -#[derive(Debug)] |
59 | | -pub enum ClipboardError { |
60 | | - /// Failure when initializing the clipboard. |
61 | | - FailedToInit(String), |
62 | | - /// Failure to retrieve clipboard content. |
63 | | - FailedToFetchContent(String), |
64 | | - /// Failure to set clipboard content. |
65 | | - FailedToSetContent(String), |
66 | | -} |
67 | | - |
68 | | -impl std::error::Error for ClipboardError {} |
69 | | -impl fmt::Display for ClipboardError { |
70 | | - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
71 | | - match self { |
72 | | - ClipboardError::FailedToInit(s) => write!(f, "{}", s), |
73 | | - ClipboardError::FailedToFetchContent(s) => write!(f, "{}", s), |
74 | | - ClipboardError::FailedToSetContent(s) => write!(f, "{}", s), |
75 | | - } |
76 | | - } |
77 | | -} |
78 | | - |
79 | | -// Tests |
80 | | -// This doesn't work in CI. |
81 | | -/*#[test] |
82 | | -fn test_clipboard() { |
83 | | - let mut clipboard = Clipboard::new().unwrap(); |
84 | | -
|
85 | | - // Preserve user's clipboard contents when testing |
86 | | - let initial_content = clipboard.get_content().unwrap(); |
87 | | -
|
88 | | - // Set the content |
89 | | - let new_content = String::from("Hello, Dioxus!"); |
90 | | - clipboard.set_content(new_content.clone()).unwrap(); |
91 | | -
|
92 | | - // Get the new content |
93 | | - let content = clipboard.get_content().unwrap(); |
94 | | -
|
95 | | - // Return previous content - For some reason this only works if the test panics..? |
96 | | - clipboard.set_content(initial_content).unwrap(); |
97 | | -
|
98 | | - // Check if the abstraction worked |
99 | | - assert_eq!(new_content, content); |
100 | | -}*/ |
| 3 | +pub use use_clipboard::*; |
0 commit comments