Skip to content

Commit 21a073e

Browse files
author
Yizhao Han
committed
Update Version to 1.0.13
Add option to show or hide tray icon.
1 parent 140db31 commit 21a073e

14 files changed

+226
-109
lines changed

NetSpeedMonitor/App.xaml.cs

+32-22
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
6464
{
6565
if (Int32.TryParse(e.Args[1], out int id))
6666
{
67-
ProcessDetailWindow w = new ProcessDetailWindow(id, null);
67+
ProcessDetailWindow w = new ProcessDetailWindow(id);
6868
Dispatcher.InvokeAsync(new Action(() => { w.Show(); }));
6969
}
7070
}
@@ -94,7 +94,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
9494
{
9595
Dispatcher.InvokeAsync(new Action(() =>
9696
{
97-
InitViewAndNeedCloseResourcees();
97+
InitViewAndNeedClosedResourcees();
9898
welcomeWindow.ReduceAndClose(new Point(mainWindow.Left + mainWindow.Width / 2, mainWindow.Top + mainWindow.Height / 2));
9999
}));
100100
}
@@ -138,12 +138,9 @@ private void InitLanguage()
138138
}
139139
}
140140

141-
private void InitViewAndNeedCloseResourcees()
141+
private void InitViewAndNeedClosedResourcees()
142142
{
143143
mainWindow = new MainWindow();
144-
detailWindow = new DetailWindow(mainWindow);
145-
mainWindow.SetDetailWindow(detailWindow);
146-
detailWindow.IsVisibleChanged += DetailWindow_IsVisibleChanged;
147144
if (Settings.Default.MainWindowLeft > -200000 && Settings.Default.MainWindowTop > -200000)
148145
{
149146
mainWindow.Left = Settings.Default.MainWindowLeft;
@@ -241,12 +238,15 @@ private void CheckScreenCount()
241238
}
242239
}
243240

244-
private void DetailWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
241+
public void NeedPortProcessMap(object sender, bool need)
245242
{
246-
portProcessMap.Enabled = detailWindow.IsVisible;
247-
if (!detailWindow.IsVisible)
243+
if (need)
248244
{
249-
mainWindow.TryToEdgeHide();
245+
portProcessMap.RegisterCustomer(sender);
246+
}
247+
else
248+
{
249+
portProcessMap.UnRegisterCustomer(sender);
250250
}
251251
}
252252

@@ -258,6 +258,10 @@ private void InitializeTray()
258258
{
259259
Checked = Settings.Default.edgeHide
260260
};
261+
menuShowTrayIcon = new System.Windows.Forms.MenuItem(FindResource("ShowTrayIcon").ToString(), TrayMenu_Click)
262+
{
263+
Checked = Settings.Default.ShowTrayIcon
264+
};
261265
menuStartOnBoot = new System.Windows.Forms.MenuItem(FindResource("StartOnBoot").ToString(), TrayMenu_Click)
262266
{
263267
Checked = Settings.Default.startOnBoot
@@ -270,7 +274,7 @@ private void InitializeTray()
270274
Tag = ""
271275
};
272276
menuLanguage.MenuItems.Add(menuDefault);
273-
List<OneLanguage> languages = Languages.getLanguages();
277+
List<OneLanguage> languages = Languages.GetLanguages();
274278
foreach (OneLanguage i in languages)
275279
{
276280
System.Windows.Forms.MenuItem menuItem = new System.Windows.Forms.MenuItem()
@@ -297,13 +301,13 @@ private void InitializeTray()
297301

298302
menuAbout = new System.Windows.Forms.MenuItem(FindResource("AboutNetSpeedMonitor").ToString(), TrayMenu_Click);
299303
System.Windows.Forms.ContextMenu menu = new System.Windows.Forms.ContextMenu(new System.Windows.Forms.MenuItem[] {
300-
menuStartOnBoot, menuEdgeHide, menuLanguage, menuUpdate, menuAbout, menuExit });
304+
menuStartOnBoot, menuEdgeHide,menuShowTrayIcon, menuLanguage, menuUpdate, menuAbout, menuExit });
301305

302306
notifyIcon = new System.Windows.Forms.NotifyIcon
303307
{
304308
Icon = new System.Drawing.Icon(GetResourceStream(new Uri("pack://application:,,,/icon.ico", UriKind.RelativeOrAbsolute)).Stream),
305309
ContextMenu = menu,
306-
Visible = true
310+
Visible = Settings.Default.ShowTrayIcon
307311
};
308312
}
309313

@@ -365,7 +369,6 @@ public void TryToExit()
365369
{
366370
Microsoft.Win32.SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;
367371
SystemParameters.StaticPropertyChanged -= SystemParameters_StaticPropertyChanged;
368-
portProcessMap.Enabled = false;
369372
mainWindow.RegisterAppBar(false);
370373
timer.Enabled = false;
371374
notifyIcon.Dispose();
@@ -375,6 +378,13 @@ public void TryToExit()
375378
t.Start();
376379
}
377380

381+
public void TryToSetShowTrayIcon(bool showTrayIcon)
382+
{
383+
Settings.Default.ShowTrayIcon = showTrayIcon;
384+
Settings.Default.Save();
385+
notifyIcon.Visible = showTrayIcon;
386+
}
387+
378388
public void TryToSetEdgeHide(bool edgeHide)
379389
{
380390
Settings.Default.edgeHide = edgeHide;
@@ -407,6 +417,12 @@ private void TrayMenu_Click(object sender, EventArgs e)
407417
mainWindow.WindowMenuEdgeHide.IsChecked = menuEdgeHide.Checked;
408418
TryToSetEdgeHide(menuEdgeHide.Checked);
409419
}
420+
else if(sender == menuShowTrayIcon)
421+
{
422+
menuShowTrayIcon.Checked = !menuShowTrayIcon.Checked;
423+
mainWindow.WindowMenuShowTrayIcon.IsChecked = menuShowTrayIcon.Checked;
424+
TryToSetShowTrayIcon(menuShowTrayIcon.Checked);
425+
}
410426
else if (sender == menuAutoUpdate)
411427
{
412428
menuAutoUpdate.Checked = !menuAutoUpdate.Checked;
@@ -428,20 +444,14 @@ private void Timer_Elapsed(object sender, ElapsedEventArgs e)
428444
UDStatistic statistics = udMap.NextStatistic(10, portProcessMap);
429445
Dispatcher.Invoke(new Action(() =>
430446
{
431-
mainWindow.UploadLabel.Content = Tool.GetNetSpeedString(statistics.upload, statistics.timeSpan);
432-
mainWindow.DownloadLabel.Content = Tool.GetNetSpeedString(statistics.download, statistics.timeSpan);
433-
if (detailWindow.Visibility == Visibility.Visible)
434-
{
435-
detailWindow.NewData(statistics.items, statistics.timeSpan);
436-
}
447+
mainWindow.NewData(statistics);
437448
}));
438449
}
439450

440-
public System.Windows.Forms.MenuItem menuExit, menuEdgeHide, menuStartOnBoot, menuAutoUpdate, menuCheckUpdate, menuAbout;
451+
public System.Windows.Forms.MenuItem menuExit, menuEdgeHide, menuShowTrayIcon, menuStartOnBoot, menuAutoUpdate, menuCheckUpdate, menuAbout;
441452

442453
private System.Windows.Forms.NotifyIcon notifyIcon;
443454
private MainWindow mainWindow;
444-
private DetailWindow detailWindow;
445455
private WelcomeWindow welcomeWindow;
446456
private CaptureManager captureManager;
447457
private UDMap udMap = new UDMap();

NetSpeedMonitor/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@
5151
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
5252
// 方法是按如下所示使用“*”: :
5353
// [assembly: AssemblyVersion("1.0.*")]
54-
[assembly: AssemblyVersion("1.0.1.2")]
55-
[assembly: AssemblyFileVersion("1.0.1.2")]
54+
[assembly: AssemblyVersion("1.0.1.3")]
55+
[assembly: AssemblyFileVersion("1.0.1.3")]

NetSpeedMonitor/Properties/Settings.Designer.cs

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

NetSpeedMonitor/Properties/Settings.settings

+3
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
<Setting Name="AutoUpdate" Type="System.Boolean" Scope="User">
2121
<Value Profile="(Default)">True</Value>
2222
</Setting>
23+
<Setting Name="ShowTrayIcon" Type="System.Boolean" Scope="User">
24+
<Value Profile="(Default)">True</Value>
25+
</Setting>
2326
</Settings>
2427
</SettingsFile>

NetSpeedMonitor/Resources/StringResource.xaml

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@
3030
<sys:String x:Key="OK">OK</sys:String>
3131
<sys:String x:Key="Version">Version</sys:String>
3232
<sys:String x:Key="CurrentVersion">Current Version:</sys:String>
33+
<sys:String x:Key="ShowTrayIcon">Show tray icon</sys:String>
3334
</ResourceDictionary>

NetSpeedMonitor/Resources/StringResource.zh-CN.xaml

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
<sys:String x:Key="OK">确定</sys:String>
3030
<sys:String x:Key="Version">版本</sys:String>
3131
<sys:String x:Key="CurrentVersion">当前版本:</sys:String>
32+
<sys:String x:Key="ShowTrayIcon">显示托盘图标</sys:String>
3233
</ResourceDictionary>

NetSpeedMonitor/Service/Languages.cs

+27-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99

1010
namespace USTC.Software.hanyizhao.NetSpeedMonitor
1111
{
12+
/// <summary>
13+
/// Multi-language resources
14+
/// </summary>
1215
class Languages
1316
{
14-
public static List<OneLanguage> getLanguages()
17+
/// <summary>
18+
/// Get language list.
19+
/// </summary>
20+
/// <returns>Lnguage list</returns>
21+
public static List<OneLanguage> GetLanguages()
1522
{
1623
List<OneLanguage> result = new List<OneLanguage>
1724
{
@@ -21,6 +28,10 @@ public static List<OneLanguage> getLanguages()
2128
return result;
2229
}
2330

31+
/// <summary>
32+
/// Get default language resource according to language of PC.
33+
/// </summary>
34+
/// <returns>Language resource according to language of PC. Or null if don't support the language of PC.</returns>
2435
public static ResourceDictionary GetFromDefaultLanguage()
2536
{
2637
ResourceDictionary result = null;
@@ -39,6 +50,11 @@ public static ResourceDictionary GetFromDefaultLanguage()
3950
return result;
4051
}
4152

53+
/// <summary>
54+
/// Get language resource.
55+
/// </summary>
56+
/// <param name="fileName">The name of language resource file.</param>
57+
/// <returns>Language resource or null</returns>
4258
public static ResourceDictionary GetResourceDictionary(String fileName)
4359
{
4460
ResourceDictionary result = null;
@@ -60,10 +76,19 @@ public static ResourceDictionary GetResourceDictionary(String fileName)
6076
}
6177
}
6278

79+
/// <summary>
80+
/// One supported language
81+
/// </summary>
6382
class OneLanguage
6483
{
65-
84+
/// <summary>
85+
/// The name of language resource file.
86+
/// </summary>
6687
public String FileName { get { return fileName; } }
88+
89+
/// <summary>
90+
/// The name used to display to user.
91+
/// </summary>
6792
public String ShowName { get { return showName; } }
6893

6994
public OneLanguage(String fileName, String showName)

NetSpeedMonitor/Service/PortProcessMap.cs

+41-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static PortProcessMap GetInstance()
3030
}
3131

3232
/// <summary>
33-
/// Start or Stop mapping. (Reduce the cost of CPU)
33+
/// Check whether this class is working.
3434
/// </summary>
3535
public bool Enabled
3636
{
@@ -41,12 +41,46 @@ public bool Enabled
4141
return timer.Enabled;
4242
}
4343
}
44-
set
44+
}
45+
46+
/// <summary>
47+
/// Let this class know that the object needs this class.
48+
/// </summary>
49+
/// <param name="customer">the object that needs this class</param>
50+
public void RegisterCustomer(object customer)
51+
{
52+
lock (timerLock)
4553
{
46-
lock(timerLock)
47-
{
48-
timer.Enabled = value;
49-
}
54+
Console.WriteLine("PortProessMap Add Customer");
55+
customers.Add(customer);
56+
CheckTimer();
57+
}
58+
}
59+
60+
/// <summary>
61+
/// Let this class know that the object does not need this class any more.(Reduce the cost of CPU)
62+
/// </summary>
63+
/// <param name="customer">the object that does not need this class</param>
64+
public void UnRegisterCustomer(object customer)
65+
{
66+
lock(timerLock)
67+
{
68+
Console.WriteLine("PortProessMap Remove Customer");
69+
customers.Remove(customer);
70+
CheckTimer();
71+
}
72+
}
73+
74+
private void CheckTimer()
75+
{
76+
Console.WriteLine("PortProcessMap CheckTimer count:" + customers.Count);
77+
if(customers.Count == 0)
78+
{
79+
timer.Enabled = false;
80+
}
81+
else
82+
{
83+
timer.Enabled = true;
5084
}
5185
}
5286

@@ -124,6 +158,7 @@ private void Timer_Elapsed(object sender, ElapsedEventArgs e)
124158

125159
}
126160

161+
private HashSet<object> customers = new HashSet<object>();
127162
private readonly object timerLock = new object();
128163
private Timer timer;
129164
private Dictionary<Port, int> map;

0 commit comments

Comments
 (0)