|
1 | 1 | use crate::StringWrapper;
|
2 | 2 |
|
3 | 3 | #[cfg(target_os = "linux")]
|
4 |
| -pub fn get_selection_text_on_x11() -> Result<String, String> { |
| 4 | +fn get_selection_text_on_x11() -> Result<String, String> { |
5 | 5 | use crate::APP;
|
6 | 6 | use std::time::Duration;
|
7 | 7 | use tauri::Manager;
|
@@ -48,7 +48,7 @@ pub fn get_selection_text_on_x11() -> Result<String, String> {
|
48 | 48 | }
|
49 | 49 |
|
50 | 50 | #[cfg(target_os = "linux")]
|
51 |
| -pub fn get_selection_text_on_wayland() -> Result<String, String> { |
| 51 | +fn get_selection_text_on_wayland() -> Result<String, String> { |
52 | 52 | use std::io::Read;
|
53 | 53 | use wl_clipboard_rs::paste::{get_contents, ClipboardType, Error, MimeType, Seat};
|
54 | 54 | use wl_clipboard_rs::utils::is_primary_selection_supported;
|
@@ -107,11 +107,67 @@ pub fn get_selection_text() -> Result<String, String> {
|
107 | 107 | }
|
108 | 108 | }
|
109 | 109 |
|
| 110 | +#[cfg(target_os = "windows")] |
| 111 | +fn get_selection_text_by_automation() -> Result<String, String> { |
| 112 | + use windows::Win32::System::Com::{CoCreateInstance, CoInitialize, CLSCTX_ALL}; |
| 113 | + use windows::Win32::UI::Accessibility::{ |
| 114 | + CUIAutomation, IUIAutomation, IUIAutomationTextPattern, UIA_TextPatternId, |
| 115 | + }; |
| 116 | + // 初始化 COM |
| 117 | + match unsafe { CoInitialize(None) } { |
| 118 | + Ok(_) => {} |
| 119 | + Err(e) => return Err(e.to_string()), |
| 120 | + }; |
| 121 | + // 创建 IUIAutomation 对象 |
| 122 | + let auto: IUIAutomation = match unsafe { CoCreateInstance(&CUIAutomation, None, CLSCTX_ALL) } { |
| 123 | + Ok(v) => v, |
| 124 | + Err(e) => return Err(e.to_string()), |
| 125 | + }; |
| 126 | + // 获取焦点元素 |
| 127 | + let el = match unsafe { auto.GetFocusedElement() } { |
| 128 | + Ok(v) => v, |
| 129 | + Err(e) => return Err(e.to_string()), |
| 130 | + }; |
| 131 | + // 获取文本对象 |
| 132 | + let res: IUIAutomationTextPattern = match unsafe { el.GetCurrentPatternAs(UIA_TextPatternId) } { |
| 133 | + Ok(v) => v, |
| 134 | + Err(e) => return Err(e.to_string()), |
| 135 | + }; |
| 136 | + // 获取文本序列 |
| 137 | + let text_array = match unsafe { res.GetSelection() } { |
| 138 | + Ok(v) => v, |
| 139 | + Err(e) => return Err(e.to_string()), |
| 140 | + }; |
| 141 | + let length = unsafe { text_array.Length().unwrap() }; |
| 142 | + // 遍历文本序列 |
| 143 | + let mut target = String::new(); |
| 144 | + for i in 0..length { |
| 145 | + let text = match unsafe { text_array.GetElement(i) } { |
| 146 | + Ok(v) => v, |
| 147 | + Err(e) => return Err(e.to_string()), |
| 148 | + }; |
| 149 | + let str = match unsafe { text.GetText(-1) } { |
| 150 | + Ok(v) => v, |
| 151 | + Err(e) => return Err(e.to_string()), |
| 152 | + }; |
| 153 | + let str = str.to_string(); |
| 154 | + target.push_str(&str); |
| 155 | + } |
| 156 | + Ok(target) |
| 157 | +} |
| 158 | + |
110 | 159 | // 获取选择的文本(Windows)
|
111 | 160 | #[cfg(target_os = "windows")]
|
112 | 161 | pub fn get_selection_text() -> Result<String, String> {
|
| 162 | + if let Ok(text) = get_selection_text_by_automation() { |
| 163 | + if !text.is_empty() { |
| 164 | + println!("get_selection_text_by_automation"); |
| 165 | + return Ok(text); |
| 166 | + } |
| 167 | + } |
| 168 | + // Automation取词失败后再尝试获取剪切板 |
113 | 169 | use arboard::Clipboard;
|
114 |
| - |
| 170 | + println!("get_selection_text_by_clipboard"); |
115 | 171 | // 读取旧的剪切板
|
116 | 172 | let old_clipboard = (
|
117 | 173 | Clipboard::new().unwrap().get_text(),
|
|
0 commit comments