Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions QuickLook.Plugin/QuickLook.Plugin.ChmViewer/ChmWebpagePanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using QuickLook.Common.Helpers;
using QuickLook.Plugin.HtmlViewer;
using System;
using System.Collections.Generic;
Expand All @@ -42,27 +41,6 @@ static ChmWebpagePanel()
public ChmWebpagePanel()
{
_homePage = ReadResource("/chm2html.html") ?? [];

if (OSThemeHelper.AppsUseDarkTheme())
{
// Invoke using reflection: WebView2.CreationProperties.AdditionalBrowserArguments
// This approach allows the library to avoid direct dependency on WebView2
if (typeof(WebpagePanel).GetField("_webView", BindingFlags.NonPublic | BindingFlags.Instance) is FieldInfo fieldInfo)
{
object webView2 = fieldInfo.GetValue(this);

if (webView2?.GetType().GetProperty("CreationProperties", BindingFlags.Public | BindingFlags.Instance) is PropertyInfo creationPropertiesProperty)
{
object creationProperties = creationPropertiesProperty.GetValue(webView2);

if (creationProperties?.GetType().GetProperty("AdditionalBrowserArguments", BindingFlags.Public | BindingFlags.Instance) is PropertyInfo additionalBrowserArgumentsProperty)
{
string additionalBrowserArguments = (additionalBrowserArgumentsProperty.GetValue(creationProperties) as string) ?? string.Empty;
additionalBrowserArgumentsProperty.SetValue(creationProperties, additionalBrowserArguments + " --enable-features=WebContentsForceDark");
}
}
}
}
}

private static void InitializeResources()
Expand Down
8 changes: 0 additions & 8 deletions QuickLook.Plugin/QuickLook.Plugin.FontViewer/WebfontPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ static WebfontPanel()
InitializeResources();
}

public WebfontPanel()
{
if (OSThemeHelper.AppsUseDarkTheme())
{
_webView.CreationProperties.AdditionalBrowserArguments = "--enable-features=WebContentsForceDark";
}
}

protected static void InitializeResources()
{
if (_resources.Any()) return;
Expand Down
50 changes: 42 additions & 8 deletions QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ protected virtual void InitializeComponent()
{
_webView = new WebView2
{
CreationProperties = new CoreWebView2CreationProperties
{
UserDataFolder = Path.Combine(SettingHelper.LocalDataPath, @"WebView2_Data\"),
},
CreationProperties = CreateCreationProperties(),

// Prevent white flash in dark mode
DefaultBackgroundColor = OSThemeHelper.AppsUseDarkTheme() ? Color.FromArgb(255, 32, 32, 32) : Color.White,
Expand All @@ -68,6 +65,14 @@ protected virtual void InitializeComponent()
Content = _webView;
}

protected static CoreWebView2CreationProperties CreateCreationProperties()
{
return new CoreWebView2CreationProperties
{
UserDataFolder = Path.Combine(SettingHelper.LocalDataPath, @"WebView2_Data\"),
};
}

public void NavigateToFile(string path)
{
try
Expand Down Expand Up @@ -168,16 +173,45 @@ protected virtual void Webview_NavigationStarting(object sender, CoreWebView2Nav

protected virtual void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
_webView.DefaultBackgroundColor = Color.White; // Reset to white after page load to match expected default behavior
if (_webView != null)
_webView.DefaultBackgroundColor = Color.White; // Reset to white after page load to match expected default behavior
}

protected virtual void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
if (!e.IsSuccess)
{
_webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
_webView.CoreWebView2.WebResourceRequested += WebView_WebResourceRequested;
ProcessHelper.WriteLog($"WebView2 initialization failed: {e.InitializationException}");

// Remove the half-initialized control from the visual tree before
// its window procedure can receive messages with a null COM
// interface, then dispose it and show the fallback action.
Content = null;
_webView.CoreWebView2InitializationCompleted -= WebView_CoreWebView2InitializationCompleted;
try
{
_webView.Dispose();
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to dispose WebView2: {ex.Message}");
}
_webView = null;
Content = CreateDownloadButton();
Comment thread
Henglie marked this conversation as resolved.
Resolved
return;
}

_webView.CoreWebView2.Profile.PreferredColorScheme = OSThemeHelper.AppsUseDarkTheme()
? CoreWebView2PreferredColorScheme.Dark
: CoreWebView2PreferredColorScheme.Light;
_webView.CoreWebView2.ProcessFailed += WebView_ProcessFailed;
_webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
_webView.CoreWebView2.WebResourceRequested += WebView_WebResourceRequested;
}

protected virtual void WebView_ProcessFailed(object sender, CoreWebView2ProcessFailedEventArgs e)
{
ProcessHelper.WriteLog($"WebView2 process failed: {e.ProcessFailedKind}; reason: {e.Reason}; exit code: {e.ExitCode}");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

protected virtual void WebView_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

using Microsoft.Web.WebView2.Core;
using QuickLook.Common.Helpers;
using QuickLook.Plugin.HtmlViewer;
using QuickLook.Plugin.ImageViewer.Webview.Svg;
using System;
using System.IO;
Expand All @@ -32,30 +31,6 @@ public class PuImagePanel : SvgImagePanel
{
private string _puContent;

public PuImagePanel()
{
if (OSThemeHelper.AppsUseDarkTheme())
{
// Invoke using reflection: WebView2.CreationProperties.AdditionalBrowserArguments
// This approach allows the library to avoid direct dependency on WebView2
if (typeof(WebpagePanel).GetField("_webView", BindingFlags.NonPublic | BindingFlags.Instance) is FieldInfo fieldInfo)
{
object webView2 = fieldInfo.GetValue(this);

if (webView2?.GetType().GetProperty("CreationProperties", BindingFlags.Public | BindingFlags.Instance) is PropertyInfo creationPropertiesProperty)
{
object creationProperties = creationPropertiesProperty.GetValue(webView2);

if (creationProperties?.GetType().GetProperty("AdditionalBrowserArguments", BindingFlags.Public | BindingFlags.Instance) is PropertyInfo additionalBrowserArgumentsProperty)
{
string additionalBrowserArguments = (additionalBrowserArgumentsProperty.GetValue(creationProperties) as string) ?? string.Empty;
additionalBrowserArgumentsProperty.SetValue(creationProperties, additionalBrowserArguments + "--enable-features=WebContentsForceDark");
}
}
}
}
}

public override void Preview(string path)
Comment thread
Henglie marked this conversation as resolved.
Resolved
{
bool consented = SettingHelper.Get("PlantUMLOnlineConsented", false, "QuickLook.Plugin.ImageViewer");
Expand All @@ -77,7 +52,7 @@ public override void Preview(string path)
string deniedMsg = TranslationHelper.Get("PU_OnlineDeniedMessage", translationFile, failsafe:
"Network access was denied. PlantUML preview requires an internet connection.");
NavigateToHtml(
$"<html><body style='font-family:sans-serif;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;color:#555'>" +
$"<html><body style='font-family:sans-serif;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;color:#555;background:#fff'>" +
$"<p style='max-width:400px;text-align:center'>{HttpUtility.HtmlEncode(deniedMsg)}</p></body></html>");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using QuickLook.Common.Helpers;
using QuickLook.Plugin.HtmlViewer;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -62,10 +61,7 @@ protected override void InitializeComponent()
{
_webView = new WebView2()
{
CreationProperties = new CoreWebView2CreationProperties
{
UserDataFolder = Path.Combine(SettingHelper.LocalDataPath, @"WebView2_Data\"),
},
CreationProperties = CreateCreationProperties(),
DefaultBackgroundColor = Color.Transparent,
};
_webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
Expand Down
Loading