From ee91e4dcc2ac2664c2d94d127cb26828738d0385 Mon Sep 17 00:00:00 2001 From: Heiko <61519853+htcfreek@users.noreply.github.com> Date: Mon, 13 May 2024 17:41:07 +0200 Subject: [PATCH] [Settings][PTRun]Fix crash on empty additional options number box (#32832) * add NaN check to PluginOption ViewModel * fix spelling * fix spelling * Update PluginAdditionalOptionViewModel.cs --- .../ViewModels/PluginAdditionalOptionViewModel.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/settings-ui/Settings.UI/ViewModels/PluginAdditionalOptionViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/PluginAdditionalOptionViewModel.cs index d1c22ad0b5..02b83228c7 100644 --- a/src/settings-ui/Settings.UI/ViewModels/PluginAdditionalOptionViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/PluginAdditionalOptionViewModel.cs @@ -105,7 +105,15 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels get => _additionalOption.NumberValue; set { - if (value != _additionalOption.NumberValue) + if (double.IsNaN(value)) + { + // If the user clears the NumberBox and presses enter or moves focus away then `value` converted to double results in `double.NaN`. This crashes the settings app. (https://github.com/microsoft/PowerToys/issues/32738#issuecomment-2105983967) + // To prevent the crash and provide a nice user experience we reset the NumberBox to the last valid value. This happens by sending a `NotifyPropertyChanged()` command and let the NumberBox reload its value. + // (Yes we could use 0, but this needs additional code for checking 0 against min and max. + // And yes we could also use the min value of the NumberBox, but this is not user friendly as the minimum value of NumberBox can be `double.MinValue`.) + NotifyPropertyChanged(); + } + else if (value != _additionalOption.NumberValue) { _additionalOption.NumberValue = value; NotifyPropertyChanged();