WpfPlatformServices class

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

public static class WpfPlatformServices

Native platform services for the application's own WPF windows.

Methods

ForDesktopWindowOwner

public static DesktopWindowOwner ForDesktopWindowOwner(Window window)

The owner for a Runic Desktop window that belongs to a WPF window (DesktopWindowOptions.Owner): the Desktop window stays above it, and with DesktopWindowModality.Window the WPF window takes no input until the Desktop window closed, as for Window.ShowDialog.

Parameters

window

The WPF window that owns the Desktop window.

Returns

An owner with the window's HWND.

Exceptions

InvalidOperationException

The caller is not on the window's UI thread.

Remarks

await surface.OpenWindowAsync(new DesktopWindowOptions
{
    Owner = WpfPlatformServices.ForDesktopWindowOwner(mainWindow),
    Modality = DesktopWindowModality.Window,
});

Call it on the window's UI thread. It creates the window's handle if the window has none yet (WindowInteropHelper.EnsureHandle). Runic Desktop does not watch a WPF owner: close the Desktop window before the WPF window closes. The other way round, a WPF dialog that belongs to a Runic Desktop window sets new WindowInteropHelper(dialog).Owner = desktopWindow.NativeHandle before Window.ShowDialog.

ForWindow

public static IWindowPlatformServices ForWindow(Window window)

Creates the platform services of a WPF window: file dialogs, clipboard, launcher, screen-reader announcements, message boxes, taskbar badge and progress, and drags owned by it.

Parameters

window

The window, on its own thread.

Returns

Services for a ViewModel that takes IWindowPlatformServices, as on Runic Desktop, where AddRunicPlatformServices() binds them to each Runic window.

Exceptions

InvalidOperationException

The call is not on the window's thread.

Remarks

Dialogs are modal to window, and every call runs on its dispatcher, so the services can be used from any thread. Until the window has a native handle (after Window.Show or WindowInteropHelper.EnsureHandle) every service reports PlatformUnavailableReason.OwnerUnavailable, as does a window that closed before this call. Once the window closes the services wait for the operations in progress, release the file access they acquired and report PlatformUnavailableReason.OwnerClosed; they need no disposal.

IWindowPlatformServices.AnnounceAsync raises a UI Automation notification from the window's automation peer, which Narrator and NVDA speak: polite messages are queued, assertive ones replace those not yet spoken. While the page of a RunicWebView in the window has the keyboard focus, the page's Bridge says the message in a live region that it adds to the page instead, as Runic Desktop's WebView2 window does: the focus is then in WebView2's browser process, and NVDA speaks a notification only from the focused process. It reports Success once the window raised it or the page received it, also when no screen reader listens.

IWindowPlatformServices.StartDragAsync starts an OLE drag from the window, as Runic Desktop's window does: call it while the user holds the primary button, for example from a command that a RunicViewHost's page sends in its dragstart event.

Register them per window, for example services.AddSingleton(WpfPlatformServices.ForWindow(mainWindow)) on the UI thread; each call creates services with their own lifetime. A RunicViewHost in the window presents a ViewModel that uses them like any other.

ForWindowPresentation

public static IRunicWindowPresentation ForWindowPresentation(Window window, ILoggerFactory? loggerFactory = null)

Gets the IRunicWindowPresentation of a WPF window, for the ViewModels it shows: they bring it to the front, minimize, hide or show it, make it full-screen, switch it between light and dark, and read whether it is active, minimized and visible, as on Runic Desktop.

Parameters

window

The window, on its own thread.

loggerFactory

Logs a handler that throws (event 1130, WindowPresentationHandlerFailed); null writes errors to Trace.

Returns

The window's presentation; the same one for every call with this window. It raises its changes on the window's dispatcher.

Exceptions

InvalidOperationException

The call is not on the window's thread.

Remarks

The presentation belongs to one window. Hand it to that window's ViewModels: a single-window application can register it as a singleton, services.AddSingleton(WpfPlatformServices.ForWindowPresentation(mainWindow)); an application with several windows creates one per window and passes it to the ViewModels of that window, for example through the window's own DI scope or the ViewModel's constructor, never as one singleton for all. Every request runs on the window's dispatcher, so it can come from any thread.

IRunicWindowPresentation.PresentAsync is WpfPlatformServices.PresentAsync; full screen hides the window's frame and maximizes it, and leaving restores both. IRunicWindowPresentation.SetColorSchemeAsync sets the title bar's dark mode, once the window has a handle if it has none yet, and the page's prefers-color-scheme of every RunicViewHost in the window, including one that opens or is still starting. WPF draws its own controls, so the application's WPF theme stays the application's. IRunicWindowPresentation.SetDocumentEditedAsync reports PlatformUnavailableReason.BackendUnavailable, as on Windows generally.

IRunicWindowPresentation.MaximizeAsync and IRunicWindowPresentation.RestoreAsync set Window.WindowState, leaving full screen first. IRunicWindowPresentation.Placement is in WPF's device-independent pixels: Window.RestoreBounds while the window is maximized, minimized or full-screen, else Window.Left, Window.Top and its actual size. IRunicWindowPresentation.SetPlacementAsync sets the bounds the window restores to, also while it is maximized or minimized, then its state; WPF does not fit the bounds on a monitor. Windows activates a window that it maximizes or brings back from minimized.

The activity follows Window.Activated, Window.Deactivated, Window.StateChanged and UIElement.IsVisibleChanged. A handler that throws is logged and does not stop the others, and a change that a handler causes is raised after the handlers of the current one. Once the window closes, requests report PlatformUnavailableReason.OwnerClosed and the activity is RunicWindowActivity.Closed.

PresentAsync

public static ValueTask<PlatformResult<PlatformUnit>> PresentAsync(Window window, CancellationToken cancellationToken = default)

Brings a WPF window to the front, for example for a launch that another copy of the application forwarded.

Parameters

window

The window, on any thread; the work runs on its dispatcher.

cancellationToken

Cancels before the request reaches the window's dispatcher.

Returns

Success when the window took the foreground; PlatformFailureCode.PermissionDenied when Windows kept another window in front and the taskbar button flashes instead; PlatformUnavailableReason.OwnerClosed for a closed window or a dispatcher that shut down.

Remarks

The WPF counterpart of DesktopPlatformServices.PresentAsync for Runic Desktop windows. A hidden window, or one not shown yet, is shown with Window.Show, so WPF's UIElement.Visibility stays current; a minimized one is restored to its previous size. Windows lets a process take the foreground only when the user just interacted with it or the process in the foreground allows it: a launch forwarded by ApplicationInstance.ClaimAsync allows the running instance (AllowSetForegroundWindow), so call this promptly when the launch arrives.