Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw window handle 0.6 #24

Merged
merged 6 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ keywords = ["clipboard", "window", "ui", "gui", "raw-window-handle"]
categories = ["gui"]

[dependencies]
raw-window-handle = "0.5"
raw-window-handle = { version = "0.6", features = ["std"] }
thiserror = "1.0"

[target.'cfg(windows)'.dependencies]
clipboard-win = { version = "4.0", features = ["std"] }
clipboard-win = { version = "5.0", features = ["std"] }

[target.'cfg(target_os = "macos")'.dependencies]
clipboard_macos = { version = "0.1", path = "./macos" }
Expand All @@ -27,7 +27,7 @@ clipboard_wayland = { version = "0.2", path = "./wayland" }

[dev-dependencies]
rand = "0.8"
winit = "0.28"
winit = "0.29"

[workspace]
members = [
Expand Down
28 changes: 15 additions & 13 deletions examples/big_file.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use rand::distributions::{Alphanumeric, Distribution};
use window_clipboard::Clipboard;
use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
error::EventLoopError,
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
keyboard::Key,
window::WindowBuilder,
};

fn main() {
fn main() -> Result<(), EventLoopError> {
let mut rng = rand::thread_rng();

let data: String = Alphanumeric
Expand All @@ -15,40 +17,40 @@ fn main() {
.map(char::from)
.collect();

let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();

let window = WindowBuilder::new()
.with_title("Press G to start the test!")
.build(&event_loop)
.unwrap();

let mut clipboard =
Clipboard::connect(&window).expect("Connect to clipboard");
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");

clipboard.write(data.clone()).unwrap();

event_loop.run(move |event, _, control_flow| match event {
event_loop.run(move |event, elwt| match event {
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::G),
event:
KeyEvent {
logical_key: Key::Character(c),
state: ElementState::Released,
..
},
..
},
..
} => {
} if c == "G" => {
let new_data = clipboard.read().expect("Read data");
assert_eq!(data, new_data, "Data is equal");
println!("Data copied successfully!");
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
_ => *control_flow = ControlFlow::Wait,
});
} if window_id == window.id() => elwt.exit(),
_ => {}
})
}
20 changes: 11 additions & 9 deletions examples/read.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
use window_clipboard::Clipboard;
use winit::{
error::EventLoopError,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

fn main() {
let event_loop = EventLoop::new();
fn main() -> Result<(), EventLoopError> {
let event_loop = EventLoop::new().unwrap();

let window = WindowBuilder::new()
.with_title("A fantastic window!")
.build(&event_loop)
.unwrap();

let clipboard = Clipboard::connect(&window).expect("Connect to clipboard");
let clipboard =
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");

event_loop.run(move |event, _, control_flow| match event {
Event::MainEventsCleared => {
event_loop.run(move |event, elwt| match event {
Event::AboutToWait => {
println!("{:?}", clipboard.read());
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
_ => *control_flow = ControlFlow::Wait,
});
} if window_id == window.id() => elwt.exit(),
_ => {}
})
}
18 changes: 9 additions & 9 deletions examples/write.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
use window_clipboard::Clipboard;
use winit::{
error::EventLoopError,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

fn main() {
let event_loop = EventLoop::new();
fn main() -> Result<(), EventLoopError> {
let event_loop = EventLoop::new().unwrap();

let window = WindowBuilder::new()
.with_title("A fantastic window!")
.build(&event_loop)
.unwrap();

let mut clipboard =
Clipboard::connect(&window).expect("Connect to clipboard");
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");

clipboard
.write(String::from("Hello, world!"))
.expect("Write to clipboard");

event_loop.run(move |event, _, control_flow| match event {
Event::MainEventsCleared => {}
event_loop.run(move |event, elwt| match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
_ => *control_flow = ControlFlow::Wait,
});
} if window_id == window.id() => elwt.exit(),
_ => {}
})
}
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ mod platform;
#[path = "platform/dummy.rs"]
mod platform;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;
use std::error::Error;

pub struct Clipboard {
raw: Box<dyn ClipboardProvider>,
}

impl Clipboard {
pub fn connect<W: HasRawDisplayHandle>(
/// Safety: the display handle must be valid for the lifetime of `Clipboard`
pub unsafe fn connect<W: HasDisplayHandle>(
window: &W,
) -> Result<Self, Box<dyn Error>> {
let raw = platform::connect(window)?;
Expand Down
4 changes: 2 additions & 2 deletions src/platform/android.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::ClipboardProvider;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;
use std::error::Error;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
Ok(Box::new(Clipboard::new()?))
Expand Down
4 changes: 2 additions & 2 deletions src/platform/dummy.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::ClipboardProvider;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;

struct Dummy;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn std::error::Error>> {
Ok(Box::new(Dummy))
Expand Down
4 changes: 2 additions & 2 deletions src/platform/ios.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::ClipboardProvider;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;
use std::error::Error;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
Ok(Box::new(Clipboard::new()?))
Expand Down
12 changes: 4 additions & 8 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use crate::ClipboardProvider;

use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle};
use raw_window_handle::{HasDisplayHandle, RawDisplayHandle};
use std::error::Error;

pub use clipboard_wayland as wayland;
pub use clipboard_x11 as x11;

pub fn connect<W: HasRawDisplayHandle>(
pub unsafe fn connect<W: HasDisplayHandle>(
window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
let clipboard = match window.raw_display_handle() {
let clipboard = match window.display_handle()?.as_raw() {
RawDisplayHandle::Wayland(handle) => {
assert!(!handle.display.is_null());

Box::new(unsafe {
wayland::Clipboard::connect(handle.display as *mut _)
}) as _
Box::new(wayland::Clipboard::connect(handle.display.as_ptr())) as _
}
_ => Box::new(x11::Clipboard::connect()?) as _,
};
Expand Down
4 changes: 2 additions & 2 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::ClipboardProvider;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;
use std::error::Error;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
Ok(Box::new(clipboard_macos::Clipboard::new()?))
Expand Down
4 changes: 2 additions & 2 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::ClipboardProvider;

use clipboard_win::{get_clipboard_string, set_clipboard_string};
use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;

use std::error::Error;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
Ok(Box::new(Clipboard))
Expand Down
2 changes: 1 addition & 1 deletion wayland/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ documentation = "https://docs.rs/clipboard_wayland"
keywords = ["clipboard", "wayland"]

[dependencies]
smithay-clipboard = "0.6"
smithay-clipboard = "0.7"
2 changes: 1 addition & 1 deletion x11/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ documentation = "https://docs.rs/clipboard_x11"
keywords = ["clipboard", "x11"]

[dependencies]
x11rb = "0.11"
x11rb = "0.13"
thiserror = "1.0"