IRunicWindowContext interface

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

public interface IRunicWindowContext

The Window whose DI scope resolved the service, for a ViewModel that closes its own Window. Take it in a constructor and close from a command:

[RelayCommand]
private async Task QuitAsync() => await _window.RequestCloseAsync();

Remarks

RunicWindowServiceCollectionExtensions.AddRunicWindowService registers it per scope. RunicWindowHostExtensions.OpenWindowAsync binds it to the Window it opens before it resolves the Window's ViewModel, on every host, so the ViewModel and the services it shares that scope with need no holder of the Window. IRunicWindowService opens other Windows; this service acts on the caller's own. In tests, register a fake that records the request and returns the answer the test needs.

Properties

Window

IRunicWindow? Window { get; }

The Window of this scope, for example as the RunicWindowOptions.Owner of a dialog it opens, or null outside a Window's scope.

Remarks

It is set before the ViewModel is resolved, so a constructor may keep it, but the Window opens only after the constructor returned. The default implementation, for a test fake, returns null.

Methods

DrainOnClose

IDisposable DrainOnClose(AcceptedWorkScope work)

Makes this Window's close wait for work as for its own operations: the close waits, within its timeout or drain (RunicWindowOptions.CloseDrain), until the work that the scope accepted before the close was decided has finished (AcceptedWorkScope.WaitForAcceptedAsync), and the Window's scope is released only after it. Dispose the result to stop.

Parameters

work

The application work the close waits for.

Returns

Stops this registration when disposed.

Exceptions

ArgumentNullException

work is null.

Remarks

The scope keeps accepting work while the Window is open, and a close that a guard refuses changes nothing. Runic never asks the scope's work to cancel: it counts as RunicOperationClose.Durable. Several Windows may drain one scope, such as an application-wide one; each waits for the work accepted before its own close.

The default implementation, for a test fake, does nothing.

RequestCloseAsync

ValueTask<bool> RequestCloseAsync(CancellationToken cancellationToken = default)

Asks to close this Window as its close button does, and returns whether it closed, as RunicWindow.RequestCloseAsync does: with RunicWindow.GuardClose it first asks CanCloseAsync and the navigation guards, and returns false with the Window open when they keep it.

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 service was resolved outside the scope of a Window that OpenWindowAsync opened or of an attached window, the Window has not opened yet, for example when the ViewModel's constructor calls it, or the attached window closed.

OperationCanceledException

cancellationToken was cancelled.

Remarks

In the scope of a window the application opened itself, which DesktopPlatformServices.AttachWindowServices attached (Runic.Application.Views.Desktop), it asks the window's own close confirmation as its close button does, and IRunicWindowContext.Window stays null.