mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 18:57:19 +02:00
What the title says. 😄
Rather than relying on the potentially overloaded `!=` or `==` operators
when checking for null, now we'll use the `is` expression (possibly
combined with the `not` operator) to ensure correct checking. Probably
overkill for many of these classes, but decided to err on the side of
consistency. Would matter more on classes that may be inherited or
extended.
Using `is` and `is not` will provide us a guarantee that no
user-overloaded equality operators (`==`/`!=`) is invoked when a
`expression is null` is evaluated.
In code form, changed all instances of:
```c#
something != null
something == null
```
to:
```c#
something is not null
something is null
```
The one exception was checking null on a `KeyChord`. `KeyChord` is a
struct which is never null so VS will raise an error when trying this
versus just providing a warning when using `keyChord != null`. In
reality, we shouldn't do this check because it can't ever be null. In
the case of a `KeyChord` it **would** be a `KeyChord` equivalent to:
```c#
KeyChord keyChord = new ()
{
Modifiers = 0,
Vkey = 0,
ScanCode = 0
};
```
144 lines
5.0 KiB
C#
144 lines
5.0 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using Microsoft.CmdPal.Core.ViewModels;
|
|
using Microsoft.CmdPal.Core.ViewModels.Messages;
|
|
using Microsoft.CmdPal.UI.Messages;
|
|
using Microsoft.CmdPal.UI.Views;
|
|
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
using Microsoft.UI.Xaml.Controls.Primitives;
|
|
using Windows.System;
|
|
|
|
namespace Microsoft.CmdPal.UI.Controls;
|
|
|
|
public sealed partial class CommandBar : UserControl,
|
|
IRecipient<OpenContextMenuMessage>,
|
|
IRecipient<CloseContextMenuMessage>,
|
|
IRecipient<TryCommandKeybindingMessage>,
|
|
ICurrentPageAware
|
|
{
|
|
public CommandBarViewModel ViewModel { get; } = new();
|
|
|
|
public PageViewModel? CurrentPageViewModel
|
|
{
|
|
get => (PageViewModel?)GetValue(CurrentPageViewModelProperty);
|
|
set => SetValue(CurrentPageViewModelProperty, value);
|
|
}
|
|
|
|
// Using a DependencyProperty as the backing store for CurrentPage. This enables animation, styling, binding, etc...
|
|
public static readonly DependencyProperty CurrentPageViewModelProperty =
|
|
DependencyProperty.Register(nameof(CurrentPageViewModel), typeof(PageViewModel), typeof(CommandBar), new PropertyMetadata(null));
|
|
|
|
public CommandBar()
|
|
{
|
|
this.InitializeComponent();
|
|
|
|
// RegisterAll isn't AOT compatible
|
|
WeakReferenceMessenger.Default.Register<OpenContextMenuMessage>(this);
|
|
WeakReferenceMessenger.Default.Register<CloseContextMenuMessage>(this);
|
|
WeakReferenceMessenger.Default.Register<TryCommandKeybindingMessage>(this);
|
|
}
|
|
|
|
public void Receive(OpenContextMenuMessage message)
|
|
{
|
|
if (!ViewModel.ShouldShowContextMenu)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (message.Element is null)
|
|
{
|
|
_ = DispatcherQueue.TryEnqueue(
|
|
() =>
|
|
{
|
|
ContextMenuFlyout.ShowAt(
|
|
MoreCommandsButton,
|
|
new FlyoutShowOptions()
|
|
{
|
|
ShowMode = FlyoutShowMode.Standard,
|
|
Placement = FlyoutPlacementMode.TopEdgeAlignedRight,
|
|
});
|
|
});
|
|
}
|
|
else
|
|
{
|
|
_ = DispatcherQueue.TryEnqueue(
|
|
() =>
|
|
{
|
|
ContextMenuFlyout.ShowAt(
|
|
message.Element!,
|
|
new FlyoutShowOptions()
|
|
{
|
|
ShowMode = FlyoutShowMode.Standard,
|
|
Placement = (FlyoutPlacementMode)message.FlyoutPlacementMode!,
|
|
Position = message.Point,
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
public void Receive(CloseContextMenuMessage message)
|
|
{
|
|
if (ContextMenuFlyout.IsOpen)
|
|
{
|
|
ContextMenuFlyout.Hide();
|
|
}
|
|
}
|
|
|
|
public void Receive(TryCommandKeybindingMessage msg)
|
|
{
|
|
if (!ViewModel.ShouldShowContextMenu)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var result = ViewModel?.CheckKeybinding(msg.Ctrl, msg.Alt, msg.Shift, msg.Win, msg.Key);
|
|
|
|
if (result == ContextKeybindingResult.Hide)
|
|
{
|
|
msg.Handled = true;
|
|
}
|
|
else if (result == ContextKeybindingResult.KeepOpen)
|
|
{
|
|
WeakReferenceMessenger.Default.Send<OpenContextMenuMessage>(new OpenContextMenuMessage(null, null, null, ContextMenuFilterLocation.Bottom));
|
|
msg.Handled = true;
|
|
}
|
|
else if (result == ContextKeybindingResult.Unhandled)
|
|
{
|
|
msg.Handled = false;
|
|
}
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "VS has a tendency to delete XAML bound methods over-aggressively")]
|
|
private void PrimaryButton_Clicked(object sender, RoutedEventArgs e)
|
|
{
|
|
ViewModel.InvokePrimaryCommand();
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "VS has a tendency to delete XAML bound methods over-aggressively")]
|
|
private void SecondaryButton_Clicked(object sender, RoutedEventArgs e)
|
|
{
|
|
ViewModel.InvokeSecondaryCommand();
|
|
}
|
|
|
|
private void SettingsIcon_Clicked(object sender, RoutedEventArgs e)
|
|
{
|
|
WeakReferenceMessenger.Default.Send<OpenSettingsMessage>();
|
|
}
|
|
|
|
private void MoreCommandsButton_Clicked(object sender, RoutedEventArgs e)
|
|
{
|
|
WeakReferenceMessenger.Default.Send<OpenContextMenuMessage>(new OpenContextMenuMessage(null, null, null, ContextMenuFilterLocation.Bottom));
|
|
}
|
|
|
|
private void ContextMenuFlyout_Opened(object sender, object e)
|
|
{
|
|
// We need to wait until our flyout is opened to try and toss focus
|
|
// at its search box. The control isn't in the UI tree before that
|
|
ContextControl.FocusSearchBox();
|
|
}
|
|
}
|