Skip to content

Commit 2c87c00

Browse files
committed
added context menu support (same as everything plugin)
1 parent 92e4b5c commit 2c87c00

File tree

4 files changed

+107
-33
lines changed

4 files changed

+107
-33
lines changed

Plugins/Wox.Plugin.Folder/ContextMenuLoader.cs

Lines changed: 98 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
45
using System.Windows;
6+
using Wox.Infrastructure.Logger;
57

68
namespace Wox.Plugin.Folder
79
{
@@ -19,71 +21,77 @@ public List<Result> LoadContextMenus(Result selectedResult)
1921
var contextMenus = new List<Result>();
2022
if (selectedResult.ContextData is SearchResult record)
2123
{
22-
string editorPath = "notepad.exe"; // TODO add the ability to create a custom editor
24+
if (record.Type == ResultType.File)
25+
{
26+
contextMenus.Add(CreateOpenWithEditorResult(record));
27+
contextMenus.Add(CreateOpenContainingFolderResult(record));
28+
}
2329

24-
var name = "Open With Editor: " + Path.GetFileNameWithoutExtension(editorPath);
30+
var icoPath = (record.Type == ResultType.File) ? Main.FileImagePath : Main.FolderImagePath;
31+
var fileOrFolder = (record.Type == ResultType.File) ? "file" : "folder";
2532
contextMenus.Add(new Result
2633
{
27-
Title = name,
28-
Action = _ =>
34+
Title = "Copy Path",
35+
SubTitle = $"Copy the path of {fileOrFolder} into the clipboard",
36+
Action = (context) =>
2937
{
3038
try
3139
{
32-
Process.Start(editorPath, record.FullPath);
40+
Clipboard.SetText(record.FullPath);
41+
return true;
3342
}
34-
catch
43+
catch (Exception e)
3544
{
36-
// TODO: update this
37-
_context.API.ShowMsg(
38-
string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_start"),
39-
record.FullPath), string.Empty, string.Empty);
45+
var message = "Fail to set text in clipboard";
46+
LogException(message, e);
47+
_context.API.ShowMsg(message);
4048
return false;
4149
}
42-
43-
return true;
44-
},
45-
IcoPath = editorPath
46-
});
47-
48-
var icoPath = (record.Type == ResultType.File) ? "Images\\file.png" : "Images\\folder.png";
49-
contextMenus.Add(new Result
50-
{
51-
Title = _context.API.GetTranslation("wox_plugin_everything_copy_path"),
52-
Action = (context) =>
53-
{
54-
Clipboard.SetText(record.FullPath);
55-
return true;
5650
},
5751
IcoPath = icoPath
5852
});
5953

6054
contextMenus.Add(new Result
6155
{
62-
Title = _context.API.GetTranslation("wox_plugin_everything_copy"),
56+
Title = "Copy",
57+
SubTitle = $"Copy the {fileOrFolder} to the clipboard",
6358
Action = (context) =>
6459
{
65-
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection { record.FullPath });
66-
return true;
60+
try
61+
{
62+
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection { record.FullPath });
63+
return true;
64+
}
65+
catch (Exception e)
66+
{
67+
var message = $"Fail to set {fileOrFolder} in clipboard";
68+
LogException(message, e);
69+
_context.API.ShowMsg(message);
70+
return false;
71+
}
72+
6773
},
6874
IcoPath = icoPath
6975
});
7076

7177
if (record.Type == ResultType.File || record.Type == ResultType.Folder)
7278
contextMenus.Add(new Result
7379
{
74-
Title = _context.API.GetTranslation("wox_plugin_everything_delete"),
80+
Title = "Delete",
7581
Action = (context) =>
7682
{
7783
try
7884
{
7985
if (record.Type == ResultType.File)
80-
System.IO.File.Delete(record.FullPath);
86+
File.Delete(record.FullPath);
8187
else
82-
System.IO.Directory.Delete(record.FullPath);
88+
Directory.Delete(record.FullPath);
8389
}
84-
catch
90+
catch(Exception e)
8591
{
86-
_context.API.ShowMsg(string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_delete"), record.FullPath), string.Empty, string.Empty);
92+
var message = $"Fail to delete {fileOrFolder} at {record.FullPath}";
93+
LogException(message, e);
94+
_context.API.ShowMsg(message);
8795
return false;
8896
}
8997

@@ -96,6 +104,64 @@ public List<Result> LoadContextMenus(Result selectedResult)
96104

97105
return contextMenus;
98106
}
107+
108+
private Result CreateOpenContainingFolderResult(SearchResult record)
109+
{
110+
return new Result
111+
{
112+
Title = "Open containing folder",
113+
Action = _ =>
114+
{
115+
try
116+
{
117+
Process.Start("explorer.exe", $" /select,\"{record.FullPath}\"");
118+
}
119+
catch(Exception e)
120+
{
121+
var message = $"Fail to open file at {record.FullPath}";
122+
LogException(message, e);
123+
_context.API.ShowMsg(message);
124+
return false;
125+
}
126+
127+
return true;
128+
},
129+
IcoPath = Main.FolderImagePath
130+
};
131+
}
132+
133+
134+
private Result CreateOpenWithEditorResult(SearchResult record)
135+
{
136+
string editorPath = "notepad.exe"; // TODO add the ability to create a custom editor
137+
138+
var name = "Open With Editor: " + Path.GetFileNameWithoutExtension(editorPath);
139+
return new Result
140+
{
141+
Title = name,
142+
Action = _ =>
143+
{
144+
try
145+
{
146+
Process.Start(editorPath, record.FullPath);
147+
return true;
148+
}
149+
catch (Exception e)
150+
{
151+
var message = $"Fail to editor for file at {record.FullPath}";
152+
LogException(message, e);
153+
_context.API.ShowMsg(message);
154+
return false;
155+
}
156+
},
157+
IcoPath = editorPath
158+
};
159+
}
160+
161+
public void LogException(string message, Exception e)
162+
{
163+
Log.Exception($"|Wox.Plugin.Folder.ContextMenu|{message}", e);
164+
}
99165
}
100166

101167
public class SearchResult
290 Bytes
Loading

Plugins/Wox.Plugin.Folder/Main.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ namespace Wox.Plugin.Folder
1212
{
1313
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IContextMenu
1414
{
15+
public const string FolderImagePath = "Images\\folder.png";
16+
public const string FileImagePath = "Images\\file.png";
17+
18+
1519
private static List<string> _driverNames;
1620
private PluginInitContext _context;
1721

1822
private readonly Settings _settings;
1923
private readonly PluginJsonStorage<Settings> _storage;
20-
private readonly IContextMenu _contextMenuLoader = new ContextMenuLoader();
24+
private IContextMenu _contextMenuLoader;
2125

2226
public Main()
2327
{
@@ -38,6 +42,7 @@ public Control CreateSettingPanel()
3842
public void Init(PluginInitContext context)
3943
{
4044
_context = context;
45+
_contextMenuLoader = new ContextMenuLoader(context);
4146
InitialDriverList();
4247
}
4348

Plugins/Wox.Plugin.Folder/Wox.Plugin.Folder.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181
<None Include="Images\copy.png">
8282
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8383
</None>
84+
<Content Include="Images\file.png">
85+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
86+
</Content>
8487
<Content Include="Languages\en.xaml">
8588
<Generator>MSBuild:Compile</Generator>
8689
<SubType>Designer</SubType>

0 commit comments

Comments
 (0)