Skip to content

Commit

Permalink
Minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbyperpetual committed Jan 30, 2025
1 parent 356a5b2 commit 1d1cc2d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ namespace AdvancedPaste.Helpers;

internal static class DataPackageHelpers
{
private static readonly Lazy<HashSet<string>> ImageFileTypes = new(GetImageFileTypes());

private static readonly Lazy<HashSet<string>> AudioFileTypes = new(GetMediaFileTypes("audio"));

private static readonly Lazy<HashSet<string>> VideoFileTypes = new(GetMediaFileTypes("video"));

private static readonly (string DataFormat, ClipboardFormat ClipboardFormat)[] DataFormats =
[
(StandardDataFormats.Text, ClipboardFormat.Text),
(StandardDataFormats.Html, ClipboardFormat.Html),
(StandardDataFormats.Bitmap, ClipboardFormat.Image),
];

private static readonly Lazy<(ClipboardFormat Format, HashSet<string> FileTypes)[]> SupportedFileTypes =
new(() =>
[
(ClipboardFormat.Image, GetImageFileTypes()),
(ClipboardFormat.Audio, GetMediaFileTypes("audio")),
(ClipboardFormat.Video, GetMediaFileTypes("video")),
]);

internal static DataPackage CreateFromText(string text)
{
DataPackage dataPackage = new();
Expand Down Expand Up @@ -65,19 +67,12 @@ internal static async Task<ClipboardFormat> 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;
}
}
}
}
Expand Down Expand Up @@ -228,7 +223,7 @@ private static HashSet<string> GetImageFileTypes() =>

private static HashSet<string> GetMediaFileTypes(string mediaKind)
{
static string AssocQueryStringValue(NativeMethods.AssocStr assocStr, string extension)
static string AssocQueryString(NativeMethods.AssocStr assocStr, string extension)
{
uint pcchOut = 0;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public async Task<DataPackage> 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),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private bool Visible
}
catch (COMException)
{
return false; // Window is closed
return false; // window is closed
}
}
}
Expand Down Expand Up @@ -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)));
}

Expand Down

0 comments on commit 1d1cc2d

Please sign in to comment.