csharp4405de34 | 1 | using Avalonia; |
| 2 | using Avalonia.Controls; |
| 3 | using Avalonia.Input; |
| 4 | using Avalonia.Interactivity; |
| 5 | using Avalonia.VisualTree; |
| 6 | |
| 7 | namespace CascadeIDE.Services; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Клик по контролу (Button — RaiseEvent Click; иначе — фокус и пробуем поднять клик). Вызывать из UI-потока. Для ide_click_control. |
| 11 | /// </summary> |
| 12 | public static class UiControlClick |
| 13 | { |
| 14 | public static string Click(TopLevel topLevel, Visual root, string? controlName) |
| 15 | { |
| 16 | Control? control; |
| 17 | if (!string.IsNullOrWhiteSpace(controlName)) |
| 18 | { |
| 19 | control = root is Window mw |
| 20 | ? UiControlAppearance.FindControlByNameAcrossAllWindows(mw, controlName.Trim()) |
| 21 | : UiControlAppearance.FindControlByName(root, controlName.Trim()); |
| 22 | if (control is null) |
| 23 | return $"Control not found: {controlName}."; |
| 24 | } |
| 25 | else |
| 26 | { |
| 27 | control = UiPointerClientPosition.TryGetControlUnderPointer(topLevel); |
| 28 | if (control is null) |
| 29 | return "No control under cursor. Specify name from ide_get_ui_layout."; |
| 30 | } |
| 31 | |
| 32 | if (control is Button btn) |
| 33 | { |
| 34 | btn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); |
| 35 | return "OK"; |
| 36 | } |
| 37 | |
| 38 | return $"Control is not a Button (type: {control.GetType().Name}). Only Button click is supported."; |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | |
View only · write via MCP/CIDE