Skip to content

Commit c039b17

Browse files
committed
Refactor file explorer path getting
1 parent aa5ca76 commit c039b17

File tree

2 files changed

+72
-64
lines changed

2 files changed

+72
-64
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Text;
3+
using System.Runtime.InteropServices;
4+
using System.IO;
5+
6+
namespace Flow.Launcher.Helper
7+
{
8+
public class FileExplorerHelper
9+
{
10+
11+
/// <summary>
12+
/// Gets the path of the file explorer that is currently in the foreground
13+
/// </summary>
14+
public static string GetActiveExplorerPath()
15+
{
16+
var explorerWindow = GetActiveExplorer();
17+
string locationUrl = explorerWindow.LocationURL;
18+
if (!string.IsNullOrEmpty(locationUrl))
19+
{
20+
return new Uri(locationUrl).LocalPath;
21+
}
22+
else
23+
{
24+
return null;
25+
}
26+
}
27+
28+
/// <summary>
29+
/// Gets the file explorer that is currently in the foreground
30+
/// </summary>
31+
private static SHDocVw.InternetExplorer GetActiveExplorer()
32+
{
33+
// get the active window
34+
IntPtr handle = GetForegroundWindow();
35+
36+
// Required ref: SHDocVw (Microsoft Internet Controls COM Object) - C:\Windows\system32\ShDocVw.dll
37+
var shellWindows = new SHDocVw.ShellWindows();
38+
39+
// loop through all windows
40+
foreach (var window in shellWindows)
41+
{
42+
if (window is SHDocVw.InternetExplorer explorerWindow && new IntPtr(explorerWindow.HWND) == handle)
43+
{
44+
// we have found the desired window, now let's make sure that it is indeed a file explorer
45+
// we don't want the Internet Explorer or the classic control panel
46+
if (explorerWindow.Document is not Shell32.IShellFolderViewDual2)
47+
{
48+
return null;
49+
}
50+
if (Path.GetFileName(explorerWindow.FullName) != "explorer.exe")
51+
{
52+
return null;
53+
}
54+
55+
return explorerWindow;
56+
}
57+
}
58+
59+
return null;
60+
}
61+
62+
// COM Imports
63+
64+
[DllImport("user32.dll")]
65+
private static extern IntPtr GetForegroundWindow();
66+
67+
[DllImport("user32.dll")]
68+
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
69+
}
70+
}

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -722,68 +722,6 @@ private void SetOpenResultModifiers()
722722
OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
723723
}
724724

725-
private static string GetActiveExplorerPath()
726-
{
727-
// get the active window
728-
IntPtr handle = GetForegroundWindow();
729-
730-
// Required ref: SHDocVw (Microsoft Internet Controls COM Object) - C:\Windows\system32\ShDocVw.dll
731-
ShellWindows shellWindows = new SHDocVw.ShellWindows();
732-
733-
// loop through all windows
734-
foreach (var window in shellWindows)
735-
{
736-
if (window is not SHDocVw.InternetExplorer)
737-
{
738-
continue;
739-
}
740-
741-
var explorerWindow = (SHDocVw.InternetExplorer)window;
742-
// match active window
743-
if (explorerWindow.HWND == (int)handle)
744-
{
745-
// Required ref: Shell32 - C:\Windows\system32\Shell32.dll
746-
var shellWindow = explorerWindow.Document as Shell32.IShellFolderViewDual2;
747-
748-
// will be null if you are in Internet Explorer for example
749-
if (shellWindow != null)
750-
{
751-
// Item without an index returns the current object
752-
var currentFolder = shellWindow.Folder.Items().Item();
753-
754-
// special folder - use window title
755-
// for some reason on "Desktop" gives null
756-
if (currentFolder == null || currentFolder.Path.StartsWith("::"))
757-
{
758-
// Get window title instead
759-
const int nChars = 256;
760-
StringBuilder Buff = new StringBuilder(nChars);
761-
if (GetWindowText(handle, Buff, nChars) > 0)
762-
{
763-
return Buff.ToString();
764-
}
765-
}
766-
else
767-
{
768-
return currentFolder.Path;
769-
}
770-
}
771-
772-
break;
773-
}
774-
}
775-
776-
return null;
777-
}
778-
779-
// COM Imports
780-
781-
[DllImport("user32.dll")]
782-
private static extern IntPtr GetForegroundWindow();
783-
784-
[DllImport("user32.dll")]
785-
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
786-
787725
public void ToggleFlowLauncher()
788726
{
789727
if (!MainWindowVisibilityStatus)
@@ -798,7 +736,7 @@ public void ToggleFlowLauncher()
798736

799737
public void Show()
800738
{
801-
string _explorerPath = GetActiveExplorerPath();
739+
string _explorerPath = FileExplorerHelper.GetActiveExplorerPath();
802740

803741

804742
if (_settings.UseSound)
@@ -816,7 +754,7 @@ public void Show()
816754
((MainWindow)Application.Current.MainWindow).WindowAnimator();
817755

818756
MainWindowOpacity = 1;
819-
if (_explorerPath != null && _explorerPath != "File Explorer")
757+
if (_explorerPath != null)
820758
{
821759
ChangeQueryText($"{_explorerPath}\\>");
822760
}

0 commit comments

Comments
 (0)