Skip to content

Commit 3f932a8

Browse files
committed
Check path first & Improve code quality
1 parent a69d6ea commit 3f932a8

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

Flow.Launcher.Infrastructure/QuickSwitch/QuickSwitch.cs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -564,29 +564,26 @@ uint dwmsEventTime
564564

565565
// Edited from: https://github.com/idkidknow/Flow.Launcher.Plugin.DirQuickJump
566566

567-
public static async Task JumpToPathAsync(nint hwnd, string path)
567+
public static async Task<bool> JumpToPathAsync(nint hwnd, string path)
568568
{
569-
if (hwnd == nint.Zero) return;
569+
// Check handle
570+
if (hwnd == nint.Zero) return false;
570571

571-
var dialogWindow = GetDialogWindow(new(hwnd));
572-
if (dialogWindow == null) return;
572+
// Check path
573+
if (!CheckPath(path, out var isFile)) return false;
573574

574-
var dialogWindowTab = dialogWindow.GetCurrentTab();
575-
if (dialogWindowTab == null) return;
575+
// Get dialog tab
576+
var dialogWindowTab = GetDialogWindowTab(new(hwnd));
577+
if (dialogWindowTab == null) return false;
576578

577-
await JumpToPathAsync(dialogWindowTab, path, false);
579+
return await JumpToPathAsync(dialogWindowTab, path, isFile, false);
578580
}
579581

580582
private static async Task<bool> NavigateDialogPathAsync(HWND hwnd, bool auto = false)
581583
{
584+
// Check handle
582585
if (hwnd == HWND.Null) return false;
583586

584-
var dialogWindow = GetDialogWindow(hwnd);
585-
if (dialogWindow == null) return false;
586-
587-
var dialogWindowTab = dialogWindow.GetCurrentTab();
588-
if (dialogWindowTab == null) return false;
589-
590587
// Get explorer path
591588
string path;
592589
lock (_lastExplorerLock)
@@ -595,8 +592,38 @@ private static async Task<bool> NavigateDialogPathAsync(HWND hwnd, bool auto = f
595592
}
596593
if (string.IsNullOrEmpty(path)) return false;
597594

595+
// Check path
596+
if (!CheckPath(path, out var isFile)) return false;
597+
598+
// Get dialog tab
599+
var dialogWindowTab = GetDialogWindowTab(hwnd);
600+
if (dialogWindowTab == null) return false;
601+
598602
// Jump to path
599-
return await JumpToPathAsync(dialogWindowTab, path, auto);
603+
return await JumpToPathAsync(dialogWindowTab, path, isFile, auto);
604+
}
605+
606+
private static bool CheckPath(string path, out bool file)
607+
{
608+
file = false;
609+
// Is non-null?
610+
if (string.IsNullOrEmpty(path)) return false;
611+
// Is absolute?
612+
if (!Path.IsPathRooted(path)) return false;
613+
// Is folder?
614+
var isFolder = Directory.Exists(path);
615+
// Is file?
616+
var isFile = File.Exists(path);
617+
file = isFile;
618+
return isFolder || isFile;
619+
}
620+
621+
private static IQuickSwitchDialogWindowTab GetDialogWindowTab(HWND hwnd)
622+
{
623+
var dialogWindow = GetDialogWindow(hwnd);
624+
if (dialogWindow == null) return null;
625+
var dialogWindowTab = dialogWindow.GetCurrentTab();
626+
return dialogWindowTab;
600627
}
601628

602629
private static IQuickSwitchDialogWindow GetDialogWindow(HWND hwnd)
@@ -631,10 +658,8 @@ private static IQuickSwitchDialogWindow GetDialogWindow(HWND hwnd)
631658
return null;
632659
}
633660

634-
private static async Task<bool> JumpToPathAsync(IQuickSwitchDialogWindowTab dialog, string path, bool auto = false)
661+
private static async Task<bool> JumpToPathAsync(IQuickSwitchDialogWindowTab dialog, string path, bool isFile, bool auto = false)
635662
{
636-
if (!CheckPath(path, out var isFile)) return false;
637-
638663
// Jump after flow launcher window vanished (after JumpAction returned true)
639664
// and the dialog had been in the foreground.
640665
var dialogHandle = dialog.Handle;
@@ -691,21 +716,6 @@ private static async Task<bool> JumpToPathAsync(IQuickSwitchDialogWindowTab dial
691716
{
692717
_navigationLock.Release();
693718
}
694-
695-
static bool CheckPath(string path, out bool file)
696-
{
697-
file = false;
698-
// Is non-null?
699-
if (string.IsNullOrEmpty(path)) return false;
700-
// Is absolute?
701-
if (!Path.IsPathRooted(path)) return false;
702-
// Is folder?
703-
var isFolder = Directory.Exists(path);
704-
// Is file?
705-
var isFile = File.Exists(path);
706-
file = isFile;
707-
return isFolder || isFile;
708-
}
709719
}
710720

711721
private static bool FileJump(string filePath, IQuickSwitchDialogWindowTab dialog, bool openFile = false)

0 commit comments

Comments
 (0)