DesktopHost class

Namespace Runic.Desktop in Runic.Desktop 0.23.0

public sealed class DesktopHost : IAsyncDisposable

Inherits or implements IAsyncDisposable.

Owns listener resources and a set of isolated presentation surfaces.

Properties

ColorScheme

public DesktopColorScheme ColorScheme { get; }

Gets the application's light or dark appearance, from the last DesktopHost.SetColorSchemeAsync; DesktopColorScheme.NoPreference, the default, follows the system.

Remarks

A window with its own scheme (DesktopWindow.ColorScheme) shows that one instead.

public IReadOnlyList<DesktopMenu> MenuBar { get; }

Gets the menus of the menu bar: DesktopHostOptions.MenuBar, or those of the last DesktopHost.SetMenuBar.

public IDesktopMenuCommandTarget? MenuCommands { get; set; }

Gets or sets the target of the menu commands that no key window runs, such as New or Settings…, also while no window is open; null, the default, for none.

Monitors

public IDesktopMonitors Monitors { get; }

Gets the desktop's monitors, in the units of this host's windows.

Remarks

The built-in hosts list them on Windows, macOS and with GTK 3; on macOS the main thread must run DesktopEventLoop (or the call must come from it), as for windows. With DesktopHostOptions.WindowHostFactory they come from IDesktopWindowHostFactory.Monitors, which reports PlatformUnavailableReason.BackendUnavailable unless the factory provides them, as the GTK 4 factory does. A monitor's DesktopMonitor.WorkArea can be passed to DesktopWindow.MoveAsync as it is, and its DesktopMonitor.Id to DesktopWindow.MoveToMonitorAsync and DesktopWindowOptions.MonitorId.

Example

await foreach (var monitors in host.Monitors.WatchAsync(cancellationToken))
    if (monitors is PlatformResult<ImmutableArray<DesktopMonitor>>.Success { Value: var list }) Show(list);

Port

public int Port { get; }

Gets the bound port, or the configured port before the first surface starts.

ShutdownRequested

public CancellationToken ShutdownRequested { get; }

Gets a token that is cancelled when the application is asked to quit or a termination signal asks it to stop.

Remarks

On macOS, Quit (Command and Q), Quit in the Dock and logging out cancel it once DesktopHostOptions.ConfirmQuitAsync and the windows' close guards agreed, also with no window open. DesktopEventLoop.Run cancels it on Ctrl+C, SIGTERM or a closed Windows console, then closes every open window without close confirmation, so DesktopWindow.WaitForCloseAsync returns and the application shuts down as if the user had closed its last window. Use the token to end application work that does not wait for a window, such as an application that runs behind a status icon, and set DesktopHostOptions.HandlesShutdownWithoutWindow so a signal that arrives while the host has no surface requests shutdown too. See DesktopHostOptions.HandleShutdownSignals.

With windows that drain (DesktopWindowOptions.CloseDrainAsync) it is cancelled after their drains, when every drain returned or DesktopHostOptions.ShutdownDrainTimeout passed, so work the drains wait for can still use it; DesktopHost.ShutdownStarting is cancelled when the shutdown starts. Without such a window both are cancelled together.

ShutdownStarting

public CancellationToken ShutdownStarting { get; }

Gets a token that is cancelled when a shutdown starts, before the windows drain: the first Ctrl+C, SIGTERM, SIGHUP or closed Windows console, a quit once every guard agreed, or the host protocol's shutdown.

Remarks

DesktopHost.ShutdownRequested follows once the windows that drain (DesktopWindowOptions.CloseDrainAsync) have drained or DesktopHostOptions.ShutdownDrainTimeout passed, and together with this token when no window drains. Observe this one to stop work that should end early, such as polling or a background refresh; work that the drains wait for observes DesktopHost.ShutdownRequested or nothing.

SpellCheckingLanguages

public IReadOnlyList<string> SpellCheckingLanguages { get; }

Gets the languages the embedded windows' spell checker checks: DesktopHostOptions.SpellCheckingLanguages, or those of the last DesktopHost.SetSpellCheckingLanguagesAsync; empty for the user's languages.

Methods

AddRecentDocumentAsync

public ValueTask AddRecentDocumentAsync(string path, CancellationToken cancellationToken = default)

Adds a document that the application opened or saved to the system's recent documents: on macOS the Open Recent menu (MacOSDesktopOptions.OpenRecentMenu) and the recent documents in the application's Dock menu.

Parameters

path

The document's fully qualified path.

cancellationToken

Cancels the queued native operation.

Returns

A task that completes when the document was noted.

Exceptions

ArgumentException

path is empty, not fully qualified, or contains a NUL character.

ArgumentNullException

path is null.

ObjectDisposedException

The host is disposed.

OperationCanceledException

cancellationToken was cancelled.

Remarks

On macOS this calls NSDocumentController.noteNewRecentDocumentURL: on the main thread, which DesktopEventLoop serves: the most recent document comes first, a document noted again moves to the top, and the system keeps as many as the user chose in System Settings. Choosing one in Open Recent or the Dock delivers it as an open request (IDesktopOpenRequests of Runic.Platform.MacOS with DesktopOpenOptions.IncludeFiles), as opening it in the Finder does. Note a document after the user opened it and after Save As. AppKit keeps its own bookmark of the file, which needs access to it: in the App Sandbox, note the LocalPath of an open document lease (IDocumentFileLease). The sandbox grants no access to a document chosen in Runic's Open Recent: keep the lease's bookmark (CreateBookmarkAsync) with the path, and when the path arrives as an open request, reopen the document with IDesktopFileBookmarks.OpenDocumentAsync.

Windows: SHAddToRecentDocs, which also fills the application's Jump List. Linux: the freedesktop.org recently-used.xbel that GTK and KDE read. A failure there is not reported; use IDesktopRecentDocuments (DesktopPlatformServices.CreateRecentDocuments of Runic Windows) for the result.

ClearRecentDocumentsAsync

public ValueTask ClearRecentDocumentsAsync(CancellationToken cancellationToken = default)

Empties the application's recent documents, as Clear Menu in Open Recent does.

Parameters

cancellationToken

Cancels the queued native operation.

Returns

A task that completes when the list is empty.

Exceptions

ObjectDisposedException

The host is disposed.

OperationCanceledException

cancellationToken was cancelled.

Remarks

On macOS this calls NSDocumentController.clearRecentDocuments: on the main thread, which DesktopEventLoop serves. Windows: removes the application's Jump List destinations. Linux: removes the application from recently-used.xbel, and the documents only it had.

CreateSurfaceAsync

public ValueTask<DesktopSurface> CreateSurfaceAsync(DesktopSurfaceOptions? options = null, CancellationToken cancellationToken = default)

Creates and starts one isolated surface namespace.

DisposeAsync

public ValueTask DisposeAsync()

Inherits documentation from IAsyncDisposable.DisposeAsync.

GetPresentationPreflight

public DesktopPresentationPreflight GetPresentationPreflight(DesktopWindowOptions? options = null)

Evaluates one requested presentation and its explicit fallbacks without starting a browser or WebView.

Remarks

The presentations come from DesktopPlatform.GetAvailability for this host's options, through DesktopAvailabilityResult.GetPresentation. DesktopPresentationPreflight.OptionDiagnostics reports each window option and permission grant the preferred presentation or its fallback rejects or ignores.

SetColorSchemeAsync

public ValueTask SetColorSchemeAsync(DesktopColorScheme colorScheme, CancellationToken cancellationToken = default)

Switches the application between light and dark while it runs: every open embedded window without a scheme of its own, and the windows that open later without one, show colorScheme.

Parameters

colorScheme

DesktopColorScheme.Light or DesktopColorScheme.Dark, or DesktopColorScheme.NoPreference to follow the system again.

cancellationToken

Cancels waiting for the windows' native threads.

Returns

A task that completes when every open window shows the scheme.

Exceptions

ArgumentOutOfRangeException

colorScheme is not a defined value.

ObjectDisposedException

The host is disposed.

OperationCanceledException

cancellationToken was cancelled.

Remarks

A window shows its own DesktopWindowOptions.ColorScheme, or the one of its last DesktopWindow.SetColorSchemeAsync, when that is not DesktopColorScheme.NoPreference; otherwise it shows this one. The page's prefers-color-scheme and form controls follow, as does the window's frame: on Windows the WebView2 profile's preferred color scheme and the title bar's dark mode; on macOS NSApp.appearance, which menus, alerts and panels follow too, and each window's appearance; on Linux GTK's gtk-application-prefer-dark-theme setting, and the libadwaita style manager when the application has initialized libadwaita.

What stays as it was: installed browsers follow their own theme, and on Linux a GTK theme without a dark variant, or a WebKitGTK that reads only the GTK theme's name, keeps its appearance. With DesktopHostOptions.WindowHostFactory, each window applies the scheme when its host implements IDesktopColorSchemeWindowHost, as the GTK 4 host does.

SetMenuBar

public void SetMenuBar(IReadOnlyList<DesktopMenu> menus)

Replaces the application's menus in every window's menu bar, for example with titles in a new language after a locale change, or with new checked state. The standard menus stay.

Parameters

menus

The new menus; empty removes the application's menus.

Exceptions

ArgumentException

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

Remarks

Each window's bar changes on its UI thread as soon as it can; on macOS that is the main thread, which DesktopEventLoop runs.

SetSpellCheckingLanguagesAsync

public ValueTask SetSpellCheckingLanguagesAsync(IReadOnlyList<string> languages, CancellationToken cancellationToken = default)

Changes the languages the spell checker checks, in every open window and in the windows that open later, for example when the language of the open document changes.

Parameters

languages

BCP 47 language tags, such as de-CH and en; empty for the user's languages.

cancellationToken

Cancels waiting for the windows' native threads.

Returns

A task that completes when every open window has applied the languages.

Exceptions

ArgumentException

A language is not a BCP 47 language tag.

ObjectDisposedException

The host is disposed.

OperationCanceledException

cancellationToken was cancelled.

Remarks

The platforms apply the languages as DesktopHostOptions.SpellCheckingLanguages describes: WebKitGTK and macOS check with them in every window of the process, and WebView2 ignores them. New and edited text is checked with them; text checked before may keep its marks until it is edited. With DesktopHostOptions.WindowHostFactory, each window applies them when its host implements IDesktopSpellCheckingWindowHost, as the GTK 4 host does.

SetStandardMenuTitles

public void SetStandardMenuTitles(DesktopStandardMenuTitles? titles)

Replaces the titles of the standard macOS menus and their items (DesktopHostOptions.StandardMenuTitles) while the application runs, for example in a new language after a locale change.

Parameters

titles

The new titles; null returns to the English ones.

Exceptions

ArgumentException

A title is invalid; see DesktopStandardMenuTitles.

Remarks

The menus Runic added change on the main thread as soon as it can, which DesktopEventLoop runs; a window that opens later uses the new titles too. An application that builds its own main menu keeps its titles, and only the View menu's zoom items, which Runic adds to it, change. Windows and Linux show no standard menus: there the call only checks the titles.

StartAsync

public static ValueTask<DesktopHost> StartAsync(DesktopHostOptions? options = null, CancellationToken cancellationToken = default)

Creates a host. Its listener binds when the first surface starts.

Validate

public DesktopValidationResult Validate(DesktopWindowOptions? options = null)

Checks a window request before it opens: the presentation and its prerequisites, the window options, and the permission grants. Each diagnostic is also logged through DesktopHostOptions.LoggerFactory.

Remarks

Call it at startup and use DesktopValidationResult.ThrowIfInvalid to stop before any window opens. Opening the window repeats the checks it depends on; validation does not start a browser or WebView.

Events

public event EventHandler? MenuBarChanged

Occurs after DesktopHost.SetMenuBar has replaced the menus, so command sources and pages that render the bar can follow them.

ReopenRequested

public event EventHandler<DesktopReopenEventArgs>? ReopenRequested

Occurs when the user asks to reopen the running application: on macOS by clicking its Dock icon or opening it again from Finder (applicationShouldHandleReopen:hasVisibleWindows:).

Remarks

Raised on the thread pool while DesktopEventLoop runs. An application that keeps running without a window, for example after its last window closed, opens or shows its main window here when DesktopReopenEventArgs.HasVisibleWindows is false. AppKit then deminiaturizes a minimized window itself. Other platforms do not raise it; a second launch reaches the running instance as an open request instead. A handler that throws is logged as event 3020, DesktopReopenHandlerFailed; later handlers still run.