mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-01-15 08:46:37 +01:00
Fix form pages
Also, add the spongebot settings page back to test This fixes the form bubbling thing I was talking about with @crutkas
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
// 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.IO;
|
||||
using System.Text.Json.Nodes;
|
||||
using Microsoft.Windows.CommandPalette.Extensions;
|
||||
using Microsoft.Windows.CommandPalette.Extensions.Helpers;
|
||||
|
||||
namespace SpongebotExtension;
|
||||
|
||||
internal sealed class SpongeSettingsForm : Form
|
||||
{
|
||||
public override string TemplateJson()
|
||||
{
|
||||
var json = $$"""
|
||||
{
|
||||
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
|
||||
"type": "AdaptiveCard",
|
||||
"version": "1.5",
|
||||
"body": [
|
||||
{
|
||||
"type": "Input.Text",
|
||||
"style": "text",
|
||||
"id": "username",
|
||||
"label": "Username",
|
||||
"isRequired": true,
|
||||
"errorMessage": "Username is required"
|
||||
},
|
||||
{
|
||||
"type": "Input.Text",
|
||||
"style": "password",
|
||||
"id": "password",
|
||||
"label": "Password",
|
||||
"isRequired": true,
|
||||
"errorMessage": "Password is required"
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"type": "Action.Submit",
|
||||
"title": "Save",
|
||||
"data": {
|
||||
"username": "username",
|
||||
"password": "password"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
""";
|
||||
return json;
|
||||
}
|
||||
|
||||
public override string DataJson() => throw new NotImplementedException();
|
||||
|
||||
public override string StateJson() => throw new NotImplementedException();
|
||||
|
||||
public override ActionResult SubmitForm(string payload)
|
||||
{
|
||||
var formInput = JsonNode.Parse(payload);
|
||||
if (formInput == null)
|
||||
{
|
||||
return ActionResult.GoHome();
|
||||
}
|
||||
|
||||
// get the name and url out of the values
|
||||
var formName = formInput["username"] ?? string.Empty;
|
||||
var formPassword = formInput["password"] ?? string.Empty;
|
||||
|
||||
// Construct a new json blob with the name and url
|
||||
var json = $$"""
|
||||
{
|
||||
"username": "{{formName}}",
|
||||
"password": "{{formPassword}}"
|
||||
}
|
||||
""";
|
||||
|
||||
File.WriteAllText(SpongebotPage.StateJsonPath(), json);
|
||||
return ActionResult.GoHome();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.Windows.CommandPalette.Extensions;
|
||||
using Microsoft.Windows.CommandPalette.Extensions.Helpers;
|
||||
|
||||
@@ -14,16 +15,28 @@ internal sealed class SpongebotCommandsProvider : ICommandProvider
|
||||
|
||||
public IconDataType Icon => new(string.Empty);
|
||||
|
||||
private readonly SpongebotPage mainPage = new();
|
||||
|
||||
private readonly SpongebotSettingsPage settingsPage = new();
|
||||
|
||||
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize
|
||||
public void Dispose() => throw new NotImplementedException();
|
||||
#pragma warning restore CA1816 // Dispose methods should call SuppressFinalize
|
||||
|
||||
public IListItem[] TopLevelCommands()
|
||||
{
|
||||
var spongebotPage = new SpongebotPage();
|
||||
var listItem = new ListItem(spongebotPage)
|
||||
var settingsPath = SpongebotPage.StateJsonPath();
|
||||
if (!File.Exists(settingsPath))
|
||||
{
|
||||
MoreCommands = [new CommandContextItem(spongebotPage.CopyTextAction)],
|
||||
return [new ListItem(settingsPage) { Title = "Spongebot settings", Subtitle = "Enter your imgflip credentials" }];
|
||||
}
|
||||
|
||||
var listItem = new ListItem(mainPage)
|
||||
{
|
||||
MoreCommands = [
|
||||
new CommandContextItem(mainPage.CopyTextAction),
|
||||
new CommandContextItem(settingsPage),
|
||||
],
|
||||
};
|
||||
return [listItem];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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 Microsoft.Windows.CommandPalette.Extensions;
|
||||
using Microsoft.Windows.CommandPalette.Extensions.Helpers;
|
||||
|
||||
namespace SpongebotExtension;
|
||||
|
||||
internal sealed class SpongebotSettingsPage : Microsoft.Windows.CommandPalette.Extensions.Helpers.FormPage
|
||||
{
|
||||
private readonly SpongeSettingsForm settingsForm = new();
|
||||
|
||||
public override IForm[] Forms() => [settingsForm];
|
||||
|
||||
public SpongebotSettingsPage()
|
||||
{
|
||||
_Name = "Settings";
|
||||
Icon = new("https://imgflip.com/s/meme/Mocking-Spongebob.jpg");
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using Microsoft.UI.Xaml.Navigation;
|
||||
using Microsoft.Windows.CommandPalette.Extensions;
|
||||
using Microsoft.Windows.CommandPalette.Extensions.Helpers;
|
||||
using Windows.Foundation;
|
||||
using Windows.System;
|
||||
using Windows.UI.ViewManagement;
|
||||
@@ -115,7 +116,9 @@ public sealed class FormPageViewModel : PageViewModel
|
||||
var f = this.Page.Forms();
|
||||
foreach (var form in f)
|
||||
{
|
||||
Forms.Add(new FormViewModel(form));
|
||||
var formVm = new FormViewModel(form);
|
||||
formVm.RequestSubmitForm += RequestSubmitFormBubbler;
|
||||
Forms.Add(formVm);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -133,6 +136,10 @@ public sealed class FormPageViewModel : PageViewModel
|
||||
await t;
|
||||
}
|
||||
|
||||
private void RequestSubmitFormBubbler(object sender, SubmitFormArgs args)
|
||||
{
|
||||
this.SubmitForm(args.FormData, args.Form);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed partial class FormPage : Page
|
||||
|
||||
Reference in New Issue
Block a user