2016-02-23 05:43:37 +08:00
|
|
|
|
using System;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Windows;
|
2016-02-12 17:20:46 +08:00
|
|
|
|
using System.Windows.Input;
|
2016-02-23 05:43:37 +08:00
|
|
|
|
using NHotkey;
|
|
|
|
|
using NHotkey.Wpf;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
using Wox.Core.Plugin;
|
|
|
|
|
using Wox.Core.Resource;
|
|
|
|
|
using Wox.Core.UserSettings;
|
|
|
|
|
using Wox.Helper;
|
|
|
|
|
using Wox.Infrastructure.Hotkey;
|
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
using Wox.ViewModel;
|
|
|
|
|
|
|
|
|
|
namespace Wox
|
|
|
|
|
{
|
|
|
|
|
public class PublicAPIInstance : IPublicAPI
|
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
private Settings _settings;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
#region Constructor
|
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public PublicAPIInstance(MainViewModel mainVM, Settings settings)
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
MainVM = mainVM;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settings = settings;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
|
|
|
|
|
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
|
2016-03-28 10:09:57 +08:00
|
|
|
|
SetHotkey(_settings.Hotkey, OnHotkey);
|
|
|
|
|
SetCustomPluginHotkey();
|
|
|
|
|
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
2016-03-28 08:09:40 +08:00
|
|
|
|
public MainViewModel MainVM
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Public API
|
|
|
|
|
|
|
|
|
|
public void ChangeQuery(string query, bool requery = false)
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
MainVM.QueryText = query;
|
2016-02-27 07:43:57 +08:00
|
|
|
|
MainVM.OnCursorMovedToEnd();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeQueryText(string query, bool selectAll = false)
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
MainVM.QueryText = query;
|
2016-02-27 07:43:57 +08:00
|
|
|
|
MainVM.OnTextBoxSelected();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CloseApp()
|
|
|
|
|
{
|
|
|
|
|
//notifyIcon.Visible = false;
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RestarApp()
|
|
|
|
|
{
|
|
|
|
|
ProcessStartInfo info = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = Application.ResourceAssembly.Location,
|
|
|
|
|
Arguments = SingleInstance<App>.Restart
|
|
|
|
|
};
|
|
|
|
|
Process.Start(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HideApp()
|
|
|
|
|
{
|
|
|
|
|
HideWox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ShowApp()
|
|
|
|
|
{
|
|
|
|
|
ShowWox();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 09:22:24 +08:00
|
|
|
|
public void ShowMsg(string title, string subTitle = "", string iconPath = "")
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
var m = new Msg { Owner = Application.Current.MainWindow };
|
|
|
|
|
m.Show(title, subTitle, iconPath);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OpenSettingDialog(string tabName = "general")
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this, _settings);
|
2016-02-18 19:30:36 +08:00
|
|
|
|
sw.SwitchTo(tabName);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StartLoadingBar()
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
MainVM.ProgressBarVisibility = Visibility.Visible;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopLoadingBar()
|
|
|
|
|
{
|
2016-02-24 10:07:35 +08:00
|
|
|
|
MainVM.ProgressBarVisibility = Visibility.Collapsed;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InstallPlugin(string path)
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() => PluginManager.InstallPlugin(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReloadPlugins()
|
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
PluginManager.Initialize();
|
|
|
|
|
PluginManager.InitializePlugins(this);
|
|
|
|
|
});
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetTranslation(string key)
|
|
|
|
|
{
|
|
|
|
|
return InternationalizationManager.Instance.GetTranslation(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PluginPair> GetAllPlugins()
|
|
|
|
|
{
|
|
|
|
|
return PluginManager.AllPlugins.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event WoxGlobalKeyboardEventHandler GlobalKeyboardEvent;
|
|
|
|
|
|
2016-03-28 08:09:40 +08:00
|
|
|
|
[Obsolete("This will be removed in Wox 1.3")]
|
2016-02-18 19:30:36 +08:00
|
|
|
|
public void PushResults(Query query, PluginMetadata plugin, List<Result> results)
|
|
|
|
|
{
|
|
|
|
|
results.ForEach(o =>
|
|
|
|
|
{
|
|
|
|
|
o.PluginDirectory = plugin.PluginDirectory;
|
|
|
|
|
o.PluginID = plugin.ID;
|
|
|
|
|
o.OriginQuery = query;
|
|
|
|
|
});
|
2016-02-21 23:42:37 +08:00
|
|
|
|
MainVM.UpdateResultView(results, plugin, query);
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Private Methods
|
|
|
|
|
|
|
|
|
|
private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state)
|
|
|
|
|
{
|
|
|
|
|
if (GlobalKeyboardEvent != null)
|
|
|
|
|
{
|
|
|
|
|
return GlobalKeyboardEvent((int)keyevent, vkcode, state);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HideWox()
|
|
|
|
|
{
|
2016-02-26 20:05:32 +08:00
|
|
|
|
MainVM.MainWindowVisibility = Visibility.Collapsed;
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ShowWox(bool selectAll = true)
|
|
|
|
|
{
|
2016-02-26 20:05:32 +08:00
|
|
|
|
MainVM.MainWindowVisibility = Visibility.Visible;
|
2016-02-27 07:43:57 +08:00
|
|
|
|
MainVM.OnTextBoxSelected();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 09:22:24 +08:00
|
|
|
|
internal void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
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
|
2016-03-28 10:09:57 +08:00
|
|
|
|
if (_settings.IgnoreHotkeysOnFullscreen)
|
2016-02-18 19:30:36 +08:00
|
|
|
|
if (WindowIntelopHelper.IsWindowFullscreen())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 09:22:24 +08:00
|
|
|
|
internal void SetCustomPluginHotkey()
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
if (_settings.CustomPluginHotkeys == null) return;
|
|
|
|
|
foreach (CustomPluginHotkey hotkey in _settings.CustomPluginHotkeys)
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
CustomPluginHotkey hotkey1 = hotkey;
|
|
|
|
|
SetHotkey(hotkey.Hotkey, delegate
|
|
|
|
|
{
|
|
|
|
|
if (ShouldIgnoreHotkeys()) return;
|
|
|
|
|
ShowApp();
|
|
|
|
|
ChangeQuery(hotkey1.ActionKeyword, true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 09:22:24 +08:00
|
|
|
|
protected internal void OnHotkey(object sender, HotkeyEventArgs e)
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
if (ShouldIgnoreHotkeys()) return;
|
|
|
|
|
ToggleWox();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ToggleWox()
|
|
|
|
|
{
|
2016-02-26 20:05:32 +08:00
|
|
|
|
if (!MainVM.MainWindowVisibility.IsVisible())
|
2016-02-18 19:30:36 +08:00
|
|
|
|
{
|
|
|
|
|
ShowWox();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HideWox();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|