Skip to content

Commit

Permalink
feat: add actions and commands for new clipboard methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Mar 14, 2024
1 parent 4f16c84 commit 2c6953d
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 8 deletions.
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ sctk.optional = true
thiserror.workspace = true
iced_accessibility.workspace = true
iced_accessibility.optional = true
window_clipboard.workspace = true
81 changes: 81 additions & 0 deletions runtime/src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Access the clipboard.
use window_clipboard::mime::{AllowedMimeTypes, AsMimeTypes};

use crate::command::{self, Command};
use crate::futures::MaybeSend;

Expand All @@ -13,6 +15,24 @@ pub enum Action<T> {

/// Write the given contents to the clipboard.
Write(String),

/// Write the given contents to the clipboard.
WriteData(Box<dyn AsMimeTypes + Send + Sync + 'static>),

/// Read the clipboard and produce `T` with the result.
ReadData(Vec<String>, Box<dyn Fn(Option<(Vec<u8>, String)>) -> T>),

Check warning on line 23 in runtime/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

very complex type used. Consider factoring parts into `type` definitions

Check warning on line 23 in runtime/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

very complex type used. Consider factoring parts into `type` definitions

/// Read the clipboard and produce `T` with the result.
ReadPrimary(Box<dyn Fn(Option<String>) -> T>),

/// Write the given contents to the clipboard.
WritePrimary(String),

/// Write the given contents to the clipboard.
WritePrimaryData(Box<dyn AsMimeTypes + Send + Sync + 'static>),

/// Read the clipboard and produce `T` with the result.
ReadPrimaryData(Vec<String>, Box<dyn Fn(Option<(Vec<u8>, String)>) -> T>),

Check warning on line 35 in runtime/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

very complex type used. Consider factoring parts into `type` definitions

Check warning on line 35 in runtime/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

very complex type used. Consider factoring parts into `type` definitions
}

impl<T> Action<T> {
Expand All @@ -27,6 +47,20 @@ impl<T> Action<T> {
match self {
Self::Read(o) => Action::Read(Box::new(move |s| f(o(s)))),
Self::Write(content) => Action::Write(content),
Self::WriteData(content) => Action::WriteData(content),
Self::ReadData(a, o) => {
Action::ReadData(a, Box::new(move |s| f(o(s))))
}
Self::ReadPrimary(o) => {
Action::ReadPrimary(Box::new(move |s| f(o(s))))
}
Self::WritePrimary(content) => Action::WritePrimary(content),
Self::WritePrimaryData(content) => {
Action::WritePrimaryData(content)
}
Self::ReadPrimaryData(a, o) => {
Action::ReadPrimaryData(a, Box::new(move |s| f(o(s))))
}
}
}
}
Expand All @@ -36,6 +70,12 @@ impl<T> fmt::Debug for Action<T> {
match self {
Self::Read(_) => write!(f, "Action::Read"),
Self::Write(_) => write!(f, "Action::Write"),
Self::WriteData(_) => write!(f, "Action::WriteData"),
Self::ReadData(_, _) => write!(f, "Action::ReadData"),
Self::ReadPrimary(_) => write!(f, "Action::ReadPrimary"),
Self::WritePrimary(_) => write!(f, "Action::WritePrimary"),
Self::WritePrimaryData(_) => write!(f, "Action::WritePrimaryData"),
Self::ReadPrimaryData(_, _) => write!(f, "Action::ReadPrimaryData"),
}
}
}
Expand All @@ -51,3 +91,44 @@ pub fn read<Message>(
pub fn write<Message>(contents: String) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Write(contents)))
}

/// Read the current contents of the clipboard.
pub fn read_data<T: AllowedMimeTypes + Send + Sync + 'static, Message>(
f: impl Fn(Option<T>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::ReadData(
T::allowed().into(),
Box::new(move |d| f(d.and_then(|d| T::try_from(d).ok()))),
)))
}

/// Write the given contents to the clipboard.
pub fn write_data<Message>(
contents: impl AsMimeTypes + std::marker::Sync + std::marker::Send + 'static,
) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::WriteData(Box::new(
contents,
))))
}

/// Read the current contents of the clipboard.
pub fn read_primary_data<
T: AllowedMimeTypes + Send + Sync + 'static,
Message,
>(
f: impl Fn(Option<T>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::ReadPrimaryData(
T::allowed().into(),
Box::new(move |d| f(d.and_then(|d| T::try_from(d).ok()))),
)))
}

/// Write the given contents to the clipboard.
pub fn write_primary_data<Message>(
contents: impl AsMimeTypes + std::marker::Sync + std::marker::Send + 'static,
) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::WritePrimaryData(
Box::new(contents),
)))
}
37 changes: 29 additions & 8 deletions sctk/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ use std::{
};
use wayland_backend::client::ObjectId;
use wayland_protocols::wp::viewporter::client::wp_viewport::WpViewport;
use window_clipboard::mime::ClipboardStoreData;

use crate::subsurface_widget::{SubsurfaceInstance, SubsurfaceState};

Expand Down Expand Up @@ -2002,17 +2003,37 @@ where
}
command::Action::Clipboard(action) => match action {
clipboard::Action::Read(s_to_msg) => {
if matches!(clipboard.state(), crate::clipboard::State::Connected(_)) {
let contents = clipboard.read();
let message = s_to_msg(contents);
proxy.send_event(Event::Message(message));
}
let contents = clipboard.read();
let message = s_to_msg(contents);
proxy.send_event(Event::Message(message));
}
clipboard::Action::Write(contents) => {
if matches!(clipboard.state(), crate::clipboard::State::Connected(_)) {
clipboard.write(contents)
}
clipboard.write(contents)
}
clipboard::Action::WriteData(contents) => {
clipboard.write_data(ClipboardStoreData(contents))
},
clipboard::Action::ReadData(allowed, to_msg) => {
let contents = clipboard.read_data(allowed);
let message = to_msg(contents);
proxy.send_event(Event::Message(message));
},
clipboard::Action::ReadPrimary(s_to_msg) => {
let contents = clipboard.read_primary();
let message = s_to_msg(contents);
proxy.send_event(Event::Message(message));
},
clipboard::Action::WritePrimary(content) => {
clipboard.write_primary(content)
},
clipboard::Action::WritePrimaryData(content) => {
clipboard.write_primary_data(ClipboardStoreData(content))
},
clipboard::Action::ReadPrimaryData(a, to_msg) => {
let contents = clipboard.read_primary_data(a);
let message = to_msg(contents);
proxy.send_event(Event::Message(message));
},
},
command::Action::Window(..) => {
unimplemented!("Use platform specific events instead")
Expand Down
26 changes: 26 additions & 0 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use iced_graphics::core::widget::operation::focusable::focus;
use iced_graphics::core::widget::operation::OperationWrapper;
use iced_graphics::core::widget::Operation;
use iced_runtime::futures::futures::FutureExt;
use iced_style::core::Clipboard as CoreClipboard;
pub use state::State;
use window_clipboard::mime::ClipboardStoreData;

use crate::conversion;
use crate::core;
Expand Down Expand Up @@ -908,6 +910,30 @@ pub fn run_command<A, C, E>(
clipboard::Action::Write(contents) => {
clipboard.write(contents);
}
clipboard::Action::WriteData(contents) => {
clipboard.write_data(ClipboardStoreData(contents))
}
clipboard::Action::ReadData(allowed, to_msg) => {
let contents = clipboard.read_data(allowed);
let message = to_msg(contents);
_ = proxy.send_event(UserEventWrapper::Message(message));
}
clipboard::Action::ReadPrimary(s_to_msg) => {
let contents = clipboard.read_primary();
let message = s_to_msg(contents);
_ = proxy.send_event(UserEventWrapper::Message(message));
}
clipboard::Action::WritePrimary(content) => {
clipboard.write_primary(content)
}
clipboard::Action::WritePrimaryData(content) => {
clipboard.write_primary_data(ClipboardStoreData(content))
}
clipboard::Action::ReadPrimaryData(a, to_msg) => {
let contents = clipboard.read_primary_data(a);
let message = to_msg(contents);
_ = proxy.send_event(UserEventWrapper::Message(message));
}
},
command::Action::Window(action) => match action {
window::Action::Close(_id) => {
Expand Down
26 changes: 26 additions & 0 deletions winit/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ use crate::runtime::user_interface::{self, UserInterface};
use crate::runtime::Debug;
use crate::style::application::StyleSheet;
use crate::{Clipboard, Error, Proxy, Settings};
use core::Clipboard as CoreClipboard;
use iced_runtime::futures::futures::FutureExt;
use iced_style::Theme;
pub use state::State;
use window_clipboard::mime::ClipboardStoreData;

use std::collections::HashMap;
use std::mem::ManuallyDrop;
Expand Down Expand Up @@ -1142,6 +1144,30 @@ fn run_command<A, C, E>(
clipboard::Action::Write(contents) => {
clipboard.write(contents);
}
clipboard::Action::WriteData(contents) => {
clipboard.write_data(ClipboardStoreData(contents))
}
clipboard::Action::ReadData(allowed, to_msg) => {
let contents = clipboard.read_data(allowed);
let message = to_msg(contents);
_ = proxy.send_event(UserEventWrapper::Message(message));
}
clipboard::Action::ReadPrimary(s_to_msg) => {
let contents = clipboard.read_primary();
let message = s_to_msg(contents);
_ = proxy.send_event(UserEventWrapper::Message(message));
}
clipboard::Action::WritePrimary(content) => {
clipboard.write_primary(content)
}
clipboard::Action::WritePrimaryData(content) => {
clipboard.write_primary_data(ClipboardStoreData(content))
}
clipboard::Action::ReadPrimaryData(a, to_msg) => {
let contents = clipboard.read_primary_data(a);
let message = to_msg(contents);
_ = proxy.send_event(UserEventWrapper::Message(message));
}
},
command::Action::Window(action) => match action {
window::Action::Spawn(id, settings) => {
Expand Down

0 comments on commit 2c6953d

Please sign in to comment.