From 1d1cc2dc7e6f636f1686af73c8b380bbae0475ce Mon Sep 17 00:00:00 2001 From: Ani <115020168+drawbyperpetual@users.noreply.github.com> Date: Thu, 30 Jan 2025 23:42:50 +0100 Subject: [PATCH] Minor cleanups --- .../Helpers/DataPackageHelpers.cs | 37 ++++++++----------- .../AdvancedPaste/Models/PasteFormats.cs | 2 +- .../Services/PasteFormatExecutor.cs | 6 +-- .../ViewModels/OptionsViewModel.cs | 6 +-- 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/DataPackageHelpers.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/DataPackageHelpers.cs index 97af3bbab12a..529773f9a6d5 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/DataPackageHelpers.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/DataPackageHelpers.cs @@ -22,12 +22,6 @@ namespace AdvancedPaste.Helpers; internal static class DataPackageHelpers { - private static readonly Lazy> ImageFileTypes = new(GetImageFileTypes()); - - private static readonly Lazy> AudioFileTypes = new(GetMediaFileTypes("audio")); - - private static readonly Lazy> VideoFileTypes = new(GetMediaFileTypes("video")); - private static readonly (string DataFormat, ClipboardFormat ClipboardFormat)[] DataFormats = [ (StandardDataFormats.Text, ClipboardFormat.Text), @@ -35,6 +29,14 @@ private static readonly (string DataFormat, ClipboardFormat ClipboardFormat)[] D (StandardDataFormats.Bitmap, ClipboardFormat.Image), ]; + private static readonly Lazy<(ClipboardFormat Format, HashSet FileTypes)[]> SupportedFileTypes = + new(() => + [ + (ClipboardFormat.Image, GetImageFileTypes()), + (ClipboardFormat.Audio, GetMediaFileTypes("audio")), + (ClipboardFormat.Video, GetMediaFileTypes("video")), + ]); + internal static DataPackage CreateFromText(string text) { DataPackage dataPackage = new(); @@ -65,19 +67,12 @@ internal static async Task GetAvailableFormatsAsync(this DataPa { availableFormats |= ClipboardFormat.File; - if (ImageFileTypes.Value.Contains(file.FileType)) - { - availableFormats |= ClipboardFormat.Image; - } - - if (AudioFileTypes.Value.Contains(file.FileType)) - { - availableFormats |= ClipboardFormat.Audio; - } - - if (VideoFileTypes.Value.Contains(file.FileType)) + foreach (var (format, fileTypes) in SupportedFileTypes.Value) { - availableFormats |= ClipboardFormat.Video; + if (fileTypes.Contains(file.FileType)) + { + availableFormats |= format; + } } } } @@ -228,7 +223,7 @@ private static HashSet GetImageFileTypes() => private static HashSet GetMediaFileTypes(string mediaKind) { - static string AssocQueryStringValue(NativeMethods.AssocStr assocStr, string extension) + static string AssocQueryString(NativeMethods.AssocStr assocStr, string extension) { uint pcchOut = 0; @@ -242,8 +237,8 @@ static string AssocQueryStringValue(NativeMethods.AssocStr assocStr, string exte var comparison = StringComparison.OrdinalIgnoreCase; var extensions = from extension in Registry.ClassesRoot.GetSubKeyNames() where extension.StartsWith('.') - where AssocQueryStringValue(NativeMethods.AssocStr.PerceivedType, extension).Equals(mediaKind, comparison) || - AssocQueryStringValue(NativeMethods.AssocStr.ContentType, extension).StartsWith($"{mediaKind}/", comparison) + where AssocQueryString(NativeMethods.AssocStr.PerceivedType, extension).Equals(mediaKind, comparison) || + AssocQueryString(NativeMethods.AssocStr.ContentType, extension).StartsWith($"{mediaKind}/", comparison) select extension; return extensions.ToHashSet(StringComparer.InvariantCultureIgnoreCase); diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Models/PasteFormats.cs b/src/modules/AdvancedPaste/AdvancedPaste/Models/PasteFormats.cs index b7903ca00a29..99243ebb5efa 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Models/PasteFormats.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Models/PasteFormats.cs @@ -101,7 +101,7 @@ public enum PasteFormats CanPreview = false, SupportedClipboardFormats = ClipboardFormat.Video, IPCKey = AdvancedPasteTranscodeAction.PropertyNames.TranscodeToMp4, - KernelFunctionDescription = "Takes a video file in the clipboard and transcodes it to MP4.")] + KernelFunctionDescription = "Takes a video file in the clipboard and transcodes it to MP4 (H.264/AAC).")] TranscodeToMp4, [PasteFormatMetadata( diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Services/PasteFormatExecutor.cs b/src/modules/AdvancedPaste/AdvancedPaste/Services/PasteFormatExecutor.cs index f247bbe40558..5d6740977bb5 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/PasteFormatExecutor.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/PasteFormatExecutor.cs @@ -35,9 +35,9 @@ public async Task ExecutePasteFormatAsync(PasteFormat pasteFormat, return await Task.Run(async () => pasteFormat.Format switch { - PasteFormats.KernelQuery => await _kernelService.TransformClipboardAsync(pasteFormat.Prompt, clipboardData, pasteFormat.IsSavedQuery, cancellationToken, progress), - PasteFormats.CustomTextTransformation => DataPackageHelpers.CreateFromText(await _customTextTransformService.TransformTextAsync(pasteFormat.Prompt, await clipboardData.GetTextAsync(), cancellationToken, progress)), - _ => await TransformHelpers.TransformAsync(format, clipboardData, cancellationToken, progress), + PasteFormats.KernelQuery => await _kernelService.TransformClipboardAsync(pasteFormat.Prompt, clipboardData, pasteFormat.IsSavedQuery, cancellationToken, progress), + PasteFormats.CustomTextTransformation => DataPackageHelpers.CreateFromText(await _customTextTransformService.TransformTextAsync(pasteFormat.Prompt, await clipboardData.GetTextAsync(), cancellationToken, progress)), + _ => await TransformHelpers.TransformAsync(format, clipboardData, cancellationToken, progress), }); } diff --git a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs index 0db41186d4a4..688c3047e236 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs @@ -103,7 +103,7 @@ private bool Visible } catch (COMException) { - return false; // Window is closed + return false; // window is closed } } } @@ -354,8 +354,8 @@ private async Task CopyPasteAndHideAsync(DataPackage package) await ClipboardHelper.TryCopyPasteAsync(package, HideWindow); Query = string.Empty; - // Delete any temp files created. A the delay is needed to ensure the file is not in use by the target application - - // for example, when pasting into Explorer, the paste operation will trigger a file copy. + // Delete any temp files created. A delay is needed to ensure the file is not in use by the target application - + // for example, when pasting onto File Explorer, the paste operation will trigger a file copy. _ = Task.Run(() => package.GetView().TryCleanupAfterDelayAsync(TimeSpan.FromSeconds(30))); }