IRunicHostedWindowPolicy interface

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

public interface IRunicHostedWindowPolicy

A host's rules for what a guest application may do with the windows the host shows for it (the Runic host protocol, runic-host/1): which windows open, with which native options, which presentation requests apply, which menus and dialogs the guest shows, and which window services it uses.

Remarks

Every member has a default, and IRunicHostedWindowPolicy.Limits the protocol's default limits, so a policy implements only what it changes. The defaults allow windows, presentation, menus and dialogs unchanged; window services follow RunicHostedServiceRequest.IsAllowedByDefault, which keeps the clipboard's content and other applications from a guest the user is not working in. A later release can add members for later protocol features, with defaults that keep this release's behaviour. A policy's members can run concurrently, for different windows and requests.

sealed class OneWindowPolicy : IRunicHostedWindowPolicy
{
    public ValueTask<RunicHostedWindowDecision> OpenWindowAsync(RunicHostedWindowRequest request, CancellationToken cancellationToken) =>
        ValueTask.FromResult(request.Application.Windows.Count == 0
            ? RunicHostedWindowDecision.Allow(request.WindowOptions with { Placement = SavedPlacement(request) })
            : RunicHostedWindowDecision.Refuse("This host shows one window of each tool."));

    public RunicHostedPresentationDecision Present(RunicHostedPresentationRequest request) =>
        request.Kind == RunicHostedPresentationKind.SetFullScreen ? RunicHostedPresentationDecision.Refuse() : RunicHostedPresentationDecision.Allow();
}

Experimental; see docs/experimental.md.

Properties

Limits

RunicHostedLimits Limits { get; }

The limits of the guest's menu, dialog and window service requests (menus/1, dialogs/1, window-services/1); the host reads them once, when the guest started. The default is RunicHostedLimits.Default.

Methods

DialogAsync

ValueTask<RunicHostedDialogDecision> DialogAsync(RunicHostedDialogRequest request, CancellationToken cancellationToken)

Decides a file dialog or message box a guest asks to show over one of its windows (dialogs/1). The default shows it unchanged.

Parameters

request

The dialog.

cancellationToken

Cancelled when the guest gives the dialog up, the window closes or the guest goes away.

Returns

The decision: show it, show a changed dialog of the same kind (RunicHostedDialogDecision.Apply), such as with another initial directory, or refuse it, which the guest receives as PlatformFailureCode.PermissionDenied.

Remarks

A policy that throws fails the guest's dialog as failed. The host shows the dialog owned by the guest's window, but its title, texts and button labels are the guest's, and nothing marks them as the guest's: a guest can word a message box as the host application's. A policy that does not trust a guest can mark its dialogs, or refuse its message boxes:

public ValueTask<RunicHostedDialogDecision> DialogAsync(RunicHostedDialogRequest request, CancellationToken cancellationToken) =>
    ValueTask.FromResult(request.Kind == RunicHostedDialogKind.MessageBox
        ? RunicHostedDialogDecision.Apply(request with { MessageBox = request.MessageBox! with { Title = $"{request.Window.Application.Guest.Name}: {request.MessageBox.Title}" } })
        : RunicHostedDialogDecision.Apply(request with { Title = $"{request.Window.Application.Guest.Name}: {request.Title}" }));
RunicHostedMenuDecision Menu(RunicHostedMenuRequest request)

Decides the menus a guest shows in one of its windows (menus/1): its menu bar, each time it or an item's state changes, and each context menu. The default shows them unchanged.

Parameters

request

The menus as the host would show them.

Returns

The decision: show them, show changed menus (RunicHostedMenuDecision.Apply), such as without an item or with an item of the host's own, or refuse them, which the guest receives as PlatformFailureCode.PermissionDenied; a refused menu bar shows no menus of the guest.

Remarks

Called on any thread; for a bar each time the host rebuilds the window's native bar, at most once per 50 milliseconds, after any number of changes. Keep it cheap. Menu titles are the guest's texts: a policy that does not trust a guest can refuse its bar, or show it changed, such as with each menu's title marked as the guest's.

OpenWindowAsync

ValueTask<RunicHostedWindowDecision> OpenWindowAsync(RunicHostedWindowRequest request, CancellationToken cancellationToken)

Decides whether a window the guest asks for opens, and with which options. The default opens it with RunicHostedWindowRequest.WindowOptions.

Parameters

request

The guest's request and the options the host would open the window with.

cancellationToken

Cancelled when the guest goes away before the decision.

Returns

The decision. A refusal fails the guest's open with hosted-window-refused.

Remarks

A policy that throws refuses the window as failed; the guest's open fails with hosted-window-failed.

Present

RunicHostedPresentationDecision Present(RunicHostedPresentationRequest request)

Decides one presentation request of the guest for an open window, such as presenting it, entering full screen or resizing it. The default applies it unchanged.

Parameters

request

The request.

Returns

The decision: apply it, apply a changed request (a clamp, such as a smaller size), or refuse it, which the guest receives as PlatformFailureCode.PermissionDenied.

ServiceAsync

ValueTask<RunicHostedServiceDecision> ServiceAsync(RunicHostedServiceRequest request, CancellationToken cancellationToken)

Decides a window service a guest uses for one of its windows (window-services/1): a clipboard read or write, the monitors, opening a file or link, or files the user dropped onto the window. The default allows what RunicHostedServiceRequest.IsAllowedByDefault allows.

Parameters

request

The request.

cancellationToken

Cancelled when the window closes or the guest goes away.

Returns

The decision: allow it, allow a changed request (RunicHostedServiceDecision.Apply), such as revealing a file instead of opening it or only some of the dropped files, or refuse it, which the guest receives as PlatformFailureCode.PermissionDenied; a refused drop does not reach the guest.

Remarks

The host serves each request with its own rights: its clipboard, its monitors, and its activation rights to open a file or link. By default a guest reads and writes the clipboard only while the user works in the window (it is active, not because the guest asked: RunicHostedServiceRequest.IsGuestActivated), so a guest the user is not working in cannot read what the user copies elsewhere or replace it, and opens only web and mail links and the files the user gave it through the host, from the host's open dialogs or a drop, while the user works in the window. Widen it with care: a guest that may open any file can start any program the desktop runs for a file type.

// Let a trusted tool write the clipboard from any of its windows, and paste only as plain text.
public ValueTask<RunicHostedServiceDecision> ServiceAsync(RunicHostedServiceRequest request, CancellationToken cancellationToken) =>
    ValueTask.FromResult(request.Kind switch
    {
        RunicHostedServiceKind.ClipboardWrite => RunicHostedServiceDecision.Apply(request with { ClipboardFormats = DesktopClipboardFormats.Text }),
        _ => request.IsAllowedByDefault ? RunicHostedServiceDecision.Allow() : RunicHostedServiceDecision.Refuse(),
    });

A policy that throws fails the guest's request as failed, and drops the files of a drop.