Tabs
BeginTabBar / Tab
A tab bar is a row of tabs. The user clicks a tab to show its content. Only one tab is selected at a time.
- *selectedIndex is the index of the selected tab.
- The user changes it with a click or with the arrow keys.
- The content under the bar is yours to build, from *selectedIndex.
| Input | Effect |
| Drag a tab | Reorders; calls onReorder(from, to) |
| onClose(index) set | "Close Panel" context menu (Shift+F10), Ctrl+W / Ctrl+F4 close the focused tab |
| Alt+Left / Right | Moves the focused tab (clicking a tab focuses it) |
BeginTabBar(&selectedFile,
[this](int from, int to) {
std::string moved = std::move(openFiles[size_t(from)]);
openFiles.erase(openFiles.begin() + from);
openFiles.insert(openFiles.begin() + to, std::move(moved));
},
[this](int index) { openFiles.erase(openFiles.begin() + index); });
for (const std::string& file : openFiles)
Tab(file);
EndTabBar();
When the tabs don't fit in the bar, its TabBarLayout decides what happens. The user switches it per bar, from the context menu of the bar, of a tab or of the "+N" button:
- One row ("Collapse Rows", the default): the first tabs that fit, then a "+N" button that lists the rest.
- The selected tab is always shown. It takes the place of the last tabs that fit, so the arrow keys and Home/End reach the listed tabs too.
- A Tab that is in the list builds nothing and returns false.
- What fits is worked out from the previous frame's sizes, so after a resize the bar settles one frame later.
- Several rows ("Multiple Rows"): all tabs, wrapping onto more rows.
- Dragging a tab to another row moves it there.
- The arrow keys and Alt+arrows continue across rows.
Where the choice is kept:
- Pass Layout to keep the choice yourself.
- Otherwise the bar remembers it by its ID, for as long as the app runs.
- A docked tab group (see Docking) stores it on its dock node, so it's saved with the layout.
onDragOut and dragVisualizer work as in the reorderable group:
- onDragOut gets a tab released outside the bar.
- dragVisualizer runs every frame while a tab is dragged outside the bar, to draw it.
TabBarOptions:
| Field | Effect |
| closeLabel | Text of the close item ("Close Panel") |
| listHidden = false | No "+N" button, for listing hidden tabs yourself |
| layoutMenu = false | No "Collapse Rows" / "Multiple Rows" choice |
| dropArea | Element around the bar that counts as the bar while dragging: reorders inside, past the last tab goes to the end; only outside it drags out |
bool BeginTabBar(ID id, int* selectedIndex, std::function<void(int, int)> onReorder,
std::function<void(int)> onClose = nullptr,
std::function<void(int, int2)> onDragOut = nullptr,
std::function<void(int, int2)> dragVisualizer = nullptr,
StyleRef style = {}, LayoutOverride&& constraints = {},
TabBarLayout* layout = nullptr, const TabBarOptions& options = {});
void EndTabBar();
bool Tab(ID id, const GUIContent& content, StyleRef style, LayoutOverride&& constraints = {});
bool BeginTab(ID id, const GUIContent& content, StyleRef style, StyleRef contentStyle,
LayoutOverride&& constraints = {});
void EndTab();
A bar that keeps its layout in a member, always on several rows, with its own close label:
BeginTabBar(tabsID, &selectedFile, nullptr,
[this](int index) { openFiles.erase(openFiles.begin() + index); },
nullptr, nullptr, {}, {}, &tabLayout,
TabBarOptions{ .closeLabel = "Close File", .layoutMenu = false });
for (const std::string& file : openFiles)
EndTabBar();
BeginTab and EndTab are for tabs with more in them than an icon and text.
- BeginTab draws the icon and text, in contentStyle (default tab-content).
- Then comes what you add before EndTab.
- Always pair it with EndTab.
- If BeginTab returns false, the tab is in the "+N" list. Add nothing to it then.
if (BeginTab(errorsTabID, "Errors", theme.tabStyleRef, theme.tabContentStyleRef))
Text(std::to_string(errorCount));
EndTab();
- The forms without an ID make one from the place they're called from. See IDs.
- The CSS classes:
- tab-bar
- tab
- tab-content
- tab-overflow for the "+N" button. Its list uses the menu classes.
- In the several-rows layout the bar's style gets flex-wrap: wrap (StyleManager::WithFlexWrap). In the one-row layout it gets nowrap.
BeginTabBar(idManager.AllocateDynamicID("tabs"), &selectedTab, nullptr);
Tab("First");
Tab("Second");
EndTabBar();
Text(selectedTab == 0 ? "Content of the first tab" : "Content of the second tab");
Docking
BeginDockingContainer / EndDockingContainer
Docking lets the user arrange the parts of a window, called panels, by dragging them around. A panel is a part of the UI with a title, such as "Scene" or "Log".
- The panels sit in a tree of splits, stored in a DockingLayout.
- A split divides its space between two children, side by side or one above the other.
- Each node of the tree is a dock node: a split, or a leaf.
- Each leaf is a tab group: a tab bar with one tab per panel, showing the selected one.
- The user drags a panel's tab to:
- move it within its tab group
- dock it on another leaf
- float it outside the window, in a window of its own
- Create each panel once, with CreatePanel. Its callback builds the panel's content.
ID CreatePanel(std::unordered_map<ID, Panel>& panels, std::string title, IconID icon,
std::function<void()> onPanelGUI, std::string panelType = {},
std::function<void()> onEnable = {}, std::function<void()> onDisable = {});
bool BeginDockingContainer(ID id, DockingLayout* layout, StyleRef style, LayoutOverride&& constraints = {});
bool BeginDockingContainer(ID id, DockingLayout* layout, LayoutOverride&& constraints = {});
void EndDockingContainer();
void EmitFloatingDockWindows();
- Build the tree with MakeLeaf and MakeSplit (gui_dock_node.h).
- MakeLeaf({ panelIDs... }) makes a tab group of those panels.
- MakeSplit(mode, size, first, second) makes a split of two nodes.
- DockSplitMode says which child keeps size: LeftPinned, RightPinned, TopPinned or BottomPinned.
- Call EmitFloatingDockWindows() after the container. It builds the windows of the floating panels.
- Closing a panel destroys it.
- The tab group holding the focus shows it: its shown tab is underlined in the accent.
- The focus counts as held when it's in the group's panel or on one of its tabs.
- For example, click into a field in the Log panel: Log's tab is underlined, while the Scene group's shown tab keeps its plain look.
- A torn-off window's title-bar tabs do the same.
- The CSS classes:
- dock-container
- dock-leaf
- dock-content
- tab and title-bar-tab for the tabs; tab-panel-focused and title-bar-tab-panel-focused for those of the group holding the focus
- dock-split-horz and dock-split-vert
- dock-sizer-horz and dock-sizer-vert for the handles between the halves of a split
- dock-window for a floating panel's window
- panel-drop-visualization for the highlight where a dragged panel will land
const ID scene = CreatePanel(layout.panels, "Scene", IconID::Invalid, [this] { ScenePanel(); });
const ID details = CreatePanel(layout.panels, "Details", IconID::Invalid, [this] { DetailsPanel(); });
const ID log = CreatePanel(layout.panels, "Log", IconID::Invalid, [this] { Text("Nothing logged yet."); });
layout.root = MakeSplit(DockSplitMode::LeftPinned, 220,
MakeLeaf({ scene }),
MakeSplit(DockSplitMode::BottomPinned, 150, MakeLeaf({ details }), MakeLeaf({ log })));
if (BeginWindow(windowID, "Docking", theme.mainWindowStyleRef, LayoutOverride::FixedSize(900, 600).Centered()))
{
BeginDockingContainer(dockID, &layout, LayoutOverride::Auto());
EndDockingContainer();
EmitFloatingDockWindows();
}
EndWindow();
onEnable and onDisable run when a panel becomes visible and when it's hidden. A hidden panel isn't built, so it can let go of what it made:
const ID previewPanel = CreatePanel(layout.panels, "Preview", IconID::Invalid, [this] { PreviewPanel(); }, {},
[this] { LoadPreview(); },
[this] { UnloadPreview(); });
Docked panels are cached: their drawn output is kept and reused while nothing in them changes (see BeginCached). A panel's cacheSignature controls this:
- Return a value to also rebuild the panel whenever that value changes. Use it for data the cache can't see change.
- Return std::nullopt to not cache the panel this frame, for a panel that changes every frame.
layout.panels[log].cacheSignature = [this] { return std::optional<uint64_t>(logLines.size()); };
A panel's onChanged runs after a frame in which the user acted in it, once its handlers have run:
- It counts a press, a release or a drag of the mouse in the panel, the wheel over it, and keys or text typed into its focused element.
- The pointer only passing over it doesn't count.
- Use it when other panels show the same data, such as two Inspectors of one object.
- They're cached, so they don't see what changed in this one.
- Rebuild the ones that need it with InvalidatePanel.
- Which panels need it is your app's to say: the library doesn't know what a panel shows.
layout.panels[inspector].onChanged = [this, inspector] {
for (const ID other : inspectorPanels)
if (other != inspector)
InvalidatePanel(other);
};
A tab group's "+" sits right after its last tab when the layout has a newTabMenu:
- newTabMenu(layout, leafNodeID) builds the menu's items: the panels that can be added.
- An item adds its panel with AddPanelToLeaf(layout.root.get(), leafNodeID, panelID), which appends it as a tab and selects it.
- A torn-off window's layout takes its home layout's newTabMenu.
- With documents, set DocumentSet::newTabMenu: every document's layout gets it.
layout.newTabMenu = [this](DockingLayout& in, ID leafNodeID) {
MenuItem("Log", [this, &in, leafNodeID] {
const ID log = CreatePanel(in.panels, "Log", IconID::Invalid, [this] { LogPanel(); });
AddPanelToLeaf(in.root.get(), leafNodeID, log);
});
};
To change the layout from code:
- FloatPanel(layout, panelID, screenPos) moves a panel into a window of its own.
- CloseDockedPanel(layout, leaf, tabIndex) closes a docked panel.
- ClosePanel(layout, panelID) closes a panel wherever it is in the layout.
- CloseFloatingDockWindow(index) closes a floating window and its panels.
- DisableAllPanels(layout) hides every panel, for example before a theme reload.
- ReleaseDockingLayout(layout) closes a whole layout for good: its panels, its torn-off windows, and its tree. Call it before you destroy a layout you made, or at exit.
A closed panel gives its ID back when CreatePanel made it (the overload without an ID). One you gave CreatePanel stays yours to release.
A panel can be closed from an input handler, such as a button in it. It's out of the layout at once, but its callbacks, and what they hold, are destroyed when the next frame starts. Until then, what the frame queued to draw (a viewport's scene, a custom draw) can still use them.
(void)FloatPanel(layout, log, int2{ styleManager.Px(200), styleManager.Px(200) });
Documents
Documents are the files the user edits, such as levels, prefabs or graphs. Each document has its own DockingLayout, with its own panels.
- A document tab bar sits under the title bar, with one tab per open document.
- The active document's panels fill the window below it.
- Only the active document's panels are built and accept drops. The others get onDisable.
Each tab shows:
- the document's icon
- its name
- its extension, muted ("Forest" + ".level")
- on the active or hovered tab, a close button
- for a dirty document, a dot in place of the close button, meaning it has unsaved changes
The tab colors come from the document's kind:
| Class | Colors |
| document-tab-<kind> | Tab tint; solid fill when active |
| document-name-<kind> | Icon |
| document-line-<kind> | Line under the bar (active document's color) |
- The themes define the kinds level, material, Layout, prefab and animation.
- Other kinds fall back to document-tab, document-name and document-line.
What the user can do:
- Drag a tab to reorder.
- Tabs that don't fit are hidden. The "N open" list names them all.
- Click "+" to call onNew.
- The "+" is hidden if onNew is unset.
- Close a document with its close button, "Close Document" in the context menu, or Ctrl+W.
- This calls onClose first. The document then closes in the next frame.
- If onClose returns false, the document stays open. Ask about unsaved changes there, and call CloseDocument yourself.
- Closing a document destroys its panels and their torn-off windows.
- Drag a tab off the bar.
- Dropped on another document bar, in any main window, the document moves there, at the spot of the placeholder tab shown during the drag.
- Dropped elsewhere, onTearOff gets the document, already removed from its set, and the screen position. Use them to open a new main window.
- Without onTearOff, the tab goes back.
- Document tabs drop only on document bars, panel tabs only on panel docks.
- A document dropped on panels is torn off.
- A panel dropped on a document bar floats.
- Drops apply at the start of the next frame.
documents.onNew = [this] {
Document& created = CreateDocument(documents, "Untitled", ".level", "level", levelIcon);
created.layout.root = MakeLeaf({ CreatePanel(created.layout.panels, "Scene", IconID::Invalid, [this] { ScenePanel(); }) });
documents.active = int(documents.documents.size()) - 1;
};
documents.onClose = [this](Document& document) {
if (!document.dirty)
return true;
closingDocument = &document;
return false;
};
Each main window is built by your app, in this order:
- the title bar and its menus
- Documents, for the window's DocumentSet
- the status bar
- EmitFloatingDockWindows(set), which builds the windows of the panels torn off from that set. They belong to that main window, and follow a document that moves to another one.
The bar can also go in the title bar, with the menus under it, so the menus sit with the document they act on:
- DocumentBar(id, set, theme.titleBarDocumentsStyleRef) draws only the bar: the tabs, "+" and "N open".
- Put it in the title bar, after its other items.
- The bar's empty space still drags the window.
- DocumentLine(set) draws the line in the active document's color. Call it right after EndTitleBar, so it runs under the whole title bar, not only under the tabs.
- In its own style, DocumentBar draws the line itself, under its tabs.
- DocumentPanels(set) draws the active document's panels, after the bar.
- Documents(id, set) is the two together, the bar on top.
BeginTitleBar(titleBarID);
Text(appIconID, "T", appIconStyle);
DocumentBar(documentsID, documents, theme.titleBarDocumentsStyleRef);
EndTitleBar();
DocumentLine(documents);
BeginMenuBar(menuBarID);
EndMenuBar();
DocumentPanels(documents);
- Close a main window when no documents are left in it.
- A set must stay alive until the end of the frame it was built in.
Saving:
- PersistDocuments(key, windows) takes a function that lists the main windows and their sets (DocumentWindowRef).
- SaveUIState then saves:
- each window's position
- the order of its documents
- its active document
- each document's name, extension, kind and dock tree
Restoring needs your app, since the library can't create your panels:
- After RestoreUIState, call TakeSavedDocuments(key).
- For each window:
- create its documents with their panels
- lay each one out with RestoreDocumentLayout
- pass it a makePanel, as for ApplyDockLayout, to make the panels a document was saved with that it doesn't have now, such as a second Viewport the user opened
- each of several panels of one kind gets its own place and its own state back
- set active
- For extra windows, call RestoreWindowPlace before declaring them.
PersistDocuments("documents", [this] { return std::vector<DocumentWindowRef>{ { mainWindowID, &documents } }; });
(void)RestoreUIState(savedText);
for (const SavedDocumentWindow& window : TakeSavedDocuments("documents"))
{
for (const SavedDocument& saved : window.documents)
{
Document& document = MakeMyDocument(documents, saved.name, saved.extension, saved.kind);
(void)RestoreDocumentLayout(document, saved, [this, &document](std::string_view type) -> std::optional<ID> {
return MakeMyPanel(document.layout, type);
});
}
documents.active = window.active;
}
Document& CreateDocument(DocumentSet& set, std::string name, std::string extension,
std::string kind, IconID icon);
void CloseDocument(DocumentSet& set, int index);
void CloseAllDocuments(DocumentSet& set);
void Documents(ID id, DocumentSet& set, LayoutOverride&& constraints = {});
void DocumentBar(ID id, DocumentSet& set, StyleRef style, LayoutOverride&& constraints = LayoutOverride::Auto());
void DocumentLine(const DocumentSet& set, LayoutOverride&& constraints = LayoutOverride::Auto());
void DocumentPanels(DocumentSet& set, LayoutOverride&& constraints = LayoutOverride::Auto());
void EmitFloatingDockWindows(const DocumentSet& documents);
- DocumentSet holds:
- the documents, in tab order
- active, the index of the shown document
- onNew, onClose and onTearOff
- Active() returns the shown document.
- The other CSS classes:
- documents
- document-bar
- document-row
- document-tab-bar
- document-extension
- document-mark
- document-close
- document-dirty
- document-new
- document-list
- title-bar-documents, for a bar in a title bar
Document& forest = CreateDocument(documents, "Forest", ".level", "level", levelIcon);
forest.layout.root = MakeLeaf({ CreatePanel(forest.layout.panels, "Scene", IconID::Invalid, [this] { ScenePanel(); }) });
Document& terrain = CreateDocument(documents, "Terrain", ".mat", "material", materialIcon);
terrain.layout.root = MakeLeaf({ CreatePanel(terrain.layout.panels, "Graph", IconID::Invalid, [this] { GraphPanel(); }) });
Documents(documentsID, documents);
EmitFloatingDockWindows(documents);
documents.onTearOff = [this](std::unique_ptr<Document> document, int2 at) {
auto& window = windows.emplace_back(std::make_unique<MyDocumentWindow>());
window->windowID = idManager.CreateStaticID();
window->position = at;
window->documents.onTearOff = documents.onTearOff;
window->documents.documents.push_back(std::move(document));
};
Layout presets
A layout preset is a named arrangement of a document's panels, such as "Scene" or "Graph". The user picks one for the document they're in, and the panels move to where the preset puts them.
- A preset is a dock tree as text.
- SaveDockLayout(layout) writes a layout's tree.
- Panels are named in it by their panelType, or by their title when they have none.
- Floating panels aren't in it.
- ApplyDockLayout(layout, tree, makePanel) lays a layout out as a tree says.
- A panel the tree names that the layout already has stays, and moves to its new place.
- A panel the tree names that the layout lacks is made by makePanel. It gets the panel's type or title, creates the panel in layout.panels, and returns its ID.
- With several panels of one kind in the tree, each is a panel of its own.
- For example, a tree with two Viewports applied to a layout with one keeps that one in the first place and makes a second for the other.
- A panel the layout has that the tree doesn't name is closed, as closing its tab does.
- It returns false, and changes nothing, when the tree can't be read or no panel is left.
- Call it outside the layout's Docking, for example at the start of the frame.
const std::string tree = SaveDockLayout(document.layout);
(void)ApplyDockLayout(other.layout, tree, [this, &other](std::string_view title) -> std::optional<ID> {
return MakeMyPanel(other.layout, title);
});
LayoutPresets keeps the presets:
- Built-in presets are the app's own.
- Add them with AddBuiltIn(name, tree).
- They come first, and can't be replaced or deleted.
- Saved presets are the user's.
- Save(name, tree) adds one, or replaces the saved one of that name.
- It returns false for an empty name, or the name of a built-in preset.
- Remove(name) deletes one.
- All() lists them all, built-in ones first, each in the order added.
- Find(name) returns the preset of that name, or null.
LayoutPresets presets;
presets.AddBuiltIn("Graph", graphTree);
presets.Save("My layout", SaveDockLayout(document.layout));
for (const LayoutPreset& preset : presets.All())
MenuItem(preset.name, [this, name = preset.name] { pendingPreset = name; });
A file type can open with a preset, so a file the user opens (a .mat file double-clicked in a file view, say) gets its type's layout:
- SetForFileType(".mat", "Graph") makes .mat documents open with "Graph".
- The extension's case doesn't matter.
- An empty name stops it.
- Deleting a saved preset stops the file types that used it.
- ForFileType(extension) returns that type's preset, or null when it has none.
Document& document = CreateDocument(documents, name, ".mat", "material", materialIcon);
if (const LayoutPreset* preset = presets.ForFileType(document.extension))
(void)ApplyDockLayout(document.layout, preset->tree, makePanel);
PersistLayoutPresets(key, presets) keeps the saved presets and the file types in the saved UI state. Add the built-in presets yourself, before or after RestoreUIState.
The showcase:
- Has a "Layouts" dropdown in its title bar (BeginDropdownMenu, see Menus).
- It lists the presets; picking one lays out the active document.
- "Save layout as..." asks for a name with a MessageDialog text field (see Tooltips and dialogs).
- "Delete layout" deletes a saved preset.
- Opens .level files with "Scene" and .mat files with "Graph": double-click one in the Files panel's documents folder (see GridViewVirtualState::onOpen in Trees and grids).
void AddBuiltIn(std::string name, std::string tree);
bool Save(std::string name, std::string tree);
bool Remove(std::string_view name);
const LayoutPreset* Find(std::string_view name) const;
const std::vector<LayoutPreset>& All() const;
void SetForFileType(std::string_view extension, std::string name);
const LayoutPreset* ForFileType(std::string_view extension) const;
std::string SaveDockLayout(const DockingLayout& layout) const;
bool ApplyDockLayout(DockingLayout& layout, std::string_view tree,
const std::function<std::optional<ID>(std::string_view key)>& makePanel = {});
void PersistLayoutPresets(std::string key, LayoutPresets& presets);
Toolbars
BeginToolbar / EndToolbar
A toolbar is a row of small buttons and fields for frequent actions. It wraps onto more rows when it runs out of room, or it can be a column.
Controls with their default style use their toolbar versions inside it:
- toolbar-button
- toolbar-toggle
- toolbar-textbox
- toolbar-number-field
- toolbar-menu-trigger
bool BeginToolbar(bool horizontal = true);
void EndToolbar();
BeginToolbar();
Button(saveIcon, [this] { Save(); });
SetTooltip("Save", "Ctrl S");
Toggle(snapToGrid, "Snap");
EndToolbar();
A toolbar standing as a column:
BeginToolbar(false);
Button(moveIcon, [this] { tool = Tool::Move; });
Button(rotateIcon, [this] { tool = Tool::Rotate; });
EndToolbar();
RenderToolbarDock
A free function, not a member of GUIApplication (controls_toolbar_dock.h). It places toolbars on the four sides of some content.
- The user drags toolbars between the sides, or tears them off into floating windows.
- Your app owns the lists of toolbars, and moves them in the callbacks. The dock never changes them itself.
void InitializeToolbarDockState(ToolbarDockState& state, GUIApplication& app);
void ReleaseToolbarDockState(ToolbarDockState& state, GUIApplication& app);
void RenderToolbarDock(GUIApplication& app, ToolbarDockState& state,
const std::vector<ToolbarItem> (&itemsPerSide)[kToolbarSideCount],
const ToolbarDockCallbacks& callbacks, std::function<void()> renderContent);
sides[int(ToolbarSide::Top)].push_back(ToolbarItem{ .id = idManager.CreateStaticID(), .debugName = "File",
.renderBody = [this] { Button(saveIcon, [this] { Save(); }); } });
RenderToolbarDock(*this, dockState, sides, callbacks, [this] { ViewportPanel(); });
callbacks.onReorder = [this](ToolbarSide side, int from, int to) {
std::vector<ToolbarItem>& items = sides[int(side)];
ToolbarItem moved = std::move(items[size_t(from)]);
items.erase(items.begin() + from);
items.insert(items.begin() + to, std::move(moved));
};
callbacks.onCrossSideDrop = [this](ToolbarSide from, int index, ToolbarSide to) {
sides[int(to)].push_back(std::move(sides[int(from)][size_t(index)]));
sides[int(from)].erase(sides[int(from)].begin() + index);
};
callbacks.onTearOff = [this](ToolbarSide from, int index, int2 at) {
floatingToolbars.push_back(FloatingToolbarItem{ .item = std::move(sides[int(from)][size_t(index)]),
.windowID = idManager.CreateStaticID(), .spawnPosition = at, .homeSide = from });
sides[int(from)].erase(sides[int(from)].begin() + index);
};
EmitFloatingToolbarWindows(floatingToolbars);
Previous: Tree and grid views | Controls | Next: Node view