Screens, areas and regions
Blender's resizable, splittable, dockable editor layout in every language. Drag the edges between areas, drag a corner to split, join or dock, maximize with Ctrl+Space; regions (headers, toolbars, sidebars) resize, hide and flip; panels reorder and pin. Ported from Blender's editors/screen.
Blender’s window is a screen of areas, each showing one editor, and each area is made of regions: a header, a toolbar, a sidebar, the main view. BUI has the same thing, ported from Blender’s editors/screen (screen_geometry.cc, screen_edit.cc, area.cc, screen_ops.cc, screen_draw.cc), so it behaves like Blender in every language.
The layout lives on Blender’s graph of shared vertices and edges, not on nested boxes. That is what makes joining, docking and moving several aligned edges at once possible.
What the user can do
| Do this | And this happens | Blender’s operator |
|---|---|---|
| Drag the edge between two areas | Both resize; the edge snaps near its limits | SCREEN_OT_area_move |
| … with Shift | Every edge aligned with it moves too | the same, extending |
| … with Ctrl | It snaps to 24ths of the screen and to other areas’ edges | the same, SNAP_FRACTION_AND_ADJACENT |
| Escape or right-click while dragging | The edge goes back | the same, cancel |
| Drag an area’s corner inward | The area splits; hold Ctrl during the drag (not at the press, which swaps) to snap to 12ths and other edges | SCREEN_OT_area_join (split) |
| Drag a corner into a neighbor | The two areas join | the same (join) |
| Drag a corner into any other area | The area docks beside it (left, right, top, bottom) or replaces it (center), with a preview; Ctrl and Alt adjust the size | the same (dock) |
| Drag a corner out of the window | The host is asked to open the area in a window of its own | the same (new window) |
| Ctrl-drag a corner | Two areas swap places | SCREEN_OT_area_swap |
| Shift-drag a corner | The host is asked to open a copy in a new window | SCREEN_OT_area_dupli |
| Right-click an edge | Vertical/Horizontal Split, Join, Swap Areas, Merge Edge | SCREEN_OT_area_options |
| Right-click a header (not in Rust and JavaScript) | Hide Header, Flip to Bottom, Move/Split Area, Maximize Area, Focus Mode, Duplicate Area into New Window, Close Area | the header context menu |
| Click the header’s left corner | The same area options | the touch menu |
| Ctrl+Space / Ctrl+Alt+Space | Maximize the area under the pointer / focus mode (regions hidden); again restores | SCREEN_OT_screen_full_area |
| Drag a region’s inner edge | The region resizes (toolbars snap to their columns, sidebars to their tabs) | SCREEN_OT_region_scale |
| … all the way to the side | The region hides, leaving a small tab; click the tab or drag it out to show it again | the same |
| Press a region’s hotkey | The region toggles (N for a sidebar, T for a toolbar, as you set them) | SCREEN_OT_region_toggle |
| Drag a panel’s grip | Panels reorder (the host hears where it went) | panel dragging |
| Shift+click a panel header in a tabbed sidebar | The panel is pinned: it shows under every tab | PNL_PIN |
Areas are drawn as in Blender: rounded corners, the theme’s editor border (editor_border, editor_outline, editor_outline_active), the highlight on the edge being dragged, and Blender’s previews for splitting, joining and docking.
Regions
An area’s children are its regions and one main view. A region is a header, tool_header, footer, asset_shelf, channels, nav_bar, toolbar, sidebar, execute or hud (the floating Adjust Last Operation panel), one of each, laid out in Blender’s order with Blender’s defaults:
| Region | Side | Default size | Notes |
|---|---|---|---|
| header, tool_header, footer, execute | top/bottom | the header height | can’t be resized, only hidden and flipped |
| toolbar | left/right | one toolbar column | snaps to 1, 2 or 2.7 columns; drawn over the main view |
| sidebar | left/right | 280 | tabs for panel categories; drawn over the main view; narrow, it keeps only its tabs |
| channels, nav_bar | left/right | 200 / 160 | |
| asset_shelf | top/bottom | 120 | drawn over the main view |
| hud | floating | its content’s size | at the lower left of the main view |
Each region takes these properties: size (unscaled pixels across the area), hidden, side, overlap (drawn over the main view or beside it), resizable (false: only hides), maxsize and hotkey. The user resizing, hiding or flipping it is reported to the region (BK_EV_RESIZE: the size and whether it is hidden).
An area works on its own too, outside a screen: a panel with a header and sidebars that resize and hide.
Areas the user makes
Splitting, docking and restoring a layout make areas. Each area has an editor name; a new area shows the editor of the one it came from, and the screen asks the host to fill it (BK_AREA_NEW) once the operation is done, as Blender queues its notifiers, so the handler may change the screen again. The bindings do this for you from a table of editors: one factory per editor name, which also lists the header’s editor-type menu.
Saving and restoring
The screen’s layout property is its arrangement as JSON: the vertices, each area’s corners and editor, and each region’s side, size and hidden state. Store it when the screen reports a change and set it back to restore: the screen makes or closes areas to match, and the vertices scale to the screen’s size.
The screen reports a change once the edit is done, as Blender queues its notifiers: an area you remove yourself is gone from the layout you save then. An area that is closing (its close handler is running, or one further down the chain) refuses split, join, dock and close, so close handlers that close each other close every area once.
In your language
Every example below is a whole file: the editors, a layout kept in layout.json between runs, and a window. The first run uses the starting layout; every change the user makes is saved, and the next run restores it.
Rust
use blenderui::prelude::*;use blenderui::screen::{AreaRef, AreaSplit, Editor, Editors, Region, Screen, ScreenEvent};
const LAYOUT_FILE: &str = "layout.json";
fn editors() -> Editors { Editors::new() .editor("3D Viewport", "VIEW3D", |_area: &AreaRef| { Editor::new(div().child("viewport")) .header(div().child("View")) .toolbar(div()) .sidebar(Region::new(div().child("Item")).size(280).hotkey("n") .on_resize(|e, _, _| println!("{} {}", e.size, e.hidden))) }) .editor("Outliner", "OUTLINER", |area: &AreaRef| Editor::new(div().child(area.editor().to_owned())))}
struct Layout { saved: Option<String>,}
impl Render for Layout { fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { rsx! { <body m-0 min-h-screen> <Screen editors={editors()} layout={AreaSplit::v(0.75, "3D Viewport", "Outliner")} saved={self.saved.as_deref()} on_change={|e: &ScreenEvent, _, _| { let _ = std::fs::write(LAYOUT_FILE, e.layout()); }} /> </body> } }}
fn main() { Application::new().run(|cx| { let opts = WindowOptions { window_bounds: Some(WindowBounds::Windowed(Bounds { origin: point(px(0.0), px(0.0)), size: size(px(900.0), px(600.0)), })), ..Default::default() }; let saved = std::fs::read_to_string(LAYOUT_FILE).ok(); cx.open_window(opts, |_, cx| cx.new(|_| Layout { saved })).unwrap(); });}use blenderui::prelude::*;use blenderui::screen::{AreaRef, AreaSplit, Editor, Editors, Region, Screen, ScreenEvent};
const LAYOUT_FILE: &str = "layout.json";
fn editors() -> Editors { Editors::new() .editor("3D Viewport", "VIEW3D", |_area: &AreaRef| { Editor::new(div().child("viewport")) .header(div().child("View")) .toolbar(div()) .sidebar(Region::new(div().child("Item")).size(280).hotkey("n") .on_resize(|e, _, _| println!("{} {}", e.size, e.hidden))) }) .editor("Outliner", "OUTLINER", |area: &AreaRef| Editor::new(div().child(area.editor().to_owned())))}
struct Layout { saved: Option<String>,}
impl Render for Layout { fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { body().m_0().css("min-height", "100vh").child( Screen() .editors(editors()) .layout(AreaSplit::v(0.75, "3D Viewport", "Outliner")) .saved(self.saved.as_deref()) .on_change(|e: &ScreenEvent, _, _| { let _ = std::fs::write(LAYOUT_FILE, e.layout()); }), ) }}
fn main() { Application::new().run(|cx| { let opts = WindowOptions { window_bounds: Some(WindowBounds::Windowed(Bounds { origin: point(px(0.0), px(0.0)), size: size(px(900.0), px(600.0)), })), ..Default::default() }; let saved = std::fs::read_to_string(LAYOUT_FILE).ok(); cx.open_window(opts, |_, cx| cx.new(|_| Layout { saved })).unwrap(); });}Region contents are your HTML elements: the kit lays the areas and regions out, and each region’s elements are placed at its rect (they follow one render behind, as container queries do, and live during a drag). AreaRef (passed to each editor’s factory) has editor(), split, join, dock, swap, close, maximize, focus and restore. on_window gets the editor’s name of an area dragged out of the window; the window itself is yours to open.
The other languages
import fs from 'node:fs'import { render, Screen, Editor, Region, AreaSplit } from '@blender-ui/react' // Vue and Svelte: the same components
const LAYOUT_FILE = 'layout.json'const saved = fs.existsSync(LAYOUT_FILE) ? fs.readFileSync(LAYOUT_FILE, 'utf8') : undefined
const editors = { '3D Viewport': { icon: 'VIEW3D', render: area => <Editor header={<div>View</div>} toolbar={<div />} sidebar={<Region size={280} hotkey="n" onResize={e => console.log(e)}><div>Item</div></Region>}> <div>viewport</div> </Editor> }, Outliner: { icon: 'OUTLINER', render: area => <Editor><div>{area.editor}</div></Editor> },}
const { window } = render( <Screen editors={editors} layout={AreaSplit.v(0.75, '3D Viewport', 'Outliner')} saved={saved} onChange={e => fs.writeFileSync(LAYOUT_FILE, e.layout)} onWindow={e => console.log('open a window for', e.editor)} />, { title: 'Screens', width: 900, height: 600 })window.body.style = 'margin: 0'Run it as in JavaScript (bui build screen.jsx, or bundle it for Android).
The native DOM API has createScreen(document, { editors, layout, saved, onChange, onWindow }) and editorElement(document, { main, header, sidebar: { element, size, hotkey, onResize } }). An area keeps its editor element (and a text field’s draft) while the user drags an edge, and keeps its component state in React, Vue and Svelte while its neighbors are split, joined or closed; areas that close are released. JavaScript has no area operations (split, join, dock) yet: the user does them.
import os
from blenderui.components import (App, AreaSplit, Column, Editor, Label, MenuButton, NodeEditor, NumberSlider, Panel, Region, Screen, Sidebar)
LAYOUT_FILE = "layout.json"
def load(): if os.path.exists(LAYOUT_FILE): with open(LAYOUT_FILE, encoding="utf-8") as f: return f.read() return None
def save(layout): with open(LAYOUT_FILE, "w", encoding="utf-8") as f: f.write(layout)
def open_window(area): print("the user dragged", area.editor, "out of the window") # a host opens its own window here
def viewport(area): return Editor(NodeEditor(), # the main view: any component header=[MenuButton("View", ["Frame All"])], toolbar=Column(), sidebar=Region(Sidebar(Panel("Transform", NumberSlider("X", 0.0), category="Item")), size=280, hotkey="n", on_resize=lambda size, hidden: print(size, hidden)))
def outliner(area): return Editor(Column(Label(area.editor)), header=[])
screen = Screen({"3D Viewport": viewport, "Outliner": outliner, "Properties": outliner}, AreaSplit("v", 0.75, "3D Viewport", AreaSplit("h", 0.4, "Outliner", "Properties")), saved=load(), on_change=save, on_window=open_window)
# Areas can be rearranged from code too, once the screen is shown (screen.areas):# view = screen.areas[0]# right = view.split("v", 0.3) # the screen fills it with view's editor# right.dock(view, "left", 0.25) # "left", "right", "top", "bottom", "center"# view.maximize(); view.restore()
if __name__ == "__main__": App("Blender-like", (1280, 800), screen, padding=0).run()AreaSplit(axis, factor, first, second): "v" puts first on the left, "h" puts it on top, first taking factor. An Area("Outliner", body, header=[...], sidebar=...) also works on its own. Panel(..., pinned=True, on_move=lambda old, new: ..., on_pin=...).
Qt’s QSplitter and tkinter’s PanedWindow are screens that only let the user move their edges, so they drag like Blender’s areas; setSizes, sizes, splitterMoved, sash_coord and sash_place work as before.
#include <stdio.h>#include <stdlib.h>#include <string.h>
#include "BUI_kit.h"
/* what an area shows: a header, a sidebar, a toolbar and the main view */static void fill(BKNode *area){ BKNode *header = BK_node_new("areaheader"); BKNode *name = BK_node_new("label"); BK_set_str(name, "text", BK_get_str(area, "editor")); BK_node_add(header, name, -1); BK_node_add(area, header, -1);
BKNode *side = BK_node_new("sidebar"); BK_set_num(side, "size", 280); BK_set_str(side, "hotkey", "n"); BK_node_add(area, side, -1);
BKNode *tools = BK_node_new("column"); BK_set_str(tools, "region", "toolbar"); BK_node_add(area, tools, -1);
BK_node_add(area, BK_node_new("column"), -1); /* the one child that isn't a region */}
/* the whole file, or NULL */static char *read_file(const char *path){ FILE *f = fopen(path, "rb"); if (!f) { return NULL; } fseek(f, 0, SEEK_END); const long n = ftell(f); rewind(f); char *text = n >= 0 ? malloc((size_t)n + 1) : NULL; if (text) { text[fread(text, 1, (size_t)n, f)] = '\0'; } fclose(f); return text;}
static void on_event(void *data, BKNode *node, int kind, void *ref, double a, double b){ if (kind == BK_EV_AREA && (int)b == BK_AREA_NEW) { fill(BK_node_child(node, (int)a)); /* the screen made an area (a split, a dock, a restore) */ } else if (kind == BK_EV_CHANGE && strcmp(BK_node_type(node), "screen") == 0) { FILE *f = fopen("layout.json", "wb"); /* the layout, kept between runs */ if (f) { fputs(BK_get_str(node, "layout"), f); fclose(f); } }}
int main(void){ BKNode *screen = BK_node_new("screen"); BKNode *view = BK_node_new("area"); BK_set_str(view, "editor", "3D Viewport"); BK_node_add(screen, view, -1); /* the first area fills the screen */ BKNode *props = BK_area_split(view, BK_AXIS_V, 0.75f); /* the right quarter */ BK_set_str(props, "editor", "Properties"); BKNode *outliner = BK_area_split(props, BK_AXIS_H, 0.6f); /* its top part */ BK_set_str(outliner, "editor", "Outliner"); for (int i = 0; i < BK_node_child_count(screen); i++) { fill(BK_node_child(screen, i)); } /* BK_area_dock(outliner, view, BK_DOCK_LEFT, 0.25f); and BK_set_str(view, "full", "maximized") * ("", "maximized", "focus") rearrange areas from code */
BKContext *ctx = BK_context_new(screen); BKHost host = {0}; host.emit = on_event; BK_context_set_host(ctx, &host); char *saved = read_file("layout.json"); /* the last run's layout: the areas it makes are filled */ if (saved) { BK_set_str(screen, "layout", saved); /* 0 for a malformed one: the starting layout stays */ free(saved); }
BKWindow *win = BK_window_new(ctx, "Screens", 900, 600); BK_window_show(win); BK_window_run(); BK_window_free(win); BK_context_free(ctx); BK_node_free(screen); return 0;}Events: BK_EV_AREA with BK_AREA_NEW on the screen (a = the new area’s index: fill it for its "editor"), BK_AREA_CLOSE and BK_AREA_WINDOW on the area; BK_EV_CHANGE on the screen; BK_EV_RESIZE on regions; BK_EV_MOVE on panels. Setters return 0 for what they refuse: a second region of a type, a side the type can’t be on, a second main view, a malformed layout. To restore a saved layout, BK_set_str(screen, "layout", text) before showing the window.
local bui = require("bui")
local function fill(area) local header = bui.Node("areaheader") local name = bui.Node("label") name:set("text", area:get("editor")) header:add(name) area:add(header) local side = bui.Node("sidebar") side:set("size", 280) side:set("hotkey", "n") assert(not side:set("side", "top")) -- set returns false for what it refuses area:add(side) area:add(bui.Node("column")) -- the main viewend
local screen = bui.Node("screen")local view = bui.Node("area")view:set("editor", "3D Viewport")screen:add(view)local props = view:split("v", 0.75)props:set("editor", "Properties")for _, area in ipairs(screen:children()) do fill(area) end
screen:on(bui.EV_AREA, function(self, index, what) if what == bui.AREA_NEW then fill(self:child(index)) endend)screen:on(bui.EV_CHANGE, function(self) -- the layout, kept between runs local f = io.open("layout.json", "w") f:write(self:get("layout")) f:close()end)
local ctx = bui.Context(screen)local saved = io.open("layout.json") -- the last run's layout: the areas it makes are filledif saved then screen:set("layout", saved:read("a")) -- false for a malformed one: the starting layout stays saved:close()endlocal win = bui.Window(ctx, "Screens", 900, 600)win:show()bui.run()A closed area’s wrapper becomes stale (node:type() is "(freed)"), never dangling. A close handler cannot free() the area it is told about (or its screen), nor remove() it or add() it anywhere: the screen detaches and frees it right after.
import bui;import std.file : exists, readText, write;
void fill(Node area) { auto header = new Node("areaheader"); header.add(new Node("label").set("text", area.str("editor"))); area.add(header); area.add(new Node("sidebar").set("size", 280.0).set("hotkey", "n")); area.add(new Node("column")); // the main view}
void main() { auto screen = new Node("screen"); auto view = new Node("area"); view.set("editor", "3D Viewport"); screen.add(view); auto props = view.split('v', 0.75f); props.set("editor", "Properties"); foreach (i; 0 .. screen.childCount) { fill(screen.child(i)); } screen.onArea((NodeEvent e) { if (cast(int) e.b == BK_AREA_NEW) { fill(screen.child(cast(int) e.a)); } }); screen.onChange((NodeEvent e) { write("layout.json", e.node.str("layout")); }); // kept between runs
auto context = new Context(screen); if (exists("layout.json")) { // the last run's layout: the areas it makes are filled screen.set("layout", readText("layout.json")); // a malformed one is refused: the starting layout stays } auto window = new Window(context, "Screens", 900, 600); window.show(); run(); window.free(); context.free(); screen.free();}A D-owned node inside an area keeps its handlers however the area leaves the screen: closed, joined, docked away, restored over or by the user’s drag. A close handler may free() such a node. A D-owned area that closes takes its nodes’ handlers, as remove() does, so a close button whose handler holds the area does not keep it alive. The screen says an area left through its Context: close areas of a screen that has one.
What isn’t there
- Workspaces: several screens with tabs above them.
- Global areas: Blender’s top bar and status bar that snap between two sizes.
- Moving the HUD: the floating region stays at its corner.
- Header-less panels and Blender’s floating panel drag (the dragged panel follows the pointer while the others slide): panels reorder as the grip crosses them.
- JavaScript area operations (split, join, dock from code), and in Rust and JavaScript the header’s right-click menu (the corner and edge menus work).
- Blender’s dedicated cursors (split, swap, the arrows) show as the nearest kit cursor.
Next steps
The window, the view and the two Rust syntaxes the screen example above uses.
A panel in Python, then try examples/screen_demo.py, a full Blender-like layout.
What goes inside the regions: Blender’s sliders, panels and menus.
Last updated