diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index 827c15c60d..e6e5912376 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -417,20 +417,6 @@ namespace Wox.ViewModel { this._searchResultPanel = new ResultPanelViewModel(); this.IsSearchResultPanelVisible = false; - this._searchResultPanel.ResultOpenedInPanel += (o, e) => - { - if (e.HideWindow) - { - this.IsVisible = false; - } - UserSelectedRecordStorage.Instance.Add(e.Result.RawResult); - QueryHistoryStorage.Instance.Add(this.QueryText); - }; - - this._searchResultPanel.ResultActionPanelOpenedInPanel += (o, e) => - { - this.ShowActionPanel(e.Result.RawResult); - }; } private void ShowActionPanel(Result result) @@ -501,13 +487,6 @@ namespace Wox.ViewModel { this._actionPanel = new ResultPanelViewModel(); this.IsActionPanelVisible = false; - this._actionPanel.ResultOpenedInPanel += (o, e) => - { - if (e.HideWindow) - { - this.IsVisible = false; - } - }; } private void HandleQueryTextUpdated() diff --git a/Wox/ViewModel/ResultItemViewModel.cs b/Wox/ViewModel/ResultItemViewModel.cs index 07092df8df..a963e294cd 100644 --- a/Wox/ViewModel/ResultItemViewModel.cs +++ b/Wox/ViewModel/ResultItemViewModel.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Wox.Core.Plugin; +using Wox.Core.Resource; +using Wox.Infrastructure; using Wox.Infrastructure.Hotkey; using Wox.Plugin; using Wox.Storage; @@ -33,23 +36,36 @@ namespace Wox.ViewModel SpecialKeyState = GlobalHotkey.Instance.CheckModifiers() }); - if (null != this.ResultOpened) + if (hideWindow) { - this.ResultOpened(this, new ResultOpenedEventArgs(hideWindow)); + App.API.HideApp(); + UserSelectedRecordStorage.Instance.Add(this._result); + QueryHistoryStorage.Instance.Add(this._result.OriginQuery.RawQuery); } }); - this.OpenResultActionPanelCommand = new RelayCommand((parameter) => { + this.OpenResultActionPanelCommand = new RelayCommand((parameter) => + { - if(null!= ResultActionPanelOpened) + var actions = PluginManager.GetContextMenusForPlugin(result); + + var pluginMetaData = PluginManager.GetPluginForId(result.PluginID).Metadata; + actions.ForEach(o => { - this.ResultActionPanelOpened(this, new EventArgs()); - } + o.PluginDirectory = pluginMetaData.PluginDirectory; + o.PluginID = result.PluginID; + o.OriginQuery = result.OriginQuery; + }); + + actions.Add(GetTopMostContextMenu(result)); + + App.API.ShowContextMenu(pluginMetaData, actions); }); } } + #endregion #region ViewModel Properties @@ -112,6 +128,41 @@ namespace Wox.ViewModel } } + #endregion + + #region Private Methods + + private Result GetTopMostContextMenu(Result result) + { + if (TopMostRecordStorage.Instance.IsTopMost(result)) + { + return new Result(InternationalizationManager.Instance.GetTranslation("cancelTopMostInThisQuery"), "Images\\down.png") + { + PluginDirectory = WoxDirectroy.Executable, + Action = _ => + { + TopMostRecordStorage.Instance.Remove(result); + App.API.ShowMsg("Succeed", "", ""); + return false; + } + }; + } + else + { + return new Result(InternationalizationManager.Instance.GetTranslation("setAsTopMostInThisQuery"), "Images\\up.png") + { + PluginDirectory = WoxDirectroy.Executable, + Action = _ => + { + TopMostRecordStorage.Instance.AddOrUpdate(result); + App.API.ShowMsg("Succeed", "", ""); + return false; + } + }; + } + } + + #endregion public override bool Equals(object obj) @@ -135,24 +186,5 @@ namespace Wox.ViewModel return _result.ToString(); } - public event EventHandler ResultOpened; - - public event EventHandler ResultActionPanelOpened; - - } - - public class ResultOpenedEventArgs : EventArgs - { - - public bool HideWindow - { - get; - private set; - } - - public ResultOpenedEventArgs(bool hideWindow) - { - this.HideWindow = hideWindow; - } } } diff --git a/Wox/ViewModel/ResultPanelViewModel.cs b/Wox/ViewModel/ResultPanelViewModel.cs index e2046e0237..2cc946df62 100644 --- a/Wox/ViewModel/ResultPanelViewModel.cs +++ b/Wox/ViewModel/ResultPanelViewModel.cs @@ -30,25 +30,7 @@ namespace Wox.ViewModel public ResultPanelViewModel() { - this._results = new ResultCollection( - - (o, e)=> { - - if(null != ResultOpenedInPanel) - { - this.ResultOpenedInPanel(this, new ResultOpenedInPanelEventArgs(o as ResultItemViewModel, e.HideWindow)); - } - }, - - (o, e) => { - - if(null != ResultActionPanelOpenedInPanel) - { - this.ResultActionPanelOpenedInPanel(this, new ResultActionPanelOpenedInPanelEventArgs(o as ResultItemViewModel)); - } - - } - ); + this._results = new ResultCollection(); } #endregion @@ -293,64 +275,12 @@ namespace Wox.ViewModel #endregion - public event EventHandler ResultOpenedInPanel; - - public event EventHandler ResultActionPanelOpenedInPanel; - public class ResultCollection : ObservableCollection // todo implement custom moveItem,removeItem,insertItem for better performance { - private EventHandler _resultOpenedHandler; - private EventHandler _resultActionPanelOpenedHandler; - - public ResultCollection(EventHandler resultOpenedHandler, - EventHandler resultActionPanelOpenedHandler) + public ResultCollection() { - this._resultOpenedHandler = resultOpenedHandler; - this._resultActionPanelOpenedHandler = resultActionPanelOpenedHandler; - } - - protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) - { - base.OnCollectionChanged(e); - - if(e.Action == NotifyCollectionChangedAction.Add) - { - foreach(var item in e.NewItems) - { - var resultVM = item as ResultItemViewModel; - resultVM.ResultOpened += this._resultOpenedHandler; - resultVM.ResultActionPanelOpened += this._resultActionPanelOpenedHandler; - } - } - - if(e.Action == NotifyCollectionChangedAction.Remove) - { - foreach (var item in e.OldItems) - { - var resultVM = item as ResultItemViewModel; - resultVM.ResultOpened -= this._resultOpenedHandler; - resultVM.ResultActionPanelOpened -= this._resultActionPanelOpenedHandler; - } - } - - if(e.Action == NotifyCollectionChangedAction.Replace) - { - foreach (var item in e.NewItems) - { - var resultVM = item as ResultItemViewModel; - resultVM.ResultOpened += this._resultOpenedHandler; - resultVM.ResultActionPanelOpened += this._resultActionPanelOpenedHandler; - } - - foreach (var item in e.OldItems) - { - var resultVM = item as ResultItemViewModel; - resultVM.ResultOpened -= this._resultOpenedHandler; - resultVM.ResultActionPanelOpened -= this._resultActionPanelOpenedHandler; - } - } } public void RemoveAll(Predicate predicate) @@ -418,41 +348,5 @@ namespace Wox.ViewModel } } - - public class ResultOpenedInPanelEventArgs : EventArgs - { - - public bool HideWindow - { - get; - private set; - } - - public ResultItemViewModel Result - { - get; - private set; - } - - public ResultOpenedInPanelEventArgs(ResultItemViewModel result, bool hideWindow) - { - this.HideWindow = hideWindow; - this.Result = result; - } - } - - public class ResultActionPanelOpenedInPanelEventArgs : EventArgs - { - - public ResultItemViewModel Result - { - get; - private set; - } - - public ResultActionPanelOpenedInPanelEventArgs(ResultItemViewModel result) - { - this.Result = result; - } - } + }