From 9af3fe7f4ef6b84f4758089fd34373be85e1a77d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 09:12:48 +0000 Subject: [PATCH 1/4] Initial plan From c4ee8937bc12f74c0ad762b57f8590a2721b7199 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 17 Nov 2025 09:19:39 +0000 Subject: [PATCH 2/4] Add theme toggle button to SVG preview Co-authored-by: emako <24737061+emako@users.noreply.github.com> --- .../Webview/Svg/SvgImagePanel.cs | 98 ++++++++++++++++++- .../Webview/Svg/SvgImagePanel.xaml | 32 ++++++ .../Webview/WebHandler.cs | 2 +- 3 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Webview/Svg/SvgImagePanel.xaml diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Webview/Svg/SvgImagePanel.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Webview/Svg/SvgImagePanel.cs index 9374fd128..51c35c289 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Webview/Svg/SvgImagePanel.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Webview/Svg/SvgImagePanel.cs @@ -18,6 +18,7 @@ using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.Wpf; using QuickLook.Common.Helpers; +using QuickLook.Common.Plugin; using QuickLook.Plugin.HtmlViewer; using System; using System.Collections.Generic; @@ -29,17 +30,36 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; namespace QuickLook.Plugin.ImageViewer.Webview.Svg; -public class SvgImagePanel : WebpagePanel, IWebImagePanel +public partial class SvgImagePanel : UserControl, IWebImagePanel { protected const string _resourcePrefix = "QuickLook.Plugin.ImageViewer.Resources."; protected internal static readonly Dictionary _resources = []; protected byte[] _homePage; + protected WebView2 _webView; + protected Uri _currentUri; + protected string _primaryPath; + protected string _fallbackPath; + private ContextObject _contextObject; private object _objectForScripting; + public string FallbackPath + { + get => _fallbackPath; + set => _fallbackPath = value; + } + + public ContextObject ContextObject + { + get => _contextObject; + set => _contextObject = value; + } + public object ObjectForScripting { get => _objectForScripting; @@ -60,7 +80,21 @@ static SvgImagePanel() InitializeResources(); } - protected override void InitializeComponent() + public SvgImagePanel() + { + InitializeComponent(); + + // Clear merged dictionaries from design time + Resources.MergedDictionaries.Clear(); + + // Initialize WebView2 + InitializeWebView(); + + // Wire up the theme toggle button + buttonBackgroundColour.Click += OnBackgroundColourOnClick; + } + + protected void InitializeWebView() { _webView = new WebView2() { @@ -71,7 +105,53 @@ protected override void InitializeComponent() DefaultBackgroundColor = Color.Transparent, }; _webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted; - Content = _webView; + + // Add WebView2 to the Grid at the first position (behind the button) + var grid = (Grid)Content; + grid.Children.Insert(0, _webView); + } + + private void OnBackgroundColourOnClick(object sender, RoutedEventArgs e) + { + if (ContextObject == null) return; + + // Toggle the theme + var newTheme = ContextObject.Theme == Themes.Dark ? Themes.Light : Themes.Dark; + ContextObject.Theme = newTheme; + + // Save the theme preference + SettingHelper.Set("LastTheme", (int)newTheme, "QuickLook.Plugin.ImageViewer"); + + // Update WebView2 background color + UpdateWebViewBackgroundColor(); + } + + private void UpdateWebViewBackgroundColor() + { + if (_webView == null) return; + + var isDark = ContextObject?.Theme == Themes.Dark; + _webView.DefaultBackgroundColor = isDark + ? Color.FromArgb(255, 32, 32, 32) + : Color.White; + } + + public void NavigateToUri(Uri uri) + { + if (_webView == null) + return; + + _webView.Source = uri; + _currentUri = _webView.Source; + } + + protected virtual void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e) + { + if (e.IsSuccess) + { + _webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All); + _webView.CoreWebView2.WebResourceRequested += WebView_WebResourceRequested; + } } protected static void InitializeResources() @@ -102,10 +182,14 @@ public virtual void Preview(string path) ObjectForScripting ??= new ScriptHandler(path); _homePage = _resources["/svg2html.html"]; + + // Update WebView2 background color based on current theme + UpdateWebViewBackgroundColor(); + NavigateToUri(new Uri("file://quicklook/")); } - protected override void WebView_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs args) + protected virtual void WebView_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs args) { Debug.WriteLine($"[{args.Request.Method}] {args.Request.Uri}"); @@ -186,6 +270,12 @@ public static string ReadString(string key) return reader.ReadToEnd(); } + public void Dispose() + { + _webView?.Dispose(); + _webView = null; + } + public new static class MimeTypes { public static string GetContentTypeHeader(string extension = null) diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Webview/Svg/SvgImagePanel.xaml b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Webview/Svg/SvgImagePanel.xaml new file mode 100644 index 000000000..074c355b0 --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Webview/Svg/SvgImagePanel.xaml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + +