Skip to content

Window Management ​

Quick Reference ​

csharp
var window = new HermesWindow()
    .SetTitle("My App")
    .SetSize(1024, 768)
    .SetMinSize(640, 480)
    .Center()
    .SetResizable(true)
    .RememberWindowState();

Configuration ​

All configuration methods use a fluent API and must be called before Show() or WaitForClose().

MethodDescription
SetTitle(string)Window title
SetSize(int, int)Initial size in pixels
SetPosition(int, int)Initial position
Center()Center on screen
SetResizable(bool)Allow resizing
SetMinSize(int, int)Minimum size constraint
SetMaxSize(int, int)Maximum size constraint
SetCustomTitleBar(bool)Enable custom title bar mode
RememberWindowState(string?)Persist position, size, and maximized state across launches
SetTheme(HermesWindowTheme)Light or dark window chrome and WebView (default: follow the system)

Window State ​

Control window state at runtime:

csharp
window.MaximizeWindow();
window.MinimizeWindow();
window.RestoreWindow();
window.ToggleMaximize();

bool maximized = window.IsMaximized;

Light and Dark Appearance ​

HermesWindowTheme sets whether the native window chrome and the WebView use the light or dark appearance. System (the default) follows the operating system; Light and Dark override it. Match it to your app's own theme so the native parts of the window stay readable, for example the macOS traffic lights on a light app theme.

csharp
var window = new HermesWindow()
    .SetTheme(HermesWindowTheme.Light);

// Later, when the user switches the app's theme:
window.Theme = HermesWindowTheme.Dark;

Theme can be set at any time and from any thread; Hermes applies the change on the UI thread. In a Blazor app, set the initial value with options.Theme in ConfigureWindow, and inject HermesWindow to change it at runtime:

razor
@inject HermesWindow Window

@code {
    private void OnDarkModeChanged(bool dark) =>
        Window.Theme = dark ? HermesWindowTheme.Dark : HermesWindowTheme.Light;
}
PlatformEffect
macOSWindow appearance, including the title bar and traffic lights; the WebView's prefers-color-scheme follows
WindowsTitle bar colors and the WebView2 preferred color scheme
LinuxIgnored for now; the window follows the GTK theme

Close Request Handling ​

OnCloseRequested lets you intercept the close button click before the window closes. This is useful for prompting the user about unsaved changes or running cleanup logic.

The handler returns a Task<bool>: return true to allow the close, or false to cancel it.

csharp
var window = new HermesWindow()
    .SetTitle("My Editor")
    .SetSize(1024, 768)
    .OnCloseRequested(async () =>
    {
        if (!hasUnsavedChanges)
            return true;

        var shouldClose = await PromptSaveChangesAsync();
        return shouldClose;
    })
    .LoadHtml("<h1>Hello</h1>");

window.WaitForClose();

In a Blazor application, you can wire this up from a root component:

razor
@inject HermesWindow Window

@code {
    protected override void OnInitialized()
    {
        Window.OnCloseRequested(OnCloseRequestedAsync);
    }

    private async Task<bool> OnCloseRequestedAsync()
    {
        if (!IsDirty)
            return true;

        ShowConfirmDialog = true;
        await InvokeAsync(StateHasChanged);

        // Wait for the user's decision
        return await _closeDecision.Task;
    }
}

OnCloseRequested vs OnClosing

OnCloseRequested fires before the window closes and can prevent it. OnClosing fires after the close is confirmed and cannot cancel it. Use OnCloseRequested for save prompts; use OnClosing for final cleanup.

Next Steps ​