From 2ed33a232d06770593f9f7eabe2ede5922ba2b33 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 14 May 2023 01:40:45 +0800 Subject: [PATCH] Refactor QuickLook toggle and update logic - Forcibly open/close/switch content when needed - Remove unused functions --- Flow.Launcher/Helper/QuickLookHelper.cs | 25 +++++- Flow.Launcher/ViewModel/MainViewModel.cs | 109 ++++++++++------------- 2 files changed, 68 insertions(+), 66 deletions(-) diff --git a/Flow.Launcher/Helper/QuickLookHelper.cs b/Flow.Launcher/Helper/QuickLookHelper.cs index 0d8251f1f..59c94ae2f 100644 --- a/Flow.Launcher/Helper/QuickLookHelper.cs +++ b/Flow.Launcher/Helper/QuickLookHelper.cs @@ -23,14 +23,13 @@ namespace Flow.Launcher.Helper /// Toggle QuickLook /// /// File path to preview - /// Is swtiching file /// - public static async Task ToggleQuickLookAsync(string path, bool switchPreview = false) + public static async Task 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; } + /// + /// Switch QuickLook to preview another file if it's on + /// + /// File path to preview + /// + public static async Task SwitchQuickLookAsync(string path) + { + if (string.IsNullOrEmpty(path)) + return false; + + bool success = await SendQuickLookPipeMsgAsync(pipeMessageSwitch, path); + if (!success) + { + ShowQuickLookUnavailableToast(); + } + return success; + } + private static async Task SendQuickLookPipeMsgAsync(string message, string arg = "") { await using var client = new NamedPipeClientStream(".", pipeName, PipeDirection.Out); diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index b70d15d45..06ac46209 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -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()) {