From bec52b81fd3832fe5f3be14972ee1fbced2483da Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Wed, 11 Dec 2019 02:21:50 +0200 Subject: [PATCH] adding conetxt menu start --- .../Wox.Plugin.Folder/ContextMenuLoader.cs | 113 ++++++++++++++++++ Plugins/Wox.Plugin.Folder/Main.cs | 14 ++- .../Wox.Plugin.Folder.csproj | 1 + 3 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 Plugins/Wox.Plugin.Folder/ContextMenuLoader.cs diff --git a/Plugins/Wox.Plugin.Folder/ContextMenuLoader.cs b/Plugins/Wox.Plugin.Folder/ContextMenuLoader.cs new file mode 100644 index 0000000000..e1a923b4ee --- /dev/null +++ b/Plugins/Wox.Plugin.Folder/ContextMenuLoader.cs @@ -0,0 +1,113 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Windows; + +namespace Wox.Plugin.Folder +{ + internal class ContextMenuLoader : IContextMenu + { + private readonly PluginInitContext _context; + + public ContextMenuLoader(PluginInitContext context) + { + _context = context; + } + + public List LoadContextMenus(Result selectedResult) + { + var contextMenus = new List(); + if (selectedResult.ContextData is SearchResult record) + { + string editorPath = "notepad.exe"; // TODO add the ability to create a custom editor + + var name = "Open With Editor: " + Path.GetFileNameWithoutExtension(editorPath); + contextMenus.Add(new Result + { + Title = name, + Action = _ => + { + try + { + Process.Start(editorPath, record.FullPath); + } + catch + { + // TODO: update this + _context.API.ShowMsg( + string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_start"), + record.FullPath), string.Empty, string.Empty); + return false; + } + + return true; + }, + IcoPath = editorPath + }); + + var icoPath = (record.Type == ResultType.File) ? "Images\\file.png" : "Images\\folder.png"; + contextMenus.Add(new Result + { + Title = _context.API.GetTranslation("wox_plugin_everything_copy_path"), + Action = (context) => + { + Clipboard.SetText(record.FullPath); + return true; + }, + IcoPath = icoPath + }); + + contextMenus.Add(new Result + { + Title = _context.API.GetTranslation("wox_plugin_everything_copy"), + Action = (context) => + { + Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection { record.FullPath }); + return true; + }, + IcoPath = icoPath + }); + + if (record.Type == ResultType.File || record.Type == ResultType.Folder) + contextMenus.Add(new Result + { + Title = _context.API.GetTranslation("wox_plugin_everything_delete"), + Action = (context) => + { + try + { + if (record.Type == ResultType.File) + System.IO.File.Delete(record.FullPath); + else + System.IO.Directory.Delete(record.FullPath); + } + catch + { + _context.API.ShowMsg(string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_delete"), record.FullPath), string.Empty, string.Empty); + return false; + } + + return true; + }, + IcoPath = icoPath + }); + + } + + return contextMenus; + } + } + + public class SearchResult + { + public string FullPath { get; set; } + public ResultType Type { get; set; } + } + + public enum ResultType + { + Volume, + Folder, + File + } +} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Folder/Main.cs b/Plugins/Wox.Plugin.Folder/Main.cs index 4f8fac60d9..baac79b1ef 100644 --- a/Plugins/Wox.Plugin.Folder/Main.cs +++ b/Plugins/Wox.Plugin.Folder/Main.cs @@ -9,13 +9,14 @@ using Wox.Infrastructure.Storage; namespace Wox.Plugin.Folder { - public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable + public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IContextMenu { private static List _driverNames; private PluginInitContext _context; private readonly Settings _settings; private readonly PluginJsonStorage _storage; + private readonly IContextMenu _contextMenuLoader = new ContextMenuLoader(); public Main() { @@ -104,7 +105,8 @@ namespace Wox.Plugin.Folder string changeTo = path.EndsWith("\\") ? path : path + "\\"; _context.API.ChangeQuery(string.IsNullOrEmpty(queryActionKeyword)? changeTo : queryActionKeyword + " " + changeTo); return false; - } + }, + ContextData = new SearchResult { Type = ResultType.Folder, FullPath = path } }; } @@ -226,7 +228,8 @@ namespace Wox.Plugin.Folder } return true; - } + }, + ContextData = new SearchResult { Type = ResultType.File, FullPath = filePath} }; return result; } @@ -263,5 +266,10 @@ namespace Wox.Plugin.Folder { return _context.API.GetTranslation("wox_plugin_folder_plugin_description"); } + + public List LoadContextMenus(Result selectedResult) + { + return _contextMenuLoader.LoadContextMenus(selectedResult); + } } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Folder/Wox.Plugin.Folder.csproj b/Plugins/Wox.Plugin.Folder/Wox.Plugin.Folder.csproj index 6a05ebd26f..d29dc9874b 100644 --- a/Plugins/Wox.Plugin.Folder/Wox.Plugin.Folder.csproj +++ b/Plugins/Wox.Plugin.Folder/Wox.Plugin.Folder.csproj @@ -58,6 +58,7 @@ Properties\SolutionAssemblyInfo.cs +