PowerToys/Wox/ViewModel/MainViewModel.cs

637 lines
18 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2016-02-12 14:21:12 +08:00
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
2016-02-12 17:20:46 +08:00
using System.Windows.Forms;
using System.Windows.Input;
using Wox.Core.Plugin;
using Wox.Core.Resource;
2016-02-12 17:20:46 +08:00
using Wox.Core.UserSettings;
using Wox.Infrastructure;
using Wox.Infrastructure.Hotkey;
using Wox.Plugin;
using Wox.Storage;
using Wox.Extensions;
namespace Wox.ViewModel
{
public class MainViewModel : BaseViewModel
{
#region Private Fields
private string _queryText;
private bool _isProgressBarTooltipVisible;
2016-02-12 17:20:46 +08:00
private bool _selectAllText;
private int _caretIndex;
private double _left;
private double _top;
private Visibility _contextMenuVisibility;
private Visibility _progressBarVisibility;
private Visibility _resultListBoxVisibility;
private Visibility _windowVisibility;
private bool _queryHasReturn;
private Query _lastQuery = new Query();
private bool _ignoreTextChange;
private List<Result> CurrentContextMenus = new List<Result>();
private string _textBeforeEnterContextMenuMode;
#endregion
#region Constructor
public MainViewModel()
{
InitializeResultListBox();
InitializeContextMenu();
InitializeKeyCommands();
_queryHasReturn = false;
}
#endregion
#region ViewModel Properties
public ResultsViewModel Results { get; private set; }
public ResultsViewModel ContextMenu { get; private set; }
public string QueryText
{
get
{
return _queryText;
}
set
{
_queryText = value;
OnPropertyChanged("QueryText");
HandleQueryTextUpdated();
}
}
2016-02-12 17:20:46 +08:00
public bool SelectAllText
{
get
{
return _selectAllText;
2016-02-12 17:20:46 +08:00
}
set
{
_selectAllText = value;
2016-02-12 17:20:46 +08:00
OnPropertyChanged("SelectAllText");
}
}
public int CaretIndex
{
get
{
return _caretIndex;
2016-02-12 17:20:46 +08:00
}
set
{
_caretIndex = value;
2016-02-12 17:20:46 +08:00
OnPropertyChanged("CaretIndex");
}
}
public bool IsProgressBarTooltipVisible
{
get
{
2016-02-24 10:07:35 +08:00
return _isProgressBarTooltipVisible;
}
set
{
2016-02-24 10:07:35 +08:00
_isProgressBarTooltipVisible = value;
OnPropertyChanged("IsProgressBarTooltipVisible");
}
}
public double Left
{
get
{
2016-02-24 10:07:35 +08:00
return _left;
}
set
{
2016-02-24 10:07:35 +08:00
_left = value;
OnPropertyChanged("Left");
}
}
public double Top
{
get
{
2016-02-24 10:07:35 +08:00
return _top;
}
set
{
2016-02-24 10:07:35 +08:00
_top = value;
OnPropertyChanged("Top");
}
}
public Visibility ContextMenuVisibility
{
get
{
2016-02-24 10:07:35 +08:00
return _contextMenuVisibility;
}
set
{
2016-02-24 10:07:35 +08:00
_contextMenuVisibility = value;
OnPropertyChanged("ContextMenuVisibility");
}
}
public Visibility ProgressBarVisibility
{
get
{
2016-02-24 10:07:35 +08:00
return _progressBarVisibility;
}
set
{
2016-02-24 10:07:35 +08:00
_progressBarVisibility = value;
OnPropertyChanged("ProgressBarVisibility");
}
}
public Visibility ResultListBoxVisibility
{
get
{
2016-02-24 10:07:35 +08:00
return _resultListBoxVisibility;
}
set
{
2016-02-24 10:07:35 +08:00
_resultListBoxVisibility = value;
OnPropertyChanged("ResultListBoxVisibility");
}
}
public Visibility WindowVisibility
{
get
{
2016-02-24 10:07:35 +08:00
return _windowVisibility;
}
set
{
2016-02-24 10:07:35 +08:00
_windowVisibility = value;
OnPropertyChanged("WindowVisibility");
2016-02-24 10:07:35 +08:00
if (value.IsNotVisible() && ContextMenuVisibility.IsVisible())
{
2016-02-24 10:07:35 +08:00
BackToSearchMode();
}
}
}
public ICommand EscCommand { get; set; }
public ICommand SelectNextItemCommand { get; set; }
2016-02-12 14:21:12 +08:00
public ICommand SelectPrevItemCommand { get; set; }
2016-02-12 14:21:12 +08:00
public ICommand CtrlOCommand { get; set; }
2016-02-12 14:21:12 +08:00
public ICommand DisplayNextQueryCommand { get; set; }
2016-02-12 14:21:12 +08:00
public ICommand DisplayPrevQueryCommand { get; set; }
2016-02-12 14:21:12 +08:00
public ICommand SelectNextPageCommand { get; set; }
2016-02-12 14:21:12 +08:00
public ICommand SelectPrevPageCommand { get; set; }
2016-02-12 17:20:46 +08:00
public ICommand StartHelpCommand { get; set; }
public ICommand ShiftEnterCommand { get; set; }
public ICommand OpenResultCommand { get; set; }
public ICommand BackCommand { get; set; }
#endregion
#region Private Methods
private void InitializeKeyCommands()
{
EscCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-24 10:07:35 +08:00
if (ContextMenuVisibility.IsVisible())
{
BackToSearchMode();
}
else
{
2016-02-24 10:07:35 +08:00
WindowVisibility = Visibility.Collapsed;
}
});
2016-02-12 14:21:12 +08:00
SelectNextItemCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
2016-02-24 10:07:35 +08:00
if (ContextMenuVisibility.IsVisible())
2016-02-12 14:21:12 +08:00
{
ContextMenu.SelectNextResult();
2016-02-12 14:21:12 +08:00
}
else
{
Results.SelectNextResult();
2016-02-12 14:21:12 +08:00
}
});
SelectPrevItemCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
2016-02-24 10:07:35 +08:00
if (ContextMenuVisibility.IsVisible())
2016-02-12 14:21:12 +08:00
{
ContextMenu.SelectPrevResult();
2016-02-12 14:21:12 +08:00
}
else
{
Results.SelectPrevResult();
2016-02-12 14:21:12 +08:00
}
});
CtrlOCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
2016-02-24 10:07:35 +08:00
if (ContextMenuVisibility.IsVisible())
2016-02-12 14:21:12 +08:00
{
BackToSearchMode();
}
else
{
ShowContextMenu(Results.SelectedResult.RawResult);
2016-02-12 14:21:12 +08:00
}
});
DisplayNextQueryCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
var nextQuery = QueryHistoryStorage.Instance.Next();
DisplayQueryHistory(nextQuery);
});
DisplayPrevQueryCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
var prev = QueryHistoryStorage.Instance.Previous();
DisplayQueryHistory(prev);
});
SelectNextPageCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
Results.SelectNextPage();
2016-02-12 14:21:12 +08:00
});
SelectPrevPageCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
Results.SelectPrevPage();
2016-02-12 14:21:12 +08:00
});
StartHelpCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
Process.Start("http://doc.getwox.com");
});
ShiftEnterCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
2016-02-24 10:07:35 +08:00
if (ContextMenuVisibility.IsNotVisible() && null != Results.SelectedResult)
2016-02-12 14:21:12 +08:00
{
ShowContextMenu(Results.SelectedResult.RawResult);
2016-02-12 14:21:12 +08:00
}
});
OpenResultCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
2016-02-12 17:20:46 +08:00
if (null != parameter)
2016-02-12 14:21:12 +08:00
{
var index = int.Parse(parameter.ToString());
Results.SelectResult(index);
2016-02-12 14:21:12 +08:00
}
if (null != Results.SelectedResult)
2016-02-12 14:21:12 +08:00
{
Results.SelectedResult.OpenResultListBoxItemCommand.Execute(null);
2016-02-12 14:21:12 +08:00
}
});
2016-02-12 17:20:46 +08:00
BackCommand = new RelayCommand((parameter) =>
2016-02-12 17:20:46 +08:00
{
if (null != ListeningKeyPressed)
{
ListeningKeyPressed(this, new ListeningKeyPressedEventArgs(parameter as System.Windows.Input.KeyEventArgs));
2016-02-12 17:20:46 +08:00
}
});
}
private void InitializeResultListBox()
{
Results = new ResultsViewModel();
2016-02-24 10:07:35 +08:00
ResultListBoxVisibility = Visibility.Collapsed;
}
private void ShowContextMenu(Result result)
{
if (result == null) return;
ShowContextMenu(result, PluginManager.GetContextMenusForPlugin(result));
2016-02-12 17:20:46 +08:00
}
private void ShowContextMenu(Result result, List<Result> actions)
2016-02-12 17:20:46 +08:00
{
actions.ForEach(o =>
{
o.PluginDirectory = PluginManager.GetPluginForId(result.PluginID).Metadata.PluginDirectory;
o.PluginID = result.PluginID;
o.OriginQuery = result.OriginQuery;
});
2016-02-12 17:20:46 +08:00
actions.Add(GetTopMostContextMenu(result));
DisplayContextMenu(actions, result.PluginID);
2016-02-12 17:20:46 +08:00
}
private void DisplayContextMenu(List<Result> actions, string pluginID)
2016-02-12 17:20:46 +08:00
{
_textBeforeEnterContextMenuMode = QueryText;
ContextMenu.Clear();
ContextMenu.AddResults(actions, pluginID);
2016-02-12 17:20:46 +08:00
CurrentContextMenus = actions;
2016-02-24 10:07:35 +08:00
ContextMenuVisibility = Visibility.Visible;
ResultListBoxVisibility = Visibility.Collapsed;
QueryText = "";
}
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);
2016-02-12 17:20:46 +08:00
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);
2016-02-12 17:20:46 +08:00
App.API.ShowMsg("Succeed", "", "");
return false;
}
};
}
}
private void InitializeContextMenu()
{
ContextMenu = new ResultsViewModel();
2016-02-24 10:07:35 +08:00
ContextMenuVisibility = Visibility.Collapsed;
}
private void HandleQueryTextUpdated()
{
if (_ignoreTextChange) { _ignoreTextChange = false; return; }
IsProgressBarTooltipVisible = false;
2016-02-24 10:07:35 +08:00
if (ContextMenuVisibility.IsVisible())
{
QueryContextMenu();
}
else
{
string query = QueryText.Trim();
if (!string.IsNullOrEmpty(query))
{
Query(query);
//reset query history index after user start new query
ResetQueryHistoryIndex();
}
else
{
Results.Clear();
}
}
}
private void QueryContextMenu()
{
var contextMenuId = "Context Menu Id";
ContextMenu.Clear();
var query = QueryText.ToLower();
if (string.IsNullOrEmpty(query))
{
ContextMenu.AddResults(CurrentContextMenus, contextMenuId);
}
else
{
List<Result> filterResults = new List<Result>();
foreach (Result contextMenu in CurrentContextMenus)
{
if (StringMatcher.IsMatch(contextMenu.Title, query)
|| StringMatcher.IsMatch(contextMenu.SubTitle, query))
{
filterResults.Add(contextMenu);
}
}
ContextMenu.AddResults(filterResults, contextMenuId);
}
}
private void Query(string text)
{
_queryHasReturn = false;
var query = PluginManager.QueryInit(text);
if (query != null)
{
// handle the exclusiveness of plugin using action keyword
string lastKeyword = _lastQuery.ActionKeyword;
string keyword = query.ActionKeyword;
if (string.IsNullOrEmpty(lastKeyword))
{
if (!string.IsNullOrEmpty(keyword))
{
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
}
}
else
{
if (string.IsNullOrEmpty(keyword))
{
Results.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata);
}
else if (lastKeyword != keyword)
{
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
}
}
_lastQuery = query;
Action action = new Action(async () =>
{
await Task.Delay(150);
if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
{
IsProgressBarTooltipVisible = true;
}
});
action.Invoke();
//Application.Current.Dispatcher.InvokeAsync(async () =>
//{
// await Task.Delay(150);
// if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
// {
// StartProgress();
// }
//});
PluginManager.QueryForAllPlugins(query);
}
IsProgressBarTooltipVisible = false;
}
private void ResetQueryHistoryIndex()
{
Results.RemoveResultsFor(QueryHistoryStorage.MetaData);
QueryHistoryStorage.Instance.Reset();
}
private void UpdateResultViewInternal(List<Result> list, PluginMetadata metadata)
{
2016-02-12 14:21:12 +08:00
Infrastructure.Stopwatch.Normal($"UI update cost for {metadata.Name}",
() => { Results.AddResults(list, metadata.ID); });
}
private void BackToSearchMode()
{
QueryText = _textBeforeEnterContextMenuMode;
2016-02-24 10:07:35 +08:00
ContextMenuVisibility = Visibility.Collapsed;
ResultListBoxVisibility = Visibility.Visible;
CaretIndex = QueryText.Length;
}
2016-02-12 14:21:12 +08:00
private void DisplayQueryHistory(HistoryItem history)
{
if (history != null)
{
var historyMetadata = QueryHistoryStorage.MetaData;
QueryText = history.Query;
SelectAllText = true;
2016-02-12 14:21:12 +08:00
var executeQueryHistoryTitle = InternationalizationManager.Instance.GetTranslation("executeQuery");
var lastExecuteTime = InternationalizationManager.Instance.GetTranslation("lastExecuteTime");
Results.RemoveResultsExcept(historyMetadata);
2016-02-12 14:21:12 +08:00
UpdateResultViewInternal(new List<Result>
{
new Result
{
Title = string.Format(executeQueryHistoryTitle,history.Query),
SubTitle = string.Format(lastExecuteTime,history.ExecutedDateTime),
IcoPath = "Images\\history.png",
PluginDirectory = WoxDirectroy.Executable,
Action = _ =>{
QueryText = history.Query;
SelectAllText = true;
2016-02-12 14:21:12 +08:00
return false;
}
}
}, historyMetadata);
}
}
#endregion
#region Public Methods
public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery)
{
_queryHasReturn = true;
IsProgressBarTooltipVisible = false;
list.ForEach(o =>
{
o.Score += UserSelectedRecordStorage.Instance.GetSelectedCount(o) * 5;
});
if (originQuery.RawQuery == _lastQuery.RawQuery)
{
2016-02-12 17:20:46 +08:00
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
UpdateResultViewInternal(list, metadata);
});
}
2016-02-12 17:20:46 +08:00
if (list.Count > 0)
{
2016-02-24 10:07:35 +08:00
ResultListBoxVisibility = Visibility.Visible;
}
}
public void ShowContextMenu(List<Result> actions, string pluginID)
2016-02-12 17:20:46 +08:00
{
DisplayContextMenu(actions, pluginID);
2016-02-12 17:20:46 +08:00
}
#endregion
2016-02-12 17:20:46 +08:00
public event EventHandler<ListeningKeyPressedEventArgs> ListeningKeyPressed;
}
public class ListeningKeyPressedEventArgs : EventArgs
{
public System.Windows.Input.KeyEventArgs KeyEventArgs
{
get;
private set;
}
public ListeningKeyPressedEventArgs(System.Windows.Input.KeyEventArgs keyEventArgs)
{
KeyEventArgs = keyEventArgs;
2016-02-12 17:20:46 +08:00
}
}
}