RunicCsWebUiHost class

Namespace Runic.Application.Views.CsWebUi in Runic.Application.Views.CsWebUi 0.23.0-preview.1

public sealed class RunicCsWebUiHost : IRunicWindowHost

Inherits or implements IRunicWindowHost.

Opens Runic Windows with CS-WebUI, in an installed browser in app mode or the platform WebView. Start it with RunicCsWebUiHost.Run and open Windows with RunicWindowHostExtensions.OpenWindowAsync:

return RunicCsWebUiHost.Run(provider, async host =>
{
    await using var window = await host.OpenWindowAsync<MainWindow>();
    await window.WaitForCloseAsync();
    return 0;
});

Replacing RunicCsWebUiHost with RunicDesktopHost (package Runic.Application.Views.Desktop) runs the same application in a native window.

Remarks

As with RunicDesktopHost, the application runs as long as the body passed to Run, with or without open Windows. CS-WebUI is supported on a best-effort basis. Its windows have no native owner, so platform services report OwnerUnavailable, and it reports closing for all of its windows at once. A RunicCsWebUiHostOptions.UseWebView window on Windows (WebView2) or Linux (WebKitGTK) stays open while a Window with RunicWindow.GuardClose decides the user's close. A browser window, a macOS WebView, a window that RunicCsWebUiHostOptions.ShowWindow shows and serve-only mode cannot keep a window open, so such a Window opens unguarded and logs WindowCloseGuardUnavailable (event 1053). When the RUNIC_APPLICATION_SERVE_ONLY environment variable is 1, no browser is launched: each window starts its local server, writes RUNIC_APPLICATION_URL=<url> to standard output, and keeps serving until a line is read from standard input or the process ends. Test harnesses and remote browsers use this without changing application code. RunicWindowHostExtensions.ValidateWindow reports a CSWEBUI_BROWSER_PATH that names no file (browser-not-found). Whether WebUI finds another browser, the system default browser or the WebView runtime shows only when a window opens, so RunicWindowHostExtensions.OpenWindowAsync reports those failures.

Constructors

RunicCsWebUiHost

public RunicCsWebUiHost(IServiceProvider services)

Creates a host that opens Windows from services.

Remarks

RunicCsWebUiHost.Run creates the host for you and keeps the process main thread in WebUI's wait loop, as CS-WebUI expects. A host created here shows windows on the calling thread, and RunicWindow.WaitForCloseAsync polls WebUiApplication.WaitAsync; call WebUiApplication.Clean after the last window is disposed.

RunicCsWebUiHost

public RunicCsWebUiHost(IServiceProvider services, RunicCsWebUiHostOptions options)

Creates a host that opens Windows from services with options.

Parameters

services

The application's service provider.

options

How the host shows each window.

Remarks

RunicCsWebUiHost.Run creates the host for you and keeps the process main thread in WebUI's wait loop, as CS-WebUI expects. A host created here shows windows on the calling thread, and RunicWindow.WaitForCloseAsync polls WebUiApplication.WaitAsync; call WebUiApplication.Clean after the last window is disposed.

Documentation from RunicCsWebUiHost.constructor.

Fields

OwnerUnsupportedCode

public const string OwnerUnsupportedCode = "window-owner-unsupported"

The code of the warning RunicWindowHostExtensions.ValidateWindow reports for a RunicWindowOptions.Owner: CS-WebUI opens every Window on its own.

Properties

ConfigureWindow

public Action<WebUiWindow>? ConfigureWindow { get; }

Configures each native window before it is shown: RunicCsWebUiHostOptions.ConfigureWindow.

public IReadOnlyList<DesktopMenu> MenuBar { get; }

Gets the menus of each window's menu bar: RunicCsWebUiHostOptions.MenuBar, or those of the last RunicCsWebUiHost.SetMenuBar.

Placement

public Func<IRunicWindow, RunicWindowPlacement?>? Placement { get; }

Chooses the saved placement each Window restores: RunicCsWebUiHostOptions.Placement.

Services

public IServiceProvider Services { get; }

The application's service provider. Each Window opens in a new scope of it.

Documentation from IRunicWindowHost.Services.

ShowWindow

public Action<WebUiWindow, string>? ShowWindow { get; }

Shows each native window in place of the selected presentation: RunicCsWebUiHostOptions.ShowWindow.

UseWebView

public bool UseWebView { get; }

Whether windows open in the platform WebView: RunicCsWebUiHostOptions.UseWebView.

Methods

Run

public static int Run(IServiceProvider services, Func<RunicCsWebUiHost, Task<int>> application)

Runs application while the process main thread waits for WebUI, then releases WebUI's process-wide state with WebUiApplication.Clean.

Parameters

services

The application's service provider.

application

The application. Its result becomes the return value.

Returns

The value returned by application, or 1 when it did not handle a window that could not be shown.

Remarks

Call it from the process entry point and return its result as the exit code. The application runs on the thread pool. The main thread shows the windows the application opens while it is idle, and blocks in WebUiApplication.Wait while windows are shown; when that returns, every shown window's RunicWindow.WaitForCloseAsync completes. Dispose services after this method returns.

A window that WebUI cannot show, for example because no browser is installed, the browser selected with CSWEBUI_BROWSER_PATH or WebUiApplication.SetBrowserPath does not exist or fails to start, or its page does not connect in time, fails RunicWindowHostExtensions.OpenWindowAsync with a RunicWindowConfigurationException. Its diagnostic names the browser, its path and the reason, with a code such as browser-not-found, and WebUI's exception is the inner exception. The failure is logged as event 2100 CsWebUiWindowShowFailed. When the application does not handle it, this method writes the message to standard error and returns 1.

Run

public static int Run(IServiceProvider services, RunicCsWebUiHostOptions options, Func<RunicCsWebUiHost, Task<int>> application)

Runs application with a host that shows each window as options say.

Parameters

services

The application's service provider.

options

How the host shows each window, for example in the platform WebView.

application

The application. Its result becomes the return value.

Returns

The value returned by application, or 1 when it did not handle a window that could not be shown.

Remarks

Call it from the process entry point and return its result as the exit code. The application runs on the thread pool. The main thread shows the windows the application opens while it is idle, and blocks in WebUiApplication.Wait while windows are shown; when that returns, every shown window's RunicWindow.WaitForCloseAsync completes. Dispose services after this method returns.

A window that WebUI cannot show, for example because no browser is installed, the browser selected with CSWEBUI_BROWSER_PATH or WebUiApplication.SetBrowserPath does not exist or fails to start, or its page does not connect in time, fails RunicWindowHostExtensions.OpenWindowAsync with a RunicWindowConfigurationException. Its diagnostic names the browser, its path and the reason, with a code such as browser-not-found, and WebUI's exception is the inner exception. The failure is logged as event 2100 CsWebUiWindowShowFailed. When the application does not handle it, this method writes the message to standard error and returns 1.

Documentation from RunicCsWebUiHost.Run.

SetMenuBar

public void SetMenuBar(IReadOnlyList<DesktopMenu> menus)

Replaces the menus of the menu bar in the page of every Window this host opened, for example with new checked state or titles in a new language.

Parameters

menus

The new menus; empty removes the bar.

Exceptions

ArgumentException

A menu or item is incomplete; see RunicCsWebUiHostOptions.MenuBar.

Remarks

The pages follow at once, and each Window's root ViewModel is asked again for the commands of the new ids. The rules are those of RunicCsWebUiHostOptions.MenuBar.