Start with an initial draft
This commit is contained in:
commit
6f71824857
7 changed files with 160 additions and 0 deletions
16
.github/workflows/test.yml
vendored
Normal file
16
.github/workflows/test.yml
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
name: Test
|
||||||
|
on: [push, pull_request]
|
||||||
|
jobs:
|
||||||
|
all:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||||
|
rust: [stable, beta]
|
||||||
|
steps:
|
||||||
|
- uses: hecrj/setup-rust-action@v1
|
||||||
|
with:
|
||||||
|
rust-version: ${{ matrix.rust }}
|
||||||
|
- uses: actions/checkout@master
|
||||||
|
- name: Run tests
|
||||||
|
run: cargo test --verbose
|
||||||
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
/target
|
||||||
|
**/*.rs.bk
|
||||||
|
Cargo.lock
|
||||||
22
Cargo.toml
Normal file
22
Cargo.toml
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
[package]
|
||||||
|
name = "window_clipboard"
|
||||||
|
version = "0.1.0-alpha"
|
||||||
|
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
description = "A library to obtain clipboard access from a `raw-window-handle`"
|
||||||
|
license = "MIT"
|
||||||
|
repository = "https://github.com/hecrj/iced"
|
||||||
|
documentation = "https://docs.rs/window_clipboard"
|
||||||
|
readme = "README.md"
|
||||||
|
keywords = ["clipboard", "window", "ui", "gui", "raw-window-handle"]
|
||||||
|
categories = ["gui"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
raw-window-handle = "0.3"
|
||||||
|
clipboard = "0.5"
|
||||||
|
|
||||||
|
[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dependencies]
|
||||||
|
smithay-clipboard = "0.3"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
winit = "=0.20.0-alpha5"
|
||||||
3
README.md
Normal file
3
README.md
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
# window_clipboard
|
||||||
|
|
||||||
|
A library to obtain clipboard access from a `raw-window-handle`.
|
||||||
28
examples/basic.rs
Normal file
28
examples/basic.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
use window_clipboard::Clipboard;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::{ControlFlow, EventLoop},
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let event_loop = EventLoop::new();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let clipboard = Clipboard::new(&window).expect("Create clipboard");
|
||||||
|
|
||||||
|
event_loop.run(move |event, _, control_flow| match event {
|
||||||
|
Event::EventsCleared => {
|
||||||
|
println!("{:?}", clipboard.read());
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
window_id,
|
||||||
|
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
|
||||||
|
_ => *control_flow = ControlFlow::Wait,
|
||||||
|
});
|
||||||
|
}
|
||||||
3
rustfmt.toml
Normal file
3
rustfmt.toml
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
max_width=80
|
||||||
|
wrap_comments=true
|
||||||
|
merge_imports=true
|
||||||
85
src/lib.rs
Normal file
85
src/lib.rs
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
pub struct Clipboard {
|
||||||
|
raw: Raw,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Raw {
|
||||||
|
#[cfg(all(
|
||||||
|
unix,
|
||||||
|
not(any(
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "android",
|
||||||
|
target_os = "emscripten"
|
||||||
|
))
|
||||||
|
))]
|
||||||
|
Wayland(RefCell<smithay_clipboard::WaylandClipboard>),
|
||||||
|
|
||||||
|
NotWayland(RefCell<clipboard::ClipboardContext>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clipboard {
|
||||||
|
pub fn new<W: HasRawWindowHandle>(
|
||||||
|
window: &W,
|
||||||
|
) -> Result<Self, Box<dyn Error>> {
|
||||||
|
let raw = match window.raw_window_handle() {
|
||||||
|
#[cfg(all(
|
||||||
|
unix,
|
||||||
|
not(any(
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "android",
|
||||||
|
target_os = "emscripten"
|
||||||
|
))
|
||||||
|
))]
|
||||||
|
RawWindowHandle::Wayland(handle) => {
|
||||||
|
assert!(!handle.display.is_null());
|
||||||
|
|
||||||
|
Raw::Wayland(RefCell::new(unsafe {
|
||||||
|
smithay_clipboard::WaylandClipboard::new_from_external(
|
||||||
|
handle.display as *mut _,
|
||||||
|
)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
use clipboard::ClipboardProvider as _;
|
||||||
|
|
||||||
|
Raw::NotWayland(RefCell::new(
|
||||||
|
clipboard::ClipboardContext::new()?
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Clipboard { raw })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(&self) -> Result<String, Box<dyn Error>> {
|
||||||
|
// TODO: Think about use of `RefCell`
|
||||||
|
// Maybe we should make `read` mutable (?)
|
||||||
|
use clipboard::ClipboardProvider as _;
|
||||||
|
|
||||||
|
match &self.raw {
|
||||||
|
Raw::Wayland(clipboard) => Ok(clipboard.borrow_mut().load(None)),
|
||||||
|
Raw::NotWayland(clipboard) => clipboard.borrow_mut().get_contents(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(
|
||||||
|
&mut self,
|
||||||
|
contents: impl Into<String>,
|
||||||
|
) -> Result<(), Box<dyn Error>> {
|
||||||
|
use clipboard::ClipboardProvider as _;
|
||||||
|
|
||||||
|
match &self.raw {
|
||||||
|
Raw::Wayland(clipboard) => {
|
||||||
|
clipboard.borrow_mut().store(None, contents);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Raw::NotWayland(clipboard) => {
|
||||||
|
clipboard.borrow_mut().set_contents(contents.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue