Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(runtime-wry): avoid panic during clipboard initialization on wayland #9003

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/clipboard-wayland.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-runtime-wry': 'patch'
---

Fix panic during intialization on wayland when using `clipboard` feature, instead propagate the error during API usage.
38 changes: 34 additions & 4 deletions core/tauri-runtime-wry/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tauri_runtime::{ClipboardManager, Result};

#[derive(Clone)]
pub struct ClipboardManagerWrapper {
pub clipboard: Arc<Mutex<Clipboard>>,
pub clipboard: Arc<Mutex<std::result::Result<Clipboard, arboard::Error>>>,
}

impl fmt::Debug for ClipboardManagerWrapper {
Expand All @@ -23,17 +23,47 @@ impl fmt::Debug for ClipboardManagerWrapper {
}
}

struct ClipboardError(String);
impl std::error::Error for ClipboardError {}
impl fmt::Display for ClipboardError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ClipboardError: {}", self.0)
}
}
impl fmt::Debug for ClipboardError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("ClipboardError").field(&self.0).finish()
}
}
impl From<ClipboardError> for crate::Error {
fn from(e: ClipboardError) -> crate::Error {
crate::Error::Clipboard(Box::new(e))
}
}

impl ClipboardManager for ClipboardManagerWrapper {
fn read_text(&self) -> Result<Option<String>> {
Ok(self.clipboard.lock().unwrap().get_text().ok())
self
.clipboard
.lock()
.unwrap()
.as_mut()
.map(|c| c.get_text().map(Some))
.map_err(|e| ClipboardError(e.to_string()))?
.map_err(|e| ClipboardError(e.to_string()))
.map_err(Into::into)
}

fn write_text<V: Into<String>>(&mut self, text: V) -> Result<()> {
let text = text.into();
self
.clipboard
.lock()
.unwrap()
.set_text(text.into())
.map_err(|e| crate::Error::Clipboard(Box::new(e)))
.as_mut()
.map(|c| c.set_text(text))
.map_err(|e| ClipboardError(e.to_string()))?
.map_err(|e| ClipboardError(e.to_string()))
.map_err(Into::into)
}
}
2 changes: 1 addition & 1 deletion core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,7 @@ impl<T: UserEvent> Wry<T> {
#[cfg(feature = "clipboard")]
#[allow(clippy::redundant_clone)]
let clipboard_manager_handle = ClipboardManagerWrapper {
clipboard: Arc::new(Mutex::new(Clipboard::new().unwrap())),
clipboard: Arc::new(Mutex::new(Clipboard::new())),
};

Ok(Self {
Expand Down
Loading