-
-
Notifications
You must be signed in to change notification settings - Fork 457
Insert active explorer path #904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,9 @@ | |
| using Microsoft.VisualStudio.Threading; | ||
| using System.Threading.Channels; | ||
| using ISavable = Flow.Launcher.Plugin.ISavable; | ||
|
|
||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using SHDocVw; | ||
|
|
||
| namespace Flow.Launcher.ViewModel | ||
| { | ||
|
|
@@ -720,6 +722,68 @@ private void SetOpenResultModifiers() | |
| 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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've gotten far more consistent results with the following. It also seems to handle special cases like the documents folder or the recycle bin folder. string locationUrl = window.LocationURL;
if (!string.IsNullOrEmpty(locationUrl))
{
string folderPath = new Uri(locationUrl).LocalPath;
return folderPath;
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can fire a pr based on this one, and modify code there. @Garulf will have access to modify your pr so it would be easier to review and edit the code. |
||
|
|
||
| // 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) | ||
|
|
@@ -734,6 +798,9 @@ public void ToggleFlowLauncher() | |
|
|
||
| public void Show() | ||
| { | ||
| string _explorerPath = GetActiveExplorerPath(); | ||
|
|
||
|
|
||
| if (_settings.UseSound) | ||
| { | ||
| MediaPlayer media = new MediaPlayer(); | ||
|
|
@@ -749,6 +816,11 @@ public void Show() | |
| ((MainWindow)Application.Current.MainWindow).WindowAnimator(); | ||
|
|
||
| MainWindowOpacity = 1; | ||
| if (_explorerPath != null && _explorerPath != "File Explorer") | ||
| { | ||
| ChangeQueryText($"{_explorerPath}\\>"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public async void Hide() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left some design questions over at https://github.com/Flow-Launcher/Flow.Launcher/pull/1018/files#r922377206