Skip to content

Commit 77e966e

Browse files
authored
Merge pull request #2004 from VictoriousRaptor/ExplorerDefaultAction
Explorer default enter keypress action: open in file manager option
2 parents 7130d87 + 9f88a07 commit 77e966e

File tree

7 files changed

+102
-49
lines changed

7 files changed

+102
-49
lines changed

Flow.Launcher.Plugin/ActionContext.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Flow.Launcher.Plugin
1+
using System.Windows.Input;
2+
3+
namespace Flow.Launcher.Plugin
24
{
35
public class ActionContext
46
{
@@ -11,5 +13,13 @@ public class SpecialKeyState
1113
public bool ShiftPressed { get; set; }
1214
public bool AltPressed { get; set; }
1315
public bool WinPressed { get; set; }
16+
17+
public ModifierKeys ToModifierKeys()
18+
{
19+
return (CtrlPressed ? ModifierKeys.Control : ModifierKeys.None) |
20+
(ShiftPressed ? ModifierKeys.Shift : ModifierKeys.None) |
21+
(AltPressed ? ModifierKeys.Alt : ModifierKeys.None) |
22+
(WinPressed ? ModifierKeys.Windows : ModifierKeys.None);
23+
}
1424
}
15-
}
25+
}

Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,36 @@ public static void OpenPath(string fileOrFolderPath)
170170
}
171171
}
172172

173+
/// <summary>
174+
/// Open a file with associated application
175+
/// </summary>
176+
/// <param name="filePath">File path</param>
177+
/// <param name="workingDir">Working directory</param>
178+
/// <param name="asAdmin">Open as Administrator</param>
179+
public static void OpenFile(string filePath, string workingDir = "", bool asAdmin = false)
180+
{
181+
var psi = new ProcessStartInfo
182+
{
183+
FileName = filePath,
184+
UseShellExecute = true,
185+
WorkingDirectory = workingDir,
186+
Verb = asAdmin ? "runas" : string.Empty
187+
};
188+
try
189+
{
190+
if (FileExists(filePath))
191+
Process.Start(psi);
192+
}
193+
catch (Exception)
194+
{
195+
#if DEBUG
196+
throw;
197+
#else
198+
MessageBox.Show(string.Format("Unable to open the path {0}, please check if it exists", filePath));
199+
#endif
200+
}
201+
}
202+
173203
///<summary>
174204
/// This checks whether a given string is a directory path or network location string.
175205
/// It does not check if location actually exists.

Flow.Launcher/Languages/en.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@
348348
<system:String x:Key="HotkeyUpDownDesc">Back / Context Menu</system:String>
349349
<system:String x:Key="HotkeyLeftRightDesc">Item Navigation</system:String>
350350
<system:String x:Key="HotkeyShiftEnterDesc">Open Context Menu</system:String>
351-
<system:String x:Key="HotkeyCtrlEnterDesc">Open Containing Folder</system:String>
352-
<system:String x:Key="HotkeyCtrlShiftEnterDesc">Run as Admin</system:String>
351+
<system:String x:Key="HotkeyCtrlEnterDesc">Open A File/Folder's Containing Folder</system:String>
352+
<system:String x:Key="HotkeyCtrlShiftEnterDesc">Run as Admin / Open Folder in Default File Manager</system:String>
353353
<system:String x:Key="HotkeyCtrlHDesc">Query History</system:String>
354354
<system:String x:Key="HotkeyESCDesc">Back to Result in Context Menu</system:String>
355355
<system:String x:Key="HotkeyTabDesc">Autocomplete</system:String>

Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<system:String x:Key="plugin_explorer_alternative">The warning message has been switched off. As an alternative for searching files and folders, would you like to install Everything plugin?{0}{0}Select 'Yes' to install Everything plugin, or 'No' to return</system:String>
1919
<system:String x:Key="plugin_explorer_alternative_title">Explorer Alternative</system:String>
2020
<system:String x:Key="plugin_explorer_directoryinfosearch_error">Error occurred during search: {0}</system:String>
21+
<system:String x:Key="plugin_explorer_opendir_error">Could not open folder</system:String>
22+
<system:String x:Key="plugin_explorer_openfile_error">Could not open file</system:String>
2123

2224
<!-- Controls -->
2325
<system:String x:Key="plugin_explorer_delete">Delete</system:String>
@@ -34,6 +36,7 @@
3436
<system:String x:Key="plugin_explorer_shell_path">Shell Path</system:String>
3537
<system:String x:Key="plugin_explorer_indexsearchexcludedpaths_header">Index Search Excluded Paths</system:String>
3638
<system:String x:Key="plugin_explorer_use_location_as_working_dir">Use search result's location as the working directory of the executable</system:String>
39+
<system:String x:Key="plugin_explorer_default_open_in_file_manager">Hit Enter to open folder in Default File Manager</system:String>
3740
<system:String x:Key="plugin_explorer_usewindowsindexfordirectorysearch">Use Index Search For Path Search</system:String>
3841
<system:String x:Key="plugin_explorer_manageindexoptions">Indexing Options</system:String>
3942
<system:String x:Key="plugin_explorer_actionkeywordview_search">Search:</system:String>

Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading.Tasks;
99
using System.Windows;
1010
using Flow.Launcher.Plugin.Explorer.Search.Everything;
11+
using System.Windows.Input;
1112

1213
namespace Flow.Launcher.Plugin.Explorer.Search
1314
{
@@ -79,7 +80,7 @@ internal static Result CreateFolderResult(string title, string subtitle, string
7980
Action = c =>
8081
{
8182
// open folder
82-
if (c.SpecialKeyState.CtrlPressed || (!Settings.PathSearchKeywordEnabled && !Settings.SearchActionKeywordEnabled))
83+
if (c.SpecialKeyState.ToModifierKeys() == (ModifierKeys.Control | ModifierKeys.Shift))
8384
{
8485
try
8586
{
@@ -88,13 +89,44 @@ internal static Result CreateFolderResult(string title, string subtitle, string
8889
}
8990
catch (Exception ex)
9091
{
91-
MessageBox.Show(ex.Message, "Could not start " + path);
92+
MessageBox.Show(ex.Message, Context.API.GetTranslation("plugin_explorer_opendir_error"));
93+
return false;
94+
}
95+
}
96+
// Open containing folder
97+
if (c.SpecialKeyState.ToModifierKeys() == ModifierKeys.Control)
98+
{
99+
try
100+
{
101+
Context.API.OpenDirectory(Path.GetDirectoryName(path), path);
102+
return true;
103+
}
104+
catch (Exception ex)
105+
{
106+
MessageBox.Show(ex.Message, Context.API.GetTranslation("plugin_explorer_opendir_error"));
92107
return false;
93108
}
94109
}
95110

96-
// or make this folder the current query
97-
Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword));
111+
// If path search is disabled just open it in file manager
112+
if (Settings.DefaultOpenFolderInFileManager || (!Settings.PathSearchKeywordEnabled && !Settings.SearchActionKeywordEnabled))
113+
{
114+
try
115+
{
116+
OpenFolder(path);
117+
return true;
118+
}
119+
catch (Exception ex)
120+
{
121+
MessageBox.Show(ex.Message, Context.API.GetTranslation("plugin_explorer_opendir_error"));
122+
return false;
123+
}
124+
}
125+
else
126+
{
127+
// or make this folder the current query
128+
Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword));
129+
}
98130

99131
return false;
100132
},
@@ -222,22 +254,22 @@ internal static Result CreateFileResult(string filePath, Query query, int score
222254
{
223255
try
224256
{
225-
if (c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed)
257+
if (c.SpecialKeyState.ToModifierKeys() == (ModifierKeys.Control | ModifierKeys.Shift))
226258
{
227-
OpenFileAsAdmin(filePath);
259+
OpenFile(filePath, Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty, true);
228260
}
229-
else if (c.SpecialKeyState.CtrlPressed)
261+
else if (c.SpecialKeyState.ToModifierKeys() == ModifierKeys.Control)
230262
{
231263
OpenFolder(filePath, filePath);
232264
}
233265
else
234266
{
235-
OpenFile(filePath);
267+
OpenFile(filePath, Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty);
236268
}
237269
}
238270
catch (Exception ex)
239271
{
240-
MessageBox.Show(ex.Message, "Could not start " + filePath);
272+
MessageBox.Show(ex.Message, Context.API.GetTranslation("plugin_explorer_openfile_error"));
241273
}
242274

243275
return true;
@@ -256,38 +288,16 @@ private static bool IsMedia(string extension)
256288
return MediaExtensions.Contains(extension.ToLowerInvariant());
257289
}
258290

259-
private static void OpenFile(string filePath)
291+
private static void OpenFile(string filePath, string workingDir = "", bool asAdmin = false)
260292
{
261293
IncrementEverythingRunCounterIfNeeded(filePath);
262-
FilesFolders.OpenPath(filePath);
294+
FilesFolders.OpenFile(filePath, workingDir, asAdmin);
263295
}
264296

265297
private static void OpenFolder(string folderPath, string fileNameOrFilePath = null)
266298
{
267299
IncrementEverythingRunCounterIfNeeded(folderPath);
268-
Context.API.OpenDirectory(Path.GetDirectoryName(folderPath), fileNameOrFilePath);
269-
}
270-
271-
private static void OpenFileAsAdmin(string filePath)
272-
{
273-
_ = Task.Run(() =>
274-
{
275-
try
276-
{
277-
IncrementEverythingRunCounterIfNeeded(filePath);
278-
Process.Start(new ProcessStartInfo
279-
{
280-
FileName = filePath,
281-
UseShellExecute = true,
282-
WorkingDirectory = Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty,
283-
Verb = "runas",
284-
});
285-
}
286-
catch (Exception e)
287-
{
288-
MessageBox.Show(e.Message, "Could not start " + filePath);
289-
}
290-
});
300+
Context.API.OpenDirectory(folderPath, fileNameOrFilePath);
291301
}
292302

293303
private static void IncrementEverythingRunCounterIfNeeded(string fileOrFolder)

Plugins/Flow.Launcher.Plugin.Explorer/Settings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class Settings
3232

3333
public bool ShowWindowsContextMenu { get; set; } = true;
3434

35+
public bool DefaultOpenFolderInFileManager { get; set; } = false;
3536

3637
public string SearchActionKeyword { get; set; } = Query.GlobalPluginWildcardSign;
3738

Plugins/Flow.Launcher.Plugin.Explorer/Views/ExplorerSettings.xaml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@
168168
HorizontalAlignment="Left"
169169
Content="{DynamicResource plugin_explorer_use_location_as_working_dir}"
170170
IsChecked="{Binding Settings.UseLocationAsWorkingDir}" />
171+
<CheckBox
172+
Margin="0,10,0,0"
173+
HorizontalAlignment="Left"
174+
Content="{DynamicResource plugin_explorer_default_open_in_file_manager}"
175+
IsChecked="{Binding Settings.DefaultOpenInFileManager}" />
171176
<StackPanel Margin="0,10,0,10" Orientation="Horizontal">
172177
<Grid>
173178
<Grid.ColumnDefinitions>
@@ -348,17 +353,11 @@
348353
Header="{DynamicResource plugin_explorer_everything_setting_header}"
349354
Style="{DynamicResource ExplorerTabItem}">
350355
<StackPanel Margin="10" Orientation="Vertical">
351-
<StackPanel Orientation="Horizontal">
352-
<TextBlock
353-
Margin="10"
354-
VerticalAlignment="Center"
355-
Text="{DynamicResource flowlauncher_plugin_everything_search_fullpath}"
356-
TextBlock.Foreground="{DynamicResource Color05B}" />
357-
<CheckBox
358-
Margin="10"
359-
VerticalAlignment="Center"
360-
IsChecked="{Binding Settings.EverythingSearchFullPath}" />
361-
</StackPanel>
356+
<CheckBox
357+
Margin="20,10,0,0"
358+
HorizontalAlignment="Left"
359+
Content="{DynamicResource flowlauncher_plugin_everything_search_fullpath}"
360+
IsChecked="{Binding Settings.EverythingSearchFullPath}" />
362361
<StackPanel Orientation="Horizontal">
363362
<Grid Margin="20,10,0,10">
364363
<Grid.ColumnDefinitions>

0 commit comments

Comments
 (0)