Bars and panels¶
A LayerWindow (0.5.8) is how an app makes a bar, a panel, a dock or an OSD: a window the
compositor places at a screen edge, above or below ordinary windows, optionally reserving
space so maximised windows stop short of it. It works only on a Wayland compositor that has
the layer shell (KDE's KWin and the wlroots compositors do; GNOME's Mutter, X11, Windows
and macOS do not), and raises tess_re.UnsupportedPlatform elsewhere. Every setting is in the
LayerWindow reference; this page is how they fit together.
A bar¶
from tess_re import App, LayerWindow
bar = LayerWindow(
"example.bar", # what it is for
layer="top", # above ordinary windows
anchor=("top", "left", "right"), # attached to the top edge, across the screen
size=(0, 32), # 0 = stretch between the anchored sides
exclusive_zone=32, # reserve 32 px: maximised windows stop below it
)
bar.root.set(fill=(0x1D, 0x1B, 0x20, 0xF0), flex_direction="horizontal",
align_items="center", padding_left=12, padding_right=12, gap=10)
bar.root.add_child(bar.create("text", text="hello", font_size=14,
fill=(255, 255, 255, 255),
width="fit-content", height="fit-content"))
app = App()
app.add_window(bar)
app.run()
The bar is an ordinary tree: nodes, listeners, timers (bar.every), animation, text.
examples/layer_bar.py adds a clock.
Where it goes¶
| You want | anchor |
size |
exclusive_zone |
|---|---|---|---|
| A bar across the top | ("top", "left", "right") |
(0, 32) |
32 |
| A bar across the bottom | ("bottom", "left", "right") |
(0, 40) |
40 |
| A sidebar on the left | ("left", "top", "bottom") |
(64, 0) |
64 |
| A dock, centred on the bottom | ("bottom",) |
(480, 56) |
56 |
| A clock in a corner, reserving nothing | ("top", "right") |
(120, 28) |
0 |
| An OSD in the middle of the screen | () |
(300, 80) |
0 |
A 0 on an axis stretches between the two edges anchored on it, so it needs both. A
positive zone reserves space only for a surface anchored to one edge, or to one edge and
both edges across it; the clock row above shows the other case, where the zone is 0. A
zone the protocol would ignore raises no error: a warning event says so.
margin=(top, right, bottom, left) moves it off the edge. layer="overlay" puts an OSD
above everything, fullscreen windows included; "bottom" and "background" are for
widgets and wallpapers under windows.
A clock in a corner: exclusive_edge¶
A surface anchored to one edge (or to one edge and both edges across it) reserves space on
that edge. A surface in a corner is anchored to two edges, and the compositor cannot tell
which one exclusive_zone is meant for, so it reserves nothing unless you name the edge:
clock = LayerWindow("example.clock", anchor=("top", "right"), size=(120, 28),
exclusive_zone=28, exclusive_edge="top") # maximised windows stop below it
Without exclusive_edge the same window floats over the corner and reserves nothing, and a
warning event says so. The edge must be one of the anchored ones, can be changed with set()
but not cleared once set, and needs layer-shell version 5 (KWin and recent wlroots compositors
have it; on version 4 the surface still opens and a warning event says the edge is ignored).
The reference has the details and what was checked on which
compositor.
Several screens¶
Leave output as None and the compositor picks one, and picks differently from run to
run. A shell that wants a bar on each screen reads the list and makes one LayerWindow per
Output, and follows screens being plugged in and out:
bars = {}
def add_bar(out):
bar = make_bar(output=out) # LayerWindow(..., output=out)
bars[out.name] = bar
app.add_window(bar)
for out in app.outputs(): # the screens as they are now, before run()
add_bar(out)
app.on("output_added", add_bar)
app.on("output_removed", lambda out: bars.pop(out.name, None)) # its bar closes itself
app.run()
A bar bound to a screen that is unplugged is closed by the compositor and fires closed with
reason "output_removed". output= also takes a connector name ("DP-1"); a name that is not
there makes App.run() raise RuntimeError listing the ones that are. Each bar's
output_changed event gives the name the compositor put it on.
Changing it while it runs¶
set() changes the layer settings in one commit:
bar.set(exclusive_zone=0) # stop reserving the space
bar.set(margin=(-32, 0, 0, 0), exclusive_zone=0) # slide it off the top edge (hidden)
bar.set(margin=0, exclusive_zone=32) # and back
A margin set this way takes effect as a step. To slide the surface, animate the margin in
the engine: bar.animate("margin_top", -32, duration_ms=300, on_complete=bar.close) moves it
with one small commit per step and no Python until on_complete, and set(margin=...)
cancels it. The surface's own size and exclusive_zone are step changes only. Animating what
is inside the surface (a node's opacity, translate_y, a width) works as in any window and
costs no protocol traffic. See the LayerWindow reference for
animate and for window_region="drag_margin", which lets the user drag a surface by its
margins.
The keyboard¶
keyboard="none" (the default) means no key events: a bar that only shows things should
not take the keyboard from the window the user is typing in. "on_demand" lets the user
focus it with a click, and it keeps keys until they click elsewhere; give the node that
should receive them focusable=True. "exclusive" takes the keyboard when the surface
opens and keeps it until it closes: for a lock screen or a launcher, not a bar.
Launching an app and giving it focus¶
A launcher on a bar starts programs, and a compositor that guards against focus stealing
(KWin does) keeps the new window in the background unless the launcher passes it an
activation token. Ask for one from the handler of the click or key that launches, and start
the program with it in XDG_ACTIVATION_TOKEN:
import os, subprocess
def launch(command):
def go(token):
env = dict(os.environ, **({"XDG_ACTIVATION_TOKEN": token} if token else {}))
subprocess.Popen(command, env=env)
bar.request_activation_token(go, app_id="org.example.app") # or any Window
button.on("click", lambda e: launch(["firefox"]))
callback runs on the loop's thread, with the token, or with None when there is none to
give (the compositor has no xdg_activation_v1, the window is not open, or the request
failed): launch without it then. The token names the last pointer press, key press or touch
of this app, which is what the compositor checks, so a request made long after the click is
refused. It works on a Window and a LayerWindow; app_id (the launched app's id, a
desktop-file name) is used for a LayerWindow only. Linux only (Wayland and X11); elsewhere
the callback gets None.
In tests¶
A LayerWindow is a Window, so simulate, advance and snapshot work with no
compositor and no display, and the headless size of a stretched axis is 1920 x 1080
unless resize says otherwise. The protocol's validation errors are raised in Python, so
they are testable the same way. Where there is a display but no layer shell the
constructor raises UnsupportedPlatform; tess_re.supports("layer_shell") tells a test
whether to skip.
Related¶
A menu or flyout that leaves the bar's own surface is a PopupWindow, a compositor popup
parented to the bar: see Menus from a bar. Nothing in the plan for
LayerWindow is outstanding: input_region (for a screen frame, a painter surface plus
reservers), margin animation and drag_margin are all built (see the
LayerWindow reference).