From 0b6f737436d11763c8745a4e022ca0a6ec6dbf18 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sat, 22 Apr 2023 13:09:17 +0800 Subject: [PATCH] Add open/close QuickLook function --- Flow.Launcher/Helper/QuickLookHelper.cs | 55 ++++++++++++++++++------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/Flow.Launcher/Helper/QuickLookHelper.cs b/Flow.Launcher/Helper/QuickLookHelper.cs index fe12f481c..1003e731f 100644 --- a/Flow.Launcher/Helper/QuickLookHelper.cs +++ b/Flow.Launcher/Helper/QuickLookHelper.cs @@ -9,12 +9,14 @@ using Flow.Launcher.Infrastructure.Logger; namespace Flow.Launcher.Helper { - internal class QuickLookHelper + internal static class QuickLookHelper { private const int TIMEOUT = 500; private static readonly string pipeName = $"QuickLook.App.Pipe.{WindowsIdentity.GetCurrent().User?.Value}"; private static readonly string pipeMessageSwitch = "QuickLook.App.PipeMessages.Switch"; private static readonly string pipeMessageToggle = "QuickLook.App.PipeMessages.Toggle"; + private static readonly string pipeMessageClose = "QuickLook.App.PipeMessages.Close"; + private static readonly string pipeMessageInvoke = "QuickLook.App.PipeMessages.Invoke"; /// /// Toggle QuickLook @@ -22,35 +24,60 @@ namespace Flow.Launcher.Helper /// File path to preview /// Is swtiching file /// - public static async Task ToggleQuickLookPreviewAsync(string path, bool switchPreview = false) + public static async Task ToggleQuickLookAsync(string path, bool switchPreview = false) { - bool isQuickLookAvailable = await DetectQuickLookAvailabilityAsync(); + //bool isQuickLookAvailable = await DetectQuickLookAvailabilityAsync(); - if (!isQuickLookAvailable) - { - if (!switchPreview) - { - Log.Warn($"{nameof(QuickLookHelper)}", "QuickLook not detected"); - } - return; - } + //if (!isQuickLookAvailable) + //{ + // if (!switchPreview) + // { + // Log.Warn($"{nameof(QuickLookHelper)}", "QuickLook not detected"); + // } + // return; + //} - string pipeName = $"QuickLook.App.Pipe.{WindowsIdentity.GetCurrent().User?.Value}"; - string message = switchPreview ? pipeMessageSwitch : pipeMessageToggle; + if (string.IsNullOrEmpty(path)) + return false; + return await SendQuickLookPipeMsgAsync(switchPreview ? pipeMessageSwitch : pipeMessageToggle, path); + } + + public static async Task CloseQuickLookAsync() + { + return await SendQuickLookPipeMsgAsync(pipeMessageClose); + } + + public static async Task OpenQuickLookAsync(string path) + { + if (string.IsNullOrEmpty(path)) + return false; + return await SendQuickLookPipeMsgAsync(pipeMessageInvoke, path); + } + + private static async Task SendQuickLookPipeMsgAsync(string message, string arg = "") + { await using var client = new NamedPipeClientStream(".", pipeName, PipeDirection.Out); try { await client.ConnectAsync(TIMEOUT); await using var writer = new StreamWriter(client); - await writer.WriteLineAsync($"{message}|{path}"); + await writer.WriteLineAsync($"{message}|{arg}"); await writer.FlushAsync(); } catch (TimeoutException) { client.Close(); + Log.Error($"{nameof(QuickLookHelper)}", "QuickLook timeout"); + return false; } + catch (Exception e) + { + Log.Exception($"{nameof(QuickLookHelper)}", "QuickLook error", e); + return false; + } + return true; } private static async Task DetectQuickLookAvailabilityAsync()