mirror of
https://github.com/microsoft/PowerToys
synced 2024-11-21 15:53:19 +00:00
Add Program Plugin
This commit is contained in:
parent
dc51bc39ab
commit
07d002da48
@ -23,10 +23,12 @@ namespace WinAlfred.Plugin.System
|
||||
Action = () =>
|
||||
{
|
||||
Process process = new Process();
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo();
|
||||
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
||||
startInfo.FileName = "cmd.exe";
|
||||
startInfo.Arguments = "/C " + cmd;
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
{
|
||||
WindowStyle = ProcessWindowStyle.Normal,
|
||||
FileName = "cmd.exe",
|
||||
Arguments = "/C " + cmd
|
||||
};
|
||||
process.StartInfo = startInfo;
|
||||
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>();
|
||||
|
||||
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")]
|
||||
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
|
||||
[DllImport("user32")]
|
||||
@ -37,14 +43,16 @@ namespace WinAlfred.Plugin.System
|
||||
Title = "Shutdown",
|
||||
SubTitle = "Shutdown Computer",
|
||||
Score = 100,
|
||||
Action = () => MessageBox.Show("shutdown")
|
||||
IcoPath = "Images\\exit.png",
|
||||
Action = () => ExitWindowsEx(EWX_SHUTDOWN,0)
|
||||
});
|
||||
availableResults.Add(new Result
|
||||
{
|
||||
Title = "Log off",
|
||||
SubTitle = "Log off current user",
|
||||
Score = 10,
|
||||
Action = () => MessageBox.Show("Logoff")
|
||||
Score = 20,
|
||||
IcoPath = "Images\\logoff.png",
|
||||
Action = () => ExitWindowsEx(EWX_LOGOFF, 0)
|
||||
});
|
||||
availableResults.Add(new Result
|
||||
{
|
||||
|
@ -41,6 +41,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="CMD.cs" />
|
||||
<Compile Include="ISystemPlugin.cs" />
|
||||
<Compile Include="Programs.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Sys.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
|
||||
/// </summary>
|
||||
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.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
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;
|
||||
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
using Timer = System.Threading.Timer;
|
||||
|
||||
namespace WinAlfred
|
||||
{
|
||||
public partial class MainWindow : Window
|
||||
public partial class MainWindow
|
||||
{
|
||||
private KeyboardHook hook = new KeyboardHook();
|
||||
private List<Result> results = new List<Result>();
|
||||
private NotifyIcon notifyIcon = null;
|
||||
private NotifyIcon notifyIcon;
|
||||
private Command cmdDispatcher;
|
||||
|
||||
public MainWindow()
|
||||
@ -31,7 +29,7 @@ namespace WinAlfred
|
||||
hook.KeyPressed += OnHotKey;
|
||||
hook.RegisterHotKey(XModifierKeys.Alt, Keys.Space);
|
||||
resultCtrl.resultItemChangedEvent += resultCtrl_resultItemChangedEvent;
|
||||
ThreadPool.SetMaxThreads(10, 5);
|
||||
ThreadPool.SetMaxThreads(30, 10);
|
||||
}
|
||||
|
||||
private void InitialTray()
|
||||
@ -54,11 +52,6 @@ namespace WinAlfred
|
||||
{
|
||||
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 };
|
||||
|
||||
if (resultCtrl.GetCurrentResultCount() == 1)
|
||||
{
|
||||
resultCtrl.SelectFirst();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHotKey(object sender, KeyPressedEventArgs e)
|
||||
@ -75,17 +68,18 @@ namespace WinAlfred
|
||||
|
||||
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
//MessageBox.Show("s");
|
||||
resultCtrl.Dirty = true;
|
||||
//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
|
||||
//we will see the splash. The more closer that clear and addResult method, the less splash we will see.
|
||||
////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
|
||||
////we will see the splash. The more closer that clear and addResult method, the less splash we will see.
|
||||
new Timer(o =>
|
||||
{
|
||||
if (resultCtrl.Dirty)
|
||||
{
|
||||
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;
|
||||
|
||||
var q = new Query(tbQuery.Text);
|
||||
@ -99,17 +93,16 @@ namespace WinAlfred
|
||||
|
||||
public void ShowWinAlfred()
|
||||
{
|
||||
tbQuery.SelectAll();
|
||||
Show();
|
||||
Focus();
|
||||
FocusManager.SetFocusedElement(this, tbQuery);
|
||||
//FocusManager.SetFocusedElement(this, tbQuery);
|
||||
Keyboard.Focus(tbQuery);
|
||||
tbQuery.SelectAll();
|
||||
}
|
||||
|
||||
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Plugins.Init(this);
|
||||
cmdDispatcher = new Command(this);
|
||||
ShowWinAlfred();
|
||||
InitialTray();
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,12 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using WinAlfred.Plugin;
|
||||
using Brush = System.Windows.Media.Brush;
|
||||
|
||||
namespace WinAlfred
|
||||
{
|
||||
@ -40,10 +43,36 @@ namespace WinAlfred
|
||||
|
||||
tbTitle.Text = result.Title;
|
||||
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.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using WinAlfred.Plugin;
|
||||
@ -32,11 +33,15 @@ namespace WinAlfred
|
||||
for (int i = 0; i < results.Count; i++)
|
||||
{
|
||||
Result result = results[i];
|
||||
if (!CheckExisted(result))
|
||||
{
|
||||
ResultItem control = new ResultItem(result);
|
||||
control.SetIndex(i + 1);
|
||||
pnlContainer.Children.Add(control);
|
||||
pnlContainer.Children.Insert(GetInsertLocation(result.Score), control);
|
||||
}
|
||||
}
|
||||
|
||||
SelectFirst();
|
||||
pnlContainer.UpdateLayout();
|
||||
|
||||
double resultItemHeight = 0;
|
||||
@ -46,16 +51,43 @@ namespace WinAlfred
|
||||
if (resultItem != null)
|
||||
resultItemHeight = resultItem.ActualHeight;
|
||||
}
|
||||
pnlContainer.Height = results.Count * resultItemHeight;
|
||||
pnlContainer.Height = pnlContainer.Children.Count * resultItemHeight;
|
||||
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()
|
||||
{
|
||||
return pnlContainer.Children.Count;
|
||||
}
|
||||
|
||||
private int GetCurrentSelectedResultIndex()
|
||||
public int GetCurrentSelectedResultIndex()
|
||||
{
|
||||
for (int i = 0; i < pnlContainer.Children.Count; i++)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user