/ src / settings-ui / Settings.UI / SettingsXAML / Controls / ImageResizerDimensionsNumberBox.cs
ImageResizerDimensionsNumberBox.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using Microsoft.UI.Xaml;
 6  using Microsoft.UI.Xaml.Controls;
 7  
 8  namespace Microsoft.PowerToys.Settings.UI.Controls;
 9  
10  public partial class ImageResizerDimensionsNumberBox : NumberBox
11  {
12      public ImageResizerDimensionsNumberBox()
13      {
14          this.Loaded += (_, _) => UpdateDisplayText();
15  
16          this.ValueChanged += (_, _) => UpdateDisplayText();
17  
18          this.GotFocus += (s, e) =>
19          {
20              // Show "0" in the UI when focused on the empty value. This ensures that the spinbutton
21              // controls are usable.
22              if (Value is double.NaN)
23              {
24                  Value = 0.0;
25              }
26          };
27  
28          this.LostFocus += (_, _) => UpdateDisplayText();
29      }
30  
31      private void UpdateDisplayText()
32      {
33          if (FocusState == FocusState.Unfocused && Value == 0)
34          {
35              Text = string.Empty;
36          }
37      }
38  }