| 1 |
//! Interact with the clipboard. |
| 2 |
//! |
| 3 |
//! The portal is mostly meant to be used along with |
| 4 |
//! [`RemoteDesktop`](crate::desktop::remote_desktop::RemoteDesktop) or |
| 5 |
//! [`InputCapture`](crate::desktop::input_capture::InputCapture). |
| 6 |
|
| 7 |
use futures_util::{Stream, StreamExt}; |
| 8 |
use serde::{Deserialize, Serialize}; |
| 9 |
use zbus::zvariant::{OwnedFd, OwnedObjectPath, Type, as_value, as_value::optional}; |
| 10 |
|
| 11 |
use super::{Session, SessionPortal}; |
| 12 |
use crate::{Result, proxy::Proxy}; |
| 13 |
|
| 14 |
/// A session that is compatible with the Clipboard portal |
| 15 |
pub trait IsClipboardSession: SessionPortal {} |
| 16 |
|
| 17 |
#[derive(Debug, Type, Serialize, Default)] |
| 18 |
#[zvariant(signature = "dict")] |
| 19 |
/// Specified options for a [`Clipboard::set_selection`] request. |
| 20 |
pub struct SetSelectionOptions<'a> { |
| 21 |
#[serde(borrow)] |
| 22 |
#[serde(with = "as_value", skip_serializing_if = "<[_]>::is_empty")] |
| 23 |
mime_types: &'a [&'a str], |
| 24 |
} |
| 25 |
|
| 26 |
impl<'a> SetSelectionOptions<'a> { |
| 27 |
/// Sets the mime types for the clipboard selection. |
| 28 |
pub fn set_mime_types(mut self, mime_types: &'a [&'a str]) -> Self { |
| 29 |
self.mime_types = mime_types; |
| 30 |
self |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
#[derive(Debug, Type, Serialize, Default)] |
| 35 |
#[zvariant(signature = "dict")] |
| 36 |
/// Specified options for a [`Clipboard::request`] request. |
| 37 |
pub struct RequestClipboardOptions {} |
| 38 |
|
| 39 |
#[derive(Debug, Type, Deserialize, Default)] |
| 40 |
#[zvariant(signature = "dict")] |
| 41 |
/// The details of a new clipboard selection. |
| 42 |
pub struct SelectionOwnerChanged { |
| 43 |
#[serde(default, with = "as_value")] |
| 44 |
mime_types: Vec<String>, |
| 45 |
#[serde(default, with = "optional")] |
| 46 |
session_is_owner: Option<bool>, |
| 47 |
} |
| 48 |
|
| 49 |
impl SelectionOwnerChanged { |
| 50 |
/// Whether the session is the owner of the clipboard selection or not. |
| 51 |
pub fn session_is_owner(&self) -> Option<bool> { |
| 52 |
self.session_is_owner |
| 53 |
} |
| 54 |
|
| 55 |
/// A list of mime types the new clipboard has content for. |
| 56 |
pub fn mime_types(&self) -> &[String] { |
| 57 |
&self.mime_types |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
#[doc(alias = "org.freedesktop.portal.Clipboard")] |
| 62 |
/// Wrapper of the DBus interface: [`org.freedesktop.portal.Clipboard`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Clipboard.html). |
| 63 |
pub struct Clipboard(Proxy<'static>); |
| 64 |
|
| 65 |
impl Clipboard { |
| 66 |
/// Create a new instance of [`Clipboard`]. |
| 67 |
pub async fn new() -> Result<Self> { |
| 68 |
Ok(Self( |
| 69 |
Proxy::new_desktop("org.freedesktop.portal.Clipboard").await?, |
| 70 |
)) |
| 71 |
} |
| 72 |
|
| 73 |
/// Create a new instance of [`Clipboard`]. |
| 74 |
pub async fn with_connection(connection: zbus::Connection) -> Result<Self> { |
| 75 |
Ok(Self( |
| 76 |
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Clipboard") |
| 77 |
.await?, |
| 78 |
)) |
| 79 |
} |
| 80 |
|
| 81 |
/// Returns the version of the portal interface. |
| 82 |
pub fn version(&self) -> u32 { |
| 83 |
self.0.version() |
| 84 |
} |
| 85 |
|
| 86 |
/// # Specifications |
| 87 |
/// |
| 88 |
/// See also [`RequestClipboard`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Clipboard.html#org-freedesktop-portal-clipboard-requestclipboard). |
| 89 |
#[doc(alias = "RequestClipboard")] |
| 90 |
pub async fn request<T: IsClipboardSession>( |
| 91 |
&self, |
| 92 |
session: &Session<T>, |
| 93 |
options: RequestClipboardOptions, |
| 94 |
) -> Result<()> { |
| 95 |
self.0 |
| 96 |
.call_method("RequestClipboard", &(session, options)) |
| 97 |
.await?; |
| 98 |
Ok(()) |
| 99 |
} |
| 100 |
|
| 101 |
/// # Specifications |
| 102 |
/// |
| 103 |
/// See also [`SetSelection`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Clipboard.html#org-freedesktop-portal-clipboard-setselection). |
| 104 |
#[doc(alias = "SetSelection")] |
| 105 |
pub async fn set_selection<'a, T: IsClipboardSession>( |
| 106 |
&self, |
| 107 |
session: &Session<T>, |
| 108 |
options: SetSelectionOptions<'a>, |
| 109 |
) -> Result<()> { |
| 110 |
self.0 |
| 111 |
.call::<()>("SetSelection", &(session, options)) |
| 112 |
.await?; |
| 113 |
|
| 114 |
Ok(()) |
| 115 |
} |
| 116 |
|
| 117 |
/// # Specifications |
| 118 |
/// |
| 119 |
/// See also [`SelectionWrite`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Clipboard.html#org-freedesktop-portal-clipboard-selectionwrite). |
| 120 |
#[doc(alias = "SelectionWrite")] |
| 121 |
pub async fn selection_write<T: IsClipboardSession>( |
| 122 |
&self, |
| 123 |
session: &Session<T>, |
| 124 |
serial: u32, |
| 125 |
) -> Result<OwnedFd> { |
| 126 |
let fd = self |
| 127 |
.0 |
| 128 |
.call::<OwnedFd>("SelectionWrite", &(session, serial)) |
| 129 |
.await?; |
| 130 |
Ok(fd) |
| 131 |
} |
| 132 |
|
| 133 |
/// # Specifications |
| 134 |
/// |
| 135 |
/// See also [`SelectionWriteDone`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Clipboard.html#org-freedesktop-portal-clipboard-selectionwritedone). |
| 136 |
#[doc(alias = "SelectionWriteDone")] |
| 137 |
pub async fn selection_write_done<T: IsClipboardSession>( |
| 138 |
&self, |
| 139 |
session: &Session<T>, |
| 140 |
serial: u32, |
| 141 |
success: bool, |
| 142 |
) -> Result<()> { |
| 143 |
self.0 |
| 144 |
.call("SelectionWriteDone", &(session, serial, success)) |
| 145 |
.await |
| 146 |
} |
| 147 |
|
| 148 |
/// # Specifications |
| 149 |
/// |
| 150 |
/// See also [`SelectionRead`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Clipboard.html#org-freedesktop-portal-clipboard-selectionread). |
| 151 |
#[doc(alias = "SelectionRead")] |
| 152 |
pub async fn selection_read<T: IsClipboardSession>( |
| 153 |
&self, |
| 154 |
session: &Session<T>, |
| 155 |
mime_type: &str, |
| 156 |
) -> Result<OwnedFd> { |
| 157 |
let fd = self |
| 158 |
.0 |
| 159 |
.call::<OwnedFd>("SelectionRead", &(session, mime_type)) |
| 160 |
.await?; |
| 161 |
Ok(fd) |
| 162 |
} |
| 163 |
|
| 164 |
/// Notifies the session that the clipboard selection has changed. |
| 165 |
/// # Specifications |
| 166 |
/// |
| 167 |
/// See also [`SelectionOwnerChanged`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Clipboard.html#org-freedesktop-portal-clipboard-selectionownerchanged). |
| 168 |
#[doc(alias = "SelectionOwnerChanged")] |
| 169 |
pub async fn receive_selection_owner_changed<T: IsClipboardSession>( |
| 170 |
&self, |
| 171 |
) -> Result<impl Stream<Item = (Session<T>, SelectionOwnerChanged)>> { |
| 172 |
let connection = self.connection().clone(); |
| 173 |
Ok(self |
| 174 |
.0 |
| 175 |
.signal::<(OwnedObjectPath, SelectionOwnerChanged)>("SelectionOwnerChanged") |
| 176 |
.await? |
| 177 |
.filter_map(move |(p, o)| { |
| 178 |
let connection = connection.clone(); |
| 179 |
async move { |
| 180 |
Session::with_connection(connection, p) |
| 181 |
.await |
| 182 |
.map(|s| (s, o)) |
| 183 |
.ok() |
| 184 |
} |
| 185 |
})) |
| 186 |
} |
| 187 |
|
| 188 |
/// # Specifications |
| 189 |
/// |
| 190 |
/// See also [`SelectionTransfer`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Clipboard.html#org-freedesktop-portal-clipboard-selectiontransfer). |
| 191 |
#[doc(alias = "SelectionTransfer")] |
| 192 |
pub async fn receive_selection_transfer<T: IsClipboardSession>( |
| 193 |
&self, |
| 194 |
) -> Result<impl Stream<Item = (Session<T>, String, u32)>> { |
| 195 |
let connection = self.connection().clone(); |
| 196 |
Ok(self |
| 197 |
.0 |
| 198 |
.signal::<(OwnedObjectPath, String, u32)>("SelectionTransfer") |
| 199 |
.await? |
| 200 |
.filter_map(move |(p, mime_type, serial)| { |
| 201 |
let connection = connection.clone(); |
| 202 |
async move { |
| 203 |
Session::with_connection(connection, p) |
| 204 |
.await |
| 205 |
.map(|session| (session, mime_type, serial)) |
| 206 |
.ok() |
| 207 |
} |
| 208 |
})) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
impl std::ops::Deref for Clipboard { |
| 213 |
type Target = zbus::Proxy<'static>; |
| 214 |
|
| 215 |
fn deref(&self) -> &Self::Target { |
| 216 |
&self.0 |
| 217 |
} |
| 218 |
} |