5
5
using System . Collections . Generic ;
6
6
using System . Diagnostics ;
7
7
using System . Drawing ;
8
- using System . Runtime . InteropServices ;
9
8
using System . Reflection ;
9
+ using System . Runtime . InteropServices ;
10
10
using System . Text . RegularExpressions ;
11
11
using System . Threading ;
12
12
using System . Windows . Forms ;
@@ -140,15 +140,23 @@ void controller_Errored(object sender, System.IO.ErrorEventArgs e)
140
140
{
141
141
MessageBox . Show ( e . GetException ( ) . ToString ( ) , String . Format ( I18N . GetString ( "Shadowsocks Error: {0}" ) , e . GetException ( ) . Message ) ) ;
142
142
}
143
- public static void SetNotifyIconText ( NotifyIcon ni , string text )
143
+
144
+ private static void SetNotifyIconText ( NotifyIcon ni , string text )
144
145
{
145
- if ( text . Length >= 128 ) throw new ArgumentOutOfRangeException ( "Text limited to 127 characters" ) ;
146
- Type t = typeof ( NotifyIcon ) ;
147
- BindingFlags hidden = BindingFlags . NonPublic | BindingFlags . Instance ;
148
- t . GetField ( "text" , hidden ) . SetValue ( ni , text ) ;
149
- if ( ( bool ) t . GetField ( "added" , hidden ) . GetValue ( ni ) )
150
- t . GetMethod ( "UpdateIcon" , hidden ) . Invoke ( ni , new object [ ] { true } ) ;
146
+ if ( text . Length > 127 )
147
+ {
148
+ text = text . Substring ( 0 , 127 ) ;
149
+ }
150
+
151
+ var t = typeof ( NotifyIcon ) ;
152
+ const BindingFlags hidden = BindingFlags . NonPublic | BindingFlags . Instance ;
153
+ t . GetField ( @"text" , hidden ) ? . SetValue ( ni , text ) ;
154
+ if ( t . GetField ( @"added" , hidden ) ? . GetValue ( ni ) is bool b && b )
155
+ {
156
+ t . GetMethod ( @"UpdateIcon" , hidden ) ? . Invoke ( ni , new object [ ] { true } ) ;
157
+ }
151
158
}
159
+
152
160
private void UpdateTrayIcon ( )
153
161
{
154
162
int dpi = 96 ;
@@ -158,16 +166,6 @@ private void UpdateTrayIcon()
158
166
}
159
167
Configuration config = controller . GetCurrentConfiguration ( ) ;
160
168
bool enabled = config . sysProxyMode != ( int ) ProxyMode . NoModify && config . sysProxyMode != ( int ) ProxyMode . Direct ;
161
- string server ;
162
- if ( config . random )
163
- {
164
- server = config . balanceAlgorithm ;
165
- }
166
- else
167
- {
168
- int server_current = config . index ;
169
- server = config . configs [ server_current ] . remarks ;
170
- }
171
169
bool global = config . sysProxyMode == ( int ) ProxyMode . Global ;
172
170
bool random = config . random ;
173
171
@@ -231,44 +229,40 @@ private void UpdateTrayIcon()
231
229
_notifyIcon . Icon = newIcon ;
232
230
}
233
231
234
- string strServer = server ;
232
+ var strServer = random ? config . balanceAlgorithm : config . configs [ config . index ] . remarks ;
235
233
switch ( strServer )
236
234
{
237
235
case "OneByOne" :
238
- strServer = I18N . GetString ( "Balance" ) + " : " + I18N . GetString ( "OneByOne" ) ;
236
+ strServer = $@ " { I18N . GetString ( "Balance" ) } : { I18N . GetString ( "OneByOne" ) } " ;
239
237
break ;
240
238
case "Random" :
241
- strServer = I18N . GetString ( "Balance" ) + " : " + I18N . GetString ( "Random" ) ;
239
+ strServer = $@ " { I18N . GetString ( "Balance" ) } : { I18N . GetString ( "Random" ) } " ;
242
240
break ;
243
241
case "FastDownloadSpeed" :
244
- strServer = I18N . GetString ( "Balance" ) + " : " + I18N . GetString ( "FastDownloadSpeed" ) ;
242
+ strServer = $@ " { I18N . GetString ( "Balance" ) } : { I18N . GetString ( "FastDownloadSpeed" ) } " ;
245
243
break ;
246
244
case "LowLatency" :
247
- strServer = I18N . GetString ( "Balance" ) + " : " + I18N . GetString ( "LowLatency" ) ;
245
+ strServer = $@ " { I18N . GetString ( "Balance" ) } : { I18N . GetString ( "LowLatency" ) } " ;
248
246
break ;
249
247
case "LowException" :
250
- strServer = I18N . GetString ( "Balance" ) + " : " + I18N . GetString ( "LowException" ) ;
248
+ strServer = $@ " { I18N . GetString ( "Balance" ) } : { I18N . GetString ( "LowException" ) } " ;
251
249
break ;
252
250
case "SelectedFirst" :
253
- strServer = I18N . GetString ( "Balance" ) + " : " + I18N . GetString ( "SelectedFirst" ) ;
251
+ strServer = $@ " { I18N . GetString ( "Balance" ) } : { I18N . GetString ( "SelectedFirst" ) } " ;
254
252
break ;
255
253
case "Timer" :
256
- strServer = I18N . GetString ( "Balance" ) + " : " + I18N . GetString ( "OneByOne" ) ;
257
- break ;
258
- default :
259
- strServer = server ;
254
+ strServer = $@ "{ I18N . GetString ( "Balance" ) } : { I18N . GetString ( "OneByOne" ) } ";
260
255
break ;
261
256
}
262
- // we want to show more details but notify icon title is limited to 63 characters
263
- string text = ( enabled ?
264
- ( global ? I18N . GetString ( "Global" ) : I18N . GetString ( "PAC" ) ) :
257
+ // we want to show more details but notify icon title is limited to 127 characters
258
+ var text = ( enabled ?
259
+ global ? I18N . GetString ( "Global" ) : I18N . GetString ( "PAC" ) :
265
260
I18N . GetString ( "Disable system proxy" ) )
266
- + " \r \n "
261
+ + Environment . NewLine
267
262
+ strServer
268
- + " \r \n "
269
- + String . Format ( I18N . GetString ( "Running: Port {0}" ) , config . localPort ) // this feedback is very important because they need to know Shadowsocks is running
263
+ + Environment . NewLine
264
+ + string . Format ( I18N . GetString ( "Running: Port {0}" ) , config . localPort ) // this feedback is very important because they need to know Shadowsocks is running
270
265
;
271
- //_notifyIcon.Text = text.Substring(0, Math.Min(63, text.Length));
272
266
SetNotifyIconText ( _notifyIcon , text ) ;
273
267
}
274
268
0 commit comments