mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-14 19:49:15 +08:00
Merge branch 'master' into dev
This commit is contained in:
commit
d3169b10f8
@ -19,7 +19,8 @@ class Wox(object):
|
||||
|
||||
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
|
||||
|
@ -9,6 +9,16 @@ class HelloWorld(Wox):
|
||||
results.append({
|
||||
"Title": "Hello World",
|
||||
"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
|
||||
|
@ -204,25 +204,30 @@ namespace Wox.Plugin.Program.Programs
|
||||
|
||||
private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes)
|
||||
{
|
||||
if (Directory.Exists(directory))
|
||||
if (!Directory.Exists(directory))
|
||||
return new string[] { };
|
||||
|
||||
var ds = Directory.GetDirectories(directory);
|
||||
|
||||
var paths = ds.SelectMany(d =>
|
||||
{
|
||||
IEnumerable<string> files;
|
||||
try
|
||||
{
|
||||
files = Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories);
|
||||
var paths_for_suffixes = suffixes.SelectMany(s =>
|
||||
{
|
||||
var pattern = $"*.{s}";
|
||||
var ps = Directory.EnumerateFiles(d, pattern, SearchOption.AllDirectories);
|
||||
return ps;
|
||||
});
|
||||
return paths_for_suffixes;
|
||||
}
|
||||
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
||||
{
|
||||
Log.Exception($"|Program.Win32.ProgramPaths|Can't parse directory <{directory}>", e);
|
||||
return new string[] { };
|
||||
}
|
||||
files = files.Where(f => suffixes.Contains(Extension(f)));
|
||||
return files;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new string[] { };
|
||||
Log.Exception($"|Program.Win32.ProgramPaths|Don't have permission on <{directory}>", e);
|
||||
return new List<string>();
|
||||
}
|
||||
});
|
||||
return paths;
|
||||
}
|
||||
|
||||
private static string Extension(string path)
|
||||
|
@ -1,3 +1,7 @@
|
||||
NOTE
|
||||
====
|
||||
This project will not be maintained by @happlebao for about 2 month due to personal reason.
|
||||
|
||||
WoX
|
||||
===
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -56,7 +56,7 @@ namespace Wox.Core.Plugin
|
||||
string rpc = string.Empty;
|
||||
if (Parameters != null && Parameters.Length > 0)
|
||||
{
|
||||
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParamterByType(o) + ","));
|
||||
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParameterByType(o) + ","));
|
||||
parameters = parameters.Substring(0, parameters.Length - 1) + "]";
|
||||
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
|
||||
}
|
||||
@ -69,25 +69,27 @@ namespace Wox.Core.Plugin
|
||||
|
||||
}
|
||||
|
||||
private string GetParamterByType(object paramter)
|
||||
private string GetParameterByType(object parameter)
|
||||
{
|
||||
|
||||
if (paramter is string)
|
||||
{
|
||||
return string.Format(@"\""{0}\""", RepalceEscapes(paramter.ToString()));
|
||||
if (parameter == null) {
|
||||
return "null";
|
||||
}
|
||||
if (paramter is int || paramter is float || paramter is double)
|
||||
if (parameter is string)
|
||||
{
|
||||
return string.Format(@"{0}", paramter);
|
||||
return string.Format(@"\""{0}\""", ReplaceEscapes(parameter.ToString()));
|
||||
}
|
||||
if (paramter is bool)
|
||||
if (parameter is int || parameter is float || parameter is double)
|
||||
{
|
||||
return string.Format(@"{0}", paramter.ToString().ToLower());
|
||||
return string.Format(@"{0}", parameter);
|
||||
}
|
||||
return paramter.ToString();
|
||||
if (parameter is bool)
|
||||
{
|
||||
return string.Format(@"{0}", parameter.ToString().ToLower());
|
||||
}
|
||||
return parameter.ToString();
|
||||
}
|
||||
|
||||
private string RepalceEscapes(string str)
|
||||
private string ReplaceEscapes(string str)
|
||||
{
|
||||
return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo
|
||||
.Replace(@"\", @"\\") //Escapes itself when passed to client
|
||||
|
@ -17,7 +17,7 @@ namespace Wox.Core.Plugin
|
||||
/// Represent the plugin that using JsonPRC
|
||||
/// every JsonRPC plugin should has its own plugin instance
|
||||
/// </summary>
|
||||
internal abstract class JsonRPCPlugin : IPlugin
|
||||
internal abstract class JsonRPCPlugin : IPlugin, IContextMenu
|
||||
{
|
||||
protected PluginInitContext context;
|
||||
public const string JsonRPC = "JsonRPC";
|
||||
@ -29,13 +29,39 @@ namespace Wox.Core.Plugin
|
||||
|
||||
protected abstract string ExecuteQuery(Query query);
|
||||
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
|
||||
protected abstract string ExecuteContextMenu(Result selectedResult);
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
string output = ExecuteQuery(query);
|
||||
if (!String.IsNullOrEmpty(output))
|
||||
{
|
||||
try
|
||||
{
|
||||
return DeserializedResult(output);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|JsonRPCPlugin.Query|Exception when query <{query}>", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
List<Result> results = new List<Result>();
|
||||
|
||||
@ -73,13 +99,11 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
return results;
|
||||
}
|
||||
catch (Exception e)
|
||||
else
|
||||
{
|
||||
Log.Exception($"|JsonRPCPlugin.Query|Exception when query <{query}>", e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteWoxAPI(string method, object[] parameters)
|
||||
{
|
||||
@ -149,7 +173,7 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
else if (result.StartsWith("DEBUG:"))
|
||||
{
|
||||
MessageBox.Show(new Form {TopMost = true}, result.Substring(6));
|
||||
MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
|
||||
return string.Empty;
|
||||
}
|
||||
else
|
||||
|
@ -83,7 +83,7 @@ namespace Wox.Core.Plugin
|
||||
// Plugins.Initialize();
|
||||
//}
|
||||
if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" +
|
||||
" Restart Wox to take effect?",
|
||||
"Restart Wox to take effect?",
|
||||
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
|
||||
{
|
||||
PluginManager.API.RestarApp();
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user