PowerToys/Wox/ViewModel/MainViewModel.cs

719 lines
23 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.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;
2016-02-26 20:05:32 +08:00
using Wox.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Hotkey;
2016-05-10 05:45:20 +08:00
using Wox.Infrastructure.Image;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Storage;
namespace Wox.ViewModel
{
public class MainViewModel : BaseViewModel, ISavable
{
#region Private Fields
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;
private bool _ignoreTextChange;
private string _queryTextBeforeLoadContextMenu;
private string _queryText;
private readonly JsonStrorage<QueryHistory> _queryHistoryStorage;
private readonly JsonStrorage<UserSelectedRecord> _userSelectedRecordStorage;
private readonly JsonStrorage<TopMostRecord> _topMostRecordStorage;
private readonly Settings _settings;
private readonly QueryHistory _queryHistory;
private readonly UserSelectedRecord _userSelectedRecord;
private readonly TopMostRecord _topMostRecord;
private CancellationTokenSource _updateSource;
private CancellationToken _updateToken;
2016-05-10 05:45:20 +08:00
private bool _saved;
#endregion
#region Constructor
2016-05-22 05:44:27 +08:00
public MainViewModel(Settings settings)
{
2016-05-10 05:45:20 +08:00
_saved = false;
_queryTextBeforeLoadContextMenu = "";
_queryText = "";
_lastQuery = new Query();
_settings = settings;
// happlebao todo temp fix for instance code logic
HttpProxy.Instance.Settings = _settings;
InternationalizationManager.Instance.Settings = _settings;
2016-05-07 08:52:04 +08:00
InternationalizationManager.Instance.ChangeLanguage(_settings.Language);
ThemeManager.Instance.Settings = _settings;
_queryHistoryStorage = new JsonStrorage<QueryHistory>();
_userSelectedRecordStorage = new JsonStrorage<UserSelectedRecord>();
_topMostRecordStorage = new JsonStrorage<TopMostRecord>();
_queryHistory = _queryHistoryStorage.Load();
_userSelectedRecord = _userSelectedRecordStorage.Load();
_topMostRecord = _topMostRecordStorage.Load();
InitializeResultListBox();
InitializeContextMenu();
InitializeKeyCommands();
RegisterResultsUpdatedEvent();
SetHotkey(_settings.Hotkey, OnHotkey);
SetCustomPluginHotkey();
}
private void RegisterResultsUpdatedEvent()
{
foreach (var pair in PluginManager.GetPluginsForInterface<IResultUpdated>())
{
var plugin = (IResultUpdated) pair.Plugin;
plugin.ResultsUpdated += (s, e) =>
{
Task.Run(() =>
{
PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query);
UpdateResultView(e.Results, pair.Metadata, e.Query);
}, _updateToken);
};
}
}
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())
{
ContextMenuVisibility = Visibility.Collapsed;
}
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-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
{
var nextQuery = _queryHistory.Next();
2016-02-12 14:21:12 +08:00
DisplayQueryHistory(nextQuery);
});
2016-02-23 05:43:37 +08:00
DisplayPrevQueryCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
var prev = _queryHistory.Previous();
2016-02-12 14:21:12 +08:00
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-05-06 10:24:14 +08:00
OpenResultCommand = new RelayCommand(index =>
2016-02-12 17:20:46 +08:00
{
var results = ContextMenuVisibility.IsVisible() ? ContextMenu : Results;
2016-05-06 10:24:14 +08:00
if (index != null)
{
2016-05-06 10:24:14 +08:00
results.SelectedIndex = int.Parse(index.ToString());
}
2016-05-06 10:24:14 +08:00
var result = results.SelectedItem?.RawResult;
if (result != null) // SelectedItem returns null if selection is empty.
2016-02-12 14:21:12 +08:00
{
bool hideWindow = result.Action != null && result.Action(new ActionContext
2016-05-06 10:24:14 +08:00
{
SpecialKeyState = GlobalHotkey.Instance.CheckModifiers()
});
2016-05-06 10:24:14 +08:00
if (hideWindow)
{
MainWindowVisibility = Visibility.Collapsed;
}
if (!ContextMenuVisibility.IsVisible())
{
_userSelectedRecord.Add(result);
_queryHistory.Add(result.OriginQuery.RawQuery);
}
}
2016-02-12 14:21:12 +08:00
});
LoadContextMenuCommand = new RelayCommand(_ =>
2016-02-12 17:20:46 +08:00
{
if (!ContextMenuVisibility.IsVisible())
2016-02-12 14:21:12 +08:00
{
2016-05-06 10:24:14 +08:00
var result = Results.SelectedItem?.RawResult;
2016-05-06 10:24:14 +08:00
if (result != null) // SelectedItem returns null if selection is empty.
{
2016-05-06 10:24:14 +08:00
var id = result.PluginID;
var menus = PluginManager.GetContextMenusForPlugin(result);
menus.Add(ContextMenuTopMost(result));
menus.Add(ContextMenuPluginInfo(id));
ContextMenu.Clear();
Task.Run(() =>
{
ContextMenu.AddResults(menus, id);
}, _updateToken);
ContextMenuVisibility = Visibility.Visible;
}
}
else
{
ContextMenuVisibility = Visibility.Collapsed;
2016-02-12 14:21:12 +08:00
}
});
2016-02-12 17:20:46 +08:00
}
private void InitializeResultListBox()
{
Results = new ResultsViewModel(_settings);
2016-02-24 10:07:35 +08:00
ResultListBoxVisibility = Visibility.Collapsed;
}
private void InitializeContextMenu()
{
ContextMenu = new ResultsViewModel(_settings);
2016-02-24 10:07:35 +08:00
ContextMenuVisibility = Visibility.Collapsed;
}
private void HandleQueryTextUpdated()
{
2016-04-25 10:00:55 +08:00
ProgressBarVisibility = Visibility.Hidden;
_updateSource?.Cancel();
_updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token;
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();
ResultListBoxVisibility = Visibility.Collapsed;
}
}
}
#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();
if (_ignoreTextChange)
{
_ignoreTextChange = false;
}
else
{
HandleQueryTextUpdated();
}
}
}
public double Left
{
get { return _left; }
set
{
_left = value;
OnPropertyChanged();
}
}
public double Top
{
get { return _top; }
set
{
_top = value;
OnPropertyChanged();
}
}
public Visibility ContextMenuVisibility
{
get { return _contextMenuVisibility; }
set
{
_contextMenuVisibility = value;
OnPropertyChanged();
_ignoreTextChange = true;
if (!value.IsVisible())
{
QueryText = _queryTextBeforeLoadContextMenu;
ResultListBoxVisibility = Visibility.Visible;
OnCursorMovedToEnd();
}
else
{
_queryTextBeforeLoadContextMenu = QueryText;
QueryText = "";
ResultListBoxVisibility = Visibility.Collapsed;
}
}
}
public Visibility ProgressBarVisibility
{
get { return _progressBarVisibility; }
set
{
_progressBarVisibility = value;
OnPropertyChanged();
}
}
public Visibility ResultListBoxVisibility
{
get { return _resultListBoxVisibility; }
set
{
_resultListBoxVisibility = value;
OnPropertyChanged();
}
}
public Visibility MainWindowVisibility
{
get { return _mainWindowVisibility; }
set
{
_mainWindowVisibility = value;
OnPropertyChanged();
MainWindowVisibilityChanged?.Invoke(this, new EventArgs());
}
}
public ICommand EscCommand { get; set; }
public ICommand SelectNextItemCommand { get; set; }
public ICommand SelectPrevItemCommand { 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 LoadContextMenuCommand { get; set; }
public ICommand OpenResultCommand { get; set; }
#endregion
#region Private Methods
private void QueryContextMenu()
{
var contextMenuId = "Context Menu Id";
var query = QueryText.ToLower();
if (!string.IsNullOrEmpty(query))
{
List<Result> filterResults = new List<Result>();
foreach (var contextMenu in ContextMenu.Results)
{
if (StringMatcher.IsMatch(contextMenu.Title, query)
|| StringMatcher.IsMatch(contextMenu.SubTitle, query))
{
filterResults.Add(contextMenu.RawResult);
}
}
ContextMenu.Clear();
Task.Run(() =>
{
ContextMenu.AddResults(filterResults, contextMenuId);
}, _updateToken);
}
}
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;
Task.Delay(200, _updateToken).ContinueWith(_ =>
{
if (query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
{
ProgressBarVisibility = Visibility.Visible;
}
}, _updateToken);
var plugins = PluginManager.ValidPluginsForQuery(query);
Task.Run(() =>
{
Parallel.ForEach(plugins, plugin =>
{
var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID];
if (!config.Disabled)
{
var results = PluginManager.QueryForPlugin(plugin, query);
UpdateResultView(results, plugin.Metadata, query);
}
});
}, _updateToken);
}
}
private void ResetQueryHistoryIndex()
{
Results.RemoveResultsFor(QueryHistory.MetaData);
_queryHistory.Reset();
}
2016-02-12 14:21:12 +08:00
private void DisplayQueryHistory(HistoryItem history)
{
if (history != null)
{
var historyMetadata = QueryHistory.MetaData;
2016-02-12 14:21:12 +08:00
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);
var result = new Result
2016-02-12 14:21:12 +08:00
{
Title = string.Format(executeQueryHistoryTitle, history.Query),
SubTitle = string.Format(lastExecuteTime, history.ExecutedDateTime),
IcoPath = "Images\\history.png",
2016-05-19 02:38:43 +08:00
PluginDirectory = Infrastructure.Constant.ProgramDirectory,
Action = _ =>
2016-02-12 14:21:12 +08:00
{
QueryText = history.Query;
OnTextBoxSelected();
return false;
2016-02-12 14:21:12 +08:00
}
};
Task.Run(() =>
{
Results.AddResults(new List<Result> {result}, historyMetadata.ID);
}, _updateToken);
2016-02-12 14:21:12 +08:00
}
}
private Result ContextMenuTopMost(Result result)
{
Result menu;
if (_topMostRecord.IsTopMost(result))
{
menu = new Result
{
Title = InternationalizationManager.Instance.GetTranslation("cancelTopMostInThisQuery"),
IcoPath = "Images\\down.png",
2016-05-19 02:38:43 +08:00
PluginDirectory = Infrastructure.Constant.ProgramDirectory,
Action = _ =>
{
_topMostRecord.Remove(result);
App.API.ShowMsg("Succeed");
return false;
}
};
}
else
{
menu = new Result
{
Title = InternationalizationManager.Instance.GetTranslation("setAsTopMostInThisQuery"),
IcoPath = "Images\\up.png",
2016-05-19 02:38:43 +08:00
PluginDirectory = Infrastructure.Constant.ProgramDirectory,
Action = _ =>
{
_topMostRecord.AddOrUpdate(result);
App.API.ShowMsg("Succeed");
return false;
}
};
}
return menu;
}
private Result ContextMenuPluginInfo(string id)
{
var metadata = PluginManager.GetPluginForId(id).Metadata;
var translator = InternationalizationManager.Instance;
var author = translator.GetTranslation("author");
var website = translator.GetTranslation("website");
var version = translator.GetTranslation("version");
var plugin = translator.GetTranslation("plugin");
var title = $"{plugin}: {metadata.Name}";
var icon = metadata.IcoPath;
var subtitle = $"{author}: {metadata.Author}, {website}: {metadata.Website} {version}: {metadata.Version}";
var menu = new Result
{
Title = title,
IcoPath = icon,
SubTitle = subtitle,
PluginDirectory = metadata.PluginDirectory,
Action = _ => false
};
return menu;
}
#endregion
#region Hotkey
internal void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
{
var hotkey = new HotkeyModel(hotkeyStr);
SetHotkey(hotkey, action);
}
public void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> 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);
}
}
/// <summary>
/// Checks if Wox should ignore any hotkeys
/// </summary>
/// <returns></returns>
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
public void Save()
{
2016-05-10 05:45:20 +08:00
if (!_saved)
{
_queryHistoryStorage.Save();
_userSelectedRecordStorage.Save();
_topMostRecordStorage.Save();
PluginManager.Save();
ImageLoader.Save();
_saved = true;
}
}
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </summary>
public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery)
{
_queryHasReturn = true;
2016-04-25 10:00:55 +08:00
ProgressBarVisibility = Visibility.Hidden;
foreach (var result in list)
{
if (_topMostRecord.IsTopMost(result))
{
result.Score = int.MaxValue;
}
else
{
result.Score += _userSelectedRecord.GetSelectedCount(result)*5;
}
}
if (originQuery.RawQuery == _lastQuery.RawQuery)
{
Results.AddResults(list, metadata.ID);
}
2016-04-25 10:00:55 +08:00
if (list.Count > 0 && !ResultListBoxVisibility.IsVisible())
{
2016-02-24 10:07:35 +08:00
ResultListBoxVisibility = Visibility.Visible;
}
}
#endregion
2016-02-27 07:43:57 +08:00
public event EventHandler MainWindowVisibilityChanged;
public event EventHandler CursorMovedToEnd;
2016-02-27 07:43:57 +08:00
public void OnCursorMovedToEnd()
{
CursorMovedToEnd?.Invoke(this, new EventArgs());
}
public event EventHandler TextBoxSelected;
2016-02-27 07:43:57 +08:00
public void OnTextBoxSelected()
{
TextBoxSelected?.Invoke(this, new EventArgs());
}
2016-02-12 17:20:46 +08:00
}
}