1 parent
50d4b4b09c
commit
f27e83b467
9 files changed
+220
-128
No files matched your search
+3
-10
@@ -3,18 +3,11 @@ use mlua::{Lua, Result as LuaResult};
|
||||
|
||||
pub fn copy(l: &Lua, text: String) -> LuaResult<()> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
ctx
|
||||
.clipboard()
|
||||
.set_text(text)
|
||||
.map_err(|e| anyhow::anyhow!("{}", e))?;
|
||||
ctx.window().set_clipboard_text(text);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn paste(l: &Lua, _: ()) -> LuaResult<String> {
|
||||
pub fn paste(l: &Lua, _: ()) -> LuaResult<Option<String>> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let text = ctx
|
||||
.clipboard()
|
||||
.get_text()
|
||||
.map_err(|e| anyhow::anyhow!("{}", e))?;
|
||||
Ok(text)
|
||||
Ok(ctx.window().get_clipboard_text())
|
||||
}
|
||||
@@ -11,7 +11,6 @@ use crate::{
|
||||
window::WindowState,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use arboard::Clipboard;
|
||||
use std::sync::Arc;
|
||||
use winit::{
|
||||
application::ApplicationHandler, event::*, event_loop::ActiveEventLoop, keyboard::PhysicalKey,
|
||||
@@ -27,7 +26,6 @@ pub struct AppState {
|
||||
pub input: InputState,
|
||||
pub fonts: Fonts,
|
||||
pub texture_manager: WrappedTextureManager,
|
||||
pub clipboard: Clipboard,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
@@ -54,7 +52,6 @@ impl App {
|
||||
input: InputState::default(),
|
||||
fonts: Fonts::new(pob_font_definitions()),
|
||||
texture_manager: WrappedTextureManager::new(),
|
||||
clipboard: arboard::Clipboard::new()?,
|
||||
},
|
||||
tessellator: Tessellator::default(),
|
||||
force_render: false,
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
use raw_window_handle::RawDisplayHandle;
|
||||
|
||||
/// Abstraction over clipboard crates
|
||||
///
|
||||
/// `smithay_clipboard`: Used on wayland
|
||||
/// `arboard`: All other platforms (check arboard docs for platform support)
|
||||
pub struct Clipboard {
|
||||
#[cfg(target_family = "unix")]
|
||||
smithay: Option<smithay_clipboard::Clipboard>,
|
||||
arboard: Option<arboard::Clipboard>,
|
||||
// fallback if everything else fails. only supports intra-application copy/paste
|
||||
fallback: Option<String>,
|
||||
}
|
||||
|
||||
impl Clipboard {
|
||||
pub fn new(_raw_display_handle: Option<RawDisplayHandle>) -> Self {
|
||||
Self {
|
||||
#[cfg(target_family = "unix")]
|
||||
smithay: create_smithay_clipboard(_raw_display_handle),
|
||||
arboard: create_arboard_clipboard(),
|
||||
fallback: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the text content of clipboard
|
||||
pub fn set_text(&mut self, text: String) {
|
||||
#[cfg(target_family = "unix")]
|
||||
if let Some(clipboard) = &mut self.smithay {
|
||||
clipboard.store(text);
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(clipboard) = &mut self.arboard {
|
||||
let _ = clipboard.set_text(text);
|
||||
return;
|
||||
}
|
||||
|
||||
self.fallback = Some(text);
|
||||
}
|
||||
|
||||
/// Gets the text content of clipboard
|
||||
pub fn get_text(&mut self) -> Option<String> {
|
||||
#[cfg(target_family = "unix")]
|
||||
if let Some(clipboard) = &mut self.smithay {
|
||||
return clipboard.load().ok();
|
||||
}
|
||||
|
||||
if let Some(clipboard) = &mut self.arboard {
|
||||
return clipboard.get_text().ok();
|
||||
}
|
||||
|
||||
if let Some(text) = &self.fallback {
|
||||
return Some(text.clone());
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
fn create_smithay_clipboard(
|
||||
raw_display_handle: Option<RawDisplayHandle>,
|
||||
) -> Option<smithay_clipboard::Clipboard> {
|
||||
if let Some(RawDisplayHandle::Wayland(handle)) = raw_display_handle {
|
||||
log::debug!("Creating smithay clipboard...");
|
||||
Some(unsafe { smithay_clipboard::Clipboard::new(handle.display.as_ptr()) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn create_arboard_clipboard() -> Option<arboard::Clipboard> {
|
||||
log::debug!("Creating arboard clipboard...");
|
||||
match arboard::Clipboard::new() {
|
||||
Ok(clipboard) => Some(clipboard),
|
||||
Err(err) => {
|
||||
log::warn!("Failed to create arboard clipboard: {err}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-11
@@ -12,7 +12,6 @@ use crate::{
|
||||
util::change_working_directory,
|
||||
window::WindowState,
|
||||
};
|
||||
use arboard::Clipboard;
|
||||
use clap::Parser;
|
||||
use mlua::{Lua, Result as LuaResult, Table};
|
||||
use std::{
|
||||
@@ -45,11 +44,10 @@ macro_rules! ctx_accessor {
|
||||
/// Before executing any lua code, we need to "plug" the references into
|
||||
/// the Context and "unplug" them afterwards.
|
||||
pub struct Context {
|
||||
window: Cell<*const WindowState>,
|
||||
window: Cell<*mut WindowState>,
|
||||
input: Cell<*const InputState>,
|
||||
fonts: Cell<*mut Fonts>,
|
||||
texture_manager: Cell<*mut WrappedTextureManager>,
|
||||
clipboard: Cell<*mut Clipboard>,
|
||||
current_working_dir: Cell<*mut PathBuf>,
|
||||
layers: Cell<*mut Layers>,
|
||||
needs_restart: Cell<*mut bool>,
|
||||
@@ -59,11 +57,10 @@ pub struct Context {
|
||||
impl Context {
|
||||
pub fn new() -> &'static Self {
|
||||
Box::leak(Box::new(Self {
|
||||
window: Cell::new(std::ptr::null()),
|
||||
window: Cell::new(std::ptr::null_mut()),
|
||||
input: Cell::new(std::ptr::null()),
|
||||
fonts: Cell::new(std::ptr::null_mut()),
|
||||
texture_manager: Cell::new(std::ptr::null_mut()),
|
||||
clipboard: Cell::new(std::ptr::null_mut()),
|
||||
current_working_dir: Cell::new(std::ptr::null_mut()),
|
||||
layers: Cell::new(std::ptr::null_mut()),
|
||||
needs_restart: Cell::new(std::ptr::null_mut()),
|
||||
@@ -72,11 +69,10 @@ impl Context {
|
||||
}
|
||||
|
||||
pub fn set(&self, ctx: &mut PoBContext) {
|
||||
self.window.set(&ctx.app.window);
|
||||
self.window.set(&mut ctx.app.window);
|
||||
self.input.set(&ctx.app.input);
|
||||
self.fonts.set(&mut ctx.app.fonts);
|
||||
self.texture_manager.set(&mut ctx.app.texture_manager);
|
||||
self.clipboard.set(&mut ctx.app.clipboard);
|
||||
self.current_working_dir
|
||||
.set(&mut ctx.pob.current_working_dir);
|
||||
self.layers.set(&mut ctx.pob.layers);
|
||||
@@ -85,22 +81,20 @@ impl Context {
|
||||
}
|
||||
|
||||
pub fn clear(&self) {
|
||||
self.window.set(std::ptr::null());
|
||||
self.window.set(std::ptr::null_mut());
|
||||
self.input.set(std::ptr::null());
|
||||
self.fonts.set(std::ptr::null_mut());
|
||||
self.texture_manager.set(std::ptr::null_mut());
|
||||
self.clipboard.set(std::ptr::null_mut());
|
||||
self.current_working_dir.set(std::ptr::null_mut());
|
||||
self.layers.set(std::ptr::null_mut());
|
||||
self.needs_restart.set(std::ptr::null_mut());
|
||||
self.is_dpi_aware.set(std::ptr::null_mut());
|
||||
}
|
||||
|
||||
ctx_accessor!(window: &WindowState);
|
||||
ctx_accessor!(window: &mut WindowState);
|
||||
ctx_accessor!(input: &InputState);
|
||||
ctx_accessor!(fonts: &mut Fonts);
|
||||
ctx_accessor!(texture_manager: &mut WrappedTextureManager);
|
||||
ctx_accessor!(clipboard: &mut Clipboard);
|
||||
ctx_accessor!(current_working_dir: &mut PathBuf);
|
||||
ctx_accessor!(layers: &mut Layers);
|
||||
ctx_accessor!(needs_restart: &mut bool);
|
||||
|
||||
@@ -8,6 +8,7 @@ use winit::event_loop::EventLoop;
|
||||
mod api;
|
||||
mod app;
|
||||
mod args;
|
||||
mod clipboard;
|
||||
mod color;
|
||||
mod dpi;
|
||||
mod fonts;
|
||||
|
||||
+25
-3
@@ -1,10 +1,14 @@
|
||||
use crate::{
|
||||
clipboard::Clipboard,
|
||||
dpi::{ConvertToLogical, LogicalSize, PhysicalSize},
|
||||
};
|
||||
use raw_window_handle::HasDisplayHandle;
|
||||
use std::sync::Arc;
|
||||
|
||||
use winit::window::Window;
|
||||
|
||||
use crate::dpi::{ConvertToLogical, LogicalSize, PhysicalSize};
|
||||
|
||||
pub struct WindowState {
|
||||
// NOTE: clipboard needs to be destroyed before window
|
||||
clipboard: Option<Clipboard>,
|
||||
pub window: Option<Arc<Window>>,
|
||||
pub size: PhysicalSize<u32>,
|
||||
pub scale_factor: f32,
|
||||
@@ -18,6 +22,7 @@ impl Default for WindowState {
|
||||
size: Default::default(),
|
||||
scale_factor: 1.0,
|
||||
pending_window_title: std::cell::Cell::new(None),
|
||||
clipboard: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +36,9 @@ impl WindowState {
|
||||
let winit::dpi::PhysicalSize { width, height } = window.inner_size();
|
||||
self.size = PhysicalSize::new(width, height);
|
||||
self.scale_factor = window.scale_factor() as f32;
|
||||
|
||||
let raw_display_handle = window.display_handle().ok().map(|h| h.as_raw());
|
||||
self.clipboard = Some(Clipboard::new(raw_display_handle));
|
||||
self.window = Some(window);
|
||||
}
|
||||
|
||||
@@ -51,4 +59,18 @@ impl WindowState {
|
||||
window.focus_window();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_clipboard_text(&mut self, text: String) {
|
||||
if let Some(clipboard) = &mut self.clipboard {
|
||||
clipboard.set_text(text);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_clipboard_text(&mut self) -> Option<String> {
|
||||
if let Some(clipboard) = &mut self.clipboard {
|
||||
clipboard.get_text()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user