mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
Mouse select support & Code refactoring
This commit is contained in:
@@ -51,7 +51,16 @@ namespace WinAlfred
|
||||
|
||||
public partial class App : Application
|
||||
{
|
||||
private MainWindow window;
|
||||
|
||||
private static MainWindow window;
|
||||
|
||||
public static MainWindow Window
|
||||
{
|
||||
get
|
||||
{
|
||||
return window;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
|
||||
@@ -8,24 +8,11 @@ namespace WinAlfred.Commands
|
||||
{
|
||||
public abstract class BaseCommand
|
||||
{
|
||||
private MainWindow window;
|
||||
|
||||
public void Dispatch(Query query)
|
||||
{
|
||||
Dispatch(query, true);
|
||||
}
|
||||
|
||||
public abstract void Dispatch(Query query, bool updateView);
|
||||
|
||||
//TODO:Ugly, we should subscribe events here, instead of just use usercontrol as the parameter
|
||||
protected BaseCommand(MainWindow window)
|
||||
{
|
||||
this.window = window;
|
||||
}
|
||||
public abstract void Dispatch(Query query, bool updateView = true);
|
||||
|
||||
protected void UpdateResultView(List<Result> results)
|
||||
{
|
||||
window.OnUpdateResultView(results);
|
||||
App.Window.OnUpdateResultView(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WinAlfred.Helper;
|
||||
using WinAlfred.Plugin;
|
||||
|
||||
namespace WinAlfred.Commands
|
||||
{
|
||||
public class Command
|
||||
{
|
||||
private PluginCommand pluginCmd;
|
||||
private SystemCommand systemCmd;
|
||||
|
||||
public Command(MainWindow window)
|
||||
{
|
||||
pluginCmd = new PluginCommand(window);
|
||||
systemCmd = new SystemCommand(window);
|
||||
}
|
||||
|
||||
public void DispatchCommand(Query query)
|
||||
{
|
||||
systemCmd.Dispatch(query);
|
||||
pluginCmd.Dispatch(query);
|
||||
}
|
||||
|
||||
public void DispatchCommand(Query query,bool updateView)
|
||||
{
|
||||
systemCmd.Dispatch(query,updateView);
|
||||
pluginCmd.Dispatch(query,updateView);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
WinAlfred/Commands/CommandFactory.cs
Normal file
31
WinAlfred/Commands/CommandFactory.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WinAlfred.Helper;
|
||||
using WinAlfred.Plugin;
|
||||
|
||||
namespace WinAlfred.Commands
|
||||
{
|
||||
internal static class CommandFactory
|
||||
{
|
||||
private static PluginCommand pluginCmd;
|
||||
private static SystemCommand systemCmd;
|
||||
|
||||
public static void DispatchCommand(Query query, bool updateView = true)
|
||||
{
|
||||
//lazy init command instance.
|
||||
if (pluginCmd == null)
|
||||
{
|
||||
pluginCmd = new PluginCommand();
|
||||
}
|
||||
if (systemCmd == null)
|
||||
{
|
||||
systemCmd = new SystemCommand();
|
||||
}
|
||||
|
||||
systemCmd.Dispatch(query,updateView);
|
||||
pluginCmd.Dispatch(query,updateView);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,13 +14,7 @@ namespace WinAlfred.Commands
|
||||
private string currentPythonModulePath = string.Empty;
|
||||
private IntPtr GIL;
|
||||
|
||||
public PluginCommand(MainWindow mainWindow)
|
||||
: base(mainWindow)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void Dispatch(Query q,bool updateView)
|
||||
public override void Dispatch(Query q,bool updateView = true)
|
||||
{
|
||||
PluginPair thirdPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == q.ActionName);
|
||||
if (thirdPlugin != null && !string.IsNullOrEmpty(thirdPlugin.Metadata.ActionKeyword))
|
||||
|
||||
@@ -12,13 +12,12 @@ namespace WinAlfred.Commands
|
||||
{
|
||||
private List<PluginPair> systemPlugins;
|
||||
|
||||
public SystemCommand(MainWindow window)
|
||||
: base(window)
|
||||
public SystemCommand()
|
||||
{
|
||||
systemPlugins = Plugins.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.System).ToList();
|
||||
}
|
||||
|
||||
public override void Dispatch(Query query,bool updateView)
|
||||
public override void Dispatch(Query query,bool updateView = true)
|
||||
{
|
||||
foreach (PluginPair pair in systemPlugins)
|
||||
{
|
||||
|
||||
@@ -13,8 +13,22 @@ namespace WinAlfred.Helper
|
||||
private int hasAddedCount = 0;
|
||||
private Dictionary<string, int> dict = new Dictionary<string, int>();
|
||||
private string filePath = Directory.GetCurrentDirectory() + "\\selectedRecords.dat";
|
||||
private static readonly SelectedRecords instance = new SelectedRecords();
|
||||
|
||||
public void LoadSelectedRecords()
|
||||
private SelectedRecords()
|
||||
{
|
||||
LoadSelectedRecords();
|
||||
}
|
||||
|
||||
public static SelectedRecords Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadSelectedRecords()
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
|
||||
@@ -16,6 +16,7 @@ using WinAlfred.PluginLoader;
|
||||
using Application = System.Windows.Application;
|
||||
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
using UserControl = System.Windows.Controls.UserControl;
|
||||
|
||||
namespace WinAlfred
|
||||
{
|
||||
@@ -23,10 +24,8 @@ namespace WinAlfred
|
||||
{
|
||||
private KeyboardHook hook = new KeyboardHook();
|
||||
private NotifyIcon notifyIcon;
|
||||
private Command cmdDispatcher;
|
||||
Storyboard progressBarStoryboard = new Storyboard();
|
||||
private bool queryHasReturn = false;
|
||||
SelectedRecords selectedRecords = new SelectedRecords();
|
||||
|
||||
private KeyboardListener keyboardListener = new KeyboardListener();
|
||||
private bool WinRStroked = false;
|
||||
@@ -68,7 +67,7 @@ namespace WinAlfred
|
||||
double oldLeft = Left;
|
||||
Left = 20000;
|
||||
ShowWinAlfred();
|
||||
cmdDispatcher.DispatchCommand(new Query("qq"), false);
|
||||
CommandFactory.DispatchCommand(new Query("qq"), false);
|
||||
HideWinAlfred();
|
||||
Left = oldLeft;
|
||||
}
|
||||
@@ -132,7 +131,7 @@ namespace WinAlfred
|
||||
if (resultCtrl.Dirty) resultCtrl.Clear();
|
||||
}, TimeSpan.FromMilliseconds(30), null);
|
||||
var q = new Query(tbQuery.Text);
|
||||
cmdDispatcher.DispatchCommand(q);
|
||||
CommandFactory.DispatchCommand(q);
|
||||
queryHasReturn = false;
|
||||
if (Plugins.HitThirdpartyKeyword(q))
|
||||
{
|
||||
@@ -169,7 +168,7 @@ namespace WinAlfred
|
||||
Activate();
|
||||
Focus();
|
||||
tbQuery.Focus();
|
||||
if(selectAll) tbQuery.SelectAll();
|
||||
if (selectAll) tbQuery.SelectAll();
|
||||
}
|
||||
|
||||
public void ParseArgs(string[] args)
|
||||
@@ -179,7 +178,7 @@ namespace WinAlfred
|
||||
switch (args[0])
|
||||
{
|
||||
case "reloadWorkflows":
|
||||
Plugins.Init(this);
|
||||
Plugins.Init();
|
||||
break;
|
||||
|
||||
case "query":
|
||||
@@ -218,12 +217,11 @@ namespace WinAlfred
|
||||
Left = (SystemParameters.PrimaryScreenWidth - ActualWidth) / 2;
|
||||
Top = (SystemParameters.PrimaryScreenHeight - ActualHeight) / 3;
|
||||
|
||||
Plugins.Init(this);
|
||||
cmdDispatcher = new Command(this);
|
||||
Plugins.Init();
|
||||
InitialTray();
|
||||
selectedRecords.LoadSelectedRecords();
|
||||
SetAutoStart(true);
|
||||
WakeupApp();
|
||||
|
||||
//var engine = new Jurassic.ScriptEngine();
|
||||
//MessageBox.Show(engine.Evaluate("5 * 10 + 2").ToString());
|
||||
keyboardListener.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
|
||||
@@ -279,20 +277,25 @@ namespace WinAlfred
|
||||
break;
|
||||
|
||||
case Key.Enter:
|
||||
Result result = resultCtrl.AcceptSelect();
|
||||
if (result != null)
|
||||
{
|
||||
selectedRecords.AddSelect(result);
|
||||
if (!result.DontHideWinAlfredAfterSelect)
|
||||
{
|
||||
HideWinAlfred();
|
||||
}
|
||||
}
|
||||
AcceptSelect();
|
||||
e.Handled = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void AcceptSelect()
|
||||
{
|
||||
Result result = resultCtrl.AcceptSelect();
|
||||
if (result != null)
|
||||
{
|
||||
SelectedRecords.Instance.AddSelect(result);
|
||||
if (!result.DontHideWinAlfredAfterSelect)
|
||||
{
|
||||
HideWinAlfred();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnUpdateResultView(List<Result> list)
|
||||
{
|
||||
queryHasReturn = true;
|
||||
@@ -302,7 +305,7 @@ namespace WinAlfred
|
||||
//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 += selectedRecords.GetSelectedCount(o);
|
||||
if (o.AutoAjustScore) o.Score += SelectedRecords.Instance.GetSelectedCount(o);
|
||||
});
|
||||
resultCtrl.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace WinAlfred.PluginLoader
|
||||
{
|
||||
private static List<PluginPair> plugins = new List<PluginPair>();
|
||||
|
||||
public static void Init(MainWindow window)
|
||||
public static void Init()
|
||||
{
|
||||
plugins.Clear();
|
||||
BasePluginLoader.ParsePluginsConfig();
|
||||
@@ -30,12 +30,12 @@ namespace WinAlfred.PluginLoader
|
||||
{
|
||||
Plugins = plugins,
|
||||
PluginMetadata = metadata,
|
||||
ChangeQuery = s => window.ChangeQuery(s),
|
||||
CloseApp = window.CloseApp,
|
||||
HideApp = window.HideApp,
|
||||
ShowApp = () => window.ShowApp(),
|
||||
ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath),
|
||||
OpenSettingDialog = ()=> window.OpenSettingDialog()
|
||||
ChangeQuery = s => App.Window.ChangeQuery(s),
|
||||
CloseApp = App.Window.CloseApp,
|
||||
HideApp = App.Window.HideApp,
|
||||
ShowApp = () => App.Window.ShowApp(),
|
||||
ShowMsg = (title, subTitle, iconPath) => App.Window.ShowMsg(title, subTitle, iconPath),
|
||||
OpenSettingDialog = () => App.Window.OpenSettingDialog()
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
x:Name="resultItemControl"
|
||||
Style="{DynamicResource ItemStyle}"
|
||||
Height="50">
|
||||
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" >
|
||||
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" Cursor="Hand">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="32"></ColumnDefinition>
|
||||
<ColumnDefinition/>
|
||||
|
||||
@@ -4,9 +4,11 @@ using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using WinAlfred.Annotations;
|
||||
using WinAlfred.Helper;
|
||||
using WinAlfred.Plugin;
|
||||
using Brush = System.Windows.Media.Brush;
|
||||
|
||||
@@ -14,6 +16,7 @@ namespace WinAlfred
|
||||
{
|
||||
public partial class ResultItem : UserControl, INotifyPropertyChanged
|
||||
{
|
||||
|
||||
private bool selected;
|
||||
|
||||
public Result Result { get; private set; }
|
||||
@@ -47,7 +50,6 @@ namespace WinAlfred
|
||||
|
||||
public ResultItem(Result result)
|
||||
{
|
||||
|
||||
InitializeComponent();
|
||||
Result = result;
|
||||
|
||||
@@ -74,6 +76,17 @@ namespace WinAlfred
|
||||
imgIco.Source = new BitmapImage(new Uri(path));
|
||||
}
|
||||
}
|
||||
|
||||
AddHandler(MouseLeftButtonUpEvent, new RoutedEventHandler((o, e) =>
|
||||
{
|
||||
Result.Action();
|
||||
SelectedRecords.Instance.AddSelect(result);
|
||||
if (!result.DontHideWinAlfredAfterSelect)
|
||||
{
|
||||
App.Window.HideApp();
|
||||
}
|
||||
e.Handled = true;
|
||||
}));
|
||||
}
|
||||
|
||||
private static ImageSource GetIcon(string fileName)
|
||||
|
||||
@@ -111,7 +111,7 @@
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Commands\BaseCommand.cs" />
|
||||
<Compile Include="Commands\Command.cs" />
|
||||
<Compile Include="Commands\CommandFactory.cs" />
|
||||
<Compile Include="Commands\PluginCommand.cs" />
|
||||
<Compile Include="Commands\SystemCommand.cs" />
|
||||
<Compile Include="DispatcherExtensions.cs" />
|
||||
|
||||
Reference in New Issue
Block a user