fixed light switch shortcut not working issue (#42340)

<!-- 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
This pull request refactors how the `ToggleThemeHotkey` property is
handled in the LightSwitch settings and view model. The changes simplify
property management by directly referencing the property within
`LightSwitchProperties` and ensure that the hotkey setting is
consistently updated and serialized.

**Settings property management:**

* In `LightSwitchSettings.Clone()`, the `LightSwitchProperties` object
now directly references the existing property instances instead of
creating new ones, and also includes the `ToggleThemeHotkey` property.

**View model property handling:**

* In `LightSwitchViewModel.ToggleThemeActivationShortcut`, the getter
and setter now directly access and update
`ModuleSettings.Properties.ToggleThemeHotkey.Value`, removing the need
for a backing field and ensuring changes are properly serialized and
notified.
<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] Closes: #42330
- [ ] **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

---------

Signed-off-by: Shawn Yuan (from Dev Box) <shuaiyuan@microsoft.com>
This commit is contained in:
Shawn Yuan
2025-10-14 22:45:10 +08:00
committed by GitHub
parent 471022e842
commit 7b9d5af8c1
3 changed files with 44 additions and 12 deletions

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
@@ -20,8 +21,10 @@ using Settings.UI.Library.Helpers;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class LightSwitchViewModel : Observable
public partial class LightSwitchViewModel : PageViewModelBase
{
protected override string ModuleName => LightSwitchSettings.ModuleName;
private Func<string, int> SendConfigMSG { get; }
public ObservableCollection<SearchLocation> SearchLocations { get; } = new();
@@ -35,14 +38,24 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
ForceDarkCommand = new RelayCommand(ForceDarkNow);
AvailableScheduleModes = new ObservableCollection<string>
{
"FixedHours",
"SunsetToSunrise",
};
{
"FixedHours",
"SunsetToSunrise",
};
_toggleThemeHotkey = _moduleSettings.Properties.ToggleThemeHotkey.Value;
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [ToggleThemeActivationShortcut],
};
return hotkeysDict;
}
private void ForceLightNow()
{
Logger.LogInfo("Sending custom action: forceLight");
@@ -395,22 +408,21 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
public HotkeySettings ToggleThemeActivationShortcut
{
get => _toggleThemeHotkey;
get => ModuleSettings.Properties.ToggleThemeHotkey.Value;
set
{
if (value != _toggleThemeHotkey)
if (value != ModuleSettings.Properties.ToggleThemeHotkey.Value)
{
if (value == null)
{
_toggleThemeHotkey = LightSwitchProperties.DefaultToggleThemeHotkey;
ModuleSettings.Properties.ToggleThemeHotkey.Value = LightSwitchProperties.DefaultToggleThemeHotkey;
}
else
{
_toggleThemeHotkey = value;
ModuleSettings.Properties.ToggleThemeHotkey.Value = value;
}
_moduleSettings.Properties.ToggleThemeHotkey.Value = _toggleThemeHotkey;
NotifyPropertyChanged();
SendConfigMSG(
@@ -418,7 +430,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
LightSwitchSettings.ModuleName,
JsonSerializer.Serialize(_moduleSettings, (System.Text.Json.Serialization.Metadata.JsonTypeInfo<LightSwitchSettings>)SourceGenerationContextContext.Default.LightSwitchSettings)));
JsonSerializer.Serialize(_moduleSettings, SourceGenerationContextContext.Default.LightSwitchSettings)));
}
}
}