Refactor QuickLook toggle and update logic

- Forcibly open/close/switch content when needed
- Remove unused functions
This commit is contained in:
Vic 2023-05-14 01:40:45 +08:00
parent cda587fb58
commit 2ed33a232d
2 changed files with 68 additions and 66 deletions

View file

@ -23,14 +23,13 @@ namespace Flow.Launcher.Helper
/// Toggle QuickLook
/// </summary>
/// <param name="path">File path to preview</param>
/// <param name="switchPreview">Is swtiching file</param>
/// <returns></returns>
public static async Task<bool> ToggleQuickLookAsync(string path, bool switchPreview = false)
public static async Task<bool> ToggleQuickLookAsync(string path)
{
if (string.IsNullOrEmpty(path))
return false;
bool success = await SendQuickLookPipeMsgAsync(switchPreview ? pipeMessageSwitch : pipeMessageToggle, path);
bool success = await SendQuickLookPipeMsgAsync(pipeMessageToggle, path);
if (!success)
{
ShowQuickLookUnavailableToast();
@ -61,6 +60,24 @@ namespace Flow.Launcher.Helper
return success;
}
/// <summary>
/// Switch QuickLook to preview another file if it's on
/// </summary>
/// <param name="path">File path to preview</param>
/// <returns></returns>
public static async Task<bool> SwitchQuickLookAsync(string path)
{
if (string.IsNullOrEmpty(path))
return false;
bool success = await SendQuickLookPipeMsgAsync(pipeMessageSwitch, path);
if (!success)
{
ShowQuickLookUnavailableToast();
}
return success;
}
private static async Task<bool> SendQuickLookPipeMsgAsync(string message, string arg = "")
{
await using var client = new NamedPipeClientStream(".", pipeName, PipeDirection.Out);

View file

@ -564,8 +564,6 @@ namespace Flow.Launcher.ViewModel
public bool PreviewVisible { get; set; } = false;
public bool ExternalPreviewOpen { get; set; } = false;
public int ResultAreaColumn { get; set; } = 1;
#endregion
@ -608,14 +606,24 @@ namespace Flow.Launcher.ViewModel
private void ToggleExternalPreview(string path)
{
if (!ExternalPreviewOpen)
{
_ = OpenQuickLookPreviewAsync(path).ConfigureAwait(false);
}
else
{
_ = CloseQuickLookPreviewAsync().ConfigureAwait(false);
}
_ = QuickLookHelper.ToggleQuickLookAsync(path).ConfigureAwait(false);
}
private void OpenExternalPreview(string path)
{
_ = QuickLookHelper.OpenQuickLookAsync(path).ConfigureAwait(false);
}
private void CloseExternalPreview()
{
_ = QuickLookHelper.CloseQuickLookAsync().ConfigureAwait(false);
}
private void SwitchExternalPreview(string path)
{
// Switches preview content
// When external is off, do nothing
_ = QuickLookHelper.SwitchQuickLookAsync(path).ConfigureAwait(false);
}
private void ShowInternalPreview()
@ -645,61 +653,41 @@ namespace Flow.Launcher.ViewModel
private void UpdatePreview()
{
if (Settings.UseQuickLook && CanExternalPreviewSelectedResult(out var path))
if (Settings.UseQuickLook)
{
// TODO: When always preview (internal is open) and select another result can use external preview
// then switched to external, looks bad
// Should use external preview for selected result
if (ExternalPreviewOpen)
if (CanExternalPreviewSelectedResult(out var path))
{
_ = ToggleQuickLookPreviewAsync(path, true).ConfigureAwait(false);
// Should use external preview for selected result
if (PreviewVisible)
{
// Previewing
// When internal is open and select a result that should use external preview
// External must be off when PreviewVisible
HideInternalPreview();
OpenExternalPreview(path);
}
else
{
// Internal is off, try to switch preview content
SwitchExternalPreview(path);
}
}
else if(PreviewVisible)
else
{
// When internal is open and select a result that should use external preview
_ = OpenQuickLookPreviewAsync(path).ConfigureAwait(false);
HideInternalPreview();
// Should use internal preview for selected result
if (PreviewVisible)
{
Results.SelectedItem?.LoadPreviewImage();
}
else
{
CloseExternalPreview(); // Forcibly close, ideally should only close when it's on
}
}
}
else
else if(PreviewVisible)
{
if (PreviewVisible)
{
Results.SelectedItem?.LoadPreviewImage();
}
else if (ExternalPreviewOpen)
{
// When external is open and select a result that can't use external preview
_ = CloseQuickLookPreviewAsync().ConfigureAwait(false);
ShowInternalPreview();
}
}
}
private async Task ToggleQuickLookPreviewAsync(string path, bool switchFile = false)
{
bool success = await QuickLookHelper.ToggleQuickLookAsync(path, switchFile);
if (success)
{
ExternalPreviewOpen = switchFile || !ExternalPreviewOpen;
}
}
private async Task OpenQuickLookPreviewAsync(string path)
{
bool success = await QuickLookHelper.OpenQuickLookAsync(path);
if (success)
{
ExternalPreviewOpen = true;
}
}
private async Task CloseQuickLookPreviewAsync()
{
bool success = await QuickLookHelper.CloseQuickLookAsync();
if (success)
{
ExternalPreviewOpen = false;
Results.SelectedItem?.LoadPreviewImage();
}
}
@ -1112,10 +1100,7 @@ namespace Flow.Launcher.ViewModel
// Trick for no delay
MainWindowOpacity = 0;
if (ExternalPreviewOpen)
{
_ = CloseQuickLookPreviewAsync().ConfigureAwait(false);
}
CloseExternalPreview();
if (!SelectedIsFromQueryResults())
{