Browse Source

Workaround NotifyIcon's 63 chars limit(#965)

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
tags/3.4.2.1
Syrone Wong 7 years ago
parent
commit
d21162318e
2 changed files with 16 additions and 2 deletions
  1. +14
    -0
      shadowsocks-csharp/Util/ViewUtils.cs
  2. +2
    -2
      shadowsocks-csharp/View/MenuViewController.cs

+ 14
- 0
shadowsocks-csharp/Util/ViewUtils.cs View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Windows.Forms; using System.Windows.Forms;


namespace Shadowsocks.Util namespace Shadowsocks.Util
@@ -17,5 +18,18 @@ namespace Shadowsocks.Util
var children = control.Controls.OfType<TControl>().ToList(); var children = control.Controls.OfType<TControl>().ToList();
return children.SelectMany(GetChildControls<TControl>).Concat(children); return children.SelectMany(GetChildControls<TControl>).Concat(children);
} }

// Workaround NotifyIcon's 63 chars limit
// https://stackoverflow.com/questions/579665/how-can-i-show-a-systray-tooltip-longer-than-63-chars
public static void SetNotifyIconText(NotifyIcon ni, string text)
{
if (text.Length >= 128)
throw new ArgumentOutOfRangeException("Text limited to 127 characters");
Type t = typeof(NotifyIcon);
BindingFlags hidden = BindingFlags.NonPublic | BindingFlags.Instance;
t.GetField("text", hidden).SetValue(ni, text);
if ((bool)t.GetField("added", hidden).GetValue(ni))
t.GetMethod("UpdateIcon", hidden).Invoke(ni, new object[] { true });
}
} }
} }

+ 2
- 2
shadowsocks-csharp/View/MenuViewController.cs View File

@@ -180,13 +180,13 @@ namespace Shadowsocks.View
{ {
serverInfo = config.GetCurrentServer().FriendlyName(); serverInfo = config.GetCurrentServer().FriendlyName();
} }
// we want to show more details but notify icon title is limited to 63 characters
// show more info by hacking the P/Invoke declaration for NOTIFYICONDATA inside Windows Forms
string text = I18N.GetString("Shadowsocks") + " " + UpdateChecker.Version + "\n" + string text = I18N.GetString("Shadowsocks") + " " + UpdateChecker.Version + "\n" +
(enabled ? (enabled ?
I18N.GetString("System Proxy On: ") + (global ? I18N.GetString("Global") : I18N.GetString("PAC")) : I18N.GetString("System Proxy On: ") + (global ? I18N.GetString("Global") : I18N.GetString("PAC")) :
String.Format(I18N.GetString("Running: Port {0}"), config.localPort)) // this feedback is very important because they need to know Shadowsocks is running String.Format(I18N.GetString("Running: Port {0}"), config.localPort)) // this feedback is very important because they need to know Shadowsocks is running
+ "\n" + serverInfo; + "\n" + serverInfo;
_notifyIcon.Text = text.Substring(0, Math.Min(63, text.Length));
ViewUtils.SetNotifyIconText(_notifyIcon, text);
} }
private Bitmap getTrayIconByState(Bitmap originIcon, bool enabled, bool global) private Bitmap getTrayIconByState(Bitmap originIcon, bool enabled, bool global)


Loading…
Cancel
Save