mirror of
https://github.com/microsoft/PowerToys
synced 2025-08-30 05:57:42 +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
};
```
177 lines
4.8 KiB
C#
177 lines
4.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 System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using ManagedCommon;
|
|
using Microsoft.CmdPal.Ext.Bookmarks.Properties;
|
|
using Microsoft.CmdPal.Ext.Indexer;
|
|
using Microsoft.CommandPalette.Extensions;
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
|
|
namespace Microsoft.CmdPal.Ext.Bookmarks;
|
|
|
|
public partial class BookmarksCommandProvider : CommandProvider
|
|
{
|
|
private readonly List<CommandItem> _commands = [];
|
|
|
|
private readonly AddBookmarkPage _addNewCommand = new(null);
|
|
|
|
private Bookmarks? _bookmarks;
|
|
|
|
public BookmarksCommandProvider()
|
|
{
|
|
Id = "Bookmarks";
|
|
DisplayName = Resources.bookmarks_display_name;
|
|
Icon = Icons.PinIcon;
|
|
|
|
_addNewCommand.AddedCommand += AddNewCommand_AddedCommand;
|
|
}
|
|
|
|
private void AddNewCommand_AddedCommand(object sender, BookmarkData args)
|
|
{
|
|
ExtensionHost.LogMessage($"Adding bookmark ({args.Name},{args.Bookmark})");
|
|
_bookmarks?.Data.Add(args);
|
|
|
|
SaveAndUpdateCommands();
|
|
}
|
|
|
|
// In the edit path, `args` was already in _bookmarks, we just updated it
|
|
private void Edit_AddedCommand(object sender, BookmarkData args)
|
|
{
|
|
ExtensionHost.LogMessage($"Edited bookmark ({args.Name},{args.Bookmark})");
|
|
|
|
SaveAndUpdateCommands();
|
|
}
|
|
|
|
private void SaveAndUpdateCommands()
|
|
{
|
|
if (_bookmarks is not null)
|
|
{
|
|
var jsonPath = BookmarksCommandProvider.StateJsonPath();
|
|
Bookmarks.WriteToFile(jsonPath, _bookmarks);
|
|
}
|
|
|
|
LoadCommands();
|
|
RaiseItemsChanged(0);
|
|
}
|
|
|
|
private void LoadCommands()
|
|
{
|
|
List<CommandItem> collected = [];
|
|
collected.Add(new CommandItem(_addNewCommand));
|
|
|
|
if (_bookmarks is null)
|
|
{
|
|
LoadBookmarksFromFile();
|
|
}
|
|
|
|
if (_bookmarks is not null)
|
|
{
|
|
collected.AddRange(_bookmarks.Data.Select(BookmarkToCommandItem));
|
|
}
|
|
|
|
_commands.Clear();
|
|
_commands.AddRange(collected);
|
|
}
|
|
|
|
private void LoadBookmarksFromFile()
|
|
{
|
|
try
|
|
{
|
|
var jsonFile = StateJsonPath();
|
|
if (File.Exists(jsonFile))
|
|
{
|
|
_bookmarks = Bookmarks.ReadFromFile(jsonFile);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError(ex.Message);
|
|
}
|
|
|
|
if (_bookmarks is null)
|
|
{
|
|
_bookmarks = new();
|
|
}
|
|
}
|
|
|
|
private CommandItem BookmarkToCommandItem(BookmarkData bookmark)
|
|
{
|
|
ICommand command = bookmark.IsPlaceholder ?
|
|
new BookmarkPlaceholderPage(bookmark) :
|
|
new UrlCommand(bookmark);
|
|
|
|
var listItem = new CommandItem(command) { Icon = command.Icon };
|
|
|
|
List<CommandContextItem> contextMenu = [];
|
|
|
|
// Add commands for folder types
|
|
if (command is UrlCommand urlCommand)
|
|
{
|
|
if (!bookmark.IsWebUrl())
|
|
{
|
|
contextMenu.Add(
|
|
new CommandContextItem(new DirectoryPage(urlCommand.Url)));
|
|
|
|
contextMenu.Add(
|
|
new CommandContextItem(new OpenInTerminalCommand(urlCommand.Url)));
|
|
}
|
|
}
|
|
|
|
listItem.Title = bookmark.Name;
|
|
listItem.Subtitle = bookmark.Bookmark;
|
|
|
|
var edit = new AddBookmarkPage(bookmark) { Icon = Icons.EditIcon };
|
|
edit.AddedCommand += Edit_AddedCommand;
|
|
contextMenu.Add(new CommandContextItem(edit));
|
|
|
|
var delete = new CommandContextItem(
|
|
title: Resources.bookmarks_delete_title,
|
|
name: Resources.bookmarks_delete_name,
|
|
action: () =>
|
|
{
|
|
if (_bookmarks is not null)
|
|
{
|
|
ExtensionHost.LogMessage($"Deleting bookmark ({bookmark.Name},{bookmark.Bookmark})");
|
|
|
|
_bookmarks.Data.Remove(bookmark);
|
|
|
|
SaveAndUpdateCommands();
|
|
}
|
|
},
|
|
result: CommandResult.KeepOpen())
|
|
{
|
|
IsCritical = true,
|
|
Icon = Icons.DeleteIcon,
|
|
};
|
|
contextMenu.Add(delete);
|
|
|
|
listItem.MoreCommands = contextMenu.ToArray();
|
|
|
|
return listItem;
|
|
}
|
|
|
|
public override ICommandItem[] TopLevelCommands()
|
|
{
|
|
if (_commands.Count == 0)
|
|
{
|
|
LoadCommands();
|
|
}
|
|
|
|
return _commands.ToArray();
|
|
}
|
|
|
|
internal static string StateJsonPath()
|
|
{
|
|
var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal");
|
|
Directory.CreateDirectory(directory);
|
|
|
|
// now, the state is just next to the exe
|
|
return System.IO.Path.Combine(directory, "bookmarks.json");
|
|
}
|
|
}
|