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
};
```
129 lines
4.3 KiB
C#
129 lines
4.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 System.Diagnostics;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.Json.Serialization;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
using Windows.Foundation;
|
|
|
|
namespace Microsoft.CmdPal.UI.ViewModels;
|
|
|
|
public partial class AppStateModel : ObservableObject
|
|
{
|
|
[JsonIgnore]
|
|
public static readonly string FilePath;
|
|
|
|
public event TypedEventHandler<AppStateModel, object?>? StateChanged;
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// STATE HERE
|
|
// Make sure that you make the setters public (JsonSerializer.Deserialize will fail silently otherwise)!
|
|
// Make sure that any new types you add are added to JsonSerializationContext!
|
|
public RecentCommandsManager RecentCommands { get; set; } = new();
|
|
|
|
public List<string> RunHistory { get; set; } = [];
|
|
|
|
// END SETTINGS
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
static AppStateModel()
|
|
{
|
|
FilePath = StateJsonPath();
|
|
}
|
|
|
|
public static AppStateModel LoadState()
|
|
{
|
|
if (string.IsNullOrEmpty(FilePath))
|
|
{
|
|
throw new InvalidOperationException($"You must set a valid {nameof(SettingsModel.FilePath)} before calling {nameof(LoadState)}");
|
|
}
|
|
|
|
if (!File.Exists(FilePath))
|
|
{
|
|
Debug.WriteLine("The provided settings file does not exist");
|
|
return new();
|
|
}
|
|
|
|
try
|
|
{
|
|
// Read the JSON content from the file
|
|
var jsonContent = File.ReadAllText(FilePath);
|
|
|
|
var loaded = JsonSerializer.Deserialize<AppStateModel>(jsonContent, JsonSerializationContext.Default.AppStateModel);
|
|
|
|
Debug.WriteLine(loaded is not null ? "Loaded settings file" : "Failed to parse");
|
|
|
|
return loaded ?? new();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine(ex.ToString());
|
|
}
|
|
|
|
return new();
|
|
}
|
|
|
|
public static void SaveState(AppStateModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(FilePath))
|
|
{
|
|
throw new InvalidOperationException($"You must set a valid {nameof(FilePath)} before calling {nameof(SaveState)}");
|
|
}
|
|
|
|
try
|
|
{
|
|
// Serialize the main dictionary to JSON and save it to the file
|
|
var settingsJson = JsonSerializer.Serialize(model, JsonSerializationContext.Default.AppStateModel);
|
|
|
|
// Is it valid JSON?
|
|
if (JsonNode.Parse(settingsJson) is JsonObject newSettings)
|
|
{
|
|
// Now, read the existing content from the file
|
|
var oldContent = File.Exists(FilePath) ? File.ReadAllText(FilePath) : "{}";
|
|
|
|
// Is it valid JSON?
|
|
if (JsonNode.Parse(oldContent) is JsonObject savedSettings)
|
|
{
|
|
foreach (var item in newSettings)
|
|
{
|
|
savedSettings[item.Key] = item.Value?.DeepClone();
|
|
}
|
|
|
|
var serialized = savedSettings.ToJsonString(JsonSerializationContext.Default.AppStateModel.Options);
|
|
File.WriteAllText(FilePath, serialized);
|
|
|
|
// TODO: Instead of just raising the event here, we should
|
|
// have a file change watcher on the settings file, and
|
|
// reload the settings then
|
|
model.StateChanged?.Invoke(model, null);
|
|
}
|
|
else
|
|
{
|
|
Debug.WriteLine("Failed to parse settings file as JsonObject.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.WriteLine("Failed to parse settings file as JsonObject.");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine(ex.ToString());
|
|
}
|
|
}
|
|
|
|
internal static string StateJsonPath()
|
|
{
|
|
var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal");
|
|
Directory.CreateDirectory(directory);
|
|
|
|
// now, the settings is just next to the exe
|
|
return Path.Combine(directory, "state.json");
|
|
}
|
|
}
|