Skip to content

Commit

Permalink
Win32: Unify and fix UIA runtime id transformation.
Browse files Browse the repository at this point in the history
We need the third element in the array to indicate the kind of
provider (always "main" in our case), instead of always being
UiaAppendRuntimeId.
  • Loading branch information
madewokherd committed Aug 5, 2024
1 parent 685293b commit f234a6a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
17 changes: 3 additions & 14 deletions xalia/Win32/AccessibleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -966,20 +966,9 @@ public static ElementIdentifier ElementIdFromVariantBackground(object variant, I
var fragment = uiaprov as IRawElementProviderFragment;
if (!(fragment is null))
{
var runtime_id = fragment.GetRuntimeId();

if (!(runtime_id is null))
{
if (runtime_id[0] == UiaAppendRuntimeId)
{
result.runtime_id = new int[runtime_id.Length + 2];
result.runtime_id[0] = 42;
result.runtime_id[1] = (int)root_hwnd;
Array.Copy(runtime_id, 0, result.runtime_id, 2, runtime_id.Length);
}
else
result.runtime_id = runtime_id;
}
result.runtime_id = fragment.GetRuntimeId();
if (!(result.runtime_id is null))
return result;
}
}

Expand Down
12 changes: 1 addition & 11 deletions xalia/Win32/UiaProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,17 +655,7 @@ private ElementIdentifier ElementIdFromFragmentBackground(IRawElementProviderFra
result.is_root_hwnd = false;
result.prov = simple;

var runtime_id = child.GetRuntimeId();

if (runtime_id[0] == UiaAppendRuntimeId)
{
result.runtime_id = new int[runtime_id.Length + 2];
result.runtime_id[0] = 42;
result.runtime_id[1] = (int)RootHwnd.Hwnd;
Array.Copy(runtime_id, 0, result.runtime_id, 2, runtime_id.Length);
}
else
result.runtime_id = runtime_id;
result.runtime_id = child.GetRuntimeId();

return result;
}
Expand Down
21 changes: 17 additions & 4 deletions xalia/Win32/Win32Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,35 @@ public string GetElementName(ElementIdentifier id)
if (id.is_root_hwnd)
return GetElementName(id.root_hwnd);
if (!(id.runtime_id is null))
return GetElementName(id.runtime_id);
return GetElementName(id.runtime_id, id.root_hwnd);
if (!(id.acc2 is null))
return GetElementName(id.root_hwnd, OBJID_CLIENT, id.acc2_uniqueId);
if (!(id.punk == IntPtr.Zero))
return $"punk-{id.root_hwnd.ToInt64():x}-{id.punk.ToInt64():x}";
return null;
}

public string GetElementName(int[] runtime_id)
public string GetElementName(int[] runtime_id, IntPtr hwnd = default)
{
if (runtime_id.Length == 2 && runtime_id[0] == 42)
{
// HWND element
return GetElementName((IntPtr)runtime_id[1]);
}

if (runtime_id.Length == 0)
return null;

if (runtime_id[0] == UiaAppendRuntimeId)
{
var new_runtime_id = new int[runtime_id.Length + 2];
new_runtime_id[0] = 42;
new_runtime_id[1] = Utils.TruncatePtr(hwnd);
new_runtime_id[2] = 4;
Array.Copy(runtime_id, 1, new_runtime_id, 3, runtime_id.Length - 1);
runtime_id = new_runtime_id;
}

StringBuilder sb = new StringBuilder();
sb.Append("uia");
foreach (int i in runtime_id)
Expand Down Expand Up @@ -211,9 +224,9 @@ public UiDomElement LookupElement(ElementIdentifier id)
return LookupElement(element_name);
}

public UiDomElement LookupElement(int[] runtime_id)
public UiDomElement LookupElement(int[] runtime_id, IntPtr hwnd = default)
{
var element_name = GetElementName(runtime_id);
var element_name = GetElementName(runtime_id, hwnd);
if (element_name is null)
return null;

Expand Down

0 comments on commit f234a6a

Please sign in to comment.