PowerToys/Wox/ViewModel/MainViewModel.cs

578 lines
17 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2016-02-27 07:43:57 +08:00
using System.ComponentModel;
2016-02-12 14:21:12 +08:00
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Wox.Core.Plugin;
using Wox.Core.Resource;
2016-02-26 20:05:32 +08:00
using Wox.Helper;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Storage;
2016-02-23 05:43:37 +08:00
using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace Wox.ViewModel
{
public class MainViewModel : BaseViewModel
{
#region Private Fields
private string _queryText;
private bool _isProgressBarTooltipVisible;
private double _left;
private double _top;
private Visibility _contextMenuVisibility;
private Visibility _progressBarVisibility;
private Visibility _resultListBoxVisibility;
2016-02-26 20:05:32 +08:00
private Visibility _mainWindowVisibility;
private bool _queryHasReturn;
private Query _lastQuery = new Query();
private bool _ignoreTextChange;
2016-02-23 05:43:37 +08:00
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();
HandleQueryTextUpdated();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
2016-02-26 20:05:32 +08:00
public Visibility MainWindowVisibility
{
get
{
2016-02-26 20:05:32 +08:00
return _mainWindowVisibility;
}
set
{
2016-02-26 20:05:32 +08:00
_mainWindowVisibility = value;
OnPropertyChanged();
2016-02-27 07:43:57 +08:00
MainWindowVisibilityChanged?.Invoke(this, new EventArgs());
2016-02-26 20:05:32 +08:00
if (!value.IsVisible() && ContextMenuVisibility.IsVisible())
{
2016-02-24 10:07:35 +08:00
BackToSearchMode();
}
}
}
public ICommand EscCommand { get; set; }
public ICommand SelectNextItemCommand { get; set; }
public ICommand SelectPrevItemCommand { get; set; }
public ICommand CtrlOCommand { get; set; }
public ICommand DisplayNextQueryCommand { get; set; }
public ICommand DisplayPrevQueryCommand { get; set; }
public ICommand SelectNextPageCommand { get; set; }
public ICommand SelectPrevPageCommand { get; set; }
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()
{
2016-02-23 05:43:37 +08:00
EscCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
2016-02-24 10:07:35 +08:00
if (ContextMenuVisibility.IsVisible())
{
BackToSearchMode();
}
else
{
2016-02-26 20:05:32 +08:00
MainWindowVisibility = Visibility.Collapsed;
}
});
2016-02-12 14:21:12 +08:00
2016-02-27 07:43:57 +08:00
SelectNextItemCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +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
}
});
2016-02-23 05:43:37 +08:00
SelectPrevItemCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +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
}
});
2016-02-23 05:43:37 +08:00
CtrlOCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +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
}
});
2016-02-23 05:43:37 +08:00
DisplayNextQueryCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
var nextQuery = QueryHistoryStorage.Instance.Next();
DisplayQueryHistory(nextQuery);
});
2016-02-23 05:43:37 +08:00
DisplayPrevQueryCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
var prev = QueryHistoryStorage.Instance.Previous();
DisplayQueryHistory(prev);
});
2016-02-23 05:43:37 +08:00
SelectNextPageCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
Results.SelectNextPage();
2016-02-12 14:21:12 +08:00
});
2016-02-23 05:43:37 +08:00
SelectPrevPageCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
Results.SelectPrevPage();
2016-02-12 14:21:12 +08:00
});
2016-02-23 05:43:37 +08:00
StartHelpCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
2016-02-12 14:21:12 +08:00
Process.Start("http://doc.getwox.com");
});
2016-02-23 05:43:37 +08:00
ShiftEnterCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
2016-02-26 20:05:32 +08:00
if (!ContextMenuVisibility.IsVisible() && null != Results.SelectedResult)
2016-02-12 14:21:12 +08:00
{
ShowContextMenu(Results.SelectedResult.RawResult);
2016-02-12 14:21:12 +08:00
}
});
2016-02-23 05:43:37 +08:00
OpenResultCommand = new RelayCommand(o =>
2016-02-12 17:20:46 +08:00
{
if (o != null)
2016-02-12 14:21:12 +08:00
{
2016-02-23 05:43:37 +08:00
var index = int.Parse(o.ToString());
Results.SelectResult(index);
2016-02-12 14:21:12 +08:00
}
2016-02-23 05:43:37 +08:00
Results.SelectedResult?.OpenResultListBoxItemCommand.Execute(null);
2016-02-12 14:21:12 +08:00
});
2016-02-12 17:20:46 +08:00
2016-02-23 05:43:37 +08:00
BackCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
2016-02-23 05:43:37 +08:00
ListeningKeyPressed?.Invoke(this, new ListeningKeyPressedEventArgs(_ as 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-23 05:43:37 +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);
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;
}
};
}
}
private void InitializeContextMenu()
{
ContextMenu = new ResultsViewModel();
2016-02-24 10:07:35 +08:00
ContextMenuVisibility = Visibility.Collapsed;
}
private void HandleQueryTextUpdated()
{
2016-02-27 07:43:57 +08:00
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))
{
2016-02-23 05:43:37 +08:00
ContextMenu.AddResults(_currentContextMenus, contextMenuId);
}
else
{
List<Result> filterResults = new List<Result>();
2016-02-23 05:43:37 +08:00
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;
2016-02-23 05:43:37 +08:00
Action action = async () =>
{
await Task.Delay(150);
if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
{
IsProgressBarTooltipVisible = true;
}
2016-02-23 05:43:37 +08:00
};
action.Invoke();
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-23 05:43:37 +08:00
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;
2016-02-27 07:43:57 +08:00
OnCursorMovedToEnd();
}
2016-02-12 14:21:12 +08:00
private void DisplayQueryHistory(HistoryItem history)
{
if (history != null)
{
var historyMetadata = QueryHistoryStorage.MetaData;
QueryText = history.Query;
2016-02-27 07:43:57 +08:00
OnTextBoxSelected();
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;
2016-02-27 07:43:57 +08:00
OnTextBoxSelected();
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-23 05:43:37 +08:00
Application.Current.Dispatcher.Invoke(() =>
2016-02-12 17:20:46 +08:00
{
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;
2016-02-27 07:43:57 +08:00
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());
}
2016-02-12 17:20:46 +08:00
}
public class ListeningKeyPressedEventArgs : EventArgs
{
2016-02-23 05:43:37 +08:00
public KeyEventArgs KeyEventArgs
2016-02-12 17:20:46 +08:00
{
get;
private set;
}
2016-02-23 05:43:37 +08:00
public ListeningKeyPressedEventArgs(KeyEventArgs keyEventArgs)
2016-02-12 17:20:46 +08:00
{
KeyEventArgs = keyEventArgs;
2016-02-12 17:20:46 +08:00
}
}
}