mirror of
https://github.com/microsoft/PowerToys
synced 2025-08-22 01:58:04 +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
};
```
82 lines
2.1 KiB
C#
82 lines
2.1 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 System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.CommandPalette.Extensions;
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
|
|
namespace Microsoft.CmdPal.Common;
|
|
|
|
public partial class ExtensionHostInstance
|
|
{
|
|
public IExtensionHost? Host { get; private set; }
|
|
|
|
public void Initialize(IExtensionHost host) => Host = host;
|
|
|
|
/// <summary>
|
|
/// Fire-and-forget a log message to the Command Palette host app. Since
|
|
/// the host is in another process, we do this in a try/catch in a
|
|
/// background thread, as to not block the calling thread, nor explode if
|
|
/// the host app is gone.
|
|
/// </summary>
|
|
/// <param name="message">The log message to send</param>
|
|
public void LogMessage(ILogMessage message)
|
|
{
|
|
if (Host is not null)
|
|
{
|
|
_ = Task.Run(async () =>
|
|
{
|
|
try
|
|
{
|
|
await Host.LogMessage(message);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public void LogMessage(string message)
|
|
{
|
|
var logMessage = new LogMessage() { Message = message };
|
|
LogMessage(logMessage);
|
|
}
|
|
|
|
public void ShowStatus(IStatusMessage message, StatusContext context)
|
|
{
|
|
if (Host is not null)
|
|
{
|
|
_ = Task.Run(async () =>
|
|
{
|
|
try
|
|
{
|
|
await Host.ShowStatus(message, context);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public void HideStatus(IStatusMessage message)
|
|
{
|
|
if (Host is not null)
|
|
{
|
|
_ = Task.Run(async () =>
|
|
{
|
|
try
|
|
{
|
|
await Host.HideStatus(message);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|