UI Toolbox logoUI Toolbox
Support

Create windows

There are three ways to create a window. All three end in Workspace.RegisterWindow.

A Notes window with the default chrome: a title bar with the minimize, maximize and close buttons over the contentA Notes window with the default chrome: a title bar with the minimize, maximize and close buttons over the content

Video tutorial: Windows. All videos: Video tutorials.

Way Use it when
UXML Window The window is known at design time.
WindowDescriptor.Builder() The window is created at run time.
BuildAndRegister Same as the builder, in one call.

A registered window is closed until something opens it. Window ids must be unique in a workspace.

UXML: Window

Put Window elements inside a Workspace. The children of Window become the window content. On attach, the element registers the window and removes itself from the tree. It opens the window if the initial state is not closed.

In UI Builder, the element does not register. It draws the window with its title bar and its content at its initial position and size, so you can drop controls into it on the canvas.

<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:tb="UIToolbox">
  <tb:Workspace name="workspace" theme-name="dark" style="flex-grow: 1;">
    <tb:Window
        id="Inventory"
        title="Inventory"
        width="400"
        height="300"
        initial-window-state="open">
      <ui:Label text="Items go here." />
    </tb:Window>
  </tb:Workspace>
</ui:UXML>

The Windows deep dive (the Declarative scene) is the bridge of a starship made of UXML windows. Comms and Cargo start in a DockZone at the left, Helm and Sensors float as glass windows with x, y and template-uri, and the Manual window stays closed until its toolbar button opens it. A few lines of C# put Sensors and Tactical in one tab group, flash the Tactical tab with a count of the raiders, and minimize Helm on a red alert with a user style sheet above the theme.

Window attributes:

Attribute Property Default Meaning
id WindowId empty Unique window id. Required.
title WindowTitle empty Title. The id is used when it is empty.
width, height InitialWidth, InitialHeight 420, 320 Size in pixels. A size below min-width and min-height is raised to them.
min-width, min-height MinWidth, MinHeight 160, 120 The smallest size of the window. A resize with the mouse, the keyboard or a snap stops at it. A value below 160 by 120 takes 160 by 120. A tab group takes the largest minimum of its tabs.
placement Placement Auto Where the window opens the first time: Auto, Manual, Center, TopLeft, Top, TopRight, Left, Right, BottomLeft, Bottom or BottomRight. Auto puts the window in a free place, or uses x and y when they are set. See Where a new window opens. Center keeps the window in the middle at any screen size. After the first open, the window stays where the user moves it.
x, y InitialX, InitialY -1 Position in the viewport. With an edge or a corner placement, the distance from those edges; -1 takes the margin of 32 pixels. With a centered axis, the move from the center. -1 on both with Auto puts the window in a free place.
initial-window-state InitialWindowState closed A WindowState: open, closed, minimized or maximized. A window without this attribute stays closed until code or a toolbar button opens it.
template-uri TemplateUri null Chrome template. See Custom chrome templates.
enable-scrolling EnableScrolling true Wraps the content in a ScrollView. The content takes the window width, so text wraps, and it scrolls up and down. Content that cannot shrink to the window width, such as a child with a fixed width, also scrolls left and right. A child that grows with scale or moves with translate does not show a horizontal bar. With false, the content fills the window, so a footer can stay at the bottom and a child such as a PropertyGrid scrolls on its own.
resizable (old name can-resize) CanResize true Allows resize. A resizable window gets resize handles, template chrome included, unless the template has its own.
minimize-mode MinimizeMode Inherit What minimize does to this window: Inherit (takes the workspace), Collapse or Hide. See Minimize modes.
allow-merge AllowMerge Inherit Whether the window can merge with other windows into a tab group: Inherit (takes the workspace), Allow or Deny. A window that cannot merge is neither a merge source nor a merge target. See Turn merging off.
show-min-button, show-max-button, show-close-button ShowMinButton, ShowMaxButton, ShowCloseButton true Shows the window buttons. The Kingdom example has a window without a close button, one without a maximize button and one with only a close button.
category Category empty A name that groups windows. See Window templates.
icon Icon none A texture, a sprite, a render texture or a vector image that shows before the title, in the title bar and in the tab. It wins over icon-name. See Window icons.
icon-name IconName empty The icon by name: a name of InlineIconLabel.RegisterIcon or a PromptFont name, such as device-gamepad. See Window icons.
title-format TitleFormat empty Makes the title from the window data, such as {Name} ({Moons} moons). See Detail windows in UXML. Without data the window shows title.

Workspace attributes:

Attribute Property Default Meaning
auto-load-styles AutoLoadStyles true Loads WindowStyles.uss and the theme sheet.
theme-name ThemeName default Theme to load. See Theme API.
background-opacity BackgroundOpacity 1 Opacity of the theme background, from 0 to 1. 0 shows the game behind the workspace, for a HUD. A value between 0 and 1 dims the game, for example behind a pause menu. The windows keep their own backgrounds.
text-size TextSize Normal Text size: Normal, Large, Larger or Largest (100 to 200%). See Text size.
remember-window-states RememberWindowStates true Restores the session on attach. Saves it on state changes and on detach. Applies remembered bounds when a window opens.
animations-enabled AnimationsEnabled true Plays window transitions.
minimize-mode MinimizeMode Collapse What minimize does to the windows: Collapse rolls a window up to its title bar, Hide takes it out of the view. A window can set its own minimize-mode. See Minimize modes.
allow-merge AllowMerge true False gives no merge zone: windows cannot become tabs of one group by a drag. A window can set its own allow-merge. See Turn merging off.
layout-name LayoutName Default Key prefix for saved layouts and the session. Use one value per workspace.
layout-presets LayoutPresetsPath empty A Resources folder of layout JSON files. Each file is a preset. See Layout presets.
keyboard-shortcuts KeyboardShortcutsEnabled true Handles the window shortcuts. See Keyboard shortcuts.
key-step KeyStep 10 Pixels that one move or resize shortcut changes the focused window by.
dpad-crosses-windows DpadCrossesWindows false True: the D-pad and the left stick go on past the edge of a window to the nearest window. False: they stop at the edge, and LB and RB switch the windows. See Gamepad.
overview OverviewEnabled true A held window key shows every window in a grid to pick one. See Overview.
overview-duration OverviewDuration 200 The milliseconds that the windows take to move into the overview grid and back. 0 moves them at once.
window-mode WindowModeEnabled true View held or Ctrl+Alt+Space moves and resizes the focused window. See Window mode.
input-actions InputActions none Input System asset with the UIToolbox shortcut map. None uses the default asset. Only with the Input System package.
tooltips TooltipsEnabled true Shows tooltip text in runtime panels. See Tooltips.
tooltip-delay TooltipDelayMs 500 Milliseconds before a tooltip shows.
no-merge-modifier NoMergeModifier Shift The key that stops a merge during a window drag. See Tab groups and dock zones.
snap Snap true False turns off the snap of a window drag or resize: the window follows the pointer. See Snap and the snap keys.
no-snap-modifier NoSnapModifier Alt The key that stops the snap during a window drag or resize. See Snap and the snap keys.
align-snap-modifier AlignSnapModifier Control The key that snaps a window to the edge lines of windows that are far away.

A workspace keeps its registered windows when it leaves the panel and comes back, for a change of the UIDocument panel settings or a move to another parent. The windows that were open show again on the next update, with their bounds, their stacking order, the focused window, and the minimized and maximized states. Merged tab groups and dock tabs come back as groups. A dock zone makes its dock window again. A workspace that comes back with its windows does not read the saved session, so the session does not restore over them.

Window icons

A window can show an icon before its title, in its title bar and in its tab. A window without an icon has no icon element.

<tb:Window id="Controls" title="Controls" icon-name="device-gamepad" />
var window = workspace.GetWindow("Controls");
window.IconName = "device-keyboard";
window.Icon = Background.FromSprite(saveSprite);
window.Icon = default; // back to the name

A change shows at once and raises IconChanged. The title bar of a tab group shows the icon of the active tab, as it shows its title. A name that gets its picture from RegisterIcon after the window opens shows it at the next change of the icon or of the chrome. The icon is 16 by 16 pixels; style it with the tb-window-icon class. See Window and workspace classes.

To put more in the title bar than an icon, see Anything in the title bar.

Where a new window opens

A window with the Auto placement and no x or y opens in the first free place of the viewport: from the top left, along a row, then on the next row down. A free place keeps 16 pixels from the other open windows and 32 pixels from the edges of the viewport. When there is no free place, the window goes on a cascade step down and right from the top left: the first step that covers no title bar of another window. A step is 26 pixels, or the height of the title bars when they are taller. When every step covers a title bar, the window takes the step that covers the fewest and that is not on the top-left corner of another window, so no window hides another one exactly. A window that does not fit with the margin of 32 pixels gets a smaller margin, so it stays inside the viewport. In a workspace with dock zones, the free places and the cascade steps are between the docks, so a new window does not cover a dock. A window that is too wide or too tall for the space between the docks uses the full viewport on that axis.

The windows that open before the first layout, such as the open windows of a UXML file, take their places again when the workspace knows its size, so they open below the toolbar and beside each other. A window that code moves before then keeps its place.

The placement is a WindowPlacement. WindowPlacements.Resolve(placement, x, y) gives the placement that applies (Auto with x or y set is Manual), and WindowPlacements.Position(placement, x, y, size, viewport) gives the top-left corner of a window, or null for Auto.

To place the windows yourself, give them x and y or a placement such as Center or TopRight. Leave about 20 pixels between them, for the shadow of the chrome and for a close button that stands out of the corner, and check the places at the smallest screen that you support, such as 1280 by 720.

Code: the builder

WindowDescriptor.Builder() returns a WindowDescriptor.WindowDescriptorBuilder.

Method Default Meaning
WithId(string) none Required. Build throws ArgumentException without it.
WithTitle(string) the id Title.
WithIcon(Background) none Icon picture before the title. See Window icons.
WithIconName(string?) empty Icon by name. See Window icons.
WithSize(float, float) 160, 120 Initial size. Values below the minimum size are raised to it.
WithMinSize(float, float) 160, 120 The smallest size of the window. See the min-width attribute.
WithInitialPosition(float?, float?) null Position in the viewport. Null on both puts the window in a free place.
WithPlacement(WindowPlacement) Auto Where the window opens the first time. See the placement attribute.
WithContent(VisualElement?) null Window content.
WithDataSource(object?) null Data that the content binds to. See Data-bound windows.
WithTitleFromData(Func<object?, string>?) null Makes the title from the data. See Window templates.
WithCategory(string?) empty A name that groups windows.
WithInitialState(WindowState) Closed State applied on the first open.
WithTemplate(string?) null Chrome template.
WithEnableScrolling(bool) true Wraps the content in a ScrollView. See the enable-scrolling attribute.
WithRememberState(bool) true Remember the bounds and state of this window.
WithCanResize(bool) true Allows resize, and adds eight resize handles to the chrome unless the template has its own.
WithButtons(bool, bool, bool) true, true, true Shows the minimize, maximize and close buttons.
Build() Returns the descriptor.
BuildAndRegister(Workspace, bool) Builds, registers, and opens when the second argument is true.

Important:

using UIToolbox;
using UnityEngine;
using UnityEngine.UIElements;

public class InventoryWindow : MonoBehaviour
{
    private void Start()
    {
        var workspace = GetComponent<UIDocument>().rootVisualElement
            .Q<Workspace>();

        var content = new VisualElement();
        content.Add(new Label("Items go here."));

        var window = WindowDescriptor.Builder()
            .WithId("Inventory")
            .WithTitle("Inventory")
            .WithSize(400, 300)
            .WithContent(content)
            .Build();

        window.Closing += args =>
        {
            if (HasUnsavedChanges()) args.Cancel = true;
        };

        workspace.RegisterWindow(window, openImmediately: true);
    }

    private static bool HasUnsavedChanges() => false;
}

In the examples