forked from neovide/neovide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
25 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters