Code Cleanup

This commit is contained in:
ema
2024-12-11 22:17:26 +08:00
parent c056438c58
commit 28ec7655f8
89 changed files with 7796 additions and 7698 deletions

View File

@@ -1,17 +1,17 @@
// Copyright © 2021 Paddy Xu and Frank Becker
//
//
// This file is part of QuickLook program.
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -20,67 +20,66 @@ using System.IO;
using System.Text;
using Microsoft.Web.WebView2.Core;
namespace QuickLook.Plugin.HtmlViewer
namespace QuickLook.Plugin.HtmlViewer;
internal static class Helper
{
internal static class Helper
public static bool IsWebView2Available()
{
public static bool IsWebView2Available()
try
{
try
{
return !string.IsNullOrEmpty(CoreWebView2Environment.GetAvailableBrowserVersionString());
}
catch (Exception)
{
return false;
}
return !string.IsNullOrEmpty(CoreWebView2Environment.GetAvailableBrowserVersionString());
}
public static Uri FilePathToFileUrl(string filePath)
catch (Exception)
{
var uri = new StringBuilder();
foreach (var v in filePath)
if (v >= 'a' && v <= 'z' || v >= 'A' && v <= 'Z' || v >= '0' && v <= '9' ||
v == '+' || v == '/' || v == ':' || v == '.' || v == '-' || v == '_' || v == '~' ||
v > '\x80')
uri.Append(v);
else if (v == Path.DirectorySeparatorChar || v == Path.AltDirectorySeparatorChar)
uri.Append('/');
else
uri.Append($"%{(int) v:X2}");
if (uri.Length >= 2 && uri[0] == '/' && uri[1] == '/') // UNC path
uri.Insert(0, "file:");
else
uri.Insert(0, "file:///");
try
{
return new Uri(uri.ToString());
}
catch
{
return null;
}
}
internal static string GetUrlPath(string url)
{
var index = -1;
var lines = File.ReadAllLines(url);
foreach (var line in lines)
if (line.ToLower().Contains("url="))
{
index = Array.IndexOf(lines, line);
break;
}
if (index != -1)
{
var fullLine = lines.GetValue(index);
return fullLine.ToString().Substring(fullLine.ToString().LastIndexOf('=') + 1);
}
return url;
return false;
}
}
}
public static Uri FilePathToFileUrl(string filePath)
{
var uri = new StringBuilder();
foreach (var v in filePath)
if (v >= 'a' && v <= 'z' || v >= 'A' && v <= 'Z' || v >= '0' && v <= '9' ||
v == '+' || v == '/' || v == ':' || v == '.' || v == '-' || v == '_' || v == '~' ||
v > '\x80')
uri.Append(v);
else if (v == Path.DirectorySeparatorChar || v == Path.AltDirectorySeparatorChar)
uri.Append('/');
else
uri.Append($"%{(int)v:X2}");
if (uri.Length >= 2 && uri[0] == '/' && uri[1] == '/') // UNC path
uri.Insert(0, "file:");
else
uri.Insert(0, "file:///");
try
{
return new Uri(uri.ToString());
}
catch
{
return null;
}
}
internal static string GetUrlPath(string url)
{
var index = -1;
var lines = File.ReadAllLines(url);
foreach (var line in lines)
if (line.ToLower().Contains("url="))
{
index = Array.IndexOf(lines, line);
break;
}
if (index != -1)
{
var fullLine = lines.GetValue(index);
return fullLine.ToString().Substring(fullLine.ToString().LastIndexOf('=') + 1);
}
return url;
}
}

View File

@@ -1,17 +1,17 @@
// Copyright © 2021 Paddy Xu and Frank Becker
//
//
// This file is part of QuickLook program.
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -21,50 +21,49 @@ using System.Windows;
using System.Windows.Threading;
using QuickLook.Common.Plugin;
namespace QuickLook.Plugin.HtmlViewer
namespace QuickLook.Plugin.HtmlViewer;
public class Plugin : IViewer
{
public class Plugin : IViewer
private static readonly string[] Extensions = [".mht", ".mhtml", ".htm", ".html"];
private static readonly string[] SupportedProtocols = ["http", "https"];
private WebpagePanel _panel;
public int Priority => 0;
public void Init()
{
private static readonly string[] Extensions = {".mht", ".mhtml", ".htm", ".html"};
private static readonly string[] SupportedProtocols = {"http", "https"};
private WebpagePanel _panel;
public int Priority => 0;
public void Init()
{
}
public bool CanHandle(string path)
{
return !Directory.Exists(path) && (Extensions.Any(path.ToLower().EndsWith) ||
path.ToLower().EndsWith(".url") &&
SupportedProtocols.Contains(Helper.GetUrlPath(path).Split(':')[0]
.ToLower()));
}
public void Prepare(string path, ContextObject context)
{
context.PreferredSize = new Size(1280, 720);
}
public void View(string path, ContextObject context)
{
_panel = new WebpagePanel();
context.ViewerContent = _panel;
context.Title = Path.IsPathRooted(path) ? Path.GetFileName(path) : path;
if (path.ToLower().EndsWith(".url"))
path = Helper.GetUrlPath(path);
_panel.NavigateToFile(path);
_panel.Dispatcher.Invoke(() => { context.IsBusy = false; }, DispatcherPriority.Loaded);
}
public void Cleanup()
{
_panel?.Dispose();
_panel = null;
}
}
}
public bool CanHandle(string path)
{
return !Directory.Exists(path) && (Extensions.Any(path.ToLower().EndsWith) ||
path.ToLower().EndsWith(".url") &&
SupportedProtocols.Contains(Helper.GetUrlPath(path).Split(':')[0]
.ToLower()));
}
public void Prepare(string path, ContextObject context)
{
context.PreferredSize = new Size(1280, 720);
}
public void View(string path, ContextObject context)
{
_panel = new WebpagePanel();
context.ViewerContent = _panel;
context.Title = Path.IsPathRooted(path) ? Path.GetFileName(path) : path;
if (path.ToLower().EndsWith(".url"))
path = Helper.GetUrlPath(path);
_panel.NavigateToFile(path);
_panel.Dispatcher.Invoke(() => { context.IsBusy = false; }, DispatcherPriority.Loaded);
}
public void Cleanup()
{
_panel?.Dispose();
_panel = null;
}
}

View File

@@ -28,282 +28,281 @@ using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace QuickLook.Plugin.HtmlViewer
namespace QuickLook.Plugin.HtmlViewer;
public class WebpagePanel : UserControl
{
public class WebpagePanel : UserControl
private Uri _currentUri;
private string _primaryPath;
private string _fallbackPath;
private WebView2 _webView;
public WebpagePanel()
{
private Uri _currentUri;
private string _primaryPath;
private string _fallbackPath;
private WebView2 _webView;
public WebpagePanel()
if (!Helper.IsWebView2Available())
{
if (!Helper.IsWebView2Available())
Content = CreateDownloadButton();
}
else
{
_webView = new WebView2
{
Content = CreateDownloadButton();
}
else
{
_webView = new WebView2
CreationProperties = new CoreWebView2CreationProperties
{
CreationProperties = new CoreWebView2CreationProperties
{
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;
_webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
Content = _webView;
}
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;
_webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
Content = _webView;
}
}
public void NavigateToFile(string path, string fallbackPath = null)
{
try
{
_primaryPath = Path.GetDirectoryName(path);
_fallbackPath = fallbackPath;
}
catch (Exception e)
{
// Omit logging for less important logs
Debug.WriteLine(e);
}
public void NavigateToFile(string path, string fallbackPath = null)
var uri = Path.IsPathRooted(path) ? Helper.FilePathToFileUrl(path) : new Uri(path);
NavigateToUri(uri);
}
public void NavigateToUri(Uri uri)
{
if (_webView == null)
return;
_webView.Source = uri;
_currentUri = _webView.Source;
}
public void NavigateToHtml(string html)
{
_webView?.EnsureCoreWebView2Async()
.ContinueWith(_ => Dispatcher.Invoke(() => _webView?.NavigateToString(html)));
}
private void NavigationStarting_CancelNavigation(object sender, CoreWebView2NavigationStartingEventArgs e)
{
if (e.Uri.StartsWith("data:")) // when using NavigateToString
return;
var newUri = new Uri(e.Uri);
if (newUri == _currentUri) return;
e.Cancel = true;
// Open in default browser
try
{
try
if (!Uri.TryCreate(e.Uri, UriKind.Absolute, out var uri))
{
_primaryPath = Path.GetDirectoryName(path);
_fallbackPath = fallbackPath;
}
catch (Exception e)
{
// Omit logging for less important logs
Debug.WriteLine(e);
}
var uri = Path.IsPathRooted(path) ? Helper.FilePathToFileUrl(path) : new Uri(path);
NavigateToUri(uri);
}
public void NavigateToUri(Uri uri)
{
if (_webView == null)
Debug.WriteLine($"Invalid URI format: {e.Uri}");
return;
}
_webView.Source = uri;
_currentUri = _webView.Source;
}
public void NavigateToHtml(string html)
{
_webView?.EnsureCoreWebView2Async()
.ContinueWith(_ => Dispatcher.Invoke(() => _webView?.NavigateToString(html)));
}
private void NavigationStarting_CancelNavigation(object sender, CoreWebView2NavigationStartingEventArgs e)
{
if (e.Uri.StartsWith("data:")) // when using NavigateToString
return;
var newUri = new Uri(e.Uri);
if (newUri == _currentUri) return;
e.Cancel = true;
// Open in default browser
try
// Safe schemes can open directly
if (uri.Scheme == Uri.UriSchemeHttp ||
uri.Scheme == Uri.UriSchemeHttps ||
uri.Scheme == Uri.UriSchemeMailto)
{
if (!Uri.TryCreate(e.Uri, UriKind.Absolute, out var uri))
try
{
Debug.WriteLine($"Invalid URI format: {e.Uri}");
return;
Process.Start(uri.AbsoluteUri);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to open URL: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
return;
}
// Safe schemes can open directly
if (uri.Scheme == Uri.UriSchemeHttp ||
uri.Scheme == Uri.UriSchemeHttps ||
uri.Scheme == Uri.UriSchemeMailto)
// Ask user for unsafe schemes. Use dispatcher to avoid blocking thread.
string associatedApp = GetAssociatedAppForScheme(uri.Scheme);
_ = Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
// TODO: translation
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(uri.AbsoluteUri);
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)
{
var 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)
{
var 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 Get Associated App For Scheme
private void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
_webView.DefaultBackgroundColor = Color.White; // Reset to white after page load to match expected default behavior
}
private void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
_webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
_webView.CoreWebView2.WebResourceRequested += (sender, args) =>
{
if (string.IsNullOrWhiteSpace(_fallbackPath) || !Directory.Exists(_fallbackPath))
{
return;
}
// Ask user for unsafe schemes. Use dispatcher to avoid blocking thread.
string associatedApp = GetAssociatedAppForScheme(uri.Scheme);
_ = Application.Current.Dispatcher.BeginInvoke(new Action(() =>
try
{
// TODO: translation
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);
var requestedUri = new Uri(args.Request.Uri);
if (result == MessageBoxResult.Yes)
// Check if the request is for a local file
if (requestedUri.Scheme == "file" && !File.Exists(requestedUri.LocalPath))
{
try
// Try loading from fallback directory
var fileName = Path.GetFileName(requestedUri.LocalPath);
var fileDirectoryName = Path.GetDirectoryName(requestedUri.LocalPath);
// Convert the primary path to fallback path
if (fileDirectoryName.StartsWith(_primaryPath))
{
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}");
}
}
var fallbackFilePath = Path.Combine(
_fallbackPath.Trim('/', '\\'), // Make it combinable
fileDirectoryName.Substring(_primaryPath.Length).Trim('/', '\\'), // Make it combinable
fileName
);
#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)
{
var 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)
{
var 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 Get Associated App For Scheme
private void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
_webView.DefaultBackgroundColor = Color.White; // Reset to white after page load to match expected default behavior
}
private void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
_webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
_webView.CoreWebView2.WebResourceRequested += (sender, args) =>
{
if (string.IsNullOrWhiteSpace(_fallbackPath) || !Directory.Exists(_fallbackPath))
{
return;
}
try
{
var requestedUri = new Uri(args.Request.Uri);
// Check if the request is for a local file
if (requestedUri.Scheme == "file" && !File.Exists(requestedUri.LocalPath))
{
// Try loading from fallback directory
var fileName = Path.GetFileName(requestedUri.LocalPath);
var fileDirectoryName = Path.GetDirectoryName(requestedUri.LocalPath);
// Convert the primary path to fallback path
if (fileDirectoryName.StartsWith(_primaryPath))
if (File.Exists(fallbackFilePath))
{
var fallbackFilePath = Path.Combine(
_fallbackPath.Trim('/', '\\'), // Make it combinable
fileDirectoryName.Substring(_primaryPath.Length).Trim('/', '\\'), // Make it combinable
fileName
);
if (File.Exists(fallbackFilePath))
{
// Serve the file from the fallback directory
var fileStream = File.OpenRead(fallbackFilePath);
var response = _webView.CoreWebView2.Environment.CreateWebResourceResponse(
fileStream, 200, "OK", "Content-Type: application/octet-stream");
args.Response = response;
}
// Serve the file from the fallback directory
var fileStream = File.OpenRead(fallbackFilePath);
var response = _webView.CoreWebView2.Environment.CreateWebResourceResponse(
fileStream, 200, "OK", "Content-Type: application/octet-stream");
args.Response = response;
}
}
}
catch (Exception e)
{
// We don't need to feel burdened by any exceptions
Debug.WriteLine(e);
}
};
}
}
public void Dispose()
{
_webView?.Dispose();
_webView = null;
}
private object CreateDownloadButton()
{
var button = new Button
{
Content = TranslationHelper.Get("WEBVIEW2_NOT_AVAILABLE",
domain: Assembly.GetExecutingAssembly().GetName().Name),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Padding = new Thickness(20, 6, 20, 6)
}
catch (Exception e)
{
// We don't need to feel burdened by any exceptions
Debug.WriteLine(e);
}
};
button.Click += (sender, e) => Process.Start("https://go.microsoft.com/fwlink/p/?LinkId=2124703");
return button;
}
}
public void Dispose()
{
_webView?.Dispose();
_webView = null;
}
private object CreateDownloadButton()
{
var button = new Button
{
Content = TranslationHelper.Get("WEBVIEW2_NOT_AVAILABLE",
domain: Assembly.GetExecutingAssembly().GetName().Name),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Padding = new Thickness(20, 6, 20, 6)
};
button.Click += (sender, e) => Process.Start("https://go.microsoft.com/fwlink/p/?LinkId=2124703");
return button;
}
}