From 2d4d7b80c1d525ee7ab3ced74683b381bc1db222 Mon Sep 17 00:00:00 2001 From: Colin Liu Date: Fri, 12 Feb 2016 15:22:01 +0800 Subject: [PATCH] Support for Sys Tray Icon --- Wox/App.xaml.cs | 3 +- Wox/Helper/SingletonWindowOpener.cs | 2 +- Wox/MainWindow.xaml.cs | 24 +----------- Wox/NotifyIconManager.cs | 40 ++++++++++++++++++++ Wox/SettingWindow.xaml.cs | 57 +++++++++++++++++++++-------- Wox/Wox.csproj | 1 + 6 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 Wox/NotifyIconManager.cs diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 2b9ed56c32..b5847ac164 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -45,12 +45,13 @@ namespace Wox RegisterUnhandledException(); ThreadPool.QueueUserWorkItem(o => { ImageLoader.ImageLoader.PreloadImages(); }); - MainViewModel mainVM = new MainViewModel(); API = new PublicAPIInstance(mainVM); Window = new MainWindow(); Window.DataContext = mainVM; + NotifyIconManager notifyIconManager = new NotifyIconManager(API); + PluginManager.Init(API); CommandArgsFactory.Execute(e.Args.ToList()); }); diff --git a/Wox/Helper/SingletonWindowOpener.cs b/Wox/Helper/SingletonWindowOpener.cs index 2b49503981..f7786ea04b 100644 --- a/Wox/Helper/SingletonWindowOpener.cs +++ b/Wox/Helper/SingletonWindowOpener.cs @@ -10,7 +10,7 @@ namespace Wox.Helper { var window = Application.Current.Windows.OfType().FirstOrDefault(x => x.GetType() == typeof(T)) ?? (T)Activator.CreateInstance(typeof(T), args); - Application.Current.MainWindow.Hide(); + App.API.HideApp(); window.Show(); window.Focus(); diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index b74752d32f..63bc3014f6 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -24,20 +24,14 @@ namespace Wox #region Properties - private readonly Storyboard progressBarStoryboard = new Storyboard(); - private NotifyIcon notifyIcon; - - + private readonly Storyboard progressBarStoryboard = new Storyboard(); #endregion - public MainWindow() { InitializeComponent(); - InitialTray(); - //pnlResult.ItemDropEvent += pnlResult_ItemDropEvent; Closing += MainWindow_Closing; } @@ -132,22 +126,6 @@ namespace Wox progressBar.BeginStoryboard(progressBarStoryboard); } - private void InitialTray() - { - //notifyIcon = new NotifyIcon { Text = "Wox", Icon = Properties.Resources.app, Visible = true }; - //notifyIcon.Click += (o, e) => ShowWox(); - //var open = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayOpen")); - //open.Click += (o, e) => ShowWox(); - //var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings")); - //setting.Click += (o, e) => OpenSettingDialog(); - //var about = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayAbout")); - //about.Click += (o, e) => OpenSettingDialog("about"); - //var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit")); - //exit.Click += (o, e) => CloseApp(); - //MenuItem[] childen = { open, setting, about, exit }; - //notifyIcon.ContextMenu = new ContextMenu(childen); - } - private void Border_OnMouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) DragMove(); diff --git a/Wox/NotifyIconManager.cs b/Wox/NotifyIconManager.cs new file mode 100644 index 0000000000..7b16fcbd16 --- /dev/null +++ b/Wox/NotifyIconManager.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Wox.Core.Resource; +using Wox.Plugin; + +namespace Wox +{ + public class NotifyIconManager + { + + private NotifyIcon notifyIcon; + private IPublicAPI _api; + + public NotifyIconManager(IPublicAPI api) + { + this.InitialTray(); + this._api = api; + } + + private void InitialTray() + { + notifyIcon = new NotifyIcon { Text = "Wox", Icon = Properties.Resources.app, Visible = true }; + notifyIcon.Click += (o, e) => this._api.ShowApp(); + var open = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayOpen")); + open.Click += (o, e) => this._api.ShowApp(); + var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings")); + setting.Click += (o, e) => this._api.OpenSettingDialog(); + var about = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayAbout")); + about.Click += (o, e) => this._api.OpenSettingDialog("about"); + var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit")); + exit.Click += (o, e) => this._api.HideApp(); + MenuItem[] childen = { open, setting, about, exit }; + notifyIcon.ContextMenu = new ContextMenu(childen); + } + } +} diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index bd181ed0c6..173668ea13 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -19,19 +19,22 @@ using Wox.Helper; using Wox.Plugin; using Application = System.Windows.Forms.Application; using Stopwatch = Wox.Infrastructure.Stopwatch; +using Wox.Infrastructure.Hotkey; +using NHotkey.Wpf; +using NHotkey; namespace Wox { public partial class SettingWindow : Window { - public readonly MainWindow MainWindow; + public readonly IPublicAPI _api; bool settingsLoaded; private Dictionary featureControls = new Dictionary(); private bool themeTabLoaded; - public SettingWindow(MainWindow mainWindow) + public SettingWindow(IPublicAPI api) { - MainWindow = mainWindow; + this._api = api; InitializeComponent(); Loaded += Setting_Loaded; } @@ -250,23 +253,45 @@ namespace Wox { if (ctlHotkey.CurrentHotkeyAvailable) { - //MainWindow.SetHotkey(ctlHotkey.CurrentHotkey, delegate - //{ - // if (!MainWindow.IsVisible) - // { - // MainWindow.ShowApp(); - // } - // else - // { - // MainWindow.HideApp(); - // } - //}); - //MainWindow.RemoveHotkey(UserSettingStorage.Instance.Hotkey); + SetHotkey(ctlHotkey.CurrentHotkey, delegate + { + if (!App.Window.IsVisible) + { + this._api.ShowApp(); + } + else + { + this._api.HideApp(); + } + }); + RemoveHotkey(UserSettingStorage.Instance.Hotkey); UserSettingStorage.Instance.Hotkey = ctlHotkey.CurrentHotkey.ToString(); UserSettingStorage.Instance.Save(); } } + void SetHotkey(HotkeyModel hotkey, EventHandler action) + { + string hotkeyStr = hotkey.ToString(); + try + { + HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action); + } + catch (Exception) + { + string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr); + MessageBox.Show(errorMsg); + } + } + + void RemoveHotkey(string hotkeyStr) + { + if (!string.IsNullOrEmpty(hotkeyStr)) + { + HotkeyManager.Current.Remove(hotkeyStr); + } + } + private void OnHotkeyTabSelected() { ctlHotkey.HotkeyChanged += ctlHotkey_OnHotkeyChanged; @@ -289,7 +314,7 @@ namespace Wox UserSettingStorage.Instance.CustomPluginHotkeys.Remove(item); lvCustomHotkey.Items.Refresh(); UserSettingStorage.Instance.Save(); - //MainWindow.RemoveHotkey(item.Hotkey); + RemoveHotkey(item.Hotkey); } } diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index e0e17c5cf2..c9256918c9 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -126,6 +126,7 @@ +