UI Toolbox logoUI Toolbox

How it works

This page gives the model behind the windows. Read it before you extend the system.

The parts

Part Role
Workspace The root control. It owns the windows, the theme, the layouts and the drag hints.
WindowDescriptor One window: its id, its title, its content, its state and its bounds.
Chrome The frame of a window: the title bar, the buttons and the resize handles. It comes from a UXML template.
Host The place that shows a window: the workspace for a standalone window, or a tab group for a tab.
WorkspaceToolbar, DockZone Optional children of the workspace: a bar of buttons, and a docked tab group at an edge.

Window states

WindowDescriptor.State is one of Closed, Open, Minimized, Maximized and Restored.

Call Result
OpenWindowAsync(id) Opens the window. If it is open, it gets the focus. The first open applies the initial state.
MinimizeAsync, MaximizeAsync, RestoreAsync Change the state. The bounds from before a maximize come back on restore.
CloseAsync Raises Closing. A handler can cancel. Then the window closes and its chrome goes away.

See Control a window.

Bounds

Positions are relative to the viewport of the workspace. An WorkspaceToolbar makes the viewport smaller at the top.

Value Meaning
Current bounds The bounds that you asked for (WindowDescriptor.CurrentBounds).
Restore bounds The bounds from before a maximize (WindowDescriptor.RestoreBounds).
Remembered bounds The bounds that the window gets when it opens again, while RememberWindowStates is true.

The workspace clamps the bounds to the viewport.

Chrome

The chrome of a window comes from the first of these that works:

  1. The custom template of the window.
  2. The default window template of the package.
  3. A chrome that the chrome factory builds in code.

The workspace adds the same tb-* classes to each chrome, so one style sheet applies to all templates. See Custom chrome templates and Window and workspace classes.

Drag and drop

Animations

Animations are USS transitions. The code adds a class, and USS does the motion. So a theme can change each animation with no code. When Workspace.AnimationsEnabled is false, each change applies at once. See Animations.

Async rules

  1. Call the API on the Unity main thread only. UI Toolkit is not thread safe.
  2. The state and the focus change at once when an async method starts. The Task completes when the animation ends.
  3. OpenWindow, CloseWindow, FocusWindow and SetBounds are synchronous. OpenWindow and CloseWindow start the async version and do not wait.
  4. Do not use async void. For a call that you do not await, use AsyncRunner.Run. It logs each exception.
  5. Each async method takes a CancellationToken. A cancel ends the animation at its end state.
  6. Closing handlers run synchronously. Do not start long work in them.
using UIToolbox;
using UIToolbox.Utils;

public static class OpenExample
{
    public static void OpenFromButton(Workspace workspace)
    {
        AsyncRunner.Run(() => workspace.OpenWindowAsync("Inventory"), "Open Inventory");
    }
}