mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Add hotkey setting
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
@@ -8,29 +9,33 @@ using System.Windows.Controls;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media.Animation;
|
||||
using WindowsInput;
|
||||
using WindowsInput.Native;
|
||||
using NHotkey;
|
||||
using NHotkey.Wpf;
|
||||
using Wox.Commands;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Plugin;
|
||||
using Wox.PluginLoader;
|
||||
using Application = System.Windows.Application;
|
||||
using ContextMenu = System.Windows.Forms.ContextMenu;
|
||||
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
||||
using MenuItem = System.Windows.Forms.MenuItem;
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
public partial class MainWindow
|
||||
{
|
||||
private static readonly object locker = new object();
|
||||
private readonly GloablHotkey globalHotkey = new GloablHotkey();
|
||||
|
||||
private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator());
|
||||
private readonly Storyboard progressBarStoryboard = new Storyboard();
|
||||
private bool WinRStroked;
|
||||
private NotifyIcon notifyIcon;
|
||||
Storyboard progressBarStoryboard = new Storyboard();
|
||||
private bool queryHasReturn = false;
|
||||
|
||||
private GloablHotkey globalHotkey = new GloablHotkey();
|
||||
private bool WinRStroked = false;
|
||||
private static object locker = new object();
|
||||
|
||||
private WindowsInput.KeyboardSimulator keyboardSimulator = new WindowsInput.KeyboardSimulator(new WindowsInput.InputSimulator());
|
||||
private bool queryHasReturn;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
@@ -49,8 +54,22 @@ namespace Wox
|
||||
SetTheme(CommonStorage.Instance.UserSetting.Theme = "Default");
|
||||
}
|
||||
|
||||
HotkeyManager.Current.AddOrReplace("ShowHideWox", Key.W, ModifierKeys.Windows, OnHotkey);
|
||||
this.Closing += MainWindow_Closing;
|
||||
|
||||
Closing += MainWindow_Closing;
|
||||
}
|
||||
|
||||
public void SetHotkey(string hotkeyStr)
|
||||
{
|
||||
HotkeyModel hotkey = new HotkeyModel(hotkeyStr);
|
||||
try
|
||||
{
|
||||
HotkeyManager.Current.AddOrReplace("ShowHideWox", hotkey.CharKey, hotkey.ModifierKeys, OnHotkey);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
MessageBox.Show("Registe hotkey: " + CommonStorage.Instance.UserSetting.Hotkey + " failed.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnHotkey(object sender, HotkeyEventArgs e)
|
||||
@@ -66,7 +85,7 @@ namespace Wox
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
||||
private void MainWindow_Closing(object sender, CancelEventArgs e)
|
||||
{
|
||||
e.Cancel = true;
|
||||
}
|
||||
@@ -77,25 +96,25 @@ namespace Wox
|
||||
//This is caused by the Virtual Mermory Page Mechanisam. So, our solution is execute some codes in every min
|
||||
//which may prevent sysetem uninstall memory from RAM to disk.
|
||||
|
||||
System.Timers.Timer t = new System.Timers.Timer(1000 * 60 * 5) { AutoReset = true, Enabled = true };
|
||||
var t = new Timer(1000*60*5) {AutoReset = true, Enabled = true};
|
||||
t.Elapsed += (o, e) => Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
if (Visibility != Visibility.Visible)
|
||||
{
|
||||
if (Visibility != Visibility.Visible)
|
||||
{
|
||||
double oldLeft = Left;
|
||||
Left = 20000;
|
||||
ShowWox();
|
||||
CommandFactory.DispatchCommand(new Query("qq"), false);
|
||||
HideWox();
|
||||
Left = oldLeft;
|
||||
}
|
||||
}));
|
||||
double oldLeft = Left;
|
||||
Left = 20000;
|
||||
ShowWox();
|
||||
CommandFactory.DispatchCommand(new Query("qq"), false);
|
||||
HideWox();
|
||||
Left = oldLeft;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private void InitProgressbarAnimation()
|
||||
{
|
||||
DoubleAnimation da = new DoubleAnimation(progressBar.X2, Width + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
|
||||
DoubleAnimation da1 = new DoubleAnimation(progressBar.X1, Width, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
|
||||
var da = new DoubleAnimation(progressBar.X2, Width + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
|
||||
var da1 = new DoubleAnimation(progressBar.X1, Width, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
|
||||
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)"));
|
||||
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)"));
|
||||
progressBarStoryboard.Children.Add(da);
|
||||
@@ -107,50 +126,51 @@ namespace Wox
|
||||
|
||||
private void InitialTray()
|
||||
{
|
||||
notifyIcon = new NotifyIcon { Text = "Wox", Icon = Properties.Resources.app, Visible = true };
|
||||
notifyIcon = new NotifyIcon {Text = "Wox", Icon = Properties.Resources.app, Visible = true};
|
||||
notifyIcon.Click += (o, e) => ShowWox();
|
||||
System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
|
||||
var open = new MenuItem("Open");
|
||||
open.Click += (o, e) => ShowWox();
|
||||
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
|
||||
var exit = new MenuItem("Exit");
|
||||
exit.Click += (o, e) => CloseApp();
|
||||
System.Windows.Forms.MenuItem[] childen = { open, exit };
|
||||
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
|
||||
MenuItem[] childen = {open, exit};
|
||||
notifyIcon.ContextMenu = new ContextMenu(childen);
|
||||
}
|
||||
|
||||
private void resultCtrl_resultItemChangedEvent()
|
||||
{
|
||||
resultCtrl.Margin = resultCtrl.GetCurrentResultCount() > 0 ? new Thickness { Top = grid.Margin.Top } : new Thickness { Top = 0 };
|
||||
resultCtrl.Margin = resultCtrl.GetCurrentResultCount() > 0
|
||||
? new Thickness {Top = grid.Margin.Top}
|
||||
: new Thickness {Top = 0};
|
||||
}
|
||||
|
||||
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
resultCtrl.Dirty = true;
|
||||
Dispatcher.DelayInvoke("UpdateSearch",
|
||||
o =>
|
||||
{
|
||||
Dispatcher.DelayInvoke("ClearResults", i =>
|
||||
{
|
||||
// first try to use clear method inside resultCtrl, which is more closer to the add new results
|
||||
// and this will not bring splash issues.After waiting 30ms, if there still no results added, we
|
||||
// must clear the result. otherwise, it will be confused why the query changed, but the results
|
||||
// didn't.
|
||||
if (resultCtrl.Dirty) resultCtrl.Clear();
|
||||
}, TimeSpan.FromMilliseconds(30), null);
|
||||
var q = new Query(tbQuery.Text);
|
||||
CommandFactory.DispatchCommand(q);
|
||||
queryHasReturn = false;
|
||||
if (Plugins.HitThirdpartyKeyword(q))
|
||||
{
|
||||
Dispatcher.DelayInvoke("ShowProgressbar", originQuery =>
|
||||
{
|
||||
if (!queryHasReturn && originQuery == tbQuery.Text)
|
||||
{
|
||||
StartProgress();
|
||||
}
|
||||
}, TimeSpan.FromSeconds(1), tbQuery.Text);
|
||||
}
|
||||
|
||||
}, TimeSpan.FromMilliseconds(150));
|
||||
o =>
|
||||
{
|
||||
Dispatcher.DelayInvoke("ClearResults", i =>
|
||||
{
|
||||
// first try to use clear method inside resultCtrl, which is more closer to the add new results
|
||||
// and this will not bring splash issues.After waiting 30ms, if there still no results added, we
|
||||
// must clear the result. otherwise, it will be confused why the query changed, but the results
|
||||
// didn't.
|
||||
if (resultCtrl.Dirty) resultCtrl.Clear();
|
||||
}, TimeSpan.FromMilliseconds(30), null);
|
||||
var q = new Query(tbQuery.Text);
|
||||
CommandFactory.DispatchCommand(q);
|
||||
queryHasReturn = false;
|
||||
if (Plugins.HitThirdpartyKeyword(q))
|
||||
{
|
||||
Dispatcher.DelayInvoke("ShowProgressbar", originQuery =>
|
||||
{
|
||||
if (!queryHasReturn && originQuery == tbQuery.Text)
|
||||
{
|
||||
StartProgress();
|
||||
}
|
||||
}, TimeSpan.FromSeconds(1), tbQuery.Text);
|
||||
}
|
||||
}, TimeSpan.FromMilliseconds(150));
|
||||
}
|
||||
|
||||
private void StartProgress()
|
||||
@@ -205,9 +225,10 @@ namespace Wox
|
||||
|
||||
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Left = (SystemParameters.PrimaryScreenWidth - ActualWidth) / 2;
|
||||
Top = (SystemParameters.PrimaryScreenHeight - ActualHeight) / 3;
|
||||
Left = (SystemParameters.PrimaryScreenWidth - ActualWidth)/2;
|
||||
Top = (SystemParameters.PrimaryScreenHeight - ActualHeight)/3;
|
||||
|
||||
SetHotkey(CommonStorage.Instance.UserSetting.Hotkey);
|
||||
WakeupApp();
|
||||
Plugins.Init();
|
||||
|
||||
@@ -219,16 +240,16 @@ namespace Wox
|
||||
if (CommonStorage.Instance.UserSetting.ReplaceWinR)
|
||||
{
|
||||
//todo:need refatoring. move those codes to CMD file or expose events
|
||||
if (keyevent == KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed)
|
||||
if (keyevent == KeyEvent.WM_KEYDOWN && vkcode == (int) Keys.R && state.WinPressed)
|
||||
{
|
||||
WinRStroked = true;
|
||||
Dispatcher.BeginInvoke(new Action(OnWinRPressed));
|
||||
return false;
|
||||
}
|
||||
if (keyevent == KeyEvent.WM_KEYUP && WinRStroked && vkcode == (int)Keys.LWin)
|
||||
if (keyevent == KeyEvent.WM_KEYUP && WinRStroked && vkcode == (int) Keys.LWin)
|
||||
{
|
||||
WinRStroked = false;
|
||||
keyboardSimulator.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.LWIN, WindowsInput.Native.VirtualKeyCode.CONTROL);
|
||||
keyboardSimulator.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.CONTROL);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -294,15 +315,17 @@ namespace Wox
|
||||
if (list.Count > 0)
|
||||
{
|
||||
//todo:this should be opened to users, it's their choise to use it or not in thier workflows
|
||||
list.ForEach(o =>
|
||||
{
|
||||
if (o.AutoAjustScore) o.Score += CommonStorage.Instance.UserSelectedRecords.GetSelectedCount(o);
|
||||
});
|
||||
list.ForEach(
|
||||
o =>
|
||||
{
|
||||
if (o.AutoAjustScore) o.Score += CommonStorage.Instance.UserSelectedRecords.GetSelectedCount(o);
|
||||
});
|
||||
lock (locker)
|
||||
{
|
||||
resultCtrl.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).ToList();
|
||||
List<Result> l =
|
||||
list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).ToList();
|
||||
resultCtrl.AddResults(list);
|
||||
}));
|
||||
}
|
||||
@@ -311,7 +334,7 @@ namespace Wox
|
||||
|
||||
public void SetTheme(string themeName)
|
||||
{
|
||||
ResourceDictionary dict = new ResourceDictionary
|
||||
var dict = new ResourceDictionary
|
||||
{
|
||||
Source = new Uri("pack://application:,,,/Themes/" + themeName + ".xaml")
|
||||
};
|
||||
@@ -348,13 +371,13 @@ namespace Wox
|
||||
|
||||
public void ShowMsg(string title, string subTitle, string iconPath)
|
||||
{
|
||||
Msg m = new Msg { Owner = GetWindow(this) };
|
||||
var m = new Msg {Owner = GetWindow(this)};
|
||||
m.Show(title, subTitle, iconPath);
|
||||
}
|
||||
|
||||
public void OpenSettingDialog()
|
||||
{
|
||||
SettingWidow s = new SettingWidow(this);
|
||||
var s = new SettingWidow(this);
|
||||
s.Show();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user