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
};
```
93 lines
2.6 KiB
C#
93 lines
2.6 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.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.RegularExpressions;
|
|
using Microsoft.CmdPal.Ext.Bookmarks.Properties;
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
|
|
namespace Microsoft.CmdPal.Ext.Bookmarks;
|
|
|
|
internal sealed partial class BookmarkPlaceholderForm : FormContent
|
|
{
|
|
private static readonly CompositeFormat ErrorMessage = System.Text.CompositeFormat.Parse(Resources.bookmarks_required_placeholder);
|
|
|
|
private readonly List<string> _placeholderNames;
|
|
|
|
private readonly string _bookmark = string.Empty;
|
|
|
|
// TODO pass in an array of placeholders
|
|
public BookmarkPlaceholderForm(string name, string url)
|
|
{
|
|
_bookmark = url;
|
|
var r = new Regex(Regex.Escape("{") + "(.*?)" + Regex.Escape("}"));
|
|
var matches = r.Matches(url);
|
|
_placeholderNames = matches.Select(m => m.Groups[1].Value).ToList();
|
|
var inputs = _placeholderNames.Select(p =>
|
|
{
|
|
var errorMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessage, p);
|
|
return $$"""
|
|
{
|
|
"type": "Input.Text",
|
|
"style": "text",
|
|
"id": "{{p}}",
|
|
"label": "{{p}}",
|
|
"isRequired": true,
|
|
"errorMessage": "{{errorMessage}}"
|
|
}
|
|
""";
|
|
}).ToList();
|
|
|
|
var allInputs = string.Join(",", inputs);
|
|
|
|
TemplateJson = $$"""
|
|
{
|
|
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
|
|
"type": "AdaptiveCard",
|
|
"version": "1.5",
|
|
"body": [
|
|
""" + allInputs + $$"""
|
|
],
|
|
"actions": [
|
|
{
|
|
"type": "Action.Submit",
|
|
"title": "{{Resources.bookmarks_form_open}}",
|
|
"data": {
|
|
"placeholder": "placeholder"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
""";
|
|
}
|
|
|
|
public override CommandResult SubmitForm(string payload)
|
|
{
|
|
var target = _bookmark;
|
|
|
|
// parse the submitted JSON and then open the link
|
|
var formInput = JsonNode.Parse(payload);
|
|
var formObject = formInput?.AsObject();
|
|
if (formObject is null)
|
|
{
|
|
return CommandResult.GoHome();
|
|
}
|
|
|
|
foreach (var (key, value) in formObject)
|
|
{
|
|
var placeholderString = $"{{{key}}}";
|
|
var placeholderData = value?.ToString();
|
|
target = target.Replace(placeholderString, placeholderData);
|
|
}
|
|
|
|
var success = UrlCommand.LaunchCommand(target);
|
|
|
|
return success ? CommandResult.Dismiss() : CommandResult.KeepOpen();
|
|
}
|
|
}
|