Class Wm
The compositor, in the global wm. The lists, input, session lock, store and the config's own events all hang off it.
wm.store.launches = wm.store.launches or 0
wm.input:on("key", function(event)
if event.state ~= "Pressed" or not event.modifiers.super then return end
if event.raw_key == Key.r and event.modifiers.shift then
event:prevent_default()
wm:reload()
elseif event.raw_key == Key.t then
event:prevent_default()
wm.store.launches = wm.store.launches + 1
Process.spawn("foot")
end
end)
wm:on("ready", function()
print("reloaded:", wm.is_reload, "terminals started:", wm.store.launches)
end)Properties
wm.name: stringname read only
Always "Hazel".
wm.input: Inputinput read only
Seats, keyboards, pointers, and the key, pointer and gesture events. See Input.
wm.session_lock: SessionLocksession_lock read only
The lock screen. See SessionLock.
wm.windows: WindowListwindows read only
Every window, and the new_window event. See WindowList.
wm.layers: LayerListlayers read only
Panels, docks, wallpapers and launchers. See LayerList.
wm.outputs: OutputListoutputs read only
The monitors, or Hazel's own window when it runs nested. See OutputList.
wm.drawables: DrawableListdrawables
Drawables in global coordinates, the ones windows use. Each is drawn on every output it overlaps.
- Assigning a table replaces the whole list.
- A reload empties it.
wm.drawables = {
Drawable.rect({ left = 0, top = 0, width = 200, height = 4, fill = "#4c7899", z = 10 }),
}wm.is_reload: booleanis_reload read only
False for the config Hazel loads at startup. True after wm:reload() or a change to a file in the config directory.
Use it for things that should happen once per session, like starting a panel.
if not wm.is_reload then
wm.store.session_started_ms = Timer.now()
endwm.wayland_display: stringwayland_display read only
The socket name apps connect to, such as "wayland-1". Processes Hazel starts get it as WAYLAND_DISPLAY.
wm.is_session_locked: booleanis_session_locked read only
True from the moment a lock client asks to lock until it unlocks. That includes the short wait before every output shows the lock screen.
- Key handlers still run while it is true. A binding that shouldn't work on the lock screen checks it first.
wm.store: tablestore
A table that survives reloads. Keep state the next config needs here, such as which output a window was on.
- It holds booleans, numbers, strings, Points, Sizes, Rects and tables of those, up to 32 levels deep.
- The new config gets a copy: equal values, not the same tables.
WARNING
A store holding anything else, such as a function or a Window, stops the reload. The error is logged and the current config keeps running.
wm.store.arrange = wm.store.arrange or {}
local saved = wm.store.arrange
saved.outputs = saved.outputs or {}wm.instruction_budget: integerinstruction_budget
How many Lua instructions one call from Hazel into the config may run before it is stopped. See Instruction budget.
- Default: 500000. Each load starts at the default again.
Errors for less than 1000.
Methods
wm:screenshot(path: string, output: Output?, options: { cursor: boolean? }?) -> nilscreenshot
Writes a PNG of output. Without output, the main output.
- The image is in physical pixels, so a scaled output gives a bigger image than its area.
- The cursor is left out unless
cursor = true. - It returns before the file exists. A file that can't be written is reported in the log.
- Only works nested. On a tty nothing is written.
Errors for any option other than cursor.
wm:screenshot("/tmp/hazel-output.png", output, { cursor = true })wm:import_environment(variables: table) -> string[]import_environment
Sets environment variables in the systemd user manager and for D-Bus activation, for services started later. Returns the errors from the two, empty when both worked.
- It waits up to 2 seconds for each.
- On a tty, Hazel already sets
WAYLAND_DISPLAY,XDG_CURRENT_DESKTOPandXDG_SESSION_TYPEthis way. See the session environment.
Errors before setting anything when a key isn't a variable name: letters, digits and _, not starting with a digit.
local errors = wm:import_environment({ XDG_CURRENT_DESKTOP = "hazel" })
for _, problem in ipairs(errors) do print(problem) endwm:reload() -> nilreload
Loads the config again once the current handler has returned. unload fires on the old config, the new one loads, then ready fires on it.
- If the new config fails to load, the error is logged and the old one keeps running. Its
unloadhandlers have already run.
wm:quit() -> nilquit
Stops Hazel once the current handler has returned. unload does not fire.
- Processes started with
Process.spawnget SIGTERM, then SIGKILL half a second later if they are still running.
wm.input:on("key", function(event)
if event.raw_key == Key.Escape and event.modifiers.super and event.modifiers.shift then
wm:quit()
end
end)wm:emit(name: string, ...: any) -> nilemit
Fires an event of the config's own. The handlers added with wm:on(name, fn) get the arguments, then wm.
- The handlers run before
emitreturns. - One that raises is logged. The other handlers and the caller go on.
wm:on("layout_changed", function(layout)
print("layout is now", layout)
end)
wm:emit("layout_changed", "master")Events
Listen with wm:on(name, fn), which returns a function that stops listening.
wmis the last argument of every handler.- Handlers run in the order they were added, each with its own instruction budget.
- A handler that raises is logged. The rest still run.
- Any other name is a custom event, fired with
wm:emit.
"ready" (wm: Wm)ready
The config has loaded and has heard about everything that already exists: added for each output, new_window for each window, new_layer for each layer.
- Fires after every load, the first and each reload.
wm:on("ready", function()
for _, existing in ipairs(wm.windows:all()) do
print("window", existing.id, existing.app_id)
end
end)"unload" (wm: Wm)unload
A reload is about to replace this config. Anything the handler puts in wm.store reaches the new one.
"session_locked" (wm: Wm)session_locked
Every output now shows the lock screen.
"session_unlocked" (wm: Wm)session_unlocked
The lock client unlocked. No seat has keyboard focus, so give it back to a window here.
"tick" (wm: Wm)tick
Whenever something happened, and at least every 20 ms. For polling that has no event of its own. It runs very often, so keep it short.
local last_count = wm.windows:count()
wm:on("tick", function()
local count = wm.windows:count()
if count ~= last_count then
last_count = count
print("windows:", count)
end
end)"overrun" (info: OverrunInfo, wm: Wm)overrun
A call into the config ran over wm.instruction_budget and was stopped.
info.source:"config","timer","socket","process","clipboard", or the name of the event whose handler it was.info.traceback: where it was stopped.info.instructions: how many it ran, counted in steps of 1000.- The handler has a budget of its own. If it runs over too,
overrundoesn't fire again.
wm:on("overrun", function(info)
print("stopped", info.source, "after", info.instructions, "instructions")
end)