Fix some more bugs. Nullable is very treacherous.

This commit is contained in:
BDisp
2024-08-31 23:43:30 +01:00
committed by Tig
parent 552f9ed0a8
commit 1e9f2f4778
4 changed files with 40 additions and 18 deletions

View File

@@ -670,7 +670,8 @@ internal sealed class Menu : View
_currentChild = 0;
}
if (this != _host.OpenCurrentMenu && _barItems?.Children? [_currentChild].IsFromSubMenu == true && _host._selectedSub > -1)
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
if (this != _host.OpenCurrentMenu && _barItems?.Children? [_currentChild]?.IsFromSubMenu == true && _host._selectedSub > -1)
{
_host.PreviousMenu (true);
_host.SelectEnabledItem (_barItems.Children, _currentChild, out _currentChild);

View File

@@ -108,7 +108,7 @@ public class MenuBarItem : MenuItem
if (menuItem.ShortcutKey != Key.Empty)
{
menuItem._menuBar = menuBar;
menuItem.UpdateShortcutKeyBinding (Key.Empty);
menuItem.AddShortcutKeyBinding (menuBar, Key.Empty);
}
SubMenu (menuItem)?.AddShortcutKeyBindings (menuBar);
@@ -126,7 +126,7 @@ public class MenuBarItem : MenuItem
);
}
Title = title;
SetTitle (title);
if (parent is { })
{
@@ -176,6 +176,12 @@ public class MenuBarItem : MenuItem
}
}
private void SetTitle (string? title)
{
title ??= string.Empty;
Title = title;
}
/// <summary>
/// Add a <see cref="MenuBarItem"/> dynamically into the <see cref="MenuBar"/><c>.Menus</c>.
/// </summary>

View File

@@ -276,6 +276,31 @@ public class MenuItem
/// <summary>Gets the text describing the keystroke combination defined by <see cref="ShortcutKey"/>.</summary>
public string ShortcutTag => ShortcutKey != Key.Empty ? ShortcutKey!.ToString () : string.Empty;
internal void AddShortcutKeyBinding (MenuBar menuBar, Key key)
{
ArgumentNullException.ThrowIfNull (menuBar);
_menuBar = menuBar;
AddOrUpdateShortcutKeyBinding (key);
}
private void AddOrUpdateShortcutKeyBinding (Key key)
{
if (key != Key.Empty)
{
_menuBar.KeyBindings.Remove (key);
}
if (ShortcutKey != Key.Empty)
{
KeyBinding keyBinding = new ([Command.Select], KeyBindingScope.HotKey, this);
// Remove an existent ShortcutKey
_menuBar.KeyBindings.Remove (ShortcutKey!);
_menuBar.KeyBindings.Add (ShortcutKey!, keyBinding);
}
}
private void UpdateHotKeyBinding (Key oldKey)
{
if (_menuBar is null or { IsInitialized: false })
@@ -306,25 +331,15 @@ public class MenuItem
}
}
internal void UpdateShortcutKeyBinding (Key oldKey)
private void UpdateShortcutKeyBinding (Key oldKey)
{
if (_menuBar is null or { IsInitialized: false })
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (_menuBar is null)
{
return;
}
if (oldKey != Key.Empty)
{
_menuBar.KeyBindings.Remove (oldKey);
}
if (ShortcutKey != Key.Empty)
{
KeyBinding keyBinding = new ([Command.Select], KeyBindingScope.HotKey, this);
// Remove an existent ShortcutKey
_menuBar.KeyBindings.Remove (ShortcutKey!);
_menuBar.KeyBindings.Add (ShortcutKey!, keyBinding);
}
AddOrUpdateShortcutKeyBinding (oldKey);
}
#endregion Keyboard Handling

View File

@@ -2938,7 +2938,7 @@ Edit
Assert.Contains (Key.A.WithCtrl, menuBar.KeyBindings.Bindings);
menuBar.Menus [0].Children [0].ShortcutKey = Key.B.WithCtrl;
menuBar.Menus [0].Children! [0].ShortcutKey = Key.B.WithCtrl;
Assert.DoesNotContain (Key.A.WithCtrl, menuBar.KeyBindings.Bindings);
Assert.Contains (Key.B.WithCtrl, menuBar.KeyBindings.Bindings);