Skip to content

Commit

Permalink
Use wayland clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
cshuaimin committed Nov 14, 2023
1 parent f99650f commit 152e5be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
use copypasta::{ClipboardContext, ClipboardProvider};
use std::error::Error;
use std::sync::OnceLock;

#[cfg(target_os = "linux")]
use copypasta::wayland_clipboard;
use copypasta::{ClipboardContext, ClipboardProvider};
use parking_lot::Mutex;
use raw_window_handle::HasRawDisplayHandle;
#[cfg(target_os = "linux")]
use raw_window_handle::{RawDisplayHandle, WaylandDisplayHandle};
use winit::event_loop::EventLoop;

use std::error::Error;
use crate::window::UserEvent;

type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync + 'static>>;

lazy_static! {
static ref CLIPBOARD_CONTEXT: Mutex<ClipboardContext> =
Mutex::new(ClipboardContext::new().unwrap());
static CLIPBOARD: OnceLock<Mutex<Box<dyn ClipboardProvider>>> = OnceLock::new();

pub fn init(event_loop: &EventLoop<UserEvent>) {
CLIPBOARD
.set(Mutex::new(match event_loop.raw_display_handle() {
#[cfg(target_os = "linux")]
RawDisplayHandle::Wayland(WaylandDisplayHandle { display, .. }) => unsafe {
Box::new(wayland_clipboard::create_clipboards_from_external(display).1)
},
_ => Box::new(ClipboardContext::new().unwrap()),
}))
.ok();
}

pub fn get_contents() -> Result<String> {
CLIPBOARD_CONTEXT.lock().get_contents()
CLIPBOARD.get().unwrap().lock().get_contents()
}

pub fn set_contents(lines: String) -> Result<()> {
CLIPBOARD_CONTEXT.lock().set_contents(lines)
CLIPBOARD.get().unwrap().lock().set_contents(lines)
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn main() -> NeovideExitCode {
Err(err) => handle_startup_errors(err, event_loop).into(),
Ok((window_size, _runtime)) => {
start_editor();
clipboard::init(&event_loop);
let window = create_window(&event_loop, &window_size);
main_loop(window, window_size, event_loop).into()
}
Expand Down

0 comments on commit 152e5be

Please sign in to comment.