Make Clipboard argument in Widget trait mutable
This commit is contained in:
parent
35425001ed
commit
21971e0037
31 changed files with 173 additions and 127 deletions
|
|
@ -1,26 +1,44 @@
|
|||
/// A buffer for short-term storage and transfer within and between
|
||||
/// applications.
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct Clipboard(window_clipboard::Clipboard);
|
||||
pub struct Clipboard {
|
||||
state: State,
|
||||
}
|
||||
|
||||
enum State {
|
||||
Connected(window_clipboard::Clipboard),
|
||||
Unavailable,
|
||||
}
|
||||
|
||||
impl Clipboard {
|
||||
/// Creates a new [`Clipboard`] for the given window.
|
||||
pub fn connect(window: &winit::window::Window) -> Option<Clipboard> {
|
||||
window_clipboard::Clipboard::connect(window)
|
||||
.map(Clipboard)
|
||||
pub fn connect(window: &winit::window::Window) -> Clipboard {
|
||||
let state = window_clipboard::Clipboard::connect(window)
|
||||
.ok()
|
||||
.map(State::Connected)
|
||||
.unwrap_or(State::Unavailable);
|
||||
|
||||
Clipboard { state }
|
||||
}
|
||||
}
|
||||
|
||||
impl iced_native::Clipboard for Clipboard {
|
||||
fn read(&self) -> Option<String> {
|
||||
self.0.read().ok()
|
||||
match &self.state {
|
||||
State::Connected(clipboard) => clipboard.read().ok(),
|
||||
State::Unavailable => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn write(&mut self, contents: String) {
|
||||
match self.0.write(contents) {
|
||||
Ok(()) => {}
|
||||
Err(error) => log::warn!("error writing to clipboard: {}", error),
|
||||
match &mut self.state {
|
||||
State::Connected(clipboard) => match clipboard.write(contents) {
|
||||
Ok(()) => {}
|
||||
Err(error) => {
|
||||
log::warn!("error writing to clipboard: {}", error)
|
||||
}
|
||||
},
|
||||
State::Unavailable => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue