From c47d9f3c9b5a57b8368846f76b4c025e1fef35ba Mon Sep 17 00:00:00 2001 From: ema Date: Tue, 8 Jul 2025 22:04:34 +0800 Subject: [PATCH] Move resource dictionary initialization --- .../QuickLook.Plugin.HelixViewer/Plugin.cs | 4 +-- .../QuickLook.Plugin.ImageViewer/Plugin.cs | 4 +-- .../QuickLook.Plugin.TextViewer/Plugin.cs | 28 ++++++------------- .../TextViewerPanel.cs | 16 +++++++++++ .../Plugin.cs | 4 +-- 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.HelixViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.HelixViewer/Plugin.cs index 6bb428d..27f45df 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.HelixViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.HelixViewer/Plugin.cs @@ -25,7 +25,7 @@ namespace QuickLook.Plugin.HelixViewer; public class Plugin : IViewer { - private static readonly HashSet WellKnownImageExtensions = new( + private static readonly HashSet WellKnownExtensions = new( [ ".stl", ".obj", ".3ds", ".lwo", ".ply", ]); @@ -41,7 +41,7 @@ public class Plugin : IViewer public bool CanHandle(string path) { return !Directory.Exists(path) - && WellKnownImageExtensions.Contains(Path.GetExtension(path.ToLower())) + && WellKnownExtensions.Contains(Path.GetExtension(path.ToLower())) && Handler.CanHandle(path); } diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs index 1329369..2d29972 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs @@ -30,7 +30,7 @@ namespace QuickLook.Plugin.ImageViewer; public class Plugin : IViewer { - private static readonly HashSet WellKnownImageExtensions = new( + private static readonly HashSet WellKnownExtensions = new( [ ".apng", ".ari", ".arw", ".avif", ".ani", ".bay", ".bmp", @@ -108,7 +108,7 @@ public class Plugin : IViewer // Disabled due mishandling text file types e.g., "*.config". // Only check extension for well known image and animated image types. - return !Directory.Exists(path) && WellKnownImageExtensions.Contains(Path.GetExtension(path).ToLower()); + return !Directory.Exists(path) && WellKnownExtensions.Contains(Path.GetExtension(path).ToLower()); } public void Prepare(string path, ContextObject context) diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs index 2169adb..0668e65 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs @@ -27,25 +27,17 @@ namespace QuickLook.Plugin.TextViewer; public class Plugin : IViewer { + private static readonly HashSet WellKnownExtensions = new( + [ + ".txt", ".rtf", + ]); + private TextViewerPanel _tvp; public int Priority => -5; public void Init() { - // Implementation of the Search Panel Styled with Fluent Theme - { - var groupDictionary = new ResourceDictionary(); - groupDictionary.MergedDictionaries.Add(new ResourceDictionary() - { - Source = new Uri("pack://application:,,,/QuickLook.Plugin.TextViewer;component/Controls/DropDownButton.xaml", UriKind.Absolute) - }); - groupDictionary.MergedDictionaries.Add(new ResourceDictionary() - { - Source = new Uri("pack://application:,,,/QuickLook.Plugin.TextViewer;component/Controls/SearchPanel.xaml", UriKind.Absolute) - }); - Application.Current.Resources.MergedDictionaries.Add(groupDictionary); - } } public bool CanHandle(string path) @@ -53,14 +45,10 @@ public class Plugin : IViewer if (Directory.Exists(path)) return false; - if (new[] { ".txt", ".rtf" }.Any(ext => path.EndsWith(ext, StringComparison.OrdinalIgnoreCase))) + if (WellKnownExtensions.Any(ext => path.EndsWith(ext, StringComparison.OrdinalIgnoreCase))) return true; - // if there is a matched highlighting scheme (by file extension), treat it as a plain text file - // if (HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(path)) != null) - // return true; - - // otherwise, read the first 16KB, check if we can get something. + // Read the first 16KB, check if we can get something. using var s = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); const int bufferLength = 16 * 1024; var buffer = new byte[bufferLength]; @@ -94,7 +82,7 @@ public class Plugin : IViewer _tvp.LoadFileAsync(path, context); context.ViewerContent = _tvp; } - context.Title = $"{Path.GetFileName(path)}"; + context.Title = Path.GetFileName(path); } public void Cleanup() diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.cs index 040314a..8ba0a72 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.cs @@ -29,6 +29,7 @@ using System.Globalization; using System.IO; using System.Reflection; using System.Threading.Tasks; +using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; @@ -42,6 +43,21 @@ public partial class TextViewerPanel : TextEditor, IDisposable static TextViewerPanel() { + // Implementation of the Search Panel Styled with Fluent Theme + { + var groupDictionary = new ResourceDictionary(); + groupDictionary.MergedDictionaries.Add(new ResourceDictionary() + { + Source = new Uri("pack://application:,,,/QuickLook.Plugin.TextViewer;component/Controls/DropDownButton.xaml", UriKind.Absolute) + }); + groupDictionary.MergedDictionaries.Add(new ResourceDictionary() + { + Source = new Uri("pack://application:,,,/QuickLook.Plugin.TextViewer;component/Controls/SearchPanel.xaml", UriKind.Absolute) + }); + Application.Current.Resources.MergedDictionaries.Add(groupDictionary); + } + + // Initialize the Highlighting Theme Manager HighlightingThemeManager.Initialize(); } diff --git a/QuickLook.Plugin/QuickLook.Plugin.ThumbnailViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ThumbnailViewer/Plugin.cs index 4599504..617f6f1 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ThumbnailViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ThumbnailViewer/Plugin.cs @@ -28,7 +28,7 @@ namespace QuickLook.Plugin.ThumbnailViewer; public class Plugin : IViewer { - private static readonly HashSet WellKnownImageExtensions = new( + private static readonly HashSet WellKnownExtensions = new( [ ".cdr", // CorelDraw ".fig", // Figma @@ -47,7 +47,7 @@ public class Plugin : IViewer public bool CanHandle(string path) { - return !Directory.Exists(path) && WellKnownImageExtensions.Contains(Path.GetExtension(path.ToLower())); + return !Directory.Exists(path) && WellKnownExtensions.Contains(Path.GetExtension(path.ToLower())); } public void Prepare(string path, ContextObject context)