Fix notify icon + move hotkey into MainViewModel

This commit is contained in:
bao-qian
2016-05-09 23:35:20 +01:00
parent f376e1ab01
commit 3f709cc39e
6 changed files with 144 additions and 184 deletions

View File

@@ -38,14 +38,12 @@ namespace Wox
ImageLoader.PreloadImages(); ImageLoader.PreloadImages();
MainViewModel mainVM = new MainViewModel(); var vm = new MainViewModel();
var pluginsSettings = mainVM._settings.PluginSettings; var pluginsSettings = vm._settings.PluginSettings;
API = new PublicAPIInstance(mainVM, mainVM._settings); var window = new MainWindow(vm._settings, vm);
API = new PublicAPIInstance(vm._settings, vm);
PluginManager.InitializePlugins(API, pluginsSettings); PluginManager.InitializePlugins(API, pluginsSettings);
var _notifyIconManager = new NotifyIconManager(API);
var window = new MainWindow(mainVM._settings, mainVM);
RegisterExitEvents(); RegisterExitEvents();
Current.MainWindow = window; Current.MainWindow = window;

View File

@@ -10,11 +10,12 @@ using Wox.Core.Resource;
using Wox.Core.UserSettings; using Wox.Core.UserSettings;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure.Hotkey; using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger;
using Wox.ViewModel; using Wox.ViewModel;
using ContextMenu = System.Windows.Forms.ContextMenu;
using DataFormats = System.Windows.DataFormats; using DataFormats = System.Windows.DataFormats;
using DragEventArgs = System.Windows.DragEventArgs; using DragEventArgs = System.Windows.DragEventArgs;
using KeyEventArgs = System.Windows.Input.KeyEventArgs; using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MenuItem = System.Windows.Forms.MenuItem;
using MessageBox = System.Windows.MessageBox; using MessageBox = System.Windows.MessageBox;
namespace Wox namespace Wox
@@ -26,6 +27,7 @@ namespace Wox
private readonly Storyboard _progressBarStoryboard = new Storyboard(); private readonly Storyboard _progressBarStoryboard = new Storyboard();
private Settings _settings; private Settings _settings;
private NotifyIcon _notifyIcon;
#endregion #endregion
@@ -41,21 +43,33 @@ namespace Wox
} }
private void OnClosing(object sender, CancelEventArgs e) private void OnClosing(object sender, CancelEventArgs e)
{ {
_notifyIcon.Visible = false;
_settings.WindowLeft = Left; _settings.WindowLeft = Left;
_settings.WindowTop = Top; _settings.WindowTop = Top;
var vm = (MainViewModel) DataContext; var vm = (MainViewModel)DataContext;
vm.Save(); vm.Save();
} }
private void OnLoaded(object sender, RoutedEventArgs _) private void OnLoaded(object sender, RoutedEventArgs _)
{ {
InitProgressbarAnimation(); InitProgressbarAnimation();
WindowIntelopHelper.DisableControlBox(this); WindowIntelopHelper.DisableControlBox(this);
ThemeManager.Instance.ChangeTheme(_settings.Theme); ThemeManager.Instance.ChangeTheme(_settings.Theme);
InitializeNotifyIcon();
var vm = (MainViewModel)DataContext; var vm = (MainViewModel)DataContext;
RegisterEvents(vm);
// happlebao todo delete
vm.Left = GetWindowsLeft();
vm.Top = GetWindowsTop();
vm.MainWindowVisibility = Visibility.Visible;
}
private void RegisterEvents(MainViewModel vm)
{
vm.TextBoxSelected += (o, e) => QueryTextBox.SelectAll(); vm.TextBoxSelected += (o, e) => QueryTextBox.SelectAll();
vm.CursorMovedToEnd += (o, e) => vm.CursorMovedToEnd += (o, e) =>
{ {
@@ -78,11 +92,22 @@ namespace Wox
_settings.WindowTop = Top; _settings.WindowTop = Top;
} }
}; };
}
// happlebao todo delete private void InitializeNotifyIcon()
vm.Left = GetWindowsLeft(); {
vm.Top = GetWindowsTop(); _notifyIcon = new NotifyIcon { Text = Infrastructure.Wox.Name, Icon = Properties.Resources.app, Visible = true };
vm.MainWindowVisibility = Visibility.Visible; _notifyIcon.Click += (o, e) => App.API.ShowApp();
var open = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayOpen"));
open.Click += (o, e) => App.API.ShowApp();
var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings"));
setting.Click += (o, e) => App.API.OpenSettingDialog();
var about = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayAbout"));
about.Click += (o, e) => App.API.OpenSettingDialog("about");
var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit"));
exit.Click += (o, e) => Close();
MenuItem[] childen = { open, setting, about, exit };
_notifyIcon.ContextMenu = new ContextMenu(childen);
} }
private double GetWindowsLeft() private double GetWindowsLeft()
@@ -247,10 +272,6 @@ namespace Wox
e.Handled = true; e.Handled = true;
break; break;
case Key.Back:
vm.BackCommand.Execute(e);
break;
case Key.F1: case Key.F1:
vm.StartHelpCommand.Execute(null); vm.StartHelpCommand.Execute(null);
break; break;

View File

@@ -1,35 +0,0 @@
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)
{
InitialTray();
_api = api;
}
private void InitialTray()
{
notifyIcon = new NotifyIcon { Text = "Wox", Icon = Properties.Resources.app, Visible = true };
notifyIcon.Click += (o, e) => _api.ShowApp();
var open = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayOpen"));
open.Click += (o, e) => _api.ShowApp();
var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings"));
setting.Click += (o, e) => _api.OpenSettingDialog();
var about = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayAbout"));
about.Click += (o, e) => _api.OpenSettingDialog("about");
var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit"));
exit.Click += (o, e) => _api.CloseApp();
MenuItem[] childen = { open, setting, about, exit };
notifyIcon.ContextMenu = new ContextMenu(childen);
}
}
}

View File

@@ -20,17 +20,17 @@ namespace Wox
{ {
public class PublicAPIInstance : IPublicAPI public class PublicAPIInstance : IPublicAPI
{ {
private Settings _settings; private readonly Settings _settings;
#region Constructor #region Constructor
public PublicAPIInstance(MainViewModel mainVM, Settings settings) public PublicAPIInstance(Settings settings, MainViewModel mainVM)
{ {
MainVM = mainVM;
_settings = settings; _settings = settings;
MainVM = mainVM;
//_settings = settings;
GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback; GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
WebRequest.RegisterPrefix("data", new DataWebRequestFactory()); WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
SetHotkey(_settings.Hotkey, OnHotkey);
SetCustomPluginHotkey();
} }
@@ -170,83 +170,6 @@ namespace Wox
MainVM.MainWindowVisibility = Visibility.Visible; MainVM.MainWindowVisibility = Visibility.Visible;
MainVM.OnTextBoxSelected(); MainVM.OnTextBoxSelected();
} }
internal void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
{
var hotkey = new HotkeyModel(hotkeyStr);
SetHotkey(hotkey, action);
}
public 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);
}
}
public void RemoveHotkey(string hotkeyStr)
{
if (!string.IsNullOrEmpty(hotkeyStr))
{
HotkeyManager.Current.Remove(hotkeyStr);
}
}
/// <summary>
/// Checks if Wox should ignore any hotkeys
/// </summary>
/// <returns></returns>
private bool ShouldIgnoreHotkeys()
{
//double if to omit calling win32 function
if (_settings.IgnoreHotkeysOnFullscreen)
if (WindowIntelopHelper.IsWindowFullscreen())
return true;
return false;
}
internal void SetCustomPluginHotkey()
{
if (_settings.CustomPluginHotkeys == null) return;
foreach (CustomPluginHotkey hotkey in _settings.CustomPluginHotkeys)
{
CustomPluginHotkey hotkey1 = hotkey;
SetHotkey(hotkey.Hotkey, delegate
{
if (ShouldIgnoreHotkeys()) return;
ShowApp();
ChangeQuery(hotkey1.ActionKeyword, true);
});
}
}
protected internal void OnHotkey(object sender, HotkeyEventArgs e)
{
if (ShouldIgnoreHotkeys()) return;
ToggleWox();
e.Handled = true;
}
private void ToggleWox()
{
if (!MainVM.MainWindowVisibility.IsVisible())
{
ShowWox();
}
else
{
HideWox();
}
}
#endregion #endregion
} }
} }

View File

@@ -5,6 +5,8 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using NHotkey;
using NHotkey.Wpf;
using Wox.Core.Plugin; using Wox.Core.Plugin;
using Wox.Core.Resource; using Wox.Core.Resource;
using Wox.Core.UserSettings; using Wox.Core.UserSettings;
@@ -81,13 +83,16 @@ namespace Wox.ViewModel
InitializeContextMenu(); InitializeContextMenu();
InitializeKeyCommands(); InitializeKeyCommands();
RegisterResultsUpdatedEvent(); RegisterResultsUpdatedEvent();
SetHotkey(_settings.Hotkey, OnHotkey);
SetCustomPluginHotkey();
} }
private void RegisterResultsUpdatedEvent() private void RegisterResultsUpdatedEvent()
{ {
foreach (var pair in PluginManager.GetPluginsForInterface<IResultUpdated>()) foreach (var pair in PluginManager.GetPluginsForInterface<IResultUpdated>())
{ {
var plugin = (IResultUpdated)pair.Plugin; var plugin = (IResultUpdated) pair.Plugin;
plugin.ResultsUpdated += (s, e) => plugin.ResultsUpdated += (s, e) =>
{ {
Task.Run(() => Task.Run(() =>
@@ -226,10 +231,6 @@ namespace Wox.ViewModel
} }
}); });
BackCommand = new RelayCommand(_ =>
{
ListeningKeyPressed?.Invoke(this, new ListeningKeyPressedEventArgs(_ as KeyEventArgs));
});
} }
private void InitializeResultListBox() private void InitializeResultListBox()
@@ -272,6 +273,7 @@ namespace Wox.ViewModel
} }
} }
} }
#endregion #endregion
#region ViewModel Properties #region ViewModel Properties
@@ -282,10 +284,7 @@ namespace Wox.ViewModel
public string QueryText public string QueryText
{ {
get get { return _queryText; }
{
return _queryText;
}
set set
{ {
_queryText = value; _queryText = value;
@@ -304,10 +303,7 @@ namespace Wox.ViewModel
public double Left public double Left
{ {
get get { return _left; }
{
return _left;
}
set set
{ {
_left = value; _left = value;
@@ -317,10 +313,7 @@ namespace Wox.ViewModel
public double Top public double Top
{ {
get get { return _top; }
{
return _top;
}
set set
{ {
_top = value; _top = value;
@@ -331,10 +324,7 @@ namespace Wox.ViewModel
public Visibility ContextMenuVisibility public Visibility ContextMenuVisibility
{ {
get get { return _contextMenuVisibility; }
{
return _contextMenuVisibility;
}
set set
{ {
_contextMenuVisibility = value; _contextMenuVisibility = value;
@@ -358,10 +348,7 @@ namespace Wox.ViewModel
public Visibility ProgressBarVisibility public Visibility ProgressBarVisibility
{ {
get get { return _progressBarVisibility; }
{
return _progressBarVisibility;
}
set set
{ {
_progressBarVisibility = value; _progressBarVisibility = value;
@@ -371,10 +358,7 @@ namespace Wox.ViewModel
public Visibility ResultListBoxVisibility public Visibility ResultListBoxVisibility
{ {
get get { return _resultListBoxVisibility; }
{
return _resultListBoxVisibility;
}
set set
{ {
_resultListBoxVisibility = value; _resultListBoxVisibility = value;
@@ -384,10 +368,7 @@ namespace Wox.ViewModel
public Visibility MainWindowVisibility public Visibility MainWindowVisibility
{ {
get get { return _mainWindowVisibility; }
{
return _mainWindowVisibility;
}
set set
{ {
_mainWindowVisibility = value; _mainWindowVisibility = value;
@@ -406,7 +387,7 @@ namespace Wox.ViewModel
public ICommand StartHelpCommand { get; set; } public ICommand StartHelpCommand { get; set; }
public ICommand LoadContextMenuCommand { get; set; } public ICommand LoadContextMenuCommand { get; set; }
public ICommand OpenResultCommand { get; set; } public ICommand OpenResultCommand { get; set; }
public ICommand BackCommand { get; set; }
#endregion #endregion
#region Private Methods #region Private Methods
@@ -525,10 +506,11 @@ namespace Wox.ViewModel
}; };
Task.Run(() => Task.Run(() =>
{ {
Results.AddResults(new List<Result> { result }, historyMetadata.ID); Results.AddResults(new List<Result> {result}, historyMetadata.ID);
}, _updateToken); }, _updateToken);
} }
} }
private Result ContextMenuTopMost(Result result) private Result ContextMenuTopMost(Result result)
{ {
Result menu; Result menu;
@@ -588,6 +570,88 @@ namespace Wox.ViewModel
}; };
return menu; return menu;
} }
#endregion
#region Hotkey
internal void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
{
var hotkey = new HotkeyModel(hotkeyStr);
SetHotkey(hotkey, action);
}
public 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);
}
}
public void RemoveHotkey(string hotkeyStr)
{
if (!string.IsNullOrEmpty(hotkeyStr))
{
HotkeyManager.Current.Remove(hotkeyStr);
}
}
/// <summary>
/// Checks if Wox should ignore any hotkeys
/// </summary>
/// <returns></returns>
private bool ShouldIgnoreHotkeys()
{
//double if to omit calling win32 function
if (_settings.IgnoreHotkeysOnFullscreen)
if (WindowIntelopHelper.IsWindowFullscreen())
return true;
return false;
}
private void SetCustomPluginHotkey()
{
if (_settings.CustomPluginHotkeys == null) return;
foreach (CustomPluginHotkey hotkey in _settings.CustomPluginHotkeys)
{
CustomPluginHotkey hotkey1 = hotkey;
SetHotkey(hotkey.Hotkey, delegate
{
if (ShouldIgnoreHotkeys()) return;
App.API.ShowApp();
App.API.ChangeQuery(hotkey1.ActionKeyword, true);
});
}
}
private void OnHotkey(object sender, HotkeyEventArgs e)
{
if (ShouldIgnoreHotkeys()) return;
ToggleWox();
e.Handled = true;
}
private void ToggleWox()
{
if (!MainWindowVisibility.IsVisible())
{
MainWindowVisibility = Visibility.Visible;
OnTextBoxSelected();
}
else
{
MainWindowVisibility = Visibility.Collapsed;
}
}
#endregion #endregion
#region Public Methods #region Public Methods
@@ -624,7 +688,7 @@ namespace Wox.ViewModel
} }
else else
{ {
result.Score += _userSelectedRecord.GetSelectedCount(result) * 5; result.Score += _userSelectedRecord.GetSelectedCount(result)*5;
} }
} }
@@ -641,29 +705,19 @@ namespace Wox.ViewModel
#endregion #endregion
public event EventHandler<ListeningKeyPressedEventArgs> ListeningKeyPressed;
public event EventHandler MainWindowVisibilityChanged; public event EventHandler MainWindowVisibilityChanged;
public event EventHandler CursorMovedToEnd; public event EventHandler CursorMovedToEnd;
public void OnCursorMovedToEnd() public void OnCursorMovedToEnd()
{ {
CursorMovedToEnd?.Invoke(this, new EventArgs()); CursorMovedToEnd?.Invoke(this, new EventArgs());
} }
public event EventHandler TextBoxSelected; public event EventHandler TextBoxSelected;
public void OnTextBoxSelected() public void OnTextBoxSelected()
{ {
TextBoxSelected?.Invoke(this, new EventArgs()); TextBoxSelected?.Invoke(this, new EventArgs());
} }
} }
}
public class ListeningKeyPressedEventArgs : EventArgs
{
public KeyEventArgs KeyEventArgs { get; private set; }
public ListeningKeyPressedEventArgs(KeyEventArgs keyEventArgs)
{
KeyEventArgs = keyEventArgs;
}
}
}

View File

@@ -159,7 +159,6 @@
</Compile> </Compile>
<Compile Include="Helper\VisibilityExtensions.cs" /> <Compile Include="Helper\VisibilityExtensions.cs" />
<Compile Include="Helper\SingletonWindowOpener.cs" /> <Compile Include="Helper\SingletonWindowOpener.cs" />
<Compile Include="NotifyIconManager.cs" />
<Compile Include="PublicAPIInstance.cs" /> <Compile Include="PublicAPIInstance.cs" />
<Compile Include="ResultListBox.xaml.cs"> <Compile Include="ResultListBox.xaml.cs">
<DependentUpon>ResultListBox.xaml</DependentUpon> <DependentUpon>ResultListBox.xaml</DependentUpon>