1use std::borrow::Cow;
15
16use crate::image::{Image, ImageSource};
17use crate::platform::ClipboardService;
18use crate::Ui;
19
20pub const TEXT: &str = "text/plain";
22pub const HTML: &str = "text/html";
24pub const IMAGE: &str = "image";
26pub const COMPONENTS: &str = "application/x-aylis-components+json";
28
29pub fn rgba(image: &Image) -> Option<(u32, u32, Cow<'_, [u8]>)> {
33 match image.source() {
34 ImageSource::Rgba { width, height, pixels } => Some((*width, *height, Cow::Borrowed(pixels))),
35 ImageSource::Bgra { width, height, pixels } => {
36 let mut out = pixels.clone();
37 out.chunks_exact_mut(4).for_each(|px| px.swap(0, 2));
38 Some((*width, *height, Cow::Owned(out)))
39 }
40 ImageSource::Encoded(_) => None,
41 }
42}
43
44#[derive(Clone, Debug, Default, PartialEq)]
46pub struct ClipboardContent {
47 pub text: Option<String>,
48 pub html: Option<String>,
49 pub image: Option<Image>,
50 pub data: Vec<(String, Vec<u8>)>,
52}
53
54impl ClipboardContent {
55 pub fn text(text: impl Into<String>) -> Self {
56 Self { text: Some(text.into()), ..Default::default() }
57 }
58
59 pub fn image(image: Image) -> Self {
60 Self { image: Some(image), ..Default::default() }
61 }
62
63 pub fn with_text(mut self, text: impl Into<String>) -> Self {
64 self.text = Some(text.into());
65 self
66 }
67
68 pub fn with_html(mut self, html: impl Into<String>) -> Self {
69 self.html = Some(html.into());
70 self
71 }
72
73 pub fn with_image(mut self, image: Image) -> Self {
74 self.image = Some(image);
75 self
76 }
77
78 pub fn with_data(mut self, format: &str, bytes: impl Into<Vec<u8>>) -> Self {
80 self.data.retain(|(f, _)| f != format);
81 self.data.push((format.to_string(), bytes.into()));
82 self
83 }
84
85 pub fn get_data(&self, format: &str) -> Option<&[u8]> {
87 self.data.iter().find(|(f, _)| f == format).map(|(_, b)| b.as_slice())
88 }
89
90 pub fn has(&self, format: &str) -> bool {
92 match format {
93 TEXT => self.text.is_some(),
94 HTML => self.html.is_some(),
95 IMAGE => self.image.is_some(),
96 _ => self.get_data(format).is_some(),
97 }
98 }
99
100 pub fn is_empty(&self) -> bool {
101 self.text.is_none() && self.html.is_none() && self.image.is_none() && self.data.is_empty()
102 }
103}
104
105#[derive(Default)]
108pub struct LocalClipboard {
109 content: ClipboardContent,
110 mark: Option<Mark>,
111}
112
113#[derive(PartialEq)]
114enum Mark {
115 Sequence(u64),
116 Text(Option<String>),
117}
118
119fn mark(service: &mut dyn ClipboardService) -> Mark {
120 match service.sequence() {
121 Some(n) => Mark::Sequence(n),
122 None => Mark::Text(service.get_text()),
123 }
124}
125
126fn local(ui: &mut Ui) -> Option<&ClipboardContent> {
128 let services = ui.services_mut();
129 let valid = match services.clipboard.as_deref_mut() {
130 Some(service) => services.clipboard_local.mark.as_ref() == Some(&mark(service)),
131 None => true,
132 };
133 valid.then_some(&services.clipboard_local.content)
134}
135
136pub fn set(ui: &mut Ui, content: ClipboardContent) {
138 let services = ui.services_mut();
139 let mark = services.clipboard.as_deref_mut().map(|service| {
140 service.set_content(&content);
141 mark(service)
142 });
143 services.clipboard_local = LocalClipboard { content, mark };
144}
145
146pub fn set_text(ui: &mut Ui, text: &str) {
148 set(ui, ClipboardContent::text(text));
149}
150
151pub fn clear(ui: &mut Ui) {
153 set(ui, ClipboardContent::default());
154}
155
156pub fn text(ui: &mut Ui) -> Option<String> {
158 let native = ui.services_mut().clipboard.as_deref_mut().and_then(|s| s.get_text());
159 native.or_else(|| local(ui)?.text.clone()).filter(|t| !t.is_empty())
160}
161
162pub fn html(ui: &mut Ui) -> Option<String> {
164 let native = ui.services_mut().clipboard.as_deref_mut().and_then(|s| s.get_html());
165 native.or_else(|| local(ui)?.html.clone())
166}
167
168pub fn image(ui: &mut Ui) -> Option<Image> {
170 if let Some(image) = local(ui).and_then(|c| c.image.clone()) {
171 return Some(image);
172 }
173 ui.services_mut().clipboard.as_deref_mut().and_then(|s| s.get_image())
174}
175
176pub fn data(ui: &mut Ui, format: &str) -> Option<Vec<u8>> {
178 if let Some(bytes) = local(ui).and_then(|c| c.get_data(format)) {
179 return Some(bytes.to_vec());
180 }
181 ui.services_mut().clipboard.as_deref_mut().and_then(|s| s.get_data(format))
182}
183
184pub fn has(ui: &mut Ui, format: &str) -> bool {
186 if local(ui).is_some_and(|c| c.has(format)) {
187 return true;
188 }
189 let Some(service) = ui.services_mut().clipboard.as_deref_mut() else { return false };
190 match format {
191 TEXT => service.get_text().is_some_and(|t| !t.is_empty()),
192 _ => service.has(format),
193 }
194}