Skip to content

Commit

Permalink
Use copypasta for clipboard handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki committed Nov 25, 2023
1 parent 4340303 commit 08dae8f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ sha2 = "0.10.6"
bitflags = "2.2.1"
indexmap = "2"
rustc-hash = "1.1.0"
clipboard = "0.5.0"
smallvec = "1.10.0"
educe = "0.4.20"
taffy = "0.3.18"
Expand All @@ -32,6 +31,7 @@ floem_reactive = { path = "reactive" }
winit = { git = "https://github.com/lapce/winit", rev = "7608048ad91efceb6d97d03dcd74b33c60cc2072", features = ["rwh_05"] }
# winit = { path = "../winit", features = ["rwh_05"] }
image = { version = "0.24", features = ["jpeg", "png"] }
copypasta = { version = "0.10.0", default-features = false, features = ["wayland", "x11"] }

[features]
serde = ["winit/serde"]
Expand Down
9 changes: 7 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ use winit::{
};

use crate::{
action::Timer, app_handle::ApplicationHandle, inspector::Capture, profiler::Profile,
view::View, window::WindowConfig,
action::Timer, app_handle::ApplicationHandle, clipboard::Clipboard, inspector::Capture,
profiler::Profile, view::View, window::WindowConfig,
};

use raw_window_handle::HasRawDisplayHandle;

type AppEventCallback = dyn Fn(AppEvent);

static EVENT_LOOP_PROXY: Lazy<Arc<Mutex<Option<EventLoopProxy<UserEvent>>>>> =
Lazy::new(|| Arc::new(Mutex::new(None)));

pub static CLIPBOARD: Lazy<Mutex<Option<Clipboard>>> = Lazy::new(|| Mutex::new(None));

thread_local! {
pub(crate) static APP_UPDATE_EVENTS: RefCell<Vec<AppUpdateEvent>> = Default::default();
}
Expand Down Expand Up @@ -94,6 +98,7 @@ impl Application {
.expect("can't start the event loop");
let event_loop_proxy = event_loop.create_proxy();
*EVENT_LOOP_PROXY.lock() = Some(event_loop_proxy.clone());
*CLIPBOARD.lock() = Some(unsafe { Clipboard::new(event_loop.raw_display_handle()) });
let handle = ApplicationHandle::new();
Self {
handle: Some(handle),
Expand Down
42 changes: 42 additions & 0 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use raw_window_handle::RawDisplayHandle;

#[cfg(not(any(target_os = "macos", windows)))]
use copypasta::{
wayland_clipboard,
x11_clipboard::{Primary as X11SelectionClipboard, X11ClipboardContext},
};

use copypasta::{ClipboardContext, ClipboardProvider};

pub struct Clipboard {
pub clipboard: Box<dyn ClipboardProvider>,
pub selection: Option<Box<dyn ClipboardProvider>>,
}

impl Clipboard {
pub unsafe fn new(display: RawDisplayHandle) -> Self {
#[cfg(not(any(target_os = "macos", windows)))]
if let RawDisplayHandle::Wayland(display) = display {
let (selection, clipboard) =
wayland_clipboard::create_clipboards_from_external(display.display);
return Self {
clipboard: Box::new(clipboard),
selection: Some(Box::new(selection)),
};
}

#[cfg(not(any(target_os = "macos", windows)))]
return Self {
clipboard: Box::new(ClipboardContext::new().unwrap()),
selection: Some(Box::new(
X11ClipboardContext::<X11SelectionClipboard>::new().unwrap(),
)),
};

#[cfg(any(target_os = "macos", windows))]
return Self {
clipboard: Box::new(ClipboardContext::new().unwrap()),
selection: None,
};
}
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub mod action;
pub mod animate;
mod app;
mod app_handle;
mod clipboard;
pub mod context;
pub mod event;
pub mod ext_event;
Expand All @@ -116,7 +117,8 @@ pub mod widgets;
pub mod window;
mod window_handle;

pub use app::{launch, quit_app, AppEvent, Application};
pub use app::{launch, quit_app, AppEvent, Application, CLIPBOARD};
pub use clipboard::Clipboard;
pub use context::EventPropagation;
pub use floem_reactive as reactive;
pub use floem_renderer::cosmic_text;
Expand Down
11 changes: 7 additions & 4 deletions src/views/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::style::{FontStyle, FontWeight, TextColor};
use crate::unit::{PxPct, PxPctAuto};
use crate::view::ViewData;
use crate::widgets::PlaceholderTextClass;
use crate::CLIPBOARD;
use crate::{prop_extracter, EventPropagation};
use clipboard::{ClipboardContext, ClipboardProvider};
use taffy::prelude::{Layout, Node};

use floem_renderer::{cosmic_text::Cursor, Renderer};
Expand Down Expand Up @@ -483,7 +483,8 @@ impl TextInput {
}
TextCommand::Copy => {
if let Some(selection) = &self.selection {
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
let mut clipboard = CLIPBOARD.lock();
let ctx = &mut clipboard.as_mut().unwrap().clipboard;
let selection_txt = self
.buffer
.get()
Expand All @@ -497,7 +498,8 @@ impl TextInput {
}
TextCommand::Cut => {
if let Some(selection) = &self.selection {
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
let mut clipboard = CLIPBOARD.lock();
let ctx = &mut clipboard.as_mut().unwrap().clipboard;
let selection_txt = self
.buffer
.get()
Expand All @@ -517,7 +519,8 @@ impl TextInput {
true
}
TextCommand::Paste => {
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
let mut clipboard = CLIPBOARD.lock();
let ctx = &mut clipboard.as_mut().unwrap().clipboard;
let clipboard_content = ctx.get_contents().unwrap();
if clipboard_content.is_empty() {
return false;
Expand Down

0 comments on commit 08dae8f

Please sign in to comment.