Add IContextMenu interface & lazy load context menus

This commit is contained in:
qianlifeng
2015-02-07 23:49:46 +08:00
parent c24e216f26
commit bc7dce6026
10 changed files with 196 additions and 166 deletions

View File

@@ -14,7 +14,7 @@ using Control = System.Windows.Controls.Control;
namespace Wox.Plugin.CMD
{
public class CMD : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IExclusiveQuery
public class CMD : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IExclusiveQuery,IContextMenu
{
private PluginInitContext context;
private bool WinRStroked;
@@ -70,8 +70,7 @@ namespace Wox.Plugin.CMD
{
ExecuteCmd(m);
return true;
},
ContextMenu = GetContextMenus(m)
}
}));
}
}
@@ -102,8 +101,7 @@ namespace Wox.Plugin.CMD
{
ExecuteCmd(m.Key);
return true;
},
ContextMenu = GetContextMenus(m.Key)
}
};
return ret;
}).Where(o => o != null).Take(4);
@@ -122,8 +120,7 @@ namespace Wox.Plugin.CMD
{
ExecuteCmd(cmd);
return true;
},
ContextMenu = GetContextMenus(cmd)
}
};
return result;
@@ -141,30 +138,11 @@ namespace Wox.Plugin.CMD
{
ExecuteCmd(m.Key);
return true;
},
ContextMenu = GetContextMenus(m.Key)
}
}).Take(5);
return history.ToList();
}
private List<Result> GetContextMenus(string cmd)
{
return new List<Result>()
{
new Result()
{
Title = "Run As Administrator",
Action = c =>
{
context.API.HideApp();
ExecuteCmd(cmd, true);
return true;
},
IcoPath = "Images/cmd.png"
}
};
}
private void ExecuteCmd(string cmd, bool runAsAdministrator = false)
{
if (context.API.ShellRun(cmd, runAsAdministrator))
@@ -233,5 +211,23 @@ namespace Wox.Plugin.CMD
{
return query.Search.StartsWith(">");
}
public List<Result> LoadContextMenus(Result selectedResult)
{
return new List<Result>()
{
new Result()
{
Title = "Run As Administrator",
Action = c =>
{
context.API.HideApp();
ExecuteCmd(selectedResult.Title, true);
return true;
},
IcoPath = "Images/cmd.png"
}
};
}
}
}

View File

@@ -6,10 +6,11 @@ using System.Linq;
using Wox.Infrastructure;
using System.Reflection;
using Wox.Plugin.Everything.Everything;
using Wox.Plugin.Features;
namespace Wox.Plugin.Everything
{
public class Main : IPlugin, IPluginI18n
public class Main : IPlugin, IPluginI18n,IContextMenu
{
PluginInitContext context;
EverythingAPI api = new EverythingAPI();
@@ -24,7 +25,7 @@ namespace Wox.Plugin.Everything
var keyword = query.Search;
if (ContextMenuStorage.Instance.MaxSearchCount <= 0)
{
ContextMenuStorage.Instance.MaxSearchCount = 100;
ContextMenuStorage.Instance.MaxSearchCount = 50;
ContextMenuStorage.Instance.Save();
}
@@ -50,7 +51,7 @@ namespace Wox.Plugin.Everything
context.API.ShellRun(path);
return true;
};
r.ContextMenu = GetContextMenu(s);
r.ContextData = s;
results.Add(r);
}
}
@@ -110,43 +111,6 @@ namespace Wox.Plugin.Everything
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern int LoadLibrary(string name);
private List<Result> GetContextMenu(SearchResult record)
{
List<Result> contextMenus = new List<Result>();
List<ContextMenu> availableContextMenus = new List<ContextMenu>();
availableContextMenus.AddRange(GetDefaultContextMenu());
availableContextMenus.AddRange(ContextMenuStorage.Instance.ContextMenus);
if (record.Type == ResultType.File)
{
foreach (ContextMenu contextMenu in availableContextMenus)
{
contextMenus.Add(new Result()
{
Title = contextMenu.Name,
Action = _ =>
{
string argument = contextMenu.Argument.Replace("{path}", record.FullPath);
try
{
Process.Start(contextMenu.Command, argument);
}
catch
{
context.API.ShowMsg(string.Format(context.API.GetTranslation("wox_plugin_everything_canot_start"), record.FullPath), string.Empty, string.Empty);
return false;
}
return true;
},
IcoPath = contextMenu.ImagePath
});
}
}
return contextMenus;
}
private List<ContextMenu> GetDefaultContextMenu()
{
List<ContextMenu> defaultContextMenus = new List<ContextMenu>();
@@ -221,5 +185,45 @@ namespace Wox.Plugin.Everything
{
return context.API.GetTranslation("wox_plugin_everything_plugin_description");
}
public List<Result> LoadContextMenus(Result selectedResult)
{
SearchResult record = selectedResult.ContextData as SearchResult;
List<Result> contextMenus = new List<Result>();
if(record == null) return contextMenus;
List<ContextMenu> availableContextMenus = new List<ContextMenu>();
availableContextMenus.AddRange(GetDefaultContextMenu());
availableContextMenus.AddRange(ContextMenuStorage.Instance.ContextMenus);
if (record.Type == ResultType.File)
{
foreach (ContextMenu contextMenu in availableContextMenus)
{
var menu = contextMenu;
contextMenus.Add(new Result()
{
Title = contextMenu.Name,
Action = _ =>
{
string argument = menu.Argument.Replace("{path}", record.FullPath);
try
{
Process.Start(menu.Command, argument);
}
catch
{
context.API.ShowMsg(string.Format(context.API.GetTranslation("wox_plugin_everything_canot_start"), record.FullPath), string.Empty, string.Empty);
return false;
}
return true;
},
IcoPath = contextMenu.ImagePath
});
}
}
return contextMenus;
}
}
}

View File

@@ -1,6 +1,6 @@
{
"ID":"D2D2C23B084D411DB66FE0C79D6C2A6E",
"ActionKeyword":"f",
"ActionKeyword":"*",
"Name":"Everything",
"Description":"Search Everything",
"Author":"qianlifeng,orzfly",

View File

@@ -9,10 +9,11 @@ using System.Windows;
using Wox.Infrastructure;
using Wox.Plugin.Program.ProgramSources;
using IWshRuntimeLibrary;
using Wox.Plugin.Features;
namespace Wox.Plugin.Program
{
public class Programs : ISettingProvider, IPlugin, IPluginI18n
public class Programs : ISettingProvider, IPlugin, IPluginI18n, IContextMenu
{
private static object lockObject = new object();
private static List<Program> programs = new List<Program>();
@@ -38,46 +39,12 @@ namespace Wox.Plugin.Program
SubTitle = c.ExecutePath,
IcoPath = c.IcoPath,
Score = c.Score,
ContextData = c,
Action = (e) =>
{
context.API.HideApp();
context.API.ShellRun(c.ExecutePath);
return true;
},
ContextMenu = new List<Result>()
{
new Result()
{
Title = context.API.GetTranslation("wox_plugin_program_run_as_administrator"),
Action = _ =>
{
context.API.HideApp();
context.API.ShellRun(c.ExecutePath,true);
return true;
},
IcoPath = "Images/cmd.png"
},
new Result()
{
Title = context.API.GetTranslation("wox_plugin_program_open_containing_folder"),
Action = _ =>
{
context.API.HideApp();
String Path=c.ExecutePath;
//check if shortcut
if (Path.EndsWith(".lnk"))
{
//get location of shortcut
Path = ResolveShortcut(Path);
}
//get parent folder
Path=System.IO.Directory.GetParent(Path).FullName;
//open the folder
context.API.ShellRun("explorer.exe "+Path,false);
return true;
},
IcoPath = "Images/folder.png"
}
}
}).ToList();
}
@@ -117,7 +84,7 @@ namespace Wox.Plugin.Program
void API_ResultItemDropEvent(Result result, IDataObject dropObject, DragEventArgs e)
{
e.Handled = true;
}
@@ -232,5 +199,46 @@ namespace Wox.Plugin.Program
{
return context.API.GetTranslation("wox_plugin_program_plugin_description");
}
public List<Result> LoadContextMenus(Result selectedResult)
{
Program p = selectedResult.ContextData as Program;
List<Result> contextMenus = new List<Result>()
{
new Result()
{
Title = context.API.GetTranslation("wox_plugin_program_run_as_administrator"),
Action = _ =>
{
context.API.HideApp();
context.API.ShellRun(p.ExecutePath, true);
return true;
},
IcoPath = "Images/cmd.png"
},
new Result()
{
Title = context.API.GetTranslation("wox_plugin_program_open_containing_folder"),
Action = _ =>
{
context.API.HideApp();
String Path = p.ExecutePath;
//check if shortcut
if (Path.EndsWith(".lnk"))
{
//get location of shortcut
Path = ResolveShortcut(Path);
}
//get parent folder
Path = Directory.GetParent(Path).FullName;
//open the folder
context.API.ShellRun("explorer.exe " + Path, false);
return true;
},
IcoPath = "Images/folder.png"
}
};
return contextMenus;
}
}
}
}