/ src / settings-ui / Settings.UI.Library / IntProperty.cs
IntProperty.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 System;
 6  using System.Globalization;
 7  using System.Text.Json;
 8  using System.Text.Json.Serialization;
 9  
10  namespace Microsoft.PowerToys.Settings.UI.Library
11  {
12      // Represents the configuration property of the settings that store Integer type.
13      public record IntProperty : ICmdLineRepresentable
14      {
15          public IntProperty()
16          {
17              Value = 0;
18          }
19  
20          public IntProperty(int value)
21          {
22              Value = value;
23          }
24  
25          // Gets or sets the integer value of the settings configuration.
26          [JsonPropertyName("value")]
27          public int Value { get; set; }
28  
29          public static bool TryParseFromCmd(string cmd, out object result)
30          {
31              result = null;
32  
33              if (!int.TryParse(cmd, out var value))
34              {
35                  return false;
36              }
37  
38              result = new IntProperty { Value = value };
39              return true;
40          }
41  
42          // Returns a JSON version of the class settings configuration class.
43          public override string ToString()
44          {
45              return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.IntProperty);
46          }
47  
48          public static implicit operator IntProperty(int v)
49          {
50              throw new NotImplementedException();
51          }
52  
53          public static implicit operator IntProperty(uint v)
54          {
55              throw new NotImplementedException();
56          }
57  
58          public bool TryToCmdRepresentable(out string result)
59          {
60              result = Value.ToString(CultureInfo.InvariantCulture);
61              return true;
62          }
63      }
64  }