iced_runtime/
clipboard.rs
1use window_clipboard::mime::{AllowedMimeTypes, AsMimeTypes};
3
4use crate::core::clipboard::Kind;
5use crate::futures::futures::channel::oneshot;
6use crate::task::{self, Task};
7
8pub enum Action {
12 Read {
14 target: Kind,
16 channel: oneshot::Sender<Option<String>>,
18 },
19
20 Write {
22 target: Kind,
24 contents: String,
26 },
27
28 WriteData(Box<dyn AsMimeTypes + Send + Sync + 'static>, Kind),
30
31 #[allow(clippy::type_complexity)]
32 ReadData(
34 Vec<String>,
35 oneshot::Sender<Option<(Vec<u8>, String)>>,
36 Kind,
38 ),
39}
40
41impl std::fmt::Debug for Action {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 match self {
44 Self::Read { channel: _, target } => {
45 write!(f, "Action::Read{target:?}")
46 }
47 Self::Write {
48 contents: _,
49 target,
50 } => {
51 write!(f, "Action::Write({target:?})")
52 }
53 Self::WriteData(_, target) => {
54 write!(f, "Action::WriteData({target:?})")
55 }
56 Self::ReadData(_, _, target) => {
57 write!(f, "Action::ReadData({target:?})")
58 }
59 }
60 }
61}
62
63pub fn read() -> Task<Option<String>> {
65 task::oneshot(|channel| {
66 crate::Action::Clipboard(Action::Read {
67 target: Kind::Standard,
68 channel,
69 })
70 })
71}
72
73pub fn read_primary() -> Task<Option<String>> {
75 task::oneshot(|channel| {
76 crate::Action::Clipboard(Action::Read {
77 target: Kind::Primary,
78 channel,
79 })
80 })
81}
82
83pub fn write<T>(contents: String) -> Task<T> {
85 task::effect(crate::Action::Clipboard(Action::Write {
86 target: Kind::Standard,
87 contents,
88 }))
89}
90
91pub fn write_primary<Message>(contents: String) -> Task<Message> {
93 task::effect(crate::Action::Clipboard(Action::Write {
94 target: Kind::Primary,
95 contents,
96 }))
97}
98pub fn read_data<T: AllowedMimeTypes>() -> Task<Option<T>> {
100 task::oneshot(|tx| {
101 crate::Action::Clipboard(Action::ReadData(
102 T::allowed().into(),
103 tx,
104 Kind::Standard,
105 ))
106 })
107 .map(|d| d.and_then(|d| T::try_from(d).ok()))
108}
109
110pub fn write_data<Message>(
112 contents: impl AsMimeTypes + std::marker::Sync + std::marker::Send + 'static,
113) -> Task<Message> {
114 task::effect(crate::Action::Clipboard(Action::WriteData(
115 Box::new(contents),
116 Kind::Standard,
117 )))
118}
119
120pub fn read_primary_data<T: AllowedMimeTypes>() -> Task<Option<T>> {
122 task::oneshot(|tx| {
123 crate::Action::Clipboard(Action::ReadData(
124 T::allowed().into(),
125 tx,
126 Kind::Primary,
128 ))
129 })
130 .map(|d| d.and_then(|d| T::try_from(d).ok()))
131}
132
133pub fn write_primary_data<Message>(
135 contents: impl AsMimeTypes + std::marker::Sync + std::marker::Send + 'static,
136) -> Task<Message> {
137 task::effect(crate::Action::Clipboard(Action::WriteData(
138 Box::new(contents),
139 Kind::Primary,
140 )))
141}