mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-12-25 02:00:12 +08:00
Merge pull request #1490 from KamilDev/feature/improve-markdown-viewer
Improved MarkdownViewer
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Copyright © 2021 Paddy Xu and Frank Becker
|
||||
// Copyright © 2021 Paddy Xu and Frank Becker
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
@@ -17,8 +17,11 @@
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Microsoft.Web.WebView2.Core;
|
||||
@@ -44,10 +47,12 @@ namespace QuickLook.Plugin.HtmlViewer
|
||||
{
|
||||
CreationProperties = new CoreWebView2CreationProperties
|
||||
{
|
||||
UserDataFolder = Path.Combine(SettingHelper.LocalDataPath, @"WebView2_Data\\")
|
||||
}
|
||||
UserDataFolder = Path.Combine(SettingHelper.LocalDataPath, @"WebView2_Data\\"),
|
||||
},
|
||||
DefaultBackgroundColor = OSThemeHelper.AppsUseDarkTheme() ? Color.FromArgb(255, 32, 32, 32) : Color.White, // Prevent white flash in dark mode
|
||||
};
|
||||
_webView.NavigationStarting += NavigationStarting_CancelNavigation;
|
||||
_webView.NavigationCompleted += WebView_NavigationCompleted;
|
||||
Content = _webView;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +85,134 @@ namespace QuickLook.Plugin.HtmlViewer
|
||||
return;
|
||||
|
||||
var newUri = new Uri(e.Uri);
|
||||
if (newUri != _currentUri) e.Cancel = true;
|
||||
if (newUri == _currentUri) return;
|
||||
e.Cancel = true;
|
||||
|
||||
// Open in default browser
|
||||
try
|
||||
{
|
||||
if (!Uri.TryCreate(e.Uri, UriKind.Absolute, out var uri))
|
||||
{
|
||||
Debug.WriteLine($"Invalid URI format: {e.Uri}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Safe schemes can open directly
|
||||
if (uri.Scheme == Uri.UriSchemeHttp ||
|
||||
uri.Scheme == Uri.UriSchemeHttps ||
|
||||
uri.Scheme == Uri.UriSchemeMailto)
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start(uri.AbsoluteUri);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Failed to open URL: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Ask user for unsafe schemes. Use dispatcher to avoid blocking thread.
|
||||
string associatedApp = GetAssociatedAppForScheme(uri.Scheme);
|
||||
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
var result = MessageBox.Show(
|
||||
!string.IsNullOrEmpty(associatedApp) ?
|
||||
$"The following link will open in {associatedApp}:\n{e.Uri}" : $"The following link will open:\n{e.Uri}",
|
||||
!string.IsNullOrEmpty(associatedApp) ?
|
||||
$"Open {associatedApp}?" : "Open custom URI?",
|
||||
MessageBoxButton.YesNo,
|
||||
MessageBoxImage.Information);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start(e.Uri);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Failed to open URL: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"Failed to open URL: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
#region Get Associated App For Scheme
|
||||
[DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern uint AssocQueryString(
|
||||
AssocF flags,
|
||||
AssocStr str,
|
||||
string pszAssoc,
|
||||
string pszExtra,
|
||||
[Out] StringBuilder pszOut,
|
||||
ref uint pcchOut);
|
||||
|
||||
[Flags]
|
||||
private enum AssocF
|
||||
{
|
||||
None = 0,
|
||||
VerifyExists = 0x1
|
||||
}
|
||||
|
||||
private enum AssocStr
|
||||
{
|
||||
Command = 1,
|
||||
Executable = 2,
|
||||
FriendlyAppName = 4
|
||||
}
|
||||
|
||||
private string GetAssociatedAppForScheme(string scheme)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Try to get friendly app name first
|
||||
uint pcchOut = 0;
|
||||
AssocQueryString(AssocF.None, AssocStr.FriendlyAppName, scheme, null, null, ref pcchOut);
|
||||
|
||||
if (pcchOut > 0)
|
||||
{
|
||||
StringBuilder pszOut = new StringBuilder((int)pcchOut);
|
||||
AssocQueryString(AssocF.None, AssocStr.FriendlyAppName, scheme, null, pszOut, ref pcchOut);
|
||||
|
||||
var appName = pszOut.ToString().Trim();
|
||||
if (!string.IsNullOrEmpty(appName))
|
||||
return appName;
|
||||
}
|
||||
|
||||
// Fall back to executable name if friendly name is not available
|
||||
pcchOut = 0;
|
||||
AssocQueryString(AssocF.None, AssocStr.Executable, scheme, null, null, ref pcchOut);
|
||||
|
||||
if (pcchOut > 0)
|
||||
{
|
||||
StringBuilder pszOut = new StringBuilder((int)pcchOut);
|
||||
AssocQueryString(AssocF.None, AssocStr.Executable, scheme, null, pszOut, ref pcchOut);
|
||||
|
||||
var exeName = pszOut.ToString().Trim();
|
||||
if (!string.IsNullOrEmpty(exeName))
|
||||
return Path.GetFileName(exeName);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"Failed to get associated app: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
private void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
|
||||
{
|
||||
_webView.DefaultBackgroundColor = Color.White; // Reset to white after page load to match expected default behavior
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user