Skip to content

Commit

Permalink
Update Version to 1.0.13
Browse files Browse the repository at this point in the history
Add option to show or hide tray icon.
  • Loading branch information
Yizhao Han committed Apr 23, 2018
1 parent 140db31 commit 21a073e
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 109 deletions.
54 changes: 32 additions & 22 deletions NetSpeedMonitor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
{
if (Int32.TryParse(e.Args[1], out int id))
{
ProcessDetailWindow w = new ProcessDetailWindow(id, null);
ProcessDetailWindow w = new ProcessDetailWindow(id);
Dispatcher.InvokeAsync(new Action(() => { w.Show(); }));
}
}
Expand Down Expand Up @@ -94,7 +94,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
{
Dispatcher.InvokeAsync(new Action(() =>
{
InitViewAndNeedCloseResourcees();
InitViewAndNeedClosedResourcees();
welcomeWindow.ReduceAndClose(new Point(mainWindow.Left + mainWindow.Width / 2, mainWindow.Top + mainWindow.Height / 2));
}));
}
Expand Down Expand Up @@ -138,12 +138,9 @@ private void InitLanguage()
}
}

private void InitViewAndNeedCloseResourcees()
private void InitViewAndNeedClosedResourcees()
{
mainWindow = new MainWindow();
detailWindow = new DetailWindow(mainWindow);
mainWindow.SetDetailWindow(detailWindow);
detailWindow.IsVisibleChanged += DetailWindow_IsVisibleChanged;
if (Settings.Default.MainWindowLeft > -200000 && Settings.Default.MainWindowTop > -200000)
{
mainWindow.Left = Settings.Default.MainWindowLeft;
Expand Down Expand Up @@ -241,12 +238,15 @@ private void CheckScreenCount()
}
}

private void DetailWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
public void NeedPortProcessMap(object sender, bool need)
{
portProcessMap.Enabled = detailWindow.IsVisible;
if (!detailWindow.IsVisible)
if (need)
{
mainWindow.TryToEdgeHide();
portProcessMap.RegisterCustomer(sender);
}
else
{
portProcessMap.UnRegisterCustomer(sender);
}
}

Expand All @@ -258,6 +258,10 @@ private void InitializeTray()
{
Checked = Settings.Default.edgeHide
};
menuShowTrayIcon = new System.Windows.Forms.MenuItem(FindResource("ShowTrayIcon").ToString(), TrayMenu_Click)
{
Checked = Settings.Default.ShowTrayIcon
};
menuStartOnBoot = new System.Windows.Forms.MenuItem(FindResource("StartOnBoot").ToString(), TrayMenu_Click)
{
Checked = Settings.Default.startOnBoot
Expand All @@ -270,7 +274,7 @@ private void InitializeTray()
Tag = ""
};
menuLanguage.MenuItems.Add(menuDefault);
List<OneLanguage> languages = Languages.getLanguages();
List<OneLanguage> languages = Languages.GetLanguages();
foreach (OneLanguage i in languages)
{
System.Windows.Forms.MenuItem menuItem = new System.Windows.Forms.MenuItem()
Expand All @@ -297,13 +301,13 @@ private void InitializeTray()

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

notifyIcon = new System.Windows.Forms.NotifyIcon
{
Icon = new System.Drawing.Icon(GetResourceStream(new Uri("pack://application:,,,/icon.ico", UriKind.RelativeOrAbsolute)).Stream),
ContextMenu = menu,
Visible = true
Visible = Settings.Default.ShowTrayIcon
};
}

Expand Down Expand Up @@ -365,7 +369,6 @@ public void TryToExit()
{
Microsoft.Win32.SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;
SystemParameters.StaticPropertyChanged -= SystemParameters_StaticPropertyChanged;
portProcessMap.Enabled = false;
mainWindow.RegisterAppBar(false);
timer.Enabled = false;
notifyIcon.Dispose();
Expand All @@ -375,6 +378,13 @@ public void TryToExit()
t.Start();
}

public void TryToSetShowTrayIcon(bool showTrayIcon)
{
Settings.Default.ShowTrayIcon = showTrayIcon;
Settings.Default.Save();
notifyIcon.Visible = showTrayIcon;
}

public void TryToSetEdgeHide(bool edgeHide)
{
Settings.Default.edgeHide = edgeHide;
Expand Down Expand Up @@ -407,6 +417,12 @@ private void TrayMenu_Click(object sender, EventArgs e)
mainWindow.WindowMenuEdgeHide.IsChecked = menuEdgeHide.Checked;
TryToSetEdgeHide(menuEdgeHide.Checked);
}
else if(sender == menuShowTrayIcon)
{
menuShowTrayIcon.Checked = !menuShowTrayIcon.Checked;
mainWindow.WindowMenuShowTrayIcon.IsChecked = menuShowTrayIcon.Checked;
TryToSetShowTrayIcon(menuShowTrayIcon.Checked);
}
else if (sender == menuAutoUpdate)
{
menuAutoUpdate.Checked = !menuAutoUpdate.Checked;
Expand All @@ -428,20 +444,14 @@ private void Timer_Elapsed(object sender, ElapsedEventArgs e)
UDStatistic statistics = udMap.NextStatistic(10, portProcessMap);
Dispatcher.Invoke(new Action(() =>
{
mainWindow.UploadLabel.Content = Tool.GetNetSpeedString(statistics.upload, statistics.timeSpan);
mainWindow.DownloadLabel.Content = Tool.GetNetSpeedString(statistics.download, statistics.timeSpan);
if (detailWindow.Visibility == Visibility.Visible)
{
detailWindow.NewData(statistics.items, statistics.timeSpan);
}
mainWindow.NewData(statistics);
}));
}

public System.Windows.Forms.MenuItem menuExit, menuEdgeHide, menuStartOnBoot, menuAutoUpdate, menuCheckUpdate, menuAbout;
public System.Windows.Forms.MenuItem menuExit, menuEdgeHide, menuShowTrayIcon, menuStartOnBoot, menuAutoUpdate, menuCheckUpdate, menuAbout;

private System.Windows.Forms.NotifyIcon notifyIcon;
private MainWindow mainWindow;
private DetailWindow detailWindow;
private WelcomeWindow welcomeWindow;
private CaptureManager captureManager;
private UDMap udMap = new UDMap();
Expand Down
4 changes: 2 additions & 2 deletions NetSpeedMonitor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.2")]
[assembly: AssemblyFileVersion("1.0.1.2")]
[assembly: AssemblyVersion("1.0.1.3")]
[assembly: AssemblyFileVersion("1.0.1.3")]
12 changes: 12 additions & 0 deletions NetSpeedMonitor/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions NetSpeedMonitor/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
<Setting Name="AutoUpdate" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="ShowTrayIcon" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
1 change: 1 addition & 0 deletions NetSpeedMonitor/Resources/StringResource.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
<sys:String x:Key="OK">OK</sys:String>
<sys:String x:Key="Version">Version</sys:String>
<sys:String x:Key="CurrentVersion">Current Version:</sys:String>
<sys:String x:Key="ShowTrayIcon">Show tray icon</sys:String>
</ResourceDictionary>
1 change: 1 addition & 0 deletions NetSpeedMonitor/Resources/StringResource.zh-CN.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
<sys:String x:Key="OK">确定</sys:String>
<sys:String x:Key="Version">版本</sys:String>
<sys:String x:Key="CurrentVersion">当前版本:</sys:String>
<sys:String x:Key="ShowTrayIcon">显示托盘图标</sys:String>
</ResourceDictionary>
29 changes: 27 additions & 2 deletions NetSpeedMonitor/Service/Languages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@

namespace USTC.Software.hanyizhao.NetSpeedMonitor
{
/// <summary>
/// Multi-language resources
/// </summary>
class Languages
{
public static List<OneLanguage> getLanguages()
/// <summary>
/// Get language list.
/// </summary>
/// <returns>Lnguage list</returns>
public static List<OneLanguage> GetLanguages()
{
List<OneLanguage> result = new List<OneLanguage>
{
Expand All @@ -21,6 +28,10 @@ public static List<OneLanguage> getLanguages()
return result;
}

/// <summary>
/// Get default language resource according to language of PC.
/// </summary>
/// <returns>Language resource according to language of PC. Or null if don't support the language of PC.</returns>
public static ResourceDictionary GetFromDefaultLanguage()
{
ResourceDictionary result = null;
Expand All @@ -39,6 +50,11 @@ public static ResourceDictionary GetFromDefaultLanguage()
return result;
}

/// <summary>
/// Get language resource.
/// </summary>
/// <param name="fileName">The name of language resource file.</param>
/// <returns>Language resource or null</returns>
public static ResourceDictionary GetResourceDictionary(String fileName)
{
ResourceDictionary result = null;
Expand All @@ -60,10 +76,19 @@ public static ResourceDictionary GetResourceDictionary(String fileName)
}
}

/// <summary>
/// One supported language
/// </summary>
class OneLanguage
{

/// <summary>
/// The name of language resource file.
/// </summary>
public String FileName { get { return fileName; } }

/// <summary>
/// The name used to display to user.
/// </summary>
public String ShowName { get { return showName; } }

public OneLanguage(String fileName, String showName)
Expand Down
47 changes: 41 additions & 6 deletions NetSpeedMonitor/Service/PortProcessMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static PortProcessMap GetInstance()
}

/// <summary>
/// Start or Stop mapping. (Reduce the cost of CPU)
/// Check whether this class is working.
/// </summary>
public bool Enabled
{
Expand All @@ -41,12 +41,46 @@ public bool Enabled
return timer.Enabled;
}
}
set
}

/// <summary>
/// Let this class know that the object needs this class.
/// </summary>
/// <param name="customer">the object that needs this class</param>
public void RegisterCustomer(object customer)
{
lock (timerLock)
{
lock(timerLock)
{
timer.Enabled = value;
}
Console.WriteLine("PortProessMap Add Customer");
customers.Add(customer);
CheckTimer();
}
}

/// <summary>
/// Let this class know that the object does not need this class any more.(Reduce the cost of CPU)
/// </summary>
/// <param name="customer">the object that does not need this class</param>
public void UnRegisterCustomer(object customer)
{
lock(timerLock)
{
Console.WriteLine("PortProessMap Remove Customer");
customers.Remove(customer);
CheckTimer();
}
}

private void CheckTimer()
{
Console.WriteLine("PortProcessMap CheckTimer count:" + customers.Count);
if(customers.Count == 0)
{
timer.Enabled = false;
}
else
{
timer.Enabled = true;
}
}

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

}

private HashSet<object> customers = new HashSet<object>();
private readonly object timerLock = new object();
private Timer timer;
private Dictionary<Port, int> map;
Expand Down

0 comments on commit 21a073e

Please sign in to comment.