added context menu support (same as everything plugin)

This commit is contained in:
AT
2019-12-13 03:20:16 +02:00
parent 92e4b5cc16
commit 2c87c00906
4 changed files with 107 additions and 33 deletions

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Windows; using System.Windows;
using Wox.Infrastructure.Logger;
namespace Wox.Plugin.Folder namespace Wox.Plugin.Folder
{ {
@@ -19,51 +21,55 @@ namespace Wox.Plugin.Folder
var contextMenus = new List<Result>(); var contextMenus = new List<Result>();
if (selectedResult.ContextData is SearchResult record) if (selectedResult.ContextData is SearchResult record)
{ {
string editorPath = "notepad.exe"; // TODO add the ability to create a custom editor if (record.Type == ResultType.File)
{
contextMenus.Add(CreateOpenWithEditorResult(record));
contextMenus.Add(CreateOpenContainingFolderResult(record));
}
var name = "Open With Editor: " + Path.GetFileNameWithoutExtension(editorPath); var icoPath = (record.Type == ResultType.File) ? Main.FileImagePath : Main.FolderImagePath;
var fileOrFolder = (record.Type == ResultType.File) ? "file" : "folder";
contextMenus.Add(new Result contextMenus.Add(new Result
{ {
Title = name, Title = "Copy Path",
Action = _ => SubTitle = $"Copy the path of {fileOrFolder} into the clipboard",
Action = (context) =>
{ {
try try
{ {
Process.Start(editorPath, record.FullPath); Clipboard.SetText(record.FullPath);
return true;
} }
catch catch (Exception e)
{ {
// TODO: update this var message = "Fail to set text in clipboard";
_context.API.ShowMsg( LogException(message, e);
string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_start"), _context.API.ShowMsg(message);
record.FullPath), string.Empty, string.Empty);
return false; 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 IcoPath = icoPath
}); });
contextMenus.Add(new Result contextMenus.Add(new Result
{ {
Title = _context.API.GetTranslation("wox_plugin_everything_copy"), Title = "Copy",
SubTitle = $"Copy the {fileOrFolder} to the clipboard",
Action = (context) => Action = (context) =>
{ {
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection { record.FullPath }); try
return true; {
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection { record.FullPath });
return true;
}
catch (Exception e)
{
var message = $"Fail to set {fileOrFolder} in clipboard";
LogException(message, e);
_context.API.ShowMsg(message);
return false;
}
}, },
IcoPath = icoPath IcoPath = icoPath
}); });
@@ -71,19 +77,21 @@ namespace Wox.Plugin.Folder
if (record.Type == ResultType.File || record.Type == ResultType.Folder) if (record.Type == ResultType.File || record.Type == ResultType.Folder)
contextMenus.Add(new Result contextMenus.Add(new Result
{ {
Title = _context.API.GetTranslation("wox_plugin_everything_delete"), Title = "Delete",
Action = (context) => Action = (context) =>
{ {
try try
{ {
if (record.Type == ResultType.File) if (record.Type == ResultType.File)
System.IO.File.Delete(record.FullPath); File.Delete(record.FullPath);
else else
System.IO.Directory.Delete(record.FullPath); Directory.Delete(record.FullPath);
} }
catch catch(Exception e)
{ {
_context.API.ShowMsg(string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_delete"), record.FullPath), string.Empty, string.Empty); var message = $"Fail to delete {fileOrFolder} at {record.FullPath}";
LogException(message, e);
_context.API.ShowMsg(message);
return false; return false;
} }
@@ -96,6 +104,64 @@ namespace Wox.Plugin.Folder
return contextMenus; return contextMenus;
} }
private Result CreateOpenContainingFolderResult(SearchResult record)
{
return new Result
{
Title = "Open containing folder",
Action = _ =>
{
try
{
Process.Start("explorer.exe", $" /select,\"{record.FullPath}\"");
}
catch(Exception e)
{
var message = $"Fail to open file at {record.FullPath}";
LogException(message, e);
_context.API.ShowMsg(message);
return false;
}
return true;
},
IcoPath = Main.FolderImagePath
};
}
private Result CreateOpenWithEditorResult(SearchResult record)
{
string editorPath = "notepad.exe"; // TODO add the ability to create a custom editor
var name = "Open With Editor: " + Path.GetFileNameWithoutExtension(editorPath);
return new Result
{
Title = name,
Action = _ =>
{
try
{
Process.Start(editorPath, record.FullPath);
return true;
}
catch (Exception e)
{
var message = $"Fail to editor for file at {record.FullPath}";
LogException(message, e);
_context.API.ShowMsg(message);
return false;
}
},
IcoPath = editorPath
};
}
public void LogException(string message, Exception e)
{
Log.Exception($"|Wox.Plugin.Folder.ContextMenu|{message}", e);
}
} }
public class SearchResult public class SearchResult

Binary file not shown.

After

Width:  |  Height:  |  Size: 290 B

View File

@@ -12,12 +12,16 @@ namespace Wox.Plugin.Folder
{ {
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IContextMenu public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IContextMenu
{ {
public const string FolderImagePath = "Images\\folder.png";
public const string FileImagePath = "Images\\file.png";
private static List<string> _driverNames; private static List<string> _driverNames;
private PluginInitContext _context; private PluginInitContext _context;
private readonly Settings _settings; private readonly Settings _settings;
private readonly PluginJsonStorage<Settings> _storage; private readonly PluginJsonStorage<Settings> _storage;
private readonly IContextMenu _contextMenuLoader = new ContextMenuLoader(); private IContextMenu _contextMenuLoader;
public Main() public Main()
{ {
@@ -38,6 +42,7 @@ namespace Wox.Plugin.Folder
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
_context = context; _context = context;
_contextMenuLoader = new ContextMenuLoader(context);
InitialDriverList(); InitialDriverList();
} }

View File

@@ -81,6 +81,9 @@
<None Include="Images\copy.png"> <None Include="Images\copy.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<Content Include="Images\file.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Languages\en.xaml"> <Content Include="Languages\en.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>