Skip to content

Commit 24e4ff2

Browse files
authored
refactor(core): add clipboard Cargo feature, enhancing binary size (#3957)
1 parent e11878b commit 24e4ff2

10 files changed

Lines changed: 150 additions & 55 deletions

File tree

.changes/clipboard-feature.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch
3+
"tauri-runtime": minor
4+
"tauri-runtime-wry": minor
5+
---
6+
7+
**Breaking change::* Added the `clipboard` Cargo feature.

core/tauri-runtime-wry/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ objc-exception = [ "wry/objc-exception" ]
4242
gtk-tray = [ "wry/gtk-tray" ]
4343
ayatana-tray = [ "wry/ayatana-tray" ]
4444
global-shortcut = [ "tauri-runtime/global-shortcut" ]
45+
clipboard = [ "tauri-runtime/clipboard" ]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
//! Clipboard implementation.
6+
7+
use crate::{getter, Context, Message};
8+
9+
use std::sync::{
10+
mpsc::{channel, Sender},
11+
Arc, Mutex,
12+
};
13+
14+
use tauri_runtime::{ClipboardManager, Result, UserEvent};
15+
pub use wry::application::clipboard::Clipboard;
16+
17+
#[derive(Debug, Clone)]
18+
pub enum ClipboardMessage {
19+
WriteText(String, Sender<()>),
20+
ReadText(Sender<Option<String>>),
21+
}
22+
23+
#[derive(Debug, Clone)]
24+
pub struct ClipboardManagerWrapper<T: UserEvent> {
25+
pub context: Context<T>,
26+
}
27+
28+
// SAFETY: this is safe since the `Context` usage is guarded on `send_user_message`.
29+
#[allow(clippy::non_send_fields_in_send_ty)]
30+
unsafe impl<T: UserEvent> Sync for ClipboardManagerWrapper<T> {}
31+
32+
impl<T: UserEvent> ClipboardManager for ClipboardManagerWrapper<T> {
33+
fn read_text(&self) -> Result<Option<String>> {
34+
let (tx, rx) = channel();
35+
getter!(self, rx, Message::Clipboard(ClipboardMessage::ReadText(tx)))
36+
}
37+
38+
fn write_text<V: Into<String>>(&mut self, text: V) -> Result<()> {
39+
let (tx, rx) = channel();
40+
getter!(
41+
self,
42+
rx,
43+
Message::Clipboard(ClipboardMessage::WriteText(text.into(), tx))
44+
)?;
45+
Ok(())
46+
}
47+
}
48+
49+
pub fn handle_clipboard_message(
50+
message: ClipboardMessage,
51+
clipboard_manager: &Arc<Mutex<Clipboard>>,
52+
) {
53+
match message {
54+
ClipboardMessage::WriteText(text, tx) => {
55+
clipboard_manager.lock().unwrap().write_text(text);
56+
tx.send(()).unwrap();
57+
}
58+
ClipboardMessage::ReadText(tx) => tx
59+
.send(clipboard_manager.lock().unwrap().read_text())
60+
.unwrap(),
61+
}
62+
}

0 commit comments

Comments
 (0)