using Microsoft.AspNetCore.Components.WebView.WindowsForms; using Microsoft.AspNetCore.Components.WebView; using PCC2.AppScaffold; using PCC2.EventBus; using PCC2.Logging; using PCC2.Gui.Events; namespace PCC2.Gui { public partial class Pcc2MainForm : Form { private IAsyncEventBus _eventBus = null!; private bool _isShuttingDown; private bool _isShuttingDownCompleted; public Pcc2MainForm() { InitializeComponent(); Shown += Pcc2MainForm_Shown; FormClosing += Pcc2MainForm_FormClosing; } private async void Pcc2MainForm_Shown(object? sender, EventArgs e) { ApplicationInitializationStatusLabel.Text = "Building host..."; Application.DoEvents(); HostHolder.BuildHost(); _eventBus = HostHolder.EventBus; async Task UiShowEventHandler(UiShownEvent obj) { ApplicationInitializationPanel.Hide(); _eventBus.UnSubscript(UiShowEventHandler); #if DEBUG MainBlazorWebView.WebView.CoreWebView2.OpenDevToolsWindow(); #endif } _eventBus.Subscript((Func)UiShowEventHandler); async Task LocalMethod(LoggingEvent le) { if (ApplicationInitializationPanel.Visible == false) return; if (ApplicationInitializationStatusLabel.InvokeRequired) { Invoke(delegate { ApplicationInitializationStatusLabel.Text = $"{le.LogLevel} {le.Content}"; Application.DoEvents(); }); } else { ApplicationInitializationStatusLabel.Text = $"{le.LogLevel} {le.Content}"; } } HostHolder.EventBus.Subscript(LocalMethod); ApplicationInitializationStatusLabel.Text = "Starting host..."; Application.DoEvents(); await HostHolder.StartAsync(); async void OnUrlLoading(object? o, UrlLoadingEventArgs args) { MainBlazorWebView.UrlLoading -= OnUrlLoading; ApplicationInitializationStatusLabel.Text = "Loading UI..."; Application.DoEvents(); } MainBlazorWebView.UrlLoading += OnUrlLoading; MainBlazorWebView.HostPage = "wwwroot\\index.html"; MainBlazorWebView.Services = HostHolder.ServiceProvider; MainBlazorWebView.RootComponents.Add("#app"); } private async void Pcc2MainForm_FormClosing(object? sender, FormClosingEventArgs e) { if (_isShuttingDownCompleted == false) e.Cancel = true; if (_isShuttingDown) return; _isShuttingDown = true; try { ApplicationInitializationPanel.Show(); ApplicationInitializationTitleLabel.Text = "Application shutting down..."; Application.DoEvents(); await HostHolder.StopAsync(); ApplicationInitializationStatusLabel.Text = "Window will close after 3 second..."; Application.DoEvents(); await Task.Delay(3000); } finally { _isShuttingDownCompleted = true; Close(); } } } }