Introduce Kind in core::clipboard

This commit is contained in:
Héctor Ramón Jiménez 2024-02-13 03:14:08 +01:00
parent 4155edab8d
commit 508b3fe1f1
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 97 additions and 130 deletions

View file

@ -160,5 +160,5 @@ web-sys = "=0.3.67"
web-time = "0.2" web-time = "0.2"
wgpu = "0.19" wgpu = "0.19"
winapi = "0.3" winapi = "0.3"
window_clipboard = "0.4" window_clipboard = "0.4.1"
winit = { git = "https://github.com/iced-rs/winit.git", rev = "b91e39ece2c0d378c3b80da7f3ab50e17bb798a5" } winit = { git = "https://github.com/iced-rs/winit.git", rev = "b91e39ece2c0d378c3b80da7f3ab50e17bb798a5" }

View file

@ -4,16 +4,21 @@
/// applications. /// applications.
pub trait Clipboard { pub trait Clipboard {
/// Reads the current content of the [`Clipboard`] as text. /// Reads the current content of the [`Clipboard`] as text.
fn read(&self) -> Option<String>; fn read(&self, kind: Kind) -> Option<String>;
/// Writes the given text contents to the [`Clipboard`]. /// Writes the given text contents to the [`Clipboard`].
fn write(&mut self, contents: String); fn write(&mut self, kind: Kind, contents: String);
}
/// Reads the current content of Primary as text. /// The kind of [`Clipboard`].
fn read_primary(&self) -> Option<String>; #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
/// Writes the given text contents to Primary. /// The standard clipboard.
fn write_primary(&mut self, contents: String); Standard,
/// The primary clipboard.
///
/// Normally only present in X11 and Wayland.
Primary,
} }
/// A null implementation of the [`Clipboard`] trait. /// A null implementation of the [`Clipboard`] trait.
@ -21,15 +26,9 @@ pub trait Clipboard {
pub struct Null; pub struct Null;
impl Clipboard for Null { impl Clipboard for Null {
fn read(&self) -> Option<String> { fn read(&self, _kind: Kind) -> Option<String> {
None None
} }
fn write(&mut self, _contents: String) {} fn write(&mut self, _kind: Kind, _contents: String) {}
fn read_primary(&self) -> Option<String> {
None
}
fn write_primary(&mut self, _contents: String) {}
} }

View file

@ -1,5 +1,6 @@
//! Access the clipboard. //! Access the clipboard.
use crate::command::{self, Command}; use crate::command::{self, Command};
use crate::core::clipboard::Kind;
use crate::futures::MaybeSend; use crate::futures::MaybeSend;
use std::fmt; use std::fmt;
@ -9,10 +10,10 @@ use std::fmt;
/// [`Command`]: crate::Command /// [`Command`]: crate::Command
pub enum Action<T> { pub enum Action<T> {
/// Read the clipboard and produce `T` with the result. /// Read the clipboard and produce `T` with the result.
Read(Box<dyn Fn(Option<String>) -> T>), Read(Box<dyn Fn(Option<String>) -> T>, Kind),
/// Write the given contents to the clipboard. /// Write the given contents to the clipboard.
Write(String), Write(String, Kind),
} }
impl<T> Action<T> { impl<T> Action<T> {
@ -25,8 +26,10 @@ impl<T> Action<T> {
T: 'static, T: 'static,
{ {
match self { match self {
Self::Read(o) => Action::Read(Box::new(move |s| f(o(s)))), Self::Read(o, target) => {
Self::Write(content) => Action::Write(content), Action::Read(Box::new(move |s| f(o(s))), target)
}
Self::Write(content, target) => Action::Write(content, target),
} }
} }
} }
@ -34,8 +37,8 @@ impl<T> Action<T> {
impl<T> fmt::Debug for Action<T> { impl<T> fmt::Debug for Action<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Self::Read(_) => write!(f, "Action::Read"), Self::Read(_, target) => write!(f, "Action::Read{target:?}"),
Self::Write(_) => write!(f, "Action::Write"), Self::Write(_, target) => write!(f, "Action::Write({target:?})"),
} }
} }
} }
@ -44,24 +47,34 @@ impl<T> fmt::Debug for Action<T> {
pub fn read<Message>( pub fn read<Message>(
f: impl Fn(Option<String>) -> Message + 'static, f: impl Fn(Option<String>) -> Message + 'static,
) -> Command<Message> { ) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Read(Box::new(f)))) Command::single(command::Action::Clipboard(Action::Read(
Box::new(f),
Kind::Standard,
)))
}
/// Read the current contents of the primary clipboard.
pub fn read_primary<Message>(
f: impl Fn(Option<String>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Read(
Box::new(f),
Kind::Primary,
)))
} }
/// Write the given contents to the clipboard. /// Write the given contents to the clipboard.
pub fn write<Message>(contents: String) -> Command<Message> { pub fn write<Message>(contents: String) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Write(contents))) Command::single(command::Action::Clipboard(Action::Write(
contents,
Kind::Standard,
)))
} }
/// Read the current contents of primary. /// Write the given contents to the primary clipboard.
pub fn read_primary<Message>(
f: impl Fn(Option<String>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::ClipboardPrimary(Action::Read(Box::new(
f,
))))
}
/// Write the given contents to primary.
pub fn write_primary<Message>(contents: String) -> Command<Message> { pub fn write_primary<Message>(contents: String) -> Command<Message> {
Command::single(command::Action::ClipboardPrimary(Action::Write(contents))) Command::single(command::Action::Clipboard(Action::Write(
contents,
Kind::Primary,
)))
} }

View file

@ -26,9 +26,6 @@ pub enum Action<T> {
/// Run a clipboard action. /// Run a clipboard action.
Clipboard(clipboard::Action<T>), Clipboard(clipboard::Action<T>),
/// Run a clipboard action on primary.
ClipboardPrimary(clipboard::Action<T>),
/// Run a window action. /// Run a window action.
Window(window::Action<T>), Window(window::Action<T>),
@ -69,9 +66,6 @@ impl<T> Action<T> {
Self::Future(future) => Action::Future(Box::pin(future.map(f))), Self::Future(future) => Action::Future(Box::pin(future.map(f))),
Self::Stream(stream) => Action::Stream(Box::pin(stream.map(f))), Self::Stream(stream) => Action::Stream(Box::pin(stream.map(f))),
Self::Clipboard(action) => Action::Clipboard(action.map(f)), Self::Clipboard(action) => Action::Clipboard(action.map(f)),
Self::ClipboardPrimary(action) => {
Action::ClipboardPrimary(action.map(f))
}
Self::Window(window) => Action::Window(window.map(f)), Self::Window(window) => Action::Window(window.map(f)),
Self::System(system) => Action::System(system.map(f)), Self::System(system) => Action::System(system.map(f)),
Self::Widget(operation) => { Self::Widget(operation) => {
@ -94,9 +88,6 @@ impl<T> fmt::Debug for Action<T> {
Self::Clipboard(action) => { Self::Clipboard(action) => {
write!(f, "Action::Clipboard({action:?})") write!(f, "Action::Clipboard({action:?})")
} }
Self::ClipboardPrimary(action) => {
write!(f, "Action::ClipboardPrimary({action:?})")
}
Self::Window(action) => { Self::Window(action) => {
write!(f, "Action::Window({action:?})") write!(f, "Action::Window({action:?})")
} }

View file

@ -1,4 +1,5 @@
//! Display a multi-line text input for text editing. //! Display a multi-line text input for text editing.
use crate::core::clipboard::{self, Clipboard};
use crate::core::event::{self, Event}; use crate::core::event::{self, Event};
use crate::core::keyboard; use crate::core::keyboard;
use crate::core::keyboard::key; use crate::core::keyboard::key;
@ -10,7 +11,7 @@ use crate::core::text::highlighter::{self, Highlighter};
use crate::core::text::{self, LineHeight}; use crate::core::text::{self, LineHeight};
use crate::core::widget::{self, Widget}; use crate::core::widget::{self, Widget};
use crate::core::{ use crate::core::{
Clipboard, Element, Length, Padding, Pixels, Rectangle, Shell, Size, Vector, Element, Length, Padding, Pixels, Rectangle, Shell, Size, Vector,
}; };
use std::cell::RefCell; use std::cell::RefCell;
@ -448,17 +449,19 @@ where
} }
Update::Copy => { Update::Copy => {
if let Some(selection) = self.content.selection() { if let Some(selection) = self.content.selection() {
clipboard.write(selection); clipboard.write(clipboard::Kind::Standard, selection);
} }
} }
Update::Cut => { Update::Cut => {
if let Some(selection) = self.content.selection() { if let Some(selection) = self.content.selection() {
clipboard.write(selection.clone()); clipboard.write(clipboard::Kind::Standard, selection);
shell.publish(on_edit(Action::Edit(Edit::Delete))); shell.publish(on_edit(Action::Edit(Edit::Delete)));
} }
} }
Update::Paste => { Update::Paste => {
if let Some(contents) = clipboard.read() { if let Some(contents) =
clipboard.read(clipboard::Kind::Standard)
{
shell.publish(on_edit(Action::Edit(Edit::Paste( shell.publish(on_edit(Action::Edit(Edit::Paste(
Arc::new(contents), Arc::new(contents),
)))); ))));

View file

@ -12,6 +12,7 @@ pub use value::Value;
use editor::Editor; use editor::Editor;
use crate::core::alignment; use crate::core::alignment;
use crate::core::clipboard::{self, Clipboard};
use crate::core::event::{self, Event}; use crate::core::event::{self, Event};
use crate::core::keyboard; use crate::core::keyboard;
use crate::core::keyboard::key; use crate::core::keyboard::key;
@ -26,8 +27,8 @@ use crate::core::widget::operation::{self, Operation};
use crate::core::widget::tree::{self, Tree}; use crate::core::widget::tree::{self, Tree};
use crate::core::window; use crate::core::window;
use crate::core::{ use crate::core::{
Clipboard, Element, Layout, Length, Padding, Pixels, Point, Rectangle, Element, Layout, Length, Padding, Pixels, Point, Rectangle, Shell, Size,
Shell, Size, Vector, Widget, Vector, Widget,
}; };
use crate::runtime::Command; use crate::runtime::Command;
@ -864,8 +865,10 @@ where
if let Some((start, end)) = if let Some((start, end)) =
state.cursor.selection(value) state.cursor.selection(value)
{ {
clipboard clipboard.write(
.write(value.select(start, end).to_string()); clipboard::Kind::Standard,
value.select(start, end).to_string(),
);
} }
} }
keyboard::Key::Character("x") keyboard::Key::Character("x")
@ -874,8 +877,10 @@ where
if let Some((start, end)) = if let Some((start, end)) =
state.cursor.selection(value) state.cursor.selection(value)
{ {
clipboard clipboard.write(
.write(value.select(start, end).to_string()); clipboard::Kind::Standard,
value.select(start, end).to_string(),
);
} }
let mut editor = Editor::new(value, &mut state.cursor); let mut editor = Editor::new(value, &mut state.cursor);
@ -894,7 +899,7 @@ where
Some(content) => content, Some(content) => content,
None => { None => {
let content: String = clipboard let content: String = clipboard
.read() .read(clipboard::Kind::Standard)
.unwrap_or_default() .unwrap_or_default()
.chars() .chars()
.filter(|c| !c.is_control()) .filter(|c| !c.is_control())

View file

@ -704,27 +704,15 @@ pub fn run_command<A, C, E>(
runtime.run(stream); runtime.run(stream);
} }
command::Action::Clipboard(action) => match action { command::Action::Clipboard(action) => match action {
clipboard::Action::Read(tag) => { clipboard::Action::Read(tag, kind) => {
let message = tag(clipboard.read()); let message = tag(clipboard.read(kind));
proxy proxy
.send_event(message) .send_event(message)
.expect("Send message to event loop"); .expect("Send message to event loop");
} }
clipboard::Action::Write(contents) => { clipboard::Action::Write(contents, kind) => {
clipboard.write(contents); clipboard.write(kind, contents);
}
},
command::Action::ClipboardPrimary(action) => match action {
clipboard::Action::Read(tag) => {
let message = tag(clipboard.read_primary());
proxy
.send_event(message)
.expect("Send message to event loop");
}
clipboard::Action::Write(contents) => {
clipboard.write_primary(contents);
} }
}, },
command::Action::Window(action) => match action { command::Action::Window(action) => match action {

View file

@ -1,5 +1,7 @@
//! Access the clipboard. //! Access the clipboard.
use crate::core::clipboard::Kind;
/// A buffer for short-term storage and transfer within and between /// A buffer for short-term storage and transfer within and between
/// applications. /// applications.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
@ -33,46 +35,32 @@ impl Clipboard {
} }
/// Reads the current content of the [`Clipboard`] as text. /// Reads the current content of the [`Clipboard`] as text.
pub fn read(&self) -> Option<String> { pub fn read(&self, kind: Kind) -> Option<String> {
match &self.state { match &self.state {
State::Connected(clipboard) => clipboard.read().ok(), State::Connected(clipboard) => match kind {
Kind::Standard => clipboard.read().ok(),
Kind::Primary => clipboard.read_primary().and_then(Result::ok),
},
State::Unavailable => None, State::Unavailable => None,
} }
} }
/// Writes the given text contents to the [`Clipboard`]. /// Writes the given text contents to the [`Clipboard`].
pub fn write(&mut self, contents: String) { pub fn write(&mut self, kind: Kind, contents: String) {
match &mut self.state {
State::Connected(clipboard) => match clipboard.write(contents) {
Ok(()) => {}
Err(error) => {
log::warn!("error writing to clipboard: {error}");
}
},
State::Unavailable => {}
}
}
/// Reads the current content of primary as text.
pub fn read_primary(&self) -> Option<String> {
match &self.state {
State::Connected(clipboard) => {
clipboard.read_primary().and_then(|r| r.ok())
}
State::Unavailable => None,
}
}
/// Writes the given text contents to primary.
pub fn write_primary(&mut self, contents: String) {
match &mut self.state { match &mut self.state {
State::Connected(clipboard) => { State::Connected(clipboard) => {
match clipboard.write_primary(contents) { let result = match kind {
Some(Ok(())) => {} Kind::Standard => clipboard.write(contents),
Some(Err(error)) => { Kind::Primary => {
log::warn!("error writing to primary: {error}"); clipboard.write_primary(contents).unwrap_or(Ok(()))
}
};
match result {
Ok(()) => {}
Err(error) => {
log::warn!("error writing to clipboard: {error}");
} }
None => {}
} }
} }
State::Unavailable => {} State::Unavailable => {}
@ -81,19 +69,11 @@ impl Clipboard {
} }
impl crate::core::Clipboard for Clipboard { impl crate::core::Clipboard for Clipboard {
fn read(&self) -> Option<String> { fn read(&self, kind: Kind) -> Option<String> {
self.read() self.read(kind)
} }
fn write(&mut self, contents: String) { fn write(&mut self, kind: Kind, contents: String) {
self.write(contents); self.write(kind, contents);
}
fn read_primary(&self) -> Option<String> {
self.read_primary()
}
fn write_primary(&mut self, contents: String) {
self.write_primary(contents);
} }
} }

View file

@ -876,27 +876,15 @@ fn run_command<A, C, E>(
runtime.run(Box::pin(stream)); runtime.run(Box::pin(stream));
} }
command::Action::Clipboard(action) => match action { command::Action::Clipboard(action) => match action {
clipboard::Action::Read(tag) => { clipboard::Action::Read(tag, kind) => {
let message = tag(clipboard.read()); let message = tag(clipboard.read(kind));
proxy proxy
.send_event(message) .send_event(message)
.expect("Send message to event loop"); .expect("Send message to event loop");
} }
clipboard::Action::Write(contents) => { clipboard::Action::Write(contents, kind) => {
clipboard.write(contents); clipboard.write(kind, contents);
}
},
command::Action::ClipboardPrimary(action) => match action {
clipboard::Action::Read(tag) => {
let message = tag(clipboard.read_primary());
proxy
.send_event(message)
.expect("Send message to event loop");
}
clipboard::Action::Write(contents) => {
clipboard.write_primary(contents);
} }
}, },
command::Action::Window(action) => match action { command::Action::Window(action) => match action {