Skip to content

Commit 18e32b9

Browse files
committed
子窗口优化
1 parent a910333 commit 18e32b9

4 files changed

Lines changed: 70 additions & 20 deletions

File tree

‎MaiChartManager/Browser.cs‎

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,37 @@ public Browser(string loopbackUrl)
3232
PositionOnScreen();
3333
}
3434

35-
private void PositionOnScreen()
35+
private static Point CalculatePosition(int width, int height)
3636
{
37-
StartPosition = FormStartPosition.Manual;
38-
3937
// 优先选择横屏显示器
4038
var landscape = Screen.AllScreens.FirstOrDefault(s => s.WorkingArea.Width >= s.WorkingArea.Height);
4139
if (landscape != null)
4240
{
4341
// 在横屏显示器中间显示
4442
var area = landscape.WorkingArea;
45-
Location = new Point(
46-
area.X + (area.Width - Width) / 2,
47-
area.Y + (area.Height - Height) / 2
43+
return new Point(
44+
area.X + (area.Width - width) / 2,
45+
area.Y + (area.Height - height) / 2
4846
);
49-
return;
5047
}
5148

5249
// 只有竖屏显示器,在底部正方形区域中间显示
5350
var portrait = Screen.PrimaryScreen ?? Screen.AllScreens[0];
5451
var pArea = portrait.WorkingArea;
55-
var squareSize = pArea.Width; // 正方形边长 = 屏幕宽度
56-
var squareTop = pArea.Bottom - squareSize; // 正方形区域的顶部 Y 坐标
57-
Location = new Point(
58-
pArea.X + (squareSize - Width) / 2,
59-
squareTop + (squareSize - Height) / 2
52+
var squareSize = pArea.Width;
53+
var squareTop = pArea.Bottom - squareSize;
54+
return new Point(
55+
pArea.X + (squareSize - width) / 2,
56+
squareTop + (squareSize - height) / 2
6057
);
6158
}
6259

60+
private void PositionOnScreen()
61+
{
62+
StartPosition = FormStartPosition.Manual;
63+
Location = CalculatePosition(Width, Height);
64+
}
65+
6366
private void webView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
6467
{
6568
if (IsRunningAsUwp())
@@ -72,9 +75,7 @@ private void webView21_CoreWebView2InitializationCompleted(object sender, CoreWe
7275
Text += " (Unsupported)";
7376
}
7477

75-
// 这里如果直接写 mcm 的话会让启动的时候白屏时间更久
76-
webView21.CoreWebView2.SetVirtualHostNameToFolderMapping("mcm.invalid", Path.Combine(StaticSettings.exeDir, "wwwroot"), CoreWebView2HostResourceAccessKind.Deny);
77-
webView21.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync($"globalThis.backendUrl = `{loopbackUrl.ToString().TrimEnd('/')}`");
78+
SetupCoreWebView2(webView21.CoreWebView2);
7879

7980
// webView21.CoreWebView2.AddWebResourceRequestedFilter("*://mcm.invalid/MaiChartManagerServlet/*", CoreWebView2WebResourceContext.All);
8081
// webView21.CoreWebView2.WebResourceRequested += OnWebResourceRequested;
@@ -83,10 +84,51 @@ private void webView21_CoreWebView2InitializationCompleted(object sender, CoreWe
8384
webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
8485
}
8586

86-
private void CoreWebView2_NewWindowRequested(object? sender, CoreWebView2NewWindowRequestedEventArgs e)
87+
private async void CoreWebView2_NewWindowRequested(object? sender, CoreWebView2NewWindowRequestedEventArgs e)
8788
{
88-
e.Handled = true;
89-
Process.Start(new ProcessStartInfo(e.Uri) { UseShellExecute = true });
89+
if (new Uri(e.Uri).Host != "mcm.invalid")
90+
{
91+
e.Handled = true;
92+
Process.Start(new ProcessStartInfo(e.Uri) { UseShellExecute = true });
93+
return;
94+
}
95+
var deferral = e.GetDeferral();
96+
try
97+
{
98+
var features = e.WindowFeatures;
99+
var dpiScale = DeviceDpi / 96.0;
100+
var width = (int)((features.HasSize ? features.Width : 960) * dpiScale);
101+
var height = (int)((features.HasSize ? features.Height : 640) * dpiScale);
102+
var form = new Form
103+
{
104+
Text = "MaiChartManager",
105+
Icon = Icon,
106+
Width = width,
107+
Height = height,
108+
StartPosition = FormStartPosition.Manual,
109+
Location = CalculatePosition(width, height),
110+
};
111+
var webView = new Microsoft.Web.WebView2.WinForms.WebView2 { Dock = DockStyle.Fill };
112+
form.Controls.Add(webView);
113+
form.Show();
114+
await webView.EnsureCoreWebView2Async(webView21.CoreWebView2.Environment);
115+
SetupCoreWebView2(webView.CoreWebView2);
116+
e.NewWindow = webView.CoreWebView2;
117+
webView.CoreWebView2.DocumentTitleChanged += (_, _) => form.Text = webView.CoreWebView2.DocumentTitle;
118+
form.FormClosed += (_, _) => webView.Dispose();
119+
}
120+
finally
121+
{
122+
deferral.Complete();
123+
}
124+
}
125+
126+
private void SetupCoreWebView2(CoreWebView2 coreWebView2)
127+
{
128+
// 这里如果直接写 mcm 的话会让启动的时候白屏时间更久
129+
// 注意注意,这个东西访问的时候必须要自己手动加 index.html,这个坑已经踩两次了
130+
coreWebView2.SetVirtualHostNameToFolderMapping("mcm.invalid", StaticSettings.wwwroot, CoreWebView2HostResourceAccessKind.Deny);
131+
coreWebView2.AddScriptToExecuteOnDocumentCreatedAsync($"globalThis.backendUrl = `{loopbackUrl.ToString().TrimEnd('/')}`");
90132
}
91133

92134
private async void OnWebResourceRequested(object? sender, CoreWebView2WebResourceRequestedEventArgs args)

‎MaiChartManager/Front/src/views/Charts/MusicEdit/PreviewChartButton.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export default defineComponent({
1818
const height = 640;
1919
const left = (screen.width - width) / 2;
2020
const top = (screen.height - height) / 2;
21-
const url = `/#/chart-preview?${params}`;
21+
const url = new URL(location.href);
22+
url.hash = `/chart-preview?${params}`;
2223
window.open(url, '_blank', `width=${width},height=${height},left=${left},top=${top},menubar=no,toolbar=no,location=no,status=no`);
2324
};
2425

‎MaiChartManager/ServerManager.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public static void StartApp(bool export, Action? onStart = null)
198198
if (export)
199199
app.UseFileServer(new FileServerOptions
200200
{
201-
FileProvider = new PhysicalFileProvider(Path.Combine(StaticSettings.exeDir, "wwwroot")),
201+
FileProvider = new PhysicalFileProvider(StaticSettings.wwwroot),
202202
});
203203
app.MapControllers();
204204
Task.Run(app.Run);

‎MaiChartManager/StaticSettings.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ public partial class StaticSettings
1111
public static readonly string tempPath = Path.Combine(Path.GetTempPath(), "MaiChartManager");
1212
public static readonly string appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MaiChartManager");
1313
public static readonly string exeDir = Path.GetDirectoryName(Application.ExecutablePath);
14+
#if DEBUG
15+
public static readonly string wwwroot = Path.Combine(ProjectDir, "wwwroot");
16+
private static string ProjectDir => Path.GetDirectoryName(GetThisFilePath())!;
17+
private static string GetThisFilePath([System.Runtime.CompilerServices.CallerFilePath] string? path = null) => path!;
18+
#else
19+
public static readonly string wwwroot = Path.Combine(exeDir, "wwwroot");
20+
#endif
1421

1522
private static string _imageAssetsDir = "LocalAssets";
1623
private static string _movieAssetsDir = "LocalAssets";

0 commit comments

Comments
 (0)