Skip to content

Commit 2ed33a2

Browse files
Refactor QuickLook toggle and update logic
- Forcibly open/close/switch content when needed - Remove unused functions
1 parent cda587f commit 2ed33a2

File tree

2 files changed

+68
-66
lines changed

2 files changed

+68
-66
lines changed

Flow.Launcher/Helper/QuickLookHelper.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ internal static class QuickLookHelper
2323
/// Toggle QuickLook
2424
/// </summary>
2525
/// <param name="path">File path to preview</param>
26-
/// <param name="switchPreview">Is swtiching file</param>
2726
/// <returns></returns>
28-
public static async Task<bool> ToggleQuickLookAsync(string path, bool switchPreview = false)
27+
public static async Task<bool> ToggleQuickLookAsync(string path)
2928
{
3029
if (string.IsNullOrEmpty(path))
3130
return false;
32-
33-
bool success = await SendQuickLookPipeMsgAsync(switchPreview ? pipeMessageSwitch : pipeMessageToggle, path);
31+
32+
bool success = await SendQuickLookPipeMsgAsync(pipeMessageToggle, path);
3433
if (!success)
3534
{
3635
ShowQuickLookUnavailableToast();
@@ -61,6 +60,24 @@ public static async Task<bool> OpenQuickLookAsync(string path)
6160
return success;
6261
}
6362

63+
/// <summary>
64+
/// Switch QuickLook to preview another file if it's on
65+
/// </summary>
66+
/// <param name="path">File path to preview</param>
67+
/// <returns></returns>
68+
public static async Task<bool> SwitchQuickLookAsync(string path)
69+
{
70+
if (string.IsNullOrEmpty(path))
71+
return false;
72+
73+
bool success = await SendQuickLookPipeMsgAsync(pipeMessageSwitch, path);
74+
if (!success)
75+
{
76+
ShowQuickLookUnavailableToast();
77+
}
78+
return success;
79+
}
80+
6481
private static async Task<bool> SendQuickLookPipeMsgAsync(string message, string arg = "")
6582
{
6683
await using var client = new NamedPipeClientStream(".", pipeName, PipeDirection.Out);

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,6 @@ public string PreviewHotkey
564564

565565
public bool PreviewVisible { get; set; } = false;
566566

567-
public bool ExternalPreviewOpen { get; set; } = false;
568-
569567
public int ResultAreaColumn { get; set; } = 1;
570568

571569
#endregion
@@ -608,14 +606,24 @@ private void ToggleInternalPreview()
608606

609607
private void ToggleExternalPreview(string path)
610608
{
611-
if (!ExternalPreviewOpen)
612-
{
613-
_ = OpenQuickLookPreviewAsync(path).ConfigureAwait(false);
614-
}
615-
else
616-
{
617-
_ = CloseQuickLookPreviewAsync().ConfigureAwait(false);
618-
}
609+
_ = QuickLookHelper.ToggleQuickLookAsync(path).ConfigureAwait(false);
610+
}
611+
612+
private void OpenExternalPreview(string path)
613+
{
614+
_ = QuickLookHelper.OpenQuickLookAsync(path).ConfigureAwait(false);
615+
}
616+
617+
private void CloseExternalPreview()
618+
{
619+
_ = QuickLookHelper.CloseQuickLookAsync().ConfigureAwait(false);
620+
}
621+
622+
private void SwitchExternalPreview(string path)
623+
{
624+
// Switches preview content
625+
// When external is off, do nothing
626+
_ = QuickLookHelper.SwitchQuickLookAsync(path).ConfigureAwait(false);
619627
}
620628

621629
private void ShowInternalPreview()
@@ -645,61 +653,41 @@ public void ResetPreview()
645653

646654
private void UpdatePreview()
647655
{
648-
if (Settings.UseQuickLook && CanExternalPreviewSelectedResult(out var path))
649-
{
650-
// TODO: When always preview (internal is open) and select another result can use external preview
651-
// then switched to external, looks bad
652-
// Should use external preview for selected result
653-
if (ExternalPreviewOpen)
654-
{
655-
_ = ToggleQuickLookPreviewAsync(path, true).ConfigureAwait(false);
656-
}
657-
else if(PreviewVisible)
658-
{
659-
// When internal is open and select a result that should use external preview
660-
_ = OpenQuickLookPreviewAsync(path).ConfigureAwait(false);
661-
HideInternalPreview();
662-
}
663-
}
664-
else
656+
if (Settings.UseQuickLook)
665657
{
666-
if (PreviewVisible)
658+
if (CanExternalPreviewSelectedResult(out var path))
667659
{
668-
Results.SelectedItem?.LoadPreviewImage();
660+
// Should use external preview for selected result
661+
if (PreviewVisible)
662+
{
663+
// Previewing
664+
// When internal is open and select a result that should use external preview
665+
// External must be off when PreviewVisible
666+
HideInternalPreview();
667+
OpenExternalPreview(path);
668+
}
669+
else
670+
{
671+
// Internal is off, try to switch preview content
672+
SwitchExternalPreview(path);
673+
}
669674
}
670-
else if (ExternalPreviewOpen)
675+
else
671676
{
672-
// When external is open and select a result that can't use external preview
673-
_ = CloseQuickLookPreviewAsync().ConfigureAwait(false);
674-
ShowInternalPreview();
677+
// Should use internal preview for selected result
678+
if (PreviewVisible)
679+
{
680+
Results.SelectedItem?.LoadPreviewImage();
681+
}
682+
else
683+
{
684+
CloseExternalPreview(); // Forcibly close, ideally should only close when it's on
685+
}
675686
}
676687
}
677-
}
678-
679-
private async Task ToggleQuickLookPreviewAsync(string path, bool switchFile = false)
680-
{
681-
bool success = await QuickLookHelper.ToggleQuickLookAsync(path, switchFile);
682-
if (success)
683-
{
684-
ExternalPreviewOpen = switchFile || !ExternalPreviewOpen;
685-
}
686-
}
687-
688-
private async Task OpenQuickLookPreviewAsync(string path)
689-
{
690-
bool success = await QuickLookHelper.OpenQuickLookAsync(path);
691-
if (success)
692-
{
693-
ExternalPreviewOpen = true;
694-
}
695-
}
696-
697-
private async Task CloseQuickLookPreviewAsync()
698-
{
699-
bool success = await QuickLookHelper.CloseQuickLookAsync();
700-
if (success)
688+
else if(PreviewVisible)
701689
{
702-
ExternalPreviewOpen = false;
690+
Results.SelectedItem?.LoadPreviewImage();
703691
}
704692
}
705693

@@ -1112,10 +1100,7 @@ public async void Hide()
11121100
// Trick for no delay
11131101
MainWindowOpacity = 0;
11141102

1115-
if (ExternalPreviewOpen)
1116-
{
1117-
_ = CloseQuickLookPreviewAsync().ConfigureAwait(false);
1118-
}
1103+
CloseExternalPreview();
11191104

11201105
if (!SelectedIsFromQueryResults())
11211106
{

0 commit comments

Comments
 (0)