Newplus: Change the built-in newplus status will trigger error (#46029)

<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
Fix #46026
<!-- Please review the items on the PR checklist before submitting-->

## PR Checklist

- [ ] Closes: #46026
<!-- - [ ] Closes: #yyy (add separate lines for additional resolved
issues) -->
- [ ] **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

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed



https://github.com/user-attachments/assets/9150c15e-6478-46f2-92fa-771cdcc0ad01
This commit is contained in:
Kai Tao
2026-03-10 16:58:46 +08:00
committed by GitHub
parent 90131e35d9
commit f82fb2a411
2 changed files with 56 additions and 25 deletions

View File

@@ -4452,6 +4452,10 @@ Activate by holding the key for the character you want to add an accent to, then
<value>Hide the built-in "New" context menu</value>
<comment>Localize New in accordance with Windows New</comment>
</data>
<data name="NewPlus_BuiltInNewRegistryUpdateError" xml:space="preserve">
<value>Couldn't update the built-in "New" context menu setting.</value>
<comment>User-facing error shown when New+ fails to update the built-in New registry setting</comment>
</data>
<data name="NewPlus_behavior.Header" xml:space="preserve">
<value>Behavior</value>
<comment>New+ behavior related settings label</comment>

View File

@@ -12,6 +12,7 @@ using System.Windows;
using global::PowerToys.GPOWrapper;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
@@ -124,7 +125,10 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
// Re-enable built-in New handler when NewPlus is disabled if allowed by GPO
if (!IsNewPlusHideBuiltInNewToggleSettingGPOConfigured)
{
EnableBuiltInNewViaRegistry();
if (EnableBuiltInNewViaRegistry())
{
UpdateHideBuiltInNewState(false);
}
}
}
}
@@ -253,22 +257,15 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
if (_disableBuiltInNew != value)
{
// Update New visibility right now
if (_disableBuiltInNew)
{
EnableBuiltInNewViaRegistry();
}
else
{
DisableBuiltInNewViaRegistry();
}
bool registryUpdateSucceeded = value ? DisableBuiltInNewViaRegistry() : EnableBuiltInNewViaRegistry();
_disableBuiltInNew = value;
if (!registryUpdateSucceeded)
{
OnPropertyChanged(nameof(HideBuiltInNew));
return;
}
// Set the user preference for New visibility, which we then also use in powertoys_module.cpp to ensure
// that backup/restore of settings work without having to update settings
Settings.Properties.BuiltInNewHidePreference.Value = value;
NotifySettingsChanged();
UpdateHideBuiltInNewState(value);
}
}
}
@@ -393,6 +390,12 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
using (var newKey = Registry.CurrentUser.OpenSubKey(BuiltInNewRegistryPath, writable: false))
{
if (newKey is null)
{
// Missing key means Explorer is using the built-in handler state, which is enabled.
return true;
}
string builtInNewHandlerValue = newKey.GetValue(null, null) as string;
if (builtInNewHandlerValue is null || string.Equals(builtInNewHandlerValue, BuiltNewCOMGuid, StringComparison.OrdinalIgnoreCase))
@@ -412,18 +415,34 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
return false;
}
private void DisableBuiltInNewViaRegistry()
private void UpdateHideBuiltInNewState(bool hideBuiltInNew)
{
_disableBuiltInNew = hideBuiltInNew;
OnPropertyChanged(nameof(HideBuiltInNew));
// Set the user preference for New visibility, which we then also use in powertoys_module.cpp to ensure
// that backup/restore of settings work without having to update settings
Settings.Properties.BuiltInNewHidePreference.Value = hideBuiltInNew;
NotifySettingsChanged();
}
private bool DisableBuiltInNewViaRegistry()
{
try
{
using (var newKey = Registry.CurrentUser.OpenSubKey(BuiltInNewRegistryPath, writable: true))
using (var newKey = Registry.CurrentUser.CreateSubKey(BuiltInNewRegistryPath, writable: true))
{
if (newKey is null)
{
throw new InvalidOperationException("Failed to open or create the registry key for built-in New.");
}
string builtInNewHandlerValue = newKey.GetValue(null, null) as string;
if (!string.IsNullOrEmpty(builtInNewHandlerValue) && builtInNewHandlerValue.StartsWith(NewDisabledValuePrefix, StringComparison.OrdinalIgnoreCase))
{
// Already disabled
return;
return true;
}
// Any string value will disable the built-in New handler
@@ -431,42 +450,50 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
newKey.SetValue(string.Empty, newDisabledValue);
}
HideBuiltInNew = true;
OnPropertyChanged(nameof(HideBuiltInNew));
return true;
}
catch (Exception ex)
{
Logger.LogError("Failed to disable built-in New in the registry.", ex);
MessageBox.Show(ex.Message);
}
MessageBox.Show(ResourceLoaderInstance.ResourceLoader.GetString("NewPlus_BuiltInNewRegistryUpdateError"));
}
private void EnableBuiltInNewViaRegistry()
return false;
}
private bool EnableBuiltInNewViaRegistry()
{
try
{
using (var newKey = Registry.CurrentUser.OpenSubKey(BuiltInNewRegistryPath, writable: true))
{
if (newKey is null)
{
// Missing key means the built-in New handler is already enabled.
return true;
}
string builtInNewHandlerValue = newKey.GetValue(null, null) as string;
if (builtInNewHandlerValue is null)
{
// Already enabled
return;
return true;
}
// Null key default value enables built-in New handler
newKey.DeleteValue(null, true);
}
HideBuiltInNew = false;
OnPropertyChanged(nameof(HideBuiltInNew));
return true;
}
catch (Exception ex)
{
Logger.LogError("Failed to enable built-in New in the registry.", ex);
MessageBox.Show(ex.Message);
}
MessageBox.Show(ResourceLoaderInstance.ResourceLoader.GetString("NewPlus_BuiltInNewRegistryUpdateError"));
}
return false;
}
}
}