Skip to main content

ferroui_base/input/
i_async_data_transfer.rs

1use super::platform::ClipboardError;
2use super::{DataFormat, IAsyncDataTransferItem, IDataTransfer};
3use crate::reactive::IDisposable;
4use std::rc::Rc;
5
6/// Represents an object providing a list of [`IAsyncDataTransferItem`]
7/// usable by the clipboard.
8///
9/// When an implementation of this interface is put into the clipboard using
10/// [`IClipboard::set_data_async`](super::platform::IClipboard::set_data_async),
11/// it must NOT be disposed by the caller. The system will dispose of it
12/// automatically when it becomes unused.
13///
14/// When an implementation of this interface is returned from the clipboard
15/// via [`IClipboard::try_get_data_async`](super::platform::IClipboard::try_get_data_async),
16/// it MUST be disposed by the caller.
17///
18/// See [`DataTransfer`](super::DataTransfer) for the mutable
19/// implementation.
20pub trait IAsyncDataTransfer: IDisposable {
21    /// Gets the formats supported by the data transfer.
22    fn formats(&self) -> Rc<[DataFormat]>;
23
24    /// Gets a list of [`IAsyncDataTransferItem`] contained in this object.
25    ///
26    /// Some platforms (such as Windows and X11) may only support a single
27    /// data item for all formats except files.
28    ///
29    /// Items returned by this property must stay valid until the data
30    /// transfer is disposed.
31    fn items(&self) -> Rc<[Rc<dyn IAsyncDataTransferItem>]>;
32
33    /// Gets the formats supported by the data transfer, or the failure of
34    /// the platform that provides them.
35    ///
36    /// [`formats`](Self::formats) has no way to report a failure (where the
37    /// reference throws from the property, it panics); the asynchronous
38    /// clipboard operations use this member instead, so that the failure
39    /// reaches whoever awaits them. Data transfers whose formats cannot
40    /// fail keep the default.
41    fn try_formats(&self) -> Result<Rc<[DataFormat]>, ClipboardError> {
42        Ok(self.formats())
43    }
44
45    /// Gets the items of the data transfer, or the failure of the platform
46    /// that provides them. See [`try_formats`](Self::try_formats).
47    fn try_items(&self) -> Result<Rc<[Rc<dyn IAsyncDataTransferItem>]>, ClipboardError> {
48        Ok(self.items())
49    }
50
51    /// The object as a synchronous data transfer, if it also is one.
52    fn as_data_transfer(self: Rc<Self>) -> Option<Rc<dyn IDataTransfer>> {
53        None
54    }
55}