Skip to content

Commit

Permalink
Prevent duplicate initialization of CoreWebView2Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
hbl917070 committed Oct 13, 2024
1 parent 20c876f commit 4340fae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
8 changes: 0 additions & 8 deletions Tiefsee/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ static class Program {
public static StartWindow startWindow;
/// <summary> 透過 UserAgent 來驗證是否有權限請求 localhost server API </summary>
public static string webvviewUserAgent = "Tiefsee";
/// <summary> webview2 的啟動參數 </summary>
public static string webvviewArguments;

/// <summary>
/// 應用程式的主要進入點
Expand Down Expand Up @@ -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();

Expand Down
7 changes: 2 additions & 5 deletions Tiefsee/StartWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.Web.WebView2.Core;
using System.IO;
using System.IO.Pipes;
using System.Text;
Expand Down Expand Up @@ -240,10 +239,8 @@ public async void InitJumpTask() {
/// 初始化webview2
/// </summary>
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());
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Tiefsee/VW/WV_Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public void ClearBrowserCache() {
/// <summary>
/// 取得 webview2 版本資訊
/// </summary>
public string GetBrowserVersionString() {
return M.Wv2Environment.BrowserVersionString;
public async Task<string> GetBrowserVersionString() {
return (await WebWindow.GetCoreWebView2Environment()).BrowserVersionString;
}

/// <summary>
Expand Down
25 changes: 20 additions & 5 deletions Tiefsee/WebWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Tiefsee;
public class WebWindow : FormNone {

private WebView2 _wv2;
private CoreWebView2Environment _webView2Environment;
private static CoreWebView2Environment _webView2Environment;
/// <summary> 父視窗 </summary>
private WebWindow _parentWindow;
/// <summary> 命令列參數 </summary>
Expand All @@ -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; } }
Expand Down Expand Up @@ -289,6 +288,22 @@ public class AppInfo {
public DataPlugin plugin { get; set; } = Plugin.dataPlugin;
}

/// <summary>
/// 取得 CoreWebView2Environment
/// </summary>
public static async Task<CoreWebView2Environment> 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;
}

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -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 中導航
Expand Down

0 comments on commit 4340fae

Please sign in to comment.