Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit 7489cb3

Browse files
committed
Some improvements, optimizations and bugfixes for #10
1 parent 9e900b5 commit 7489cb3

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

shadowsocks-csharp/View/MenuViewController.cs

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Drawing;
8-
using System.Runtime.InteropServices;
98
using System.Reflection;
9+
using System.Runtime.InteropServices;
1010
using System.Text.RegularExpressions;
1111
using System.Threading;
1212
using System.Windows.Forms;
@@ -140,15 +140,23 @@ void controller_Errored(object sender, System.IO.ErrorEventArgs e)
140140
{
141141
MessageBox.Show(e.GetException().ToString(), String.Format(I18N.GetString("Shadowsocks Error: {0}"), e.GetException().Message));
142142
}
143-
public static void SetNotifyIconText(NotifyIcon ni, string text)
143+
144+
private static void SetNotifyIconText(NotifyIcon ni, string text)
144145
{
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+
}
151158
}
159+
152160
private void UpdateTrayIcon()
153161
{
154162
int dpi = 96;
@@ -158,16 +166,6 @@ private void UpdateTrayIcon()
158166
}
159167
Configuration config = controller.GetCurrentConfiguration();
160168
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-
}
171169
bool global = config.sysProxyMode == (int)ProxyMode.Global;
172170
bool random = config.random;
173171

@@ -231,44 +229,40 @@ private void UpdateTrayIcon()
231229
_notifyIcon.Icon = newIcon;
232230
}
233231

234-
string strServer = server;
232+
var strServer = random ? config.balanceAlgorithm : config.configs[config.index].remarks;
235233
switch (strServer)
236234
{
237235
case "OneByOne":
238-
strServer = I18N.GetString("Balance") + " : " + I18N.GetString("OneByOne");
236+
strServer = $@"{I18N.GetString("Balance")} : {I18N.GetString("OneByOne")}";
239237
break;
240238
case "Random":
241-
strServer = I18N.GetString("Balance") + " : " + I18N.GetString("Random");
239+
strServer = $@"{I18N.GetString("Balance")} : {I18N.GetString("Random")}";
242240
break;
243241
case "FastDownloadSpeed":
244-
strServer = I18N.GetString("Balance") + " : " + I18N.GetString("FastDownloadSpeed");
242+
strServer = $@"{I18N.GetString("Balance")} : {I18N.GetString("FastDownloadSpeed")}";
245243
break;
246244
case "LowLatency":
247-
strServer = I18N.GetString("Balance") + " : " + I18N.GetString("LowLatency");
245+
strServer = $@"{I18N.GetString("Balance")} : {I18N.GetString("LowLatency")}";
248246
break;
249247
case "LowException":
250-
strServer = I18N.GetString("Balance") + " : " + I18N.GetString("LowException");
248+
strServer = $@"{I18N.GetString("Balance")} : {I18N.GetString("LowException")}";
251249
break;
252250
case "SelectedFirst":
253-
strServer = I18N.GetString("Balance") + " : " + I18N.GetString("SelectedFirst");
251+
strServer = $@"{I18N.GetString("Balance")} : {I18N.GetString("SelectedFirst")}";
254252
break;
255253
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")}";
260255
break;
261256
}
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") :
265260
I18N.GetString("Disable system proxy"))
266-
+ "\r\n"
261+
+ Environment.NewLine
267262
+ 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
270265
;
271-
//_notifyIcon.Text = text.Substring(0, Math.Min(63, text.Length));
272266
SetNotifyIconText(_notifyIcon, text);
273267
}
274268

0 commit comments

Comments
 (0)