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
};
```
122 lines
3.8 KiB
C#
122 lines
3.8 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.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using Microsoft.CmdPal.Core.ViewModels.Messages;
|
|
|
|
namespace Microsoft.CmdPal.UI.ViewModels;
|
|
|
|
public partial class AliasManager : ObservableObject
|
|
{
|
|
private readonly TopLevelCommandManager _topLevelCommandManager;
|
|
|
|
// REMEMBER, CommandAlias.SearchPrefix is what we use as keys
|
|
private readonly Dictionary<string, CommandAlias> _aliases;
|
|
|
|
public AliasManager(TopLevelCommandManager tlcManager, SettingsModel settings)
|
|
{
|
|
_topLevelCommandManager = tlcManager;
|
|
_aliases = settings.Aliases;
|
|
|
|
if (_aliases.Count == 0)
|
|
{
|
|
PopulateDefaultAliases();
|
|
}
|
|
}
|
|
|
|
private void AddAlias(CommandAlias a) => _aliases.Add(a.SearchPrefix, a);
|
|
|
|
public bool CheckAlias(string searchText)
|
|
{
|
|
if (_aliases.TryGetValue(searchText, out var alias))
|
|
{
|
|
try
|
|
{
|
|
var topLevelCommand = _topLevelCommandManager.LookupCommand(alias.CommandId);
|
|
if (topLevelCommand is not null)
|
|
{
|
|
WeakReferenceMessenger.Default.Send<ClearSearchMessage>();
|
|
|
|
WeakReferenceMessenger.Default.Send<PerformCommandMessage>(topLevelCommand.GetPerformCommandMessage());
|
|
return true;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void PopulateDefaultAliases()
|
|
{
|
|
this.AddAlias(new CommandAlias(":", "com.microsoft.cmdpal.registry", true));
|
|
this.AddAlias(new CommandAlias("$", "com.microsoft.cmdpal.windowsSettings", true));
|
|
this.AddAlias(new CommandAlias("=", "com.microsoft.cmdpal.calculator", true));
|
|
this.AddAlias(new CommandAlias(">", "com.microsoft.cmdpal.shell", true));
|
|
this.AddAlias(new CommandAlias("<", "com.microsoft.cmdpal.windowwalker", true));
|
|
this.AddAlias(new CommandAlias("??", "com.microsoft.cmdpal.websearch", true));
|
|
this.AddAlias(new CommandAlias("file", "com.microsoft.indexer.fileSearch", false));
|
|
this.AddAlias(new CommandAlias(")", "com.microsoft.cmdpal.timedate", true));
|
|
}
|
|
|
|
public string? KeysFromId(string commandId)
|
|
{
|
|
return _aliases
|
|
.Where(kv => kv.Value.CommandId == commandId)
|
|
.Select(kv => kv.Value.Alias)
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
public CommandAlias? AliasFromId(string commandId)
|
|
{
|
|
return _aliases
|
|
.Where(kv => kv.Value.CommandId == commandId)
|
|
.Select(kv => kv.Value)
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
public void UpdateAlias(string commandId, CommandAlias? newAlias)
|
|
{
|
|
if (string.IsNullOrEmpty(commandId))
|
|
{
|
|
// do nothing?
|
|
return;
|
|
}
|
|
|
|
// If we already have _this exact alias_, do nothing
|
|
if (newAlias is not null &&
|
|
_aliases.TryGetValue(newAlias.SearchPrefix, out var existingAlias))
|
|
{
|
|
if (existingAlias.CommandId == commandId)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Look for the old alias, and remove it
|
|
List<CommandAlias> toRemove = [];
|
|
foreach (var kv in _aliases)
|
|
{
|
|
if (kv.Value.CommandId == commandId)
|
|
{
|
|
toRemove.Add(kv.Value);
|
|
}
|
|
}
|
|
|
|
foreach (var alias in toRemove)
|
|
{
|
|
// REMEMBER, SearchPrefix is what we use as keys
|
|
_aliases.Remove(alias.SearchPrefix);
|
|
}
|
|
|
|
if (newAlias is not null)
|
|
{
|
|
AddAlias(newAlias);
|
|
}
|
|
}
|
|
}
|