selection-capture 0.1.4

Sync, cancellable selected-text capture engine with strategy-aware fallbacks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#[cfg(all(feature = "rich-content", target_os = "windows"))]
use crate::rich_convert::plain_text_to_minimal_rtf;
use crate::traits::{CapturePlatform, MonitorPlatform};
use crate::types::{ActiveApp, CaptureMethod, CleanupStatus, PlatformAttemptResult};
use crate::windows_observer::{
    drain_events_for_monitor as windows_observer_drain_events_for_monitor, WindowsObserverBridge,
};
use crate::windows_runtime_adapter::install_default_windows_runtime_adapter_if_absent;
use crate::windows_subscriber::ensure_windows_native_subscriber_hook_installed;
use std::collections::VecDeque;
#[cfg(target_os = "windows")]
use std::process::Command;
use std::sync::Mutex;
use std::time::Duration;

#[derive(Debug, Default)]
pub struct WindowsPlatform;

pub struct WindowsSelectionMonitor {
    last_emitted: Mutex<Option<String>>,
    native_event_queue: Mutex<VecDeque<String>>,
    native_events_dropped: Mutex<u64>,
    native_queue_capacity: usize,
    poll_interval: Duration,
    backend: WindowsMonitorBackend,
    native_observer_attached: bool,
    native_event_pump: Option<WindowsNativeEventPump>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WindowsMonitorBackend {
    Polling,
    NativeEventPreferred,
}

#[derive(Clone, Copy, Debug)]
pub struct WindowsSelectionMonitorOptions {
    pub poll_interval: Duration,
    pub backend: WindowsMonitorBackend,
    pub native_queue_capacity: usize,
    pub native_event_pump: Option<WindowsNativeEventPump>,
}

pub type WindowsNativeEventPump = fn() -> Vec<String>;

trait WindowsBackend {
    fn attempt_ui_automation(&self) -> PlatformAttemptResult;
    fn attempt_iaccessible(&self) -> PlatformAttemptResult;
    fn attempt_clipboard(&self) -> PlatformAttemptResult;
    fn attempt_synthetic_copy(&self) -> PlatformAttemptResult;
}

#[derive(Debug, Default)]
struct DefaultWindowsBackend;

impl WindowsBackend for DefaultWindowsBackend {
    fn attempt_ui_automation(&self) -> PlatformAttemptResult {
        #[cfg(target_os = "windows")]
        {
            match read_uia_text() {
                Ok(Some(text)) => {
                    let trimmed = text.trim();
                    if trimmed.is_empty() {
                        PlatformAttemptResult::EmptySelection
                    } else {
                        PlatformAttemptResult::Success(trimmed.to_string())
                    }
                }
                Ok(None) => PlatformAttemptResult::EmptySelection,
                Err(_) => PlatformAttemptResult::Unavailable,
            }
        }
        #[cfg(not(target_os = "windows"))]
        {
            PlatformAttemptResult::Unavailable
        }
    }

    fn attempt_iaccessible(&self) -> PlatformAttemptResult {
        #[cfg(target_os = "windows")]
        {
            match read_iaccessible_text() {
                Ok(Some(text)) => {
                    let trimmed = text.trim();
                    if trimmed.is_empty() {
                        PlatformAttemptResult::EmptySelection
                    } else {
                        PlatformAttemptResult::Success(trimmed.to_string())
                    }
                }
                Ok(None) => PlatformAttemptResult::EmptySelection,
                Err(_) => PlatformAttemptResult::Unavailable,
            }
        }
        #[cfg(not(target_os = "windows"))]
        {
            PlatformAttemptResult::Unavailable
        }
    }

    fn attempt_clipboard(&self) -> PlatformAttemptResult {
        #[cfg(target_os = "windows")]
        {
            match read_clipboard_text() {
                Ok(Some(text)) => {
                    let trimmed = text.trim();
                    if trimmed.is_empty() {
                        PlatformAttemptResult::EmptySelection
                    } else {
                        PlatformAttemptResult::Success(trimmed.to_string())
                    }
                }
                Ok(None) => PlatformAttemptResult::EmptySelection,
                Err(_) => PlatformAttemptResult::Unavailable,
            }
        }
        #[cfg(not(target_os = "windows"))]
        {
            PlatformAttemptResult::Unavailable
        }
    }

    fn attempt_synthetic_copy(&self) -> PlatformAttemptResult {
        #[cfg(target_os = "windows")]
        {
            match synthetic_copy_capture_text() {
                Ok(Some(text)) => {
                    let trimmed = text.trim();
                    if trimmed.is_empty() {
                        PlatformAttemptResult::EmptySelection
                    } else {
                        PlatformAttemptResult::Success(trimmed.to_string())
                    }
                }
                Ok(None) => PlatformAttemptResult::EmptySelection,
                Err(_) => PlatformAttemptResult::Unavailable,
            }
        }
        #[cfg(not(target_os = "windows"))]
        {
            PlatformAttemptResult::Unavailable
        }
    }
}

impl WindowsPlatform {
    pub fn new() -> Self {
        Self
    }

    pub fn attempt_ui_automation(&self) -> PlatformAttemptResult {
        self.backend().attempt_ui_automation()
    }

    pub fn attempt_iaccessible(&self) -> PlatformAttemptResult {
        self.backend().attempt_iaccessible()
    }

    pub fn attempt_clipboard(&self) -> PlatformAttemptResult {
        self.backend().attempt_clipboard()
    }

    fn backend(&self) -> DefaultWindowsBackend {
        DefaultWindowsBackend
    }

    fn dispatch_attempt<B: WindowsBackend>(
        backend: &B,
        method: CaptureMethod,
    ) -> PlatformAttemptResult {
        match method {
            CaptureMethod::AccessibilityPrimary => backend.attempt_ui_automation(),
            CaptureMethod::AccessibilityRange => backend.attempt_iaccessible(),
            CaptureMethod::ClipboardBorrow => backend.attempt_clipboard(),
            CaptureMethod::SyntheticCopy => backend.attempt_synthetic_copy(),
        }
    }
}

impl Default for WindowsSelectionMonitor {
    fn default() -> Self {
        Self::new_with_options(WindowsSelectionMonitorOptions::default())
    }
}

impl Default for WindowsSelectionMonitorOptions {
    fn default() -> Self {
        Self {
            poll_interval: Duration::from_millis(120),
            backend: WindowsMonitorBackend::Polling,
            native_queue_capacity: 256,
            native_event_pump: None,
        }
    }
}

impl WindowsSelectionMonitor {
    pub fn new(poll_interval: Duration) -> Self {
        Self::new_with_options(WindowsSelectionMonitorOptions {
            poll_interval,
            backend: WindowsMonitorBackend::Polling,
            native_queue_capacity: 256,
            native_event_pump: None,
        })
    }

    pub fn new_with_options(options: WindowsSelectionMonitorOptions) -> Self {
        if matches!(options.backend, WindowsMonitorBackend::NativeEventPreferred) {
            install_default_windows_runtime_adapter_if_absent();
            ensure_windows_native_subscriber_hook_installed();
        }
        let native_observer_attached =
            matches!(options.backend, WindowsMonitorBackend::NativeEventPreferred)
                && WindowsObserverBridge::acquire();
        let native_event_pump = if native_observer_attached {
            options
                .native_event_pump
                .or(Some(windows_observer_drain_events_for_monitor))
        } else {
            options.native_event_pump
        };

        Self {
            last_emitted: Mutex::new(None),
            native_event_queue: Mutex::new(VecDeque::new()),
            native_events_dropped: Mutex::new(0),
            native_queue_capacity: options.native_queue_capacity.max(1),
            poll_interval: options.poll_interval,
            backend: options.backend,
            native_observer_attached,
            native_event_pump,
        }
    }

    pub fn backend(&self) -> WindowsMonitorBackend {
        self.backend
    }

    pub fn poll_interval(&self) -> Duration {
        self.poll_interval
    }

    pub fn enqueue_native_selection_event<T>(&self, text: T) -> bool
    where
        T: Into<String>,
    {
        let text = text.into();
        let trimmed = text.trim();
        if trimmed.is_empty() {
            return false;
        }
        if let Ok(mut queue) = self.native_event_queue.lock() {
            if queue.back().map(|s| s == trimmed).unwrap_or(false) {
                return false;
            }
            if queue.len() >= self.native_queue_capacity {
                queue.pop_front();
                if let Ok(mut dropped) = self.native_events_dropped.lock() {
                    *dropped += 1;
                }
            }
            queue.push_back(trimmed.to_string());
            return true;
        }
        false
    }

    pub fn enqueue_native_selection_events<I, T>(&self, events: I) -> usize
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        let mut accepted = 0usize;
        for event in events {
            if self.enqueue_native_selection_event(event.into()) {
                accepted += 1;
            }
        }
        accepted
    }

    pub fn native_queue_depth(&self) -> usize {
        self.native_event_queue
            .lock()
            .map(|queue| queue.len())
            .unwrap_or(0)
    }

    pub fn native_events_dropped(&self) -> u64 {
        self.native_events_dropped
            .lock()
            .map(|dropped| *dropped)
            .unwrap_or(0)
    }

    pub fn poll_native_event_pump_once(&self) -> usize {
        let Some(pump) = self.native_event_pump else {
            return 0;
        };
        self.enqueue_native_selection_events(pump())
    }

    fn next_selection_text(&self) -> Option<String> {
        if matches!(self.backend, WindowsMonitorBackend::NativeEventPreferred) {
            let _ = self.poll_native_event_pump_once();
            if let Some(next) = self.native_event_queue.lock().ok()?.pop_front() {
                return self.emit_if_new(next);
            }
        }
        let next = self.read_selection_text()?;
        self.emit_if_new(next)
    }

    fn emit_if_new(&self, next: String) -> Option<String> {
        let mut last = self.last_emitted.lock().ok()?;
        if last.as_ref() == Some(&next) {
            return None;
        }
        *last = Some(next.clone());
        Some(next)
    }

    fn read_selection_text(&self) -> Option<String> {
        #[cfg(target_os = "windows")]
        {
            let atspi = read_uia_text().ok().flatten();
            if let Some(next) = atspi {
                let trimmed = next.trim();
                if !trimmed.is_empty() {
                    return Some(trimmed.to_string());
                }
            }

            let legacy = read_iaccessible_text().ok().flatten();
            if let Some(next) = legacy {
                let trimmed = next.trim();
                if !trimmed.is_empty() {
                    return Some(trimmed.to_string());
                }
            }
            None
        }
        #[cfg(not(target_os = "windows"))]
        {
            None
        }
    }
}

impl CapturePlatform for WindowsPlatform {
    fn active_app(&self) -> Option<ActiveApp> {
        #[cfg(target_os = "windows")]
        {
            return read_active_app().ok().flatten();
        }
        #[cfg(not(target_os = "windows"))]
        {
            None
        }
    }

    fn attempt(&self, method: CaptureMethod, _app: Option<&ActiveApp>) -> PlatformAttemptResult {
        Self::dispatch_attempt(&self.backend(), method)
    }

    fn cleanup(&self) -> CleanupStatus {
        CleanupStatus::Clean
    }
}

impl MonitorPlatform for WindowsSelectionMonitor {
    fn next_selection_change(&self) -> Option<String> {
        self.next_selection_text()
    }
}

impl Drop for WindowsSelectionMonitor {
    fn drop(&mut self) {
        if self.native_observer_attached {
            let _ = WindowsObserverBridge::release();
        }
    }
}

#[cfg(target_os = "windows")]
fn read_clipboard_text() -> Result<Option<String>, String> {
    let output = Command::new("powershell")
        .args([
            "-NoProfile",
            "-NonInteractive",
            "-Command",
            "$t = Get-Clipboard -Raw; if ($null -eq $t) { '' } else { $t }",
        ])
        .output()
        .map_err(|err| err.to_string())?;

    if !output.status.success() {
        return Err(String::from_utf8_lossy(&output.stderr).trim().to_string());
    }

    let stdout = String::from_utf8(output.stdout).map_err(|err| err.to_string())?;
    Ok(normalize_windows_text_stdout(&stdout))
}

#[cfg(target_os = "windows")]
fn read_uia_text() -> Result<Option<String>, String> {
    let output = Command::new("powershell")
        .args([
            "-NoProfile",
            "-NonInteractive",
            "-Command",
            r#"
Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes
$focused = [System.Windows.Automation.AutomationElement]::FocusedElement
if ($null -eq $focused) { return }
try {
  $textPattern = $focused.GetCurrentPattern([System.Windows.Automation.TextPattern]::Pattern)
} catch {
  $textPattern = $null
}
if ($null -ne $textPattern) {
  $selection = $textPattern.GetSelection()
  if ($null -ne $selection -and $selection.Length -gt 0) {
    $text = $selection[0].GetText(-1)
    if ($null -ne $text -and $text.Trim().Length -gt 0) {
      Write-Output $text
      return
    }
  }
}
try {
  $valuePattern = $focused.GetCurrentPattern([System.Windows.Automation.ValuePattern]::Pattern)
} catch {
  $valuePattern = $null
}
if ($null -ne $valuePattern) {
  $value = $valuePattern.Current.Value
  if ($null -ne $value -and $value.Trim().Length -gt 0) {
    Write-Output $value
    return
  }
}
"#,
        ])
        .output()
        .map_err(|err| err.to_string())?;

    if !output.status.success() {
        return Err(String::from_utf8_lossy(&output.stderr).trim().to_string());
    }

    let stdout = String::from_utf8(output.stdout).map_err(|err| err.to_string())?;
    Ok(normalize_windows_text_stdout(&stdout))
}

#[cfg(all(feature = "rich-content", target_os = "windows"))]
pub(crate) fn try_selected_rtf_by_uia() -> Option<String> {
    let text = read_uia_text().ok().flatten()?;
    let trimmed = text.trim();
    if trimmed.is_empty() {
        None
    } else {
        Some(plain_text_to_minimal_rtf(trimmed))
    }
}

pub(crate) fn windows_default_runtime_event_source() -> Option<String> {
    #[cfg(target_os = "windows")]
    {
        return read_uia_text().ok().flatten();
    }
    #[cfg(not(target_os = "windows"))]
    {
        None
    }
}

#[cfg(target_os = "windows")]
fn synthetic_copy_capture_text() -> Result<Option<String>, String> {
    let output = Command::new("powershell")
        .args([
            "-NoProfile",
            "-NonInteractive",
            "-STA",
            "-Command",
            r#"
Add-Type -AssemblyName System.Windows.Forms
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class Win32 {
  [DllImport("user32.dll")]
  public static extern IntPtr GetForegroundWindow();
}
"@
$hwnd = [Win32]::GetForegroundWindow()
if ($hwnd -eq [IntPtr]::Zero) { return }

$original = $null
$hasOriginal = $false
try {
  $original = Get-Clipboard -Raw -ErrorAction Stop
  $hasOriginal = $true
} catch {}

[System.Windows.Forms.SendKeys]::SendWait("^c")
Start-Sleep -Milliseconds 90

$captured = $null
try {
  $captured = Get-Clipboard -Raw -ErrorAction Stop
} catch {}

if ($hasOriginal) {
  try {
    Set-Clipboard -Value $original
  } catch {}
}

if ($null -ne $captured) {
  Write-Output $captured
}
"#,
        ])
        .output()
        .map_err(|err| err.to_string())?;

    if !output.status.success() {
        return Err(String::from_utf8_lossy(&output.stderr).trim().to_string());
    }

    let stdout = String::from_utf8(output.stdout).map_err(|err| err.to_string())?;
    Ok(normalize_windows_text_stdout(&stdout))
}

#[cfg(target_os = "windows")]
fn read_iaccessible_text() -> Result<Option<String>, String> {
    let output = Command::new("powershell")
        .args([
            "-NoProfile",
            "-NonInteractive",
            "-Command",
            r#"
Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes
$focused = [System.Windows.Automation.AutomationElement]::FocusedElement
if ($null -eq $focused) { return }
try {
  $legacy = $focused.GetCurrentPattern([System.Windows.Automation.LegacyIAccessiblePattern]::Pattern)
} catch {
  $legacy = $null
}
if ($null -eq $legacy) { return }
$value = $legacy.Current.Value
if ($null -ne $value -and $value.Trim().Length -gt 0) {
  Write-Output $value
  return
}
$name = $legacy.Current.Name
if ($null -ne $name -and $name.Trim().Length -gt 0) {
  Write-Output $name
  return
}
"#,
        ])
        .output()
        .map_err(|err| err.to_string())?;

    if !output.status.success() {
        return Err(String::from_utf8_lossy(&output.stderr).trim().to_string());
    }

    let stdout = String::from_utf8(output.stdout).map_err(|err| err.to_string())?;
    Ok(normalize_windows_text_stdout(&stdout))
}

#[cfg(target_os = "windows")]
fn read_active_app() -> Result<Option<ActiveApp>, String> {
    let output = Command::new("powershell")
        .args([
            "-NoProfile",
            "-NonInteractive",
            "-Command",
            r#"
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class Win32 {
  [DllImport("user32.dll")]
  public static extern IntPtr GetForegroundWindow();

  [DllImport("user32.dll")]
  public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
}
"@
$hwnd = [Win32]::GetForegroundWindow()
if ($hwnd -eq [IntPtr]::Zero) { return }
$pid = 0
[Win32]::GetWindowThreadProcessId($hwnd, [ref]$pid) | Out-Null
if ($pid -eq 0) { return }
$process = Get-Process -Id $pid -ErrorAction SilentlyContinue
if ($null -eq $process) { return }
$name = $process.ProcessName
$path = $process.Path
Write-Output ("NAME:" + $name)
Write-Output ("PATH:" + $path)
"#,
        ])
        .output()
        .map_err(|err| err.to_string())?;

    if !output.status.success() {
        return Err(String::from_utf8_lossy(&output.stderr).trim().to_string());
    }

    let stdout = String::from_utf8(output.stdout).map_err(|err| err.to_string())?;
    Ok(parse_active_app_stdout(&stdout))
}

#[cfg(target_os = "windows")]
fn normalize_windows_text_stdout(stdout: &str) -> Option<String> {
    let text = stdout.replace("\r\n", "\n");
    let normalized = text.trim_end_matches(['\r', '\n']);
    if normalized.is_empty() {
        None
    } else {
        Some(normalized.to_string())
    }
}

#[cfg(target_os = "windows")]
fn parse_active_app_stdout(stdout: &str) -> Option<ActiveApp> {
    let mut name: Option<String> = None;
    let mut path: Option<String> = None;

    for line in stdout.lines() {
        if let Some(value) = line.strip_prefix("NAME:") {
            let trimmed = value.trim();
            if !trimmed.is_empty() {
                name = Some(trimmed.to_string());
            }
        } else if let Some(value) = line.strip_prefix("PATH:") {
            let trimmed = value.trim();
            if !trimmed.is_empty() {
                path = Some(trimmed.to_string());
            }
        }
    }

    let app_name = name?;
    let bundle_id = path.unwrap_or_else(|| format!("process://{}", app_name.to_lowercase()));
    Some(ActiveApp {
        bundle_id,
        name: app_name,
    })
}

#[cfg(test)]
#[path = "windows_tests.rs"]
mod tests;