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::Rgba16 { width, height, pixels } => Some((*width, *height, Cow::Owned(crate::image::rgba16_to_rgba8(pixels)))),
41 ImageSource::Encoded(_) => None,
42 }
43}
44
45#[derive(Clone, Debug, Default, PartialEq)]
47pub struct ClipboardContent {
48 pub text: Option<String>,
49 pub html: Option<String>,
50 pub image: Option<Image>,
51 pub data: Vec<(String, Vec<u8>)>,
53}
54
55impl ClipboardContent {
56 pub fn text(text: impl Into<String>) -> Self {
57 Self { text: Some(text.into()), ..Default::default() }
58 }
59
60 pub fn image(image: Image) -> Self {
61 Self { image: Some(image), ..Default::default() }
62 }
63
64 pub fn with_text(mut self, text: impl Into<String>) -> Self {
65 self.text = Some(text.into());
66 self
67 }
68
69 pub fn with_html(mut self, html: impl Into<String>) -> Self {
70 self.html = Some(html.into());
71 self
72 }
73
74 pub fn with_image(mut self, image: Image) -> Self {
75 self.image = Some(image);
76 self
77 }
78
79 pub fn with_data(mut self, format: &str, bytes: impl Into<Vec<u8>>) -> Self {
81 self.data.retain(|(f, _)| f != format);
82 self.data.push((format.to_string(), bytes.into()));
83 self
84 }
85
86 pub fn get_data(&self, format: &str) -> Option<&[u8]> {
88 self.data.iter().find(|(f, _)| f == format).map(|(_, b)| b.as_slice())
89 }
90
91 pub fn has(&self, format: &str) -> bool {
93 match format {
94 TEXT => self.text.is_some(),
95 HTML => self.html.is_some(),
96 IMAGE => self.image.is_some(),
97 _ => self.get_data(format).is_some(),
98 }
99 }
100
101 pub fn is_empty(&self) -> bool {
102 self.text.is_none() && self.html.is_none() && self.image.is_none() && self.data.is_empty()
103 }
104}
105
106#[derive(Default)]
109pub struct LocalClipboard {
110 content: ClipboardContent,
111 mark: Option<Mark>,
112}
113
114#[derive(PartialEq)]
115enum Mark {
116 Sequence(u64),
117 Text(Option<String>),
118}
119
120fn mark(service: &mut dyn ClipboardService) -> Mark {
121 match service.sequence() {
122 Some(n) => Mark::Sequence(n),
123 None => Mark::Text(service.get_text()),
124 }
125}
126
127fn local(ui: &mut Ui) -> Option<&ClipboardContent> {
129 let services = ui.services_mut();
130 let valid = match services.clipboard.as_deref_mut() {
131 Some(service) => services.clipboard_local.mark.as_ref() == Some(&mark(service)),
132 None => true,
133 };
134 valid.then_some(&services.clipboard_local.content)
135}
136
137pub fn set(ui: &mut Ui, content: ClipboardContent) {
139 let services = ui.services_mut();
140 let mark = services.clipboard.as_deref_mut().map(|service| {
141 service.set_content(&content);
142 mark(service)
143 });
144 services.clipboard_local = LocalClipboard { content, mark };
145}
146
147pub fn set_text(ui: &mut Ui, text: &str) {
149 set(ui, ClipboardContent::text(text));
150}
151
152pub fn clear(ui: &mut Ui) {
154 set(ui, ClipboardContent::default());
155}
156
157pub fn text(ui: &mut Ui) -> Option<String> {
159 let native = ui.services_mut().clipboard.as_deref_mut().and_then(|s| s.get_text());
160 native.or_else(|| local(ui)?.text.clone()).filter(|t| !t.is_empty())
161}
162
163pub fn html(ui: &mut Ui) -> Option<String> {
165 let native = ui.services_mut().clipboard.as_deref_mut().and_then(|s| s.get_html());
166 native.or_else(|| local(ui)?.html.clone())
167}
168
169pub fn image(ui: &mut Ui) -> Option<Image> {
171 if let Some(image) = local(ui).and_then(|c| c.image.clone()) {
172 return Some(image);
173 }
174 ui.services_mut().clipboard.as_deref_mut().and_then(|s| s.get_image())
175}
176
177pub fn data(ui: &mut Ui, format: &str) -> Option<Vec<u8>> {
179 if let Some(bytes) = local(ui).and_then(|c| c.get_data(format)) {
180 return Some(bytes.to_vec());
181 }
182 ui.services_mut().clipboard.as_deref_mut().and_then(|s| s.get_data(format))
183}
184
185pub fn has(ui: &mut Ui, format: &str) -> bool {
187 if local(ui).is_some_and(|c| c.has(format)) {
188 return true;
189 }
190 let Some(service) = ui.services_mut().clipboard.as_deref_mut() else { return false };
191 match format {
192 TEXT => service.get_text().is_some_and(|t| !t.is_empty()),
193 _ => service.has(format),
194 }
195}