using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Windows; using Wox.Infrastructure.Logger; using Wox.Plugin; using Microsoft.Plugin.Indexer.SearchHelper; using System.Reflection; namespace Microsoft.Plugin.Indexer { internal class ContextMenuLoader : IContextMenu { private readonly PluginInitContext _context; public enum ResultType { Folder, File } public ContextMenuLoader(PluginInitContext context) { _context = context; } public List LoadContextMenus(Result selectedResult) { var contextMenus = new List(); if (selectedResult.ContextData is SearchResult record) { ResultType type = Path.HasExtension(record.Path) ? ResultType.File : ResultType.Folder; if (type == ResultType.File) { contextMenus.Add(CreateOpenContainingFolderResult(record)); } var fileOrFolder = (type == ResultType.File) ? "file" : "folder"; contextMenus.Add(new ContextMenuResult { PluginName = Assembly.GetExecutingAssembly().GetName().Name, Title = "Copy path", Glyph = "\xE8C8", FontFamily = "Segoe MDL2 Assets", SubTitle = $"Copy the current {fileOrFolder} path to clipboard", AcceleratorKey = "C", AcceleratorModifiers = "Control", Action = (context) => { try { Clipboard.SetText(record.Path); return true; } catch (Exception e) { var message = "Fail to set text in clipboard"; LogException(message, e); _context.API.ShowMsg(message); return false; } } }); } return contextMenus; } private ContextMenuResult CreateOpenContainingFolderResult(SearchResult record) { return new ContextMenuResult { PluginName = Assembly.GetExecutingAssembly().GetName().Name, Title = "Open containing folder", Glyph = "\xE838", FontFamily = "Segoe MDL2 Assets", AcceleratorKey = "E", AcceleratorModifiers = "Control,Shift", Action = _ => { try { Process.Start("explorer.exe", $" /select,\"{record.Path}\""); } catch(Exception e) { var message = $"Fail to open file at {record.Path}"; LogException(message, e); _context.API.ShowMsg(message); return false; } return true; }, }; } public void LogException(string message, Exception e) { Log.Exception($"|Microsoft.Plugin.Folder.ContextMenu|{message}", e); } } }