Skip to content

App

The top-level entry point — opens and drives one or more Windows together.

App

App(scheme="auto")

Constructs an empty App with no registered windows. (0.6.11) scheme is the colour scheme every window follows that sets none of its own: "light", "dark" or "auto" (the default: the OS appearance, light while it is unknown). app.set(scheme=...) changes it live (every following window repaints) and app.get("scheme") reads it; a bad value or an unknown key raises ValueError and changes nothing.

app = App()
app = App(scheme="dark")
app.set(scheme="light")

add_window

add_window(window)

Registers a Window to be opened the next time run() is called. Calling this multiple times before run() opens multiple windows together.

Since 0.5.6, it also works while run() is going: from a listener, a frame handler or LoopHandle.call_soon. The window opens on the loop's next turn. Closing it leaves the other windows running, and closing the last one ends the run (App.run returns). Adding a window that is already open raises ValueError; a window that was closed may be added again. If the new window's GPU can't be set up it closes by itself and the error is logged (the call that added it has already returned). A window still waiting to open when the last open one closes is lost with the run. max_frames applies to the new window on its own.

app.add_window(window)

run

run(max_frames=None, keep_alive=False)

The single blocking call that opens every registered window and ticks, lays out, and renders each window's own tree every frame, independently, until every window has closed. It raises RuntimeError if no window was added.

app.run()
app.run(max_frames=60)  # each window individually stops after 60 frames

Staying up with no window: keep_alive (0.5.9)

A desktop shell has to stay up when no window should exist (every monitor is set to show nothing, or the last output was unplugged) and still react when an output comes back. run(keep_alive=True) keeps the loop running with no window open:

  • It does not raise when no window was added, and it does not return when the last window closes (including a bar the compositor closed because its output was unplugged).
  • App.on("output_added" | "output_removed" | "output_updated", ...) keep arriving, including after the last output was unplugged and a new one plugged in, and LoopHandle.call_soon callbacks run: a window can be added from either (app.add_window(LayerWindow(...)) from an output_added handler opens on that output).
  • It costs nothing while idle: no frames, no timers. Measured with no window, 5 s used 0.01 s of CPU, and none of the engine's own threads woke.
  • Only LoopHandle.stop() ends it (below). With it, max_frames no longer ends the run when its windows are done.
  • Where there is no display run() still returns at once, as it always did. Off Wayland there are no output events (nothing to watch), but the loop, call_soon and stop() work.
app = App()
handle = app.thread_handle()

def on_output(output):
    app.add_window(make_bar(output))            # one bar per screen, as it appears
app.on("output_added", on_output)
for output in app.outputs():
    app.add_window(make_bar(output))

app.run(keep_alive=True)                         # returns only after handle.stop()

Quitting on a signal. Python runs a signal handler only when the main thread executes Python, and an idle loop does not (a handler set with signal.signal runs late, when something else gives the main thread work). Use signal.set_wakeup_fd, which the C-level handler writes to at once, and a watcher thread that calls stop():

import signal, socket, threading

reader, writer = socket.socketpair()
writer.setblocking(False)
signal.set_wakeup_fd(writer.fileno())
for sig in (signal.SIGTERM, signal.SIGINT):
    signal.signal(sig, lambda *_: None)           # a handler must exist for the fd to be written

def watch():
    reader.recv(8)                                 # a signal arrived
    handle.stop()
threading.Thread(target=watch, daemon=True).start()
app.run(keep_alive=True)
  • max_frames, when given, is a per-window budget, not a whole-app one — useful for headless/CI runs that must end on their own.
  • Raises RuntimeError if called with zero registered windows.
  • Delivers real input — pointer, wheel, keyboard, text and input-method composition, window events, and screen-reader requests — through the same pipeline window.simulate uses, invoking the listeners it reaches.
  • If no display is available, returns None without running, so headless scripts and CI can call it unconditionally.
  • Raises RuntimeError if a window's GPU can't be set up -- no GPU adapter, no device, or a surface the adapter can't drive. (Before 0.4.0 this exited the process with status 0.)
  • Installs a tracing log subscriber as early as possible — set RUST_LOG to control verbosity (e.g. RUST_LOG=warn python app.py).

outputs

outputs() -> list[Output] (0.5.8) — the screens the compositor has, top to bottom and left to right, read now, so it works before run() (a shell decides how many bars to make first). Each is an Output. The list is empty where the platform cannot say: on anything but Wayland, and where the compositor does not report connector names.

for out in app.outputs():
    app.add_window(make_bar(output=out))

on, off and simulate

on(event, handler), off(event), simulate(event, **fields) (0.5.8) — listeners for changes in the set of screens: output_added, output_removed and output_updated. A handler takes the Output (or nothing). There is one handler per event and a second on replaces it. See Outputs. simulate delivers an event as the compositor would, for tests with no display.

connection_lost (0.6.10)

app.on("connection_lost", handler) calls handler() (no argument) once when the compositor goes away while run() is going: it was killed, crashed or its socket closed. The call comes from inside run(), before anything else happens, so the app is still its own: save state, record why, start a timer. Then every window that was open hears closed with the reason "connection_lost" and run() returns normally (before 0.6.10 it returned with nothing said; the windows heard nothing). Not called for a stop(), for a run() that never found a display, or for a window the app closed itself. off("connection_lost") removes the handler; a handler that raises is logged.

To wait for a compositor to come back, ask tess_re.supports("layer_shell", refresh=True) until it is true, then build the windows and run() again (layer surfaces).

GPU memory

Windows share one GPU device (0.5.9, #220), and a device asks the driver's allocator for memory in blocks. tess_re asks for small blocks (8 to 64 MiB, 4 to 32 MiB for host-visible ones), so a small window holds a few tens of MiB of video memory (31 MiB for a 200 x 100 window on the machine it was measured on). wgpu's own default reserves 192 MiB the moment a device exists, whatever is then put in it, and that was tess_re's behaviour before 0.5.8's follow-up (#219): set TESS_RE_GPU_MEMORY=performance in the environment before the app starts to get it back (any other value keeps the small blocks). A large image or a big window still gets the memory it needs either way; the setting only changes how much is reserved in advance. See GPU memory per window for the measurements.

One device for every window (0.5.9, #220). The first window makes the wgpu instance, adapter and device, and every later window (ordinary, layer surface or popup) draws with them, so it holds only its own swapchain, render target and renderer: five 200 x 100 windows hold the 31 MiB one does (they held 83 MiB), and open a little faster. Two things follow. A GPU error is reported to every window on the device, once each, because wgpu does not say which window's draw caused it; and a lost device is lost for all of them, so every window's gpu_lost listener hears it before the run ends. A window whose surface the shared adapter cannot present to (a second GPU) gets a device of its own. Set TESS_RE_GPU_DEVICES=per-window before the app starts for a device per window again (shared, or anything else, is the default).

One renderer too (0.5.9, #235). Windows on a shared device also share one vello_gpu renderer (its pipelines, glyph atlas and gradient cache) per surface format, and the bundled fonts are not copied for each window. A renderer cost about 4 MiB and 9 ms to make, and the fonts about 3 MiB, for every window. On the machine measured, 50 small windows hold 98 MiB of process memory (they held 310 with a renderer each, and 532 with a device each), and a window adds about 0.14 MiB, not 4.3. A window's own text layouts, images, shader passes, kept frame and damage stay its own. Set TESS_RE_GPU_RENDERERS=per-window for a renderer per window again (the device stays shared). frame_stats()["memory"] reports the device, so with a shared device every window reads the same numbers (shared_device says which case it is).

thread_handle

thread_handle() -> LoopHandle

App, Window, Node, and every other Tesserae Engine object may only be used from the thread that created them — using one from another thread raises PanicException (a BaseException, not an Exception). Dropping one there is safe: if Python's garbage collector frees a Tesserae Engine object on a background thread, the actual free happens on its own thread at the next frame or Tesserae Engine call. thread_handle() returns the one object a background thread may use: a LoopHandle onto this App's event loop. Every handle from one App shares the same queue.

handle = app.thread_handle()
threading.Thread(target=watch_files, args=(handle,), daemon=True).start()
app.run()

LoopHandle

stop (0.5.9)

stop() -> bool

Ends the running loop from any thread. Every open window is closed without asking: a stop is a shutdown, so no close_requested is sent and a listener cannot keep a window open. Each window hears closed with the reason "stopped" (on any window, not only a LayerWindow), and run() returns normally.

  • Returns True if a loop was running and has been asked to end, False if none was (before run() or after it returned). Nothing is remembered: a stop() called early never ends a later run.
  • Works with or without keep_alive, with or without windows open.
  • Safe from any thread. Like call_soon, it is the one tess_re call a worker thread may make.

call_soon

call_soon(callback)

Queues callback (called with no arguments) to run on the App's event-loop thread and wakes the loop — including an idle one waiting for input. There it can touch a Window and its Nodes exactly like an input handler can:

# on a background thread, after detecting a file change:
text = path.read_text()                              # I/O off the UI thread
handle.call_soon(lambda: rebuild_screen(text))           # on the loop
  • Safe from any thread, before, during, or after run().
  • Callbacks run in the order they were queued, at the top of the next frame. With no window open (a keep_alive loop, 0.5.9) there is no frame, so they run as soon as the loop wakes. One queued before run() runs on the first frame; one queued after run() returns waits for a later run().
  • A callback that itself calls call_soon doesn't run again in the same frame — its new entry runs on the next one.
  • An exception is logged the same way as one from an input handler and doesn't stop the loop or later callbacks.
  • A callback that changes any window's tree gets that window redrawn, even if another window's frame ran it.
  • Raises TypeError if callback isn't callable.

examples/threadsafe_reload.py is a complete, runnable file watcher built on this.