fix TrayIcon not disappearing and proper app exit

This commit is contained in:
gered 2012-05-29 13:02:20 -04:00
parent cea61f8b7d
commit c34b51cff5
2 changed files with 34 additions and 19 deletions

View file

@ -18,9 +18,12 @@ public partial class App : Application
{ {
public static MainWindow Window { get; set; } public static MainWindow Window { get; set; }
private TrayIcon _trayIcon = null;
private void Application_Startup(object sender, StartupEventArgs e) private void Application_Startup(object sender, StartupEventArgs e)
{ {
TrayIcon.Show(); _trayIcon = new TrayIcon(this);
_trayIcon.Show();
// Version check updating disabled because this is a fork and it would obviously // Version check updating disabled because this is a fork and it would obviously
// be bad of us to update this forked version with the original... // be bad of us to update this forked version with the original...
@ -100,6 +103,7 @@ bool KeyboardListener_KeyDown(KeyboardListener.KeyHookEventArgs e)
private void Application_Exit(object sender, ExitEventArgs e) private void Application_Exit(object sender, ExitEventArgs e)
{ {
_trayIcon.Close();
KeyboardListener.UnRegister(); KeyboardListener.UnRegister();
AppleKeyboardHID2.Shutdown(); AppleKeyboardHID2.Shutdown();
} }

View file

@ -9,43 +9,54 @@
namespace AppleWirelessKeyboard namespace AppleWirelessKeyboard
{ {
public static class TrayIcon public class TrayIcon
{ {
static TrayIcon() private System.Windows.Application _application = null;
{ private NotifyIcon _icon = null;
NotifyIcon icon = new NotifyIcon();
icon.Text = "AppleWirelessKeyboard";
icon.Icon = new Icon(App.GetResourceStream(new Uri("pack://application:,,,/Gnome-Preferences-Desktop-Keyboard-Shortcuts.ico")).Stream);
icon.Visible = true;
MenuItem[] menuItems = new[] { public TrayIcon(System.Windows.Application application)
{
_application = application;
}
public void Show()
{
_icon = new NotifyIcon();
_icon.Text = "AppleWirelessKeyboard";
_icon.Icon = new Icon(App.GetResourceStream(new Uri("pack://application:,,,/Gnome-Preferences-Desktop-Keyboard-Shortcuts.ico")).Stream);
_icon.Visible = true;
MenuItem[] menuItems = new[] {
new MenuItem("Configure", TriggerConfigure), new MenuItem("Configure", TriggerConfigure),
new MenuItem("Restart", TriggerRestart), new MenuItem("Restart", TriggerRestart),
new MenuItem("Refresh", TriggerRefresh), new MenuItem("Refresh", TriggerRefresh),
new MenuItem("Exit", TriggerExit) new MenuItem("Exit", TriggerExit)
}; };
ContextMenu menu = new ContextMenu(menuItems); ContextMenu menu = new ContextMenu(menuItems);
icon.ContextMenu = menu; _icon.ContextMenu = menu;
} }
public static void Show() { } public void Close()
{
_icon.Visible = false;
_icon = null;
}
private static void TriggerRestart(object sender, EventArgs e) private void TriggerRestart(object sender, EventArgs e)
{ {
Application.Restart(); Application.Restart();
} }
private static void TriggerConfigure(object sender, EventArgs e) private void TriggerConfigure(object sender, EventArgs e)
{ {
(new Configuration()).Show(); (new Configuration()).Show();
} }
private static void TriggerExit(object sender, EventArgs e) private void TriggerExit(object sender, EventArgs e)
{ {
//Application.Exit(); _application.Shutdown();
Environment.Exit(0);
} }
public static void TriggerRefresh(object sender, EventArgs e) public void TriggerRefresh(object sender, EventArgs e)
{ {
if (AppleKeyboardHID2.Registered) if (AppleKeyboardHID2.Registered)
AppleKeyboardHID2.Shutdown(); AppleKeyboardHID2.Shutdown();