Add open/close QuickLook function

This commit is contained in:
Vic 2023-04-22 13:09:17 +08:00
parent f4609135a2
commit 0b6f737436

View file

@ -9,12 +9,14 @@ using Flow.Launcher.Infrastructure.Logger;
namespace Flow.Launcher.Helper namespace Flow.Launcher.Helper
{ {
internal class QuickLookHelper internal static class QuickLookHelper
{ {
private const int TIMEOUT = 500; private const int TIMEOUT = 500;
private static readonly string pipeName = $"QuickLook.App.Pipe.{WindowsIdentity.GetCurrent().User?.Value}"; 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 pipeMessageSwitch = "QuickLook.App.PipeMessages.Switch";
private static readonly string pipeMessageToggle = "QuickLook.App.PipeMessages.Toggle"; 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";
/// <summary> /// <summary>
/// Toggle QuickLook /// Toggle QuickLook
@ -22,35 +24,60 @@ namespace Flow.Launcher.Helper
/// <param name="path">File path to preview</param> /// <param name="path">File path to preview</param>
/// <param name="switchPreview">Is swtiching file</param> /// <param name="switchPreview">Is swtiching file</param>
/// <returns></returns> /// <returns></returns>
public static async Task ToggleQuickLookPreviewAsync(string path, bool switchPreview = false) public static async Task<bool> ToggleQuickLookAsync(string path, bool switchPreview = false)
{ {
bool isQuickLookAvailable = await DetectQuickLookAvailabilityAsync(); //bool isQuickLookAvailable = await DetectQuickLookAvailabilityAsync();
if (!isQuickLookAvailable) //if (!isQuickLookAvailable)
{ //{
if (!switchPreview) // if (!switchPreview)
{ // {
Log.Warn($"{nameof(QuickLookHelper)}", "QuickLook not detected"); // Log.Warn($"{nameof(QuickLookHelper)}", "QuickLook not detected");
} // }
return; // return;
} //}
string pipeName = $"QuickLook.App.Pipe.{WindowsIdentity.GetCurrent().User?.Value}"; if (string.IsNullOrEmpty(path))
string message = switchPreview ? pipeMessageSwitch : pipeMessageToggle; return false;
return await SendQuickLookPipeMsgAsync(switchPreview ? pipeMessageSwitch : pipeMessageToggle, path);
}
public static async Task<bool> CloseQuickLookAsync()
{
return await SendQuickLookPipeMsgAsync(pipeMessageClose);
}
public static async Task<bool> OpenQuickLookAsync(string path)
{
if (string.IsNullOrEmpty(path))
return false;
return await SendQuickLookPipeMsgAsync(pipeMessageInvoke, path);
}
private static async Task<bool> SendQuickLookPipeMsgAsync(string message, string arg = "")
{
await using var client = new NamedPipeClientStream(".", pipeName, PipeDirection.Out); await using var client = new NamedPipeClientStream(".", pipeName, PipeDirection.Out);
try try
{ {
await client.ConnectAsync(TIMEOUT); await client.ConnectAsync(TIMEOUT);
await using var writer = new StreamWriter(client); await using var writer = new StreamWriter(client);
await writer.WriteLineAsync($"{message}|{path}"); await writer.WriteLineAsync($"{message}|{arg}");
await writer.FlushAsync(); await writer.FlushAsync();
} }
catch (TimeoutException) catch (TimeoutException)
{ {
client.Close(); 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<bool> DetectQuickLookAvailabilityAsync() private static async Task<bool> DetectQuickLookAvailabilityAsync()