Free, open-source Windows optimization tool for performance, privacy, and simplicity.
C# csharpdebloatdotnetgamingopen-sourceoptimizationoptimizeroptimizerduckperformanceprivacytweakertweakswindowswinui
refactor(customize): use a better design inspired by Winhance
fatDuck557fa016fab7
diff --git a/optimizerDuck.Test/Domain/Customize/BaseCustomizeSettingTests.cs b/optimizerDuck.Test/Domain/Customize/BaseCustomizeSettingTests.csindex 8c8b06f..8fdf505 100644--- a/optimizerDuck.Test/Domain/Customize/BaseCustomizeSettingTests.cs+++ b/optimizerDuck.Test/Domain/Customize/BaseCustomizeSettingTests.cs@@ -23,8 +23,8 @@ public class BaseCustomizeSettingTests : IDisposable{Path = TestKeyPath,Name = "TestValue",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],ValueKind = RegistryValueKind.DWord,},];@@ -487,4 +487,319 @@ public class BaseCustomizeSettingTests : IDisposable}#endregion++ #region RegistryToggle OnValues/OffValues tests++ [Fact]+ public void RegistryToggle_GetState_ReturnsTrueWhenValueMatchesOnValues()+ {+ RegistryService.Write(new RegistryItem(TestKeyPath, "OnValTest", 1));++ var toggle = new RegistryToggle+ {+ Path = TestKeyPath,+ Name = "OnValTest",+ OnValues = [1],+ OffValues = [0],+ };++ Assert.True(toggle.GetState());++ CleanupTestKeys();+ }++ [Fact]+ public void RegistryToggle_GetState_ReturnsFalseWhenValueMatchesOffValues()+ {+ RegistryService.Write(new RegistryItem(TestKeyPath, "OffValTest", 0));++ var toggle = new RegistryToggle+ {+ Path = TestKeyPath,+ Name = "OffValTest",+ OnValues = [1],+ OffValues = [0],+ };++ Assert.False(toggle.GetState());++ CleanupTestKeys();+ }++ [Fact]+ public void RegistryToggle_GetState_ReturnsTrueWhenKeyAbsentAndNullInOnValues()+ {+ // Key doesn't exist → value is null+ var toggle = new RegistryToggle+ {+ Path = TestKeyPath,+ Name = "MissingKeyTest",+ OnValues = [1, null],+ OffValues = [0],+ };++ Assert.True(toggle.GetState());+ }++ [Fact]+ public void RegistryToggle_GetState_ReturnsFalseWhenKeyAbsentAndNullInOffValues()+ {+ var toggle = new RegistryToggle+ {+ Path = TestKeyPath,+ Name = "MissingKeyTest2",+ OnValues = [1],+ OffValues = [0, null],+ };++ Assert.False(toggle.GetState());+ }++ [Fact]+ public void RegistryToggle_SetState_WritesFirstValueFromList()+ {+ var toggle = new RegistryToggle+ {+ Path = TestKeyPath,+ Name = "SetStateTest",+ OnValues = [1],+ OffValues = [0],+ };++ toggle.SetState(true);+ var value = RegistryService.Read<int>(new RegistryItem(TestKeyPath, "SetStateTest"));+ Assert.Equal(1, value);++ toggle.SetState(false);+ value = RegistryService.Read<int>(new RegistryItem(TestKeyPath, "SetStateTest"));+ Assert.Equal(0, value);++ CleanupTestKeys();+ }++ [Fact]+ public void RegistryToggle_SetState_DeletesKeyWhenFirstValueIsNull()+ {+ // First write a value+ RegistryService.Write(new RegistryItem(TestKeyPath, "NullSetTest", 1));++ var toggle = new RegistryToggle+ {+ Path = TestKeyPath,+ Name = "NullSetTest",+ OnValues = [null],+ OffValues = [0],+ };++ toggle.SetState(true); // Should delete the key++ var value = RegistryService.Read<object>(new RegistryItem(TestKeyPath, "NullSetTest"));+ Assert.Null(value);++ CleanupTestKeys();+ }++ [Fact]+ public void RegistryToggle_GetState_MatchesMultipleOnValues()+ {+ // Value 1 → ON+ RegistryService.Write(new RegistryItem(TestKeyPath, "MultiOnTest", 1));+ var toggle = new RegistryToggle+ {+ Path = TestKeyPath,+ Name = "MultiOnTest",+ OnValues = [1, 2],+ OffValues = [0],+ };+ Assert.True(toggle.GetState());++ // Value 2 → also ON+ RegistryService.Write(new RegistryItem(TestKeyPath, "MultiOnTest", 2));+ Assert.True(toggle.GetState());++ CleanupTestKeys();+ }++ #endregion++ #region SettingOption Bindings tests++ [Fact]+ public void SettingOption_PrimaryBinding_ReturnsFirstBinding()+ {+ var binding1 = new RegistryBinding(TestKeyPath, "Val1", 1);+ var binding2 = new RegistryBinding(TestKeyPath, "Val2", 2);+ var option = new SettingOption("Test", "value", [binding1, binding2]);++ Assert.NotNull(option.PrimaryBinding);+ Assert.Equal("Val1", option.PrimaryBinding.Name);+ }++ [Fact]+ public void SettingOption_PrimaryBinding_ReturnsNullWhenNoBindings()+ {+ var option = new SettingOption("Test", "value");++ Assert.Null(option.PrimaryBinding);+ }++ [Fact]+ public void SettingOption_Bindings_DefaultsToNull()+ {+ var option = new SettingOption("Test", "value");++ Assert.Null(option.Bindings);+ }++ #endregion++ #region Dropdown auto-read/write tests++ private class TestDropdownSetting : BaseCustomizeSetting+ {+ private const string RegPath =+ @"HKCU\Software\TestOptimizerDuckCustomize";+ private const string RegName = "DropdownTest";++ public override CustomizeControlType ControlType => CustomizeControlType.Dropdown;++ protected override IReadOnlyList<SettingOption>? GetOptions() =>+ [+ Option("OptionA", RegPath, RegName, 1),+ Option("OptionB", RegPath, RegName, 2),+ ];++ protected override CustomizeRefreshScope RefreshScope =>+ CustomizeRefreshScope.Settings;+ }++ [Fact]+ public async Task Dropdown_AutoRead_ReturnsMatchingOption()+ {+ var setting = new TestDropdownSetting { OwnerType = typeof(TestDropdownSetting) };++ RegistryService.Write(new RegistryItem(TestKeyPath, "DropdownTest", 1));+ Assert.Equal(1, setting.CurrentValue);++ RegistryService.Write(new RegistryItem(TestKeyPath, "DropdownTest", 2));+ Assert.Equal(2, setting.CurrentValue);++ CleanupTestKeys();+ }++ [Fact]+ public async Task Dropdown_AutoRead_ReturnsRawValueWhenNoMatch()+ {+ var setting = new TestDropdownSetting { OwnerType = typeof(TestDropdownSetting) };++ RegistryService.Write(new RegistryItem(TestKeyPath, "DropdownTest", 99));+ Assert.Equal(99, setting.CurrentValue);++ CleanupTestKeys();+ }++ [Fact]+ public async Task Dropdown_AutoWrite_WritesCorrectValue()+ {+ var setting = new TestDropdownSetting { OwnerType = typeof(TestDropdownSetting) };++ await setting.ApplyAsync(1);+ var value = RegistryService.Read<int>(new RegistryItem(TestKeyPath, "DropdownTest"));+ Assert.Equal(1, value);++ await setting.ApplyAsync(2);+ value = RegistryService.Read<int>(new RegistryItem(TestKeyPath, "DropdownTest"));+ Assert.Equal(2, value);++ CleanupTestKeys();+ }++ private class TestMultiBindingDropdown : BaseCustomizeSetting+ {+ private const string RegPathA = @"HKCU\Software\TestOptimizerDuckCustomize";+ private const string RegPathB = @"HKCU\Software\TestOptimizerDuckMultiKey";++ public override CustomizeControlType ControlType => CustomizeControlType.Dropdown;++ protected override IReadOnlyList<SettingOption>? GetOptions() =>+ [+ new SettingOption("Off", 0, [new RegistryBinding(RegPathA, "Key1", 0), new RegistryBinding(RegPathB, "Key2", 0)]),+ new SettingOption("On", 1, [new RegistryBinding(RegPathA, "Key1", 1), new RegistryBinding(RegPathB, "Key2", 1)]),+ ];++ protected override CustomizeRefreshScope RefreshScope => CustomizeRefreshScope.Settings;+ }++ [Fact]+ public async Task Dropdown_MultiBinding_WritesAllKeys()+ {+ var setting = new TestMultiBindingDropdown { OwnerType = typeof(TestMultiBindingDropdown) };++ await setting.ApplyAsync(1);++ var valA = RegistryService.Read<int>(new RegistryItem(TestKeyPath, "Key1"));+ var valB = RegistryService.Read<int>(new RegistryItem(@"HKCU\Software\TestOptimizerDuckMultiKey", "Key2"));+ Assert.Equal(1, valA);+ Assert.Equal(1, valB);++ CleanupTestKeys();+ try+ {+ using var hkcu = Microsoft.Win32.Registry.CurrentUser;+ hkcu.DeleteSubKeyTree(@"Software\TestOptimizerDuckMultiKey", false);+ }+ catch { }+ }++ [Fact]+ public async Task Dropdown_MultiBinding_ReadsAllKeys()+ {+ var setting = new TestMultiBindingDropdown { OwnerType = typeof(TestMultiBindingDropdown) };++ // Both keys = 1 → option "On" matches+ RegistryService.Write(new RegistryItem(TestKeyPath, "Key1", 1));+ RegistryService.Write(new RegistryItem(@"HKCU\Software\TestOptimizerDuckMultiKey", "Key2", 1));+ Assert.Equal(1, setting.CurrentValue);++ // Key A = 1, Key B = 0 → no match → returns primary binding raw value+ RegistryService.Write(new RegistryItem(TestKeyPath, "Key1", 1));+ RegistryService.Write(new RegistryItem(@"HKCU\Software\TestOptimizerDuckMultiKey", "Key2", 0));+ Assert.Equal(1, setting.CurrentValue);++ CleanupTestKeys();+ try+ {+ using var hkcu = Microsoft.Win32.Registry.CurrentUser;+ hkcu.DeleteSubKeyTree(@"Software\TestOptimizerDuckMultiKey", false);+ }+ catch { }+ }++ [Fact]+ public void Dropdown_WatchedPaths_IncludesAllBindingPaths()+ {+ var setting = new TestMultiBindingDropdown { OwnerType = typeof(TestMultiBindingDropdown) };++ var paths = ((ICustomizeSetting)setting).WatchedRegistryPaths;++ Assert.Equal(2, paths.Count);+ Assert.Contains(TestKeyPath, paths);+ Assert.Contains(@"HKCU\Software\TestOptimizerDuckMultiKey", paths);+ }++ private class OverrideWinsSetting : BaseCustomizeSetting+ {+ public override object? CurrentValue => "custom";+ public override Task ApplyAsync(object? value) => Task.CompletedTask;+ public override Task<bool> GetStateAsync() => Task.FromResult(true);+ }++ [Fact]+ public void OverrideWins_CurrentValue_UsesOverride()+ {+ var setting = new OverrideWinsSetting { OwnerType = typeof(OverrideWinsSetting) };+ Assert.Equal("custom", setting.CurrentValue);+ }++ #endregion}diff --git a/optimizerDuck/Domain/Customize/Categories/Desktop.cs b/optimizerDuck/Domain/Customize/Categories/Desktop.csindex ee06187..f4340a5 100644--- a/optimizerDuck/Domain/Customize/Categories/Desktop.cs+++ b/optimizerDuck/Domain/Customize/Categories/Desktop.cs@@ -39,8 +39,8 @@ public class Desktop : ICustomizeCategoryPath =@"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel",Name = "{20D04FE0-3AEA-1069-A2D8-08002B30309D}",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 0,},];@@ -58,8 +58,8 @@ public class Desktop : ICustomizeCategoryPath =@"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel",Name = "{645FF040-5081-101B-9F08-00AA002F954E}",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 0,},];@@ -77,8 +77,8 @@ public class Desktop : ICustomizeCategoryPath =@"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel",Name = "{59031a47-3f72-44a7-89c5-5595fe6b30ee}",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 1,},];@@ -96,8 +96,8 @@ public class Desktop : ICustomizeCategoryPath =@"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel",Name = "{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 1,},];@@ -115,8 +115,8 @@ public class Desktop : ICustomizeCategoryPath =@"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel",Name = "{5399E694-6CE5-4D6C-8FCE-1D8870FDCBA0}",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 1,},];@@ -134,8 +134,8 @@ public class Desktop : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "HideIcons",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 0,},];diff --git a/optimizerDuck/Domain/Customize/Categories/Gaming.cs b/optimizerDuck/Domain/Customize/Categories/Gaming.csindex 536ae76..72954d7 100644--- a/optimizerDuck/Domain/Customize/Categories/Gaming.cs+++ b/optimizerDuck/Domain/Customize/Categories/Gaming.cs@@ -42,16 +42,16 @@ public class Gaming : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\GameBar",Name = "AllowAutoGameMode",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},new(){Path = @"HKCU\Software\Microsoft\GameBar",Name = "AutoGameModeEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},];@@ -72,16 +72,16 @@ public class Gaming : ICustomizeCategory{Path = @"HKCU\System\GameConfigStore",Name = "GameBarEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},new(){Path = @"HKCU\Software\Microsoft\GameBar",Name = "ShowStartupPanel",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,IsOptional = true,},@@ -89,8 +89,8 @@ public class Gaming : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\GameBar",Name = "UseNexusForGameBarEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,IsOptional = true,},@@ -98,8 +98,8 @@ public class Gaming : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\GameBar",Name = "GamePanelStartupTipIndex",- OnValue = 3,- OffValue = 0,+ OnValues = [3],+ OffValues = [0],DefaultValue = 3,IsOptional = true,},@@ -122,16 +122,16 @@ public class Gaming : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\GameDVR",Name = "AppCaptureEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},new(){Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\GameDVR",Name = "HistoricalCaptureEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,IsOptional = true,},@@ -139,16 +139,16 @@ public class Gaming : ICustomizeCategory{Path = @"HKCU\System\GameConfigStore",Name = "GameDVR_Enabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},new(){Path = @"HKCU\System\GameConfigStore",Name = "GameDVR_HistoricalCaptureEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,IsOptional = true,},@@ -156,8 +156,8 @@ public class Gaming : ICustomizeCategory{Path = @"HKLM\SOFTWARE\Policies\Microsoft\Windows\GameDVR",Name = "AllowGameDVR",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},];@@ -227,32 +227,32 @@ public class Gaming : ICustomizeCategory{Path = @"HKCU\System\GameConfigStore",Name = "GameDVR_DXGIHonorFSEWindowsCompatible",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 0,},new(){Path = @"HKCU\System\GameConfigStore",Name = "GameDVR_FSEBehavior",- OnValue = 0,- OffValue = 2,+ OnValues = [0],+ OffValues = [2],DefaultValue = 0,},new(){Path = @"HKCU\System\GameConfigStore",Name = "GameDVR_FSEBehaviorMode",- OnValue = 0,- OffValue = 2,+ OnValues = [0],+ OffValues = [2],DefaultValue = 0,},new(){Path = @"HKCU\System\GameConfigStore",Name = "GameDVR_HonorUserFSEBehaviorMode",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 0,},];@@ -271,8 +271,8 @@ public class Gaming : ICustomizeCategory{Path = @"HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers",Name = "HwSchMode",- OnValue = 2,- OffValue = 1,+ OnValues = [2],+ OffValues = [1],DefaultValue = 1,},];diff --git a/optimizerDuck/Domain/Customize/Categories/Preferences.cs b/optimizerDuck/Domain/Customize/Categories/Preferences.csindex bc0ed28..7a6db62 100644--- a/optimizerDuck/Domain/Customize/Categories/Preferences.cs+++ b/optimizerDuck/Domain/Customize/Categories/Preferences.cs@@ -41,22 +41,11 @@ public class Preferences : ICustomizeCategorypublic override CustomizeControlType ControlType => CustomizeControlType.Dropdown;- public override IReadOnlyList<SettingOption>? Options =>- [Option("Center", 1), Option("Left", 0)];-- public override object? CurrentValue =>- RegistryService.Read<object>(new RegistryItem(RegPath, RegName));-- public override async Task ApplyAsync(object? value)- {- RegistryService.Write(new RegistryItem(RegPath, RegName, value ?? 1));- await ExecutePostActionAsync();- }+ protected override IReadOnlyList<SettingOption>? GetOptions() =>+ [Option("Center", RegPath, RegName, 1), Option("Left", RegPath, RegName, 0)];protected override CustomizeRefreshScope RefreshScope =>CustomizeRefreshScope.TaskbarSettings;-- protected override IReadOnlyList<string> GetWatchedRegistryPaths() => [RegPath];}[CustomizeSetting(@@ -75,16 +64,16 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "TaskbarDa",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},new(){Path = @"HKLM\SOFTWARE\Policies\Microsoft\Windows\Windows Feeds",Name = "EnableFeeds",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,IsOptional = true,},@@ -92,8 +81,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKLM\SOFTWARE\Policies\Microsoft\Dsh",Name = "AllowNewsAndInterests",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,IsOptional = true,},@@ -112,8 +101,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "ShowTaskViewButton",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},];@@ -136,8 +125,8 @@ public class Preferences : ICustomizeCategoryPath =@"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\TaskbarDeveloperSettings",Name = "TaskbarEndTask",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];@@ -155,16 +144,16 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",Name = "AppsUseLightTheme",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 1,},new(){Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",Name = "SystemUsesLightTheme",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 1,},];@@ -185,8 +174,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "ShowSyncProviderNotifications",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},];@@ -207,8 +196,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager",Name = "SystemPaneSuggestionsEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},];@@ -225,16 +214,16 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\PushNotifications",Name = "ToastEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},new(){Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\PushNotifications",Name = "LockScreenToastEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,IsOptional = true,},@@ -252,8 +241,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "UseCompactMode",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];@@ -270,8 +259,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "EnableSnapAssistFlyout",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 1,},];@@ -288,8 +277,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "AutoCheckSelect",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];@@ -310,8 +299,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "HideFileExt",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 1,},];@@ -332,8 +321,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "Hidden",- OnValue = 1,- OffValue = 2,+ OnValues = [1],+ OffValues = [2],DefaultValue = 2,},];@@ -354,8 +343,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Clipboard",Name = "EnableClipboardHistory",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];@@ -372,8 +361,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "DisallowShaking",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 0,},];@@ -391,8 +380,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "ShowSecondsInSystemClock",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];@@ -409,8 +398,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",Name = "LaunchTo",- OnValue = 1,- OffValue = 2,+ OnValues = [1],+ OffValues = [2],DefaultValue = 2,},];@@ -432,8 +421,8 @@ public class Preferences : ICustomizeCategory{Path = @"HKCU\Software\Policies\Microsoft\Windows\Explorer",Name = "DisableSearchBoxSuggestions",- OnValue = 0,- OffValue = 1,+ OnValues = [0],+ OffValues = [1],DefaultValue = 0,},];@@ -484,35 +473,26 @@ public class Preferences : ICustomizeCategorypublic override CustomizeControlType ControlType => CustomizeControlType.Dropdown;- public override IReadOnlyList<SettingOption>? Options- {- get- {- if (Shared.IsWindows11OrGreater)- return- [- Option("Hidden", 0),- Option("Icon", 1),- Option("IconAndLabel", 2),- Option("SearchBox", 3),- ];- return [Option("Hidden", 0), Option("Icon", 1), Option("SearchBox", 2)];- }- }-- public override object? CurrentValue =>- RegistryService.Read<object>(new RegistryItem(RegPath, RegName));-- public override async Task ApplyAsync(object? value)+ protected override IReadOnlyList<SettingOption>? GetOptions(){- RegistryService.Write(new RegistryItem(RegPath, RegName, value ?? 0));- await ExecutePostActionAsync();+ if (Shared.IsWindows11OrGreater)+ return+ [+ Option("Hidden", RegPath, RegName, 0),+ Option("Icon", RegPath, RegName, 1),+ Option("IconAndLabel", RegPath, RegName, 2),+ Option("SearchBox", RegPath, RegName, 3),+ ];+ return+ [+ Option("Hidden", RegPath, RegName, 0),+ Option("Icon", RegPath, RegName, 1),+ Option("SearchBox", RegPath, RegName, 2),+ ];}protected override CustomizeRefreshScope RefreshScope =>CustomizeRefreshScope.TaskbarSettings;-- protected override IReadOnlyList<string> GetWatchedRegistryPaths() => [RegPath];}[CustomizeSetting(Section = nameof(Sections.Taskbar), Icon = SymbolRegular.CursorClick24)]@@ -531,8 +511,8 @@ public class Preferences : ICustomizeCategory{Path = RegPath,Name = RegName,- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];diff --git a/optimizerDuck/Domain/Customize/Categories/SystemFeatures.cs b/optimizerDuck/Domain/Customize/Categories/SystemFeatures.csindex 6ef1f17..49db23a 100644--- a/optimizerDuck/Domain/Customize/Categories/SystemFeatures.cs+++ b/optimizerDuck/Domain/Customize/Categories/SystemFeatures.cs@@ -38,16 +38,16 @@ public class SystemFeatures : ICustomizeCategory{Path = @"HKU\.DEFAULT\Control Panel\Keyboard",Name = "InitialKeyboardIndicators",- OnValue = 2,- OffValue = 0,+ OnValues = [2],+ OffValues = [0],DefaultValue = 0,},new(){Path = @"HKCU\Control Panel\Keyboard",Name = "InitialKeyboardIndicators",- OnValue = 2,- OffValue = 0,+ OnValues = [2],+ OffValues = [0],DefaultValue = 0,},];@@ -70,8 +70,8 @@ public class SystemFeatures : ICustomizeCategory{Path = @"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock",Name = "AllowDevelopmentWithoutDevLicense",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];@@ -92,8 +92,8 @@ public class SystemFeatures : ICustomizeCategory{Path = @"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock",Name = "AllowAllTrustedApps",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];@@ -116,8 +116,8 @@ public class SystemFeatures : ICustomizeCategory{Path = Path,Name = "LongPathsEnabled",- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];@@ -140,8 +140,8 @@ public class SystemFeatures : ICustomizeCategory{Path = RegPath,Name = RegName,- OnValue = 1,- OffValue = 0,+ OnValues = [1],+ OffValues = [0],DefaultValue = 0,},];diff --git a/optimizerDuck/Domain/Customize/Models/BaseCustomizeSetting.cs b/optimizerDuck/Domain/Customize/Models/BaseCustomizeSetting.csindex d9a6e6f..68b238c 100644--- a/optimizerDuck/Domain/Customize/Models/BaseCustomizeSetting.cs+++ b/optimizerDuck/Domain/Customize/Models/BaseCustomizeSetting.cs@@ -2,7 +2,9 @@ using System.Reflection;using optimizerDuck.Common.Helpers;using optimizerDuck.Domain.Abstractions;using optimizerDuck.Domain.Attributes;+using optimizerDuck.Domain.Optimizations.Models.Services;using optimizerDuck.Services.Configuration;+using optimizerDuck.Services.Optimization.Providers;using Wpf.Ui.Controls;namespace optimizerDuck.Domain.Customize.Models;@@ -12,6 +14,7 @@ namespace optimizerDuck.Domain.Customize.Models;/// Subclasses declare <see cref="RegistryToggles"/> to define which registry values/// are controlled, and may override <see cref="RefreshScope"/> to specify which/// Windows surfaces should be refreshed after applying.+/// For Dropdown settings, options can carry <see cref="RegistryBinding"/> to auto-read/write./// </summary>public abstract class BaseCustomizeSetting : ICustomizeSetting{@@ -48,8 +51,37 @@ public abstract class BaseCustomizeSetting : ICustomizeSetting}public virtual CustomizeControlType ControlType => CustomizeControlType.Toggle;- public virtual object? CurrentValue => null;- public virtual IReadOnlyList<SettingOption>? Options => null;++ public virtual object? CurrentValue+ {+ get+ {+ var options = GetOptions();+ if (ControlType != CustomizeControlType.Dropdown || options == null)+ return null;++ foreach (var option in options)+ {+ if (option.Bindings is not { Count: > 0 })+ continue;++ var allMatch = option.Bindings.All(b =>+ {+ var actual = RegistryService.Read<object>(new RegistryItem(b.Path, b.Name));+ return ValuesEqual(actual, b.Value);+ });++ if (allMatch)+ return option.Value;+ }++ return ReadPrimaryRawValue(options);+ }+ }++ public virtual IReadOnlyList<SettingOption>? Options => GetOptions();++ protected virtual IReadOnlyList<SettingOption>? GetOptions() => null;public virtual Task<bool> GetStateAsync(){@@ -97,6 +129,21 @@ public abstract class BaseCustomizeSetting : ICustomizeSettingtoggle.SetState(isOn);});}+ else if (ControlType == CustomizeControlType.Dropdown && Options != null)+ {+ // Find matching option and apply all its bindings+ var option = Options.FirstOrDefault(o => Equals(o.Value, value));+ if (option?.Bindings is { Count: > 0 })+ {+ foreach (var binding in option.Bindings)+ {+ if (binding.Value == null)+ RegistryService.DeleteValue(new RegistryItem(binding.Path, binding.Name));+ else+ RegistryService.Write(binding.ToRegistryItem());+ }+ }+ }if (NeedsPostAction)await ExecutePostActionAsync();@@ -106,8 +153,25 @@ public abstract class BaseCustomizeSetting : ICustomizeSettingIReadOnlyList<string> ICustomizeSetting.WatchedRegistryPaths => GetWatchedRegistryPaths();- protected virtual IReadOnlyList<string> GetWatchedRegistryPaths() =>- [.. RegistryToggles.Select(t => t.Path).Distinct(StringComparer.OrdinalIgnoreCase)];+ protected virtual IReadOnlyList<string> GetWatchedRegistryPaths()+ {+ // From RegistryToggles+ var fromToggles = RegistryToggles.Select(t => t.Path);++ // From ALL Dropdown option bindings (not just primary)+ var fromOptions =+ ControlType == CustomizeControlType.Dropdown && Options != null+ ? Options.Where(o => o.Bindings != null)+ .SelectMany(o => o.Bindings!)+ .Select(b => b.Path)+ : [];++ return [+ .. fromToggles+ .Concat(fromOptions)+ .Distinct(StringComparer.OrdinalIgnoreCase),+ ];+ }/// <summary>/// Whether the setting requires a Windows refresh after <see cref="ApplyAsync"/>.@@ -155,8 +219,28 @@ public abstract class BaseCustomizeSetting : ICustomizeSetting});}- protected SettingOption Option(string optionKey, object value) =>- new(Loc.Instance[$"Customize.{OwnerKey}.{FeatureKey}.Options.{optionKey}"], value);+ protected SettingOption Option(+ string optionKey,+ string regPath,+ string regName,+ object value+ ) =>+ new(+ Loc.Instance[$"Customize.{OwnerKey}.{FeatureKey}.Options.{optionKey}"],+ value,+ [new RegistryBinding(regPath, regName, value)]+ );++ protected SettingOption Option(+ string optionKey,+ object value,+ params RegistryBinding[] bindings+ ) =>+ new(+ Loc.Instance[$"Customize.{OwnerKey}.{FeatureKey}.Options.{optionKey}"],+ value,+ bindings+ );protected string RecommendationPrefix => $"Customize.{OwnerKey}.{FeatureKey}.Recommendation";@@ -168,4 +252,81 @@ public abstract class BaseCustomizeSetting : ICustomizeSettingreturn new CustomizeRecommendationResult(state, $"{RecommendationPrefix}.Reason");}++ private static bool ValuesEqual(object? a, object? b)+ {+ if (a == null && b == null)+ return true;++ if (a == null || b == null)+ return false;++ if (a is IConvertible && b is IConvertible)+ {+ try+ {+ if (a.GetType() == b.GetType())+ return a.Equals(b);++ var typeA = a.GetType();+ var typeB = b.GetType();++ if (+ (+ typeA == typeof(int)+ || typeA == typeof(long)+ || typeA == typeof(short)+ || typeA == typeof(byte)+ )+ && (+ typeB == typeof(int)+ || typeB == typeof(long)+ || typeB == typeof(short)+ || typeB == typeof(byte)+ )+ )+ {+ return Convert.ToInt64(a) == Convert.ToInt64(b);+ }++ if (+ (typeA == typeof(float) || typeA == typeof(double) || typeA == typeof(decimal))+ && (+ typeB == typeof(float)+ || typeB == typeof(double)+ || typeB == typeof(decimal)+ )+ )+ {+ return Convert.ToDouble(a) == Convert.ToDouble(b);+ }++ var da = Convert.ToDecimal(a);+ var db = Convert.ToDecimal(b);+ return da == db;+ }+ catch+ {+ // fall through to string comparison+ }+ }++ var strA = a.ToString();+ var strB = b.ToString();+ return strA != null && strB != null && strA.Equals(strB, StringComparison.Ordinal);+ }++ private static object? ReadPrimaryRawValue(IReadOnlyList<SettingOption> options)+ {+ // Read from the primary binding of the first option that has bindings+ foreach (var option in options)+ {+ if (option.PrimaryBinding is not { } binding)+ continue;++ return RegistryService.Read<object>(new RegistryItem(binding.Path, binding.Name));+ }++ return null;+ }}diff --git a/optimizerDuck/Domain/Customize/Models/RegistryBinding.cs b/optimizerDuck/Domain/Customize/Models/RegistryBinding.csnew file mode 100644index 0000000..4ebc2ad--- /dev/null+++ b/optimizerDuck/Domain/Customize/Models/RegistryBinding.cs@@ -0,0 +1,18 @@+using Microsoft.Win32;+using optimizerDuck.Domain.Optimizations.Models.Services;++namespace optimizerDuck.Domain.Customize.Models;++/// <summary>+/// Binds a <see cref="SettingOption" /> to one or more registry values.+/// Used by <see cref="BaseCustomizeSetting" /> to auto-read/write registry for Dropdown settings.+/// </summary>+public record RegistryBinding(+ string Path,+ string? Name,+ object? Value,+ RegistryValueKind ValueKind = RegistryValueKind.DWord+)+{+ public RegistryItem ToRegistryItem() => new(Path, Name, Value, ValueKind);+}diff --git a/optimizerDuck/Domain/Customize/Models/RegistryToggle.cs b/optimizerDuck/Domain/Customize/Models/RegistryToggle.csindex e23a264..d089654 100644--- a/optimizerDuck/Domain/Customize/Models/RegistryToggle.cs+++ b/optimizerDuck/Domain/Customize/Models/RegistryToggle.cs@@ -6,6 +6,8 @@ namespace optimizerDuck.Domain.Customize.Models;/// <summary>/// Represents a single registry key-value pair that can be toggled on or off.+/// <see cref="OnValues" /> and <see cref="OffValues" /> support multiple values+/// (e.g. <c>null</c> means "key absent")./// </summary>public class RegistryToggle{@@ -15,20 +17,25 @@ public class RegistryToggle/// <summary>Gets the name of the registry value.</summary>public required string Name { get; init; }- /// <summary>Gets the value to write when toggling on. Default is <c>1</c>.</summary>- public object? OnValue { get; init; } = 1;-- /// <summary>Gets the value to write when toggling off. Default is <c>0</c>.</summary>- public object? OffValue { get; init; } = 0;+ /// <summary>+ /// Gets the list of values that represent the "on" state.+ /// <c>null</c> in the list means "key absent = on".+ /// Default is <c>[1]</c>.+ /// </summary>+ public IReadOnlyList<object?> OnValues { get; init; } = [1];- /// <summary>Gets the default state value when the key is missing. Default is <c>0</c>.</summary>- public object? DefaultValue { get; init; } = 0;+ /// <summary>+ /// Gets the list of values that represent the "off" state.+ /// <c>null</c> in the list means "key absent = off".+ /// Default is <c>[0]</c>.+ /// </summary>+ public IReadOnlyList<object?> OffValues { get; init; } = [0];/// <summary>- /// Gets a value that indicates whether a missing registry value should be treated as <see cref="DefaultValue"/>.- /// Default is <see langword="false"/>.+ /// Gets the default state value when the key is missing. Used for Reset to Default.+ /// Default is <c>0</c>./// </summary>- public bool TreatMissingAsDefault { get; init; } = false;+ public object? DefaultValue { get; init; } = 0;/// <summary>/// Gets a value that indicates whether this toggle is optional (non-required for state detection).@@ -39,20 +46,41 @@ public class RegistryToggle/// <summary>Gets the registry value type. Default is <see cref="RegistryValueKind.DWord"/>.</summary>public RegistryValueKind ValueKind { get; init; } = RegistryValueKind.DWord;- /// <summary>Reads the registry and returns whether the toggle is currently on.</summary>- /// <returns><see langword="true"/> if the registry value matches <see cref="OnValue"/>; otherwise <see langword="false"/>.</returns>+ /// <summary>+ /// Reads the registry and returns whether the toggle is currently on.+ /// Returns <see langword="true" /> if the registry value matches any value in <see cref="OnValues" />.+ /// </summary>public bool GetState(){var value = GetRawValue();+ return IsValueInList(value, OnValues);+ }++ public void SetState(bool isOn)+ {+ var targetValues = isOn ? OnValues : OffValues;++ // First value in the list is the primary value to write+ if (targetValues is { Count: > 0 } && targetValues[0] is { } firstValue)+ RegistryService.Write(new RegistryItem(Path, Name, firstValue, ValueKind));+ else+ RegistryService.DeleteValue(new RegistryItem(Path, Name));+ }- if (value == null)+ private object? GetRawValue()+ {+ return RegistryService.Read<object?>(new RegistryItem(Path, Name));+ }++ private static bool IsValueInList(object? value, IReadOnlyList<object?> values)+ {+ foreach (var target in values){- // If value is null and TreatMissingAsDefault is false,- // treat it as the default state (off)- return TreatMissingAsDefault ? AreEqual(DefaultValue, OnValue) : false;+ if (AreEqual(value, target))+ return true;}- return AreEqual(value, OnValue);+ return false;}private static bool AreEqual(object? a, object? b)@@ -126,23 +154,4 @@ public class RegistryTogglevar strB = b.ToString();return strA != null && strB != null && strA.Equals(strB, StringComparison.Ordinal);}-- private object? GetRawValue()- {- var value = RegistryService.Read<object?>(new RegistryItem(Path, Name));-- if (value == null && TreatMissingAsDefault)- return DefaultValue;-- return value;- }-- public void SetState(bool isOn)- {- var targetValue = isOn ? OnValue : OffValue;- if (targetValue == null)- RegistryService.DeleteValue(new RegistryItem(Path, Name));- else- RegistryService.Write(new RegistryItem(Path, Name, targetValue, ValueKind));- }}diff --git a/optimizerDuck/Domain/Customize/Models/SettingOption.cs b/optimizerDuck/Domain/Customize/Models/SettingOption.csindex d1ad9a0..9202da9 100644--- a/optimizerDuck/Domain/Customize/Models/SettingOption.cs+++ b/optimizerDuck/Domain/Customize/Models/SettingOption.cs@@ -1,3 +1,14 @@namespace optimizerDuck.Domain.Customize.Models;-public sealed record SettingOption(string DisplayName, object Value);+public sealed record SettingOption(+ string DisplayName,+ object Value,+ IReadOnlyList<RegistryBinding>? Bindings = null+)+{+ /// <summary>+ /// Primary binding — the first binding in the list, or null.+ /// Used by base class for single-key auto-read/write.+ /// </summary>+ public RegistryBinding? PrimaryBinding => Bindings is { Count: > 0 } ? Bindings[0] : null;+}