csharpdeeb25a2 | 1 | using Avalonia; |
| 2 | using Avalonia.Controls; |
| 3 | using Avalonia.Input; |
| 4 | using Avalonia.VisualTree; |
| 5 | |
| 6 | namespace CascadeIDE.Services; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Установить фокус на контрол по имени или на элемент под курсором. Для ide_set_focus. |
| 10 | /// </summary> |
| 11 | public static class UiControlSetFocus |
| 12 | { |
| 13 | public static string SetFocus(TopLevel topLevel, Visual root, string? controlName) |
| 14 | { |
| 15 | Control? control; |
| 16 | if (!string.IsNullOrWhiteSpace(controlName)) |
| 17 | { |
| 18 | control = root is Window mw |
| 19 | ? UiControlAppearance.FindControlByNameAcrossAllWindows(mw, controlName.Trim()) |
| 20 | : UiControlAppearance.FindControlByName(root, controlName.Trim()); |
| 21 | if (control is null) |
| 22 | return $"Control not found: {controlName}."; |
| 23 | } |
| 24 | else |
| 25 | { |
| 26 | control = UiPointerClientPosition.TryGetControlUnderPointer(topLevel); |
| 27 | if (control is null) |
| 28 | return "No control under cursor. Specify name from ide_get_ui_layout."; |
| 29 | } |
| 30 | |
| 31 | control.Focus(); |
| 32 | return "OK"; |
| 33 | } |
| 34 | |
| 35 | } |
| 36 | |
View only · write via MCP/CIDE