7 min de lecturaEditar en GitHub

Esta página aún no está traducida. El texto siguiente está en inglés.

Custom JavaScript layouts

Tessera can evaluate a classic JavaScript layout in a separate QuickJS worker. A script defines a global layout() function and returns an object that supplies window frames. The worker implements a tiling-model-compatible API, with Tessera-specific validation and sandboxing. See Built-in layouts and Configuration for the native alternatives. See Platform support for supported systems, and Sandbox and troubleshooting for isolation details per system.

Install and select

Put a regular .js file directly in the layouts directory beside your configuration file:

  • Linux: ~/.config/tessera/layouts/
  • macOS: ~/Library/Application Support/Tessera/layouts/ (open it with Preferences > Layouts > Layouts folder > Open folder)
  • Windows: %APPDATA%\Tessera\layouts\

Tessera excludes symlinks, subdirectories, and uppercase .JS extensions during discovery. It discovers files at startup and uses the filename without .js as the action key. Tessera does not watch the directory automatically, so restart the controller after adding a file.

For the application action interface, use a filename stem containing only ASCII letters, digits, and hyphens. Tessera does not register other discovered names, such as my_layout.js, as GActions. For example, with two-columns.js present before the controller starts, run this command in the same session to activate select-custom-two-columns:

gapplication action app.tesseratiling.Tessera select-custom-two-columns

The layouts: YAML cycle accepts built-in keys, not custom keys. Tessera diagnoses an unknown custom key in that list and leaves it inert. The source checkout includes examples/layouts/two-columns.js, which you can copy into the layout directory. Tessera refuses an unknown key or unreadable file. When a custom layout fails during evaluation, Tessera reports the failure and leaves windows in place instead of falling back to a native arrangement.

Layout object

Property Meaning
name Optional display name.
initialState Optional initial JavaScript state. Prefer JSON-serializable values.
getFrameAssignments Function returning window IDs mapped to frames.
commands Optional object with command1–command4, expandMain, shrinkMain, increaseMain and decreaseMain handlers.
updateWithChange Optional function receiving a change and current state.
extends Optional built-in layout key whose computed frames the worker passes to the script.
recommendMainPaneRatio The worker exposes this hook, but the controller does not call it for custom layouts. Do not rely on it.

The worker evaluates the script as a classic script, not an ES module. It captures the result of layout() once per loaded context. A missing global function or invalid return is an error, and a missing getFrameAssignments produces no assignments. The worker JSON-decodes geometry arguments and JSON-clones truthy state, passing falsy state as undefined. Changes to those arguments do not mutate host state, so return your result explicitly.

Frames

getFrameAssignments(windows, screenFrame, state, extendedFrames) receives:

  • windows: tiled windows for the output, in host-provided order. Each entry has an opaque string id, current frame, and isFocused. Do not assume the ID encoding, focus order, or stacking order. Identity fields include applicationIdentity and optional appId, and role fields are windowRole and role. An optional coordinateSpace describes origin, rational scale, and rotation when available.
  • screenFrame: { x, y, width, height } in global coordinates. On X11 these are root-window pixels, so another output can have a nonzero or negative origin.
  • state: initial or updated state. See below.
  • extendedFrames: undefined when no extended placements exist, otherwise an array of { id, frame } from the named built-in layout.

Return an object keyed by IDs received in windows:

{ "42": { x: 0, y: 0, width: 800, height: 600, isMain: true } }

Every frame must include all four numeric fields. isMain is optional and defaults to true. unconstrainedDimension may be "horizontal" or "vertical", defaulting to "horizontal". An omitted window gets no assignment. The worker rejects unknown IDs, malformed frames, non-finite values, non-positive sizes, unrepresentable values, and out-of-screen frames. The worker rounds frame edges to pixels and clips at most one rounded pixel of edge overflow. The window manager may still apply a different effective frame due to its own constraints.

State, commands and events

updateWithChange(change, state) returns the new state. A command is an object with updateState(state, focusedWindowID). The worker omits the second argument if no window has focus. Return the new state, because a null or undefined handler result leaves the prior state unchanged. Missing handlers are no-ops. Geometry evaluation receives queued changes before its next frame calculation, and events can wait while outputs have no windows. The bounded queue coalesces focus changes, and an overflow replaces the backlog with layout_change and the overflowing event. Treat layout_change as a reason to rebuild from the next window list.

The event kind is in change.change, and change.type is an alias. Change kinds include add, remove, focus_changed, window_swap, application_activate, application_deactivate, space_change, layout_change, tab_change, unknown, and none. Window-related changes use windowID, and swaps also use otherWindowID.

// Examples: { change: "focus_changed", type: "focus_changed", windowID: "42" }
//           { change: "window_swap", type: "window_swap", windowID: "42", otherWindowID: "43" }
updateWithChange: function (change, state) {
    if (change.change === "focus_changed") {
        return { lastFocused: change.windowID };
    }
    return state;
}

The worker can retain its JavaScript context between evaluations, but an interrupted or restarted worker or a heap-exhaustion reset can discard module-level variables. Do not rely on module-level variables surviving failure. Derive placement from the current inputs wherever possible. The host passes state returned by handlers between restarts, so do not assume a worker restart discards the host’s state snapshot.

Complete example

This is examples/layouts/two-columns.js from the source checkout without its header. One window fills the screen. With more windows, the first takes the left column and the rest stack on the right, separated by a one-pixel gap.

function layout() {
    var GAP = 1;

    return {
        name: "Two Columns",

        initialState: {},

        // Called with (windows, screenFrame, state, extendedFrames). Only the
        // first two arguments are used here; extra arguments are ignored.
        getFrameAssignments: function (windows, screenFrame) {
            var frames = {};

            if (!windows || windows.length === 0) {
                return frames;
            }

            var x = Math.round(screenFrame.x);
            var y = Math.round(screenFrame.y);
            var width = Math.round(screenFrame.width);
            var height = Math.round(screenFrame.height);

            // Degenerate screen: hand back the screen frame unchanged rather
            // than producing negative widths.
            if (screenFrame.width <= GAP || height <= 0) {
                frames[windows[0].id] = { x: x, y: y, width: width, height: height };
                return frames;
            }

            if (windows.length === 1) {
                frames[windows[0].id] = { x: x, y: y, width: width, height: height };
                return frames;
            }

            var usable = width - GAP;
            var leftWidth = Math.floor(usable / 2);
            var rightWidth = usable - leftWidth;
            var rightX = x + leftWidth + GAP;

            frames[windows[0].id] = {
                x: x,
                y: y,
                width: leftWidth,
                height: height,
                isMain: true
            };

            var column = windows.slice(1);
            var count = column.length;
            var slotHeight = Math.floor(height / count);
            var offset = 0;

            for (var i = 0; i < count; i += 1) {
                // The last window absorbs the rounding remainder so the column
                // always ends exactly at the bottom edge of the screen frame.
                var frameHeight = i === count - 1 ? height - offset : slotHeight;

                frames[column[i].id] = {
                    x: rightX,
                    y: y + offset,
                    width: rightWidth,
                    height: frameHeight,
                    isMain: false,
                    unconstrainedDimension: "horizontal"
                };

                offset += slotHeight;
            }

            return frames;
        }
    };
}

Sandbox and troubleshooting

The worker exposes no module loader, filesystem, process, network, or shell API to JavaScript. require, import, and export are not supported. console.log exists, but discards its messages. QuickJS uses a 32 MiB heap limit, a 1 MiB stack limit, and an evaluation deadline. These are implementation limits, not promises of future budgets. A timeout interrupts evaluation and can restart the worker. On Linux, Tessera installs Landlock and seccomp confinement before reading scripts. If the sandbox cannot start, execution fails closed with a diagnostic and no unconfined fallback. The worker log reports partial Landlock enforcement.

Isolation by system:

System What isolates the script
Linux (every profile) Landlock and seccomp, installed before reading the script. Needs Linux 5.13 or newer with the landlock security module on, and x86_64, aarch64 or riscv64. Without them the worker refuses to run.
macOS Deny-all Seatbelt profile ((version 1)(deny default)) via sandbox_init, installed before reading the script. The worker also runs with a cleared environment, the engine’s heap, stack, and time limits, and a kill by the main program.
Windows No platform sandbox yet. The worker runs with a cleared environment, the engine’s heap, stack, and time limits, and a kill by the main program.

Linux and macOS apply an operating-system sandbox before reading scripts. On Windows, confinement relies on the engine, with no operating-system sandbox behind it. Profile pages: macOS, Windows.

If a layout is missing, check its directory, lowercase extension, and selection action. For a JavaScript error, check that layout is global and returns an object. For invalid geometry, check every returned ID and finite positive frame against the screen bounds. If windows are not placed, check that the result includes their IDs. For timeouts, bound loops and recursion. A missing command handler does nothing, so check that the action is bound and the handler returns state.

From a source checkout, node --check examples/layouts/two-columns.js checks JavaScript syntax if you have Node installed. It does not execute the layout in Tessera or check sandbox behavior.