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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
//! Desktop UI automation through accessibility APIs
//!
//! This module provides a cross-platform API for automating desktop applications
//! through accessibility APIs, inspired by Playwright's web automation model.
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Mutex, RwLock};
use sysinfo::{ProcessesToUpdate, System};
use tracing::{debug, error, info, instrument};
pub mod browser_script;
pub mod element;
pub mod errors;
pub mod extension_bridge;
pub mod health;
pub mod locator;
pub mod platforms;
pub mod screenshot;
pub mod screenshot_logger;
pub mod selector;
#[cfg(test)]
mod tests;
pub mod tree_formatter;
pub mod types;
pub mod ui_tree_diff;
pub mod utils;
#[cfg(target_os = "windows")]
pub mod computer_use;
pub use element::{OcrElement, SerializableUIElement, UIElement, UIElementAttributes};
pub use errors::AutomationError;
pub use locator::Locator;
pub use screenshot::{
get_cursor_position, ScreenshotError, ScreenshotResult, DEFAULT_MAX_DIMENSION,
};
pub use selector::Selector;
pub use tokio_util::sync::CancellationToken;
pub use tree_formatter::{
format_clustered_tree_from_caches, format_ocr_tree_as_compact_yaml,
format_tree_as_compact_yaml, format_ui_node_as_compact_yaml, serializable_to_ui_node,
ClusteredFormattingResult, ElementSource, OcrFormattingResult, TreeFormattingResult,
UnifiedElement,
};
pub use types::{FontStyle, HighlightHandle, OmniparserItem, TextPosition, VisionElement};
pub use utils::find_pid_for_process;
// Re-export types from computeruse-computer-use crate (cross-platform).
//
// Note: execution requires platform-specific Desktop capabilities; on non-Windows
// many actions are currently UnsupportedOperation, but the types remain available.
pub use computeruse_computer_use::{
call_computer_use_backend, convert_normalized_to_screen, translate_gemini_keys,
ComputerUseActionResponse, ComputerUseFunctionCall, ComputerUsePreviousAction,
ComputerUseResponse, ComputerUseResult, ComputerUseStep, ProgressCallback,
};
// Re-export cross-platform types from platforms
pub use platforms::{OverlayDisplayMode, PropertyLoadingMode, TreeBuildConfig};
// Re-export window manager + overlay helpers.
// On non-Windows these are stubs to keep downstream crates compiling.
pub use platforms::windows::window_manager::{WindowCache, WindowInfo, WindowManager, WindowPlacement};
pub use platforms::windows::{
hide_action_overlay, hide_inspect_overlay, highlight_bounds, is_action_overlay_enabled,
set_action_overlay_enabled, show_action_overlay, show_inspect_overlay, stop_all_highlights,
update_action_overlay_message, ActionOverlayGuard, InspectElement, InspectOverlayHandle,
};
// Cross-platform helper (Windows has a richer implementation).
pub use platforms::is_browser_process;
/// Walk up the element tree to find the parent Window or Pane element.
///
/// This is useful when you have a focused element (e.g., a button inside a window)
/// and need to find the containing window to build a UI tree from.
///
/// # Arguments
/// * `element` - The UIElement to start from
///
/// # Returns
/// The parent Window/Pane element, or None if not found
pub fn find_parent_window(element: &UIElement) -> Option<UIElement> {
let mut current = element.clone();
let start_role = current.role();
let start_name = current.name().unwrap_or_default();
tracing::debug!(
"find_parent_window: starting from element role='{}' name='{}'",
start_role,
start_name
);
// Limit iterations to prevent infinite loops in malformed trees
for depth in 0..100 {
let role = current.role();
let name = current.name().unwrap_or_default();
if role == "Window"
|| role == "Pane"
// macOS Accessibility roles are typically prefixed with "AX"
|| role == "AXWindow"
|| role == "AXPane"
{
tracing::debug!(
"find_parent_window: found window at depth {} - role='{}' name='{}'",
depth,
role,
name
);
return Some(current);
}
match current.parent() {
Ok(Some(parent)) => {
tracing::trace!(
"find_parent_window: depth {} role='{}' -> moving to parent",
depth,
role
);
current = parent;
}
Ok(None) => {
tracing::debug!(
"find_parent_window: reached root at depth {} (role='{}' name='{}') without finding window",
depth,
role,
name
);
return None;
}
Err(e) => {
tracing::warn!(
"find_parent_window: parent() failed at depth {} (role='{}' name='{}'): {}",
depth,
role,
name,
e
);
return None;
}
}
}
tracing::warn!(
"find_parent_window: hit iteration limit without finding window (started from role='{}' name='{}')",
start_role,
start_name
);
None
}
/// Recommend to use any of these: ["Default", "Chrome", "Firefox", "Edge", "Brave", "Opera", "Vivaldi"]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Browser {
Default,
Chrome,
Firefox,
Edge,
Brave,
Opera,
Vivaldi,
Custom(String),
}
/// Type of mouse click to perform
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ClickType {
/// Single left click (default)
Left,
/// Double left click
Double,
/// Single right click
Right,
}
/// Source of indexed elements for click targeting
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum VisionType {
/// UI Automation tree elements
#[default]
UiTree,
/// OCR-detected text elements
Ocr,
/// Omniparser-detected elements
Omniparser,
/// Gemini Vision-detected elements
Gemini,
/// Browser DOM elements
Dom,
}
#[cfg(target_os = "windows")]
pub use platforms::windows::{
convert_uiautomation_element_to_computeruse, get_process_name_by_pid, set_recording_mode,
KNOWN_BROWSER_PROCESS_NAMES,
};
// Define a new struct to hold click result information - move to module level
pub struct ClickResult {
pub method: String,
pub coordinates: Option<(f64, f64)>,
pub details: String,
}
/// Result of text verification after typing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeVerification {
/// Whether verification passed
pub passed: bool,
/// The expected text that was typed
pub expected: String,
/// The actual value read from the element
pub actual: Option<String>,
/// Error message if verification failed
pub error: Option<String>,
}
/// Generic result struct for UI actions with state tracking
pub struct ActionResult {
pub action: String,
pub details: String,
pub data: Option<serde_json::Value>,
/// Verification result for type operations
pub verification: Option<TypeVerification>,
}
/// Holds the output of a terminal command execution
pub struct CommandOutput {
pub exit_status: Option<i32>,
pub stdout: String,
pub stderr: String,
}
/// Result of get_window_tree operation with all computed data
///
/// This struct provides everything needed for UI automation:
/// - Raw UINode tree for programmatic traversal
/// - Formatted output for LLM consumption
/// - Index-to-bounds mapping for click targeting
/// - Metadata about the window/process
#[derive(Debug, Clone)]
#[allow(clippy::type_complexity)]
pub struct WindowTreeResult {
/// The raw UI tree structure
pub tree: UINode,
/// Process ID of the window
pub pid: u32,
/// Whether this is a browser window
pub is_browser: bool,
/// Formatted compact YAML output (if format_output was true)
pub formatted: Option<String>,
/// Mapping of index to (role, name, bounds, selector) for click targeting
/// Key is 1-based index, value is (role, name, (x, y, width, height), selector)
pub index_to_bounds:
std::collections::HashMap<u32, (String, String, (f64, f64, f64, f64), Option<String>)>,
/// Total count of indexed elements (elements with bounds)
pub element_count: u32,
}
/// Options for UI diff capture during action execution
#[derive(Debug, Clone, Default)]
pub struct UiDiffOptions {
/// Maximum depth for tree capture
pub max_depth: Option<usize>,
/// Delay in ms after action for UI to settle (default 1500)
pub settle_delay_ms: Option<u64>,
/// Include detailed element attributes (enabled, focused, etc.)
pub include_detailed_attributes: Option<bool>,
}
/// Result of UI diff capture
#[derive(Debug, Clone)]
pub struct UiDiffResult {
/// The computed diff showing changes (lines starting with + or -)
pub diff: String,
/// Whether any UI changes were detected
pub has_changes: bool,
}
/// Represents a monitor/display device
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Monitor {
/// Unique identifier for the monitor
pub id: String,
/// Human-readable name of the monitor
pub name: String,
/// Whether this is the primary monitor
pub is_primary: bool,
/// Monitor dimensions
pub width: u32,
pub height: u32,
/// Monitor position (top-left corner)
pub x: i32,
pub y: i32,
/// Scale factor (e.g., 1.0 for 100%, 1.25 for 125%)
pub scale_factor: f64,
/// Work area dimensions (screen area excluding taskbar) - Windows only
/// On other platforms, this will be the same as the full monitor dimensions
pub work_area: Option<WorkAreaBounds>,
}
/// Represents the work area bounds (excluding taskbar and docked windows)
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub struct WorkAreaBounds {
pub x: i32,
pub y: i32,
pub width: u32,
pub height: u32,
}
impl Monitor {
/// Capture a screenshot of this monitor
#[instrument(skip(self, desktop))]
pub async fn capture(&self, desktop: &Desktop) -> Result<ScreenshotResult, AutomationError> {
desktop.engine.capture_monitor_by_id(&self.id).await
}
/// Check if this monitor contains the given coordinates
pub fn contains_point(&self, x: i32, y: i32) -> bool {
x >= self.x
&& x < self.x + self.width as i32
&& y >= self.y
&& y < self.y + self.height as i32
}
/// Get the center point of this monitor
pub fn center(&self) -> (i32, i32) {
(
self.x + self.width as i32 / 2,
self.y + self.height as i32 / 2,
)
}
}
/// Represents a node in the UI tree, containing its attributes and children.
#[derive(Clone, Serialize, Deserialize, Default)]
pub struct UINode {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
pub attributes: UIElementAttributes,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub children: Vec<UINode>,
/// Chained selector path from root to this node (e.g., "role:Window && name:App >> role:Button && name:Submit")
#[serde(skip_serializing_if = "Option::is_none")]
pub selector: Option<String>,
}
impl fmt::Debug for UINode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.debug_with_depth(f, 0, 100)
}
}
impl UINode {
/// Helper method for debug formatting with depth control
fn debug_with_depth(
&self,
f: &mut fmt::Formatter<'_>,
current_depth: usize,
max_depth: usize,
) -> fmt::Result {
let mut debug_struct = f.debug_struct("UINode");
debug_struct.field("attributes", &self.attributes);
if !self.children.is_empty() {
if current_depth < max_depth {
debug_struct.field(
"children",
&DebugChildrenWithDepth {
children: &self.children,
current_depth,
max_depth,
},
);
} else {
debug_struct.field(
"children",
&format!("[{} children (depth limit reached)]", self.children.len()),
);
}
}
debug_struct.finish()
}
}
/// Helper struct for debug formatting children with depth control
struct DebugChildrenWithDepth<'a> {
children: &'a Vec<UINode>,
current_depth: usize,
max_depth: usize,
}
impl fmt::Debug for DebugChildrenWithDepth<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut list = f.debug_list();
// Show ALL children, no limit
for child in self.children.iter() {
list.entry(&DebugNodeWithDepth {
node: child,
current_depth: self.current_depth + 1,
max_depth: self.max_depth,
});
}
list.finish()
}
}
/// Helper struct for debug formatting a single node with depth control
struct DebugNodeWithDepth<'a> {
node: &'a UINode,
current_depth: usize,
max_depth: usize,
}
impl fmt::Debug for DebugNodeWithDepth<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.node
.debug_with_depth(f, self.current_depth, self.max_depth)
}
}
// Removed struct ScreenshotResult (moved to screenshot.rs)
/// Cached element bounds for index-based click targeting
/// Stored as (role/label, name/text, bounds, optional_selector)
type UiaBoundsCache = HashMap<u32, (String, String, (f64, f64, f64, f64), Option<String>)>;
/// OCR bounds cache: (text, bounds)
type OcrBoundsCache = HashMap<u32, (String, (f64, f64, f64, f64))>;
/// DOM bounds cache: (tag, id, bounds)
type DomBoundsCache = HashMap<u32, (String, String, (f64, f64, f64, f64))>;
/// The main entry point for UI automation
pub struct Desktop {
engine: Arc<dyn platforms::AccessibilityEngine>,
/// Cancellation token for stopping execution (wrapped in RwLock to allow reset)
cancellation_token: Arc<RwLock<CancellationToken>>,
/// Cache for UI Automation tree element bounds (index → bounds info)
uia_cache: Arc<Mutex<UiaBoundsCache>>,
/// Cache for OCR element bounds
ocr_cache: Arc<Mutex<OcrBoundsCache>>,
/// Cache for Omniparser element bounds
omniparser_cache: Arc<Mutex<HashMap<u32, OmniparserItem>>>,
/// Cache for Gemini Vision element bounds
vision_cache: Arc<Mutex<HashMap<u32, VisionElement>>>,
/// Cache for DOM element bounds
dom_cache: Arc<Mutex<DomBoundsCache>>,
}
impl Desktop {
#[instrument(skip(use_background_apps, activate_app))]
pub fn new(use_background_apps: bool, activate_app: bool) -> Result<Self, AutomationError> {
let engine = platforms::create_engine(use_background_apps, activate_app)?;
Ok(Self {
engine,
cancellation_token: Arc::new(RwLock::new(CancellationToken::new())),
uia_cache: Arc::new(Mutex::new(HashMap::new())),
ocr_cache: Arc::new(Mutex::new(HashMap::new())),
omniparser_cache: Arc::new(Mutex::new(HashMap::new())),
vision_cache: Arc::new(Mutex::new(HashMap::new())),
dom_cache: Arc::new(Mutex::new(HashMap::new())),
})
}
/// Initializet the desktop without arguments
///
/// This is a convenience method that calls `new` with default arguments.
///
/// # Examples
///
/// ```
/// use computeruse::Desktop;
/// let desktop = Desktop::new_default()?;
/// # Ok::<(), computeruse::AutomationError>(())
/// ```
pub fn new_default() -> Result<Self, AutomationError> {
Self::new(false, false)
}
/// Gets the root element representing the entire desktop.
///
/// This is the top-level element that contains all applications, windows,
/// and UI elements on the desktop. You can use it as a starting point for
/// element searches.
///
/// # Examples
///
/// ```
/// use computeruse::Desktop;
/// let desktop = Desktop::new(false, false)?;
/// let root = desktop.root();
/// println!("Root element ID: {:?}", root.id());
/// # Ok::<(), computeruse::AutomationError>(())
/// ```
pub fn root(&self) -> UIElement {
self.engine.get_root_element()
}
#[instrument(level = "debug", skip(self, selector))]
pub fn locator(&self, selector: impl Into<Selector>) -> Locator {
let selector = selector.into();
Locator::new(self.engine.clone(), selector)
}
#[instrument(skip(self))]
pub fn focused_element(&self) -> Result<UIElement, AutomationError> {
self.engine.get_focused_element()
}
#[instrument(skip(self))]
pub fn applications(&self) -> Result<Vec<UIElement>, AutomationError> {
self.engine.get_applications()
}
#[instrument(skip(self, name))]
pub fn application(&self, name: &str) -> Result<UIElement, AutomationError> {
self.engine.get_application_by_name(name)
}
#[instrument(skip(self, app_name))]
pub fn open_application(&self, app_name: &str) -> Result<UIElement, AutomationError> {
self.engine.open_application(app_name)
}
#[instrument(skip(self, app_name))]
pub fn activate_application(&self, app_name: &str) -> Result<(), AutomationError> {
self.engine.activate_application(app_name)
}
#[instrument(skip(self, url, browser))]
pub fn open_url(
&self,
url: &str,
browser: Option<Browser>,
) -> Result<UIElement, AutomationError> {
self.engine.open_url(url, browser)
}
#[instrument(skip(self, file_path))]
pub fn open_file(&self, file_path: &str) -> Result<(), AutomationError> {
self.engine.open_file(file_path)
}
#[instrument(skip(self, windows_command, unix_command))]
pub async fn run_command(
&self,
windows_command: Option<&str>,
unix_command: Option<&str>,
) -> Result<CommandOutput, AutomationError> {
self.engine.run_command(windows_command, unix_command).await
}
/// Execute a shell command using GitHub Actions-style syntax
///
/// # Arguments
/// * `command` - The command to run (can be single or multi-line)
/// * `shell` - Optional shell to use (defaults to PowerShell on Windows, bash on Unix)
/// * `working_directory` - Optional working directory for the command
///
/// # Examples
/// ```no_run
/// use computeruse::Desktop;
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let output = desktop.run(
/// "echo 'Hello, World!'",
/// None,
/// None
/// ).await.unwrap();
/// println!("Output: {}", output.stdout);
/// }
/// ```
#[instrument(skip(self, command))]
pub async fn run(
&self,
command: &str,
shell: Option<&str>,
working_directory: Option<&str>,
) -> Result<CommandOutput, AutomationError> {
// Determine which shell to use based on platform and user preference
let (windows_cmd, unix_cmd) = if cfg!(target_os = "windows") {
let shell = shell.unwrap_or("powershell");
let command_with_cd = if let Some(cwd) = working_directory {
match shell {
"cmd" => format!("cd /d \"{cwd}\" && {command}"),
"powershell" | "pwsh" => format!("cd '{cwd}'; {command}"),
_ => command.to_string(),
}
} else {
command.to_string()
};
let windows_cmd = match shell {
"bash" => format!("bash -c \"{}\"", command_with_cd.replace('\"', "\\\"")),
"sh" => format!("sh -c \"{}\"", command_with_cd.replace('\"', "\\\"")),
"cmd" => format!("cmd /c \"{command_with_cd}\""),
"powershell" | "pwsh" => command_with_cd,
_ => command_with_cd,
};
(Some(windows_cmd), None)
} else {
let shell = shell.unwrap_or("bash");
let command_with_cd = if let Some(cwd) = working_directory {
format!("cd '{cwd}' && {command}")
} else {
command.to_string()
};
let unix_cmd = match shell {
"python" => format!("python -c \"{}\"", command_with_cd.replace('\"', "\\\"")),
"node" => format!("node -e \"{}\"", command_with_cd.replace('\"', "\\\"")),
_ => command_with_cd,
};
(None, Some(unix_cmd))
};
self.engine
.run_command(windows_cmd.as_deref(), unix_cmd.as_deref())
.await
}
// ============== NEW MONITOR ABSTRACTIONS ==============
/// List all available monitors/displays
///
/// Returns a vector of Monitor structs containing information about each display,
/// including dimensions, position, scale factor, and whether it's the primary monitor.
///
/// # Examples
///
/// ```no_run
/// use computeruse::Desktop;
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let monitors = desktop.list_monitors().await.unwrap();
/// for monitor in monitors {
/// println!("Monitor: {} ({}x{})", monitor.name, monitor.width, monitor.height);
/// }
/// }
/// ```
#[instrument(skip(self))]
pub async fn list_monitors(&self) -> Result<Vec<Monitor>, AutomationError> {
self.engine.list_monitors().await
}
/// Get the primary monitor
///
/// Returns the monitor marked as primary in the system settings.
///
/// # Examples
///
/// ```no_run
/// use computeruse::Desktop;
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let primary = desktop.get_primary_monitor().await.unwrap();
/// println!("Primary monitor: {}", primary.name);
/// }
/// ```
#[instrument(skip(self))]
pub async fn get_primary_monitor(&self) -> Result<Monitor, AutomationError> {
self.engine.get_primary_monitor().await
}
/// Get the monitor containing the currently focused window
///
/// Returns the monitor that contains the currently active/focused window.
///
/// # Examples
///
/// ```no_run
/// use computeruse::Desktop;
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let active = desktop.get_active_monitor().await.unwrap();
/// println!("Active monitor: {}", active.name);
/// }
/// ```
#[instrument(skip(self))]
pub async fn get_active_monitor(&self) -> Result<Monitor, AutomationError> {
self.engine.get_active_monitor().await
}
/// Get a monitor by its ID
///
/// # Examples
///
/// ```no_run
/// use computeruse::Desktop;
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let monitor = desktop.get_monitor_by_id("monitor_id").await.unwrap();
/// }
/// ```
#[instrument(skip(self, id))]
pub async fn get_monitor_by_id(&self, id: &str) -> Result<Monitor, AutomationError> {
self.engine.get_monitor_by_id(id).await
}
/// Get a monitor by its name
///
/// # Examples
///
/// ```no_run
/// use computeruse::Desktop;
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let monitor = desktop.get_monitor_by_name("Dell Monitor").await.unwrap();
/// }
/// ```
#[instrument(skip(self, name))]
pub async fn get_monitor_by_name(&self, name: &str) -> Result<Monitor, AutomationError> {
self.engine.get_monitor_by_name(name).await
}
/// Capture a screenshot of a specific monitor
///
/// # Examples
///
/// ```no_run
/// use computeruse::Desktop;
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let monitor = desktop.get_primary_monitor().await.unwrap();
/// let screenshot = desktop.capture_monitor(&monitor).await.unwrap();
/// }
/// ```
#[instrument(skip(self, monitor))]
pub async fn capture_monitor(
&self,
monitor: &Monitor,
) -> Result<ScreenshotResult, AutomationError> {
let mut result = self.engine.capture_monitor_by_id(&monitor.id).await?;
result.monitor = Some(monitor.clone());
Ok(result)
}
/// Capture screenshots of all monitors
///
/// Returns a vector of (Monitor, ScreenshotResult) pairs for each display.
///
/// # Examples
///
/// ```no_run
/// use computeruse::Desktop;
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let screenshots = desktop.capture_all_monitors().await.unwrap();
/// for (monitor, screenshot) in screenshots {
/// println!("Captured monitor: {} ({}x{})", monitor.name, screenshot.width, screenshot.height);
/// }
/// }
/// ```
#[instrument(skip(self))]
pub async fn capture_all_monitors(
&self,
) -> Result<Vec<(Monitor, ScreenshotResult)>, AutomationError> {
let monitors = self.list_monitors().await?;
let mut results = Vec::new();
for monitor in monitors {
match self.capture_monitor(&monitor).await {
Ok(screenshot) => results.push((monitor, screenshot)),
Err(e) => {
error!("Failed to capture monitor {}: {}", monitor.name, e);
// Continue with other monitors rather than failing completely
}
}
}
if results.is_empty() {
return Err(AutomationError::PlatformError(
"Failed to capture any monitors".to_string(),
));
}
Ok(results)
}
/// Capture a screenshot of a window by process name
///
/// Finds the first window matching the given process name and captures its screenshot.
/// Process name matching is case-insensitive and uses substring matching.
///
/// # Arguments
/// * `process` - Process name to match (e.g., "chrome", "notepad", "code")
///
/// # Examples
///
/// ```no_run
/// use computeruse::Desktop;
/// fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let screenshot = desktop.capture_window_by_process("notepad").unwrap();
/// // Convert to base64 PNG for LLM consumption
/// let base64_png = screenshot.to_base64_png_resized(Some(1920)).unwrap();
/// }
/// ```
#[instrument(skip(self))]
pub fn capture_window_by_process(
&self,
process: &str,
) -> Result<ScreenshotResult, AutomationError> {
let apps = self.applications()?;
let process_lower = process.to_lowercase();
// Use sysinfo for reliable process name lookup (same as get_applications_and_windows_list)
let mut system = System::new();
system.refresh_processes(ProcessesToUpdate::All, true);
// Find matching window by process name using sysinfo
let window_element = apps.into_iter().find(|app| {
let pid = app.process_id().unwrap_or(0);
if pid > 0 {
system
.process(sysinfo::Pid::from_u32(pid))
.map(|p| {
p.name()
.to_string_lossy()
.to_lowercase()
.contains(&process_lower)
})
.unwrap_or(false)
} else {
false
}
});
let window_element = window_element.ok_or_else(|| {
AutomationError::ElementNotFound(format!("No window found for process '{}'", process))
})?;
window_element.capture()
}
// ============== DEPRECATED METHODS ==============
// ============== END DEPRECATED METHODS ==============
#[instrument(skip(self, image_path))]
pub async fn ocr_image_path(&self, image_path: &str) -> Result<String, AutomationError> {
self.engine.ocr_image_path(image_path).await
}
#[instrument(skip(self, screenshot))]
pub async fn ocr_screenshot(
&self,
screenshot: &ScreenshotResult,
) -> Result<String, AutomationError> {
self.engine.ocr_screenshot(screenshot).await
}
/// OCR on screenshot with bounding boxes - returns structured OCR elements with absolute screen coordinates
/// Window coordinates are used to convert OCR bounding boxes to absolute screen positions
///
/// # Arguments
/// * `screenshot` - The screenshot to perform OCR on
/// * `window_x` - X offset of the window on screen in logical coordinates
/// * `window_y` - Y offset of the window on screen in logical coordinates
/// * `dpi_scale_x` - DPI scale factor for X (screenshot_width / window_logical_width)
/// * `dpi_scale_y` - DPI scale factor for Y (screenshot_height / window_logical_height)
#[instrument(skip(self, screenshot))]
pub fn ocr_screenshot_with_bounds(
&self,
screenshot: &ScreenshotResult,
window_x: f64,
window_y: f64,
dpi_scale_x: f64,
dpi_scale_y: f64,
) -> Result<OcrElement, AutomationError> {
self.engine.ocr_screenshot_with_bounds(
screenshot,
window_x,
window_y,
dpi_scale_x,
dpi_scale_y,
)
}
/// Click at absolute screen coordinates
/// This is useful for clicking on OCR-detected text elements
/// If `restore_cursor` is true, the cursor position will be restored after the click
#[instrument(skip(self))]
pub fn click_at_coordinates(
&self,
x: f64,
y: f64,
restore_cursor: bool,
) -> Result<(), AutomationError> {
self.engine.click_at_coordinates(x, y, restore_cursor)
}
/// Click at absolute screen coordinates with specified click type (left, double, right)
/// This is useful for clicking on OCR-detected text elements with different click types
/// If `restore_cursor` is true, the cursor position will be restored after the click
#[instrument(skip(self))]
pub fn click_at_coordinates_with_type(
&self,
x: f64,
y: f64,
click_type: ClickType,
restore_cursor: bool,
) -> Result<(), AutomationError> {
self.engine
.click_at_coordinates_with_type(x, y, click_type, restore_cursor)
}
/// Click within element bounds at a specified position (percentage-based).
///
/// This is useful for clicking on elements from UI tree, OCR, omniparser, gemini vision, or DOM
/// without needing an element reference - just the bounds.
///
/// # Arguments
/// * `bounds` - Element bounds as (x, y, width, height)
/// * `click_position` - Optional (x_percentage, y_percentage) within bounds. Defaults to center (50, 50)
/// * `click_type` - Type of click: Left, Double, or Right
/// * `restore_cursor` - If true, cursor position will be restored after the click
///
/// # Returns
/// ClickResult with coordinates and method details
#[instrument(skip(self))]
pub fn click_at_bounds(
&self,
bounds: (f64, f64, f64, f64),
click_position: Option<(u8, u8)>,
click_type: ClickType,
restore_cursor: bool,
) -> Result<ClickResult, AutomationError> {
let (x_pct, y_pct) = click_position.unwrap_or((50, 50));
let x = bounds.0 + bounds.2 * x_pct as f64 / 100.0;
let y = bounds.1 + bounds.3 * y_pct as f64 / 100.0;
self.engine
.click_at_coordinates_with_type(x, y, click_type, restore_cursor)?;
Ok(ClickResult {
method: "bounds".to_string(),
coordinates: Some((x, y)),
details: format!(
"Clicked at {}%,{}% within bounds ({}, {}, {}, {})",
x_pct, y_pct, bounds.0, bounds.1, bounds.2, bounds.3
),
})
}
/// Click on an element by its index from the last tree/vision query.
///
/// This looks up cached bounds from the appropriate cache based on vision_type,
/// then clicks at the specified position within those bounds.
///
/// # Arguments
/// * `index` - 1-based index from the tree/vision output (e.g., #1, #2)
/// * `vision_type` - Source of the index: UiTree, Ocr, Omniparser, Gemini, or Dom
/// * `click_position` - Optional (x_percentage, y_percentage) within bounds. Defaults to center (50, 50)
/// * `click_type` - Type of click: Left, Double, or Right
/// * `restore_cursor` - If true, cursor position will be restored after the click
///
/// # Returns
/// ClickResult with coordinates, element info, and method details
///
/// # Errors
/// Returns error if index not found in cache (call get_window_tree/get_ocr/etc first)
#[instrument(skip(self))]
pub fn click_by_index(
&self,
index: u32,
vision_type: VisionType,
click_position: Option<(u8, u8)>,
click_type: ClickType,
restore_cursor: bool,
) -> Result<ClickResult, AutomationError> {
let (label, bounds) = match vision_type {
VisionType::UiTree => {
let cache = self.uia_cache.lock().map_err(|e| {
AutomationError::Internal(format!("Failed to lock UIA cache: {}", e))
})?;
let entry = cache.get(&index).ok_or_else(|| {
AutomationError::ElementNotFound(format!(
"UI tree index #{} not found. Call get_window_tree first.",
index
))
})?;
let label = if entry.1.is_empty() {
entry.0.clone()
} else {
format!("{}: {}", entry.0, entry.1)
};
(label, entry.2)
}
VisionType::Ocr => {
let cache = self.ocr_cache.lock().map_err(|e| {
AutomationError::Internal(format!("Failed to lock OCR cache: {}", e))
})?;
let entry = cache.get(&index).ok_or_else(|| {
AutomationError::ElementNotFound(format!(
"OCR index #{} not found. Call ocr methods first.",
index
))
})?;
(entry.0.clone(), entry.1)
}
VisionType::Omniparser => {
let cache = self.omniparser_cache.lock().map_err(|e| {
AutomationError::Internal(format!("Failed to lock Omniparser cache: {}", e))
})?;
let item = cache.get(&index).ok_or_else(|| {
AutomationError::ElementNotFound(format!(
"Omniparser index #{} not found. Call omniparser methods first.",
index
))
})?;
let box_2d = item.box_2d.ok_or_else(|| {
AutomationError::Internal(format!("Omniparser index #{} has no bounds", index))
})?;
// Convert [x_min, y_min, x_max, y_max] to (x, y, width, height)
let bounds = (
box_2d[0],
box_2d[1],
box_2d[2] - box_2d[0],
box_2d[3] - box_2d[1],
);
(item.label.clone(), bounds)
}
VisionType::Gemini => {
let cache = self.vision_cache.lock().map_err(|e| {
AutomationError::Internal(format!("Failed to lock Vision cache: {}", e))
})?;
let item = cache.get(&index).ok_or_else(|| {
AutomationError::ElementNotFound(format!(
"Gemini index #{} not found. Call gemini vision methods first.",
index
))
})?;
let box_2d = item.box_2d.ok_or_else(|| {
AutomationError::Internal(format!("Gemini index #{} has no bounds", index))
})?;
// Convert [x_min, y_min, x_max, y_max] to (x, y, width, height)
let bounds = (
box_2d[0],
box_2d[1],
box_2d[2] - box_2d[0],
box_2d[3] - box_2d[1],
);
(item.element_type.clone(), bounds)
}
VisionType::Dom => {
let cache = self.dom_cache.lock().map_err(|e| {
AutomationError::Internal(format!("Failed to lock DOM cache: {}", e))
})?;
let entry = cache.get(&index).ok_or_else(|| {
AutomationError::ElementNotFound(format!(
"DOM index #{} not found. Call DOM methods first.",
index
))
})?;
let label = if entry.1.is_empty() {
entry.0.clone()
} else {
format!("{}: {}", entry.0, entry.1)
};
(label, entry.2)
}
};
let (x_pct, y_pct) = click_position.unwrap_or((50, 50));
let x = bounds.0 + bounds.2 * x_pct as f64 / 100.0;
let y = bounds.1 + bounds.3 * y_pct as f64 / 100.0;
self.engine
.click_at_coordinates_with_type(x, y, click_type, restore_cursor)?;
Ok(ClickResult {
method: "index".to_string(),
coordinates: Some((x, y)),
details: format!(
"Clicked #{} [{}] at {}%,{}% (bounds: {:.0},{:.0},{:.0},{:.0})",
index, label, x_pct, y_pct, bounds.0, bounds.1, bounds.2, bounds.3
),
})
}
/// Populate the OCR cache for index-based clicking.
/// Call this after performing OCR to enable click_by_index with VisionType::Ocr.
///
/// # Arguments
/// * `bounds_map` - Map of index to (text, bounds) from OCR formatting result
#[allow(clippy::type_complexity)]
pub fn populate_ocr_cache(&self, bounds_map: HashMap<u32, (String, (f64, f64, f64, f64))>) {
if let Ok(mut cache) = self.ocr_cache.lock() {
cache.clear();
cache.extend(bounds_map);
debug!("Populated OCR cache with {} elements", cache.len());
}
}
/// Populate the Omniparser cache for index-based clicking.
/// Call this after performing Omniparser to enable click_by_index with VisionType::Omniparser.
///
/// # Arguments
/// * `items` - Map of index to OmniparserItem from Omniparser result
pub fn populate_omniparser_cache(&self, items: HashMap<u32, OmniparserItem>) {
if let Ok(mut cache) = self.omniparser_cache.lock() {
cache.clear();
cache.extend(items);
debug!("Populated Omniparser cache with {} elements", cache.len());
}
}
/// Populate the Gemini vision cache for index-based clicking.
/// Call this after performing Gemini vision to enable click_by_index with VisionType::Gemini.
///
/// # Arguments
/// * `items` - Map of index to VisionElement from Gemini result
pub fn populate_vision_cache(&self, items: HashMap<u32, VisionElement>) {
if let Ok(mut cache) = self.vision_cache.lock() {
cache.clear();
cache.extend(items);
debug!("Populated Vision cache with {} elements", cache.len());
}
}
/// Populate the DOM cache for index-based clicking.
/// Call this after capturing browser DOM to enable click_by_index with VisionType::Dom.
///
/// # Arguments
/// * `bounds_map` - Map of index to (tag, id, bounds) from DOM capture
#[allow(clippy::type_complexity)]
pub fn populate_dom_cache(
&self,
bounds_map: HashMap<u32, (String, String, (f64, f64, f64, f64))>,
) {
if let Ok(mut cache) = self.dom_cache.lock() {
cache.clear();
cache.extend(bounds_map);
debug!("Populated DOM cache with {} elements", cache.len());
}
}
/// Clear all vision caches.
/// Call this when starting a new session or switching contexts.
pub fn clear_vision_caches(&self) {
if let Ok(mut cache) = self.uia_cache.lock() {
cache.clear();
}
if let Ok(mut cache) = self.ocr_cache.lock() {
cache.clear();
}
if let Ok(mut cache) = self.omniparser_cache.lock() {
cache.clear();
}
if let Ok(mut cache) = self.vision_cache.lock() {
cache.clear();
}
if let Ok(mut cache) = self.dom_cache.lock() {
cache.clear();
}
debug!("Cleared all vision caches");
}
#[instrument(skip(self, title))]
pub fn activate_browser_window_by_title(&self, title: &str) -> Result<(), AutomationError> {
self.engine.activate_browser_window_by_title(title)
}
#[instrument(skip(self))]
pub async fn get_current_browser_window(&self) -> Result<UIElement, AutomationError> {
self.engine.get_current_browser_window().await
}
/// Execute JavaScript in the currently focused browser tab.
/// Automatically finds the active browser window and executes the script.
///
/// This method respects cancellation - if `stop_execution()` is called,
/// the operation will be interrupted and return an error.
#[instrument(skip(self, script))]
pub async fn execute_browser_script(&self, script: &str) -> Result<String, AutomationError> {
let browser_window = self.engine.get_current_browser_window().await?;
let cancel_token = self.cancellation_token();
tokio::select! {
result = browser_window.execute_browser_script(script) => result,
_ = cancel_token.cancelled() => {
Err(AutomationError::OperationCancelled("Browser script execution cancelled by stop_execution".into()))
}
}
}
/// Close a browser tab safely using the browser extension
///
/// This method can identify the tab to close by:
/// - tab_id: Close a specific tab by its Chrome tab ID
/// - url: Find and close a tab matching this URL
/// - title: Find and close a tab matching this title
/// - If none provided, closes the currently active tab
///
/// Returns information about the closed tab for verification.
/// Returns None if no extension is connected or tab couldn't be found.
///
/// # Safety
/// - Will NOT close protected browser pages (chrome://, about:, etc.)
/// - Returns the closed tab's URL/title so you can verify the right tab was closed
///
/// # Examples
/// ```no_run
/// use computeruse::Desktop;
/// use std::time::Duration;
///
/// async fn example() {
/// let desktop = Desktop::new_default().unwrap();
///
/// // Close by URL
/// let result = desktop.close_tab(None, Some("example.com"), None).await;
///
/// // Close by title
/// let result = desktop.close_tab(None, None, Some("My Tab")).await;
///
/// // Close active tab
/// let result = desktop.close_tab(None, None, None).await;
/// }
/// ```
#[instrument(skip(self))]
pub async fn close_tab(
&self,
tab_id: Option<i32>,
url: Option<&str>,
title: Option<&str>,
) -> Result<Option<extension_bridge::CloseTabResult>, AutomationError> {
use std::time::Duration;
extension_bridge::try_close_tab(tab_id, url, title, Duration::from_secs(10)).await
}
#[instrument(skip(self))]
pub async fn get_current_window(&self) -> Result<UIElement, AutomationError> {
self.engine.get_current_window().await
}
#[instrument(skip(self))]
pub async fn get_current_application(&self) -> Result<UIElement, AutomationError> {
self.engine.get_current_application().await
}
#[instrument(skip(self, pid, title, config))]
pub fn get_window_tree(
&self,
pid: u32,
title: Option<&str>,
config: Option<crate::platforms::TreeBuildConfig>,
) -> Result<UINode, AutomationError> {
let tree_config = config.unwrap_or_default();
self.engine.get_window_tree(pid, title, tree_config)
}
/// Build UI tree directly from a UIElement
///
/// This avoids the PID-based window enumeration which can fail during
/// transient UI Automation states. Use when you already have a UIElement.
///
/// # Arguments
/// * `element` - The UIElement to build tree from
/// * `config` - Optional tree building configuration
///
/// # Returns
/// Complete UI tree starting from the provided element
#[instrument(skip(self, element, config))]
pub fn get_tree_from_element(
&self,
element: &UIElement,
config: Option<crate::platforms::TreeBuildConfig>,
) -> Result<UINode, AutomationError> {
let tree_config = config.unwrap_or_default();
self.engine.get_tree_from_element(element, tree_config)
}
/// Find the parent window of an element and build tree from it
///
/// Walks up the element tree to find Window/Pane, then builds the UI tree.
/// This is the recommended method when you have a focused element from an event,
/// as it avoids desktop enumeration which can fail during transient states.
///
/// # Arguments
/// * `element` - The UIElement to start from (e.g., focused element)
/// * `config` - Optional tree building configuration
///
/// # Returns
/// Complete UI tree starting from the parent window
#[instrument(skip(self, element, config))]
pub fn get_window_tree_from_element(
&self,
element: &UIElement,
config: Option<crate::platforms::TreeBuildConfig>,
) -> Result<UINode, AutomationError> {
let window = find_parent_window(element).ok_or_else(|| {
AutomationError::ElementNotFound("Could not find parent window for element".to_string())
})?;
tracing::info!(
"Found parent window: '{}' (role: {})",
window.name().unwrap_or_default(),
window.role()
);
self.get_tree_from_element(&window, config)
}
/// Get the UI tree with full result including formatting and bounds mapping
///
/// This is the recommended method for getting window trees when you need:
/// - Formatted YAML output for LLM consumption
/// - Index-to-bounds mapping for click targeting
/// - Browser detection
///
/// # Arguments
/// * `pid` - Process ID of the target application
/// * `title` - Optional window title filter
/// * `config` - Tree building configuration (format_output controls formatted output)
///
/// # Returns
/// `WindowTreeResult` containing the tree, formatted output, and bounds mapping
#[instrument(skip(self, pid, title, config))]
pub fn get_window_tree_result(
&self,
pid: u32,
title: Option<&str>,
config: Option<crate::platforms::TreeBuildConfig>,
) -> Result<WindowTreeResult, AutomationError> {
let tree_config = config.unwrap_or_default();
let format_output = tree_config.format_output;
// Get the raw tree
let tree = self.engine.get_window_tree(pid, title, tree_config)?;
// Check if browser process
let is_browser = crate::platforms::is_browser_process(pid);
// Format the tree and get bounds mapping if requested
let (formatted, index_to_bounds, element_count) = if format_output {
let result = format_ui_node_as_compact_yaml(&tree, 0);
(
Some(result.formatted),
result.index_to_bounds,
result.element_count,
)
} else {
(None, HashMap::new(), 0)
};
// Populate the UIA cache for index-based clicking
if !index_to_bounds.is_empty() {
if let Ok(mut cache) = self.uia_cache.lock() {
cache.clear();
cache.extend(index_to_bounds.clone());
debug!("Populated UIA cache with {} elements", cache.len());
}
}
Ok(WindowTreeResult {
tree,
pid,
is_browser,
formatted,
index_to_bounds,
element_count,
})
}
/// Get the UI tree with full result, with async support for from_selector
///
/// This method extends `get_window_tree_result` with support for `from_selector`
/// in the config, which allows building a subtree starting from a specific element
/// instead of the full window.
///
/// # Arguments
/// * `pid` - Process ID of the target application
/// * `title` - Optional window title filter
/// * `config` - Tree building configuration. Set `from_selector` to scope the tree.
///
/// # Returns
/// `WindowTreeResult` containing the tree (or subtree), formatted output, and bounds mapping
#[instrument(skip(self, pid, title, config))]
pub async fn get_window_tree_result_async(
&self,
pid: u32,
title: Option<&str>,
config: Option<crate::platforms::TreeBuildConfig>,
) -> Result<WindowTreeResult, AutomationError> {
let tree_config = config.unwrap_or_default();
let format_output = tree_config.format_output;
let from_selector = tree_config.from_selector.clone();
let max_depth = tree_config.max_depth.unwrap_or(30);
// If from_selector is specified, find the element and build subtree from it
if let Some(selector_str) = from_selector {
// Find app element by PID
let apps = self.applications()?;
let app_element = apps
.into_iter()
.find(|app| app.process_id().ok() == Some(pid))
.ok_or_else(|| {
AutomationError::ElementNotFound(format!(
"No application found with PID {}",
pid
))
})?;
// Find element by selector within the app
let selector = Selector::from(selector_str.as_str());
let locator = app_element.locator(selector)?;
let element = locator
.first(Some(std::time::Duration::from_millis(2000)))
.await?;
// Build subtree from this element
let serializable_tree = element.to_serializable_tree(max_depth);
let tree = serializable_to_ui_node(&serializable_tree);
// Check if browser process
let is_browser = crate::platforms::is_browser_process(pid);
// Format the tree and get bounds mapping if requested
let (formatted, index_to_bounds, element_count) = if format_output {
let result = format_tree_as_compact_yaml(&serializable_tree, 0);
(
Some(result.formatted),
result.index_to_bounds,
result.element_count,
)
} else {
(None, HashMap::new(), 0)
};
// Populate the UIA cache for index-based clicking
if !index_to_bounds.is_empty() {
if let Ok(mut cache) = self.uia_cache.lock() {
cache.clear();
cache.extend(index_to_bounds.clone());
debug!(
"Populated UIA cache with {} elements (from_selector)",
cache.len()
);
}
}
return Ok(WindowTreeResult {
tree,
pid,
is_browser,
formatted,
index_to_bounds,
element_count,
});
}
// No from_selector - use the sync method
self.get_window_tree_result(pid, title, Some(tree_config))
}
/// Get the UI tree for all open applications in parallel.
///
/// This function retrieves the UI hierarchy for every running application
/// on the desktop. It processes applications in parallel for better performance.
///
/// # Examples
///
/// ```no_run
/// use computeruse::Desktop;
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// let app_trees = desktop.get_all_applications_tree().await.unwrap();
/// for tree in app_trees {
/// println!("Application Tree: {:#?}", tree);
/// }
/// }
/// ```
/// This method respects cancellation - if `stop_execution()` is called,
/// the operation will be interrupted and return an error.
#[instrument(skip(self))]
pub async fn get_all_applications_tree(&self) -> Result<Vec<UINode>, AutomationError> {
let applications = self.applications()?;
let futures = applications.into_iter().map(|app| {
let desktop = self.clone();
tokio::task::spawn_blocking(move || {
let pid = match app.process_id() {
Ok(pid) if pid > 0 => pid,
_ => return None, // Skip apps with invalid or zero/negative PIDs
};
// TODO: tbh not sure it cannot lead to crash to run this in threads on windows :)
match desktop.get_window_tree(pid, None, None) {
Ok(tree) => {
if !tree.children.is_empty() || tree.attributes.name.is_some() {
Some(tree)
} else {
None
}
}
Err(e) => {
let app_name = app.name().unwrap_or_else(|| "Unknown".to_string());
tracing::warn!(
"Could not get window tree for app '{}' (PID: {}): {}",
app_name,
pid,
e
);
None
}
}
})
});
// Use select to allow cancellation while waiting for all futures
let cancel_token = self.cancellation_token();
tokio::select! {
results = futures::future::join_all(futures) => {
let trees: Vec<UINode> = results
.into_iter()
.filter_map(|res| match res {
Ok(Some(tree)) => Some(tree),
Ok(None) => None,
Err(e) => {
error!("A task for getting a window tree panicked: {}", e);
None
}
})
.collect();
Ok(trees)
}
_ = cancel_token.cancelled() => {
Err(AutomationError::OperationCancelled("get_all_applications_tree cancelled by stop_execution".into()))
}
}
}
/// Get all window elements for a given application by name
#[instrument(skip(self, app_name))]
pub async fn windows_for_application(
&self,
app_name: &str,
) -> Result<Vec<UIElement>, AutomationError> {
// 1. Find the application element
let app_element = match self.application(app_name) {
Ok(app) => app,
Err(e) => {
error!("Application '{}' not found: {}", app_name, e);
return Err(e);
}
};
// 2. Get children of the application element
let children = match app_element.children() {
Ok(ch) => ch,
Err(e) => {
error!(
"Failed to get children for application '{}': {}",
app_name, e
);
return Err(e);
}
};
// 3. Filter children to find windows (cross-platform)
let windows: Vec<UIElement> = children
.into_iter()
.filter(|el| {
let role = el.role().to_lowercase();
#[cfg(target_os = "macos")]
{
role == "axwindow" || role == "window"
}
#[cfg(target_os = "windows")]
{
role == "window"
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
{
// Fallback: just look for 'window' role
role == "window"
}
})
.collect();
debug!(
window_count = windows.len(),
"Found windows for application '{}'", app_name
);
Ok(windows)
}
pub async fn press_key(&self, key: &str) -> Result<(), AutomationError> {
self.engine.press_key(key)
}
/// Delay execution for a specified number of milliseconds.
/// Useful for waiting between actions to ensure UI stability.
///
/// This method respects cancellation - if `stop_execution()` is called,
/// the delay will be interrupted and return an error.
pub async fn delay(&self, delay_ms: u64) -> Result<(), AutomationError> {
let cancel_token = self.cancellation_token();
tokio::select! {
_ = tokio::time::sleep(std::time::Duration::from_millis(delay_ms)) => Ok(()),
_ = cancel_token.cancelled() => {
Err(AutomationError::OperationCancelled("Delay cancelled by stop_execution".into()))
}
}
}
/// Sets the zoom level to a specific percentage
///
/// # Arguments
/// * `percentage` - The zoom percentage (e.g., 100 for 100%, 150 for 150%, 50 for 50%)
///
/// # Examples
/// ```no_run
/// use computeruse::Desktop;
///
/// #[tokio::main]
/// async fn main() {
/// let desktop = Desktop::new_default().unwrap();
/// // Set zoom to 150%
/// desktop.set_zoom(150).await.unwrap();
///
/// // Reset zoom to 100%
/// desktop.set_zoom(100).await.unwrap();
/// }
/// ```
pub async fn set_zoom(&self, percentage: u32) -> Result<(), AutomationError> {
self.engine.set_zoom(percentage)
}
/// Stop all currently executing operations.
///
/// This cancels the internal cancellation token, which will cause any
/// operations that check `is_cancelled()` to abort. After calling this,
/// you should create a new Desktop instance to start fresh.
///
/// # Examples
/// ```no_run
/// use computeruse::Desktop;
///
/// let desktop = Desktop::new_default().unwrap();
/// // ... start some operations ...
/// desktop.stop_execution();
/// ```
pub fn stop_execution(&self) {
info!("[STOP-DEBUG] Desktop::stop_execution called - cancelling all operations");
if let Ok(token) = self.cancellation_token.read() {
info!("[STOP-DEBUG] Calling cancellation_token.cancel()");
token.cancel();
info!("[STOP-DEBUG] cancellation_token.cancel() completed");
} else {
info!("[STOP-DEBUG] WARNING: Could not acquire cancellation_token lock");
}
}
/// Check if execution has been cancelled.
///
/// Returns `true` if `stop_execution()` has been called.
/// Long-running operations should periodically check this and abort if true.
///
/// # Examples
/// ```no_run
/// use computeruse::Desktop;
///
/// let desktop = Desktop::new_default().unwrap();
/// if desktop.is_cancelled() {
/// println!("Execution was cancelled");
/// }
/// ```
pub fn is_cancelled(&self) -> bool {
self.cancellation_token
.read()
.map(|t| t.is_cancelled())
.unwrap_or(false)
}
/// Get a clone of the cancellation token for use in async operations.
///
/// This allows external code to wait on cancellation or create child tokens.
pub fn cancellation_token(&self) -> CancellationToken {
self.cancellation_token
.read()
.map(|t| t.clone())
.unwrap_or_else(|_| CancellationToken::new())
}
/// Reset the cancellation state, allowing new operations to run.
///
/// This should be called at the start of new operations to clear any
/// previous cancellation state from `stop_execution()`.
///
/// # Examples
/// ```no_run
/// use computeruse::Desktop;
///
/// let desktop = Desktop::new_default().unwrap();
/// desktop.stop_execution(); // Cancel previous operations
/// // ... later ...
/// desktop.reset_cancellation(); // Allow new operations
/// ```
pub fn reset_cancellation(&self) {
if let Ok(mut token) = self.cancellation_token.write() {
if token.is_cancelled() {
info!("🔄 Resetting cancellation state for new operations");
*token = CancellationToken::new();
}
}
}
/// Execute an action on an element with UI diff capture.
///
/// This method:
/// 1. Finds the element by selector
/// 2. Captures the UI tree before the action
/// 3. Executes the action
/// 4. Waits for UI to settle (configurable, default 1500ms)
/// 5. Captures the UI tree after the action
/// 6. Computes and returns the diff
///
/// # Arguments
/// * `selector` - Selector string to find the element
/// * `action` - Closure that takes &UIElement and returns Result<T, AutomationError>
/// * `options` - Optional UI diff capture options
///
/// # Returns
/// Tuple of (action_result, element, Option<UiDiffResult>)
///
/// # Examples
/// ```no_run
/// use computeruse::{Desktop, UiDiffOptions};
///
/// async fn example() -> Result<(), computeruse::AutomationError> {
/// let desktop = Desktop::new_default()?;
/// let options = UiDiffOptions {
/// settle_delay_ms: Some(1500),
/// ..Default::default()
/// };
/// let (result, element, diff) = desktop.execute_with_ui_diff(
/// "role:Button && name:Submit",
/// |el| el.click(),
/// Some(options),
/// ).await?;
/// Ok(())
/// }
/// ```
pub async fn execute_with_ui_diff<T, F>(
&self,
selector: &str,
action: F,
options: Option<UiDiffOptions>,
) -> Result<(T, UIElement, Option<UiDiffResult>), AutomationError>
where
F: FnOnce(&UIElement) -> Result<T, AutomationError>,
{
use std::time::Duration;
// Find the element (with default 30s timeout)
let element = self
.locator(selector)
.first(Some(Duration::from_secs(30)))
.await?;
let opts = options.unwrap_or_default();
// Get PID for tree capture
let pid = element.process_id().unwrap_or(0);
if pid == 0 {
debug!("[ui_diff] Could not get PID from element, executing without diff capture");
let result = action(&element)?;
return Ok((result, element, None));
}
// Build tree config
let detailed = opts.include_detailed_attributes.unwrap_or(true);
let tree_config = platforms::TreeBuildConfig {
property_mode: if detailed {
platforms::PropertyLoadingMode::Complete
} else {
platforms::PropertyLoadingMode::Fast
},
timeout_per_operation_ms: Some(100),
yield_every_n_elements: Some(25),
batch_size: Some(25),
max_depth: opts.max_depth,
include_all_bounds: false,
ui_settle_delay_ms: None,
format_output: false,
show_overlay: false,
overlay_display_mode: None,
from_selector: None,
};
// Capture BEFORE tree
debug!("[ui_diff] Capturing UI tree before action (PID: {})", pid);
let tree_before = match self.get_window_tree(pid, None, Some(tree_config.clone())) {
Ok(tree) => tree,
Err(e) => {
debug!(
"[ui_diff] Failed to capture tree before action: {}. Executing without diff.",
e
);
let result = action(&element)?;
return Ok((result, element, None));
}
};
let before_str = format_ui_node_as_compact_yaml(&tree_before, 0).formatted;
// Execute action
let result = action(&element)?;
// Wait for UI to settle
let settle_ms = opts.settle_delay_ms.unwrap_or(1500);
debug!("[ui_diff] Waiting {}ms for UI to settle", settle_ms);
tokio::time::sleep(Duration::from_millis(settle_ms)).await;
// Capture AFTER tree
debug!("[ui_diff] Capturing UI tree after action (PID: {})", pid);
let tree_after = match self.get_window_tree(pid, None, Some(tree_config)) {
Ok(tree) => tree,
Err(e) => {
debug!(
"[ui_diff] Failed to capture tree after action: {}. Returning without diff.",
e
);
return Ok((result, element, None));
}
};
let after_str = format_ui_node_as_compact_yaml(&tree_after, 0).formatted;
// Compute diff
let diff_result = match ui_tree_diff::simple_ui_tree_diff(&before_str, &after_str) {
Ok(Some(diff)) => {
info!(
"[ui_diff] UI changes detected: {} characters in diff",
diff.len()
);
UiDiffResult {
diff,
has_changes: true,
}
}
Ok(None) => {
debug!("[ui_diff] No UI changes detected");
UiDiffResult {
diff: "No UI changes detected".to_string(),
has_changes: false,
}
}
Err(e) => {
debug!(
"[ui_diff] Failed to compute UI diff: {}. Returning without diff.",
e
);
return Ok((result, element, None));
}
};
Ok((result, element, Some(diff_result)))
}
/// Execute an action on an already-found element with UI diff capture (async action variant).
///
/// Use this when you have complex element-finding logic (fallback selectors, retries)
/// and want to separate element finding from diff capture. This variant accepts async
/// actions that take ownership of the element.
///
/// # Arguments
/// * `element` - The element to execute the action on (will be cloned for return)
/// * `action` - Async closure that takes UIElement and returns Future<Result<T, AutomationError>>
/// * `options` - Optional UI diff capture options
///
/// # Returns
/// Tuple of (action_result, element, Option<UiDiffResult>)
pub async fn execute_on_element_with_ui_diff<T, F, Fut>(
&self,
element: UIElement,
action: F,
options: Option<UiDiffOptions>,
) -> Result<(T, UIElement, Option<UiDiffResult>), AutomationError>
where
F: FnOnce(UIElement) -> Fut,
Fut: std::future::Future<Output = Result<T, AutomationError>>,
{
use std::time::Duration;
let opts = options.unwrap_or_default();
// Clone element so we can return it after action consumes one copy
let element_for_return = element.clone();
// Get PID for tree capture
let pid = element.process_id().unwrap_or(0);
if pid == 0 {
debug!("[ui_diff] Could not get PID from element, executing without diff capture");
let result = action(element).await?;
return Ok((result, element_for_return, None));
}
// Build tree config
let detailed = opts.include_detailed_attributes.unwrap_or(true);
let tree_config = platforms::TreeBuildConfig {
property_mode: if detailed {
platforms::PropertyLoadingMode::Complete
} else {
platforms::PropertyLoadingMode::Fast
},
timeout_per_operation_ms: Some(100),
yield_every_n_elements: Some(25),
batch_size: Some(25),
max_depth: opts.max_depth,
include_all_bounds: false,
ui_settle_delay_ms: None,
format_output: false,
show_overlay: false,
overlay_display_mode: None,
from_selector: None,
};
// Capture BEFORE tree
debug!("[ui_diff] Capturing UI tree before action (PID: {})", pid);
let tree_before = match self.get_window_tree(pid, None, Some(tree_config.clone())) {
Ok(tree) => tree,
Err(e) => {
debug!(
"[ui_diff] Failed to capture tree before action: {}. Executing without diff.",
e
);
let result = action(element).await?;
return Ok((result, element_for_return, None));
}
};
let before_str = format_ui_node_as_compact_yaml(&tree_before, 0).formatted;
// Execute action (async)
let result = action(element).await?;
// Wait for UI to settle
let settle_ms = opts.settle_delay_ms.unwrap_or(1500);
debug!("[ui_diff] Waiting {}ms for UI to settle", settle_ms);
tokio::time::sleep(Duration::from_millis(settle_ms)).await;
// Capture AFTER tree
debug!("[ui_diff] Capturing UI tree after action (PID: {})", pid);
let tree_after = match self.get_window_tree(pid, None, Some(tree_config)) {
Ok(tree) => tree,
Err(e) => {
debug!(
"[ui_diff] Failed to capture tree after action: {}. Returning without diff.",
e
);
return Ok((result, element_for_return, None));
}
};
let after_str = format_ui_node_as_compact_yaml(&tree_after, 0).formatted;
// Compute diff
let diff_result = match ui_tree_diff::simple_ui_tree_diff(&before_str, &after_str) {
Ok(Some(diff)) => {
info!(
"[ui_diff] UI changes detected: {} characters in diff",
diff.len()
);
UiDiffResult {
diff,
has_changes: true,
}
}
Ok(None) => {
debug!("[ui_diff] No UI changes detected");
UiDiffResult {
diff: "No UI changes detected".to_string(),
has_changes: false,
}
}
Err(e) => {
debug!(
"[ui_diff] Failed to compute UI diff: {}. Returning without diff.",
e
);
return Ok((result, element_for_return, None));
}
};
Ok((result, element_for_return, Some(diff_result)))
}
// ============== ELEMENT VERIFICATION ==============
/// Verify that an element matching the selector exists within the same application as the scope element.
///
/// This is used for post-action verification - checking that an expected element appeared after
/// performing an action (e.g., a success dialog after clicking submit).
///
/// # Arguments
/// * `scope_element` - The element to get the application scope from (typically the element the action was performed on)
/// * `selector` - The selector string to search for
/// * `timeout_ms` - How long to wait for the element to appear
///
/// # Returns
/// The found element if verification passes, or an error if the element is not found within the timeout
///
/// # Errors
/// * `AutomationError::ElementNotFound` - If the application window cannot be determined from scope_element
/// * `AutomationError::Timeout` - If the element is not found within the timeout
#[instrument(skip(self, scope_element, selector))]
pub async fn verify_element_exists(
&self,
scope_element: &UIElement,
selector: &str,
timeout_ms: u64,
) -> Result<UIElement, AutomationError> {
use std::time::Duration;
debug!(
"Verifying element exists: '{}' within '{}'",
selector,
scope_element.name().unwrap_or_default()
);
// Create a locator scoped to the provided element (same as element.locator())
let locator = self
.locator(Selector::from(selector))
.within(scope_element.clone());
// Wait for the element with the specified timeout
locator
.wait(Some(Duration::from_millis(timeout_ms)))
.await
.map_err(|e| {
AutomationError::Timeout(format!(
"Verification failed: element '{}' not found after {}ms. {}",
selector, timeout_ms, e
))
})
}
/// Verify that an element matching the selector does NOT exist within the same application as the scope element.
///
/// This is used for post-action verification - checking that an element disappeared after
/// performing an action (e.g., a modal dialog closed after clicking OK).
///
/// # Arguments
/// * `scope_element` - The element to get the application scope from (typically the element the action was performed on)
/// * `selector` - The selector string that should NOT be found
/// * `timeout_ms` - How long to wait/check that the element doesn't appear
///
/// # Returns
/// Ok(()) if the element is NOT found (verification passes), or an error if the element IS found
///
/// # Errors
/// * `AutomationError::ElementNotFound` - If the application window cannot be determined from scope_element
/// * `AutomationError::VerificationFailed` - If the element IS found (meaning verification failed)
#[instrument(skip(self, scope_element, selector))]
pub async fn verify_element_not_exists(
&self,
scope_element: &UIElement,
selector: &str,
timeout_ms: u64,
) -> Result<(), AutomationError> {
use std::time::Duration;
debug!(
"Verifying element does NOT exist: '{}' within '{}'",
selector,
scope_element.name().unwrap_or_default()
);
// Create a locator scoped to the provided element (same as element.locator())
let locator = self
.locator(Selector::from(selector))
.within(scope_element.clone());
// Try to find the element - we WANT this to fail (timeout)
match locator.wait(Some(Duration::from_millis(timeout_ms))).await {
Ok(_found_element) => {
// Element was found - this is a verification FAILURE
Err(AutomationError::VerificationFailed(format!(
"Verification failed: element '{}' should not exist but was found",
selector
)))
}
Err(_) => {
// Element not found - this is what we wanted, verification PASSED
debug!("Verification passed: element '{}' not present", selector);
Ok(())
}
}
}
}
impl Clone for Desktop {
fn clone(&self) -> Self {
Self {
engine: self.engine.clone(),
// Clone shares the same cancellation token so stop_execution affects all clones
cancellation_token: self.cancellation_token.clone(),
// Clone shares the same caches so index lookups work across clones
uia_cache: self.uia_cache.clone(),
ocr_cache: self.ocr_cache.clone(),
omniparser_cache: self.omniparser_cache.clone(),
vision_cache: self.vision_cache.clone(),
dom_cache: self.dom_cache.clone(),
}
}
}
/// Stub ComputerUse agent loop on non-Windows.
///
/// The full agentic automation loop relies on Windows-only UI automation capabilities today.
/// We keep the method available so language bindings and downstream crates compile.
#[cfg(not(target_os = "windows"))]
impl Desktop {
pub async fn gemini_computer_use(
&self,
_process: &str,
_goal: &str,
_max_steps: Option<u32>,
_on_step: Option<ProgressCallback>,
) -> anyhow::Result<ComputerUseResult> {
Err(anyhow::anyhow!(
"gemini_computer_use is currently supported on Windows only"
))
}
}