| 1 |
use std::sync::Arc; |
| 2 |
|
| 3 |
use async_trait::async_trait; |
| 4 |
use enumflags2::BitFlags; |
| 5 |
|
| 6 |
use crate::{ |
| 7 |
MaybeAppID, WindowIdentifierType, |
| 8 |
backend::{ |
| 9 |
Result, |
| 10 |
request::{Request, RequestImpl}, |
| 11 |
}, |
| 12 |
desktop::{ |
| 13 |
Color, HandleToken, |
| 14 |
request::Response, |
| 15 |
screenshot::{ |
| 16 |
AvailableTargets, ColorOptions, Screenshot as ScreenshotResponse, ScreenshotOptions, |
| 17 |
}, |
| 18 |
}, |
| 19 |
zvariant::{Optional, OwnedObjectPath}, |
| 20 |
}; |
| 21 |
|
| 22 |
#[async_trait] |
| 23 |
pub trait ScreenshotImpl: RequestImpl { |
| 24 |
#[doc(alias = "AvailableTargets")] |
| 25 |
fn available_targets(&self) -> BitFlags<AvailableTargets>; |
| 26 |
|
| 27 |
#[doc(alias = "Screenshot")] |
| 28 |
async fn screenshot( |
| 29 |
&self, |
| 30 |
token: HandleToken, |
| 31 |
app_id: Option<MaybeAppID>, |
| 32 |
window_identifier: Option<WindowIdentifierType>, |
| 33 |
options: ScreenshotOptions, |
| 34 |
) -> Result<ScreenshotResponse>; |
| 35 |
|
| 36 |
#[doc(alias = "PickColor")] |
| 37 |
async fn pick_color( |
| 38 |
&self, |
| 39 |
token: HandleToken, |
| 40 |
app_id: Option<MaybeAppID>, |
| 41 |
window_identifier: Option<WindowIdentifierType>, |
| 42 |
options: ColorOptions, |
| 43 |
) -> Result<Color>; |
| 44 |
} |
| 45 |
|
| 46 |
pub(crate) struct ScreenshotInterface { |
| 47 |
imp: Arc<dyn ScreenshotImpl>, |
| 48 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 49 |
cnx: zbus::Connection, |
| 50 |
} |
| 51 |
|
| 52 |
impl ScreenshotInterface { |
| 53 |
pub fn new( |
| 54 |
imp: Arc<dyn ScreenshotImpl>, |
| 55 |
cnx: zbus::Connection, |
| 56 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 57 |
) -> Self { |
| 58 |
Self { imp, cnx, spawn } |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
#[zbus::interface(name = "org.freedesktop.impl.portal.Screenshot")] |
| 63 |
impl ScreenshotInterface { |
| 64 |
#[zbus(property(emits_changed_signal = "const"), name = "AvailableTargets")] |
| 65 |
fn available_targets(&self) -> u32 { |
| 66 |
self.imp.available_targets().bits() |
| 67 |
} |
| 68 |
|
| 69 |
#[zbus(property(emits_changed_signal = "const"), name = "version")] |
| 70 |
fn version(&self) -> u32 { |
| 71 |
3 |
| 72 |
} |
| 73 |
|
| 74 |
#[zbus(name = "Screenshot")] |
| 75 |
#[zbus(out_args("response", "results"))] |
| 76 |
async fn screenshot( |
| 77 |
&self, |
| 78 |
handle: OwnedObjectPath, |
| 79 |
app_id: Optional<MaybeAppID>, |
| 80 |
window_identifier: Optional<WindowIdentifierType>, |
| 81 |
options: ScreenshotOptions, |
| 82 |
) -> Result<Response<ScreenshotResponse>> { |
| 83 |
let imp = Arc::clone(&self.imp); |
| 84 |
|
| 85 |
Request::spawn( |
| 86 |
"Screenshot::Screenshot", |
| 87 |
&self.cnx, |
| 88 |
handle.clone(), |
| 89 |
Arc::clone(&self.imp), |
| 90 |
Arc::clone(&self.spawn), |
| 91 |
async move { |
| 92 |
imp.screenshot( |
| 93 |
HandleToken::try_from(&handle).unwrap(), |
| 94 |
app_id.into(), |
| 95 |
window_identifier.into(), |
| 96 |
options, |
| 97 |
) |
| 98 |
.await |
| 99 |
}, |
| 100 |
) |
| 101 |
.await |
| 102 |
} |
| 103 |
|
| 104 |
#[zbus(name = "PickColor")] |
| 105 |
#[zbus(out_args("response", "results"))] |
| 106 |
async fn pick_color( |
| 107 |
&self, |
| 108 |
handle: OwnedObjectPath, |
| 109 |
app_id: Optional<MaybeAppID>, |
| 110 |
window_identifier: Optional<WindowIdentifierType>, |
| 111 |
options: ColorOptions, |
| 112 |
) -> Result<Response<Color>> { |
| 113 |
let imp = Arc::clone(&self.imp); |
| 114 |
|
| 115 |
Request::spawn( |
| 116 |
"Screenshot::PickColor", |
| 117 |
&self.cnx, |
| 118 |
handle.clone(), |
| 119 |
Arc::clone(&self.imp), |
| 120 |
Arc::clone(&self.spawn), |
| 121 |
async move { |
| 122 |
imp.pick_color( |
| 123 |
HandleToken::try_from(&handle).unwrap(), |
| 124 |
app_id.into(), |
| 125 |
window_identifier.into(), |
| 126 |
options, |
| 127 |
) |
| 128 |
.await |
| 129 |
}, |
| 130 |
) |
| 131 |
.await |
| 132 |
} |
| 133 |
} |