mirror of
https://github.com/microsoft/PowerToys
synced 2025-08-22 10:07:37 +00: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
};
```
134 lines
3.3 KiB
C#
134 lines
3.3 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 Microsoft.CmdPal.Core.ViewModels.Models;
|
|
using Microsoft.CommandPalette.Extensions;
|
|
|
|
namespace Microsoft.CmdPal.Core.ViewModels;
|
|
|
|
public partial class CommandViewModel : ExtensionObjectViewModel
|
|
{
|
|
public ExtensionObject<ICommand> Model { get; private set; } = new(null);
|
|
|
|
protected bool IsInitialized { get; private set; }
|
|
|
|
protected bool IsFastInitialized { get; private set; }
|
|
|
|
public bool HasIcon => Icon.IsSet;
|
|
|
|
// These are properties that are "observable" from the extension object
|
|
// itself, in the sense that they get raised by PropChanged events from the
|
|
// extension. However, we don't want to actually make them
|
|
// [ObservableProperty]s, because PropChanged comes in off the UI thread,
|
|
// and ObservableProperty is not smart enough to raise the PropertyChanged
|
|
// on the UI thread.
|
|
public string Id { get; private set; } = string.Empty;
|
|
|
|
public string Name { get; private set; } = string.Empty;
|
|
|
|
public IconInfoViewModel Icon { get; private set; }
|
|
|
|
public CommandViewModel(ICommand? command, WeakReference<IPageContext> pageContext)
|
|
: base(pageContext)
|
|
{
|
|
Model = new(command);
|
|
Icon = new(null);
|
|
}
|
|
|
|
public void FastInitializeProperties()
|
|
{
|
|
if (IsFastInitialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var model = Model.Unsafe;
|
|
if (model is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Id = model.Id ?? string.Empty;
|
|
Name = model.Name ?? string.Empty;
|
|
IsFastInitialized = true;
|
|
}
|
|
|
|
public override void InitializeProperties()
|
|
{
|
|
if (IsInitialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!IsFastInitialized)
|
|
{
|
|
FastInitializeProperties();
|
|
}
|
|
|
|
var model = Model.Unsafe;
|
|
if (model is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var ico = model.Icon;
|
|
if (ico is not null)
|
|
{
|
|
Icon = new(ico);
|
|
Icon.InitializeProperties();
|
|
UpdateProperty(nameof(Icon));
|
|
}
|
|
|
|
model.PropChanged += Model_PropChanged;
|
|
}
|
|
|
|
private void Model_PropChanged(object sender, IPropChangedEventArgs args)
|
|
{
|
|
try
|
|
{
|
|
FetchProperty(args.PropertyName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ShowException(ex, Name);
|
|
}
|
|
}
|
|
|
|
protected void FetchProperty(string propertyName)
|
|
{
|
|
var model = Model.Unsafe;
|
|
if (model is null)
|
|
{
|
|
return; // throw?
|
|
}
|
|
|
|
switch (propertyName)
|
|
{
|
|
case nameof(Name):
|
|
Name = model.Name;
|
|
break;
|
|
case nameof(Icon):
|
|
var iconInfo = model.Icon;
|
|
Icon = new(iconInfo);
|
|
Icon.InitializeProperties();
|
|
break;
|
|
}
|
|
|
|
UpdateProperty(propertyName);
|
|
}
|
|
|
|
protected override void UnsafeCleanup()
|
|
{
|
|
base.UnsafeCleanup();
|
|
|
|
Icon = new(null); // necessary?
|
|
|
|
var model = Model.Unsafe;
|
|
if (model is not null)
|
|
{
|
|
model.PropChanged -= Model_PropChanged;
|
|
}
|
|
}
|
|
}
|