From 4155edab8d123b767ddad67e24ca2d4c50f31ece Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Wed, 7 Feb 2024 17:25:40 +0100 Subject: Add support for primary clipboard --- runtime/src/clipboard.rs | 14 ++++++++++++++ runtime/src/command/action.rs | 9 +++++++++ 2 files changed, 23 insertions(+) (limited to 'runtime') diff --git a/runtime/src/clipboard.rs b/runtime/src/clipboard.rs index bc450912..49044a0f 100644 --- a/runtime/src/clipboard.rs +++ b/runtime/src/clipboard.rs @@ -51,3 +51,17 @@ pub fn read( pub fn write(contents: String) -> Command { Command::single(command::Action::Clipboard(Action::Write(contents))) } + +/// Read the current contents of primary. +pub fn read_primary( + f: impl Fn(Option) -> Message + 'static, +) -> Command { + Command::single(command::Action::ClipboardPrimary(Action::Read(Box::new( + f, + )))) +} + +/// Write the given contents to primary. +pub fn write_primary(contents: String) -> Command { + Command::single(command::Action::ClipboardPrimary(Action::Write(contents))) +} diff --git a/runtime/src/command/action.rs b/runtime/src/command/action.rs index c9ffe801..f04c642c 100644 --- a/runtime/src/command/action.rs +++ b/runtime/src/command/action.rs @@ -26,6 +26,9 @@ pub enum Action { /// Run a clipboard action. Clipboard(clipboard::Action), + /// Run a clipboard action on primary. + ClipboardPrimary(clipboard::Action), + /// Run a window action. Window(window::Action), @@ -66,6 +69,9 @@ impl Action { Self::Future(future) => Action::Future(Box::pin(future.map(f))), Self::Stream(stream) => Action::Stream(Box::pin(stream.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::System(system) => Action::System(system.map(f)), Self::Widget(operation) => { @@ -88,6 +94,9 @@ impl fmt::Debug for Action { Self::Clipboard(action) => { write!(f, "Action::Clipboard({action:?})") } + Self::ClipboardPrimary(action) => { + write!(f, "Action::ClipboardPrimary({action:?})") + } Self::Window(action) => { write!(f, "Action::Window({action:?})") } -- cgit From 508b3fe1f1405bdb8b860d0d63e2c7adfbbd51ca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Feb 2024 03:14:08 +0100 Subject: Introduce `Kind` in `core::clipboard` --- runtime/src/clipboard.rs | 49 +++++++++++++++++++++++++++---------------- runtime/src/command/action.rs | 9 -------- 2 files changed, 31 insertions(+), 27 deletions(-) (limited to 'runtime') diff --git a/runtime/src/clipboard.rs b/runtime/src/clipboard.rs index 49044a0f..dd47c47d 100644 --- a/runtime/src/clipboard.rs +++ b/runtime/src/clipboard.rs @@ -1,5 +1,6 @@ //! Access the clipboard. use crate::command::{self, Command}; +use crate::core::clipboard::Kind; use crate::futures::MaybeSend; use std::fmt; @@ -9,10 +10,10 @@ use std::fmt; /// [`Command`]: crate::Command pub enum Action { /// Read the clipboard and produce `T` with the result. - Read(Box) -> T>), + Read(Box) -> T>, Kind), /// Write the given contents to the clipboard. - Write(String), + Write(String, Kind), } impl Action { @@ -25,8 +26,10 @@ impl Action { T: 'static, { match self { - Self::Read(o) => Action::Read(Box::new(move |s| f(o(s)))), - Self::Write(content) => Action::Write(content), + Self::Read(o, target) => { + Action::Read(Box::new(move |s| f(o(s))), target) + } + Self::Write(content, target) => Action::Write(content, target), } } } @@ -34,8 +37,8 @@ impl Action { impl fmt::Debug for Action { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::Read(_) => write!(f, "Action::Read"), - Self::Write(_) => write!(f, "Action::Write"), + Self::Read(_, target) => write!(f, "Action::Read{target:?}"), + Self::Write(_, target) => write!(f, "Action::Write({target:?})"), } } } @@ -44,24 +47,34 @@ impl fmt::Debug for Action { pub fn read( f: impl Fn(Option) -> Message + 'static, ) -> Command { - Command::single(command::Action::Clipboard(Action::Read(Box::new(f)))) + Command::single(command::Action::Clipboard(Action::Read( + Box::new(f), + Kind::Standard, + ))) } -/// Write the given contents to the clipboard. -pub fn write(contents: String) -> Command { - Command::single(command::Action::Clipboard(Action::Write(contents))) -} - -/// Read the current contents of primary. +/// Read the current contents of the primary clipboard. pub fn read_primary( f: impl Fn(Option) -> Message + 'static, ) -> Command { - Command::single(command::Action::ClipboardPrimary(Action::Read(Box::new( - f, - )))) + Command::single(command::Action::Clipboard(Action::Read( + Box::new(f), + Kind::Primary, + ))) +} + +/// Write the given contents to the clipboard. +pub fn write(contents: String) -> Command { + Command::single(command::Action::Clipboard(Action::Write( + contents, + Kind::Standard, + ))) } -/// Write the given contents to primary. +/// Write the given contents to the primary clipboard. pub fn write_primary(contents: String) -> Command { - Command::single(command::Action::ClipboardPrimary(Action::Write(contents))) + Command::single(command::Action::Clipboard(Action::Write( + contents, + Kind::Primary, + ))) } diff --git a/runtime/src/command/action.rs b/runtime/src/command/action.rs index f04c642c..c9ffe801 100644 --- a/runtime/src/command/action.rs +++ b/runtime/src/command/action.rs @@ -26,9 +26,6 @@ pub enum Action { /// Run a clipboard action. Clipboard(clipboard::Action), - /// Run a clipboard action on primary. - ClipboardPrimary(clipboard::Action), - /// Run a window action. Window(window::Action), @@ -69,9 +66,6 @@ impl Action { Self::Future(future) => Action::Future(Box::pin(future.map(f))), Self::Stream(stream) => Action::Stream(Box::pin(stream.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::System(system) => Action::System(system.map(f)), Self::Widget(operation) => { @@ -94,9 +88,6 @@ impl fmt::Debug for Action { Self::Clipboard(action) => { write!(f, "Action::Clipboard({action:?})") } - Self::ClipboardPrimary(action) => { - write!(f, "Action::ClipboardPrimary({action:?})") - } Self::Window(action) => { write!(f, "Action::Window({action:?})") } -- cgit