From ca3bedc9a1d5b37dc22a61937e56f2bccdc854df Mon Sep 17 00:00:00 2001 From: Colin Liu Date: Fri, 12 Feb 2016 17:20:46 +0800 Subject: [PATCH] Fix dependency bugs --- Wox/CommandArgs/ToggleCommandArg.cs | 10 +- Wox/MainWindow.xaml | 2 + Wox/MainWindow.xaml.cs | 60 ++++++++---- Wox/NotifyIconManager.cs | 2 +- Wox/PublicAPIInstance.cs | 60 ++++-------- Wox/ResultPanel.xaml.cs | 55 +---------- Wox/SettingWindow.xaml.cs | 2 + Wox/ViewModel/MainViewModel.cs | 138 ++++++++++++++++++++++----- Wox/ViewModel/ResultItemViewModel.cs | 1 - 9 files changed, 191 insertions(+), 139 deletions(-) diff --git a/Wox/CommandArgs/ToggleCommandArg.cs b/Wox/CommandArgs/ToggleCommandArg.cs index 7c2cbefb12..e509316291 100644 --- a/Wox/CommandArgs/ToggleCommandArg.cs +++ b/Wox/CommandArgs/ToggleCommandArg.cs @@ -11,8 +11,14 @@ namespace Wox.CommandArgs public void Execute(IList args) { - //TODO: Add ToggleWox Method - //App.API.ToggleWox(); + if (App.Window.IsVisible) + { + App.API.HideApp(); + } + else + { + App.API.ShowApp(); + } } } } diff --git a/Wox/MainWindow.xaml b/Wox/MainWindow.xaml index 9299032397..554ff7499e 100644 --- a/Wox/MainWindow.xaml +++ b/Wox/MainWindow.xaml @@ -48,6 +48,8 @@ + + diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 63bc3014f6..a15082f1b5 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -64,12 +64,46 @@ namespace Wox ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme); InternationalizationManager.Instance.ChangeLanguage(UserSettingStorage.Instance.Language); - Left = GetWindowsLeft(); - Top = GetWindowsTop(); - InitProgressbarAnimation(); WindowIntelopHelper.DisableControlBox(this); CheckUpdate(); + + var vm = this.DataContext as MainViewModel; + vm.PropertyChanged += (o, eve) => + { + if(eve.PropertyName == "SelectAllText") + { + if (vm.SelectAllText) + { + this.tbQuery.SelectAll(); + } + } + else if(eve.PropertyName == "CaretIndex") + { + this.tbQuery.CaretIndex = vm.CaretIndex; + } + else if(eve.PropertyName == "Left") + { + this.Left = vm.Left; + } + else if(eve.PropertyName == "Top") + { + this.Top = vm.Top; + } + else if(eve.PropertyName == "IsVisible") + { + if (vm.IsVisible) + { + this.tbQuery.Focus(); + } + } + }; + + vm.Left = GetWindowsLeft(); + vm.Top = GetWindowsTop(); + this.Activate(); + this.Focus(); + this.tbQuery.Focus(); } private double GetWindowsLeft() @@ -135,14 +169,13 @@ namespace Wox { if (UserSettingStorage.Instance.HideWhenDeactive) { - //TODO:Hide the window when deactivated - //HideWox(); + App.API.HideApp(); } } private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { - //The code here is to supress the conflict of Window.InputBinding and ListBox native Down/Up Key handle + //The code here is to supress the conflict of Window.InputBinding and ListBox/TextBox native Key handle var vm = this.DataContext as MainViewModel; //when alt is pressed, the real key should be e.SystemKey Key key = (e.Key == Key.System ? e.SystemKey : e.Key); @@ -179,21 +212,12 @@ namespace Wox vm.SelectPrevPageCommand.Execute(null); e.Handled = true; break; + case Key.Back: + vm.BackCommand.Execute(e); + break; } } - //TODO: Colin - Figure out how to raise BackKeyDownEvent? - // case Key.Back: - // if (BackKeyDownEvent != null) - // { - // BackKeyDownEvent(new WoxKeyDownEventArgs - // { - // Query = tbQuery.Text, - // keyEventArgs = e - // }); - // } - // break; - private void MainWindow_OnDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) diff --git a/Wox/NotifyIconManager.cs b/Wox/NotifyIconManager.cs index 7b16fcbd16..28ab61ad86 100644 --- a/Wox/NotifyIconManager.cs +++ b/Wox/NotifyIconManager.cs @@ -32,7 +32,7 @@ namespace Wox 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(); + exit.Click += (o, e) => this._api.CloseApp(); MenuItem[] childen = { open, setting, about, exit }; notifyIcon.ContextMenu = new ContextMenu(childen); } diff --git a/Wox/PublicAPIInstance.cs b/Wox/PublicAPIInstance.cs index f3170050e9..7cb7f296f4 100644 --- a/Wox/PublicAPIInstance.cs +++ b/Wox/PublicAPIInstance.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; +using System.Windows.Input; using System.Windows.Threading; using Wox.Core.Plugin; using Wox.Core.Resource; @@ -36,6 +37,22 @@ namespace Wox SetHotkey(UserSettingStorage.Instance.Hotkey, OnHotkey); SetCustomPluginHotkey(); + + this.MainVM.ListeningKeyPressed += (o, e) => { + + if(e.KeyEventArgs.Key == Key.Back) + { + if (null != this.BackKeyDownEvent) + { + BackKeyDownEvent(new WoxKeyDownEventArgs + { + Query = this.MainVM.QueryText, + keyEventArgs = e.KeyEventArgs + }); + } + } + + }; } #endregion @@ -55,33 +72,14 @@ namespace Wox public void ChangeQuery(string query, bool requery = false) { this.MainVM.QueryText = query; + this.MainVM.CaretIndex = this.MainVM.QueryText.Length; - //TODO: Colin - Adjust CaretIndext - //Application.Current.Dispatcher.Invoke(() => - //{ - - // tbQuery.CaretIndex = this.MainVM.QueryText.Length; - // if (requery) - // { - // TbQuery_OnTextChanged(null, null); - // } - //}); } public void ChangeQueryText(string query, bool selectAll = false) { this.MainVM.QueryText = query; - - //TODO: Colin - Select all text - //Application.Current.Dispatcher.Invoke(() => - //{ - - // tbQuery.CaretIndex = tbQuery.Text.Length; - // if (selectAll) - // { - // tbQuery.SelectAll(); - // } - //}); + this.MainVM.SelectAllText = true; } public void CloseApp() @@ -183,13 +181,7 @@ namespace Wox o.PluginID = plugin.ID; }); - this.MainVM.ActionPanel.Clear(); - - //TODO:Show Action Panel accordingly - //pnlContextMenu.Clear(); - //pnlContextMenu.AddResults(results, plugin.ID); - //pnlContextMenu.Visibility = Visibility.Visible; - //pnlResult.Visibility = Visibility.Collapsed; + this.MainVM.ShowActionPanel(results, plugin.ID); } } @@ -217,17 +209,7 @@ namespace Wox { UserSettingStorage.Instance.IncreaseActivateTimes(); this.MainVM.IsVisible = true; - - //TODO: Colin - Adjust window properties - //Left = GetWindowsLeft(); - //Top = GetWindowsTop(); - - //Show(); - //Activate(); - //Focus(); - //tbQuery.Focus(); - //ResetQueryHistoryIndex(); - //if (selectAll) tbQuery.SelectAll(); + this.MainVM.SelectAllText = true; } public void SetHotkey(string hotkeyStr, EventHandler action) diff --git a/Wox/ResultPanel.xaml.cs b/Wox/ResultPanel.xaml.cs index d20d0390c2..d6640467f5 100644 --- a/Wox/ResultPanel.xaml.cs +++ b/Wox/ResultPanel.xaml.cs @@ -17,62 +17,13 @@ namespace Wox [Synchronization] public partial class ResultPanel : UserControl { + //TODO: Refactor this event public event Action ItemDropEvent; public void AddResults(List newResults, string resultId) { - //lock (_resultsUpdateLock) - //{ - // // todo use async to do new result calculation - // var resultsCopy = _results.ToList(); - // var oldResults = resultsCopy.Where(r => r.PluginID == resultId).ToList(); - // // intersection of A (old results) and B (new newResults) - // var intersection = oldResults.Intersect(newResults).ToList(); - // // remove result of relative complement of B in A - // foreach (var result in oldResults.Except(intersection)) - // { - // resultsCopy.Remove(result); - // } - - // // update scores - // foreach (var result in newResults) - // { - // if (IsTopMostResult(result)) - // { - // result.Score = int.MaxValue; - // } - // } - - // // update index for result in intersection of A and B - // foreach (var commonResult in intersection) - // { - // int oldIndex = resultsCopy.IndexOf(commonResult); - // int oldScore = resultsCopy[oldIndex].Score; - // int newScore = newResults[newResults.IndexOf(commonResult)].Score; - // if (newScore != oldScore) - // { - // var oldResult = resultsCopy[oldIndex]; - // oldResult.Score = newScore; - // resultsCopy.RemoveAt(oldIndex); - // int newIndex = InsertIndexOf(newScore, resultsCopy); - // resultsCopy.Insert(newIndex, oldResult); - - // } - // } - - // // insert result in relative complement of A in B - // foreach (var result in newResults.Except(intersection)) - // { - // int newIndex = InsertIndexOf(result.Score, resultsCopy); - // resultsCopy.Insert(newIndex, result); - // } - - // // update UI in one run, so it can avoid UI flickering - // _results.Update(resultsCopy); - - // lbResults.Margin = lbResults.Items.Count > 0 ? new Thickness { Top = 8 } : new Thickness { Top = 0 }; - // SelectFirst(); - //} + var vm = this.DataContext as ResultPanelViewModel; + vm.AddResults(newResults, resultId); } diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index 173668ea13..f47bbbde3b 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -22,6 +22,7 @@ using Stopwatch = Wox.Infrastructure.Stopwatch; using Wox.Infrastructure.Hotkey; using NHotkey.Wpf; using NHotkey; +using Wox.ViewModel; namespace Wox { @@ -36,6 +37,7 @@ namespace Wox { this._api = api; InitializeComponent(); + this.resultPanelPreview.DataContext = new ResultPanelViewModel(); Loaded += Setting_Loaded; } diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index 6cd9ad1307..25f085b010 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -5,9 +5,11 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; +using System.Windows.Forms; using System.Windows.Input; using Wox.Core.Plugin; using Wox.Core.Resource; +using Wox.Core.UserSettings; using Wox.Infrastructure; using Wox.Infrastructure.Hotkey; using Wox.Plugin; @@ -27,6 +29,8 @@ namespace Wox.ViewModel private bool _isActionPanelVisible; private bool _isProgressBarVisible; private bool _isProgressBarTooltipVisible; + private bool _selectAllText; + private int _caretIndex; private double _left; private double _top; @@ -84,6 +88,32 @@ namespace Wox.ViewModel } } + public bool SelectAllText + { + get + { + return this._selectAllText; + } + set + { + this._selectAllText = value; + OnPropertyChanged("SelectAllText"); + } + } + + public int CaretIndex + { + get + { + return this._caretIndex; + } + set + { + this._caretIndex = value; + OnPropertyChanged("CaretIndex"); + } + } + public bool IsVisible { get @@ -246,13 +276,20 @@ namespace Wox.ViewModel set; } + public ICommand BackCommand + { + get; + set; + } + #endregion #region Private Methods private void InitializeKeyCommands() { - this.EscCommand = new RelayCommand((parameter) => { + this.EscCommand = new RelayCommand((parameter) => + { if (this.IsActionPanelVisible) { @@ -265,7 +302,8 @@ namespace Wox.ViewModel }); - this.SelectNextItemCommand = new RelayCommand((parameter) => { + this.SelectNextItemCommand = new RelayCommand((parameter) => + { if (this.IsActionPanelVisible) { @@ -278,7 +316,8 @@ namespace Wox.ViewModel }); - this.SelectPrevItemCommand = new RelayCommand((parameter) => { + this.SelectPrevItemCommand = new RelayCommand((parameter) => + { if (this.IsActionPanelVisible) { @@ -291,7 +330,8 @@ namespace Wox.ViewModel }); - this.CtrlOCommand = new RelayCommand((parameter) => { + this.CtrlOCommand = new RelayCommand((parameter) => + { if (this.IsActionPanelVisible) { @@ -303,37 +343,43 @@ namespace Wox.ViewModel } }); - this.DisplayNextQueryCommand = new RelayCommand((parameter) => { + this.DisplayNextQueryCommand = new RelayCommand((parameter) => + { var nextQuery = QueryHistoryStorage.Instance.Next(); DisplayQueryHistory(nextQuery); }); - this.DisplayPrevQueryCommand = new RelayCommand((parameter) => { + this.DisplayPrevQueryCommand = new RelayCommand((parameter) => + { var prev = QueryHistoryStorage.Instance.Previous(); DisplayQueryHistory(prev); }); - this.SelectNextPageCommand = new RelayCommand((parameter) => { + this.SelectNextPageCommand = new RelayCommand((parameter) => + { this._searchResultPanel.SelectNextPage(); }); - this.SelectPrevPageCommand = new RelayCommand((parameter) => { + this.SelectPrevPageCommand = new RelayCommand((parameter) => + { this._searchResultPanel.SelectPrevPage(); }); - this.StartHelpCommand = new RelayCommand((parameter) => { + this.StartHelpCommand = new RelayCommand((parameter) => + { Process.Start("http://doc.getwox.com"); }); - this.ShiftEnterCommand = new RelayCommand((parameter) => { + this.ShiftEnterCommand = new RelayCommand((parameter) => + { if (!this.IsActionPanelVisible && null != this._searchResultPanel.SelectedResult) { @@ -342,9 +388,10 @@ namespace Wox.ViewModel }); - this.OpenResultCommand = new RelayCommand((parameter) => { + this.OpenResultCommand = new RelayCommand((parameter) => + { - if(null != parameter) + if (null != parameter) { var index = int.Parse(parameter.ToString()); this._searchResultPanel.SelectResult(index); @@ -355,6 +402,15 @@ namespace Wox.ViewModel this._searchResultPanel.SelectedResult.OpenResultCommand.Execute(null); } }); + + this.BackCommand = new RelayCommand((parameter) => + { + if (null != ListeningKeyPressed) + { + this.ListeningKeyPressed(this, new ListeningKeyPressedEventArgs(parameter as System.Windows.Input.KeyEventArgs)); + } + + }); } private void InitializeResultPanel() @@ -380,27 +436,35 @@ namespace Wox.ViewModel private void ShowActionPanel(Result result) { if (result == null) return; - List results = PluginManager.GetContextMenusForPlugin(result); - results.ForEach(o => + this.ShowActionPanel(result, PluginManager.GetContextMenusForPlugin(result)); + } + + private void ShowActionPanel(Result result, List actions) + { + actions.ForEach(o => { o.PluginDirectory = PluginManager.GetPluginForId(result.PluginID).Metadata.PluginDirectory; o.PluginID = result.PluginID; o.OriginQuery = result.OriginQuery; }); - results.Add(GetTopMostContextMenu(result)); + actions.Add(GetTopMostContextMenu(result)); + this.DisplayActionPanel(actions, result.PluginID); + } + + private void DisplayActionPanel(List actions, string pluginID) + { _textBeforeEnterContextMenuMode = this.QueryText; this._actionPanel.Clear(); - this._actionPanel.AddResults(results, result.PluginID); - CurrentContextMenus = results; + this._actionPanel.AddResults(actions, pluginID); + CurrentContextMenus = actions; this.IsActionPanelVisible = true; this.IsSearchResultPanelVisible = false; this.QueryText = ""; - } private Result GetTopMostContextMenu(Result result) @@ -413,8 +477,7 @@ namespace Wox.ViewModel Action = _ => { TopMostRecordStorage.Instance.Remove(result); - //TODO:Modify the way showing this message - //ShowMsg("Succeed", "", ""); + App.API.ShowMsg("Succeed", "", ""); return false; } }; @@ -427,8 +490,7 @@ namespace Wox.ViewModel Action = _ => { TopMostRecordStorage.Instance.AddOrUpdate(result); - //TODO:Modify the way showing this message - //ShowMsg("Succeed", "", ""); + App.API.ShowMsg("Succeed", "", ""); return false; } }; @@ -576,7 +638,7 @@ namespace Wox.ViewModel var historyMetadata = QueryHistoryStorage.MetaData; this.QueryText = history.Query; - //TODO: Need to select all text + this.SelectAllText = true; var executeQueryHistoryTitle = InternationalizationManager.Instance.GetTranslation("executeQuery"); var lastExecuteTime = InternationalizationManager.Instance.GetTranslation("lastExecuteTime"); @@ -592,7 +654,7 @@ namespace Wox.ViewModel Action = _ =>{ this.QueryText = history.Query; - //TODO: Need to select all text + this.SelectAllText = true; return false; } @@ -616,18 +678,42 @@ namespace Wox.ViewModel }); if (originQuery.RawQuery == _lastQuery.RawQuery) { - Application.Current.Dispatcher.Invoke(() => { + System.Windows.Application.Current.Dispatcher.Invoke(() => + { UpdateResultViewInternal(list, metadata); }); } - if(list.Count > 0) + if (list.Count > 0) { this.IsSearchResultPanelVisible = true; } } + public void ShowActionPanel(List actions, string pluginID) + { + this.DisplayActionPanel(actions, pluginID); + } + #endregion + public event EventHandler ListeningKeyPressed; + + } + + public class ListeningKeyPressedEventArgs : EventArgs + { + + public System.Windows.Input.KeyEventArgs KeyEventArgs + { + get; + private set; + } + + public ListeningKeyPressedEventArgs(System.Windows.Input.KeyEventArgs keyEventArgs) + { + this.KeyEventArgs = keyEventArgs; + } + } } diff --git a/Wox/ViewModel/ResultItemViewModel.cs b/Wox/ViewModel/ResultItemViewModel.cs index 55b0306130..32aac3c430 100644 --- a/Wox/ViewModel/ResultItemViewModel.cs +++ b/Wox/ViewModel/ResultItemViewModel.cs @@ -73,7 +73,6 @@ namespace Wox.ViewModel { get { - //TODO: Some of the properties in Result class may be moved to this class return this._result.FullIcoPath; } }