From 5640b2157f2789cfeb226905a4b5312fe48ae358 Mon Sep 17 00:00:00 2001 From: Michael Eichhorn Date: Tue, 11 Apr 2017 14:34:04 +0100 Subject: [PATCH] ContextMenu 4: for non c# plugin #1366 --- JsonRPC/wox.py | 11 +++++++++-- Plugins/HelloWorldPython/main.py | 12 +++++++++++- Wox.Core/Plugin/ExecutablePlugin.cs | 11 +++++++++++ Wox.Core/Plugin/JsonRPCPlugin.cs | 27 ++++++++++++++++++++++----- Wox.Core/Plugin/PythonPlugin.cs | 11 +++++++++++ 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/JsonRPC/wox.py b/JsonRPC/wox.py index f6f3c9eb87..2e65a629d6 100644 --- a/JsonRPC/wox.py +++ b/JsonRPC/wox.py @@ -12,14 +12,15 @@ class Wox(object): def __init__(self): rpc_request = json.loads(sys.argv[1]) # proxy is not working now - self.proxy = rpc_request.get("proxy",{}) + self.proxy = rpc_request.get("proxy",{}) request_method_name = rpc_request.get("method") request_parameters = rpc_request.get("parameters") methods = inspect.getmembers(self, predicate=inspect.ismethod) request_method = dict(methods)[request_method_name] results = request_method(*request_parameters) - if request_method_name == "query": + + if request_method_name == "query" or request_method_name == "context_menu": print(json.dumps({"result": results})) def query(self,query): @@ -28,6 +29,12 @@ class Wox(object): """ return [] + def context_menu(self, data): + """ + optional context menu entries for a result + """ + return [] + def debug(self,msg): """ alert msg diff --git a/Plugins/HelloWorldPython/main.py b/Plugins/HelloWorldPython/main.py index e2f54050a7..e7b7edfc24 100644 --- a/Plugins/HelloWorldPython/main.py +++ b/Plugins/HelloWorldPython/main.py @@ -8,7 +8,17 @@ class HelloWorld(Wox): results = [] results.append({ "Title": "Hello World", - "SubTitle": "Query: {}".format(query), + "SubTitle": "Query: {}".format(query), + "IcoPath":"Images/app.ico", + "ContextData": "ctxData" + }) + return results + + def context_menu(self, data): + results = [] + results.append({ + "Title": "Context menu entry", + "SubTitle": "Data: {}".format(data), "IcoPath":"Images/app.ico" }) return results diff --git a/Wox.Core/Plugin/ExecutablePlugin.cs b/Wox.Core/Plugin/ExecutablePlugin.cs index 43992095eb..2753b00b3a 100644 --- a/Wox.Core/Plugin/ExecutablePlugin.cs +++ b/Wox.Core/Plugin/ExecutablePlugin.cs @@ -39,5 +39,16 @@ namespace Wox.Core.Plugin _startInfo.Arguments = $"\"{rpcRequest}\""; return Execute(_startInfo); } + + protected override string ExecuteContextMenu(Result selectedResult) { + JsonRPCServerRequestModel request = new JsonRPCServerRequestModel { + Method = "contextmenu", + Parameters = new object[] { selectedResult.ContextData }, + }; + + _startInfo.Arguments = $"\"{request}\""; + + return Execute(_startInfo); + } } } \ No newline at end of file diff --git a/Wox.Core/Plugin/JsonRPCPlugin.cs b/Wox.Core/Plugin/JsonRPCPlugin.cs index 7dec0084f5..5057ae472b 100644 --- a/Wox.Core/Plugin/JsonRPCPlugin.cs +++ b/Wox.Core/Plugin/JsonRPCPlugin.cs @@ -17,7 +17,7 @@ namespace Wox.Core.Plugin /// Represent the plugin that using JsonPRC /// every JsonRPC plugin should has its own plugin instance /// - internal abstract class JsonRPCPlugin : IPlugin + internal abstract class JsonRPCPlugin : IPlugin, IContextMenu { protected PluginInitContext context; public const string JsonRPC = "JsonRPC"; @@ -29,6 +29,7 @@ namespace Wox.Core.Plugin protected abstract string ExecuteQuery(Query query); protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest); + protected abstract string ExecuteContextMenu(Result selectedResult); public List Query(Query query) { @@ -44,7 +45,21 @@ namespace Wox.Core.Plugin } } - public List DeserializedResult(string output) + public List LoadContextMenus(Result selectedResult) + { + string output = ExecuteContextMenu(selectedResult); + try + { + return DeserializedResult(output); + } + catch (Exception e) + { + Log.Exception($"|JsonRPCPlugin.LoadContextMenus|Exception on result <{selectedResult}>", e); + return null; + } + } + + private List DeserializedResult(string output) { if (!String.IsNullOrEmpty(output)) { @@ -84,10 +99,12 @@ namespace Wox.Core.Plugin } return results; } - return null; + else + { + return null; + } } - private void ExecuteWoxAPI(string method, object[] parameters) { MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method); @@ -184,4 +201,4 @@ namespace Wox.Core.Plugin context = ctx; } } -} +} \ No newline at end of file diff --git a/Wox.Core/Plugin/PythonPlugin.cs b/Wox.Core/Plugin/PythonPlugin.cs index 5664d354ce..95a7d863e6 100644 --- a/Wox.Core/Plugin/PythonPlugin.cs +++ b/Wox.Core/Plugin/PythonPlugin.cs @@ -49,5 +49,16 @@ namespace Wox.Core.Plugin _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; return Execute(_startInfo); } + + protected override string ExecuteContextMenu(Result selectedResult) { + JsonRPCServerRequestModel request = new JsonRPCServerRequestModel { + Method = "context_menu", + Parameters = new object[] { selectedResult.ContextData }, + }; + _startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\""; + _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; + + return Execute(_startInfo); + } } } \ No newline at end of file