feat: add methods for loading raw data
This commit is contained in:
parent
5dd795d463
commit
71df657777
9 changed files with 153 additions and 63 deletions
|
|
@ -14,6 +14,7 @@ categories = ["gui"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
raw-window-handle = { version = "0.6", features = ["std"] }
|
raw-window-handle = { version = "0.6", features = ["std"] }
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
|
mime = { path = "./mime" }
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
clipboard-win = { version = "5.0", features = ["std"] }
|
clipboard-win = { version = "5.0", features = ["std"] }
|
||||||
|
|
@ -32,6 +33,7 @@ winit = "0.29"
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"macos",
|
"macos",
|
||||||
|
"mime",
|
||||||
"wayland",
|
"wayland",
|
||||||
"x11",
|
"x11",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
11
mime/Cargo.toml
Normal file
11
mime/Cargo.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
name = "mime"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten", target_os="ios", target_os="redox"))))'.dependencies]
|
||||||
|
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-mime-types" }
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
pub mod platform;
|
||||||
|
|
||||||
// need a type that can implement traits for storing custom data
|
// need a type that can implement traits for storing custom data
|
||||||
|
|
||||||
use std::{borrow::Cow, error, fmt};
|
use std::{borrow::Cow, error, fmt};
|
||||||
47
mime/src/platform/linux.rs
Normal file
47
mime/src/platform/linux.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
use smithay_clipboard::mime::{AllowedMimeTypes, AsMimeTypes, MimeType};
|
||||||
|
|
||||||
|
use crate::{ClipboardLoadData, ClipboardStoreData};
|
||||||
|
|
||||||
|
impl<T: crate::AsMimeTypes> AsMimeTypes for ClipboardStoreData<T> {
|
||||||
|
fn available(&self) -> std::borrow::Cow<'static, [MimeType]> {
|
||||||
|
self.data
|
||||||
|
.available()
|
||||||
|
.into_iter()
|
||||||
|
.map(|m| MimeType::Other(m.clone().into()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_bytes(
|
||||||
|
&self,
|
||||||
|
mime_type: &MimeType,
|
||||||
|
) -> Option<std::borrow::Cow<'static, [u8]>> {
|
||||||
|
self.data.as_bytes(mime_type.as_ref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: crate::AllowedMimeTypes> AllowedMimeTypes for ClipboardLoadData<T> {
|
||||||
|
// TODO select text variants if string matches...
|
||||||
|
fn allowed() -> std::borrow::Cow<'static, [MimeType]> {
|
||||||
|
T::allowed()
|
||||||
|
.into_iter()
|
||||||
|
.map(|s| MimeType::Other(s.clone().into()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> TryFrom<(Vec<u8>, MimeType)> for ClipboardLoadData<T>
|
||||||
|
where
|
||||||
|
T: for<'b> TryFrom<(Vec<u8>, String)>,
|
||||||
|
T: 'static,
|
||||||
|
{
|
||||||
|
type Error = crate::Error;
|
||||||
|
|
||||||
|
fn try_from(
|
||||||
|
(value, mime): (Vec<u8>, MimeType),
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
|
let mime = mime.to_string();
|
||||||
|
Ok(ClipboardLoadData(
|
||||||
|
T::try_from((value, mime)).map_err(|_| crate::Error)?,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
11
mime/src/platform/mod.rs
Normal file
11
mime/src/platform/mod.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#[cfg(all(
|
||||||
|
unix,
|
||||||
|
not(any(
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "ios",
|
||||||
|
target_os = "android",
|
||||||
|
target_os = "emscripten",
|
||||||
|
target_os = "redox"
|
||||||
|
))
|
||||||
|
))]
|
||||||
|
pub mod linux;
|
||||||
26
src/lib.rs
26
src/lib.rs
|
|
@ -1,4 +1,4 @@
|
||||||
pub mod mime;
|
pub use mime;
|
||||||
|
|
||||||
#[cfg(all(
|
#[cfg(all(
|
||||||
unix,
|
unix,
|
||||||
|
|
@ -48,7 +48,7 @@ mod platform;
|
||||||
#[path = "platform/dummy.rs"]
|
#[path = "platform/dummy.rs"]
|
||||||
mod platform;
|
mod platform;
|
||||||
|
|
||||||
use mime::{ClipboardLoadData, ClipboardStoreData};
|
use mime::ClipboardStoreData;
|
||||||
use raw_window_handle::HasDisplayHandle;
|
use raw_window_handle::HasDisplayHandle;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
|
|
@ -108,7 +108,7 @@ pub trait ClipboardProvider {
|
||||||
|
|
||||||
fn read_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
fn read_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
||||||
where
|
where
|
||||||
ClipboardLoadData<T>: platform::InnerAllowedMimeTypes,
|
T: mime::AllowedMimeTypes,
|
||||||
{
|
{
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
@ -118,24 +118,38 @@ pub trait ClipboardProvider {
|
||||||
_contents: ClipboardStoreData<T>,
|
_contents: ClipboardStoreData<T>,
|
||||||
) -> Option<Result<(), Box<dyn Error>>>
|
) -> Option<Result<(), Box<dyn Error>>>
|
||||||
where
|
where
|
||||||
ClipboardStoreData<T>: platform::InnerAsMimeTypes,
|
T: mime::AsMimeTypes,
|
||||||
{
|
{
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_primary_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
fn read_primary_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
||||||
where
|
where
|
||||||
ClipboardLoadData<T>: platform::InnerAllowedMimeTypes,
|
T: mime::AllowedMimeTypes,
|
||||||
{
|
{
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_primary_raw(
|
||||||
|
&self,
|
||||||
|
_allowed: Vec<String>,
|
||||||
|
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_raw(
|
||||||
|
&self,
|
||||||
|
_allowed: Vec<String>,
|
||||||
|
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
fn write_primary_data<T: Send + Sync + 'static>(
|
fn write_primary_data<T: Send + Sync + 'static>(
|
||||||
&mut self,
|
&mut self,
|
||||||
_contents: ClipboardStoreData<T>,
|
_contents: ClipboardStoreData<T>,
|
||||||
) -> Option<Result<(), Box<dyn Error>>>
|
) -> Option<Result<(), Box<dyn Error>>>
|
||||||
where
|
where
|
||||||
ClipboardStoreData<T>: platform::InnerAsMimeTypes,
|
T: mime::AsMimeTypes,
|
||||||
{
|
{
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,9 @@ use crate::{
|
||||||
|
|
||||||
use raw_window_handle::{HasDisplayHandle, RawDisplayHandle};
|
use raw_window_handle::{HasDisplayHandle, RawDisplayHandle};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use wayland::MimeType;
|
|
||||||
|
|
||||||
pub use clipboard_wayland as wayland;
|
pub use clipboard_wayland as wayland;
|
||||||
pub use clipboard_x11 as x11;
|
pub use clipboard_x11 as x11;
|
||||||
pub use wayland::{
|
|
||||||
AllowedMimeTypes as InnerAllowedMimeTypes, AsMimeTypes as InnerAsMimeTypes,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub enum Clipboard {
|
pub enum Clipboard {
|
||||||
Wayland(wayland::Clipboard),
|
Wayland(wayland::Clipboard),
|
||||||
|
|
@ -54,7 +50,7 @@ impl ClipboardProvider for Clipboard {
|
||||||
|
|
||||||
fn read_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
fn read_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
||||||
where
|
where
|
||||||
ClipboardLoadData<T>: InnerAllowedMimeTypes,
|
T: mime::AllowedMimeTypes,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Clipboard::Wayland(c) => {
|
Clipboard::Wayland(c) => {
|
||||||
|
|
@ -70,7 +66,7 @@ impl ClipboardProvider for Clipboard {
|
||||||
contents: ClipboardStoreData<T>,
|
contents: ClipboardStoreData<T>,
|
||||||
) -> Option<Result<(), Box<dyn Error>>>
|
) -> Option<Result<(), Box<dyn Error>>>
|
||||||
where
|
where
|
||||||
ClipboardStoreData<T>: InnerAsMimeTypes,
|
T: mime::AsMimeTypes,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Clipboard::Wayland(c) => {
|
Clipboard::Wayland(c) => {
|
||||||
|
|
@ -82,7 +78,7 @@ impl ClipboardProvider for Clipboard {
|
||||||
|
|
||||||
fn read_primary_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
fn read_primary_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
||||||
where
|
where
|
||||||
ClipboardLoadData<T>: InnerAllowedMimeTypes,
|
T: mime::AllowedMimeTypes,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Clipboard::Wayland(c) => {
|
Clipboard::Wayland(c) => {
|
||||||
|
|
@ -93,12 +89,32 @@ impl ClipboardProvider for Clipboard {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_primary_raw(
|
||||||
|
&self,
|
||||||
|
allowed: Vec<String>,
|
||||||
|
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
|
||||||
|
match self {
|
||||||
|
Clipboard::Wayland(c) => Some(c.read_primary_raw(allowed)),
|
||||||
|
Clipboard::X11(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_raw(
|
||||||
|
&self,
|
||||||
|
allowed: Vec<String>,
|
||||||
|
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
|
||||||
|
match self {
|
||||||
|
Clipboard::Wayland(c) => Some(c.read_raw(allowed)),
|
||||||
|
Clipboard::X11(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn write_primary_data<T: Send + Sync + 'static>(
|
fn write_primary_data<T: Send + Sync + 'static>(
|
||||||
&mut self,
|
&mut self,
|
||||||
contents: ClipboardStoreData<T>,
|
contents: ClipboardStoreData<T>,
|
||||||
) -> Option<Result<(), Box<dyn Error>>>
|
) -> Option<Result<(), Box<dyn Error>>>
|
||||||
where
|
where
|
||||||
ClipboardStoreData<T>: InnerAsMimeTypes,
|
T: mime::AsMimeTypes,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Clipboard::Wayland(c) => {
|
Clipboard::Wayland(c) => {
|
||||||
|
|
@ -121,51 +137,3 @@ pub unsafe fn connect<W: HasDisplayHandle>(
|
||||||
|
|
||||||
Ok(clipboard)
|
Ok(clipboard)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: crate::mime::AsMimeTypes> InnerAsMimeTypes for ClipboardLoadData<T> {
|
|
||||||
fn available(&self) -> std::borrow::Cow<'static, [MimeType]> {
|
|
||||||
self.0
|
|
||||||
.available()
|
|
||||||
.into_iter()
|
|
||||||
.map(|m| MimeType::Other(m.clone().into()))
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_bytes(
|
|
||||||
&self,
|
|
||||||
mime_type: &MimeType,
|
|
||||||
) -> Option<std::borrow::Cow<'static, [u8]>> {
|
|
||||||
self.0.as_bytes(mime_type.as_ref())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: crate::mime::AllowedMimeTypes> InnerAllowedMimeTypes
|
|
||||||
for ClipboardLoadData<T>
|
|
||||||
where
|
|
||||||
ClipboardLoadData<T>: TryFrom<(Vec<u8>, MimeType)>,
|
|
||||||
{
|
|
||||||
// TODO select text variants if string matches...
|
|
||||||
fn allowed() -> std::borrow::Cow<'static, [wayland::MimeType]> {
|
|
||||||
T::allowed()
|
|
||||||
.into_iter()
|
|
||||||
.map(|s| MimeType::Other(s.clone().into()))
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> TryFrom<(Vec<u8>, MimeType)> for ClipboardLoadData<T>
|
|
||||||
where
|
|
||||||
T: for<'b> TryFrom<(Vec<u8>, String)>,
|
|
||||||
T: 'static,
|
|
||||||
{
|
|
||||||
type Error = crate::mime::Error;
|
|
||||||
|
|
||||||
fn try_from(
|
|
||||||
(value, mime): (Vec<u8>, MimeType),
|
|
||||||
) -> Result<Self, Self::Error> {
|
|
||||||
let mime = mime.to_string();
|
|
||||||
Ok(ClipboardLoadData(
|
|
||||||
T::try_from((value, mime)).map_err(|_| crate::mime::Error)?,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -11,4 +11,4 @@ keywords = ["clipboard", "wayland"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-mime-types" }
|
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-mime-types" }
|
||||||
# smithay-clipboard = { path = "../../smithay-clipboard" }
|
mime = { path = "../mime" }
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
error::Error,
|
error::Error,
|
||||||
ffi::c_void,
|
ffi::c_void,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
|
|
@ -85,4 +86,38 @@ impl Clipboard {
|
||||||
) -> Result<T, Box<dyn Error>> {
|
) -> Result<T, Box<dyn Error>> {
|
||||||
Ok(self.context.lock().unwrap().load_primary()?)
|
Ok(self.context.lock().unwrap().load_primary()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_primary_raw(
|
||||||
|
&self,
|
||||||
|
allowed: Vec<String>,
|
||||||
|
) -> Result<(Vec<u8>, String), Box<dyn Error>> {
|
||||||
|
Ok(self
|
||||||
|
.context
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.load_primary_raw(
|
||||||
|
allowed
|
||||||
|
.into_iter()
|
||||||
|
.map(|s| MimeType::from(Cow::Owned(s)))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
.map(|(d, m)| (d, m.to_string()))?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_raw(
|
||||||
|
&self,
|
||||||
|
allowed: Vec<String>,
|
||||||
|
) -> Result<(Vec<u8>, String), Box<dyn Error>> {
|
||||||
|
Ok(self
|
||||||
|
.context
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.load_raw(
|
||||||
|
allowed
|
||||||
|
.into_iter()
|
||||||
|
.map(|s| MimeType::from(Cow::Owned(s)))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
.map(|(d, m)| (d, m.to_string()))?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue