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,
SubTitle = doc.Name.Replace(".docset", ""),
IcoPath = doc.IconPath,
Action = () =>
Action = (c) =>
{
string url = string.Format(@"{0}\{1}\Contents\Resources\Documents\{2}#{3}", docsetBasePath,
doc.Name, docPath, name);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -47,7 +47,7 @@ namespace Wox.Plugin.System
SubTitle = "Shutdown Computer",
Score = 100,
IcoPath = "Images\\exit.png",
Action = () => Process.Start("shutdown","/s /t 0")
Action = (c) => Process.Start("shutdown","/s /t 0")
});
availableResults.Add(new Result
{
@ -55,7 +55,7 @@ namespace Wox.Plugin.System
SubTitle = "Log off current user",
Score = 20,
IcoPath = "Images\\logoff.png",
Action = () => ExitWindowsEx(EWX_LOGOFF, 0)
Action = (c) => ExitWindowsEx(EWX_LOGOFF, 0)
});
availableResults.Add(new Result
{
@ -63,7 +63,7 @@ namespace Wox.Plugin.System
SubTitle = "Lock this computer",
Score = 20,
IcoPath = "Images\\lock.png",
Action = () => LockWorkStation()
Action = (c) => LockWorkStation()
});
availableResults.Add(new Result
{
@ -71,7 +71,7 @@ namespace Wox.Plugin.System
SubTitle = "Close this app",
Score = 110,
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),
Score = 50,
IcoPath = "Images/work.png",
Action = () => changeQuery(metadataCopy.ActionKeyword + " "),
Action = (c) => changeQuery(metadataCopy.ActionKeyword + " "),
DontHideWoxAfterSelect = true
};
results.Add(result);
@ -40,7 +40,7 @@ namespace Wox.Plugin.System
SubTitle = string.Format("Activate {0} web search", n.ActionWord),
Score = 50,
IcoPath = "Images/work.png",
Action = () => changeQuery(n.ActionWord + " "),
Action = (c) => changeQuery(n.ActionWord + " "),
DontHideWoxAfterSelect = true
}));

View file

@ -26,7 +26,7 @@ namespace Wox.Plugin.System
{
Title = string.Format("Search {0} for \"{1}\"", webSearch.Title, keyword),
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 SubTitle { get; set; }
public string IcoPath { get; set; }
public Action Action { get; set; }
public Action<ActionContext> Action { get; set; }
public int Score { 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
/// </summary>
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>
/// 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>
public string PluginDirectory { get; set; }

View file

@ -59,7 +59,7 @@
<ItemGroup>
<Compile Include="AllowedLanguage.cs" />
<Compile Include="IPlugin.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="PluginPair.cs" />
<Compile Include="PluginInitContext.cs" />
<Compile Include="PluginMetadata.cs" />
<Compile Include="PluginType.cs" />
@ -67,6 +67,7 @@
<Compile Include="PythonResult.cs" />
<Compile Include="Query.cs" />
<Compile Include="Result.cs" />
<Compile Include="ActionContext.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- 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.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Wox.Plugin;
namespace Wox.Helper
{
@ -28,17 +29,10 @@ namespace Wox.Helper
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>
/// Listens keyboard globally.
///
/// <remarks>Uses WH_KEYBOARD_LL.</remarks>
/// </summary>
public class KeyboardListener : IDisposable
@ -62,7 +56,7 @@ namespace Wox.Helper
hookId = InterceptKeys.SetHook(hookedLowLevelKeyboardProc);
}
private SpecialKeyState CheckModifiers()
public SpecialKeyState CheckModifiers()
{
SpecialKeyState state = new SpecialKeyState();
if ((InterceptKeys.GetKeyState(VK_SHIFT) & 0x8000) != 0)
@ -157,19 +151,6 @@ namespace Wox.Helper
[DllImport("user32.dll")]
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

View file

@ -185,7 +185,7 @@ namespace Wox
{
switch (args[0].ToLower())
{
case "reloadworkflows":
case "reloadplugin":
Plugins.Init();
break;
@ -250,7 +250,9 @@ namespace Wox
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:
HideWox();

View file

@ -40,7 +40,7 @@ namespace Wox.PluginLoader
PythonResult ps = pythonResult;
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);
}
@ -58,15 +58,22 @@ namespace Wox.PluginLoader
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 = { };
if (para != null && para.Length > 0)
{
paras = para.Select(o => new PyString(o)).ToArray();
}
dict["SpecialKeyState"] = specialKeyStateDict;
return dict;
}
private string InvokeFunc(string func, params PyObject[] paras)
{
string json;
IntPtr gs = PythonEngine.AcquireLock();
@ -93,6 +100,16 @@ namespace Wox.PluginLoader
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)
{

View file

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

View file

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

View file

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