mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +01:00
Add system commands
This commit is contained in:
31
WinAlfred/Commands/CommandDispatcher.cs
Normal file
31
WinAlfred/Commands/CommandDispatcher.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
|
||||
{
|
||||
public class CommandDispatcher
|
||||
{
|
||||
private PluginCommand pluginCmd = new PluginCommand();
|
||||
private SystemCommand systemCmd = new SystemCommand();
|
||||
|
||||
//public delegate void resultUpdateDelegate(List<Result> results);
|
||||
|
||||
//public event resultUpdateDelegate OnResultUpdateEvent;
|
||||
|
||||
//protected virtual void OnOnResultUpdateEvent(List<Result> list)
|
||||
//{
|
||||
// resultUpdateDelegate handler = OnResultUpdateEvent;
|
||||
// if (handler != null) handler(list);
|
||||
//}
|
||||
|
||||
public void DispatchCommand(Query query)
|
||||
{
|
||||
systemCmd.Dispatch(query);
|
||||
pluginCmd.Dispatch(query);
|
||||
}
|
||||
}
|
||||
}
|
||||
45
WinAlfred/Commands/PluginCommand.cs
Normal file
45
WinAlfred/Commands/PluginCommand.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WinAlfred.Helper;
|
||||
using WinAlfred.Plugin;
|
||||
using WinAlfred.PluginLoader;
|
||||
|
||||
namespace WinAlfred.Commands
|
||||
{
|
||||
public class PluginCommand
|
||||
{
|
||||
public void Dispatch(Query q)
|
||||
{
|
||||
//ThreadPool.QueueUserWorkItem(state =>
|
||||
//{
|
||||
foreach (PluginPair pair in Plugins.AllPlugins)
|
||||
{
|
||||
if (pair.Metadata.ActionKeyword == q.ActionName)
|
||||
{
|
||||
try
|
||||
{
|
||||
pair.Plugin.Query(q).ForEach(o => o.PluginDirectory = pair.Metadata.PluginDirecotry);
|
||||
}
|
||||
catch (Exception queryException)
|
||||
{
|
||||
Log.Error(string.Format("Plugin {0} query failed: {1}", pair.Metadata.Name,
|
||||
queryException.Message));
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
resultCtrl.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
resultCtrl.AddResults(results.OrderByDescending(o => o.Score).ToList());
|
||||
resultCtrl.SelectFirst();
|
||||
}));
|
||||
//});
|
||||
}
|
||||
}
|
||||
}
|
||||
16
WinAlfred/Commands/SystemCommand.cs
Normal file
16
WinAlfred/Commands/SystemCommand.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WinAlfred.Plugin;
|
||||
|
||||
namespace WinAlfred.Commands
|
||||
{
|
||||
public class SystemCommand
|
||||
{
|
||||
public void Dispatch(Query query)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ using System.Windows.Controls;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Threading;
|
||||
using WinAlfred.Commands;
|
||||
using WinAlfred.Helper;
|
||||
using WinAlfred.Plugin;
|
||||
using WinAlfred.PluginLoader;
|
||||
@@ -18,9 +19,9 @@ namespace WinAlfred
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
private KeyboardHook hook = new KeyboardHook();
|
||||
public List<PluginPair> plugins = new List<PluginPair>();
|
||||
private List<Result> results = new List<Result>();
|
||||
private NotifyIcon notifyIcon = null;
|
||||
private CommandDispatcher cmdDispatcher = new CommandDispatcher();
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
@@ -67,38 +68,8 @@ namespace WinAlfred
|
||||
|
||||
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
string query = tbQuery.Text;
|
||||
//ThreadPool.QueueUserWorkItem(state =>
|
||||
//{
|
||||
results.Clear();
|
||||
foreach (PluginPair pair in plugins)
|
||||
{
|
||||
var q = new Query(query);
|
||||
if (pair.Metadata.ActionKeyword == q.ActionName)
|
||||
{
|
||||
try
|
||||
{
|
||||
results.AddRange(pair.Plugin.Query(q));
|
||||
results.ForEach(o => o.PluginDirectory = pair.Metadata.PluginDirecotry);
|
||||
}
|
||||
catch (Exception queryException)
|
||||
{
|
||||
Log.Error(string.Format("Plugin {0} query failed: {1}", pair.Metadata.Name,
|
||||
queryException.Message));
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
resultCtrl.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
resultCtrl.AddResults(results.OrderByDescending(o => o.Score).ToList());
|
||||
resultCtrl.SelectFirst();
|
||||
}));
|
||||
//});
|
||||
var q = new Query(tbQuery.Text);
|
||||
cmdDispatcher.DispatchCommand(q);
|
||||
}
|
||||
|
||||
private void HideWinAlfred()
|
||||
@@ -116,9 +87,7 @@ namespace WinAlfred
|
||||
|
||||
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
|
||||
plugins.AddRange(new CSharpPluginLoader().LoadPlugin());
|
||||
|
||||
Plugins.Init();
|
||||
ShowWinAlfred();
|
||||
InitialTray();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using WinAlfred.Helper;
|
||||
using WinAlfred.Plugin;
|
||||
using WinAlfred.Plugin.System;
|
||||
|
||||
namespace WinAlfred.PluginLoader
|
||||
{
|
||||
@@ -21,11 +24,34 @@ namespace WinAlfred.PluginLoader
|
||||
|
||||
private static void ParsePlugins()
|
||||
{
|
||||
ParseDirectories();
|
||||
ParsePackagedPlugin();
|
||||
ParseSystemPlugins();
|
||||
ParseThirdPartyPlugins();
|
||||
}
|
||||
|
||||
private static void ParseDirectories()
|
||||
private static void ParseSystemPlugins()
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly asm = Assembly.GetAssembly(typeof (CMD));
|
||||
List<Type> types = asm.GetTypes().Where(o => o.GetInterfaces().Contains(typeof(ISystemPlugin))).ToList();
|
||||
foreach (Type type in types)
|
||||
{
|
||||
ISystemPlugin sysPlugin = Activator.CreateInstance(types[0]) as ISystemPlugin;
|
||||
PluginMetadata metadata = new PluginMetadata();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(string.Format("Cound't load system plugin: {0}", e.Message));
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private static void ParseThirdPartyPlugins()
|
||||
{
|
||||
string[] directories = Directory.GetDirectories(PluginPath);
|
||||
foreach (string directory in directories)
|
||||
@@ -35,11 +61,6 @@ namespace WinAlfred.PluginLoader
|
||||
}
|
||||
}
|
||||
|
||||
private static void ParsePackagedPlugin()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private static PluginMetadata GetMetadataFromIni(string directory)
|
||||
{
|
||||
string iniPath = directory + "\\" + PluginConfigName;
|
||||
@@ -50,7 +71,6 @@ namespace WinAlfred.PluginLoader
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
PluginMetadata metadata = new PluginMetadata();
|
||||
@@ -60,6 +80,7 @@ namespace WinAlfred.PluginLoader
|
||||
metadata.Description = ini.GetSetting("plugin", "Description");
|
||||
metadata.Language = ini.GetSetting("plugin", "Language");
|
||||
metadata.Version = ini.GetSetting("plugin", "Version");
|
||||
metadata.PluginType = PluginType.ThirdParty;
|
||||
metadata.ActionKeyword = ini.GetSetting("plugin", "ActionKeyword");
|
||||
metadata.ExecuteFilePath = AppDomain.CurrentDomain.BaseDirectory + directory + "\\" + ini.GetSetting("plugin", "ExecuteFile");
|
||||
metadata.PluginDirecotry = AppDomain.CurrentDomain.BaseDirectory + directory + "\\";
|
||||
|
||||
25
WinAlfred/PluginLoader/Plugins.cs
Normal file
25
WinAlfred/PluginLoader/Plugins.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WinAlfred.Plugin;
|
||||
|
||||
namespace WinAlfred.PluginLoader
|
||||
{
|
||||
public static class Plugins
|
||||
{
|
||||
private static List<PluginPair> plugins = new List<PluginPair>();
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
plugins.Clear();
|
||||
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
|
||||
plugins.AddRange(new CSharpPluginLoader().LoadPlugin());
|
||||
}
|
||||
|
||||
public static List<PluginPair> AllPlugins
|
||||
{
|
||||
get { return plugins; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,12 +64,16 @@
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Commands\CommandDispatcher.cs" />
|
||||
<Compile Include="Commands\PluginCommand.cs" />
|
||||
<Compile Include="Commands\SystemCommand.cs" />
|
||||
<Compile Include="Helper\IniParser.cs" />
|
||||
<Compile Include="Helper\KeyboardHook.cs" />
|
||||
<Compile Include="Helper\Log.cs" />
|
||||
<Compile Include="Helper\WinAlfredException.cs" />
|
||||
<Compile Include="PluginLoader\BasePluginLoader.cs" />
|
||||
<Compile Include="PluginLoader\CSharpPluginLoader.cs" />
|
||||
<Compile Include="PluginLoader\Plugins.cs" />
|
||||
<Compile Include="PluginLoader\PythonPluginLoader.cs" />
|
||||
<Compile Include="PluginLoader\PythonPluginWrapper.cs" />
|
||||
<Compile Include="ResultPanel.xaml.cs">
|
||||
@@ -128,6 +132,10 @@
|
||||
<AppDesigner Include="Properties\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\WinAlfred.Plugin.System\WinAlfred.Plugin.System.csproj">
|
||||
<Project>{69ce0206-cb41-453d-88af-df86092ef9b8}</Project>
|
||||
<Name>WinAlfred.Plugin.System</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\WinAlfred.Plugin\WinAlfred.Plugin.csproj">
|
||||
<Project>{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}</Project>
|
||||
<Name>WinAlfred.Plugin</Name>
|
||||
|
||||
Reference in New Issue
Block a user