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.Diagnostics;
using System.IO;
using System.Windows;
using Wox.Infrastructure.Logger;
namespace Wox.Plugin.Folder
{
@@ -19,51 +21,55 @@ namespace Wox.Plugin.Folder
var contextMenus = new List<Result>();
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
{
Title = name,
Action = _ =>
Title = "Copy Path",
SubTitle = $"Copy the path of {fileOrFolder} into the clipboard",
Action = (context) =>
{
try
{
Process.Start(editorPath, record.FullPath);
Clipboard.SetText(record.FullPath);
return true;
}
catch
catch (Exception e)
{
// TODO: update this
_context.API.ShowMsg(
string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_start"),
record.FullPath), string.Empty, string.Empty);
var message = "Fail to set text in clipboard";
LogException(message, e);
_context.API.ShowMsg(message);
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"),
Title = "Copy",
SubTitle = $"Copy the {fileOrFolder} to the clipboard",
Action = (context) =>
{
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection { record.FullPath });
return true;
try
{
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
});
@@ -71,19 +77,21 @@ namespace Wox.Plugin.Folder
if (record.Type == ResultType.File || record.Type == ResultType.Folder)
contextMenus.Add(new Result
{
Title = _context.API.GetTranslation("wox_plugin_everything_delete"),
Title = "Delete",
Action = (context) =>
{
try
{
if (record.Type == ResultType.File)
System.IO.File.Delete(record.FullPath);
File.Delete(record.FullPath);
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;
}
@@ -96,6 +104,64 @@ namespace Wox.Plugin.Folder
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

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

View File

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