diff --git a/Tiefsee/Program.cs b/Tiefsee/Program.cs index ae16395..63862d9 100644 --- a/Tiefsee/Program.cs +++ b/Tiefsee/Program.cs @@ -16,8 +16,6 @@ static class Program { public static StartWindow startWindow; /// 透過 UserAgent 來驗證是否有權限請求 localhost server API public static string webvviewUserAgent = "Tiefsee"; - /// webview2 的啟動參數 - public static string webvviewArguments; /// /// 應用程式的主要進入點 @@ -91,12 +89,6 @@ static void Main(string[] args) { Application.SetCompatibleTextRenderingDefault(false); Application.SetHighDpiMode(HighDpiMode.PerMonitorV2); // 高 DPI 模式 - //--disable-web-security 允許跨域請求 - //--disable-features=msWebOOUI,msPdfOOUI 禁止迷你選單 - //--user-agent 覆寫userAgent - //--enable-features=msWebView2EnableDraggableRegions 讓webview2支援css「app-region:drag」 - webvviewArguments = null; - if (startType != 1) { AppLock(false); } // 解除鎖定 startWindow = new StartWindow(); diff --git a/Tiefsee/StartWindow.cs b/Tiefsee/StartWindow.cs index 59e6a50..28503a2 100644 --- a/Tiefsee/StartWindow.cs +++ b/Tiefsee/StartWindow.cs @@ -1,4 +1,3 @@ -using Microsoft.Web.WebView2.Core; using System.IO; using System.IO.Pipes; using System.Text; @@ -240,10 +239,8 @@ public async void InitJumpTask() { /// 初始化webview2 /// private async void InitWebview() { - var opts = new CoreWebView2EnvironmentOptions { AdditionalBrowserArguments = Program.webvviewArguments }; - Microsoft.Web.WebView2.WinForms.WebView2 wv2 = new Microsoft.Web.WebView2.WinForms.WebView2(); - var webView2Environment = await CoreWebView2Environment.CreateAsync(null, AppPath.appData, opts); - await wv2.EnsureCoreWebView2Async(webView2Environment); + var wv2 = new Microsoft.Web.WebView2.WinForms.WebView2(); + await wv2.EnsureCoreWebView2Async(await WebWindow.GetCoreWebView2Environment()); } /// diff --git a/Tiefsee/VW/WV_Window.cs b/Tiefsee/VW/WV_Window.cs index f1e5f97..e5fd3fa 100644 --- a/Tiefsee/VW/WV_Window.cs +++ b/Tiefsee/VW/WV_Window.cs @@ -29,8 +29,8 @@ public void ClearBrowserCache() { /// /// 取得 webview2 版本資訊 /// - public string GetBrowserVersionString() { - return M.Wv2Environment.BrowserVersionString; + public async Task GetBrowserVersionString() { + return (await WebWindow.GetCoreWebView2Environment()).BrowserVersionString; } /// diff --git a/Tiefsee/WebWindow.cs b/Tiefsee/WebWindow.cs index 22365ed..c75e7b1 100644 --- a/Tiefsee/WebWindow.cs +++ b/Tiefsee/WebWindow.cs @@ -12,7 +12,7 @@ namespace Tiefsee; public class WebWindow : FormNone { private WebView2 _wv2; - private CoreWebView2Environment _webView2Environment; + private static CoreWebView2Environment _webView2Environment; /// 父視窗 private WebWindow _parentWindow; /// 命令列參數 @@ -35,7 +35,6 @@ public class WebWindow : FormNone { private bool _windowRoundedCorners = false; public WebView2 Wv2 { get { return _wv2; } } - public CoreWebView2Environment Wv2Environment { get { return _webView2Environment; } } public WebWindow ParentWindow { get { return _parentWindow; } } public string[] Args { get { return _args; } } public static WebWindow TempWindow { get { return _tempWindow; } } @@ -289,6 +288,22 @@ public class AppInfo { public DataPlugin plugin { get; set; } = Plugin.dataPlugin; } + /// + /// 取得 CoreWebView2Environment + /// + public static async Task GetCoreWebView2Environment() { + if (_webView2Environment != null) { + // --disable-web-security 允許跨域請求 + // --disable-features=msWebOOUI,msPdfOOUI 禁止迷你選單 + // --user-agent 覆寫userAgent + // --enable-features=msWebView2EnableDraggableRegions 讓 webview2 支援 css「app-region:drag」 + string webvviewArguments = null; + var opts = new CoreWebView2EnvironmentOptions { AdditionalBrowserArguments = webvviewArguments }; + _webView2Environment = await CoreWebView2Environment.CreateAsync(null, AppPath.appData, opts); + } + return _webView2Environment; + } + /// /// /// @@ -335,9 +350,9 @@ public async Task Init() { } }); - var opts = new CoreWebView2EnvironmentOptions { AdditionalBrowserArguments = Program.webvviewArguments }; - _webView2Environment = await CoreWebView2Environment.CreateAsync(null, AppPath.appData, opts); - await _wv2.EnsureCoreWebView2Async(_webView2Environment); // 等待初始化完成 + // 等待初始化完成 + await _wv2.EnsureCoreWebView2Async(await GetCoreWebView2Environment()); + // 指定為深色主題 _wv2.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Dark; // 是否在啟用了觸摸輸入的設備上使用輕掃手勢在 WebView2 中導航