From 0dd6505456739bea7a0ca6024c655756c3d4ac2c Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Tue, 7 May 2024 02:05:11 +0300 Subject: [PATCH] Refactor last PR --- LightBulb/Services/SettingsService.cs | 2 +- .../Components/DashboardViewModel.cs | 98 +++++++++---------- 2 files changed, 49 insertions(+), 51 deletions(-) diff --git a/LightBulb/Services/SettingsService.cs b/LightBulb/Services/SettingsService.cs index fb197c6..f33cce7 100644 --- a/LightBulb/Services/SettingsService.cs +++ b/LightBulb/Services/SettingsService.cs @@ -64,7 +64,7 @@ public partial class SettingsService() : SettingsBase(GetFilePath()) private double _configurationTransitionOffset; [ObservableProperty] - private TimeSpan _configurationSmoothingMaxDuration = TimeSpan.FromSeconds(15); + private TimeSpan _configurationSmoothingMaxDuration = TimeSpan.FromSeconds(5); // Location diff --git a/LightBulb/ViewModels/Components/DashboardViewModel.cs b/LightBulb/ViewModels/Components/DashboardViewModel.cs index 0c023f1..22f9f9f 100644 --- a/LightBulb/ViewModels/Components/DashboardViewModel.cs +++ b/LightBulb/ViewModels/Components/DashboardViewModel.cs @@ -69,6 +69,9 @@ public partial class DashboardViewModel : ViewModelBase [ObservableProperty] private ColorConfiguration _currentConfiguration = ColorConfiguration.Default; + private ColorConfiguration? _configurationSmoothingSource; + private ColorConfiguration? _configurationSmoothingTarget; + public DashboardViewModel( SettingsService settingsService, GammaService gammaService, @@ -285,67 +288,62 @@ private void UpdateInstant() } } - private const double _brightnessDefaultStep = 0.08; - private const double _temperatureDefaultStep = 30; - - private ColorConfiguration _lastTarget; - private void UpdateConfiguration() { - if (CurrentConfiguration == TargetConfiguration) - { - _gammaService.SetGamma(CurrentConfiguration); - return; - } - - double stepsPerSecond = 1000 / _updateConfigurationTimer.Interval.TotalMilliseconds; - var isSmooth = - _settingsService.IsConfigurationSmoothingEnabled - && !IsCyclePreviewEnabled + !IsCyclePreviewEnabled + && CurrentConfiguration != TargetConfiguration + && _settingsService.IsConfigurationSmoothingEnabled && _settingsService.ConfigurationSmoothingMaxDuration.TotalSeconds >= 0.1; - // If we've changed targets, restart with default settings. - if (_lastTarget != TargetConfiguration && isSmooth) + if (isSmooth) { - _lastTarget = TargetConfiguration; - } - - double brightnessMaxStep = _brightnessDefaultStep; - double temperatureMaxStep = _temperatureDefaultStep; + // Check if the target configuration has changed since the last transition started + if ( + _configurationSmoothingTarget != TargetConfiguration + || _configurationSmoothingSource is null + ) + { + _configurationSmoothingSource = CurrentConfiguration; + _configurationSmoothingTarget = TargetConfiguration; + } + + var brightnessDelta = Math.Abs( + _configurationSmoothingTarget.Value.Brightness + - _configurationSmoothingSource.Value.Brightness + ); - // Calculate the step size... - var tempDelta = Math.Abs( - TargetConfiguration.Temperature - CurrentConfiguration.Temperature - ); - var brightnessDelta = Math.Abs( - TargetConfiguration.Brightness - CurrentConfiguration.Brightness - ); - var expectedTemperatureDuration = tempDelta / (temperatureMaxStep * stepsPerSecond); - var expectedBrightnessDuration = brightnessDelta / (brightnessMaxStep * stepsPerSecond); - - // If the expected durations are longer than our duration limit, we adjust the step amount to stay at the max duration. - var goalDuration = Math.Max(expectedTemperatureDuration, expectedBrightnessDuration); - goalDuration = Math.Min( - goalDuration, - _settingsService.ConfigurationSmoothingMaxDuration.TotalSeconds - ); + var brightnessStep = Math.Max( + brightnessDelta + / _settingsService.ConfigurationSmoothingMaxDuration.TotalSeconds + * _updateConfigurationTimer.Interval.TotalSeconds, + 0.08 + ); - // Calculate the step-rate needed to reach the goal. - brightnessMaxStep = brightnessDelta / (goalDuration * stepsPerSecond); - temperatureMaxStep = tempDelta / (goalDuration * stepsPerSecond); + var temperatureDelta = Math.Abs( + _configurationSmoothingTarget.Value.Temperature + - _configurationSmoothingSource.Value.Temperature + ); - // If we ended up slower on either of the durations, speed us up. - brightnessMaxStep = Math.Max(brightnessMaxStep, _brightnessDefaultStep); - temperatureMaxStep = Math.Max(temperatureMaxStep, _temperatureDefaultStep); + var temperatureStep = Math.Max( + temperatureDelta + / _settingsService.ConfigurationSmoothingMaxDuration.TotalSeconds + * _updateConfigurationTimer.Interval.TotalSeconds, + 30 + ); - CurrentConfiguration = isSmooth - ? CurrentConfiguration.StepTo( + CurrentConfiguration = CurrentConfiguration.StepTo( TargetConfiguration, - temperatureMaxStep, - brightnessMaxStep - ) - : TargetConfiguration; + temperatureStep, + brightnessStep + ); + } + else + { + CurrentConfiguration = TargetConfiguration; + _configurationSmoothingSource = null; + _configurationSmoothingTarget = null; + } _gammaService.SetGamma(CurrentConfiguration); }