Skip to content

Commit 0779c8e

Browse files
committed
clean up some clipboard code
1 parent a6610d5 commit 0779c8e

File tree

1 file changed

+10
-68
lines changed

1 file changed

+10
-68
lines changed

src-tauri/src/clipboard.rs

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -136,80 +136,23 @@ fn paste_via_direct_input(text: &str) -> Result<(), String> {
136136
Ok(())
137137
}
138138

139-
/// Pastes text using the clipboard method with Ctrl+V/Cmd+V.
140-
/// Saves the current clipboard, writes the text, sends paste command, then restores the clipboard.
141-
fn paste_via_clipboard_ctrl_v(text: &str, app_handle: &AppHandle) -> Result<(), String> {
139+
/// Pastes text using the clipboard: saves current content, writes text, sends paste keystroke, restores clipboard.
140+
fn paste_via_clipboard(
141+
text: &str,
142+
app_handle: &AppHandle,
143+
send_paste: fn() -> Result<(), String>,
144+
) -> Result<(), String> {
142145
let clipboard = app_handle.clipboard();
143-
144-
// get the current clipboard content
145-
let clipboard_content = clipboard.read_text().unwrap_or_default();
146-
147-
clipboard
148-
.write_text(text)
149-
.map_err(|e| format!("Failed to write to clipboard: {}", e))?;
150-
151-
// small delay to ensure the clipboard content has been written to
152-
std::thread::sleep(std::time::Duration::from_millis(50));
153-
154-
send_paste_ctrl_v()?;
155-
156-
std::thread::sleep(std::time::Duration::from_millis(50));
157-
158-
// restore the clipboard
159-
clipboard
160-
.write_text(&clipboard_content)
161-
.map_err(|e| format!("Failed to restore clipboard: {}", e))?;
162-
163-
Ok(())
164-
}
165-
166-
/// Pastes text using the clipboard method with Shift+Insert (Windows/Linux only).
167-
/// Saves the current clipboard, writes the text, sends paste command, then restores the clipboard.
168-
fn paste_via_clipboard_shift_insert(text: &str, app_handle: &AppHandle) -> Result<(), String> {
169-
let clipboard = app_handle.clipboard();
170-
171-
// get the current clipboard content
172146
let clipboard_content = clipboard.read_text().unwrap_or_default();
173147

174148
clipboard
175149
.write_text(text)
176150
.map_err(|e| format!("Failed to write to clipboard: {}", e))?;
177151

178-
// small delay to ensure the clipboard content has been written to
179152
std::thread::sleep(std::time::Duration::from_millis(50));
180-
181-
send_paste_shift_insert()?;
182-
183-
std::thread::sleep(std::time::Duration::from_millis(50));
184-
185-
// restore the clipboard
186-
clipboard
187-
.write_text(&clipboard_content)
188-
.map_err(|e| format!("Failed to restore clipboard: {}", e))?;
189-
190-
Ok(())
191-
}
192-
193-
/// Pastes text using the clipboard method with Ctrl+Shift+V.
194-
/// Saves the current clipboard, writes the text, sends paste command, then restores the clipboard.
195-
fn paste_via_clipboard_ctrl_shift_v(text: &str, app_handle: &AppHandle) -> Result<(), String> {
196-
let clipboard = app_handle.clipboard();
197-
198-
// get the current clipboard content
199-
let clipboard_content = clipboard.read_text().unwrap_or_default();
200-
201-
clipboard
202-
.write_text(text)
203-
.map_err(|e| format!("Failed to write to clipboard: {}", e))?;
204-
205-
// small delay to ensure the clipboard content has been written to
206-
std::thread::sleep(std::time::Duration::from_millis(50));
207-
208-
send_paste_ctrl_shift_v()?;
209-
153+
send_paste()?;
210154
std::thread::sleep(std::time::Duration::from_millis(50));
211155

212-
// restore the clipboard
213156
clipboard
214157
.write_text(&clipboard_content)
215158
.map_err(|e| format!("Failed to restore clipboard: {}", e))?;
@@ -317,13 +260,12 @@ pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> {
317260
// Perform the paste operation
318261
match paste_method {
319262
PasteMethod::None => {
320-
// Intentionally do not perform any paste action; history/clipboard update
321263
info!("PasteMethod::None selected - skipping paste action");
322264
}
323-
PasteMethod::CtrlV => paste_via_clipboard_ctrl_v(&text, &app_handle)?,
324265
PasteMethod::Direct => paste_via_direct_input(&text)?,
325-
PasteMethod::ShiftInsert => paste_via_clipboard_shift_insert(&text, &app_handle)?,
326-
PasteMethod::CtrlShiftV => paste_via_clipboard_ctrl_shift_v(&text, &app_handle)?,
266+
PasteMethod::CtrlV => paste_via_clipboard(&text, &app_handle, send_paste_ctrl_v)?,
267+
PasteMethod::CtrlShiftV => paste_via_clipboard(&text, &app_handle, send_paste_ctrl_shift_v)?,
268+
PasteMethod::ShiftInsert => paste_via_clipboard(&text, &app_handle, send_paste_shift_insert)?,
327269
}
328270

329271
// After pasting, optionally copy to clipboard based on settings

0 commit comments

Comments
 (0)