RunicWindow<TViewModel> class

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

public abstract class RunicWindow<TViewModel> : RunicView<TViewModel>, IRunicWindow, IRunicView, IAsyncDisposable where TViewModel : class

Inherits or implements RunicView, IRunicWindow, IRunicView, IAsyncDisposable.

The native window and root typed context for one Bridge session. Declare one partial subclass per Window; the same declaration opens on every host and with either MVVM library:

public sealed partial class MainWindow : RunicWindow<MainViewModel>;

Open it with await host.OpenWindowAsync<MainWindow>(), wait with RunicWindow.WaitForCloseAsync, and release it with await using or RunicWindow.DisposeAsync.

Remarks

RunicWindowHostExtensions.OpenWindowAsync creates the Window, resolves its ViewModel in a new DI scope, assigns it to RunicView.DataContext, and opens the window on the host. The generated Bridge and TypeScript client are named after the Window class.

Constructors

RunicWindow

protected RunicWindow()

Creates a window whose ViewModel is assigned when it opens.

Properties

GuardClose

public bool GuardClose { get; init; }

Gets whether a close the user starts, with the title-bar close button, Alt+F4 or the window manager, first asks RunicWindow.CanCloseAsync, so the window can stay open. The default is false.

Remarks

Set it in the Window's constructor; the host reads it when the window opens: public sealed partial class EditorWindow : RunicWindow<EditorViewModel> { public EditorWindow() => GuardClose = true; } The default RunicWindow.CanCloseAsync asks the departure guards of the navigation regions the Window presents, as WPF's NavigationHost.GuardWindowClose does.

Runic Desktop's embedded window asks it; the window closes when it returns true. A presentation that cannot keep its window open, CS-WebUI or a browser, opens unguarded and logs WindowCloseGuardUnavailable (event 1053). RunicWindow.RequestCloseAsync asks it on every host. RunicWindow.CloseAsync, RunicWindow.DisposeAsync and a host shutdown (Ctrl+C, SIGTERM) close without asking: they are the application's own close.

Host

public IBridgeWindow Host { get; }

The host adapter of the open window, such as DesktopBridgeWindow<TViewModel>.

Exceptions

InvalidOperationException

The window has not opened.

Documentation from IRunicWindow.Host.

Presentation

public IRunicWindowPresentation Presentation { get; }

The Window as it appears on the desktop: bring it to the front, minimize, hide or show it, and read whether it is active, from code outside its scope, such as a status icon's handler. Its ViewModel takes the same object as IRunicWindowPresentation.

Remarks

Readable before the Window opens and after it closes; its requests then report PlatformUnavailableReason.OwnerUnavailable or PlatformUnavailableReason.OwnerClosed.

Services

public IServiceProvider Services { get; }

The services of the window's dependency injection scope, which resolved RunicWindow.ViewModel. Scoped services resolved here are the ones the window's ViewModels received.

Exceptions

InvalidOperationException

The window has not started opening.

Remarks

The window's host disposes the scope when the window is released; resolving from it after that throws ObjectDisposedException. A test reads the scoped services of a window it opened:

await using var window = await host.OpenWindowAsync<MainWindow>();
var documents = window.Services.GetRequiredService<DocumentStore>();

ViewModel

public TViewModel ViewModel { get; }

The window's scoped root ViewModel.

Exceptions

InvalidOperationException

The window has not opened.

Methods

CanCloseAsync

protected virtual ValueTask<bool> CanCloseAsync(CancellationToken cancellationToken)

Decides whether a close the user started may proceed, when RunicWindow.GuardClose is true. Return false to keep the window open.

Parameters

cancellationToken

Cancelled when the window closes without asking or the decision is abandoned.

Returns

true to let the window close.

Remarks

The default clears each navigation region the Window's Bridge presents (each NavigationRegion<T> property of its ViewModel), in declaration order, skipping empty ones. A clear asks the departure guard of every entry and of the child regions they own, as any other navigation would. The window closes when every clear commits; a guard that rejects or fails, a cancelled or superseded clear keeps it open with its entries, and the regions after it are not asked. The tabs of a region collection (RunicRegionCollectionAttribute) are removed one by one with NavigationSelection.RemoveAsync, so the tabs that agreed are closed even when a later tab keeps the window open; to ask every tab in one decision, own the tabs by a page entry, whose clear retires them all in one transition. A guard can confirm through a dialog region of the same window, or through a ReactiveUI interaction that a web View of this window answers: the close is the window's own departure, so the one presentation in the window that has mounted a handler for the interaction is asked. Override it to add checks of your own, and call the base method to keep the navigation guards.

It starts in a turn of the window's model context, as a command body does, and one decision runs at a time: another close request while it runs waits for the same decision. It may take as long as a prompt needs. cancellationToken is cancelled when the window closes without asking, for example with RunicWindow.CloseAsync. An exception keeps the window open and is logged as WindowCloseGuardFailed (event 1052).

CloseAsync

public ValueTask<RunicWindowCloseResult> CloseAsync(TimeSpan timeout, CancellationToken cancellationToken = default)

Stops new operation admission and waits up to timeout for accepted work. At the timeout the visible window and its browser routes close promptly, while the host keeps its scope until the remaining operations finish. The first call selects the timeout; later calls return the same result.

Parameters

timeout

How long accepted operations may run before the window closes anyway.

cancellationToken

Stops this caller's wait. The close itself continues, and other callers still receive its result.

Exceptions

ArgumentOutOfRangeException

timeout is negative and not infinite.

OperationCanceledException

cancellationToken was cancelled.

Documentation from IRunicWindow.CloseAsync.

DisposeAsync

public ValueTask DisposeAsync()

Closes the window immediately and waits until its resources are released. Does nothing before it opens.

RequestCloseAsync

public ValueTask<bool> RequestCloseAsync(CancellationToken cancellationToken = default)

Asks to close the window as the user's close button does, like Window.Close() in WPF and Avalonia, and returns whether it closed. With RunicWindow.GuardClose it first asks RunicWindow.CanCloseAsync, and returns false with the window open when that does; without it the window closes.

Parameters

cancellationToken

Stops this caller's wait; the decision continues for other requests.

Returns

true when the window closed; false when it stays open.

Exceptions

InvalidOperationException

The window has not opened.

OperationCanceledException

cancellationToken was cancelled.

Remarks

A request while another request or a user close is being decided waits for the same decision. When the window may close, its visible window closes at once, and accepted operations finish before RunicWindow.DisposeAsync releases its scope, as after a user close. On Runic Desktop's embedded window the request is the native close request, so a DesktopWindowOptions.ConfirmCloseAsync of the host is asked too. On CS-WebUI and in a browser, where the user's close cannot be guarded, this request still is.

Use RunicWindow.CloseAsync to close without asking, for example after the application has already decided. A closed window returns true.

WaitForCloseAsync

public ValueTask WaitForCloseAsync(CancellationToken cancellationToken = default)

Completes when the window closes: the user closed it, or IBridgeWindow.CloseAsync or IAsyncDisposable.DisposeAsync started closing it. It does not release the window; dispose it afterwards.

Parameters

cancellationToken

Stops the wait without closing the window.

Exceptions

OperationCanceledException

cancellationToken was cancelled.

Documentation from IRunicWindow.WaitForCloseAsync.