Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
f65b08d
commit work
h3xds1nz May 20, 2025
b5e9f88
some fixes
h3xds1nz May 20, 2025
51e0e86
keep boxing prop value due to public interface
h3xds1nz May 21, 2025
9e8dacc
fix
h3xds1nz May 22, 2025
b6f4102
Polish it a bit, fix exception text
h3xds1nz May 29, 2025
2f3391d
Convert ExceptionWrapper as static class
h3xds1nz May 29, 2025
ef2247e
Remove ExceptionFilterHelper dead code
h3xds1nz May 29, 2025
6b13583
Provide specific implementation for ExceptionWrapper, remove casts
h3xds1nz May 29, 2025
85bdcbd
Wrap ExceptionWrapper inside Dispatcher
h3xds1nz May 29, 2025
d9e6373
Move ExceptionWrapper into System.Windows.Threading
h3xds1nz May 29, 2025
454db37
Move stuff around
h3xds1nz May 30, 2025
e25f0d2
Remove unused wrapper
h3xds1nz May 30, 2025
f078b64
Remove using
h3xds1nz May 30, 2025
0db154d
since I've done half the job there
h3xds1nz May 30, 2025
84aedcb
Avoid casts where we know the type
h3xds1nz May 30, 2025
f090abc
Avoid apicompat suppressions by limiting "visibility"
h3xds1nz May 30, 2025
7a029c4
Change access modifiers
h3xds1nz May 30, 2025
b66c414
Interpolation
h3xds1nz May 30, 2025
41f9af1
Fix DWriteForwarder/System.Printing dependencies in the build process
h3xds1nz May 30, 2025
9d09b65
Add a few more tests
h3xds1nz May 30, 2025
c52cf1a
Fix wrappers
h3xds1nz May 30, 2025
74a2025
ScrollProviderWrapper / ExpandCollapseProviderWrapper
h3xds1nz May 30, 2025
3abb086
RangeValueProviderWrapper
h3xds1nz May 30, 2025
473af59
GridItemProviderWrapper
h3xds1nz May 30, 2025
da104f3
Few more tests
h3xds1nz May 31, 2025
b1d7046
TextRangeProviderWrapper
h3xds1nz May 31, 2025
7ce8720
TableItemProviderWrapper
h3xds1nz May 31, 2025
8bfc0a1
SelectionProviderWrapper
h3xds1nz May 31, 2025
c668e4c
WindowProviderWrapper
h3xds1nz May 31, 2025
118ec86
ValueProviderWrapper
h3xds1nz May 31, 2025
c720586
TableProviderWrapper
h3xds1nz May 31, 2025
057b3df
SelectionItemProviderWrapper
h3xds1nz May 31, 2025
f1719ad
TransformProviderWrapper
h3xds1nz May 31, 2025
9b634d9
Finish rest of the wrapper-returns
h3xds1nz May 31, 2025
1c27ef6
Remove legacy Invoke interface in favour of new methods, add void inv…
h3xds1nz May 31, 2025
1e43057
Fix docs for ActionInfo
h3xds1nz May 31, 2025
0b64463
Add more basic ElementUtil/Dispatcher consistency tests
h3xds1nz May 31, 2025
029c0dc
Enable nullability, seal the wrappers, make fields readonly, cleanup …
h3xds1nz May 31, 2025
735a2ea
Enable nullability, seal the wrappers, make fields readonly, cleanup …
h3xds1nz May 31, 2025
1e0e0b8
Finish nullability on ITextRangeProvider
h3xds1nz May 31, 2025
cf6d0ef
Formatting changes ONLY - region cleanup, whitespaces
h3xds1nz May 31, 2025
c59ba71
Formatting changes ONLY - Flatten all namespaces on wrappers
h3xds1nz May 31, 2025
6fc0efe
Formatting changes ONLY - Add summary comments for proxies
h3xds1nz May 31, 2025
174bb73
Re-order new files in csproj
h3xds1nz May 31, 2025
e48620f
Use internals over public to avoid confusion
h3xds1nz Jun 1, 2025
59ed9ae
Add ExpandCollapse/RangeValue/ToggleProvider unit tests
h3xds1nz Jun 1, 2025
f0c0cc8
Add ValueProviderWrapper tests
h3xds1nz Jun 1, 2025
92151a5
Fix nullability on FindItemByProperty
h3xds1nz Jun 1, 2025
5db90be
Fix IGridProvider nullability
h3xds1nz Jun 1, 2025
efcdfd3
Enable nullability on IMultipleViewProvider
h3xds1nz Jun 1, 2025
e24e4a5
Add TransformProviderWrapper unit tests as well
h3xds1nz Jun 1, 2025
d380b14
Final touches
h3xds1nz Jun 2, 2025
140210e
Fix ref to include abstract keyword, revert callvirt
h3xds1nz Jun 2, 2025
3935c32
Fix baseline (again)
h3xds1nz Jun 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Polish it a bit, fix exception text
  • Loading branch information
h3xds1nz committed May 29, 2025
commit b6f4102c327878764b3907796184c72e7c3035a0
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Provider;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Input;
using System.Windows.Threading;

namespace MS.Internal.Automation
{
Expand All @@ -25,7 +24,7 @@ namespace MS.Internal.Automation
//
// Currently exposes just BoundingRectangle, ClassName, IsEnabled
// and IsKeyboardFocused properties.
internal class ElementProxy: IRawElementProviderFragmentRoot, IRawElementProviderAdviseEvents
internal class ElementProxy : IRawElementProviderFragmentRoot, IRawElementProviderAdviseEvents
{
//------------------------------------------------------
//
Expand Down Expand Up @@ -88,44 +87,31 @@ public object GetPropertyValue(int property)

public ProviderOptions ProviderOptions
{
get
get
{
AutomationPeer peer = Peer;
if (peer == null)
{
return ProviderOptions.ServerSideProvider;
}
return ElementUtil.Invoke(peer, static (state) => state.InContextGetProviderOptions(), this);

return peer is null ? ProviderOptions.ServerSideProvider : ElementUtil.Invoke(peer, static (state) => state.InContextGetProviderOptions(), this);
}
}
}

public IRawElementProviderSimple HostRawElementProvider
{
get
{
IRawElementProviderSimple host = null;
HostedWindowWrapper hwndWrapper = null;
AutomationPeer peer = Peer;
if (peer == null)

if (peer is null)
{
return null;
}
hwndWrapper = ElementUtil.Invoke(peer, static (state) => state.InContextGetHostRawElementProvider(), this);

if (hwndWrapper != null)
host = GetHostHelper(hwndWrapper);

return host;
}
}
HostedWindowWrapper hwndWrapper = ElementUtil.Invoke(peer, static (state) => state.InContextGetHostRawElementProvider(), this);

private static IRawElementProviderSimple GetHostHelper(HostedWindowWrapper hwndWrapper)
{
return AutomationInteropProvider.HostProviderFromHandle(hwndWrapper.Handle);
return hwndWrapper is not null ? AutomationInteropProvider.HostProviderFromHandle(hwndWrapper.Handle) : null;
}
}

// IRawElementProviderFragment methods...

public IRawElementProviderFragment Navigate(NavigateDirection direction)
{
AutomationPeer peer = Peer;
Expand All @@ -149,12 +135,12 @@ public Rect BoundingRectangle
return ElementUtil.Invoke(peer, static (state) => state.InContextBoundingRectangle(), this);
}
}

public IRawElementProviderSimple[] GetEmbeddedFragmentRoots()
{
return null;
}

public void SetFocus()
{
AutomationPeer peer = Peer ?? throw new ElementNotAvailableException();
Expand All @@ -172,7 +158,6 @@ public IRawElementProviderFragmentRoot FragmentRoot
}
}

// IRawElementProviderFragmentRoot methods..
public IRawElementProviderFragment ElementProviderFromPoint(double x, double y)
{
AutomationPeer peer = Peer;
Expand Down Expand Up @@ -259,9 +244,9 @@ internal AutomationPeer Peer
{
get
{
if (_peer is WeakReference)
if (_peer is WeakReference weakRef)
{
AutomationPeer peer = (AutomationPeer)((WeakReference)_peer).Target;
AutomationPeer peer = (AutomationPeer)weakRef.Target;
return peer;
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace MS.Internal.Automation;

/// <summary>
/// Utility class for working with <see cref="AutomationPeer"/>.
/// </summary>
internal static partial class ElementUtil
{
/// <summary>
/// Wraps the return value and exception of an asynchronous operation.
/// </summary>
/// <typeparam name="TReturn"></typeparam>
[StructLayout(LayoutKind.Auto)]
private readonly struct ReturnInfo<TReturn>
{
/// <summary>
/// The exception that was thrown during the operation, if any.
/// </summary>
public Exception? StoredException { get; init; }

/// <summary>
/// Gets a value indicating whether the operation has been completed or timed out.
/// </summary>
public bool Completed { get; init; }

/// <summary>
/// The return value of the operation, if it completed successfully.
/// </summary>
public TReturn Value { get; init; }

/// <summary>
/// Creates a new instance of <see cref="ReturnInfo{TReturn}"/> with the specified exception.
/// </summary>
/// <param name="exception">The exception that was thrown during the operation.</param>
/// <returns>Returns a <see cref="ReturnInfo{TReturn}"/> object.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReturnInfo<TReturn> FromException(Exception exception)
{
return new ReturnInfo<TReturn>
{
StoredException = exception,
Completed = true,
Value = default!
};
}

/// <summary>
/// Creates a new instance of <see cref="ReturnInfo{TReturn}"/> with the specified value.
/// </summary>
/// <param name="value">The return value of the operation.</param>
/// <returns>Returns a <see cref="ReturnInfo{TReturn}"/> object.</returns>
public static ReturnInfo<TReturn> FromResult(TReturn value)
{
return new ReturnInfo<TReturn>
{
StoredException = null,
Completed = true,
Value = value
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@ namespace MS.Internal.Automation;
/// <summary>
/// Utility class for working with <see cref="AutomationPeer"/>.
/// </summary>
internal static class ElementUtil
internal static partial class ElementUtil
{
private readonly struct ReturnInfo<TReturn>
{
public Exception? StoredException { get; init; }
public bool Completed { get; init; }

public TReturn Value { get; init; }
}

///// <summary>
///// Provides a helper to invoke work on the UI thread, re-throwing all exceptions on the thread that invoked this execution.
///// </summary>
Expand Down Expand Up @@ -63,11 +55,11 @@ static ReturnInfo<TReturn> ExceptionWrapper(Func<TArg, TReturn> func, TArg arg)
{
try
{
return new ReturnInfo<TReturn>() { Value = func(arg), Completed = true };
return ReturnInfo<TReturn>.FromResult(func(arg));
}
catch (Exception e)
{
return new ReturnInfo<TReturn>() { StoredException = e, Completed = true };
return ReturnInfo<TReturn>.FromException(e);
}
}

Expand All @@ -88,11 +80,11 @@ static ReturnInfo<TReturn> ExceptionWrapper(Func<TArg1, TArg2, TReturn> func, TA
{
try
{
return new ReturnInfo<TReturn>() { Value = func(arg1, arg2), Completed = true };
return ReturnInfo<TReturn>.FromResult(func(arg1, arg2));
}
catch (Exception e)
{
return new ReturnInfo<TReturn>() { StoredException = e, Completed = true };
return ReturnInfo<TReturn>.FromException(e);
}
}

Expand Down Expand Up @@ -120,14 +112,23 @@ private static TReturn HandleReturnValue<TReturn>(Dispatcher dispatcher, ref rea
return retVal.Value;
}

/// <summary>
/// Throws an <see cref="InvalidOperationException"/> indicating that the associated dispatcher has been shut down.
/// </summary>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowInvalidOperationException() => throw new InvalidOperationException(SR.AutomationDispatcherShutdown);

/// <summary>
/// Throws a <see cref="TimeoutException"/> indicating that the automation operation has timed out.
/// </summary>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowTimeoutException() => throw new TimeoutException(SR.AutomationDispatcherShutdown);
private static void ThrowTimeoutException() => throw new TimeoutException(SR.AutomationTimeout);

/// <summary>
/// Unwraps the exception and throws it on the current thread.
/// </summary>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static void UnwrapException(Exception exception) => throw exception;
Expand All @@ -144,31 +145,23 @@ internal static object Invoke(AutomationPeer peer, DispatcherOperationCallback w
Exception? remoteException = null;
bool completed = false;

object retVal = dispatcher.Invoke(
DispatcherPriority.Send,
TimeSpan.FromMinutes(3),
(DispatcherOperationCallback)delegate (object workArg)
object retVal = dispatcher.Invoke(DispatcherPriority.Send, TimeSpan.FromMinutes(3), (DispatcherOperationCallback)delegate (object workArg)
{
try
{
try
{
return work(workArg);
}
catch (Exception e)
{
remoteException = e;
return null;
}
catch //for non-CLS Compliant exceptions
{
remoteException = null;
return null;
}
finally
{
completed = true;
}
},
arg);
return work(workArg);
}
catch (Exception e)
{
remoteException = e;
return null;
}
finally
{
completed = true;
}
},
arg);

if (completed)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<Compile Include="MS\Internal\Automation\DockProviderWrapper.cs" />
<Compile Include="MS\Internal\Automation\ElementProxy.cs" />
<Compile Include="MS\Internal\Automation\ElementUtil.cs" />
<Compile Include="MS\Internal\Automation\ElementUtil.ReturnInfo.cs" />
<Compile Include="MS\Internal\Automation\EventMap.cs" />
<Compile Include="MS\Internal\Automation\ExpandCollapseProviderWrapper.cs" />
<Compile Include="MS\Internal\Automation\GridItemProviderWrapper.cs" />
Expand Down