Skip to content

Windows & menus ​

A window is a component that derives from Window. It can have state, a menu, and access to data shared across the app.

Opening windows ​

A handler that takes Windows& can open new windows by type.

cpp
MenuItem {
    "New Window",
    on_activate = [](Windows& windows) {
        windows.open<MainWindow>({ .title = "Untitled" });
    },
}

To act on the window an event came from, such as closing it, take a CurrentWindow. A window can return a result when it closes, which turns it into a dialog.

A window can define menu, which takes state and environment just like render.

cpp
Menu menu(const State& state) const {
    return Menu {
        Submenu {
            "View",
            MenuItem {
                "Sidebar",
                checked = state.sidebar,
                on_activate = [](State& state) { state.sidebar = !state.sidebar; },
            },
        },
    };
}

Items can have icons, separators and nested submenus.

Shared data ​

Register app-wide data once on the App. Every window can read it in render and change it in a handler through AppData<T>.

cpp
struct Clicks {
    int total = 0;
};

App {}
    .data<Clicks>()
    .open<MainWindow>({ .title = "One" })
    .open<MainWindow>({ .title = "Two" })
    .run(argc, argv);

// Inside MainWindow
View render(const Clicks& clicks) const {
    return Container {
        on_click = [](AppData<Clicks> clicks) { clicks->total += 1; },
        Text { std::format("Clicked {} times across all windows", clicks.total) },
    };
}

Clicking in either window updates the count in both.

Barton is in early development.