From 759f5c02cbe188622315bfad2ee28fa84af094c8 Mon Sep 17 00:00:00 2001 From: leileizhang Date: Wed, 20 Aug 2025 16:47:38 +0800 Subject: [PATCH] [Cmdpal] Use DynamicDependency to preserve trimmed Adaptive Card action types (#41027) ## Summary of the Pull Request 1. Preserve Adaptive Card action types during trimming using DynamicDependency 2. Revert PR https://github.com/microsoft/PowerToys/pull/41010 ## PR Checklist - [x] Closes: #40979 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed --- .../ContentFormViewModel.cs | 120 ++++-------------- .../Pages/SampleContentPage.cs | 5 - 2 files changed, 26 insertions(+), 99 deletions(-) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ContentFormViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ContentFormViewModel.cs index 9728e8339e..9b2234fb16 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ContentFormViewModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ContentFormViewModel.cs @@ -2,6 +2,7 @@ // 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.CodeAnalysis; using System.Text.Json; using AdaptiveCards.ObjectModel.WinUI3; using AdaptiveCards.Templating; @@ -96,109 +97,40 @@ public partial class ContentFormViewModel(IFormContent _form, WeakReference()` - // or similar will throw a System.InvalidCastException. - // - // Instead we have this horror show. - // - // The `action.ToJson()` blob ACTUALLY CONTAINS THE `type` field, which - // we can use to determine what kind of action it is. Then we can parse - // the JSON manually based on the type. - var actionJson = action.ToJson(); - - if (actionJson.TryGetValue("type", out var actionTypeValue)) + if (action is AdaptiveOpenUrlAction openUrlAction) { - var actionTypeString = actionTypeValue.GetString(); - Logger.LogTrace($"atString={actionTypeString}"); - - var actionType = actionTypeString switch - { - "Action.Submit" => ActionType.Submit, - "Action.Execute" => ActionType.Execute, - "Action.OpenUrl" => ActionType.OpenUrl, - _ => ActionType.Unsupported, - }; - - Logger.LogDebug($"{actionTypeString}->{actionType}"); - - switch (actionType) - { - case ActionType.OpenUrl: - { - HandleOpenUrlAction(action, actionJson); - } - - break; - case ActionType.Submit: - case ActionType.Execute: - { - HandleSubmitAction(action, actionJson, inputs); - } - - break; - default: - Logger.LogError($"{actionType} was an unexpected action `type`"); - break; - } - } - else - { - Logger.LogError($"actionJson.TryGetValue(type) failed"); - } - } - - private void HandleOpenUrlAction(IAdaptiveActionElement action, JsonObject actionJson) - { - if (actionJson.TryGetValue("url", out var actionUrlValue)) - { - var actionUrl = actionUrlValue.GetString() ?? string.Empty; - if (Uri.TryCreate(actionUrl, default(UriCreationOptions), out var uri)) - { - WeakReferenceMessenger.Default.Send(new(uri)); - } - else - { - Logger.LogError($"Failed to produce URI for {actionUrlValue}"); - } - } - } - - private void HandleSubmitAction( - IAdaptiveActionElement action, - JsonObject actionJson, - JsonObject inputs) - { - var dataString = string.Empty; - if (actionJson.TryGetValue("data", out var actionDataValue)) - { - dataString = actionDataValue.Stringify() ?? string.Empty; + WeakReferenceMessenger.Default.Send(new(openUrlAction.Url)); + return; } - var inputString = inputs.Stringify(); - _ = Task.Run(() => + if (action is AdaptiveSubmitAction or AdaptiveExecuteAction) { - try + // Get the data and inputs + var dataString = (action as AdaptiveSubmitAction)?.DataJson.Stringify() ?? string.Empty; + var inputString = inputs.Stringify(); + + _ = Task.Run(() => { - var model = _formModel.Unsafe!; - if (model != null) + try { - var result = model.SubmitForm(inputString, dataString); - Logger.LogDebug($"SubmitForm() returned {result}"); - WeakReferenceMessenger.Default.Send(new(new(result))); + var model = _formModel.Unsafe!; + if (model != null) + { + var result = model.SubmitForm(inputString, dataString); + WeakReferenceMessenger.Default.Send(new(new(result))); + } } - } - catch (Exception ex) - { - ShowException(ex); - } - }); + catch (Exception ex) + { + ShowException(ex); + } + }); + } } private static readonly string ErrorCardJson = """ diff --git a/src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleContentPage.cs b/src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleContentPage.cs index 3d5b49f61d..0584d96ee6 100644 --- a/src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleContentPage.cs +++ b/src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleContentPage.cs @@ -225,11 +225,6 @@ internal sealed partial class SampleContentForm : FormContent } ] } - }, - { - "type": "Action.OpenUrl", - "title": "Action.OpenUrl", - "url": "https://adaptivecards.microsoft.com/" } ] }