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();
MainViewModel mainVM = new MainViewModel();
var pluginsSettings = mainVM._settings.PluginSettings;
API = new PublicAPIInstance(mainVM, mainVM._settings);
var vm = new MainViewModel();
var pluginsSettings = vm._settings.PluginSettings;
var window = new MainWindow(vm._settings, vm);
API = new PublicAPIInstance(vm._settings, vm);
PluginManager.InitializePlugins(API, pluginsSettings);
var _notifyIconManager = new NotifyIconManager(API);
var window = new MainWindow(mainVM._settings, mainVM);
RegisterExitEvents();
Current.MainWindow = window;

View File

@@ -10,11 +10,12 @@ using Wox.Core.Resource;
using Wox.Core.UserSettings;
using Wox.Helper;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger;
using Wox.ViewModel;
using ContextMenu = System.Windows.Forms.ContextMenu;
using DataFormats = System.Windows.DataFormats;
using DragEventArgs = System.Windows.DragEventArgs;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MenuItem = System.Windows.Forms.MenuItem;
using MessageBox = System.Windows.MessageBox;
namespace Wox
@@ -26,6 +27,7 @@ namespace Wox
private readonly Storyboard _progressBarStoryboard = new Storyboard();
private Settings _settings;
private NotifyIcon _notifyIcon;
#endregion
@@ -41,21 +43,33 @@ namespace Wox
}
private void OnClosing(object sender, CancelEventArgs e)
{
_notifyIcon.Visible = false;
_settings.WindowLeft = Left;
_settings.WindowTop = Top;
var vm = (MainViewModel) DataContext;
var vm = (MainViewModel)DataContext;
vm.Save();
}
private void OnLoaded(object sender, RoutedEventArgs _)
{
InitProgressbarAnimation();
WindowIntelopHelper.DisableControlBox(this);
ThemeManager.Instance.ChangeTheme(_settings.Theme);
InitializeNotifyIcon();
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.CursorMovedToEnd += (o, e) =>
{
@@ -78,11 +92,22 @@ namespace Wox
_settings.WindowTop = Top;
}
};
}
// happlebao todo delete
vm.Left = GetWindowsLeft();
vm.Top = GetWindowsTop();
vm.MainWindowVisibility = Visibility.Visible;
private void InitializeNotifyIcon()
{
_notifyIcon = new NotifyIcon { Text = Infrastructure.Wox.Name, Icon = Properties.Resources.app, Visible = true };
_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()
@@ -247,10 +272,6 @@ namespace Wox
e.Handled = true;
break;
case Key.Back:
vm.BackCommand.Execute(e);
break;
case Key.F1:
vm.StartHelpCommand.Execute(null);
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
{
private Settings _settings;
private readonly Settings _settings;
#region Constructor
public PublicAPIInstance(MainViewModel mainVM, Settings settings)
public PublicAPIInstance(Settings settings, MainViewModel mainVM)
{
MainVM = mainVM;
_settings = settings;
MainVM = mainVM;
//_settings = settings;
GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
SetHotkey(_settings.Hotkey, OnHotkey);
SetCustomPluginHotkey();
}
@@ -170,83 +170,6 @@ namespace Wox
MainVM.MainWindowVisibility = Visibility.Visible;
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
}
}

View File

@@ -5,6 +5,8 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using NHotkey;
using NHotkey.Wpf;
using Wox.Core.Plugin;
using Wox.Core.Resource;
using Wox.Core.UserSettings;
@@ -81,13 +83,16 @@ namespace Wox.ViewModel
InitializeContextMenu();
InitializeKeyCommands();
RegisterResultsUpdatedEvent();
SetHotkey(_settings.Hotkey, OnHotkey);
SetCustomPluginHotkey();
}
private void RegisterResultsUpdatedEvent()
{
foreach (var pair in PluginManager.GetPluginsForInterface<IResultUpdated>())
{
var plugin = (IResultUpdated)pair.Plugin;
var plugin = (IResultUpdated) pair.Plugin;
plugin.ResultsUpdated += (s, e) =>
{
Task.Run(() =>
@@ -226,10 +231,6 @@ namespace Wox.ViewModel
}
});
BackCommand = new RelayCommand(_ =>
{
ListeningKeyPressed?.Invoke(this, new ListeningKeyPressedEventArgs(_ as KeyEventArgs));
});
}
private void InitializeResultListBox()
@@ -272,6 +273,7 @@ namespace Wox.ViewModel
}
}
}
#endregion
#region ViewModel Properties
@@ -282,10 +284,7 @@ namespace Wox.ViewModel
public string QueryText
{
get
{
return _queryText;
}
get { return _queryText; }
set
{
_queryText = value;
@@ -304,10 +303,7 @@ namespace Wox.ViewModel
public double Left
{
get
{
return _left;
}
get { return _left; }
set
{
_left = value;
@@ -317,10 +313,7 @@ namespace Wox.ViewModel
public double Top
{
get
{
return _top;
}
get { return _top; }
set
{
_top = value;
@@ -331,10 +324,7 @@ namespace Wox.ViewModel
public Visibility ContextMenuVisibility
{
get
{
return _contextMenuVisibility;
}
get { return _contextMenuVisibility; }
set
{
_contextMenuVisibility = value;
@@ -358,10 +348,7 @@ namespace Wox.ViewModel
public Visibility ProgressBarVisibility
{
get
{
return _progressBarVisibility;
}
get { return _progressBarVisibility; }
set
{
_progressBarVisibility = value;
@@ -371,10 +358,7 @@ namespace Wox.ViewModel
public Visibility ResultListBoxVisibility
{
get
{
return _resultListBoxVisibility;
}
get { return _resultListBoxVisibility; }
set
{
_resultListBoxVisibility = value;
@@ -384,10 +368,7 @@ namespace Wox.ViewModel
public Visibility MainWindowVisibility
{
get
{
return _mainWindowVisibility;
}
get { return _mainWindowVisibility; }
set
{
_mainWindowVisibility = value;
@@ -406,7 +387,7 @@ namespace Wox.ViewModel
public ICommand StartHelpCommand { get; set; }
public ICommand LoadContextMenuCommand { get; set; }
public ICommand OpenResultCommand { get; set; }
public ICommand BackCommand { get; set; }
#endregion
#region Private Methods
@@ -525,10 +506,11 @@ namespace Wox.ViewModel
};
Task.Run(() =>
{
Results.AddResults(new List<Result> { result }, historyMetadata.ID);
Results.AddResults(new List<Result> {result}, historyMetadata.ID);
}, _updateToken);
}
}
private Result ContextMenuTopMost(Result result)
{
Result menu;
@@ -588,6 +570,88 @@ namespace Wox.ViewModel
};
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
#region Public Methods
@@ -624,7 +688,7 @@ namespace Wox.ViewModel
}
else
{
result.Score += _userSelectedRecord.GetSelectedCount(result) * 5;
result.Score += _userSelectedRecord.GetSelectedCount(result)*5;
}
}
@@ -641,29 +705,19 @@ namespace Wox.ViewModel
#endregion
public event EventHandler<ListeningKeyPressedEventArgs> ListeningKeyPressed;
public event EventHandler MainWindowVisibilityChanged;
public event EventHandler CursorMovedToEnd;
public void OnCursorMovedToEnd()
{
CursorMovedToEnd?.Invoke(this, new EventArgs());
}
public event EventHandler TextBoxSelected;
public void OnTextBoxSelected()
{
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 Include="Helper\VisibilityExtensions.cs" />
<Compile Include="Helper\SingletonWindowOpener.cs" />
<Compile Include="NotifyIconManager.cs" />
<Compile Include="PublicAPIInstance.cs" />
<Compile Include="ResultListBox.xaml.cs">
<DependentUpon>ResultListBox.xaml</DependentUpon>