Skip to content

Hello World

This tutorial walks through building a simple Hermes application with native menus.

Project Setup

Create a new console application:

bash
dotnet new console -n HelloHermes
cd HelloHermes
dotnet add package Hermes

Basic Window

Replace the contents of Program.cs:

csharp
using Hermes;

var window = new HermesWindow()
    .SetTitle("Hello Hermes")
    .SetSize(800, 600)
    .Center()
    .LoadHtml("""
        <!DOCTYPE html>
        <html>
        <head>
            <style>
                body {
                    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    height: 100vh;
                    margin: 0;
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    color: white;
                }
                h1 { font-size: 3rem; }
            </style>
        </head>
        <body>
            <h1>Hello, Hermes!</h1>
        </body>
        </html>
        """);

window.WaitForClose();

Run it:

bash
dotnet run

Adding Menus

Native menus are a core feature of Hermes. Let's add a menu bar:

csharp
using Hermes;

var window = new HermesWindow()
    .SetTitle("Hello Hermes")
    .SetSize(800, 600)
    .Center();

// Configure menus before showing the window
window.MenuBar
    .AddMenu("File", file =>
    {
        file.AddItem("New", "file.new", item => item.WithAccelerator("Ctrl+N"))
            .AddItem("Open...", "file.open", item => item.WithAccelerator("Ctrl+O"))
            .AddSeparator()
            .AddItem("Exit", "file.exit", item => item.WithAccelerator("Alt+F4"));
    })
    .AddMenu("Help", help =>
    {
        help.AddItem("About", "help.about");
    });

// Handle menu clicks
window.MenuBar.ItemClicked += itemId =>
{
    Console.WriteLine($"Menu clicked: {itemId}");

    if (itemId == "file.exit")
    {
        window.Close();
    }
};

window.LoadHtml("<h1>Hello with Menus!</h1>");
window.WaitForClose();

Window Configuration

Hermes uses a fluent API for window configuration:

csharp
var window = new HermesWindow()
    .SetTitle("My App")
    .SetSize(1024, 768)           // Initial size
    .SetMinSize(640, 480)         // Minimum size
    .SetMaxSize(1920, 1080)       // Maximum size
    .Center()                      // Center on screen
    .SetResizable(true)           // Allow resizing
    .SetTopMost(false)            // Not always on top
    .SetDevToolsEnabled(true)     // Enable F12 dev tools
    .RememberWindowState();       // Persist size/position

Events

Handle window events:

csharp
window
    .OnClosing(() => Console.WriteLine("Window closing..."))
    .OnResized((w, h) => Console.WriteLine($"Resized to {w}x{h}"))
    .OnFocusIn(() => Console.WriteLine("Focused"))
    .OnFocusOut(() => Console.WriteLine("Lost focus"));

Loading Content

Three ways to load content:

csharp
// Load a URL
window.Load("https://example.com");

// Load HTML directly
window.LoadHtml("<h1>Hello!</h1>");

// Load from a custom scheme (for bundled assets)
window.RegisterCustomScheme("app", path =>
{
    var content = LoadAssetFromBundle(path);
    return (content, "text/html");
});
window.Load("app://index.html");

Complete Example

Here's a complete hello world with all features:

csharp
using Hermes;

// Optional: Pre-warm WebView for faster startup (Windows)
HermesWindow.Prewarm();

var window = new HermesWindow()
    .SetTitle("Hello Hermes")
    .SetSize(800, 600)
    .Center()
    .SetDevToolsEnabled(true)
    .RememberWindowState();

window.MenuBar
    .AddMenu("File", file =>
    {
        file.AddItem("Exit", "file.exit", item => item.WithAccelerator("Ctrl+Q"));
    });

window.MenuBar.ItemClicked += itemId =>
{
    if (itemId == "file.exit") window.Close();
};

window.LoadHtml("""
    <!DOCTYPE html>
    <html>
    <body style="font-family: system-ui; text-align: center; padding: 50px;">
        <h1>Hello, Hermes!</h1>
        <p>Press Ctrl+Q or use File > Exit to close.</p>
    </body>
    </html>
    """);

window.WaitForClose();

Next Steps

Released under the Elastic License 2.0