From 3f709cc39e8c1533d8b981d882da42535004bf6c Mon Sep 17 00:00:00 2001 From: bao-qian Date: Mon, 9 May 2016 23:35:20 +0100 Subject: [PATCH] Fix notify icon + move hotkey into MainViewModel --- Wox/App.xaml.cs | 10 +-- Wox/MainWindow.xaml.cs | 43 +++++++--- Wox/NotifyIconManager.cs | 35 -------- Wox/PublicAPIInstance.cs | 87 ++----------------- Wox/ViewModel/MainViewModel.cs | 152 ++++++++++++++++++++++----------- Wox/Wox.csproj | 1 - 6 files changed, 144 insertions(+), 184 deletions(-) delete mode 100644 Wox/NotifyIconManager.cs diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 4609198f7f..c96f3bddcb 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -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; diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 8e52f586d4..0d8c061e3b 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -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; diff --git a/Wox/NotifyIconManager.cs b/Wox/NotifyIconManager.cs deleted file mode 100644 index bba0663fd1..0000000000 --- a/Wox/NotifyIconManager.cs +++ /dev/null @@ -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); - } - } -} diff --git a/Wox/PublicAPIInstance.cs b/Wox/PublicAPIInstance.cs index 448a0dbbae..2078a27ffe 100644 --- a/Wox/PublicAPIInstance.cs +++ b/Wox/PublicAPIInstance.cs @@ -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 action) - { - var hotkey = new HotkeyModel(hotkeyStr); - SetHotkey(hotkey, action); - } - - public 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); - } - } - - public void RemoveHotkey(string hotkeyStr) - { - if (!string.IsNullOrEmpty(hotkeyStr)) - { - HotkeyManager.Current.Remove(hotkeyStr); - } - } - - /// - /// Checks if Wox should ignore any hotkeys - /// - /// - 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 } } diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index 3f26abbb21..3f180cc8c7 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -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()) { - 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 }, historyMetadata.ID); + Results.AddResults(new List {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 action) + { + var hotkey = new HotkeyModel(hotkeyStr); + SetHotkey(hotkey, action); + } + + public 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); + } + } + + public void RemoveHotkey(string hotkeyStr) + { + if (!string.IsNullOrEmpty(hotkeyStr)) + { + HotkeyManager.Current.Remove(hotkeyStr); + } + } + + /// + /// Checks if Wox should ignore any hotkeys + /// + /// + 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 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; - } - } -} +} \ No newline at end of file diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index 8be8294cd7..56af733143 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -159,7 +159,6 @@ - ResultListBox.xaml