Skip to content

Commit 2dd6fd9

Browse files
committed
feat: WebView2 运行时缺失时回退提示窗
检测不到 WebView2 运行时则显示安装引导窗, 后端照常启动以便导出模式局域网访问
1 parent 2c7b732 commit 2dd6fd9

4 files changed

Lines changed: 85 additions & 0 deletions

File tree

ChuChartManager/AppMain.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public partial class AppMain : ISingleInstance
66
{
77
public static Browser? BrowserWin { get; set; }
88
public static OobeBrowser? OobeBrowserWin { get; set; }
9+
public static LauncherForm? LauncherWin { get; set; }
910
public static SynchronizationContext? UiContext { get; set; }
1011

1112
public static void ShowBrowser(string loopbackUrl)

ChuChartManager/LauncherForm.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Diagnostics;
2+
3+
namespace ChuChartManager;
4+
5+
public class LauncherForm : Form
6+
{
7+
private const string WebView2DownloadUrl = "https://developer.microsoft.com/microsoft-edge/webview2/";
8+
9+
public LauncherForm()
10+
{
11+
Text = "ChuChartManager";
12+
Width = 520;
13+
Height = 280;
14+
StartPosition = FormStartPosition.CenterScreen;
15+
FormBorderStyle = FormBorderStyle.FixedDialog;
16+
MaximizeBox = false;
17+
MinimizeBox = false;
18+
19+
var iconPath = Path.Combine(AppContext.BaseDirectory, "icon.ico");
20+
if (File.Exists(iconPath))
21+
Icon = new Icon(iconPath);
22+
23+
var message = new Label
24+
{
25+
Text = "ChuChartManager 需要 Microsoft Edge WebView2 运行时才能显示界面。\n\n"
26+
+ "当前系统未安装该运行时,请先安装后重新启动本程序。",
27+
Dock = DockStyle.Top,
28+
Height = 140,
29+
Padding = new Padding(24, 32, 24, 0),
30+
Font = new Font("Microsoft YaHei UI", 10),
31+
};
32+
33+
var installButton = new Button
34+
{
35+
Text = "下载并安装 WebView2",
36+
Width = 200,
37+
Height = 40,
38+
Left = 24,
39+
Top = 170,
40+
};
41+
installButton.Click += (_, _) =>
42+
Process.Start(new ProcessStartInfo(WebView2DownloadUrl) { UseShellExecute = true });
43+
44+
var exitButton = new Button
45+
{
46+
Text = "退出",
47+
Width = 120,
48+
Height = 40,
49+
Left = 360,
50+
Top = 170,
51+
};
52+
exitButton.Click += (_, _) => Close();
53+
54+
Controls.Add(message);
55+
Controls.Add(installButton);
56+
Controls.Add(exitButton);
57+
}
58+
59+
protected override void OnFormClosed(FormClosedEventArgs e)
60+
{
61+
base.OnFormClosed(e);
62+
Application.Exit();
63+
}
64+
}

ChuChartManager/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ public static void Main()
2828
var scanner = new MusicScanner(StaticSettings.GamePath);
2929
scanner.ScanAll();
3030
StaticSettings.Scanner = scanner;
31+
}
3132

33+
if (!WebViewHelper.IsRuntimeAvailable())
34+
{
35+
AppMain.LauncherWin = new LauncherForm();
36+
AppMain.LauncherWin.Show();
37+
}
38+
else if (hasGamePath)
39+
{
3240
AppMain.BrowserWin = new Browser();
3341
AppMain.BrowserWin.Show();
3442
}

ChuChartManager/WebViewHelper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ namespace ChuChartManager;
44

55
public static class WebViewHelper
66
{
7+
public static bool IsRuntimeAvailable()
8+
{
9+
try
10+
{
11+
return !string.IsNullOrEmpty(CoreWebView2Environment.GetAvailableBrowserVersionString());
12+
}
13+
catch
14+
{
15+
return false;
16+
}
17+
}
18+
719
public static void SetupCoreWebView2(CoreWebView2 coreWebView2, Uri? loopbackUrl)
820
{
921
coreWebView2.SetVirtualHostNameToFolderMapping(

0 commit comments

Comments
 (0)