Skip to content

Commit fa07c1b

Browse files
SimonZhao888Simon Zhao (BEYONDSOFT CONSULTING INC)
and
Simon Zhao (BEYONDSOFT CONSULTING INC)
authored
Make the toolbar menu items support the support space key. (#11520)
* Add Space key to ToolStripMenuItem to keep the same behavior with ToolStripButton * Make the Enter key only close the dropdown strip * Support space key to ToolStripMenuItem * Fix accessibility bug * Optimized code --------- Co-authored-by: Simon Zhao (BEYONDSOFT CONSULTING INC) <[email protected]>
1 parent c2c7376 commit fa07c1b

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripDropDown.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,7 @@ protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
12531253
|| !(dismissingItem.HasDropDownItems))
12541254
{ // clicking on a item w/dropdown does not dismiss window
12551255
Close(ToolStripDropDownCloseReason.ItemClicked);
1256+
SelectPreviousToolStrip();
12561257
}
12571258
}
12581259
}
@@ -1359,7 +1360,7 @@ internal void SelectPreviousToolStrip()
13591360
{
13601361
// snap the owner item before calling hide as non-auto created dropdowns will
13611362
// exit menu mode if there's no OwnerItem.
1362-
ToolStripItem? itemOnPreviousMenuToSelect = OwnerItem;
1363+
ToolStripItem? itemOnPreviousMenuToSelect = GetToplevelOwnerItem();
13631364
Hide();
13641365

13651366
if (itemOnPreviousMenuToSelect is not null)

src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripMenuItem.ToolStripMenuItemAccessibleObject.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ internal override void Toggle()
146146
_ => ToggleState.ToggleState_Indeterminate
147147
};
148148

149+
internal void OnCheckStateChanged(CheckState oldValue, CheckState newValue)
150+
{
151+
RaiseAutomationPropertyChangedEvent(
152+
UIA_PROPERTY_ID.UIA_ToggleToggleStatePropertyId,
153+
(VARIANT)(int)CheckStateToToggleState(oldValue),
154+
(VARIANT)(int)CheckStateToToggleState(newValue));
155+
}
156+
157+
private static ToggleState CheckStateToToggleState(CheckState checkState)
158+
=> checkState switch
159+
{
160+
CheckState.Checked => ToggleState.ToggleState_On,
161+
CheckState.Unchecked => ToggleState.ToggleState_Off,
162+
_ => ToggleState.ToggleState_Indeterminate
163+
};
164+
149165
#endregion
150166
}
151167
}

src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripMenuItem.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace System.Windows.Forms;
1919
$"System.ComponentModel.Design.Serialization.CodeDomSerializer, {AssemblyRef.SystemDesign}")]
2020
public partial class ToolStripMenuItem : ToolStripDropDownItem
2121
{
22+
private CheckState _prevCheckState = CheckState.Unchecked;
2223
private static readonly MenuTimer s_menuTimer = new();
2324

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

312313
if (value != CheckState)
313314
{
315+
_prevCheckState = CheckState;
314316
Properties.SetInteger(s_propCheckState, (int)value);
315317
OnCheckedChanged(EventArgs.Empty);
316318
OnCheckStateChanged(EventArgs.Empty);
@@ -820,6 +822,13 @@ protected virtual void OnCheckedChanged(EventArgs e)
820822
protected virtual void OnCheckStateChanged(EventArgs e)
821823
{
822824
AccessibilityNotifyClients(AccessibleEvents.StateChange);
825+
826+
if (IsAccessibilityObjectCreated &&
827+
AccessibilityObject is ToolStripMenuItemAccessibleObject accessibilityObject)
828+
{
829+
accessibilityObject.OnCheckStateChanged(_prevCheckState, CheckState);
830+
}
831+
823832
((EventHandler?)Events[s_eventCheckStateChanged])?.Invoke(this, e);
824833
}
825834

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

10481057
protected internal override bool ProcessCmdKey(ref Message m, Keys keyData)
10491058
{
1050-
if (Enabled && ShortcutKeys == keyData && !HasDropDownItems)
1059+
if (Enabled && !HasDropDownItems && (ShortcutKeys == keyData || keyData == Keys.Space))
10511060
{
1061+
if (keyData is Keys.Space)
1062+
{
1063+
Checked = CheckOnClick ? !Checked : Checked;
1064+
return true;
1065+
}
1066+
10521067
FireEvent(ToolStripItemEventType.Click);
10531068
return true;
10541069
}

0 commit comments

Comments
 (0)