# UI Automation study: principles, Win32 comparison, and findings ## UIA versus Win32 Windows UI Automation (UIA) exposes a semantic tree of GUI elements. Each element has properties such as `AutomationId`, `Name`, `ClassName`, `ControlType`, visibility, and bounding rectangle. Supported **patterns** describe actions: `InvokePattern` for buttons, `ValuePattern` for text fields, and `SelectionItemPattern` for selectable items. The implementation uses `pywinauto` and `comtypes` to reach these COM interfaces. Classic Win32 automation primarily works with HWNDs, window classes, messages, and coordinates. Win32 enumeration can discover native top-level windows, but does not necessarily expose the logical descendants of a WPF/DevExpress GUI. UIA exposes more meaningful structure for those controls. Conversely, UIA cannot guarantee that a control exposes a usable pattern: the graph's context menu is opened with a **coordinate-based right click**, and submenu navigation uses mouse movement/clicks. These are complementary approaches, not interchangeable interfaces. The exploratory `20_inspect_export_uia.py` used `win32gui.EnumWindows()` to enumerate top-level windows while inspecting controls through UIA. The **production `SmartLabUIA` does not import `win32gui`**; it uses UIA and `pywinauto.mouse` for the relevant operations. Do not conflate the investigation method with the production implementation. ## Element lookup strategy The main window is selected using both a title containing `SmartLab Studio II` and `AutomationId=mainShellWindow`. For fixed parameter paths, the implementation traverses known parent/child edges using `TreeScope_Children` rather than performing a broad descendant search at every step. An unnamed `ScrollViewer` is located by `ClassName`. This limits ambiguous matches when repeated AutomationIds or labels exist. Dynamic dialogs are located separately; the filename input within the Windows Save dialog is resolved through the known sequence of nested child elements. ## Investigation sequence (2026-11) 1. **`19.py`**: identified Z-axis Basic tab, scan/move/slit groups, and AutomationIds for parameter controls. This underpins `BASIC_PATH`, group paths, and `PATHS`. 2. **`20_inspect_export_uia.py`**: inspected context menus, `exportDataWindow`, its `exportBtn`, and the Windows Save dialog (`#32770`), filename edit (`1001`) and Save button (`1`). This investigation established the export UI elements. 3. **`21_test_export_uia.py`, `22_inspect_export_hierarchy.py`**: explored lookup behavior around context menus. Changes to search strategy did not establish a replacement for the verified `20` findings; do not treat these scripts as a new canonical tree. 4. **`24_inspect_graph_uia.py`**: identified the display area's `2θ/θ` document, `graphCtrl` and `chartControl`, and its tab `TwoThetaThetaTabId`. The captured graph was offscreen; the implementation selects its tab before right-clicking. ## Export investigations since the v0.2.6 tag | Study | What it checks | | --- | --- | | [30](https://github.com/ohara-lab-su/smartlab_studio_ctrl/blob/main/try/uia/30_export_menu_without_mouse.py) | UIA context-menu requests to the chart and its hit targets | | [31](https://github.com/ohara-lab-su/smartlab_studio_ctrl/blob/main/try/uia/31_export_wm_contextmenu.py) | Direct `WM_CONTEXTMENU` and subsequent UIA saving | | [32](https://github.com/ohara-lab-su/smartlab_studio_ctrl/blob/main/try/uia/32_inspect_export_click_targets.py) | Coordinates and UIA paths for both clicks. The chart center hit the `legendDock` legend | | [33](https://github.com/ohara-lab-su/smartlab_studio_ctrl/blob/main/try/uia/33_export_rbutton_message.py) | Direct right-button messages versus `SendInput` | | [34](https://github.com/ohara-lab-su/smartlab_studio_ctrl/blob/main/try/uia/34_export_uia_after_right_click.py) | UIA submenu expansion, item selection, and saving after manually opening the menu | | [35](https://github.com/ohara-lab-su/smartlab_studio_ctrl/blob/main/try/uia/35_repeat_current_export.py) | Repeated runs of the original API and desktop input permissions | | [36](https://github.com/ohara-lab-su/smartlab_studio_ctrl/blob/main/try/uia/36_inspect_export_focus.py) | Tab selection versus actual keyboard focus and context-menu key behavior | | [38](https://github.com/ohara-lab-su/smartlab_studio_ctrl/blob/main/try/uia/38_compare_2theta_menu.py) | Compare UIA, `WM_CONTEXTMENU`, and `SendInput` on the 2θ graph after measurement | | [39](https://github.com/ohara-lab-su/smartlab_studio_ctrl/blob/main/try/uia/39_test_export_timing.py) | Add waits and retries around the existing API in the test process and save 2θ to a separate file | Study 34 verified UIA saving for both 2θ and 2θ/θ after manually opening the menu. Production export in v0.2.7 uses the existing combination of mouse clicks and UIA. [Current sequence and verification](export.md) ## Lessons for future investigations Record each element's full parent chain, AutomationId, Name, ClassName, ControlType, supported patterns, visibility and bounding rectangle, together with the GUI state at capture time. Distinguish offscreen controls from missing controls. Keep investigation scripts separate from production code. Avoid replacing a verified parent-child path with a speculative global search; only add a new path after obtaining corresponding UIA evidence. Track the GUI version and distinguish a click request from confirmation that the underlying operation completed.