From 8ffbbfb7fb59278c590fe0227cae336239b0c426 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 2 Oct 2021 22:40:22 +0200 Subject: [PATCH] Create root automation nodes like others. --- .../Avalonia.Win32/Automation/AutomationNode.cs | 16 ++++++++-------- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 14 ++++---------- src/Windows/Avalonia.Win32/WindowImpl.cs | 1 - 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Automation/AutomationNode.cs b/src/Windows/Avalonia.Win32/Automation/AutomationNode.cs index ebaf096b78..1b37664e4b 100644 --- a/src/Windows/Avalonia.Win32/Automation/AutomationNode.cs +++ b/src/Windows/Avalonia.Win32/Automation/AutomationNode.cs @@ -160,11 +160,6 @@ namespace Avalonia.Win32.Automation return null; } - if (Peer.GetType().Name == "PopupAutomationPeer") - { - System.Diagnostics.Debug.WriteLine("Popup automation node navigate " + direction); - } - return InvokeSync(() => { return direction switch @@ -183,11 +178,11 @@ namespace Avalonia.Win32.Automation public static AutomationNode? GetOrCreate(AutomationPeer? peer) { - if (peer is null) - return null; - return s_nodes.GetValue(peer, x => new AutomationNode(x)); + return peer is null ? null : s_nodes.GetValue(peer, Create); } + public static void Release(AutomationPeer peer) => s_nodes.Remove(peer); + IRawElementProviderSimple[]? IRawElementProviderFragment.GetEmbeddedFragmentRoots() => null; void IRawElementProviderSimple2.ShowContextMenu() => InvokeSync(() => Peer.ShowContextMenu()); void IInvokeProvider.Invoke() => InvokeSync((AAP.IInvokeProvider x) => x.Invoke()); @@ -294,6 +289,11 @@ namespace Avalonia.Win32.Automation return peer is object ? GetOrCreate(peer) : null; } + private static AutomationNode Create(AutomationPeer peer) + { + return peer is AAP.IRootProvider ? new RootAutomationNode(peer) : new AutomationNode(peer); + } + private static UiaControlTypeId ToUiaControlType(AutomationControlType role) { return role switch diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index fcc0ac1e32..67c4aabafe 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -79,8 +79,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_DESTROY: { - if (_automationNode is object) - UiaCoreProviderApi.UiaReturnRawElementProvider(_hwnd, IntPtr.Zero, IntPtr.Zero, null); + UiaCoreProviderApi.UiaReturnRawElementProvider(_hwnd, IntPtr.Zero, IntPtr.Zero, null); //Window doesn't exist anymore _hwnd = IntPtr.Zero; @@ -484,14 +483,9 @@ namespace Avalonia.Win32 case WindowsMessage.WM_GETOBJECT: if ((long)lParam == UiaRootObjectId) { - if (_automationNode is null) - { - var peer = ControlAutomationPeer.CreatePeerForElement((Control)_owner); - _automationNode = new RootAutomationNode(peer); - } - - var r = UiaCoreProviderApi.UiaReturnRawElementProvider(_hwnd, wParam, lParam, _automationNode); - return r; + var peer = ControlAutomationPeer.CreatePeerForElement((Control)_owner); + var node = AutomationNode.GetOrCreate(peer); + return UiaCoreProviderApi.UiaReturnRawElementProvider(_hwnd, wParam, lParam, node); } break; } diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index ffad00cf95..f8c6c29fa1 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -87,7 +87,6 @@ namespace Avalonia.Win32 private POINT _maxTrackSize; private WindowImpl _parent; private ExtendClientAreaChromeHints _extendChromeHints = ExtendClientAreaChromeHints.Default; - private AutomationNode _automationNode; private bool _isCloseRequested; private bool _shown; private bool _hiddenWindowIsParent;