ashpd/desktop/
screenshot.rs1use std::fmt::Debug;
37
38use zbus::zvariant::{DeserializeDict, SerializeDict, Type};
39
40use super::{HandleToken, Request};
41use crate::{desktop::Color, proxy::Proxy, Error, WindowIdentifier};
42
43#[derive(SerializeDict, Type, Debug, Default)]
44#[zvariant(signature = "dict")]
45struct ScreenshotOptions {
46 handle_token: HandleToken,
47 modal: Option<bool>,
48 interactive: Option<bool>,
49}
50
51#[derive(SerializeDict, DeserializeDict, Type)]
52#[zvariant(signature = "dict")]
53pub struct Screenshot {
55 uri: url::Url,
56}
57
58impl Screenshot {
59 #[cfg(feature = "backend")]
60 #[cfg_attr(docsrs, doc(cfg(feature = "backend")))]
61 pub fn new(uri: url::Url) -> Self {
63 Self { uri }
64 }
65
66 pub fn request() -> ScreenshotRequest {
71 ScreenshotRequest::default()
72 }
73
74 pub fn uri(&self) -> &url::Url {
76 &self.uri
77 }
78}
79
80impl Debug for Screenshot {
81 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82 f.write_str(self.uri.as_str())
83 }
84}
85
86#[derive(SerializeDict, Type, Debug, Default)]
87#[zvariant(signature = "dict")]
88struct ColorOptions {
89 handle_token: HandleToken,
90}
91
92#[derive(Debug)]
93#[doc(alias = "org.freedesktop.portal.Screenshot")]
94struct ScreenshotProxy<'a>(Proxy<'a>);
95
96impl<'a> ScreenshotProxy<'a> {
97 pub async fn new() -> Result<ScreenshotProxy<'a>, Error> {
99 let proxy = Proxy::new_desktop("org.freedesktop.portal.Screenshot").await?;
100 Ok(Self(proxy))
101 }
102
103 #[doc(alias = "PickColor")]
113 #[doc(alias = "xdp_portal_pick_color")]
114 pub async fn pick_color(
115 &self,
116 identifier: Option<&WindowIdentifier>,
117 options: ColorOptions,
118 ) -> Result<Request<Color>, Error> {
119 let identifier = identifier.map(|i| i.to_string()).unwrap_or_default();
120 self.0
121 .request(&options.handle_token, "PickColor", &(&identifier, &options))
122 .await
123 }
124
125 #[doc(alias = "Screenshot")]
142 #[doc(alias = "xdp_portal_take_screenshot")]
143 pub async fn screenshot(
144 &self,
145 identifier: Option<&WindowIdentifier>,
146 options: ScreenshotOptions,
147 ) -> Result<Request<Screenshot>, Error> {
148 let identifier = identifier.map(|i| i.to_string()).unwrap_or_default();
149 self.0
150 .request(
151 &options.handle_token,
152 "Screenshot",
153 &(&identifier, &options),
154 )
155 .await
156 }
157}
158
159impl<'a> std::ops::Deref for ScreenshotProxy<'a> {
160 type Target = zbus::Proxy<'a>;
161
162 fn deref(&self) -> &Self::Target {
163 &self.0
164 }
165}
166
167#[derive(Debug, Default)]
168#[doc(alias = "xdp_portal_pick_color")]
169pub struct ColorRequest {
173 identifier: Option<WindowIdentifier>,
174 options: ColorOptions,
175}
176
177impl ColorRequest {
178 #[must_use]
179 pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
181 self.identifier = identifier.into();
182 self
183 }
184
185 pub async fn send(self) -> Result<Request<Color>, Error> {
187 let proxy = ScreenshotProxy::new().await?;
188 proxy
189 .pick_color(self.identifier.as_ref(), self.options)
190 .await
191 }
192}
193
194impl Color {
195 pub fn pick() -> ColorRequest {
200 ColorRequest::default()
201 }
202}
203
204#[derive(Debug, Default)]
205#[doc(alias = "xdp_portal_take_screenshot")]
206pub struct ScreenshotRequest {
210 options: ScreenshotOptions,
211 identifier: Option<WindowIdentifier>,
212}
213
214impl ScreenshotRequest {
215 #[must_use]
216 pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
218 self.identifier = identifier.into();
219 self
220 }
221
222 #[must_use]
224 pub fn modal(mut self, modal: impl Into<Option<bool>>) -> Self {
225 self.options.modal = modal.into();
226 self
227 }
228
229 #[must_use]
232 pub fn interactive(mut self, interactive: impl Into<Option<bool>>) -> Self {
233 self.options.interactive = interactive.into();
234 self
235 }
236
237 pub async fn send(self) -> Result<Request<Screenshot>, Error> {
239 let proxy = ScreenshotProxy::new().await?;
240 proxy
241 .screenshot(self.identifier.as_ref(), self.options)
242 .await
243 }
244}