From a1600fabcc7aee0a3edc382c0eb8b9263c977653 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:53:43 +0800 Subject: [PATCH] Implement preview switching logic - Internal and external preview can't open at the same time - Always preview option doesn't control external --- Flow.Launcher.Plugin/Result.cs | 5 ++- Flow.Launcher/ViewModel/MainViewModel.cs | 55 ++++++++++++------------ 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/Flow.Launcher.Plugin/Result.cs b/Flow.Launcher.Plugin/Result.cs index f618645c4..a97db4781 100644 --- a/Flow.Launcher.Plugin/Result.cs +++ b/Flow.Launcher.Plugin/Result.cs @@ -230,8 +230,11 @@ namespace Flow.Launcher.Plugin /// #26a0da (blue) public string ProgressBarColor { get; set; } = "#26a0da"; + /// + /// Preview info of the result to show on preview panel. + /// public PreviewInfo Preview { get; set; } = PreviewInfo.Default; - + /// /// Info of the preview image. /// diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 9f055d640..3f749f024 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -575,11 +575,15 @@ namespace Flow.Launcher.ViewModel [RelayCommand] private void TogglePreview() { - if (Settings.UseQuickLook) + if (Settings.UseQuickLook && CanExternalPreviewSelectedResult(out var path)) { - if (!ExternalPreviewOpen) + if (Settings.AlwaysPreview == true && PreviewVisible) { - _ = OpenQuickLookPreviewAsync(); + HidePreview(); // When Always preview, toggle off rather than open external + } + else if (!ExternalPreviewOpen) + { + _ = OpenQuickLookPreviewAsync(path); } else { @@ -620,50 +624,41 @@ namespace Flow.Launcher.ViewModel HidePreview(); } } - + private void UpdatePreview() { - if (Settings.UseQuickLook) + if (Settings.UseQuickLook && CanExternalPreviewSelectedResult(out var path)) { - if (ExternalPreviewOpen) + _ = ToggleQuickLookPreviewAsync(path, ExternalPreviewOpen); + if (PreviewVisible) { - _ = ToggleQuickLookPreviewAsync(true); + HidePreview(); } } else if (PreviewVisible) { Results.SelectedItem?.LoadPreviewImage(); } + else + { + // When external is open and select a result that can't be previewed by external program + _ = CloseQuickLookPreviewAsync(); + ShowPreview(); + } } - private async Task ToggleQuickLookPreviewAsync(bool switchFile = false) + private async Task ToggleQuickLookPreviewAsync(string path, bool switchFile = false) { - if (!SelectedIsFromQueryResults()) - return; - - var result = Results.SelectedItem?.Result; - - if (result is null || string.IsNullOrEmpty(result.Preview.FilePath)) - return; - - bool success = await QuickLookHelper.ToggleQuickLookAsync(result.Preview.FilePath, switchFile); + bool success = await QuickLookHelper.ToggleQuickLookAsync(path, switchFile); if (success) { ExternalPreviewOpen = switchFile || !ExternalPreviewOpen; } } - private async Task OpenQuickLookPreviewAsync() + private async Task OpenQuickLookPreviewAsync(string path) { - if (!SelectedIsFromQueryResults()) - return; - - var result = Results.SelectedItem?.Result; - - if (result is null || string.IsNullOrEmpty(result.Preview.FilePath)) - return; - - bool success = await QuickLookHelper.OpenQuickLookAsync(result.Preview.FilePath); + bool success = await QuickLookHelper.OpenQuickLookAsync(path); if (success) { ExternalPreviewOpen = true; @@ -679,6 +674,12 @@ namespace Flow.Launcher.ViewModel } } + private bool CanExternalPreviewSelectedResult(out string path) + { + path = Results.SelectedItem?.Result?.Preview.FilePath; + return string.IsNullOrEmpty(path); + } + #endregion #region Query