DesktopSurface class

Namespace Runic.Desktop in Runic.Desktop 0.23.0

public sealed class DesktopSurface : IAsyncDisposable

Inherits or implements IAsyncDisposable.

Owns content, capabilities, sessions, requests, and optional presentations for one namespace.

Properties

ConnectionCount

public int ConnectionCount { get; }

Gets the number of pages connected to this surface.

Remarks

A page counts from its DesktopConnectionEventKind.Connected event until its DesktopConnectionEventKind.Disconnected event, and a handler of either event already sees the count with that change. A window's page, every tab of a served surface and each reload count separately. A reload can disconnect before or after the reloaded page connects, so the count can drop to zero for a moment: to end serving when the last browser tab closes, wait a short time after it reaches zero and check again.

Url

public Uri Url { get; }

Gets the surface URL.

Methods

CloseAsync

public ValueTask CloseAsync(CancellationToken cancellationToken = default)

Closes the surface without stopping a listener shared by other surfaces.

DisposeAsync

public ValueTask DisposeAsync()

Remarks

From a capability or event callback, disposal continues after the callback returns: teardown waits for that callback's session to finish its disconnection event.

Inherits documentation from IAsyncDisposable.DisposeAsync.

ExecuteJavaScriptAsync

public ValueTask<string> ExecuteJavaScriptAsync(string script, CancellationToken cancellationToken = default)

Runs JavaScript and returns its string result from an authenticated presentation session.

Parameters

script

The script; its return value is the result.

cancellationToken

Cancels waiting for the result. To wait for a limited time, pass a token from a CancellationTokenSource created with a delay.

Returns

The whole result.

Exceptions

DesktopException

The result is larger than 16 MiB (DesktopErrorCategory.LimitExceeded, code script-result-too-large).

InvalidOperationException

No authenticated session is connected, or the script failed.

OperationCanceledException

cancellationToken was cancelled.

Remarks

The script runs in one authenticated session. While several are connected, such as browser tabs of a served surface, which one answers is not specified. A result is never cut: one larger than 16 MiB of UTF-8 fails instead. Typical callers are in-process tests of an embedded View and an application asking its page before it closes. The page evaluates the script with the Function constructor, so a DesktopSurfaceOptions.ContentSecurityPolicy without 'unsafe-eval' refuses it, and the call fails with InvalidOperationException.

OpenWindowAsync

public ValueTask<DesktopWindow> OpenWindowAsync(DesktopWindowOptions? options = null, CancellationToken cancellationToken = default)

Opens this surface in one installed browser or embedded WebView.

Remarks

On Windows, a WebView2 window that is not ready within about 1.5 seconds is shown before its page, with a loading indicator, and the WebView takes its place once it exists. A user who closes the window then has closed it: this method returns the window with DesktopWindow.IsOpen false, and no fallback opens.

SubscribeConnectionEvents

public IDisposable SubscribeConnectionEvents(Action<DesktopConnectionEvent> handler)

Observes the pages that connect to this surface and disconnect from it.

Parameters

handler

Called for each event, on a thread-pool thread.

Returns

A subscription; dispose it to stop observing.

Remarks

A page connects once it has authenticated with the surface's session credential. The events of one page run one at a time, DesktopConnectionEventKind.Connected before DesktopConnectionEventKind.Disconnected, and its disconnection follows every call of that page the surface accepted, such as a Bridge call of Runic Windows and Views. Every subscription receives every event, in subscription order. Subscribing does not change how the page navigates or reloads.