DesktopWindow class

Namespace Runic.Desktop in Runic.Desktop 0.23.0

public sealed class DesktopWindow : IAsyncDisposable

Inherits or implements IAsyncDisposable.

Represents one installed-browser or embedded-WebView presentation of a surface.

Properties

Activity

public DesktopWindowActivity? Activity { get; }

Gets whether the window is active, minimized and visible, as the window last reported it; null when the presentation reports none.

Remarks

An embedded window whose DesktopWindow.Capabilities include DesktopWindowCapabilities.Activity reports it when it opens and whenever it gains or loses the keyboard focus, is minimized or restored, and is shown or hidden. Every built-in embedded window does; an installed browser and a custom window host that does not implement IDesktopActivityWindowHost report none. Reading it is cheap and does not reach the native window. After the window closed it reports the window inactive and not visible.

Runic Windows read it through IRunicWindowPresentation (Runic.Application.Views), which a ViewModel takes in its constructor on every host.

Browser

public BrowserKind Browser { get; }

Gets the presentation that opened.

Remarks

For a BrowserKind.Any or BrowserKind.ChromiumBased request this is the concrete browser that was launched, or BrowserKind.Any when it could not be identified.

Capabilities

public DesktopWindowCapabilities Capabilities { get; }

Gets the platform-window operations the open window supports.

Remarks

Only embedded windows report capabilities; installed browsers and closed windows report DesktopWindowCapabilities.None.

ColorScheme

public DesktopColorScheme ColorScheme { get; }

Gets the light or dark appearance the window shows now, as Runic set it: its own scheme (DesktopWindowOptions.ColorScheme or its last DesktopWindow.SetColorSchemeAsync), else the application's (DesktopHost.ColorScheme); DesktopColorScheme.NoPreference when it follows the system.

Remarks

Without DesktopWindowCapabilities.PerWindowColorScheme the scheme is shared, by every GTK window of the process or by the windows of one WebView2 profile, and this is the last scheme set for all of them, whichever window or the application set it. An installed browser reports DesktopColorScheme.NoPreference.

IsFallback

public bool IsFallback { get; }

Gets whether the first presentation, such as the embedded WebView, could not open and one of its fallbacks (DesktopWindowOptions.Fallbacks, DesktopPresentationPolicy.EmbeddedThenBrowser) opened instead; DesktopWindow.Browser names it.

IsOpen

public bool IsOpen { get; }

Gets whether this is the surface's current window and its browser process or embedded window is still open.

LayoutDirection

public DesktopLayoutDirection LayoutDirection { get; }

Gets the direction of the window's native parts: DesktopWindowOptions.LayoutDirection, or the one of its last DesktopWindow.SetLayoutDirectionAsync. DesktopLayoutDirection.Default keeps the platform's.

public DesktopMenuBarPlacement MenuBarPlacement { get; }

Gets where the window shows the menu bar of DesktopHostOptions.MenuBar now.

Remarks

DesktopMenuBarPlacement.Global on macOS, where the application menu bar holds the declared menus for every presentation; DesktopMenuBarPlacement.Window for a bar in the window, and on Linux DesktopMenuBarPlacement.Global while the desktop's global menu shows it. A window with DesktopMenuBarPresentation.Page elsewhere reports DesktopMenuBarPlacement.Page once its host supports the in-page bar, and DesktopMenuBarPlacement.None before that. An installed browser, a window host without DesktopWindowCapabilities.MenuBar, the DesktopMenuBarPresentation.None presentation and a closed window report DesktopMenuBarPlacement.None.

A page that renders the bar itself does so only while the placement is DesktopMenuBarPlacement.Page.

public IDesktopMenuCommandTarget? MenuCommands { get; set; }

Gets or sets the target of the menu bar's commands while this window is the key window; null, the default, leaves them to DesktopHost.MenuCommands.

Remarks

An item of DesktopHostOptions.MenuBar asks the window's target first: on macOS the key window's. It is enabled while IDesktopMenuCommandTarget.CanExecute returns true and runs IDesktopMenuCommandTarget.Execute when chosen, when its shortcut is pressed, or through DesktopWindow.TryExecuteMenuItemAsync. Runic Windows set it for a root ViewModel that implements IRunicMenuCommandSource.

Modality

public DesktopWindowModality Modality { get; }

Gets whether this window blocks its DesktopWindow.Owner (DesktopWindowOptions.Modality); DesktopWindowModality.None without an owner.

NativeHandle

public nint NativeHandle { get; }

Gets the native top-level handle of an open embedded window, or zero for an installed browser or closed window.

Remarks

The handle kind depends on the window host:

  • Windows, WebView2 – The top-level window's HWND.
  • macOS, WKWebView – An NSWindow*.
  • Linux, GTK 3 and WebKitGTK 4.1 – A GTK 3 GtkWindow*.
  • Linux, GTK 4 (Runic.Desktop.Gtk4) – A GTK 4 GtkWindow*.
  • A custom window host – Whatever its IDesktopWindowHost.NativeHandle reports.

Reading the value on another thread is for identity only, such as comparing windows. AppKit and GTK objects may be used only on their UI thread: pass any call on the handle to DesktopWindow.InvokeNativeAsync, which runs it there with the handle. The handle is invalid once the window closes.

OwnedWindows

public IReadOnlyList<DesktopWindow> OwnedWindows { get; }

Gets the open windows that this window owns, in the order they opened.

Remarks

A window owned through DesktopWindowOwner.FromWindow is listed from its open until it closes, so not once DesktopWindow.WaitForCloseAsync returned for it. Closing this window closes them without asking.

Owner

public DesktopWindowOwner? Owner { get; }

Gets the owner this window opened with (DesktopWindowOptions.Owner), or null for a window of its own, including one whose owner was ignored.

ProcessId

public int ProcessId { get; }

Gets the installed browser's process identifier, or zero for an embedded or closed window.

RequestedBrowser

public BrowserKind RequestedBrowser { get; }

Gets the presentation requested by DesktopWindowOptions.Browser.

SidePanel

public DesktopSidePanel? SidePanel { get; }

Gets the open side panel, or null.

Remarks

Set when DesktopWindow.OpenSidePanelAsync opened one, and null again once it closed, before its DesktopSidePanel.Closed event.

State

public DesktopWindowState? State { get; }

Gets the window's bounds, maximized and full-screen state as the window last reported them, also after it has closed; null when the presentation reports none.

Remarks

An embedded window whose DesktopWindow.Capabilities include DesktopWindowCapabilities.State reports its state when it opens and after every move, resize, maximize, restore and full-screen change. An installed browser and a custom window host that does not implement IDesktopWindowStateHost report none. Reading it is cheap and does not reach the native window.

The value stays readable after the window has closed and is then the last state before closing, so an application saves DesktopWindowState.Placement after DesktopWindow.WaitForCloseAsync returns and restores it with DesktopWindowOptions.Placement. The units differ per platform; see DesktopWindowState.

SupportsNativeDispatch

public bool SupportsNativeDispatch { get; }

Whether this live embedded owner supports native-thread callbacks.

Methods

AnnounceAsync

public ValueTask AnnounceAsync(string message, DesktopAnnouncementPriority priority = DesktopAnnouncementPriority.Polite, CancellationToken cancellationToken = default)

Asks the screen reader to announce a message, such as the result of an action.

Parameters

message

The text to speak, in the page's language.

priority

DesktopAnnouncementPriority.Polite, the default, waits for what the screen reader is saying; DesktopAnnouncementPriority.Assertive interrupts it.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the message has been passed to the platform's accessibility API.

Exceptions

ArgumentException

message is empty or white space.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Announcements, which the embedded window reports on every platform. It is the native counterpart of an ARIA live region, which WebKitGTK does not report to Orca: on Linux the window's accessible object emits the AT-SPI Announcement event (ATK's notification signal with GTK 3, gtk_accessible_announce with GTK 4.14 or later), on Windows the window raises a UI Automation notification event, or, while the page or the window itself has the keyboard focus, the page's Bridge says the message in a live region that it adds to the page, and on macOS it posts NSAccessibilityAnnouncementRequestedNotification. Whether and when the message is spoken is up to the screen reader. A browser cannot be asked: an installed browser, including one opened after a fallback, reports no such capability, so keep a live region in the page for it.

CheckNativeAccess

public bool CheckNativeAccess()

Whether the caller is executing on the live native owner's thread.

CheckSidePanel

public DesktopSidePanelRefusal? CheckSidePanel(DesktopWindowEdge edge, int width)

Returns whether DesktopWindow.OpenSidePanelAsync would open a panel now, from the window's last reported DesktopWindow.State and DesktopWindow.Activity: null when it would, otherwise the refusal it would give.

Parameters

edge

The edge; DesktopWindowEdge.Start and DesktopWindowEdge.End follow DesktopWindow.LayoutDirection.

width

The panel's width, as DesktopSidePanelOptions.Width.

Returns

The refusal, or null.

Exceptions

ArgumentOutOfRangeException

edge is not defined, or width is smaller than DesktopSidePanelOptions.MinimumWidth.

ObjectDisposedException

The window is closed or was replaced.

Remarks

Cheap and synchronous, for showing or hiding a command that opens the panel. It checks everything except whether the panel's page loads: DesktopSidePanelRefusal.Unsupported, DesktopSidePanelRefusal.AlreadyOpen, the window's state (minimized or hidden, full-screen, maximized, snapped), the left edge on a platform without positions, and room in the monitor's work area. The window can change before the panel opens, so the result of DesktopWindow.OpenSidePanelAsync decides.

CloseAsync

public ValueTask CloseAsync(CancellationToken cancellationToken = default)

Forcibly closes this window without awaiting confirmation. Use RequestCloseAsync for user actions.

Parameters

cancellationToken

Stops this caller's wait. The close itself always proceeds and is shared by every caller.

Returns

A task that completes when the window has closed.

Exceptions

OperationCanceledException

cancellationToken was cancelled before the close completed.

DisposeAsync

public ValueTask DisposeAsync()

Forcibly closes the window, like DesktopWindow.CloseAsync.

FocusAsync

public ValueTask FocusAsync(CancellationToken cancellationToken = default)

Activates the window and gives it keyboard focus.

Parameters

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has been focused.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Focus. It also shows a window opened with DesktopWindowOptions.Hidden: every built-in embedded host shows the window before it activates it, and this is the way to reveal a hidden window. The windows it owns that DesktopWindow.SetVisibleAsync hid with it are shown again, and when a window-modal window blocks it (DesktopWindow.OwnedWindows), that window is focused after it, as the platform does for a dialog's owner.

HideAsync

public ValueTask HideAsync(CancellationToken cancellationToken = default)

Hides the window without closing it, as DesktopWindow.SetVisibleAsync with false.

Parameters

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the native window has been hidden.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Visibility. DesktopWindow.ShowAsync or DesktopWindow.FocusAsync shows it again.

InvokeNativeAsync

public ValueTask InvokeNativeAsync(Action<nint> action, CancellationToken cancellationToken = default)

Runs native work on this owner's thread. Cancellation prevents queued work; a running callback is awaited to completion. No native handle may outlive this window.

MaximizeAsync

public ValueTask MaximizeAsync(CancellationToken cancellationToken = default)

Maximizes the window (zooms it on macOS); does nothing when it already is.

Parameters

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has been asked to maximize; DesktopWindow.StateChanged reports it.

Exceptions

InvalidOperationException

The window is a DesktopWindowOptions.Kiosk window, which stays full-screen.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Placement. Unlike DesktopWindow.ToggleMaximizedAsync, a second call leaves the window maximized. A full-screen window leaves full screen first and then maximizes; a minimized window is restored maximized; a hidden window stays hidden and shows maximized when it is next shown. With a side panel open the window keeps it.

Windows activates a window that it maximizes, as it does when the user maximizes one. macOS animates the zoom, and a window manager on Linux decides when the window is maximized, so DesktopWindowState.IsMaximized may change after the call has completed.

MinimizeAsync

public ValueTask MinimizeAsync(CancellationToken cancellationToken = default)

Minimizes the window.

Parameters

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has been minimized.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Minimize.

MoveAsync

public ValueTask MoveAsync(int x, int y, CancellationToken cancellationToken = default)

Moves the window to a screen position.

Parameters

x

The new horizontal position in pixels; negative values are left of the primary screen's origin.

y

The new vertical position in pixels; negative values are above the primary screen's origin.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has been moved.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Move. Moving and then resizing shows the window moved but not yet resized in between; DesktopWindow.SetBoundsAsync does both in one operation. On macOS a position that would put the title bar behind a menu bar, such as y 0, puts it just below the menu bar (the work area's top), as DesktopWindow.SetBoundsAsync does.

MoveToMonitorAsync

public ValueTask MoveToMonitorAsync(string monitorId, CancellationToken cancellationToken = default)

Moves the window to a monitor, centered on its work area and shrunk to fit it.

Parameters

monitorId

The DesktopMonitor.Id of a connected monitor, from DesktopHost.Monitors.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has moved.

Exceptions

ArgumentException

monitorId is empty, or no connected monitor has this identifier.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.MonitorPlacement. A maximized window moves maximized. A full-screen window moves full-screen on Windows; macOS keeps a full-screen window in its space, and moves it when it leaves full screen.

OpenDevToolsAsync

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

Opens the page's developer tools in a window of their own, for debugging in Development.

Parameters

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes with true when the window asked its WebView to open the developer tools, or false when it cannot: its developer tools are off (DesktopWebContentPolicy.DevTools, by default on only in Development), or the window is not a WebView2 window.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

OperationCanceledException

cancellationToken was cancelled.

Remarks

WebView2 windows open Microsoft Edge DevTools (ICoreWebView2::OpenDevToolsWindow), the same window that F12, Ctrl+Shift+I and Inspect open. While the developer tools are on, the window's system menu (Alt+Space, or a right click on the title bar) also has an Open DevTools item. DevTools extensions in DesktopHostOptions.DevToolsExtensions appear as panels of that window.

WebKitGTK windows (GTK 3 and GTK 4) show WebKit's Web Inspector, as Inspect Element from the page's context menu does (since 0.23). WKWebView windows return false: WKWebView opens its Web Inspector only from Safari. An installed browser has its own developer tools. The menu bar item of DesktopMenuItem.OpenDevToolsCommandId runs this for the key window.

OpenSidePanelAsync

public ValueTask<DesktopSidePanelResult> OpenSidePanelAsync(DesktopSidePanelOptions options, CancellationToken cancellationToken = default)

Opens a page of this window's surface beside the window's page, growing the window by the panel's width on one edge, in one native operation.

Parameters

options

The edge, width and page of the panel.

cancellationToken

Cancels waiting for the panel's page; a cancelled open leaves the window unchanged.

Returns

Opened with the panel, or Refused with the reason, after which the window is unchanged and the application shows the content another way, such as in an overlay of its page.

Exceptions

ArgumentNullException

options is null.

ArgumentOutOfRangeException

The width is smaller than DesktopSidePanelOptions.MinimumWidth, the edge is not defined, or the load timeout is not positive.

ArgumentException

The path is empty, absolute, or leaves the surface.

ObjectDisposedException

The window was closed or replaced before the call.

OperationCanceledException

cancellationToken was cancelled.

Remarks

The window creates the panel's view hidden, from its page's profile, loads DesktopSidePanelOptions.Path and then grows: on the right edge by widening, on the left edge by moving left and widening, so the page keeps its size and its position on the screen and is not laid out again. The panel's page is a second connection of the window, with a credential of its own, so it binds to the same ViewModel tree as the page. It is not granted navigation-path/1: its address and history are its own, and its Back does not navigate the window. It may show hosted regions. Files dropped on it reach DesktopWindow.FilesDropped as the page's do, with DesktopFileDropEventArgs.InSidePanel set. window.close() in it closes the panel, never the window, and its title is not the window's.

While the panel is open, DesktopWindowState.Bounds and DesktopWindowState.VisibleBounds are the whole window, panel included, while DesktopWindowState.Placement excludes the panel: a window restored from it opens at its page's size, and the application decides whether to open the panel again. The minimum width grows by the panel's. The user may maximize the window or make it full-screen: the panel keeps its width and the page takes the rest.

The panel opens only in a window of its own that a built-in host shows: WebView2 on Windows, WKWebView on macOS and WebKitGTK on Linux (GTK 3, and GTK 4 with Runic.Desktop.Gtk4; on Wayland only on the right edge). Every other presentation refuses with DesktopSidePanelRefusal.Unsupported. The other refusals are the window's state (DesktopWindow.CheckSidePanel), a panel that is open already, a page that did not load, and a window that closed while the panel opened (DesktopSidePanelRefusal.Closed).

RequestCloseAsync

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

Requests closing, awaiting the same confirmation as the native close button.

Remarks

Cancelling the caller stops its wait, without cancelling another caller's shared decision. A window with a close drain (DesktopWindowOptions.CloseDrainAsync) returns true as soon as the close was approved, while it stays open and drains; a request during the drain also returns true and changes nothing. DesktopWindow.WaitForCloseAsync waits for the window itself.

ResizeAsync

public ValueTask ResizeAsync(int width, int height, CancellationToken cancellationToken = default)

Resizes the window.

Parameters

width

The new width in pixels.

height

The new height in pixels.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has been resized.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

ArgumentOutOfRangeException

width or height is not positive.

Remarks

Requires DesktopWindowCapabilities.Resize. The units are those of DesktopWindowState.Width, which on Windows round device-independent pixels; to move and resize by an exact amount in one operation, use DesktopWindow.SetBoundsAsync with DesktopWindowState.Bounds.

RestoreAsync

public ValueTask RestoreAsync(CancellationToken cancellationToken = default)

Returns the window to its normal bounds: leaves full screen, un-maximizes and un-minimizes it; does nothing when it is normal.

Parameters

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has been asked to restore; DesktopWindow.StateChanged reports it.

Exceptions

InvalidOperationException

The window is a DesktopWindowOptions.Kiosk window, which stays full-screen.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Placement. The window ends at the bounds that DesktopWindowState.Placement reports, also when it was minimized while maximized, where Windows and the window managers would return it maximized. DesktopWindow.FocusAsync keeps that platform behaviour. A hidden window stays hidden and shows at its normal bounds when it is next shown.

Windows restores a visible maximized window without activating it, but activates a window that it brings back from minimized. On Linux a minimized window is restored on X11; Wayland tells no application that its window is minimized, and GTK 4 has no call to restore one, so there DesktopWindow.FocusAsync brings it back.

SetBoundsAsync

public ValueTask SetBoundsAsync(DesktopWindowBounds bounds, CancellationToken cancellationToken = default)

Moves and resizes the window in one native operation, in units that round-trip exactly.

Parameters

bounds

The new frame in the units of DesktopWindowState.Bounds: physical pixels on Windows, points on macOS, logical pixels on Linux (see DesktopWindowBounds). DesktopWindowBounds.X and DesktopWindowBounds.Y null keep the top-left corner where it is. DesktopWindowBounds.Scale is ignored.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has been asked to take the bounds; DesktopWindow.StateChanged reports them.

Exceptions

ArgumentNullException

bounds is null.

ArgumentException

The width or height is not positive, or only one of DesktopWindowBounds.X and DesktopWindowBounds.Y is set.

InvalidOperationException

The window is maximized, full-screen or minimized.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable), or cannot be positioned (window-position-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Bounds. Unlike DesktopWindow.MoveAsync followed by DesktopWindow.ResizeAsync, the window never shows at the new position with the old size: Windows sets both with one SetWindowPos, macOS with one setFrame:display:, GTK 3 sends one configure request (gtk_window_move and gtk_window_resize in one main-loop turn), and GTK 4 on X11 one XMoveResizeWindow. Bounds read from DesktopWindowState.Bounds set the same frame again, so a window grown with DesktopWindowBounds.Expand and shrunk again is exactly as before:

var bounds = window.State!.Bounds!;
await window.SetBoundsAsync(bounds.Expand(DesktopWindowEdge.Left, 372)); // the right edge stays in place

The platform still applies the window's minimum size, and a window manager may adjust the frame, as it may for DesktopWindow.MoveAsync.

Refusals:

  • An installed browser, a custom window host without IDesktopBoundsWindowHost, and WPF's RunicWebView, whose WPF window the application sizes itself, lack the capability (window-capability-unavailable). A serve-only application has no DesktopWindow, and a hosted application's guest (runic-host/1) only asks its host to move or resize the window; the host application's DesktopWindow of a hosted window sets bounds as any other.
  • On Wayland, a position fails with window-position-unavailable: pass bounds without DesktopWindowBounds.X and DesktopWindowBounds.Y, which only resize the window.
  • A maximized, full-screen or minimized window fails with InvalidOperationException: its frame belongs to the desktop until it is restored.

SetColorSchemeAsync

public ValueTask SetColorSchemeAsync(DesktopColorScheme colorScheme, CancellationToken cancellationToken = default)

Changes the window's light or dark appearance while it is open, for example when the user picks a theme.

Parameters

colorScheme

DesktopColorScheme.Light or DesktopColorScheme.Dark, or DesktopColorScheme.NoPreference to follow the application's scheme (DesktopHost.ColorScheme), which by default follows the system.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window and its page show the scheme.

Exceptions

ArgumentOutOfRangeException

colorScheme is not a defined value.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.ColorScheme. The page's prefers-color-scheme and its form controls follow, as for DesktopWindowOptions.ColorScheme, and so does the window's own frame: on Windows the WebView2 profile's preferred color scheme and the title bar's dark mode, on macOS NSWindow.appearance.

Without DesktopWindowCapabilities.PerWindowColorScheme the scheme is not this window's alone, and the call changes every window that shares it: on Linux every GTK window of the application (GTK's settings, or the libadwaita style manager, belong to the process), on Windows every window that shares the WebView2 profile of a DesktopWindowOptions.ProfilePath. DesktopWindow.ColorScheme of each of them reports it. The next DesktopHost.SetColorSchemeAsync sets the application's scheme for all of them again.

DesktopWindow.ColorScheme changes only once the window applied the scheme; a call that fails leaves it as it was.

SetDocumentEditedAsync

public ValueTask SetDocumentEditedAsync(bool edited, CancellationToken cancellationToken = default)

Sets whether the window's document has unsaved changes: on macOS the dot in the close button.

Parameters

edited

true while the document has unsaved changes; false after it was saved or reverted.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window shows the state.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

OperationCanceledException

cancellationToken was cancelled.

Remarks

On macOS this sets NSWindow.documentEdited of the embedded window. It only marks the window: closing it still asks DesktopWindowOptions.ConfirmCloseAsync, and quitting DesktopHostOptions.ConfirmQuitAsync, which decide about the unsaved changes.

Windows and Linux have no such convention: the call checks the window and does nothing, as for an installed browser or a custom window host. Show unsaved changes in the page or the title there, such as with a leading • or a trailing *.

SetFullScreenAsync

public ValueTask SetFullScreenAsync(bool fullScreen, CancellationToken cancellationToken = default)

Enters or leaves full screen.

Parameters

fullScreen

true to fill the window's monitor without a frame; false to return to its bounds before.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has been asked to change; DesktopWindow.StateChanged reports the change.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

InvalidOperationException

fullScreen is false for a DesktopWindowOptions.Kiosk window, which stays full-screen.

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.FullScreen. Does nothing when the window already is, or is not, full-screen. On macOS the window moves into its own full-screen space with AppKit's animation, so DesktopWindowState.IsFullScreen changes when the animation ends. DesktopWindowState.Placement records the state, so a window closed full-screen opens full-screen again.

SetLayoutDirectionAsync

public ValueTask SetLayoutDirectionAsync(DesktopLayoutDirection layoutDirection, CancellationToken cancellationToken = default)

Changes the direction of the window's native parts while it is open, for example after a switch to or from a right-to-left language.

Parameters

layoutDirection

DesktopLayoutDirection.RightToLeft or DesktopLayoutDirection.LeftToRight, or DesktopLayoutDirection.Default to return to the platform's.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window shows the direction.

Exceptions

ArgumentOutOfRangeException

layoutDirection is not a defined value.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.LayoutDirection. It applies what DesktopWindowOptions.LayoutDirection applies when the window opens: on Windows the frame, caption buttons, system menu and native menu bar are mirrored; on Linux the GTK window and Runic's widgets in it, such as the menu bar, change direction. The page keeps its own layout, which it sets with <html dir> (syncDocumentLocale in @runic-artifex/views). macOS reports no capability: AppKit lays windows out for the application bundle's localization.

DesktopWindow.LayoutDirection changes only once the window applied the direction.

SetMinimumSizeAsync

public ValueTask SetMinimumSizeAsync(int width, int height, CancellationToken cancellationToken = default)

Changes the window's minimum size while it is open.

Parameters

width

The minimum width, or zero for none.

height

The minimum height, or zero for none.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the platform enforces the new minimum.

Exceptions

ArgumentOutOfRangeException

width or height is negative.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.MinimumSize. The values use the units of DesktopWindowOptions.MinimumWidth and DesktopWindowOptions.MinimumHeight, which it replaces, and of DesktopWindow.ResizeAsync: device-independent pixels of the outer frame on Windows, the content size in points on macOS and in logical pixels on Linux. They stay device-independent, so the minimum keeps its size when the window moves to a monitor of another scale.

The user cannot make the window smaller from then on: Windows answers WM_GETMINMAXINFO with it, macOS sets contentMinSize, and GTK sets the size request of the window's content. A window that is smaller grows to the minimum and keeps its top-left corner. A maximized or full-screen window keeps its frame and applies the minimum when it is restored.

Refusals: an installed browser, a custom window host without IDesktopBoundsWindowHost and WPF's RunicWebView lack the capability (window-capability-unavailable).

SetPlacementAsync

public ValueTask SetPlacementAsync(DesktopWindowPlacement placement, CancellationToken cancellationToken = default)

Gives the open window a placement, as DesktopWindowOptions.Placement gives a window that opens: the bounds it restores to and whether it is maximized or full-screen.

Parameters

placement

The placement, in the units of DesktopWindowState.Placement, so a placement read there, saved and passed back, places the window where it was:

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window has been asked to take the placement; DesktopWindow.StateChanged reports it, and DesktopWindowState.Placement then reports the placement back.

Exceptions

ArgumentNullException

placement is null.

ArgumentException

Only one of DesktopWindowPlacement.X and DesktopWindowPlacement.Y is set, or DesktopWindowPlacement.Origin is not a defined value.

InvalidOperationException

The window is a DesktopWindowOptions.Kiosk window, which stays full-screen, and DesktopWindowPlacement.FullScreen is false.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable), or cannot be positioned (window-position-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Placement. Unlike DesktopWindow.SetBoundsAsync, which takes the exact native frame of a normal window, this sets the bounds a maximized, minimized, full-screen or hidden window restores to: a maximized window stays maximized with new restored bounds unless DesktopWindowPlacement.Maximized is false, a minimized one stays minimized and a hidden one hidden, and each takes the placement's state when it is next shown or restored.

The placement is kept on a monitor that exists now, as at open: bounds off every monitor's work area move onto the work area they overlap most, or the nearest one, and bounds larger than it shrink to fit. On Windows the width and height convert with the scale of that monitor. DesktopWindowState.Placement reports the bounds after this fit.

With a side panel open (DesktopWindow.SidePanel), the placement is that of the window's page, as DesktopWindowState.Placement reports it: the window grows by the panel on its edge, so the page takes the placement's bounds.

Windows activates a window that the placement maximizes or brings back from minimized; a window that stays normal or maximized keeps its activation. On Wayland a placement with a position fails as a whole and leaves the window as it is: pass one without DesktopWindowPlacement.X and DesktopWindowPlacement.Y, as DesktopWindowState.Placement reports there.

SetRepresentedFileAsync

public ValueTask SetRepresentedFileAsync(string? path, CancellationToken cancellationToken = default)

Sets the file that the window shows a document of: on macOS the title bar's proxy icon and the path menu that Command-clicking the title opens.

Parameters

path

The document's fully qualified path, or null to remove the icon.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window represents the file.

Exceptions

ArgumentException

path is empty, not fully qualified, or contains a NUL character.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

OperationCanceledException

cancellationToken was cancelled.

Remarks

On macOS this sets NSWindow.representedURL of the embedded window; the window's title stays the page's title, so set the page's document.title to the document's name. The file need not exist. In the App Sandbox, pass the LocalPath of an open document lease (IDocumentFileLease), whose access lets the title bar show the file's icon.

Windows and Linux have no such convention: the call checks its arguments and the window, and does nothing. It also does nothing for an installed browser or a custom window host; a custom host on macOS can set the property itself through DesktopWindow.InvokeNativeAsync.

SetVisibleAsync

public ValueTask SetVisibleAsync(bool visible, CancellationToken cancellationToken = default)

Shows or hides the window without closing it.

Parameters

visible

true to show the window; false to hide it.

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the native window has been shown or hidden.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Visibility, which every embedded window reports; an installed browser cannot be hidden. A hidden window stays open: DesktopWindow.IsOpen stays true, its page keeps running and its Bridge stays connected, and DesktopWindow.WaitForCloseAsync keeps waiting. A window opened with DesktopWindowOptions.Hidden is shown the same way. Whether showing also activates the window is up to the platform and window manager; DesktopWindow.FocusAsync shows and activates it everywhere.

Hiding a window hides the windows it owns (DesktopWindow.OwnedWindows) first, and showing it shows them again.

Only the application can show a hidden window again, so keep a way to reach it, such as a status icon or a second launch that calls DesktopWindow.FocusAsync.

ShowAsync

public ValueTask ShowAsync(CancellationToken cancellationToken = default)

Shows a hidden window, as DesktopWindow.SetVisibleAsync with true. A window that is shown stays as it is.

Parameters

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the native window has been shown.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Visibility. Whether showing also activates the window is up to the platform and window manager; DesktopWindow.FocusAsync shows and activates it everywhere.

ShowContextMenuAsync

public ValueTask<DesktopMenuItem?> ShowContextMenuAsync(ImmutableArray<DesktopMenuEntry> items, DesktopContextMenuOptions? options = null, CancellationToken cancellationToken = default)

Shows a native context menu over the window and returns the item the user chose.

Parameters

items

The menu's entries: the rules of DesktopMenuItem and DesktopMenu, at most four nested submenus and 256 entries, and at least one entry. Command ids may repeat; DesktopMenu.Role is ignored.

options

Where the menu opens, in CSS pixels of the page's viewport; null opens it at the pointer.

cancellationToken

Cancels waiting for the window, and closes the open menu.

Returns

The chosen item, one of items, or null when the user dismissed the menu, a later context menu of the window replaced it, or the window closed.

Exceptions

ArgumentException

items is empty or breaks a menu rule, or options sets only one coordinate or one that is not finite.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.ContextMenu. The menu is modal for the window. Disabled items and the items of disabled submenus cannot be chosen, and a shortcut is shown as a hint without being registered. The caller runs the chosen item's command, for example with its DesktopMenuItem.CommandParameter.

StartDragAsync

public ValueTask<DesktopDragEffects> StartDragAsync(DesktopDragData data, CancellationToken cancellationToken = default)

Starts a native drag of files, text or URIs out of the window, and returns the effect of the drop.

Parameters

data

What to drag; the rules of DesktopDragData.

cancellationToken

Cancels waiting for the window before the drag starts; a running drag ends with the pointer.

Returns

The effect the drop target chose, one of DesktopDragData.AllowedEffects, or DesktopDragEffects.None when the user cancelled the drag (Escape), dropped it where nothing accepted it, or no mouse button was held when the call reached the window.

Exceptions

ArgumentException

data breaks a rule of DesktopDragData.

ArgumentNullException

data is null.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.FileDrag. Call it while the user holds the primary mouse button on the window. The page asks for the drag from its dragstart event through an ordinary command, after calling event.preventDefault() so the web view starts no HTML5 drag of its own; the application builds data from its own state, never from paths the page supplies:

row.addEventListener("dragstart", event => { event.preventDefault(); client.commands.dragItem(row.dataset.key); });

The window offers DesktopDragData.Paths as the platform's file list (CF_HDROP, public.file-url, text/uri-list), DesktopDragData.Text as plain text and DesktopDragData.Uris as links. DesktopDragEffects.Move means the target asked for a move: the files may already be gone (an optimized move on Windows, a file manager on macOS or Linux), so delete what remains.

ToggleMaximizedAsync

public ValueTask ToggleMaximizedAsync(CancellationToken cancellationToken = default)

Maximizes the window, or restores it when it is already maximized.

Parameters

cancellationToken

Cancels waiting for the window and the queued native operation.

Returns

A task that completes when the window state has changed.

Exceptions

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

DesktopException

The window lacks the required capability (window-capability-unavailable).

OperationCanceledException

cancellationToken was cancelled.

Remarks

Requires DesktopWindowCapabilities.Maximize. Its result depends on the window's state when the call reaches it; DesktopWindow.MaximizeAsync and DesktopWindow.RestoreAsync ask for a state and do nothing when the window is in it already.

TryExecuteMenuItemAsync

public ValueTask<bool> TryExecuteMenuItemAsync(DesktopMenuItem item, CancellationToken cancellationToken = default)

Runs a menu item's command as the native menu bar does, and returns whether a command ran.

Parameters

item

The item, usually one of DesktopHost.MenuBar, such as the one a page's menu bar chose.

cancellationToken

Cancels waiting for the window's UI thread.

Returns

true when a target ran the item; false when the item is disabled (DesktopMenuItem.IsEnabled) or neither DesktopWindow.MenuCommands nor DesktopHost.MenuCommands can run it now.

Exceptions

ArgumentException

item breaks a menu item rule.

ObjectDisposedException

The surface is disposed, or this window is closed or was replaced.

OperationCanceledException

cancellationToken was cancelled.

Remarks

The one command path of the native bar, a global menu, shortcuts and an in-page bar: the window's target, then the application's, each asked IDesktopMenuCommandTarget.CanExecute before IDesktopMenuCommandTarget.Execute, on the window's UI thread where the window has one. A target that throws is logged (event 3014) and does not reach the caller. It works for every presentation, including an installed browser.

WaitForCloseAsync

public ValueTask WaitForCloseAsync(CancellationToken cancellationToken = default)

Waits until this presentation closes while servicing platform window events.

Parameters

cancellationToken

Stops this caller's wait; it does not close the window.

Returns

A task that completes when the window has closed and its native resources have been released.

Exceptions

OperationCanceledException

cancellationToken was cancelled.

Remarks

On macOS and with the GTK 4 host the process main thread must run DesktopEventLoop.Run, which services native window events while the application awaits this method.

A window with a close drain (DesktopWindowOptions.CloseDrainAsync) stays open while it drains, so this returns once the drain ended and the window closed. An installed browser cannot stay open: there the drain runs once its window is gone, and this returns after it.

Events

ActivityChanged

public event EventHandler<DesktopWindowActivityChangedEventArgs>? ActivityChanged

Occurs after the window's DesktopWindow.Activity has changed.

Remarks

Raised on the thread pool, never on the window's native thread and never concurrently. Every change raises one event, in the order the window reported them, also when changes arrive faster than the handlers run: the event's DesktopWindowActivityChangedEventArgs.Activity is that change's activity, while DesktopWindow.Activity may already be a later one. A handler that throws is logged (event 3040, WindowActivityHandlerFailed) and does not stop later events. The last event of a window that closes reports it inactive and not visible; no event is raised after that or after the window was replaced.

Closed

public event EventHandler? Closed

Occurs once after the window has closed.

Remarks

Raised whatever closed the window: the user, DesktopWindow.RequestCloseAsync, DesktopWindow.CloseAsync, disposing the surface or host, a host shutdown, or the user closing an installed browser's window or the browser itself. It is the event counterpart of DesktopWindow.WaitForCloseAsync, like Window.Closed in WPF and Avalonia: DesktopWindow.IsOpen is then false and DesktopWindow.State holds the last state to save. The native window's resources may still be being released; DesktopWindow.WaitForCloseAsync waits for that too.

Raised on the thread pool, never on the window's native thread. Watching starts when the first handler is added, so a handler added after the window closed is still called once if the event has not been raised yet; a handler added after the event was raised is not called. A handler that throws is logged (event 3013, WindowClosedHandlerFailed) and does not stop the other handlers. An installed browser's window is noticed within a fraction of a second, as DesktopWindow.WaitForCloseAsync notices it. On macOS and with the GTK 4 host the process main thread must run DesktopEventLoop.Run.

FilesDropped

public event EventHandler<DesktopFileDropEventArgs>? FilesDropped

Occurs when the user drops files onto the embedded window, with their native paths.

Remarks

An embedded window whose DesktopWindow.Capabilities include DesktopWindowCapabilities.FileDrop raises it once per drop, with the absolute local paths the platform reported: WebView2 from the File objects of the page's drop event, WKWebView from the dragging pasteboard, WebKitGTK from the drag's text/uri-list or GTK 4 file list. The page never supplies a path; on WebView2 page script can at most hand over File objects the user gave it by dropping or choosing them. The page still receives its own dragenter, dragover and drop events to show a drop target, and dropping a file never navigates the window. DesktopFileDropEventArgs.Target is the page's untrusted routing label.

Raised on the thread pool, one drop at a time and in drop order. A handler that throws is logged (event 3012, FileDropHandlerFailed). An installed browser reports no drops: its page only has HTML5 File objects without paths.

public event EventHandler? MenuBarPlacementChanged

Occurs after DesktopWindow.MenuBarPlacement has changed, for example when a Linux desktop's global menu appeared or went away.

Remarks

Raised on the thread pool. A handler that throws is logged (event 3017, MenuBarPlacementHandlerFailed) and does not stop later events.

StateChanged

public event EventHandler<DesktopWindowStateChangedEventArgs>? StateChanged

Occurs after the window's DesktopWindow.State has changed.

Remarks

Raised on the thread pool, never on the window's native thread and never concurrently. While the user drags or resizes the window, changes that arrive faster than the handlers run are combined, so a handler sees the latest state, not every intermediate one. A handler that throws is logged (event 3011, WindowStateHandlerFailed) and does not stop later events. No event is raised after the window has closed or was replaced.