Support for Sys Tray Icon

This commit is contained in:
Colin Liu 2016-02-12 15:22:01 +08:00
parent df85543337
commit 2d4d7b80c1
6 changed files with 86 additions and 41 deletions

View File

@ -45,12 +45,13 @@ namespace Wox
RegisterUnhandledException(); RegisterUnhandledException();
ThreadPool.QueueUserWorkItem(o => { ImageLoader.ImageLoader.PreloadImages(); }); ThreadPool.QueueUserWorkItem(o => { ImageLoader.ImageLoader.PreloadImages(); });
MainViewModel mainVM = new MainViewModel(); MainViewModel mainVM = new MainViewModel();
API = new PublicAPIInstance(mainVM); API = new PublicAPIInstance(mainVM);
Window = new MainWindow(); Window = new MainWindow();
Window.DataContext = mainVM; Window.DataContext = mainVM;
NotifyIconManager notifyIconManager = new NotifyIconManager(API);
PluginManager.Init(API); PluginManager.Init(API);
CommandArgsFactory.Execute(e.Args.ToList()); CommandArgsFactory.Execute(e.Args.ToList());
}); });

View File

@ -10,7 +10,7 @@ namespace Wox.Helper
{ {
var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.GetType() == typeof(T)) var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.GetType() == typeof(T))
?? (T)Activator.CreateInstance(typeof(T), args); ?? (T)Activator.CreateInstance(typeof(T), args);
Application.Current.MainWindow.Hide(); App.API.HideApp();
window.Show(); window.Show();
window.Focus(); window.Focus();

View File

@ -24,20 +24,14 @@ namespace Wox
#region Properties #region Properties
private readonly Storyboard progressBarStoryboard = new Storyboard(); private readonly Storyboard progressBarStoryboard = new Storyboard();
private NotifyIcon notifyIcon;
#endregion #endregion
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
InitialTray();
//pnlResult.ItemDropEvent += pnlResult_ItemDropEvent; //pnlResult.ItemDropEvent += pnlResult_ItemDropEvent;
Closing += MainWindow_Closing; Closing += MainWindow_Closing;
} }
@ -132,22 +126,6 @@ namespace Wox
progressBar.BeginStoryboard(progressBarStoryboard); 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) private void Border_OnMouseDown(object sender, MouseButtonEventArgs e)
{ {
if (e.ChangedButton == MouseButton.Left) DragMove(); if (e.ChangedButton == MouseButton.Left) DragMove();

40
Wox/NotifyIconManager.cs Normal file
View File

@ -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);
}
}
}

View File

@ -19,19 +19,22 @@ using Wox.Helper;
using Wox.Plugin; using Wox.Plugin;
using Application = System.Windows.Forms.Application; using Application = System.Windows.Forms.Application;
using Stopwatch = Wox.Infrastructure.Stopwatch; using Stopwatch = Wox.Infrastructure.Stopwatch;
using Wox.Infrastructure.Hotkey;
using NHotkey.Wpf;
using NHotkey;
namespace Wox namespace Wox
{ {
public partial class SettingWindow : Window public partial class SettingWindow : Window
{ {
public readonly MainWindow MainWindow; public readonly IPublicAPI _api;
bool settingsLoaded; bool settingsLoaded;
private Dictionary<ISettingProvider, Control> featureControls = new Dictionary<ISettingProvider, Control>(); private Dictionary<ISettingProvider, Control> featureControls = new Dictionary<ISettingProvider, Control>();
private bool themeTabLoaded; private bool themeTabLoaded;
public SettingWindow(MainWindow mainWindow) public SettingWindow(IPublicAPI api)
{ {
MainWindow = mainWindow; this._api = api;
InitializeComponent(); InitializeComponent();
Loaded += Setting_Loaded; Loaded += Setting_Loaded;
} }
@ -250,23 +253,45 @@ namespace Wox
{ {
if (ctlHotkey.CurrentHotkeyAvailable) if (ctlHotkey.CurrentHotkeyAvailable)
{ {
//MainWindow.SetHotkey(ctlHotkey.CurrentHotkey, delegate SetHotkey(ctlHotkey.CurrentHotkey, delegate
//{ {
// if (!MainWindow.IsVisible) if (!App.Window.IsVisible)
// { {
// MainWindow.ShowApp(); this._api.ShowApp();
// } }
// else else
// { {
// MainWindow.HideApp(); this._api.HideApp();
// } }
//}); });
//MainWindow.RemoveHotkey(UserSettingStorage.Instance.Hotkey); RemoveHotkey(UserSettingStorage.Instance.Hotkey);
UserSettingStorage.Instance.Hotkey = ctlHotkey.CurrentHotkey.ToString(); UserSettingStorage.Instance.Hotkey = ctlHotkey.CurrentHotkey.ToString();
UserSettingStorage.Instance.Save(); UserSettingStorage.Instance.Save();
} }
} }
void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> 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() private void OnHotkeyTabSelected()
{ {
ctlHotkey.HotkeyChanged += ctlHotkey_OnHotkeyChanged; ctlHotkey.HotkeyChanged += ctlHotkey_OnHotkeyChanged;
@ -289,7 +314,7 @@ namespace Wox
UserSettingStorage.Instance.CustomPluginHotkeys.Remove(item); UserSettingStorage.Instance.CustomPluginHotkeys.Remove(item);
lvCustomHotkey.Items.Refresh(); lvCustomHotkey.Items.Refresh();
UserSettingStorage.Instance.Save(); UserSettingStorage.Instance.Save();
//MainWindow.RemoveHotkey(item.Hotkey); RemoveHotkey(item.Hotkey);
} }
} }

View File

@ -126,6 +126,7 @@
<Compile Include="Converters\VisibilityConverter.cs" /> <Compile Include="Converters\VisibilityConverter.cs" />
<Compile Include="Helper\SingletonWindowOpener.cs" /> <Compile Include="Helper\SingletonWindowOpener.cs" />
<Compile Include="ImageLoader\ImageCacheStroage.cs" /> <Compile Include="ImageLoader\ImageCacheStroage.cs" />
<Compile Include="NotifyIconManager.cs" />
<Compile Include="PublicAPIInstance.cs" /> <Compile Include="PublicAPIInstance.cs" />
<Compile Include="Storage\QueryHistoryStorage.cs" /> <Compile Include="Storage\QueryHistoryStorage.cs" />
<Compile Include="Storage\TopMostRecordStorage.cs" /> <Compile Include="Storage\TopMostRecordStorage.cs" />