From ea4397428770bfc365266f8ed5b461e8dac400d7 Mon Sep 17 00:00:00 2001 From: Shawn Yuan <128874481+shuaiyuanxx@users.noreply.github.com> Date: Mon, 26 Jan 2026 15:28:59 +0800 Subject: [PATCH] [Settings] [Advanced Paste] Upgrade advanced paste settings safely to fix settings ui crash (#44862) ## Summary of the Pull Request This pull request makes a minor fix in the `AdvancedPasteViewModel` constructor to ensure the correct settings repository is used for null checking. The change improves code correctness by verifying `advancedPasteSettingsRepository` instead of the generic `settingsRepository`. - Fixed null check to use `advancedPasteSettingsRepository` instead of `settingsRepository` in the `AdvancedPasteViewModel` constructor for more accurate validation. ## PR Checklist - [x] Closes: #44835 - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **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 --- .../AdvancedPastePasteAsFileAction.cs | 6 ++--- .../AdvancedPasteProperties.cs | 4 +-- .../AdvancedPasteTranscodeAction.cs | 4 +-- .../ViewModels/AdvancedPasteViewModel.cs | 26 ++++++++++++++----- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/settings-ui/Settings.UI.Library/AdvancedPastePasteAsFileAction.cs b/src/settings-ui/Settings.UI.Library/AdvancedPastePasteAsFileAction.cs index c4489eaaf7..b645c68cb5 100644 --- a/src/settings-ui/Settings.UI.Library/AdvancedPastePasteAsFileAction.cs +++ b/src/settings-ui/Settings.UI.Library/AdvancedPastePasteAsFileAction.cs @@ -34,21 +34,21 @@ public sealed class AdvancedPastePasteAsFileAction : Observable, IAdvancedPasteA public AdvancedPasteAdditionalAction PasteAsTxtFile { get => _pasteAsTxtFile; - init => Set(ref _pasteAsTxtFile, value); + init => Set(ref _pasteAsTxtFile, value ?? new()); } [JsonPropertyName(PropertyNames.PasteAsPngFile)] public AdvancedPasteAdditionalAction PasteAsPngFile { get => _pasteAsPngFile; - init => Set(ref _pasteAsPngFile, value); + init => Set(ref _pasteAsPngFile, value ?? new()); } [JsonPropertyName(PropertyNames.PasteAsHtmlFile)] public AdvancedPasteAdditionalAction PasteAsHtmlFile { get => _pasteAsHtmlFile; - init => Set(ref _pasteAsHtmlFile, value); + init => Set(ref _pasteAsHtmlFile, value ?? new()); } [JsonIgnore] diff --git a/src/settings-ui/Settings.UI.Library/AdvancedPasteProperties.cs b/src/settings-ui/Settings.UI.Library/AdvancedPasteProperties.cs index 9e2fa7ee12..ecfa0ce636 100644 --- a/src/settings-ui/Settings.UI.Library/AdvancedPasteProperties.cs +++ b/src/settings-ui/Settings.UI.Library/AdvancedPasteProperties.cs @@ -93,11 +93,11 @@ namespace Microsoft.PowerToys.Settings.UI.Library [JsonPropertyName("custom-actions")] [CmdConfigureIgnoreAttribute] - public AdvancedPasteCustomActions CustomActions { get; init; } + public AdvancedPasteCustomActions CustomActions { get; set; } [JsonPropertyName("additional-actions")] [CmdConfigureIgnoreAttribute] - public AdvancedPasteAdditionalActions AdditionalActions { get; init; } + public AdvancedPasteAdditionalActions AdditionalActions { get; set; } [JsonPropertyName("paste-ai-configuration")] [CmdConfigureIgnoreAttribute] diff --git a/src/settings-ui/Settings.UI.Library/AdvancedPasteTranscodeAction.cs b/src/settings-ui/Settings.UI.Library/AdvancedPasteTranscodeAction.cs index 82ea4d09f5..e0ed7d7421 100644 --- a/src/settings-ui/Settings.UI.Library/AdvancedPasteTranscodeAction.cs +++ b/src/settings-ui/Settings.UI.Library/AdvancedPasteTranscodeAction.cs @@ -32,14 +32,14 @@ public sealed class AdvancedPasteTranscodeAction : Observable, IAdvancedPasteAct public AdvancedPasteAdditionalAction TranscodeToMp3 { get => _transcodeToMp3; - init => Set(ref _transcodeToMp3, value); + init => Set(ref _transcodeToMp3, value ?? new()); } [JsonPropertyName(PropertyNames.TranscodeToMp4)] public AdvancedPasteAdditionalAction TranscodeToMp4 { get => _transcodeToMp4; - init => Set(ref _transcodeToMp4, value); + init => Set(ref _transcodeToMp4, value ?? new()); } [JsonIgnore] diff --git a/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs index c98242d36b..deb47719e1 100644 --- a/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs @@ -76,16 +76,24 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels GeneralSettingsConfig = settingsRepository.SettingsConfig; - // To obtain the settings configurations of Fancy zones. - ArgumentNullException.ThrowIfNull(settingsRepository); + // To obtain the settings configurations of Advanced Paste. + ArgumentNullException.ThrowIfNull(advancedPasteSettingsRepository); _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); - ArgumentNullException.ThrowIfNull(advancedPasteSettingsRepository); + _advancedPasteSettings = advancedPasteSettingsRepository.SettingsConfig ?? throw new ArgumentException("SettingsConfig cannot be null", nameof(advancedPasteSettingsRepository)); - _advancedPasteSettings = advancedPasteSettingsRepository.SettingsConfig; + if (_advancedPasteSettings.Properties is null) + { + throw new ArgumentException("AdvancedPasteSettings.Properties cannot be null", nameof(advancedPasteSettingsRepository)); + } + + // Ensure AdditionalActions and CustomActions are initialized to prevent null reference exceptions + // This handles legacy settings files that may be missing these properties + _advancedPasteSettings.Properties.AdditionalActions ??= new AdvancedPasteAdditionalActions(); + _advancedPasteSettings.Properties.CustomActions ??= new AdvancedPasteCustomActions(); AttachConfigurationHandlers(); @@ -93,7 +101,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels SendConfigMSG = ipcMSGCallBackFunc; _additionalActions = _advancedPasteSettings.Properties.AdditionalActions; - _customActions = _advancedPasteSettings.Properties.CustomActions.Value; + _customActions = _advancedPasteSettings.Properties.CustomActions.Value ?? new ObservableCollection(); SetupSettingsFileWatcher(); @@ -469,7 +477,13 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels public PasteAIConfiguration PasteAIConfiguration { - get => _advancedPasteSettings.Properties.PasteAIConfiguration; + get + { + // Ensure PasteAIConfiguration is never null for XAML binding + _advancedPasteSettings.Properties.PasteAIConfiguration ??= new PasteAIConfiguration(); + return _advancedPasteSettings.Properties.PasteAIConfiguration; + } + set { if (!ReferenceEquals(value, _advancedPasteSettings.Properties.PasteAIConfiguration))