mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
_⚠️ targets #40427_ This is a different approach to #39059 that I was thinking about like a month ago. It builds on the work from the rejuv'd run page (#39955) to process the bookmark as an exe/path/url automatically. I need to cross-check this with #39059 - I haven't cached that back in since I got back from leave. I remember thinking that I wanted to try this approach, but wasn't sure if it was right. More than anything, I want to get it off my local PC and out for discussion * We don't need to manually store the type anymore. * breaking change: paths with a space do need to be wrapped in spaces closes #38700 ---- I accidentally destroyed #40430 with a fat-finger merge from #40427 into it. This resurrects that PR
84 lines
2.7 KiB
C#
84 lines
2.7 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.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using Microsoft.CmdPal.Ext.Bookmarks.Properties;
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
using Windows.Foundation;
|
|
|
|
namespace Microsoft.CmdPal.Ext.Bookmarks;
|
|
|
|
internal sealed partial class AddBookmarkForm : FormContent
|
|
{
|
|
internal event TypedEventHandler<object, BookmarkData>? AddedCommand;
|
|
|
|
private readonly BookmarkData? _bookmark;
|
|
|
|
public AddBookmarkForm(BookmarkData? bookmark)
|
|
{
|
|
_bookmark = bookmark;
|
|
var name = _bookmark?.Name ?? string.Empty;
|
|
var url = _bookmark?.Bookmark ?? string.Empty;
|
|
TemplateJson = $$"""
|
|
{
|
|
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
|
|
"type": "AdaptiveCard",
|
|
"version": "1.5",
|
|
"body": [
|
|
{
|
|
"type": "Input.Text",
|
|
"style": "text",
|
|
"id": "name",
|
|
"label": "{{Resources.bookmarks_form_name_label}}",
|
|
"value": {{JsonSerializer.Serialize(name, BookmarkSerializationContext.Default.String)}},
|
|
"isRequired": true,
|
|
"errorMessage": "{{Resources.bookmarks_form_name_required}}"
|
|
},
|
|
{
|
|
"type": "Input.Text",
|
|
"style": "text",
|
|
"id": "bookmark",
|
|
"value": {{JsonSerializer.Serialize(url, BookmarkSerializationContext.Default.String)}},
|
|
"label": "{{Resources.bookmarks_form_bookmark_label}}",
|
|
"isRequired": true,
|
|
"errorMessage": "{{Resources.bookmarks_form_bookmark_required}}"
|
|
}
|
|
],
|
|
"actions": [
|
|
{
|
|
"type": "Action.Submit",
|
|
"title": "{{Resources.bookmarks_form_save}}",
|
|
"data": {
|
|
"name": "name",
|
|
"bookmark": "bookmark"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
""";
|
|
}
|
|
|
|
public override CommandResult SubmitForm(string payload)
|
|
{
|
|
var formInput = JsonNode.Parse(payload);
|
|
if (formInput == null)
|
|
{
|
|
return CommandResult.GoHome();
|
|
}
|
|
|
|
// get the name and url out of the values
|
|
var formName = formInput["name"] ?? string.Empty;
|
|
var formBookmark = formInput["bookmark"] ?? string.Empty;
|
|
var hasPlaceholder = formBookmark.ToString().Contains('{') && formBookmark.ToString().Contains('}');
|
|
|
|
var updated = _bookmark ?? new BookmarkData();
|
|
updated.Name = formName.ToString();
|
|
updated.Bookmark = formBookmark.ToString();
|
|
|
|
AddedCommand?.Invoke(this, updated);
|
|
return CommandResult.GoHome();
|
|
}
|
|
}
|