mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-06-11 12:14:53 +08:00
Add Program Plugin
This commit is contained in:
parent
dc51bc39ab
commit
07d002da48
@ -23,10 +23,12 @@ namespace WinAlfred.Plugin.System
|
|||||||
Action = () =>
|
Action = () =>
|
||||||
{
|
{
|
||||||
Process process = new Process();
|
Process process = new Process();
|
||||||
ProcessStartInfo startInfo = new ProcessStartInfo();
|
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||||
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
{
|
||||||
startInfo.FileName = "cmd.exe";
|
WindowStyle = ProcessWindowStyle.Normal,
|
||||||
startInfo.Arguments = "/C " + cmd;
|
FileName = "cmd.exe",
|
||||||
|
Arguments = "/C " + cmd
|
||||||
|
};
|
||||||
process.StartInfo = startInfo;
|
process.StartInfo = startInfo;
|
||||||
process.Start();
|
process.Start();
|
||||||
}
|
}
|
||||||
|
106
WinAlfred.Plugin.System/Programs.cs
Normal file
106
WinAlfred.Plugin.System/Programs.cs
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
using Microsoft.Win32;
|
||||||
|
|
||||||
|
namespace WinAlfred.Plugin.System
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public string Title { get; set; }
|
||||||
|
public string IcoPath { get; set; }
|
||||||
|
public string ExecutePath { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Programs : ISystemPlugin
|
||||||
|
{
|
||||||
|
List<Program> installedList = new List<Program>();
|
||||||
|
|
||||||
|
public List<Result> Query(Query query)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.Length <= 1) return new List<Result>();
|
||||||
|
|
||||||
|
return installedList.Where(o => o.Title.ToLower().Contains(query.RawQuery.ToLower())).Select(c => new Result()
|
||||||
|
{
|
||||||
|
Title = c.Title,
|
||||||
|
IcoPath = c.IcoPath,
|
||||||
|
Score = 10,
|
||||||
|
Action = () =>
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(c.ExecutePath))
|
||||||
|
{
|
||||||
|
MessageBox.Show("couldn't start" + c.Title);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Process.Start(c.ExecutePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Init(PluginInitContext context)
|
||||||
|
{
|
||||||
|
GetAppFromStartMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GetAppFromStartMenu()
|
||||||
|
{
|
||||||
|
List<string> path =
|
||||||
|
Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Programs)).ToList();
|
||||||
|
foreach (string s in path)
|
||||||
|
{
|
||||||
|
GetAppFromDirectory(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GetAppFromDirectory(string path)
|
||||||
|
{
|
||||||
|
foreach (string file in Directory.GetFiles(path))
|
||||||
|
{
|
||||||
|
if (file.EndsWith(".lnk") || file.EndsWith(".exe"))
|
||||||
|
{
|
||||||
|
Program p = new Program()
|
||||||
|
{
|
||||||
|
Title = getAppNameFromAppPath(file),
|
||||||
|
IcoPath = file,
|
||||||
|
ExecutePath = file
|
||||||
|
};
|
||||||
|
installedList.Add(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var subDirectory in Directory.GetDirectories(path))
|
||||||
|
{
|
||||||
|
GetAppFromDirectory(subDirectory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string getAppNameFromAppPath(string app)
|
||||||
|
{
|
||||||
|
string temp = app.Substring(app.LastIndexOf('\\') + 1);
|
||||||
|
string name = temp.Substring(0, temp.LastIndexOf('.'));
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "Programs";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Description
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "get system programs";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,12 @@ namespace WinAlfred.Plugin.System
|
|||||||
{
|
{
|
||||||
List<Result> availableResults = new List<Result>();
|
List<Result> availableResults = new List<Result>();
|
||||||
|
|
||||||
|
internal const int EWX_LOGOFF = 0x00000000;
|
||||||
|
internal const int EWX_SHUTDOWN = 0x00000001;
|
||||||
|
internal const int EWX_REBOOT = 0x00000002;
|
||||||
|
internal const int EWX_FORCE = 0x00000004;
|
||||||
|
internal const int EWX_POWEROFF = 0x00000008;
|
||||||
|
internal const int EWX_FORCEIFHUNG = 0x00000010;
|
||||||
[DllImport("user32")]
|
[DllImport("user32")]
|
||||||
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
|
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
|
||||||
[DllImport("user32")]
|
[DllImport("user32")]
|
||||||
@ -37,14 +43,16 @@ namespace WinAlfred.Plugin.System
|
|||||||
Title = "Shutdown",
|
Title = "Shutdown",
|
||||||
SubTitle = "Shutdown Computer",
|
SubTitle = "Shutdown Computer",
|
||||||
Score = 100,
|
Score = 100,
|
||||||
Action = () => MessageBox.Show("shutdown")
|
IcoPath = "Images\\exit.png",
|
||||||
|
Action = () => ExitWindowsEx(EWX_SHUTDOWN,0)
|
||||||
});
|
});
|
||||||
availableResults.Add(new Result
|
availableResults.Add(new Result
|
||||||
{
|
{
|
||||||
Title = "Log off",
|
Title = "Log off",
|
||||||
SubTitle = "Log off current user",
|
SubTitle = "Log off current user",
|
||||||
Score = 10,
|
Score = 20,
|
||||||
Action = () => MessageBox.Show("Logoff")
|
IcoPath = "Images\\logoff.png",
|
||||||
|
Action = () => ExitWindowsEx(EWX_LOGOFF, 0)
|
||||||
});
|
});
|
||||||
availableResults.Add(new Result
|
availableResults.Add(new Result
|
||||||
{
|
{
|
||||||
|
@ -41,6 +41,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="CMD.cs" />
|
<Compile Include="CMD.cs" />
|
||||||
<Compile Include="ISystemPlugin.cs" />
|
<Compile Include="ISystemPlugin.cs" />
|
||||||
|
<Compile Include="Programs.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Sys.cs" />
|
<Compile Include="Sys.cs" />
|
||||||
<Compile Include="ThirdpartyPluginIndicator.cs" />
|
<Compile Include="ThirdpartyPluginIndicator.cs" />
|
||||||
|
@ -26,5 +26,13 @@ namespace WinAlfred.Plugin
|
|||||||
/// you don't need to set this property if you are developing a plugin
|
/// you don't need to set this property if you are developing a plugin
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string PluginDirectory { get; set; }
|
public string PluginDirectory { get; set; }
|
||||||
|
|
||||||
|
public new bool Equals(object obj)
|
||||||
|
{
|
||||||
|
if (obj == null || !(obj is Result)) return false;
|
||||||
|
|
||||||
|
Result r = (Result)obj;
|
||||||
|
return r.Title == Title && r.SubTitle == SubTitle;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,27 +1,25 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO.Ports;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Threading;
|
|
||||||
using WinAlfred.Commands;
|
using WinAlfred.Commands;
|
||||||
using WinAlfred.Helper;
|
using WinAlfred.Helper;
|
||||||
using WinAlfred.Plugin;
|
using WinAlfred.Plugin;
|
||||||
using WinAlfred.PluginLoader;
|
using WinAlfred.PluginLoader;
|
||||||
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
||||||
|
using MessageBox = System.Windows.MessageBox;
|
||||||
using Timer = System.Threading.Timer;
|
using Timer = System.Threading.Timer;
|
||||||
|
|
||||||
namespace WinAlfred
|
namespace WinAlfred
|
||||||
{
|
{
|
||||||
public partial class MainWindow : Window
|
public partial class MainWindow
|
||||||
{
|
{
|
||||||
private KeyboardHook hook = new KeyboardHook();
|
private KeyboardHook hook = new KeyboardHook();
|
||||||
private List<Result> results = new List<Result>();
|
private NotifyIcon notifyIcon;
|
||||||
private NotifyIcon notifyIcon = null;
|
|
||||||
private Command cmdDispatcher;
|
private Command cmdDispatcher;
|
||||||
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
@ -31,7 +29,7 @@ namespace WinAlfred
|
|||||||
hook.KeyPressed += OnHotKey;
|
hook.KeyPressed += OnHotKey;
|
||||||
hook.RegisterHotKey(XModifierKeys.Alt, Keys.Space);
|
hook.RegisterHotKey(XModifierKeys.Alt, Keys.Space);
|
||||||
resultCtrl.resultItemChangedEvent += resultCtrl_resultItemChangedEvent;
|
resultCtrl.resultItemChangedEvent += resultCtrl_resultItemChangedEvent;
|
||||||
ThreadPool.SetMaxThreads(10, 5);
|
ThreadPool.SetMaxThreads(30, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitialTray()
|
private void InitialTray()
|
||||||
@ -54,11 +52,6 @@ namespace WinAlfred
|
|||||||
{
|
{
|
||||||
Height = resultCtrl.pnlContainer.ActualHeight + tbQuery.Height + tbQuery.Margin.Top + tbQuery.Margin.Bottom;
|
Height = resultCtrl.pnlContainer.ActualHeight + tbQuery.Height + tbQuery.Margin.Top + tbQuery.Margin.Bottom;
|
||||||
resultCtrl.Margin = resultCtrl.GetCurrentResultCount() > 0 ? new Thickness { Bottom = 10, Left = 10, Right = 10 } : new Thickness { Bottom = 0, Left = 10, Right = 10 };
|
resultCtrl.Margin = resultCtrl.GetCurrentResultCount() > 0 ? new Thickness { Bottom = 10, Left = 10, Right = 10 } : new Thickness { Bottom = 0, Left = 10, Right = 10 };
|
||||||
|
|
||||||
if (resultCtrl.GetCurrentResultCount() == 1)
|
|
||||||
{
|
|
||||||
resultCtrl.SelectFirst();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnHotKey(object sender, KeyPressedEventArgs e)
|
private void OnHotKey(object sender, KeyPressedEventArgs e)
|
||||||
@ -75,17 +68,18 @@ namespace WinAlfred
|
|||||||
|
|
||||||
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
|
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
|
||||||
{
|
{
|
||||||
|
//MessageBox.Show("s");
|
||||||
resultCtrl.Dirty = true;
|
resultCtrl.Dirty = true;
|
||||||
//auto clear results after 50ms if there are any results returned by plugins
|
////auto clear results after 50ms if there are any results returned by plugins
|
||||||
//why we do this? because if we clear resulsts in the start of the text changed event
|
////why we do this? because if we clear resulsts in the start of the text changed event
|
||||||
//we will see the splash. The more closer that clear and addResult method, the less splash we will see.
|
////we will see the splash. The more closer that clear and addResult method, the less splash we will see.
|
||||||
new Timer(o =>
|
new Timer(o =>
|
||||||
{
|
{
|
||||||
if (resultCtrl.Dirty)
|
if (resultCtrl.Dirty)
|
||||||
{
|
{
|
||||||
resultCtrl.Dispatcher.Invoke(new Action(() => resultCtrl.Clear()));
|
resultCtrl.Dispatcher.Invoke(new Action(() => resultCtrl.Clear()));
|
||||||
}
|
}
|
||||||
}, null, TimeSpan.FromMilliseconds(50),TimeSpan.FromMilliseconds(-1));
|
}, null, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(-1));
|
||||||
if (string.IsNullOrEmpty(tbQuery.Text)) return;
|
if (string.IsNullOrEmpty(tbQuery.Text)) return;
|
||||||
|
|
||||||
var q = new Query(tbQuery.Text);
|
var q = new Query(tbQuery.Text);
|
||||||
@ -99,17 +93,16 @@ namespace WinAlfred
|
|||||||
|
|
||||||
public void ShowWinAlfred()
|
public void ShowWinAlfred()
|
||||||
{
|
{
|
||||||
tbQuery.SelectAll();
|
|
||||||
Show();
|
Show();
|
||||||
Focus();
|
//FocusManager.SetFocusedElement(this, tbQuery);
|
||||||
FocusManager.SetFocusedElement(this, tbQuery);
|
Keyboard.Focus(tbQuery);
|
||||||
|
tbQuery.SelectAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
|
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
Plugins.Init(this);
|
Plugins.Init(this);
|
||||||
cmdDispatcher = new Command(this);
|
cmdDispatcher = new Command(this);
|
||||||
ShowWinAlfred();
|
|
||||||
InitialTray();
|
InitialTray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +138,7 @@ namespace WinAlfred
|
|||||||
resultCtrl.Dispatcher.Invoke(new Action(() =>
|
resultCtrl.Dispatcher.Invoke(new Action(() =>
|
||||||
{
|
{
|
||||||
List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).OrderByDescending(o => o.Score).ToList();
|
List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).OrderByDescending(o => o.Score).ToList();
|
||||||
resultCtrl.AddResults(l);
|
resultCtrl.AddResults(l);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Drawing;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
using WinAlfred.Plugin;
|
using WinAlfred.Plugin;
|
||||||
|
using Brush = System.Windows.Media.Brush;
|
||||||
|
|
||||||
namespace WinAlfred
|
namespace WinAlfred
|
||||||
{
|
{
|
||||||
@ -40,10 +43,36 @@ namespace WinAlfred
|
|||||||
|
|
||||||
tbTitle.Text = result.Title;
|
tbTitle.Text = result.Title;
|
||||||
tbSubTitle.Text = result.SubTitle;
|
tbSubTitle.Text = result.SubTitle;
|
||||||
if (!string.IsNullOrEmpty(result.IcoPath) && File.Exists(result.PluginDirectory + result.IcoPath))
|
string path = string.Empty;
|
||||||
|
if (!string.IsNullOrEmpty(result.IcoPath) && result.IcoPath.Contains(":\\") && File.Exists(result.IcoPath))
|
||||||
{
|
{
|
||||||
imgIco.Source = new BitmapImage(new Uri(result.PluginDirectory + result.IcoPath));
|
path = result.IcoPath;
|
||||||
}
|
}
|
||||||
|
else if (!string.IsNullOrEmpty(result.IcoPath) && File.Exists(result.PluginDirectory + result.IcoPath))
|
||||||
|
{
|
||||||
|
path = result.PluginDirectory + result.IcoPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(path))
|
||||||
|
{
|
||||||
|
if (path.ToLower().EndsWith(".exe") || path.ToLower().EndsWith(".lnk"))
|
||||||
|
{
|
||||||
|
imgIco.Source = GetIcon(path);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
imgIco.Source = new BitmapImage(new Uri(path));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ImageSource GetIcon(string fileName)
|
||||||
|
{
|
||||||
|
Icon icon = Icon.ExtractAssociatedIcon(fileName);
|
||||||
|
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
|
||||||
|
icon.Handle,
|
||||||
|
new Int32Rect(0, 0, icon.Width, icon.Height),
|
||||||
|
BitmapSizeOptions.FromEmptyOptions());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using WinAlfred.Plugin;
|
using WinAlfred.Plugin;
|
||||||
@ -32,11 +33,15 @@ namespace WinAlfred
|
|||||||
for (int i = 0; i < results.Count; i++)
|
for (int i = 0; i < results.Count; i++)
|
||||||
{
|
{
|
||||||
Result result = results[i];
|
Result result = results[i];
|
||||||
ResultItem control = new ResultItem(result);
|
if (!CheckExisted(result))
|
||||||
control.SetIndex(i + 1);
|
{
|
||||||
pnlContainer.Children.Add(control);
|
ResultItem control = new ResultItem(result);
|
||||||
|
control.SetIndex(i + 1);
|
||||||
|
pnlContainer.Children.Insert(GetInsertLocation(result.Score), control);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SelectFirst();
|
||||||
pnlContainer.UpdateLayout();
|
pnlContainer.UpdateLayout();
|
||||||
|
|
||||||
double resultItemHeight = 0;
|
double resultItemHeight = 0;
|
||||||
@ -46,16 +51,43 @@ namespace WinAlfred
|
|||||||
if (resultItem != null)
|
if (resultItem != null)
|
||||||
resultItemHeight = resultItem.ActualHeight;
|
resultItemHeight = resultItem.ActualHeight;
|
||||||
}
|
}
|
||||||
pnlContainer.Height = results.Count * resultItemHeight;
|
pnlContainer.Height = pnlContainer.Children.Count * resultItemHeight;
|
||||||
OnResultItemChangedEvent();
|
OnResultItemChangedEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool CheckExisted(Result result)
|
||||||
|
{
|
||||||
|
return pnlContainer.Children.Cast<ResultItem>().Any(child => child.Result.Equals(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetInsertLocation(int currentScore)
|
||||||
|
{
|
||||||
|
int location = pnlContainer.Children.Count;
|
||||||
|
if (pnlContainer.Children.Count == 0) return 0;
|
||||||
|
if (currentScore > ((ResultItem)pnlContainer.Children[0]).Result.Score) return 0;
|
||||||
|
|
||||||
|
for (int index = 1; index < pnlContainer.Children.Count; index++)
|
||||||
|
{
|
||||||
|
ResultItem next = pnlContainer.Children[index] as ResultItem;
|
||||||
|
ResultItem prev = pnlContainer.Children[index - 1] as ResultItem;
|
||||||
|
if (next != null && prev != null)
|
||||||
|
{
|
||||||
|
if ((currentScore >= next.Result.Score && currentScore <= prev.Result.Score))
|
||||||
|
{
|
||||||
|
location = index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
public int GetCurrentResultCount()
|
public int GetCurrentResultCount()
|
||||||
{
|
{
|
||||||
return pnlContainer.Children.Count;
|
return pnlContainer.Children.Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int GetCurrentSelectedResultIndex()
|
public int GetCurrentSelectedResultIndex()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < pnlContainer.Children.Count; i++)
|
for (int i = 0; i < pnlContainer.Children.Count; i++)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user