From c34b51cff5c71062e2a9afa183dfad2b20769470 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 29 May 2012 13:02:20 -0400 Subject: [PATCH] fix TrayIcon not disappearing and proper app exit --- AppleWirelessKeyboard/App.xaml.cs | 6 +++- AppleWirelessKeyboard/TrayIcon.cs | 47 +++++++++++++++++++------------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/AppleWirelessKeyboard/App.xaml.cs b/AppleWirelessKeyboard/App.xaml.cs index e106fe5..d95437e 100644 --- a/AppleWirelessKeyboard/App.xaml.cs +++ b/AppleWirelessKeyboard/App.xaml.cs @@ -18,9 +18,12 @@ public partial class App : Application { public static MainWindow Window { get; set; } + private TrayIcon _trayIcon = null; + 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 // 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) { + _trayIcon.Close(); KeyboardListener.UnRegister(); AppleKeyboardHID2.Shutdown(); } diff --git a/AppleWirelessKeyboard/TrayIcon.cs b/AppleWirelessKeyboard/TrayIcon.cs index 1b43b15..90cb9bd 100644 --- a/AppleWirelessKeyboard/TrayIcon.cs +++ b/AppleWirelessKeyboard/TrayIcon.cs @@ -9,43 +9,54 @@ namespace AppleWirelessKeyboard { - public static class TrayIcon + public class TrayIcon { - static TrayIcon() - { - 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; + private System.Windows.Application _application = null; + private NotifyIcon _icon = null; - 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("Restart", TriggerRestart), new MenuItem("Refresh", TriggerRefresh), new MenuItem("Exit", TriggerExit) }; - ContextMenu menu = new ContextMenu(menuItems); - icon.ContextMenu = menu; - } + ContextMenu menu = new ContextMenu(menuItems); + _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(); } - private static void TriggerConfigure(object sender, EventArgs e) + private void TriggerConfigure(object sender, EventArgs e) { (new Configuration()).Show(); } - private static void TriggerExit(object sender, EventArgs e) + private void TriggerExit(object sender, EventArgs e) { - //Application.Exit(); - Environment.Exit(0); + _application.Shutdown(); } - public static void TriggerRefresh(object sender, EventArgs e) + public void TriggerRefresh(object sender, EventArgs e) { if (AppleKeyboardHID2.Registered) AppleKeyboardHID2.Shutdown();