PowerToys/Wox/PublicAPIInstance.cs

176 lines
4.6 KiB
C#
Raw Normal View History

2016-02-23 05:43:37 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
2016-02-23 05:43:37 +08:00
using NHotkey;
using NHotkey.Wpf;
using Squirrel;
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;
2016-05-22 06:42:23 +08:00
using static Wox.ViewModel.SettingWindowViewModel;
namespace Wox
{
public class PublicAPIInstance : IPublicAPI
{
2016-05-22 05:44:27 +08:00
private readonly SettingWindowViewModel _settingsViewModel;
#region Constructor
2016-05-22 05:44:27 +08:00
public PublicAPIInstance(SettingWindowViewModel settingsViewModel, MainViewModel mainVM)
{
2016-05-22 05:44:27 +08:00
_settingsViewModel = settingsViewModel;
MainVM = mainVM;
//_settings = settings;
GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
}
#endregion
#region Properties
public MainViewModel MainVM
{
get;
set;
}
#endregion
#region Public API
public void ChangeQuery(string query, bool requery = false)
{
MainVM.QueryText = query;
2016-02-27 07:43:57 +08:00
MainVM.OnCursorMovedToEnd();
}
public void ChangeQueryText(string query, bool selectAll = false)
{
MainVM.QueryText = query;
2016-02-27 07:43:57 +08:00
MainVM.OnTextBoxSelected();
}
public void CloseApp()
{
//notifyIcon.Visible = false;
2016-05-10 05:45:20 +08:00
Application.Current.MainWindow.Close();
}
public void RestarApp()
{
2016-05-09 05:26:46 +08:00
HideWox();
2016-05-10 05:45:20 +08:00
// we must manually save
2016-05-09 00:47:28 +08:00
// UpdateManager.RestartApp() will call Environment.Exit(0)
// which will cause ungraceful exit
2016-05-10 05:45:20 +08:00
var vm = (MainViewModel) Application.Current.MainWindow.DataContext;
vm.Save();
UpdateManager.RestartApp();
}
public void HideApp()
{
HideWox();
}
public void ShowApp()
{
ShowWox();
}
public void ShowMsg(string title, string subTitle = "", string iconPath = "")
{
Application.Current.Dispatcher.Invoke(() =>
{
var m = new Msg { Owner = Application.Current.MainWindow };
m.Show(title, subTitle, iconPath);
});
}
2016-05-23 02:16:39 +08:00
public void OpenSettingDialog()
{
Application.Current.Dispatcher.Invoke(() =>
{
2016-05-22 05:44:27 +08:00
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this, _settingsViewModel);
});
}
public void StartLoadingBar()
{
2016-02-24 10:07:35 +08:00
MainVM.ProgressBarVisibility = Visibility.Visible;
}
public void StopLoadingBar()
{
2016-02-24 10:07:35 +08:00
MainVM.ProgressBarVisibility = Visibility.Collapsed;
}
public void InstallPlugin(string path)
{
Application.Current.Dispatcher.Invoke(() => PluginManager.InstallPlugin(path));
}
public string GetTranslation(string key)
{
return InternationalizationManager.Instance.GetTranslation(key);
}
public List<PluginPair> GetAllPlugins()
{
return PluginManager.AllPlugins.ToList();
}
public event WoxGlobalKeyboardEventHandler GlobalKeyboardEvent;
[Obsolete("This will be removed in Wox 1.3")]
public void PushResults(Query query, PluginMetadata plugin, List<Result> results)
{
results.ForEach(o =>
{
o.PluginDirectory = plugin.PluginDirectory;
o.PluginID = plugin.ID;
o.OriginQuery = query;
});
Task.Run(() =>
{
MainVM.UpdateResultView(results, plugin, query);
});
}
#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;
}
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();
}
#endregion
}
}