Skip to content
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

Make the toolbar menu items support the support space key. #11520

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
Expand Up @@ -1253,6 +1253,7 @@ protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
|| !(dismissingItem.HasDropDownItems))
{ // clicking on a item w/dropdown does not dismiss window
Close(ToolStripDropDownCloseReason.ItemClicked);
SelectPreviousToolStrip();
lonitra marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimonZhao888 - Perhaps this line requires additional checks, sometime focus should not return to the owner item. See #11909

}
}
}
Expand Down Expand Up @@ -1359,7 +1360,7 @@ internal void SelectPreviousToolStrip()
{
// snap the owner item before calling hide as non-auto created dropdowns will
// exit menu mode if there's no OwnerItem.
ToolStripItem? itemOnPreviousMenuToSelect = OwnerItem;
ToolStripItem? itemOnPreviousMenuToSelect = GetToplevelOwnerItem();
Hide();

if (itemOnPreviousMenuToSelect is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ internal override void Toggle()
_ => ToggleState.ToggleState_Indeterminate
};

internal void OnCheckStateChanged(CheckState oldValue, CheckState newValue)
{
RaiseAutomationPropertyChangedEvent(
UIA_PROPERTY_ID.UIA_ToggleToggleStatePropertyId,
(VARIANT)(int)CheckStateToToggleState(oldValue),
(VARIANT)(int)CheckStateToToggleState(newValue));
}

private static ToggleState CheckStateToToggleState(CheckState checkState)
=> checkState switch
{
CheckState.Checked => ToggleState.ToggleState_On,
CheckState.Unchecked => ToggleState.ToggleState_Off,
_ => ToggleState.ToggleState_Indeterminate
};

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace System.Windows.Forms;
$"System.ComponentModel.Design.Serialization.CodeDomSerializer, {AssemblyRef.SystemDesign}")]
public partial class ToolStripMenuItem : ToolStripDropDownItem
{
private CheckState _prevCheckState = CheckState.Unchecked;
private static readonly MenuTimer s_menuTimer = new();

private static readonly int s_propShortcutKeys = PropertyStore.CreateKey();
Expand Down Expand Up @@ -311,6 +312,7 @@ public CheckState CheckState

if (value != CheckState)
{
_prevCheckState = CheckState;
Properties.SetInteger(s_propCheckState, (int)value);
OnCheckedChanged(EventArgs.Empty);
OnCheckStateChanged(EventArgs.Empty);
Expand Down Expand Up @@ -820,6 +822,13 @@ protected virtual void OnCheckedChanged(EventArgs e)
protected virtual void OnCheckStateChanged(EventArgs e)
{
AccessibilityNotifyClients(AccessibleEvents.StateChange);

if (IsAccessibilityObjectCreated &&
AccessibilityObject is ToolStripMenuItemAccessibleObject accessibilityObject)
{
accessibilityObject.OnCheckStateChanged(_prevCheckState, CheckState);
}

((EventHandler?)Events[s_eventCheckStateChanged])?.Invoke(this, e);
}

Expand Down Expand Up @@ -1047,8 +1056,14 @@ protected override void OnPaint(PaintEventArgs e)

protected internal override bool ProcessCmdKey(ref Message m, Keys keyData)
{
if (Enabled && ShortcutKeys == keyData && !HasDropDownItems)
if (Enabled && !HasDropDownItems && (ShortcutKeys == keyData || keyData == Keys.Space))
{
if (keyData is Keys.Space)
{
Checked = CheckOnClick ? !Checked : Checked;
return true;
}

FireEvent(ToolStripItemEventType.Click);
return true;
}
Expand Down