public interface IRunicWindowService Opens Windows from ViewModels and application services, on the host that runs the application. Take it in a constructor and open a Window from a command:
[RelayCommand]
private async Task OpenProjectAsync() => await _windows.OpenWindowAsync<ProjectWindow>();Remarks
RunicWindowServiceCollectionExtensions.AddRunicWindowService registers it as a singleton, so ViewModels in every Window's scope and singleton services can take it. It works the same on every host, because it calls RunicWindowHostExtensions.OpenWindowAsync on the host that opened the application's Windows. In tests, register a fake that records the requested Window type instead.
Properties
Windows
IReadOnlyList<IRunicWindow> Windows { get; } The open Windows of the application, in the order they opened: every Window that a host opened with these services, through this service or host.OpenWindowAsync, until its close starts.
Remarks
Each read returns a new list. IRunicWindowService.WindowsChanged reports each change, and RunicWindowServiceExtensions.ObserveWindows observes the list. The default implementation, for a test fake, throws NotSupportedException.
Methods
ActivateAsync
ValueTask<PlatformResult<PlatformUnit>> ActivateAsync(IRunicWindow window, DesktopActivationContext? activation = null, CancellationToken cancellationToken = default) Brings window to the front with IRunicWindowPresentation.PresentAsync: it shows a hidden Window, restores a minimized one and asks for the focus. When a window-modal Window blocks it, that Window is brought forward after it.
Parameters
windowAn open Window.
activationThe desktop's activation data of a launch, or
null.cancellationTokenCancels the request.
Returns
The result of presenting window, such as Unavailable on a host without it.
Remarks
The default implementation presents window alone.
FindOrOpenAsync
ValueTask<TWindow> FindOrOpenAsync<TWindow, TParameter>(object key, TParameter parameter, RunicWindowOptions? options = null, DesktopActivationContext? activation = null, CancellationToken cancellationToken = default) where TWindow : class, IRunicWindow, new() where TParameter : class Brings the open TWindow with key to the front, or opens a new one with that key, as IRunicWindowService.OpenWindowAsync does.
Parameters
keyThe Window's identity, such as a document path; compared with
Object.Equals.parameterThe new Window's parameter, as for
IRunicWindowService.OpenWindowAsync.optionsThe new Window's options.
activationThe desktop's activation data of a launch, passed to the Window's presentation.
cancellationTokenCancels opening or activating.
Returns
The Window that was found or opened.
Remarks
[RelayCommand]
private async Task OpenDocumentAsync(string path) =>
await _windows.FindOrOpenAsync<DocumentWindow, DocumentRequest>(path, new DocumentRequest(path));The key belongs to the Window type: a DocumentWindow and a PreviewWindow for the same path are two Windows. A Window that is found is brought forward with IRunicWindowService.ActivateAsync, which shows a hidden one and restores a minimized one; the parameter and options are then not used. Two calls with the same key at once open one Window. A closed Window is forgotten, so the next call opens a new one. The default implementation, for a test fake, throws NotSupportedException.
FindOrOpenAsync
ValueTask<TWindow> FindOrOpenAsync<TWindow>(object key) where TWindow : class, IRunicWindow, new() Brings the open TWindow with key to the front, or opens a new one without a parameter, as IRunicWindowService.FindOrOpenAsync does.
Parameters
keyThe Window's identity; compared with
Object.Equals.
Returns
The Window that was found or opened.
FindOrOpenAsync
ValueTask<TWindow> FindOrOpenAsync<TWindow>(object key, RunicWindowOptions? options, DesktopActivationContext? activation, CancellationToken cancellationToken) where TWindow : class, IRunicWindow, new() Brings the open TWindow with key to the front, or opens a new one without a parameter, as IRunicWindowService.FindOrOpenAsync does.
Parameters
keyThe Window's identity; compared with
Object.Equals.optionsThe new Window's options, or
null.activationThe desktop's activation data of a launch, or
null.cancellationTokenCancels opening or activating.
Returns
The Window that was found or opened.
Remarks
The default implementation, for a test fake, throws NotSupportedException.
FindWindow
TWindow? FindWindow<TWindow>(object key) where TWindow : class, IRunicWindow The open TWindow that IRunicWindowService.FindOrOpenAsync opened with key, or null.
Parameters
keyThe key, compared with
Object.Equalsamong the Windows of this type.
Remarks
The default implementation, for a test fake, throws NotSupportedException.
OpenForResultAsync
ValueTask<RunicWindowResult<TResult>> OpenForResultAsync<TWindow, TParameter, TResult>(TParameter parameter, RunicWindowOptions? options = null, CancellationToken cancellationToken = default) where TWindow : class, IRunicWindow, new() where TParameter : class Opens TWindow with parameter, as IRunicWindowService.OpenWindowAsync does, and returns its result once it closed.
Parameters
parameterThe value the new Window's scope receives.
optionsThe Window's options, such as its owner and modality.
cancellationTokenCancels opening, or closes the open Window.
Returns
The result, or none (RunicWindowResult.HasValue is false).
Remarks
var chosen = await _windows.OpenForResultAsync<ChooseAccountWindow, AccountQuery, Account>(query,
new RunicWindowOptions { Owner = _window.Window, Modality = RunicWindowModality.Window });
if (chosen.TryGetValue(out var account)) Use(account);The Window's ViewModel takes IRunicWindowResult and completes it, which closes the Window. A Window that closes without a result, by the user or the application, returns none (RunicWindowResult.HasValue is false). Give it an owner and RunicWindowModality.Window for a dialog. Cancelling cancellationToken while the Window is open closes it without asking and throws OperationCanceledException. The default implementation, for a test fake, throws NotSupportedException.
OpenForResultAsync
ValueTask<RunicWindowResult<TResult>> OpenForResultAsync<TWindow, TResult>(RunicWindowOptions? options, CancellationToken cancellationToken) where TWindow : class, IRunicWindow, new() Opens TWindow without a parameter and returns its result once it closed, as IRunicWindowService.OpenForResultAsync does.
Parameters
optionsThe Window's options, such as its owner and modality.
cancellationTokenCancels opening, or closes the open Window.
Returns
The result, or none (RunicWindowResult.HasValue is false).
Remarks
The default implementation, for a test fake, throws NotSupportedException.
OpenForResultAsync
ValueTask<RunicWindowResult<TResult>> OpenForResultAsync<TWindow, TResult>() where TWindow : class, IRunicWindow, new() Opens TWindow without a parameter or options and returns its result once it closed, as IRunicWindowService.OpenForResultAsync does.
Returns
The result, or none.
OpenWindowAsync
ValueTask<TWindow> OpenWindowAsync<TWindow>(RunicWindowOptions? options = null, CancellationToken cancellationToken = default) where TWindow : class, IRunicWindow, new() Creates TWindow, resolves its ViewModel in a new DI scope, and opens it, as RunicWindowHostExtensions.OpenWindowAsync does.
Parameters
optionsThe content and size;
nullopenswww/index.htmlnext to the application.cancellationTokenCancels opening. A cancelled open releases everything it created.
Returns
The open Window. The service releases it once it closes; awaiting IRunicWindow.WaitForCloseAsync or disposing it earlier is optional.
Exceptions
InvalidOperationExceptionNo host has opened a Window with these services yet.
RunicWindowConfigurationExceptionA check failed before anything opened.
OpenWindowAsync
ValueTask<TWindow> OpenWindowAsync<TWindow, TParameter>(TParameter parameter, RunicWindowOptions? options = null, CancellationToken cancellationToken = default) where TWindow : class, IRunicWindow, new() where TParameter : class Opens TWindow as IRunicWindowService.OpenWindowAsync does, and hands parameter to the new Window's scope, where its ViewModel takes IRunicWindowParameter, as RunicWindowHostExtensions.OpenWindowAsync does.
Parameters
parameterThe value the new Window's scope receives.
optionsThe content and size;
nullopenswww/index.htmlnext to the application.cancellationTokenCancels opening. A cancelled open releases everything it created.
Returns
The open Window, released by the service once it closes.
Exceptions
InvalidOperationExceptionNo host has opened a Window with these services yet.
RunicWindowConfigurationExceptionA check failed before anything opened.
Remarks
await _windows.OpenWindowAsync<PartWindow, PartRequest>(new PartRequest(world, part));
public sealed class PartViewModel(IRunicWindowParameter<PartRequest> request) { ... }The default implementation, for a test fake that does not override it, throws NotSupportedException.
Events
WindowsChanged
event EventHandler<RunicWindowsChangedEventArgs>? WindowsChanged Occurs after a Window opened or its close started, so IRunicWindowService.Windows changed.
Remarks
The changes are raised one at a time and in order, on a thread-pool thread, each with its own RunicWindowsChangedEventArgs.Windows list. They are not raised in any Window's model context: a ViewModel, which belongs to its own Window, posts what it does to its model context, or observes the list with RunicWindowServiceExtensions.ObserveWindows, which does that. A handler that throws is logged (event 1181) and does not stop the others. The default implementation, for a test fake, does nothing.