From d72e0ab20d6ca3738b63d087e47525ada66df606 Mon Sep 17 00:00:00 2001 From: Shawn Yuan <128874481+shuaiyuanxx@users.noreply.github.com> Date: Fri, 8 Aug 2025 22:55:00 +0800 Subject: [PATCH] Fixed toggle switch not working issue. (#41049) ## Summary of the Pull Request Fixed toggle switch not working issue. ## AI Summary This pull request refactors how `DashboardListItem` objects are created and added to collections in the `DashboardViewModel`. The main improvement is to separate the instantiation of each `DashboardListItem` from the assignment of its `EnabledChangedCallback` property, which is now set after the object is added to the relevant collection. This change improves clarity and may help prevent issues related to object initialization order. Refactoring of `DashboardListItem` creation and initialization: * In the `AddDashboardListItem` method, the `DashboardListItem` object is now created and added to `AllModules` before its `EnabledChangedCallback` property is set, instead of setting this property during object initialization. * In the `GetShortcutModules` method, both `ShortcutModules` and `ActionModules` collections now receive `DashboardListItem` objects that are instantiated first, added to the collection, and then have their `EnabledChangedCallback` property set. This replaces the previous pattern of setting the callback during object creation. [[1]](diffhunk://#diff-aea3404667e7a3de2750bf9ab7ee8ff5e717892caa68ee1de86713cf8e21b44cL123-R136) [[2]](diffhunk://#diff-aea3404667e7a3de2750bf9ab7ee8ff5e717892caa68ee1de86713cf8e21b44cL144-R159) * ## PR Checklist - [x] Closes: #41046 - [ ] **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 It is an regression from https://github.com/microsoft/PowerToys/pull/40214 ## Validation Steps Performed --------- Signed-off-by: Shuai Yuan --- .../ViewModels/DashboardViewModel.cs | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs index 6e2bb2d432..8dd97c85fa 100644 --- a/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs @@ -69,16 +69,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private void AddDashboardListItem(ModuleType moduleType) { GpoRuleConfigured gpo = ModuleHelper.GetModuleGpoConfiguration(moduleType); - AllModules.Add(new DashboardListItem() + var newItem = new DashboardListItem() { Tag = moduleType, Label = resourceLoader.GetString(ModuleHelper.GetModuleLabelResourceName(moduleType)), IsEnabled = gpo == GpoRuleConfigured.Enabled || (gpo != GpoRuleConfigured.Disabled && ModuleHelper.GetIsModuleEnabled(generalSettingsConfig, moduleType)), IsLocked = gpo == GpoRuleConfigured.Enabled || gpo == GpoRuleConfigured.Disabled, Icon = ModuleHelper.GetModuleTypeFluentIconName(moduleType), - EnabledChangedCallback = EnabledChangedOnUI, DashboardModuleItems = GetModuleItems(moduleType), - }); + }; + + AllModules.Add(newItem); + newItem.EnabledChangedCallback = EnabledChangedOnUI; } private void EnabledChangedOnUI(DashboardListItem dashboardListItem) @@ -120,16 +122,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels if (filteredItems.Count != 0) { - ShortcutModules.Add(new DashboardListItem + var newItem = new DashboardListItem { - EnabledChangedCallback = x.EnabledChangedCallback, Icon = x.Icon, IsLocked = x.IsLocked, Label = x.Label, Tag = x.Tag, IsEnabled = x.IsEnabled, DashboardModuleItems = new ObservableCollection(filteredItems), - }); + }; + + ShortcutModules.Add(newItem); + newItem.EnabledChangedCallback = x.EnabledChangedCallback; } } @@ -141,16 +145,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels if (filteredItems.Count != 0) { - ActionModules.Add(new DashboardListItem + var newItem = new DashboardListItem { - EnabledChangedCallback = x.EnabledChangedCallback, Icon = x.Icon, IsLocked = x.IsLocked, Label = x.Label, Tag = x.Tag, IsEnabled = x.IsEnabled, DashboardModuleItems = new ObservableCollection(filteredItems), - }); + }; + + ActionModules.Add(newItem); + newItem.EnabledChangedCallback = x.EnabledChangedCallback; } } }