RunicDesktopHost class

Namespace Runic.Application.Views.Desktop in Runic.Application.Views.Desktop 0.23.0

public sealed class RunicDesktopHost : IRunicWindowHost

Inherits or implements IRunicWindowHost.

Opens Runic Windows in native Runic Desktop windows with the embedded WebView, falling back to an installed browser. Start it with RunicDesktopHost.Run and open Windows with RunicWindowHostExtensions.OpenWindowAsync:

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

Replacing RunicDesktopHost with RunicCsWebUiHost (package Runic.Application.Views.CsWebUi) runs the same application on CS-WebUI.

Remarks

The application runs as long as the body passed to Run: closing the last Window does not end it, and a body may run without any Window, for example behind a status icon. To keep one copy running, claim ApplicationInstance.ClaimAsync before Run and bring an open Window forward with DesktopPlatformServices.PresentAsync when another launch arrives, or open one for it with RunicDesktopHost.OpenWindowAsync. When RunicDesktopHostOptions.ServeOnly is set or the RUNIC_APPLICATION_SERVE_ONLY environment variable is 1, as on CS-WebUI, no window or browser is opened, and Run starts no window toolkit loop: each window creates its surface, writes RUNIC_APPLICATION_URL=<url> to standard output, and keeps serving until a line is read from standard input, shutdown is requested or the process ends; then RunicWindow.WaitForCloseAsync returns. A line ends every Window serving when it is read; a Window opened after it serves until the next line. The surface admits one browser, a guarded Window opens unguarded (event 1053) and platform services, including DesktopPlatformServices.PresentAsync, report OwnerUnavailable. This switch takes precedence over RUNIC_APPLICATION_CLOSE_AFTER_OPEN. RunicWindowHostExtensions.ValidateWindow makes the same decision: it skips the findings of DesktopHost.Validate about what this machine lacks to present a window, such as graphical-session-missing, because no presentation opens, and still reports configuration errors such as window-option-invalid. When the RUNIC_APPLICATION_CLOSE_AFTER_OPEN environment variable is 1, every window opened through this host is closed as soon as it has opened (and, with DesktopHostOptions.WaitForConnection, its bridge has connected). Before closing, a warning is written to standard error (and logged as event 2002 when an ILoggerFactory is registered) and RUNIC_APPLICATION_OPENED=<presentation>, such as Embedded or Chrome, is written to standard output. It is an automation switch for applications shaped like the runic-app template, which exit when their one window's RunicWindow.WaitForCloseAsync returns; other applications may keep running or reopen windows. When dotnet runic dev runs without a development server, it sets RUNIC_APPLICATION_DEVELOPMENT_ASSETS to the frontend output directory. In a Development environment, and when RunicDesktopHost.SurfaceOptions is not set, every window serves that directory before its content root and reloads when the directory changes.

Constructors

RunicDesktopHost

public RunicDesktopHost(IServiceProvider services, DesktopHost desktop)

Creates a host that opens Windows from services on desktop.

Remarks

RunicDesktopHost.Run creates the host for you. Use this constructor inside DesktopEventLoop.Run or with a DesktopHost you started yourself, for example for a second host on the same Desktop host.

RunicDesktopHost

public RunicDesktopHost(IServiceProvider services, DesktopHost desktop, RunicDesktopHostOptions options)

Creates a host that opens Windows from services on desktop with options.

Parameters

services

The application's service provider.

desktop

The Desktop host that presents the windows.

options

The window, surface, resource and placement options of this host's Windows.

Exceptions

ArgumentException

RunicDesktopHostOptions.Guest is set: only RunicDesktopHost.Run starts hosted mode.

Remarks

RunicDesktopHost.Run creates the host for you. Use this constructor inside DesktopEventLoop.Run or with a DesktopHost you started yourself, for example for a second host on the same Desktop host.

Documentation from RunicDesktopHost.constructor.

Properties

DefaultWindowOptions

public static DesktopWindowOptions DefaultWindowOptions { get; }

The window options RunicDesktopHost.WindowOptions starts from: the embedded WebView, falling back to an installed browser (DesktopPresentationPolicy.EmbeddedThenBrowser).

Desktop

public DesktopHost Desktop { get; }

The Runic Desktop host that presents the windows.

Guest

public RunicGuestSession? Guest { get; }

The host channel in hosted mode, with the host's name and version; null when the application shows its own windows.

HostedApplications

public IRunicHostedApplications HostedApplications { get; }

Starts other Runic applications as guests and shows their Windows in native windows of this host (the Runic host protocol, runic-host/1, in window mode).

Remarks

The guests still running are stopped when RunicDesktopHost.Desktop shuts down and when RunicDesktopHost.Run returns. A hosted or serve-only host cannot start guests. Experimental; see docs/experimental.md.

IsHosted

public bool IsHosted { get; }

Whether this host runs in hosted mode (RunicDesktopHostOptions.Guest): another Runic application started it and shows its Windows in its own windows.

Placement

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

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

Resources

public Func<IRunicWindow, ContentHandler?>? Resources { get; }

Chooses the resources of each Window: RunicDesktopHostOptions.Resources.

ServeOnly

public bool ServeOnly { get; }

Whether this host serves its Windows to a browser instead of showing them: RunicDesktopHostOptions.ServeOnly, or RUNIC_APPLICATION_SERVE_ONLY=1, read when a Window opens.

Remarks

Hosted mode (RunicDesktopHostOptions.Guest) is not serve-only.

Services

public IServiceProvider Services { get; }

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

Documentation from IRunicWindowHost.Services.

SurfaceOptions

public DesktopSurfaceOptions? SurfaceOptions { get; }

The surface options of every window: RunicDesktopHostOptions.SurfaceOptions.

WindowOptions

public DesktopWindowOptions WindowOptions { get; }

The Desktop options of every window: RunicDesktopHostOptions.WindowOptions.

Remarks

RunicWindowOptions.Width and RunicWindowOptions.Height replace their width and height when set.

Methods

GetSurface

public static DesktopSurface? GetSurface(IRunicWindow window)

Gets the Desktop surface that serves an open Window of this host, or null for another host.

Parameters

window

The open Window.

Exceptions

InvalidOperationException

The Window has not opened.

Remarks

The surface is the same in a native window, in a browser after a fallback and under RUNIC_APPLICATION_SERVE_ONLY. Its DesktopSurface.ConnectionCount and DesktopSurface.SubscribeConnectionEvents tell when the last browser tab of a served Window closed, and DesktopSurface.Url is its address. It is the DesktopBridgeWindow.Surface of IRunicWindow.Host, without naming the ViewModel type.

OpenWindowAsync

public ValueTask<TWindow> OpenWindowAsync<TWindow>(RunicWindowOptions? options, DesktopActivationContext? activation, CancellationToken cancellationToken = default) where TWindow : class, IRunicWindow, new()

Opens TWindow as RunicWindowHostExtensions.OpenWindowAsync does, and asks the desktop to give the new window focus with activation.

Parameters

options

The content and size; null opens www/index.html next to the application.

activation

The desktop's short-lived activation data. Use it once, promptly.

cancellationToken

Cancels opening. A cancelled open releases everything it created.

Returns

The open Window.

Exceptions

RunicWindowConfigurationException

A check failed before anything opened.

Remarks

Open a Window for a launch with the launch's activation context, such as ApplicationLaunch.PlatformContext of a launch the running instance received. Wayland compositors such as KWin give a new window focus only with the activation token of the launch it answers; without it, the window opens behind the active one and only asks for attention. The window is presented with the context as soon as it is shown, as DesktopPlatformServices.PresentAsync does; the desktop decides whether to grant focus. Nothing is presented for a null context, a hidden window (DesktopWindowOptions.Hidden), a window served under RUNIC_APPLICATION_SERVE_ONLY or an installed browser after an embedded-window fallback. The activation follows the options, so a call with one argument still opens through RunicWindowHostExtensions.OpenWindowAsync.

Run

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

Runs application with the default DesktopHostOptions.

Parameters

services

The application's service provider.

options

The Desktop host options, for example new DesktopHostOptions().WithGtk4().

hostOptions

The window, surface, resource and placement options of the host's Windows.

application

The application. Its result becomes the return value.

Returns

The value returned by application.

Remarks

Call it from the process entry point before any await and return its result as the exit code; see DesktopEventLoop.Run. Ctrl+C and SIGTERM close the open windows, so RunicWindow.WaitForCloseAsync returns, and cancel DesktopHost.ShutdownRequested. A body that runs without a window, for example behind a status icon, sets DesktopHostOptions.HandlesShutdownWithoutWindow and ends when host.Desktop.ShutdownRequested is cancelled; without that option, a signal that arrives while no Window is open or served ends the process with its default action. Dispose services after this method returns. When DesktopHostOptions.LoggerFactory is not set, the host logs through the ILoggerFactory registered in services, if any, until this method returns; a host message after that, such as a browser profile that still cannot be deleted, goes to Trace.

Documentation from RunicDesktopHost.Run.

Run

public static int Run(IServiceProvider services, DesktopHostOptions options, Func<RunicDesktopHost, Task<int>> application)

Runs application with the default RunicDesktopHostOptions.

Parameters

services

The application's service provider.

options

The Desktop host options, for example new DesktopHostOptions().WithGtk4().

hostOptions

The window, surface, resource and placement options of the host's Windows.

application

The application. Its result becomes the return value.

Returns

The value returned by application.

Remarks

Call it from the process entry point before any await and return its result as the exit code; see DesktopEventLoop.Run. Ctrl+C and SIGTERM close the open windows, so RunicWindow.WaitForCloseAsync returns, and cancel DesktopHost.ShutdownRequested. A body that runs without a window, for example behind a status icon, sets DesktopHostOptions.HandlesShutdownWithoutWindow and ends when host.Desktop.ShutdownRequested is cancelled; without that option, a signal that arrives while no Window is open or served ends the process with its default action. Dispose services after this method returns. When DesktopHostOptions.LoggerFactory is not set, the host logs through the ILoggerFactory registered in services, if any, until this method returns; a host message after that, such as a browser profile that still cannot be deleted, goes to Trace.

Documentation from RunicDesktopHost.Run.

Run

public static int Run(IServiceProvider services, DesktopHostOptions options, RunicDesktopHostOptions hostOptions, Func<RunicDesktopHost, Task<int>> application)

Starts a Desktop host with options and runs application on the event loop this platform and backend need.

Parameters

services

The application's service provider.

options

The Desktop host options, for example new DesktopHostOptions().WithGtk4().

hostOptions

The window, surface, resource and placement options of the host's Windows.

application

The application. Its result becomes the return value.

Returns

The value returned by application.

Remarks

Call it from the process entry point before any await and return its result as the exit code; see DesktopEventLoop.Run. Ctrl+C and SIGTERM close the open windows, so RunicWindow.WaitForCloseAsync returns, and cancel DesktopHost.ShutdownRequested. A body that runs without a window, for example behind a status icon, sets DesktopHostOptions.HandlesShutdownWithoutWindow and ends when host.Desktop.ShutdownRequested is cancelled; without that option, a signal that arrives while no Window is open or served ends the process with its default action. Dispose services after this method returns. When DesktopHostOptions.LoggerFactory is not set, the host logs through the ILoggerFactory registered in services, if any, until this method returns; a host message after that, such as a browser profile that still cannot be deleted, goes to Trace.

UseApplicationMenu

public IDisposable UseApplicationMenu(IReadOnlyDictionary<string, ICommand> commands)

Runs the application's menu bar commands by command id, such as New or Settings…, when no key window's root ViewModel (IRunicMenuCommandSource) runs the id, until the returned object is disposed.

Parameters

commands

The commands by command id of DesktopHost.MenuBar.

Returns

Disposing it stops running the commands and releases their CanExecuteChanged subscriptions.

Exceptions

InvalidOperationException

RunicDesktopHost.Desktop already has application menu commands, from another call that is not disposed yet or set directly on DesktopHost.MenuCommands.

Remarks

The commands are application-wide: they become DesktopHost.MenuCommands of RunicDesktopHost.Desktop, which every host on that Desktop host shares. Each command's availability follows its ICommand.CanExecute with a null parameter, and it runs on the thread pool: an application command has no ViewModel turn. A failure is logged (event 2004).

return RunicDesktopHost.Run(provider, new DesktopHostOptions { MenuBar = menus }, async host =>
{
    using var menu = host.UseApplicationMenu(new Dictionary<string, ICommand> { ["file.new"] = newWindowCommand });
    await using var window = await host.OpenWindowAsync<MainWindow>();
    await window.WaitForCloseAsync();
    return 0;
});