Refactor file explorer path getting

This commit is contained in:
stefnotch 2022-07-16 11:16:00 +02:00
parent aa5ca76b18
commit c039b17d93
2 changed files with 72 additions and 64 deletions

View file

@ -0,0 +1,70 @@
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace Flow.Launcher.Helper
{
public class FileExplorerHelper
{
/// <summary>
/// Gets the path of the file explorer that is currently in the foreground
/// </summary>
public static string GetActiveExplorerPath()
{
var explorerWindow = GetActiveExplorer();
string locationUrl = explorerWindow.LocationURL;
if (!string.IsNullOrEmpty(locationUrl))
{
return new Uri(locationUrl).LocalPath;
}
else
{
return null;
}
}
/// <summary>
/// Gets the file explorer that is currently in the foreground
/// </summary>
private static SHDocVw.InternetExplorer GetActiveExplorer()
{
// get the active window
IntPtr handle = GetForegroundWindow();
// Required ref: SHDocVw (Microsoft Internet Controls COM Object) - C:\Windows\system32\ShDocVw.dll
var shellWindows = new SHDocVw.ShellWindows();
// loop through all windows
foreach (var window in shellWindows)
{
if (window is SHDocVw.InternetExplorer explorerWindow && new IntPtr(explorerWindow.HWND) == handle)
{
// we have found the desired window, now let's make sure that it is indeed a file explorer
// we don't want the Internet Explorer or the classic control panel
if (explorerWindow.Document is not Shell32.IShellFolderViewDual2)
{
return null;
}
if (Path.GetFileName(explorerWindow.FullName) != "explorer.exe")
{
return null;
}
return explorerWindow;
}
}
return null;
}
// COM Imports
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
}
}

View file

@ -722,68 +722,6 @@ namespace Flow.Launcher.ViewModel
OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
}
private static string GetActiveExplorerPath()
{
// get the active window
IntPtr handle = GetForegroundWindow();
// Required ref: SHDocVw (Microsoft Internet Controls COM Object) - C:\Windows\system32\ShDocVw.dll
ShellWindows shellWindows = new SHDocVw.ShellWindows();
// loop through all windows
foreach (var window in shellWindows)
{
if (window is not SHDocVw.InternetExplorer)
{
continue;
}
var explorerWindow = (SHDocVw.InternetExplorer)window;
// match active window
if (explorerWindow.HWND == (int)handle)
{
// Required ref: Shell32 - C:\Windows\system32\Shell32.dll
var shellWindow = explorerWindow.Document as Shell32.IShellFolderViewDual2;
// will be null if you are in Internet Explorer for example
if (shellWindow != null)
{
// Item without an index returns the current object
var currentFolder = shellWindow.Folder.Items().Item();
// special folder - use window title
// for some reason on "Desktop" gives null
if (currentFolder == null || currentFolder.Path.StartsWith("::"))
{
// Get window title instead
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
}
else
{
return currentFolder.Path;
}
}
break;
}
}
return null;
}
// COM Imports
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public void ToggleFlowLauncher()
{
if (!MainWindowVisibilityStatus)
@ -798,7 +736,7 @@ namespace Flow.Launcher.ViewModel
public void Show()
{
string _explorerPath = GetActiveExplorerPath();
string _explorerPath = FileExplorerHelper.GetActiveExplorerPath();
if (_settings.UseSound)
@ -816,7 +754,7 @@ namespace Flow.Launcher.ViewModel
((MainWindow)Application.Current.MainWindow).WindowAnimator();
MainWindowOpacity = 1;
if (_explorerPath != null && _explorerPath != "File Explorer")
if (_explorerPath != null)
{
ChangeQueryText($"{_explorerPath}\\>");
}