@@ -16,7 +16,7 @@ private enum IconKind
1616 }
1717
1818 private readonly NotifyIconAdv _notifyIcon ;
19- private IconFactory _iconFactory ;
19+ private readonly IconFactory _iconFactory ;
2020 private IconKind _iconKind ;
2121
2222 public SensorNotifyIcon ( SystemTray sensorSystemTray , ISensor sensor , PersistentSettings settings )
@@ -110,45 +110,23 @@ private string GetString()
110110 if ( ! Sensor . Value . HasValue )
111111 return "-" ;
112112
113- switch ( Sensor . SensorType )
113+ return Sensor . SensorType switch
114114 {
115- case SensorType . Temperature :
116- return UnitManager . IsFahrenheitUsed ? $ "{ UnitManager . CelsiusToFahrenheit ( Sensor . Value ) : F0} " : $ "{ Sensor . Value : F0} ";
117- case SensorType . TimeSpan :
118- return $ "{ TimeSpan . FromSeconds ( Sensor . Value . Value ) : g} ";
119- case SensorType . Clock :
120- case SensorType . Fan :
121- case SensorType . Flow :
122- return $ "{ 1e-3f * Sensor . Value : F1} ";
123- case SensorType . Voltage :
124- case SensorType . Current :
125- case SensorType . SmallData :
126- case SensorType . Factor :
127- case SensorType . Conductivity :
128- return $ "{ Sensor . Value : F1} ";
129- case SensorType . IntFactor :
130- return $ "{ Sensor . Value : F0} ";
131- case SensorType . Throughput :
132- return GetThroughputValue ( Sensor . Value ?? 0 ) ;
133- case SensorType . Control :
134- case SensorType . Frequency :
135- case SensorType . Level :
136- case SensorType . Load :
137- case SensorType . Noise :
138- case SensorType . Humidity :
139- return $ "{ Sensor . Value : F0} ";
140- case SensorType . Energy :
141- case SensorType . Power :
142- case SensorType . Data :
143- return Sensor . Value . Value . ToTrayValue ( ) ;
144- default :
145- return "-" ;
146- }
115+ SensorType . Temperature => UnitManager . IsFahrenheitUsed ? $ "{ UnitManager . CelsiusToFahrenheit ( Sensor . Value ) : F0} " : $ "{ Sensor . Value : F0} ",
116+ SensorType . TimeSpan => $ "{ TimeSpan . FromSeconds ( Sensor . Value . Value ) : g} ",
117+ SensorType . Clock or SensorType . Fan or SensorType . Flow => $ "{ 1e-3f * Sensor . Value : F1} ",
118+ SensorType . Voltage or SensorType . Current or SensorType . SmallData or SensorType . Factor or SensorType . Conductivity => $ "{ Sensor . Value : F1} ",
119+ SensorType . IntFactor => $ "{ Sensor . Value : F0} ",
120+ SensorType . Throughput => GetThroughputValue ( Sensor . Value ?? 0 ) ,
121+ SensorType . Control or SensorType . Frequency or SensorType . Level or SensorType . Load or SensorType . Noise or SensorType . Humidity => $ "{ Sensor . Value : F0} ",
122+ SensorType . Energy or SensorType . Power or SensorType . Data => Sensor . Value . Value . ToTrayValue ( ) ,
123+ _ => "-" ,
124+ } ;
147125 }
148126
149127 private static string GetThroughputValue ( float byteCount , byte preferredSufNum = 1 , bool addSuffix = false )
150128 {
151- string [ ] suf = { " B" , " KB" , " MB" , " GB" } ;
129+ string [ ] suf = [ " B" , " KB" , " MB" , " GB" ] ;
152130 if ( byteCount == 0 )
153131 return addSuffix ? "0" + suf [ 0 ] : "0" ;
154132 long bytes = Math . Abs ( ( long ) byteCount ) ;
@@ -158,7 +136,7 @@ private static string GetThroughputValue(float byteCount, byte preferredSufNum =
158136 double num = Math . Round ( bytes / Math . Pow ( 1024 , place ) , 1 ) ;
159137 if ( num >= 10 )
160138 num = Math . Round ( num , 0 ) ;
161- var result = ( Math . Sign ( byteCount ) * num ) . ToString ( ) ;
139+ string result = ( Math . Sign ( byteCount ) * num ) . ToString ( ) ;
162140 if ( addSuffix )
163141 return result + suf [ place ] ;
164142 return new string ( result . Take ( 3 ) . ToArray ( ) ) ;
@@ -167,61 +145,44 @@ private static string GetThroughputValue(float byteCount, byte preferredSufNum =
167145 public void Update ( )
168146 {
169147 Icon icon = _notifyIcon . Icon ;
170- switch ( Sensor . SensorType )
148+ _notifyIcon . Icon = Sensor . SensorType switch
171149 {
172- case SensorType . Load :
173- case SensorType . Control :
174- case SensorType . Level :
175- _notifyIcon . Icon = _iconKind switch
176- {
177- IconKind . Percent => _iconFactory . CreatePercentageIcon ( Sensor . Value . GetValueOrDefault ( ) ) ,
178- IconKind . Pie => _iconFactory . CreatePercentagePieIcon ( ( byte ) Sensor . Value . GetValueOrDefault ( ) ) ,
179- _ => _iconFactory . CreateTransparentIcon ( GetString ( ) ) ,
180- } ;
181- break ;
182- default :
183- _notifyIcon . Icon = _iconFactory . CreateTransparentIcon ( GetString ( ) ) ;
184- break ;
185- }
150+ SensorType . Load or SensorType . Control or SensorType . Level => _iconKind switch
151+ {
152+ IconKind . Percent => _iconFactory . CreatePercentageIcon ( Sensor . Value . GetValueOrDefault ( ) ) ,
153+ IconKind . Pie => _iconFactory . CreatePercentagePieIcon ( ( byte ) Sensor . Value . GetValueOrDefault ( ) ) ,
154+ _ => _iconFactory . CreateTransparentIcon ( GetString ( ) ) ,
155+ } ,
156+ _ => _iconFactory . CreateTransparentIcon ( GetString ( ) ) ,
157+ } ;
186158 icon ? . Destroy ( ) ;
187-
188- string format ;
189- switch ( Sensor . SensorType )
190- {
191- case SensorType . Voltage : format = "\n {0}: {1:F2} V" ; break ;
192- case SensorType . Current : format = "\n {0}: {1:F2} A" ; break ;
193- case SensorType . Clock : format = "\n {0}: {1:F0} MHz" ; break ;
194- case SensorType . Load : format = "\n {0}: {1:F1} %" ; break ;
195- case SensorType . Temperature : format = "\n {0}: {1:F1} °C" ; break ;
196- case SensorType . Fan : format = "\n {0}: {1:F0} RPM" ; break ;
197- case SensorType . Flow : format = "\n {0}: {1:F0} L/h" ; break ;
198- case SensorType . Control : format = "\n {0}: {1:F1} %" ; break ;
199- case SensorType . Level : format = "\n {0}: {1:F1} %" ; break ;
200- case SensorType . Power : format = "\n {0}: {1:F2} W" ; break ;
201- case SensorType . Data : format = "\n {0}: {1:F2} GB" ; break ;
202- case SensorType . Factor : format = "\n {0}: {1:F3}" ; break ;
203- case SensorType . IntFactor : format = "\n {0}: {1:F0}" ; break ;
204- case SensorType . Energy : format = "\n {0}: {0:F0} mWh" ; break ;
205- case SensorType . Noise : format = "\n {0}: {0:F0} dBA" ; break ;
206- case SensorType . Conductivity : format = "\n {0}: {0:F1} µS/cm" ; break ;
207- case SensorType . Humidity : format = "\n {0}: {0:F0} %" ; break ;
208- default : format = "\n {0}: {1}" ; break ;
209- }
210-
211- string formattedValue ;
212- if ( Sensor . SensorType == SensorType . Temperature && UnitManager . IsFahrenheitUsed )
213- {
214- formattedValue = string . Format ( "\n {0}: {1:F1} °F" , Sensor . Name , UnitManager . CelsiusToFahrenheit ( Sensor . Value ) ) ;
215- }
216- else if ( Sensor . SensorType == SensorType . Throughput && ! "Connection Speed" . Equals ( Sensor . Name ) )
159+ string format = Sensor . SensorType switch
217160 {
218- formattedValue = $ "\n { Sensor . Name } : { GetThroughputValue ( Sensor . Value ?? 0 , 0 , true ) } /s";
219- }
220- else
161+ SensorType . Voltage => "\n {0}: {1:F2} V" ,
162+ SensorType . Current => "\n {0}: {1:F2} A" ,
163+ SensorType . Clock => "\n {0}: {1:F0} MHz" ,
164+ SensorType . Load => "\n {0}: {1:F1} %" ,
165+ SensorType . Temperature => "\n {0}: {1:F1} °C" ,
166+ SensorType . Fan => "\n {0}: {1:F0} RPM" ,
167+ SensorType . Flow => "\n {0}: {1:F0} L/h" ,
168+ SensorType . Control => "\n {0}: {1:F1} %" ,
169+ SensorType . Level => "\n {0}: {1:F1} %" ,
170+ SensorType . Power => "\n {0}: {1:F2} W" ,
171+ SensorType . Data => "\n {0}: {1:F2} GB" ,
172+ SensorType . Factor => "\n {0}: {1:F3}" ,
173+ SensorType . IntFactor => "\n {0}: {1:F0}" ,
174+ SensorType . Energy => "\n {0}: {0:F0} mWh" ,
175+ SensorType . Noise => "\n {0}: {0:F0} dBA" ,
176+ SensorType . Conductivity => "\n {0}: {0:F1} µS/cm" ,
177+ SensorType . Humidity => "\n {0}: {0:F0} %" ,
178+ _ => "\n {0}: {1}" ,
179+ } ;
180+ string formattedValue = Sensor . SensorType switch
221181 {
222- formattedValue = string . Format ( format , Sensor . Name , Sensor . Value ) ;
223- }
224-
182+ SensorType . Temperature when UnitManager . IsFahrenheitUsed => string . Format ( "\n {0}: {1:F1} °F" , Sensor . Name , UnitManager . CelsiusToFahrenheit ( Sensor . Value ) ) ,
183+ SensorType . Throughput when ! "Connection Speed" . Equals ( Sensor . Name ) => $ "\n { Sensor . Name } : { GetThroughputValue ( Sensor . Value ?? 0 , 0 , true ) } /s",
184+ _ => string . Format ( format , Sensor . Name , Sensor . Value ) ,
185+ } ;
225186 string hardwareName = Sensor . Hardware . Name ;
226187 hardwareName = hardwareName . Substring ( 0 , Math . Min ( 63 - formattedValue . Length , hardwareName . Length ) ) ;
227188 string text = hardwareName + formattedValue ;
0 commit comments