Add modifier key support when user action items.

This commit is contained in:
qianlifeng
2014-02-09 20:55:18 +08:00
parent 953adadd62
commit fcdb9e39c6
23 changed files with 84 additions and 133 deletions

View File

@@ -135,7 +135,7 @@ namespace Wox.Plugin.Doc
Title = name, Title = name,
SubTitle = doc.Name.Replace(".docset", ""), SubTitle = doc.Name.Replace(".docset", ""),
IcoPath = doc.IconPath, IcoPath = doc.IconPath,
Action = () => Action = (c) =>
{ {
string url = string.Format(@"{0}\{1}\Contents\Resources\Documents\{2}#{3}", docsetBasePath, string url = string.Format(@"{0}\{1}\Contents\Resources\Documents\{2}#{3}", docsetBasePath,
doc.Name, docPath, name); doc.Name, docPath, name);

View File

@@ -25,7 +25,9 @@ def query(key):
results.append(res) results.append(res)
return json.dumps(results) return json.dumps(results)
def openUrl(url): def openUrl(context,url):
#shift + enter
#if context["SpecialKeyState"]["ShiftPressed"] == "True":
webbrowser.open(url) webbrowser.open(url)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -22,7 +22,7 @@ namespace Wox.Plugin.Everything
Result r = new Result(); Result r = new Result();
r.Title = Path.GetFileName(s); r.Title = Path.GetFileName(s);
r.SubTitle = s; r.SubTitle = s;
r.Action = () => r.Action = (c) =>
{ {
context.HideApp(); context.HideApp();
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();

View File

@@ -76,7 +76,7 @@ namespace Wox.Plugin.Fanyi
Title = dst, Title = dst,
SubTitle = "Copy to clipboard", SubTitle = "Copy to clipboard",
IcoPath = "Images\\translate.png", IcoPath = "Images\\translate.png",
Action = () => Action = (c) =>
{ {
Clipboard.SetText(dst); Clipboard.SetText(dst);
context.ShowMsg("translation has been copyed to your clipboard.", "", context.ShowMsg("translation has been copyed to your clipboard.", "",

View File

@@ -33,7 +33,7 @@ namespace Wox.Plugin.System
SubTitle = "Bookmark: " + c.Url, SubTitle = "Bookmark: " + c.Url,
IcoPath = Directory.GetCurrentDirectory() + @"\Images\bookmark.png", IcoPath = Directory.GetCurrentDirectory() + @"\Images\bookmark.png",
Score = 5, Score = 5,
Action = () => Action = (context) =>
{ {
try try
{ {

View File

@@ -26,7 +26,7 @@ namespace Wox.Plugin.System
Title = m.Key, Title = m.Key,
SubTitle = "this command has been executed " + m.Value + " times", SubTitle = "this command has been executed " + m.Value + " times",
IcoPath = "Images/cmd.png", IcoPath = "Images/cmd.png",
Action = () => Action = (c) =>
{ {
ExecuteCmd(m.Key); ExecuteCmd(m.Key);
AddCmdHistory(m.Key); AddCmdHistory(m.Key);
@@ -45,7 +45,7 @@ namespace Wox.Plugin.System
Score = 5000, Score = 5000,
SubTitle = "execute command through command shell", SubTitle = "execute command through command shell",
IcoPath = "Images/cmd.png", IcoPath = "Images/cmd.png",
Action = () => Action = (c) =>
{ {
ExecuteCmd(cmd); ExecuteCmd(cmd);
AddCmdHistory(cmd); AddCmdHistory(cmd);
@@ -60,7 +60,7 @@ namespace Wox.Plugin.System
Title = m.Key, Title = m.Key,
SubTitle = "this command has been executed " + m.Value + " times", SubTitle = "this command has been executed " + m.Value + " times",
IcoPath = "Images/cmd.png", IcoPath = "Images/cmd.png",
Action = () => Action = (c) =>
{ {
ExecuteCmd(m.Key); ExecuteCmd(m.Key);
AddCmdHistory(m.Key); AddCmdHistory(m.Key);

View File

@@ -4,6 +4,7 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Forms;
namespace Wox.Plugin.System namespace Wox.Plugin.System
{ {
@@ -22,7 +23,10 @@ namespace Wox.Plugin.System
SubTitle = string.Format("path: {0}", query.RawQuery), SubTitle = string.Format("path: {0}", query.RawQuery),
Score = 50, Score = 50,
IcoPath = "Images/folder.png", IcoPath = "Images/folder.png",
Action = () => Process.Start(query.RawQuery) Action = (c) =>
{
Process.Start(query.RawQuery);
}
}; };
results.Add(result); results.Add(result);
} }

View File

@@ -46,7 +46,7 @@ namespace Wox.Plugin.System
Title = c.Title, Title = c.Title,
IcoPath = c.IcoPath, IcoPath = c.IcoPath,
Score = c.Score, Score = c.Score,
Action = () => Action = (context) =>
{ {
if (string.IsNullOrEmpty(c.ExecutePath)) if (string.IsNullOrEmpty(c.ExecutePath))
{ {

View File

@@ -18,7 +18,7 @@ namespace Wox.Plugin.System
Title = "Wox Setting Dialog", Title = "Wox Setting Dialog",
Score = 100, Score = 100,
IcoPath = "Images/app.png", IcoPath = "Images/app.png",
Action = () => context.OpenSettingDialog() Action = (contenxt) => context.OpenSettingDialog()
}); });
} }

View File

@@ -47,7 +47,7 @@ namespace Wox.Plugin.System
SubTitle = "Shutdown Computer", SubTitle = "Shutdown Computer",
Score = 100, Score = 100,
IcoPath = "Images\\exit.png", IcoPath = "Images\\exit.png",
Action = () => Process.Start("shutdown","/s /t 0") Action = (c) => Process.Start("shutdown","/s /t 0")
}); });
availableResults.Add(new Result availableResults.Add(new Result
{ {
@@ -55,7 +55,7 @@ namespace Wox.Plugin.System
SubTitle = "Log off current user", SubTitle = "Log off current user",
Score = 20, Score = 20,
IcoPath = "Images\\logoff.png", IcoPath = "Images\\logoff.png",
Action = () => ExitWindowsEx(EWX_LOGOFF, 0) Action = (c) => ExitWindowsEx(EWX_LOGOFF, 0)
}); });
availableResults.Add(new Result availableResults.Add(new Result
{ {
@@ -63,7 +63,7 @@ namespace Wox.Plugin.System
SubTitle = "Lock this computer", SubTitle = "Lock this computer",
Score = 20, Score = 20,
IcoPath = "Images\\lock.png", IcoPath = "Images\\lock.png",
Action = () => LockWorkStation() Action = (c) => LockWorkStation()
}); });
availableResults.Add(new Result availableResults.Add(new Result
{ {
@@ -71,7 +71,7 @@ namespace Wox.Plugin.System
SubTitle = "Close this app", SubTitle = "Close this app",
Score = 110, Score = 110,
IcoPath = "Images\\app.png", IcoPath = "Images\\app.png",
Action = () => context.CloseApp() Action = (c) => context.CloseApp()
}); });
} }
} }

View File

@@ -27,7 +27,7 @@ namespace Wox.Plugin.System
SubTitle = string.Format("Activate {0} plugin", metadata.Name), SubTitle = string.Format("Activate {0} plugin", metadata.Name),
Score = 50, Score = 50,
IcoPath = "Images/work.png", IcoPath = "Images/work.png",
Action = () => changeQuery(metadataCopy.ActionKeyword + " "), Action = (c) => changeQuery(metadataCopy.ActionKeyword + " "),
DontHideWoxAfterSelect = true DontHideWoxAfterSelect = true
}; };
results.Add(result); results.Add(result);
@@ -40,7 +40,7 @@ namespace Wox.Plugin.System
SubTitle = string.Format("Activate {0} web search", n.ActionWord), SubTitle = string.Format("Activate {0} web search", n.ActionWord),
Score = 50, Score = 50,
IcoPath = "Images/work.png", IcoPath = "Images/work.png",
Action = () => changeQuery(n.ActionWord + " "), Action = (c) => changeQuery(n.ActionWord + " "),
DontHideWoxAfterSelect = true DontHideWoxAfterSelect = true
})); }));

View File

@@ -26,7 +26,7 @@ namespace Wox.Plugin.System
{ {
Title = string.Format("Search {0} for \"{1}\"", webSearch.Title, keyword), Title = string.Format("Search {0} for \"{1}\"", webSearch.Title, keyword),
IcoPath = webSearch.IconPath, IcoPath = webSearch.IconPath,
Action = () => Process.Start(webSearch.Url.Replace("{q}", keyword)) Action = (c) => Process.Start(webSearch.Url.Replace("{q}", keyword))
}); });
} }

View File

@@ -0,0 +1,15 @@
namespace Wox.Plugin
{
public class ActionContext
{
public SpecialKeyState SpecialKeyState { get; set; }
}
public class SpecialKeyState
{
public bool CtrlPressed { get; set; }
public bool ShiftPressed { get; set; }
public bool AltPressed { get; set; }
public bool WinPressed { get; set; }
}
}

View File

@@ -9,7 +9,7 @@ namespace Wox.Plugin
public string Title { get; set; } public string Title { get; set; }
public string SubTitle { get; set; } public string SubTitle { get; set; }
public string IcoPath { get; set; } public string IcoPath { get; set; }
public Action Action { get; set; } public Action<ActionContext> Action { get; set; }
public int Score { get; set; } public int Score { get; set; }
public bool DontHideWoxAfterSelect { get; set; } public bool DontHideWoxAfterSelect { get; set; }
@@ -24,13 +24,9 @@ namespace Wox.Plugin
/// Only resulsts that originQuery match with curren query will be displayed in the panel /// Only resulsts that originQuery match with curren query will be displayed in the panel
/// </summary> /// </summary>
public Query OriginQuery { get; set; } public Query OriginQuery { get; set; }
/// <summary>
/// context results connected with current reuslt, usually, it can use <- or -> navigate context results
/// </summary>
public List<Result> ContextResults { get; set; }
/// <summary> /// <summary>
/// you don't need to set this property if you are developing a plugin /// Don't set this property if you are developing a plugin
/// </summary> /// </summary>
public string PluginDirectory { get; set; } public string PluginDirectory { get; set; }

View File

@@ -59,7 +59,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="AllowedLanguage.cs" /> <Compile Include="AllowedLanguage.cs" />
<Compile Include="IPlugin.cs" /> <Compile Include="IPlugin.cs" />
<Compile Include="Plugin.cs" /> <Compile Include="PluginPair.cs" />
<Compile Include="PluginInitContext.cs" /> <Compile Include="PluginInitContext.cs" />
<Compile Include="PluginMetadata.cs" /> <Compile Include="PluginMetadata.cs" />
<Compile Include="PluginType.cs" /> <Compile Include="PluginType.cs" />
@@ -67,6 +67,7 @@
<Compile Include="PythonResult.cs" /> <Compile Include="PythonResult.cs" />
<Compile Include="Query.cs" /> <Compile Include="Query.cs" />
<Compile Include="Result.cs" /> <Compile Include="Result.cs" />
<Compile Include="ActionContext.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -1,73 +0,0 @@
using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace Wox.Helper
{
public static class DwmDropShadow
{
[DllImport("dwmapi.dll", PreserveSig = true)]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);
/// <summary>
/// Drops a standard shadow to a WPF Window, even if the window isborderless. Only works with DWM (Vista and Seven).
/// This method is much more efficient than setting AllowsTransparency to true and using the DropShadow effect,
/// as AllowsTransparency involves a huge permormance issue (hardware acceleration is turned off for all the window).
/// </summary>
/// <param name="window">Window to which the shadow will be applied</param>
public static void DropShadowToWindow(Window window)
{
if (!DropShadow(window))
{
window.SourceInitialized += new EventHandler(window_SourceInitialized);
}
}
private static void window_SourceInitialized(object sender, EventArgs e) //fixed typo
{
Window window = (Window)sender;
DropShadow(window);
window.SourceInitialized -= new EventHandler(window_SourceInitialized);
}
/// <summary>
/// The actual method that makes API calls to drop the shadow to the window
/// </summary>
/// <param name="window">Window to which the shadow will be applied</param>
/// <returns>True if the method succeeded, false if not</returns>
private static bool DropShadow(Window window)
{
try
{
WindowInteropHelper helper = new WindowInteropHelper(window);
int val = 2;
int ret1 = DwmSetWindowAttribute(helper.Handle, 2, ref val, 2);
if (ret1 == 0)
{
Margins m = new Margins { Bottom = 0, Left = 0, Right = 0, Top = 0 };
int ret2 = DwmExtendFrameIntoClientArea(helper.Handle, ref m);
return ret2 == 0;
}
else
{
return false;
}
}
catch (Exception ex)
{
// Probably dwmapi.dll not found (incompatible OS)
return false;
}
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Wox.Plugin;
namespace Wox.Helper namespace Wox.Helper
{ {
@@ -28,17 +29,10 @@ namespace Wox.Helper
WM_SYSKEYDOWN = 260 WM_SYSKEYDOWN = 260
} }
public class SpecialKeyState
{
public bool CtrlPressed { get; set; }
public bool ShiftPressed { get; set; }
public bool AltPressed { get; set; }
public bool WinPressed { get; set; }
}
/// <summary> /// <summary>
/// Listens keyboard globally. /// Listens keyboard globally.
///
/// <remarks>Uses WH_KEYBOARD_LL.</remarks> /// <remarks>Uses WH_KEYBOARD_LL.</remarks>
/// </summary> /// </summary>
public class KeyboardListener : IDisposable public class KeyboardListener : IDisposable
@@ -62,7 +56,7 @@ namespace Wox.Helper
hookId = InterceptKeys.SetHook(hookedLowLevelKeyboardProc); hookId = InterceptKeys.SetHook(hookedLowLevelKeyboardProc);
} }
private SpecialKeyState CheckModifiers() public SpecialKeyState CheckModifiers()
{ {
SpecialKeyState state = new SpecialKeyState(); SpecialKeyState state = new SpecialKeyState();
if ((InterceptKeys.GetKeyState(VK_SHIFT) & 0x8000) != 0) if ((InterceptKeys.GetKeyState(VK_SHIFT) & 0x8000) != 0)
@@ -157,19 +151,6 @@ namespace Wox.Helper
[DllImport("user32.dll")] [DllImport("user32.dll")]
internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize); internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
public static void SendKeyStroke(int funckey, int key)
{
INPUT[] input = new INPUT[4];
input[0].type = input[1].type = input[2].type = input[3].type = (int)InputType.INPUT_KEYBOARD;
input[0].ki.wVk = input[2].ki.wVk = (short) funckey;
input[1].ki.wVk = input[3].ki.wVk = (short) key;
input[2].ki.dwFlags = input[3].ki.dwFlags =(int) KEYEVENTF.KEYUP;
SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]));
}
} }
public enum InputType public enum InputType

View File

@@ -185,7 +185,7 @@ namespace Wox
{ {
switch (args[0].ToLower()) switch (args[0].ToLower())
{ {
case "reloadworkflows": case "reloadplugin":
Plugins.Init(); Plugins.Init();
break; break;
@@ -250,7 +250,9 @@ namespace Wox
private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e) private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e)
{ {
switch (e.Key) //when alt is pressed, the real key should be e.SystemKey
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
switch (key)
{ {
case Key.Escape: case Key.Escape:
HideWox(); HideWox();

View File

@@ -40,7 +40,7 @@ namespace Wox.PluginLoader
PythonResult ps = pythonResult; PythonResult ps = pythonResult;
if (!string.IsNullOrEmpty(ps.ActionName)) if (!string.IsNullOrEmpty(ps.ActionName))
{ {
ps.Action = () => InvokeFunc(ps.ActionName, ps.ActionPara); ps.Action = (context) => InvokeFunc(ps.ActionName, GetPythonActionContext(context),new PyString(ps.ActionPara));
} }
r.Add(ps); r.Add(ps);
} }
@@ -58,15 +58,22 @@ namespace Wox.PluginLoader
return new List<Result>(); return new List<Result>();
} }
private string InvokeFunc(string func, params string[] para) private PyObject GetPythonActionContext(ActionContext context)
{ {
string json = ""; PyDict dict = new PyDict();
PyDict specialKeyStateDict = new PyDict();
specialKeyStateDict["CtrlPressed"] = new PyString(context.SpecialKeyState.CtrlPressed.ToString());
specialKeyStateDict["AltPressed"] = new PyString(context.SpecialKeyState.AltPressed.ToString());
specialKeyStateDict["WinPressed"] = new PyString(context.SpecialKeyState.WinPressed.ToString());
specialKeyStateDict["ShiftPressed"] = new PyString(context.SpecialKeyState.ShiftPressed.ToString());
PyObject[] paras = { }; dict["SpecialKeyState"] = specialKeyStateDict;
if (para != null && para.Length > 0) return dict;
{ }
paras = para.Select(o => new PyString(o)).ToArray();
} private string InvokeFunc(string func, params PyObject[] paras)
{
string json;
IntPtr gs = PythonEngine.AcquireLock(); IntPtr gs = PythonEngine.AcquireLock();
@@ -93,6 +100,16 @@ namespace Wox.PluginLoader
return json; return json;
} }
private string InvokeFunc(string func, params string[] para)
{
PyObject[] paras = { };
if (para != null && para.Length > 0)
{
paras = para.Select(o => new PyString(o)).ToArray();
}
return InvokeFunc(func, paras);
}
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {

View File

@@ -70,7 +70,10 @@ namespace Wox
AddHandler(MouseLeftButtonUpEvent, new RoutedEventHandler((o, e) => AddHandler(MouseLeftButtonUpEvent, new RoutedEventHandler((o, e) =>
{ {
if (Result.Action != null) if (Result.Action != null)
Result.Action(); Result.Action(new ActionContext()
{
SpecialKeyState = new KeyboardListener().CheckModifiers()
});
CommonStorage.Instance.UserSelectedRecords.Add(result); CommonStorage.Instance.UserSelectedRecords.Add(result);
if (!result.DontHideWoxAfterSelect) if (!result.DontHideWoxAfterSelect)

View File

@@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using Wox.Helper;
using Wox.Plugin; using Wox.Plugin;
namespace Wox namespace Wox
@@ -212,7 +213,10 @@ namespace Wox
{ {
if (resultItemControl.Result.Action != null) if (resultItemControl.Result.Action != null)
{ {
resultItemControl.Result.Action(); resultItemControl.Result.Action(new ActionContext()
{
SpecialKeyState = new KeyboardListener().CheckModifiers()
});
} }
return resultItemControl.Result; return resultItemControl.Result;

View File

@@ -120,7 +120,6 @@
<Compile Include="Commands\PluginCommand.cs" /> <Compile Include="Commands\PluginCommand.cs" />
<Compile Include="Commands\SystemCommand.cs" /> <Compile Include="Commands\SystemCommand.cs" />
<Compile Include="DispatcherExtensions.cs" /> <Compile Include="DispatcherExtensions.cs" />
<Compile Include="Helper\DwmDropShadow.cs" />
<Compile Include="Helper\KeyboardHook.cs" /> <Compile Include="Helper\KeyboardHook.cs" />
<Compile Include="Helper\Log.cs" /> <Compile Include="Helper\Log.cs" />
<Compile Include="Helper\PluginInstaller.cs" /> <Compile Include="Helper\PluginInstaller.cs" />