ContextMenu 4: for non c# plugin #1366

This commit is contained in:
Michael Eichhorn 2017-04-11 14:34:04 +01:00 committed by bao-qian
parent f3d79d906e
commit 5640b2157f
5 changed files with 64 additions and 8 deletions

View File

@ -12,14 +12,15 @@ class Wox(object):
def __init__(self): def __init__(self):
rpc_request = json.loads(sys.argv[1]) rpc_request = json.loads(sys.argv[1])
# proxy is not working now # 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_method_name = rpc_request.get("method")
request_parameters = rpc_request.get("parameters") request_parameters = rpc_request.get("parameters")
methods = inspect.getmembers(self, predicate=inspect.ismethod) methods = inspect.getmembers(self, predicate=inspect.ismethod)
request_method = dict(methods)[request_method_name] request_method = dict(methods)[request_method_name]
results = request_method(*request_parameters) 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})) print(json.dumps({"result": results}))
def query(self,query): def query(self,query):
@ -28,6 +29,12 @@ class Wox(object):
""" """
return [] return []
def context_menu(self, data):
"""
optional context menu entries for a result
"""
return []
def debug(self,msg): def debug(self,msg):
""" """
alert msg alert msg

View File

@ -8,7 +8,17 @@ class HelloWorld(Wox):
results = [] results = []
results.append({ results.append({
"Title": "Hello World", "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" "IcoPath":"Images/app.ico"
}) })
return results return results

View File

@ -39,5 +39,16 @@ namespace Wox.Core.Plugin
_startInfo.Arguments = $"\"{rpcRequest}\""; _startInfo.Arguments = $"\"{rpcRequest}\"";
return Execute(_startInfo); 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);
}
} }
} }

View File

@ -17,7 +17,7 @@ namespace Wox.Core.Plugin
/// Represent the plugin that using JsonPRC /// Represent the plugin that using JsonPRC
/// every JsonRPC plugin should has its own plugin instance /// every JsonRPC plugin should has its own plugin instance
/// </summary> /// </summary>
internal abstract class JsonRPCPlugin : IPlugin internal abstract class JsonRPCPlugin : IPlugin, IContextMenu
{ {
protected PluginInitContext context; protected PluginInitContext context;
public const string JsonRPC = "JsonRPC"; public const string JsonRPC = "JsonRPC";
@ -29,6 +29,7 @@ namespace Wox.Core.Plugin
protected abstract string ExecuteQuery(Query query); protected abstract string ExecuteQuery(Query query);
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest); protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
protected abstract string ExecuteContextMenu(Result selectedResult);
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
@ -44,7 +45,21 @@ namespace Wox.Core.Plugin
} }
} }
public List<Result> DeserializedResult(string output) public List<Result> 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<Result> DeserializedResult(string output)
{ {
if (!String.IsNullOrEmpty(output)) if (!String.IsNullOrEmpty(output))
{ {
@ -84,10 +99,12 @@ namespace Wox.Core.Plugin
} }
return results; return results;
} }
return null; else
{
return null;
}
} }
private void ExecuteWoxAPI(string method, object[] parameters) private void ExecuteWoxAPI(string method, object[] parameters)
{ {
MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method); MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method);
@ -184,4 +201,4 @@ namespace Wox.Core.Plugin
context = ctx; context = ctx;
} }
} }
} }

View File

@ -49,5 +49,16 @@ namespace Wox.Core.Plugin
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
return Execute(_startInfo); 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);
}
} }
} }