2016-02-18 19:30:36 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-02-12 14:21:12 +08:00
|
|
|
|
using System.Diagnostics;
|
2016-03-28 08:09:40 +08:00
|
|
|
|
using System.Threading;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using Wox.Core.Plugin;
|
|
|
|
|
using Wox.Core.Resource;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
using Wox.Core.Updater;
|
2016-03-28 08:09:40 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
2016-02-26 20:05:32 +08:00
|
|
|
|
using Wox.Helper;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2016-03-26 09:20:42 +08:00
|
|
|
|
using Wox.Infrastructure.Hotkey;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
using Wox.Infrastructure.Storage;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
using Wox.Storage;
|
2016-02-23 05:43:37 +08:00
|
|
|
|
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
|
|
|
|
|
namespace Wox.ViewModel
|
|
|
|
|
{
|
|
|
|
|
public class MainViewModel : BaseViewModel
|
|
|
|
|
{
|
|
|
|
|
#region Private Fields
|
|
|
|
|
|
|
|
|
|
private bool _isProgressBarTooltipVisible;
|
|
|
|
|
private double _left;
|
|
|
|
|
private double _top;
|
|
|
|
|
|
2016-02-22 05:27:05 +08:00
|
|
|
|
private Visibility _contextMenuVisibility;
|
|
|
|
|
private Visibility _progressBarVisibility;
|
|
|
|
|
private Visibility _resultListBoxVisibility;
|
2016-02-26 20:05:32 +08:00
|
|
|
|
private Visibility _mainWindowVisibility;
|
2016-02-22 05:27:05 +08:00
|
|
|
|
|
2016-02-18 19:30:36 +08:00
|
|
|
|
private bool _queryHasReturn;
|
2016-03-26 09:20:42 +08:00
|
|
|
|
private Query _lastQuery;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
private bool _ignoreTextChange;
|
2016-03-26 09:20:42 +08:00
|
|
|
|
private string _queryTextBeforeLoadContextMenu;
|
|
|
|
|
private string _queryText;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
private readonly JsonStrorage<Settings> _settingsStorage;
|
|
|
|
|
private readonly JsonStrorage<QueryHistory> _queryHistoryStorage;
|
|
|
|
|
private readonly JsonStrorage<UserSelectedRecord> _userSelectedRecordStorage;
|
|
|
|
|
private readonly JsonStrorage<TopMostRecord> _topMostRecordStorage;
|
|
|
|
|
// todo happlebao this field should be private in the future
|
|
|
|
|
public readonly Settings _settings;
|
|
|
|
|
private readonly QueryHistory _queryHistory;
|
|
|
|
|
private readonly UserSelectedRecord _userSelectedRecord;
|
|
|
|
|
private readonly TopMostRecord _topMostRecord;
|
2016-03-28 08:09:40 +08:00
|
|
|
|
|
2016-02-18 19:30:36 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructor
|
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public MainViewModel()
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
_queryTextBeforeLoadContextMenu = "";
|
|
|
|
|
_queryText = "";
|
|
|
|
|
_lastQuery = new Query();
|
2016-04-21 08:53:21 +08:00
|
|
|
|
|
|
|
|
|
_settingsStorage = new JsonStrorage<Settings>();
|
|
|
|
|
_settings = _settingsStorage.Load();
|
|
|
|
|
|
|
|
|
|
// happlebao todo temp fix for instance code logic
|
|
|
|
|
HttpProxy.Instance.Settings = _settings;
|
|
|
|
|
UpdaterManager.Instance.Settings = _settings;
|
|
|
|
|
InternationalizationManager.Instance.Settings = _settings;
|
|
|
|
|
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();
|
2016-03-26 09:20:42 +08:00
|
|
|
|
|
2016-02-21 23:42:37 +08:00
|
|
|
|
InitializeResultListBox();
|
|
|
|
|
InitializeContextMenu();
|
|
|
|
|
InitializeKeyCommands();
|
2016-03-28 10:09:57 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
~MainViewModel()
|
2016-03-28 10:09:57 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
_settingsStorage.Save();
|
|
|
|
|
_queryHistoryStorage.Save();
|
|
|
|
|
_userSelectedRecordStorage.Save();
|
|
|
|
|
_topMostRecordStorage.Save();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ViewModel Properties
|
|
|
|
|
|
2016-02-21 22:33:18 +08:00
|
|
|
|
public ResultsViewModel Results { get; private set; }
|
2016-02-18 19:30:36 +08:00
|
|
|
|
|
2016-02-21 22:40:10 +08:00
|
|
|
|
public ResultsViewModel ContextMenu { get; private set; }
|
2016-02-18 19:30:36 +08:00
|
|
|
|
|
|
|
|
|
public string QueryText
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
return _queryText;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
_queryText = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-03-26 09:20:42 +08:00
|
|
|
|
|
|
|
|
|
if (_ignoreTextChange)
|
|
|
|
|
{
|
|
|
|
|
_ignoreTextChange = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HandleQueryTextUpdated();
|
|
|
|
|
}
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 05:27:05 +08:00
|
|
|
|
public bool IsProgressBarTooltipVisible
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
return _isProgressBarTooltipVisible;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
_isProgressBarTooltipVisible = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 05:27:05 +08:00
|
|
|
|
public double Left
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
return _left;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
_left = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 05:27:05 +08:00
|
|
|
|
public double Top
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
return _top;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
_top = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 05:27:05 +08:00
|
|
|
|
public Visibility ContextMenuVisibility
|
2016-03-26 09:20:42 +08:00
|
|
|
|
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
return _contextMenuVisibility;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
_contextMenuVisibility = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-03-26 09:20:42 +08:00
|
|
|
|
|
2016-03-26 11:15:28 +08:00
|
|
|
|
_ignoreTextChange = true;
|
2016-03-26 09:20:42 +08:00
|
|
|
|
if (!value.IsVisible())
|
|
|
|
|
{
|
|
|
|
|
QueryText = _queryTextBeforeLoadContextMenu;
|
|
|
|
|
ResultListBoxVisibility = Visibility.Visible;
|
|
|
|
|
OnCursorMovedToEnd();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_queryTextBeforeLoadContextMenu = QueryText;
|
|
|
|
|
QueryText = "";
|
|
|
|
|
ResultListBoxVisibility = Visibility.Collapsed;
|
|
|
|
|
}
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 05:27:05 +08:00
|
|
|
|
public Visibility ProgressBarVisibility
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
return _progressBarVisibility;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
_progressBarVisibility = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 05:27:05 +08:00
|
|
|
|
public Visibility ResultListBoxVisibility
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
return _resultListBoxVisibility;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
_resultListBoxVisibility = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-26 20:05:32 +08:00
|
|
|
|
public Visibility MainWindowVisibility
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-26 20:05:32 +08:00
|
|
|
|
return _mainWindowVisibility;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-26 20:05:32 +08:00
|
|
|
|
_mainWindowVisibility = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-02-27 07:43:57 +08:00
|
|
|
|
MainWindowVisibilityChanged?.Invoke(this, new EventArgs());
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-21 23:42:37 +08:00
|
|
|
|
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; }
|
2016-03-26 09:20:42 +08:00
|
|
|
|
public ICommand LoadContextMenuCommand { get; set; }
|
2016-02-21 23:42:37 +08:00
|
|
|
|
public ICommand OpenResultCommand { get; set; }
|
|
|
|
|
public ICommand BackCommand { get; set; }
|
2016-02-18 19:30:36 +08:00
|
|
|
|
#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())
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
ContextMenuVisibility = Visibility.Collapsed;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-02-26 20:05:32 +08:00
|
|
|
|
MainWindowVisibility = Visibility.Collapsed;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
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
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
ContextMenu.SelectNextResult();
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
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
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
ContextMenu.SelectPrevResult();
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Results.SelectPrevResult();
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-03-26 09:20:42 +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
|
|
|
|
{
|
2016-03-28 08:09:40 +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
|
|
|
|
{
|
2016-03-28 08:09:40 +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
|
|
|
|
{
|
2016-02-21 23:42:37 +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
|
|
|
|
{
|
2016-02-21 23:42:37 +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-03-26 09:20:42 +08:00
|
|
|
|
OpenResultCommand = new RelayCommand(o =>
|
2016-02-12 17:20:46 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
var results = ContextMenuVisibility.IsVisible() ? ContextMenu : Results;
|
|
|
|
|
|
|
|
|
|
if (o != null)
|
|
|
|
|
{
|
|
|
|
|
var index = int.Parse(o.ToString());
|
|
|
|
|
results.SelectResult(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = results.SelectedResult.RawResult;
|
|
|
|
|
bool hideWindow = result.Action(new ActionContext
|
2016-02-12 14:21:12 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
SpecialKeyState = GlobalHotkey.Instance.CheckModifiers()
|
|
|
|
|
});
|
|
|
|
|
if (hideWindow)
|
|
|
|
|
{
|
|
|
|
|
MainWindowVisibility = Visibility.Collapsed;
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
2016-03-26 09:20:42 +08:00
|
|
|
|
|
2016-03-28 08:09:40 +08:00
|
|
|
|
_userSelectedRecord.Add(result);
|
|
|
|
|
_queryHistory.Add(result.OriginQuery.RawQuery);
|
2016-02-12 14:21:12 +08:00
|
|
|
|
});
|
|
|
|
|
|
2016-03-26 09:20:42 +08:00
|
|
|
|
LoadContextMenuCommand = new RelayCommand(_ =>
|
2016-02-12 17:20:46 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
if (!ContextMenuVisibility.IsVisible())
|
2016-02-12 14:21:12 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
var result = Results.SelectedResult.RawResult;
|
|
|
|
|
var pluginID = result.PluginID;
|
|
|
|
|
|
|
|
|
|
var contextMenuResults = PluginManager.GetContextMenusForPlugin(result);
|
|
|
|
|
contextMenuResults.Add(GetTopMostContextMenu(result));
|
|
|
|
|
|
|
|
|
|
ContextMenu.Clear();
|
|
|
|
|
ContextMenu.AddResults(contextMenuResults, pluginID);
|
|
|
|
|
ContextMenuVisibility = Visibility.Visible;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ContextMenuVisibility = Visibility.Collapsed;
|
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
|
|
|
|
});
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-21 22:22:34 +08:00
|
|
|
|
private void InitializeResultListBox()
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
Results = new ResultsViewModel(_settings, _topMostRecord);
|
2016-02-24 10:07:35 +08:00
|
|
|
|
ResultListBoxVisibility = Visibility.Collapsed;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-02-21 22:40:10 +08:00
|
|
|
|
private void InitializeContextMenu()
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
ContextMenu = new ResultsViewModel(_settings, _topMostRecord);
|
2016-02-24 10:07:35 +08:00
|
|
|
|
ContextMenuVisibility = Visibility.Collapsed;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleQueryTextUpdated()
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
IsProgressBarTooltipVisible = false;
|
2016-02-24 10:07:35 +08:00
|
|
|
|
if (ContextMenuVisibility.IsVisible())
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
2016-02-21 22:40:10 +08:00
|
|
|
|
QueryContextMenu();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
string query = QueryText.Trim();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(query))
|
|
|
|
|
{
|
|
|
|
|
Query(query);
|
|
|
|
|
//reset query history index after user start new query
|
|
|
|
|
ResetQueryHistoryIndex();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Results.Clear();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-21 22:40:10 +08:00
|
|
|
|
private void QueryContextMenu()
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
var contextMenuId = "Context Menu Id";
|
2016-02-21 23:42:37 +08:00
|
|
|
|
var query = QueryText.ToLower();
|
2016-03-26 09:20:42 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(query))
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
|
2016-02-18 19:30:36 +08:00
|
|
|
|
List<Result> filterResults = new List<Result>();
|
2016-03-26 09:20:42 +08:00
|
|
|
|
foreach (var contextMenu in ContextMenu.Results)
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
if (StringMatcher.IsMatch(contextMenu.Title, query)
|
|
|
|
|
|| StringMatcher.IsMatch(contextMenu.SubTitle, query))
|
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
filterResults.Add(contextMenu.RawResult);
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-26 09:20:42 +08:00
|
|
|
|
ContextMenu.Clear();
|
2016-02-21 23:42:37 +08:00
|
|
|
|
ContextMenu.AddResults(filterResults, contextMenuId);
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(keyword))
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Results.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata);
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
else if (lastKeyword != keyword)
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_lastQuery = query;
|
|
|
|
|
|
2016-02-23 05:43:37 +08:00
|
|
|
|
Action action = async () =>
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
await Task.Delay(150);
|
|
|
|
|
if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
IsProgressBarTooltipVisible = true;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
2016-02-23 05:43:37 +08:00
|
|
|
|
};
|
2016-02-18 19:30:36 +08:00
|
|
|
|
action.Invoke();
|
2016-03-28 08:09:40 +08:00
|
|
|
|
var plugins = PluginManager.ValidPluginsForQuery(query);
|
|
|
|
|
foreach (var plugin in plugins)
|
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
var config = _settings.PluginSettings[plugin.Metadata.ID];
|
2016-03-28 08:09:40 +08:00
|
|
|
|
if (!config.Disabled)
|
|
|
|
|
{
|
|
|
|
|
ThreadPool.QueueUserWorkItem(o =>
|
|
|
|
|
{
|
|
|
|
|
var results = PluginManager.QueryForPlugin(plugin, query);
|
|
|
|
|
UpdateResultView(results, plugin.Metadata, query);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-21 23:42:37 +08:00
|
|
|
|
IsProgressBarTooltipVisible = false;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResetQueryHistoryIndex()
|
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
Results.RemoveResultsFor(QueryHistory.MetaData);
|
2016-03-28 08:09:40 +08:00
|
|
|
|
_queryHistory.Reset();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateResultViewInternal(List<Result> list, PluginMetadata metadata)
|
|
|
|
|
{
|
2016-02-23 05:43:37 +08:00
|
|
|
|
Stopwatch.Normal($"UI update cost for {metadata.Name}",
|
2016-02-21 23:42:37 +08:00
|
|
|
|
() => { Results.AddResults(list, metadata.ID); });
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 14:21:12 +08:00
|
|
|
|
private void DisplayQueryHistory(HistoryItem history)
|
|
|
|
|
{
|
|
|
|
|
if (history != null)
|
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
var historyMetadata = QueryHistory.MetaData;
|
2016-02-12 14:21:12 +08:00
|
|
|
|
|
2016-02-21 23:42:37 +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");
|
2016-02-21 23:42:37 +08:00
|
|
|
|
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 = _ =>{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
QueryText = history.Query;
|
2016-02-27 07:43:57 +08:00
|
|
|
|
OnTextBoxSelected();
|
2016-02-12 14:21:12 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, historyMetadata);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-26 09:20:42 +08:00
|
|
|
|
private Result GetTopMostContextMenu(Result result)
|
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
if (_topMostRecord.IsTopMost(result))
|
2016-03-26 09:20:42 +08:00
|
|
|
|
{
|
|
|
|
|
return new Result(InternationalizationManager.Instance.GetTranslation("cancelTopMostInThisQuery"), "Images\\down.png")
|
|
|
|
|
{
|
|
|
|
|
PluginDirectory = WoxDirectroy.Executable,
|
|
|
|
|
Action = _ =>
|
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
_topMostRecord.Remove(result);
|
2016-03-26 09:20:42 +08:00
|
|
|
|
App.API.ShowMsg("Succeed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new Result(InternationalizationManager.Instance.GetTranslation("setAsTopMostInThisQuery"), "Images\\up.png")
|
|
|
|
|
{
|
|
|
|
|
PluginDirectory = WoxDirectroy.Executable,
|
|
|
|
|
Action = _ =>
|
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
_topMostRecord.AddOrUpdate(result);
|
2016-03-26 09:20:42 +08:00
|
|
|
|
App.API.ShowMsg("Succeed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-18 19:30:36 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Public Methods
|
|
|
|
|
|
|
|
|
|
public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery)
|
|
|
|
|
{
|
|
|
|
|
_queryHasReturn = true;
|
2016-02-21 23:42:37 +08:00
|
|
|
|
IsProgressBarTooltipVisible = false;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
|
|
|
|
|
list.ForEach(o =>
|
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
o.Score += _userSelectedRecord.GetSelectedCount(o) * 5;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
{
|
2016-02-18 19:30:36 +08:00
|
|
|
|
UpdateResultViewInternal(list, metadata);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 17:20:46 +08:00
|
|
|
|
if (list.Count > 0)
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
ResultListBoxVisibility = Visibility.Visible;
|
2016-02-18 19:30:36 +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-03-24 04:31:59 +08:00
|
|
|
|
public KeyEventArgs KeyEventArgs { get; private set; }
|
2016-02-12 17:20:46 +08:00
|
|
|
|
|
2016-02-23 05:43:37 +08:00
|
|
|
|
public ListeningKeyPressedEventArgs(KeyEventArgs keyEventArgs)
|
2016-02-12 17:20:46 +08:00
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
KeyEventArgs = keyEventArgs;
|
2016-02-12 17:20:46 +08:00
|
|
|
|
}
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|