Implement and expose read and write helpers for clipboard

This commit is contained in:
Héctor Ramón Jiménez 2021-09-02 15:12:55 +07:00
parent 6887afdb2c
commit 7a335a0408
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
4 changed files with 22 additions and 1 deletions

View file

@ -16,10 +16,13 @@ pub use iced_native::*;
pub mod application;
pub use iced_winit::clipboard;
pub use iced_winit::settings;
pub use iced_winit::{Clipboard, Error, Mode};
pub use iced_winit::{Error, Mode};
#[doc(no_inline)]
pub use application::Application;
#[doc(no_inline)]
pub use clipboard::Clipboard;
#[doc(no_inline)]
pub use settings::Settings;

3
src/clipboard.rs Normal file
View file

@ -0,0 +1,3 @@
//! Access the clipboard.
#[cfg(not(target_arch = "wasm32"))]
pub use crate::runtime::clipboard::{read, write};

View file

@ -183,6 +183,7 @@ mod error;
mod result;
mod sandbox;
pub mod clipboard;
pub mod executor;
pub mod keyboard;
pub mod mouse;

View file

@ -1,6 +1,8 @@
//! Access the clipboard.
pub use iced_native::clipboard::Action;
use crate::command::{self, Command};
/// A buffer for short-term storage and transfer within and between
/// applications.
#[allow(missing_debug_implementations)]
@ -55,3 +57,15 @@ impl iced_native::Clipboard for Clipboard {
self.write(contents)
}
}
/// Read the current contents of the clipboard.
pub fn read<Message>(
f: impl Fn(Option<String>) -> Message + 'static,
) -> Command<Message> {
Command::Single(command::Action::Clipboard(Action::Read(Box::new(f))))
}
/// Write the given contents to the clipboard.
pub fn write<Message>(contents: String) -> Command<Message> {
Command::Single(command::Action::Clipboard(Action::Write(contents)))
}