diff --git a/MainWindow.Behaviors.partial.cs b/MainWindow.Behaviors.partial.cs index bc7bedf..e03dbfe 100644 --- a/MainWindow.Behaviors.partial.cs +++ b/MainWindow.Behaviors.partial.cs @@ -605,6 +605,7 @@ namespace ThreadPilot try { this.LogDebug("Starting graceful shutdown..."); + this.selfResourceManagementService.RestoreForegroundMode(); // 1. Stop monitoring services try @@ -1425,9 +1426,12 @@ namespace ThreadPilot var currentSettings = settings ?? this.settingsService.Settings; var isHiddenState = state is AppActivityState.Minimized or AppActivityState.TrayHidden; - if (isHiddenState && currentSettings.EnableSelfLowImpactMode) + if (SelfResourcePolicy.ShouldApplyLowImpactMode(isHiddenState, currentSettings.EnableSelfLowImpactMode)) { - this.selfResourceManagementService.ApplyLowImpactMode(currentSettings.EnableSelfAffinityLimit); + this.selfResourceManagementService.ApplyLowImpactMode(SelfResourcePolicy.ShouldLimitAffinity( + isHiddenState, + currentSettings.EnableSelfLowImpactMode, + currentSettings.EnableSelfAffinityLimit)); return; } @@ -1844,6 +1848,7 @@ namespace ThreadPilot this.initializationTimeoutTimer?.Stop(); this.initializationTimeoutTimer?.Dispose(); + this.selfResourceManagementService.RestoreForegroundMode(); this.navigationBehavior.Dispose(); } catch (Exception ex) diff --git a/Models/ApplicationSettingsModel.cs b/Models/ApplicationSettingsModel.cs index 16972e6..b3f9cab 100644 --- a/Models/ApplicationSettingsModel.cs +++ b/Models/ApplicationSettingsModel.cs @@ -172,7 +172,7 @@ namespace ThreadPilot.Models private bool hasSeenElevationWarning = false; [ObservableProperty] - private bool enableSelfLowImpactMode = false; + private bool enableSelfLowImpactMode = true; [ObservableProperty] private bool enableSelfAffinityLimit = false; @@ -340,4 +340,3 @@ namespace ThreadPilot.Models Custom, } } - diff --git a/Services/SelfResourceManagementService.cs b/Services/SelfResourceManagementService.cs index 8b9cace..6350e5b 100644 --- a/Services/SelfResourceManagementService.cs +++ b/Services/SelfResourceManagementService.cs @@ -23,6 +23,19 @@ namespace ThreadPilot.Services public static class SelfResourcePolicy { + public static bool ShouldApplyLowImpactMode(bool isHidden, bool enableSelfLowImpactMode) + { + return isHidden && enableSelfLowImpactMode; + } + + public static bool ShouldLimitAffinity( + bool isHidden, + bool enableSelfLowImpactMode, + bool enableSelfAffinityLimit) + { + return ShouldApplyLowImpactMode(isHidden, enableSelfLowImpactMode) && enableSelfAffinityLimit; + } + public static bool TryCreateLowImpactAffinityMask(int logicalProcessorCount, out long affinityMask) { affinityMask = 0; diff --git a/Tests/ThreadPilot.Core.Tests/ApplicationSettingsServiceTests.cs b/Tests/ThreadPilot.Core.Tests/ApplicationSettingsServiceTests.cs index 94dcf81..2c3517c 100644 --- a/Tests/ThreadPilot.Core.Tests/ApplicationSettingsServiceTests.cs +++ b/Tests/ThreadPilot.Core.Tests/ApplicationSettingsServiceTests.cs @@ -22,6 +22,8 @@ namespace ThreadPilot.Core.Tests Assert.True(storage.Writes.ContainsKey(TestPaths.SettingsFilePath)); Assert.Equal(3000, service.Settings.NotificationDisplayDurationMs); Assert.Equal(5000, service.Settings.BalloonNotificationTimeoutMs); + Assert.True(service.Settings.EnableSelfLowImpactMode); + Assert.False(service.Settings.EnableSelfAffinityLimit); } [Fact] @@ -35,6 +37,44 @@ namespace ThreadPilot.Core.Tests Assert.Equal(3000, service.Settings.NotificationDisplayDurationMs); Assert.Equal(string.Empty, service.Settings.CustomTrayIconPath); + Assert.True(service.Settings.EnableSelfLowImpactMode); + Assert.False(service.Settings.EnableSelfAffinityLimit); + } + + [Fact] + public async Task LoadSettingsAsync_EnablesSafeSelfLowImpactDefault_ForOlderSettingsJson() + { + var storage = new FakeSettingsStorage(); + storage.Files[TestPaths.SettingsFilePath] = """ + { + "notificationDisplayDurationMs": 3000, + "balloonNotificationTimeoutMs": 5000 + } + """; + var service = CreateService(storage); + + await service.LoadSettingsAsync(); + + Assert.True(service.Settings.EnableSelfLowImpactMode); + Assert.False(service.Settings.EnableSelfAffinityLimit); + } + + [Fact] + public async Task LoadSettingsAsync_PreservesExplicitSelfLowImpactOptOut() + { + var storage = new FakeSettingsStorage(); + storage.Files[TestPaths.SettingsFilePath] = """ + { + "enableSelfLowImpactMode": false, + "enableSelfAffinityLimit": true + } + """; + var service = CreateService(storage); + + await service.LoadSettingsAsync(); + + Assert.False(service.Settings.EnableSelfLowImpactMode); + Assert.True(service.Settings.EnableSelfAffinityLimit); } [Fact] diff --git a/Tests/ThreadPilot.Core.Tests/SelfResourcePolicyTests.cs b/Tests/ThreadPilot.Core.Tests/SelfResourcePolicyTests.cs index 1a4767e..06191c3 100644 --- a/Tests/ThreadPilot.Core.Tests/SelfResourcePolicyTests.cs +++ b/Tests/ThreadPilot.Core.Tests/SelfResourcePolicyTests.cs @@ -33,5 +33,21 @@ namespace ThreadPilot.Core.Tests { Assert.False(SelfResourcePolicy.TryCreateLowImpactAffinityMask(logicalProcessorCount, out _)); } + + [Theory] + [InlineData(false, true, true, false)] + [InlineData(true, false, true, false)] + [InlineData(true, true, false, false)] + [InlineData(true, true, true, true)] + public void ShouldLimitAffinity_RequiresHiddenLowImpactModeAndAffinitySetting( + bool isHidden, + bool enableSelfLowImpactMode, + bool enableSelfAffinityLimit, + bool expected) + { + Assert.Equal( + expected, + SelfResourcePolicy.ShouldLimitAffinity(isHidden, enableSelfLowImpactMode, enableSelfAffinityLimit)); + } } }