refactor: add type that hides the platform clipboard
This commit is contained in:
parent
886f430414
commit
5dd795d463
4 changed files with 15 additions and 13 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
use rand::distributions::{Alphanumeric, Distribution};
|
use rand::distributions::{Alphanumeric, Distribution};
|
||||||
use window_clipboard::Clipboard;
|
use window_clipboard::PlatformClipboard;
|
||||||
use winit::{
|
use winit::{
|
||||||
error::EventLoopError,
|
error::EventLoopError,
|
||||||
event::{ElementState, Event, KeyEvent, WindowEvent},
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
|
@ -24,8 +24,8 @@ fn main() -> Result<(), EventLoopError> {
|
||||||
.build(&event_loop)
|
.build(&event_loop)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut clipboard =
|
let mut clipboard = unsafe { PlatformClipboard::connect(&window) }
|
||||||
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");
|
.expect("Connect to clipboard");
|
||||||
|
|
||||||
clipboard.write(data.clone()).unwrap();
|
clipboard.write(data.clone()).unwrap();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use window_clipboard::Clipboard;
|
use window_clipboard::PlatformClipboard;
|
||||||
use winit::{
|
use winit::{
|
||||||
error::EventLoopError,
|
error::EventLoopError,
|
||||||
event::{Event, WindowEvent},
|
event::{Event, WindowEvent},
|
||||||
|
|
@ -14,8 +14,8 @@ fn main() -> Result<(), EventLoopError> {
|
||||||
.build(&event_loop)
|
.build(&event_loop)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let clipboard =
|
let clipboard = unsafe { PlatformClipboard::connect(&window) }
|
||||||
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");
|
.expect("Connect to clipboard");
|
||||||
|
|
||||||
event_loop.run(move |event, elwt| match event {
|
event_loop.run(move |event, elwt| match event {
|
||||||
Event::AboutToWait => {
|
Event::AboutToWait => {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use window_clipboard::Clipboard;
|
use window_clipboard::PlatformClipboard;
|
||||||
use winit::{
|
use winit::{
|
||||||
error::EventLoopError,
|
error::EventLoopError,
|
||||||
event::{Event, WindowEvent},
|
event::{Event, WindowEvent},
|
||||||
|
|
@ -14,8 +14,8 @@ fn main() -> Result<(), EventLoopError> {
|
||||||
.build(&event_loop)
|
.build(&event_loop)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut clipboard =
|
let mut clipboard = unsafe { PlatformClipboard::connect(&window) }
|
||||||
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");
|
.expect("Connect to clipboard");
|
||||||
|
|
||||||
clipboard
|
clipboard
|
||||||
.write(String::from("Hello, world!"))
|
.write(String::from("Hello, world!"))
|
||||||
|
|
|
||||||
10
src/lib.rs
10
src/lib.rs
|
|
@ -52,16 +52,18 @@ use mime::{ClipboardLoadData, ClipboardStoreData};
|
||||||
use raw_window_handle::HasDisplayHandle;
|
use raw_window_handle::HasDisplayHandle;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
pub struct Clipboard<C> {
|
pub type Clipboard = PlatformClipboard<platform::Clipboard>;
|
||||||
|
|
||||||
|
pub struct PlatformClipboard<C> {
|
||||||
raw: C,
|
raw: C,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clipboard<platform::Clipboard> {
|
impl PlatformClipboard<platform::Clipboard> {
|
||||||
/// Safety: the display handle must be valid for the lifetime of `Clipboard`
|
/// Safety: the display handle must be valid for the lifetime of `Clipboard`
|
||||||
pub unsafe fn connect<W: HasDisplayHandle>(
|
pub unsafe fn connect<W: HasDisplayHandle>(
|
||||||
window: &W,
|
window: &W,
|
||||||
) -> Result<Self, Box<dyn Error>> {
|
) -> Result<Self, Box<dyn Error>> {
|
||||||
Ok(Clipboard {
|
Ok(PlatformClipboard {
|
||||||
raw: platform::connect(window)?,
|
raw: platform::connect(window)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +77,7 @@ impl Clipboard<platform::Clipboard> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C: ClipboardProvider> Clipboard<C> {
|
impl<C: ClipboardProvider> PlatformClipboard<C> {
|
||||||
pub fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
|
pub fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
|
||||||
self.raw.read_primary()
|
self.raw.read_primary()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue