Skip to content

Commit 725eee7

Browse files
authored
Merge pull request #103 from theClueless/folderPluginUpdates
Folderpluginupdates
2 parents e66b646 + 2c87c00 commit 725eee7

File tree

5 files changed

+221
-5
lines changed

5 files changed

+221
-5
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Windows;
6+
using Wox.Infrastructure.Logger;
7+
8+
namespace Wox.Plugin.Folder
9+
{
10+
internal class ContextMenuLoader : IContextMenu
11+
{
12+
private readonly PluginInitContext _context;
13+
14+
public ContextMenuLoader(PluginInitContext context)
15+
{
16+
_context = context;
17+
}
18+
19+
public List<Result> LoadContextMenus(Result selectedResult)
20+
{
21+
var contextMenus = new List<Result>();
22+
if (selectedResult.ContextData is SearchResult record)
23+
{
24+
if (record.Type == ResultType.File)
25+
{
26+
contextMenus.Add(CreateOpenWithEditorResult(record));
27+
contextMenus.Add(CreateOpenContainingFolderResult(record));
28+
}
29+
30+
var icoPath = (record.Type == ResultType.File) ? Main.FileImagePath : Main.FolderImagePath;
31+
var fileOrFolder = (record.Type == ResultType.File) ? "file" : "folder";
32+
contextMenus.Add(new Result
33+
{
34+
Title = "Copy Path",
35+
SubTitle = $"Copy the path of {fileOrFolder} into the clipboard",
36+
Action = (context) =>
37+
{
38+
try
39+
{
40+
Clipboard.SetText(record.FullPath);
41+
return true;
42+
}
43+
catch (Exception e)
44+
{
45+
var message = "Fail to set text in clipboard";
46+
LogException(message, e);
47+
_context.API.ShowMsg(message);
48+
return false;
49+
}
50+
},
51+
IcoPath = icoPath
52+
});
53+
54+
contextMenus.Add(new Result
55+
{
56+
Title = "Copy",
57+
SubTitle = $"Copy the {fileOrFolder} to the clipboard",
58+
Action = (context) =>
59+
{
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+
73+
},
74+
IcoPath = icoPath
75+
});
76+
77+
if (record.Type == ResultType.File || record.Type == ResultType.Folder)
78+
contextMenus.Add(new Result
79+
{
80+
Title = "Delete",
81+
Action = (context) =>
82+
{
83+
try
84+
{
85+
if (record.Type == ResultType.File)
86+
File.Delete(record.FullPath);
87+
else
88+
Directory.Delete(record.FullPath);
89+
}
90+
catch(Exception e)
91+
{
92+
var message = $"Fail to delete {fileOrFolder} at {record.FullPath}";
93+
LogException(message, e);
94+
_context.API.ShowMsg(message);
95+
return false;
96+
}
97+
98+
return true;
99+
},
100+
IcoPath = icoPath
101+
});
102+
103+
}
104+
105+
return contextMenus;
106+
}
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+
}
165+
}
166+
167+
public class SearchResult
168+
{
169+
public string FullPath { get; set; }
170+
public ResultType Type { get; set; }
171+
}
172+
173+
public enum ResultType
174+
{
175+
Volume,
176+
Folder,
177+
File
178+
}
179+
}
290 Bytes
Loading

Plugins/Wox.Plugin.Folder/Main.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010

1111
namespace Wox.Plugin.Folder
1212
{
13-
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable
13+
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;
24+
private IContextMenu _contextMenuLoader;
2025

2126
public Main()
2227
{
@@ -37,6 +42,7 @@ public Control CreateSettingPanel()
3742
public void Init(PluginInitContext context)
3843
{
3944
_context = context;
45+
_contextMenuLoader = new ContextMenuLoader(context);
4046
InitialDriverList();
4147
}
4248

@@ -45,7 +51,7 @@ public List<Result> Query(Query query)
4551
var results = GetUserFolderResults(query);
4652

4753
string search = query.Search.ToLower();
48-
if (_driverNames != null && !_driverNames.Any(search.StartsWith))
54+
if (!IsDriveOrSharedFolder(search))
4955
return results;
5056

5157
results.AddRange(QueryInternal_Directory_Exists(query));
@@ -59,6 +65,26 @@ public List<Result> Query(Query query)
5965
return results;
6066
}
6167

68+
private static bool IsDriveOrSharedFolder(string search)
69+
{
70+
if (search.StartsWith(@"\\"))
71+
{ // share folder
72+
return true;
73+
}
74+
75+
if (_driverNames != null && _driverNames.Any(search.StartsWith))
76+
{ // normal drive letter
77+
return true;
78+
}
79+
80+
if (_driverNames == null && search.Length > 2 && char.IsLetter(search[0]) && search[1] == ':')
81+
{ // when we don't have the drive letters we can try...
82+
return true; // we don't know so let's give it the possibility
83+
}
84+
85+
return false;
86+
}
87+
6288
private Result CreateFolderResult(string title, string path, Query query)
6389
{
6490
return new Result
@@ -88,7 +114,8 @@ private Result CreateFolderResult(string title, string path, Query query)
88114
changeTo :
89115
query.ActionKeyword + " " + changeTo);
90116
return false;
91-
}
117+
},
118+
ContextData = new SearchResult { Type = ResultType.Folder, FullPath = path }
92119
};
93120
}
94121

@@ -217,7 +244,8 @@ private static Result CreateFileResult(string filePath, Query query)
217244
}
218245

219246
return true;
220-
}
247+
},
248+
ContextData = new SearchResult { Type = ResultType.File, FullPath = filePath}
221249
};
222250
return result;
223251
}
@@ -254,5 +282,10 @@ public string GetTranslatedPluginDescription()
254282
{
255283
return _context.API.GetTranslation("wox_plugin_folder_plugin_description");
256284
}
285+
286+
public List<Result> LoadContextMenus(Result selectedResult)
287+
{
288+
return _contextMenuLoader.LoadContextMenus(selectedResult);
289+
}
257290
}
258291
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Compile Include="..\..\SolutionAssemblyInfo.cs">
5959
<Link>Properties\SolutionAssemblyInfo.cs</Link>
6060
</Compile>
61+
<Compile Include="ContextMenuLoader.cs" />
6162
<Compile Include="FolderLink.cs" />
6263
<Compile Include="Main.cs" />
6364
<Compile Include="FolderPluginSettings.xaml.cs">
@@ -80,6 +81,9 @@
8081
<None Include="Images\copy.png">
8182
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8283
</None>
84+
<Content Include="Images\file.png">
85+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
86+
</Content>
8387
<Content Include="Languages\en.xaml">
8488
<Generator>MSBuild:Compile</Generator>
8589
<SubType>Designer</SubType>

Wox.Infrastructure/Logger/Log.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static void Exception(string message, System.Exception e)
8787

8888
do
8989
{
90-
logger.Error($"Exception fulle name:\n <{e.GetType().FullName}>");
90+
logger.Error($"Exception full name:\n <{e.GetType().FullName}>");
9191
logger.Error($"Exception message:\n <{e.Message}>");
9292
logger.Error($"Exception stack trace:\n <{e.StackTrace}>");
9393
logger.Error($"Exception source:\n <{e.Source}>");

0 commit comments

Comments
 (0)