Skip to content

Improving accessible object for label edit in ListView #6309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class User32
{
[DllImport(Libraries.User32, ExactSpelling = true, SetLastError = true)]
private static extern nint SetWinEventHook(uint eventMin, uint eventMax, nint hmodWinEventProc,
WINEVENTPROC pfnWinEventProc, uint idProcess, uint idThread, WINEVENT dwFlags);

public static nint SetWinEventHook(uint eventId, WINEVENTPROC pfnWinEventProc)
=> SetWinEventHook(eventId, eventId, Kernel32.GetModuleHandleW(null), pfnWinEventProc,
(uint)Environment.ProcessId, Kernel32.GetCurrentThreadId(), WINEVENT.INCONTEXT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class User32
{
[DllImport(Libraries.User32, ExactSpelling = true, SetLastError = true)]
public static extern BOOL UnhookWinEvent(nint hWinEventHook);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

internal static partial class Interop
{
internal static partial class User32
{
public enum WINEVENT : uint
{
OUTOFCONTEXT = 0,
SKIPOWNTHREAD = 1,
SKIPOWNPROCESS = 2,
INCONTEXT = 4,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

internal static partial class Interop
{
internal static partial class User32
{
public delegate void WINEVENTPROC(nint hWinEventHook, uint eventId, nint hwnd, int idObject, int idChild, uint idEventThread, uint dwmsEventTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ internal override UiaCore.RowOrColumnMajor RowOrColumnMajor
return null;
}

if (_owningListView._labelEdit is {} labelEdit && index == GetChildCount() - 1)
{
return labelEdit.AccessibilityObject;
}

if (_owningListView.GroupsDisplayed)
{
IReadOnlyList<ListViewGroup> visibleGroups = GetVisibleGroups();
Expand All @@ -105,7 +110,13 @@ public override int GetChildCount()
return InvalidIndex;
}

return _owningListView.GroupsDisplayed ? GetVisibleGroups().Count : _owningListView.Items.Count;
int count = _owningListView.GroupsDisplayed ? GetVisibleGroups().Count : _owningListView.Items.Count;
if (_owningListView._labelEdit is not null)
{
count++;
}

return count;
}

private int GetItemIndex(AccessibleObject? child)
Expand Down Expand Up @@ -143,7 +154,10 @@ private int GetGroupIndex(AccessibleObject? child)
return InvalidIndex;
}

internal override int GetChildIndex(AccessibleObject? child) => _owningListView.GroupsDisplayed ? GetGroupIndex(child) : GetItemIndex(child);
internal override int GetChildIndex(AccessibleObject? child)
=> _owningListView._labelEdit is {} labelEdit && child == labelEdit.AccessibilityObject
? GetChildCount() - 1
: _owningListView.GroupsDisplayed ? GetGroupIndex(child) : GetItemIndex(child);

private string GetItemStatus()
=> _owningListView.Sorting switch
Expand Down Expand Up @@ -195,6 +209,11 @@ internal override string GetMultiViewProviderViewName(int viewId)

private AccessibleObject? GetLastChild()
{
if (_owningListView._labelEdit is {} labelEdit)
{
return labelEdit.AccessibilityObject;
}

if (_owningListView.GroupsDisplayed)
{
IReadOnlyList<ListViewGroup> visibleGroups = GetVisibleGroups();
Expand Down
26 changes: 26 additions & 0 deletions src/System.Windows.Forms/src/System/Windows/Forms/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public partial class ListView : Control

private bool _blockLabelEdit;

private ListViewLabelEditNativeWindow? _labelEdit;

// Background image stuff
// Because we have to create a temporary file and the OS does not clean up the temporary files from the machine
// we have to do that ourselves
Expand Down Expand Up @@ -6466,6 +6468,14 @@ private unsafe void WmReflectNotify(ref Message m)

case (int)LVN.BEGINLABELEDITW:
{
Debug.Assert(_labelEdit is null,
"A new label editing shouldn't start before the previous one ended");
if (_labelEdit is not null)
{
_labelEdit.ReleaseHandle();
_labelEdit = null;
}

bool cancelEdit;
if (_blockLabelEdit)
{
Expand All @@ -6481,6 +6491,13 @@ private unsafe void WmReflectNotify(ref Message m)

m.ResultInternal = cancelEdit ? 1 : 0;
_listViewState[LISTVIEWSTATE_inLabelEdit] = !cancelEdit;

if (!cancelEdit)
{
_labelEdit = new ListViewLabelEditNativeWindow(this);
_labelEdit.AssignHandle(User32.SendMessageW(this, (User32.WM)LVM.GETEDITCONTROL));
}

break;
}

Expand Down Expand Up @@ -6510,6 +6527,15 @@ private unsafe void WmReflectNotify(ref Message m)

case (int)LVN.ENDLABELEDITW:
{
Debug.Assert(_labelEdit is not null, "There is no active label edit to end");
if (_labelEdit is null)
{
break;
}

_labelEdit.ReleaseHandle();
_labelEdit = null;

_listViewState[LISTVIEWSTATE_inLabelEdit] = false;
NMLVDISPINFO* dispInfo = (NMLVDISPINFO*)m.LParamInternal;
string? text = dispInfo->item.pszText is null ? null : new string(dispInfo->item.pszText);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Drawing;
using System.Runtime.InteropServices;
using static Interop;

namespace System.Windows.Forms
{
internal class ListViewLabelEditAccessibleObject : AccessibleObject
{
private const string LIST_VIEW_LABEL_EDIT_AUTOMATION_ID = "1";

private readonly ListView _owningListView;
private readonly IntPtr _handle;
private readonly ListViewLabelEditUiaTextProvider _textProvider;
private int[]? _runtimeId;

public ListViewLabelEditAccessibleObject(ListView owningListView, IHandle handle)
{
_owningListView = owningListView.OrThrowIfNull();
_handle = handle.Handle;
UseStdAccessibleObjects(_handle);
_textProvider = new ListViewLabelEditUiaTextProvider(owningListView, handle, this);
}

private protected override string AutomationId => LIST_VIEW_LABEL_EDIT_AUTOMATION_ID;

internal override UiaCore.IRawElementProviderFragment? FragmentNavigate(UiaCore.NavigateDirection direction)
{
AccessibleObject parent = _owningListView.AccessibilityObject;

return direction switch
{
UiaCore.NavigateDirection.Parent => parent,
UiaCore.NavigateDirection.NextSibling => parent.GetChildIndex(this) is int childId and >= 0 ? parent.GetChild(childId + 1) : null,
UiaCore.NavigateDirection.PreviousSibling => parent.GetChildIndex(this) is int childId and >= 0 ? parent.GetChild(childId - 1) : null,
_ => base.FragmentNavigate(direction),
};
}

internal override UiaCore.IRawElementProviderFragmentRoot FragmentRoot => _owningListView.AccessibilityObject;

internal override object? GetPropertyValue(UiaCore.UIA propertyID)
=> propertyID switch
{
UiaCore.UIA.ProcessIdPropertyId => Environment.ProcessId,
UiaCore.UIA.ControlTypePropertyId => UiaCore.UIA.EditControlTypeId,
UiaCore.UIA.AccessKeyPropertyId => string.Empty,
UiaCore.UIA.HasKeyboardFocusPropertyId => true,
UiaCore.UIA.IsKeyboardFocusablePropertyId => (State & AccessibleStates.Focusable) == AccessibleStates.Focusable,
UiaCore.UIA.IsEnabledPropertyId => _owningListView.Enabled,
UiaCore.UIA.IsContentElementPropertyId => true,
UiaCore.UIA.NativeWindowHandlePropertyId => _handle,
_ => base.GetPropertyValue(propertyID),
};

internal override UiaCore.IRawElementProviderSimple? HostRawElementProvider
{
get
{
UiaCore.UiaHostProviderFromHwnd(new HandleRef(this, _handle), out UiaCore.IRawElementProviderSimple provider);
return provider;
}
}

internal override bool IsPatternSupported(UiaCore.UIA patternId) => patternId switch
{
UiaCore.UIA.TextPatternId => true,
UiaCore.UIA.TextPattern2Id => true,
UiaCore.UIA.ValuePatternId => true,
UiaCore.UIA.LegacyIAccessiblePatternId => true,
_ => base.IsPatternSupported(patternId),
};

public override string Name => User32.GetWindowText(_handle);

internal override int[] RuntimeId => _runtimeId ??= new int[] { RuntimeIDFirstItem, PARAM.ToInt(_handle) };

internal override UiaCore.ITextRangeProvider DocumentRangeInternal
=> _textProvider.DocumentRange;

internal override UiaCore.ITextRangeProvider[]? GetTextSelection()
=> _textProvider.GetSelection();

internal override UiaCore.ITextRangeProvider[]? GetTextVisibleRanges()
=> _textProvider.GetVisibleRanges();

internal override UiaCore.ITextRangeProvider? GetTextRangeFromChild(UiaCore.IRawElementProviderSimple childElement)
=> _textProvider.RangeFromChild(childElement);

internal override UiaCore.ITextRangeProvider? GetTextRangeFromPoint(Point screenLocation)
=> _textProvider.RangeFromPoint(screenLocation);

internal override UiaCore.SupportedTextSelection SupportedTextSelectionInternal
=> _textProvider.SupportedTextSelection;

internal override UiaCore.ITextRangeProvider? GetTextCaretRange(out BOOL isActive)
=> _textProvider.GetCaretRange(out isActive);

internal override UiaCore.ITextRangeProvider GetRangeFromAnnotation(UiaCore.IRawElementProviderSimple annotationElement)
=> _textProvider.RangeFromAnnotation(annotationElement);
}
}
Loading