Skip to content

Class Window ​

An app's window, from wm.windows.

  • It exists before the app has drawn anything.
  • It starts at 0,0.

Where it goes and how it looks is up to the config, usually in a new_window handler:

lua
wm.windows:on("new_window", function(window)
	window.decoration = "server"
	window.box.corner_radius = 8
	window:on("first_content", function(window)
		local area = wm.outputs:get(1):usable_area()
		local size = window.size
		window.box.position = area:center() - Point(size.width / 2, size.height / 2)
		wm.input.seats:get(1):focus(window)
	end)
	window:on("closed", function(window)
		window.box.opacity = Anim.tween(0, 160)
	end)
end)

Two Window values for the same window are equal with ==.

Properties ​

window.box: WindowBoxbox read only ​

Where and how the window is drawn. See WindowBox.

lua
window.box.position = Anim.spring(Point(100, 80))
window.box.size = Anim.spring({ width = 800, height = 600 })
window:request_size({ width = 800, height = 600 })

window.size: Size?size read only ​

The size the app last drew at, without the shadows it draws around itself.

  • nil until the window has content.
  • The app picks it, so it may differ from what request_size asked for. resized fires when it changes.
lua
local drawn_size = window.size
local frame = drawn_size and Rect(window.box.position:target(), drawn_size)

window.min_size: Sizemin_size read only ​

The smallest size the app says it can be.

  • 0 on an axis means no limit. Both are 0 once the window is closed.
  • Hazel doesn't enforce it. A config that sizes windows can:
lua
local function within_limits(window, width, height)
	local smallest, largest = window.min_size, window.max_size
	width = math.max(width, smallest.width)
	height = math.max(height, smallest.height)
	if largest.width > 0 then width = math.min(width, largest.width) end
	if largest.height > 0 then height = math.min(height, largest.height) end
	return { width = width, height = height }
end

window:request_size(within_limits(window, 300, 200))

window.max_size: Sizemax_size read only ​

The largest size the app says it can be. Same rules as min_size.

window.decoration: "server" | "client" | "none"decoration ​

Who draws the title bar and borders. "server" asks the app to leave them out, "client" asks it to draw its own.

  • "none": no mode has been set.
  • Hazel draws no title bar. With "server", draw one with drawables.
  • Apps that don't support this setting ignore it.

Errors for any other value.

window.title: string?title read only ​

The title the app set.

  • nil until the app sets one.
  • After the window closes it still reads the last title.

window.app_id: string?app_id read only ​

The app's id, such as "foot" or "org.gnome.Nautilus".

  • nil until the app sets one.
  • After the window closes it still reads the last id.
lua
wm.windows:on("new_window", function(window)
	window:on("app_id_changed", function(window)
		if window.app_id == "mpv" then window.box.z = 1 end
	end)
end)

window.kind: "window"kind read only ​

Always "window". Layers read "layer", which tells the two apart in what wm.input:target_under and seat:focused() return.

lua
local target = wm.input:target_under(pointer:position())
if target and target.kind == "window" then target.box:raise() end

window.id: integerid read only ​

A number unique to the window for the whole session, never reused.

  • It survives reloads. Use it as a key in wm.store, which can't hold windows.

window.parent: Window?parent read only ​

The window this one belongs to, such as the main window of a dialog.

  • nil when there is none.

window.has_content: booleanhas_content read only ​

Whether the app has drawn anything yet. Turns true just before first_content and stays true.

window.focused_seats: Seat[]focused_seats read only ​

The seats whose keyboard focus is on the window, sorted by name. Empty when none.

window.states: WindowStatesstates read only ​

The states the app is told about, such as maximized or fullscreen. See WindowStates.

  • Whether the app draws itself as active is the "activated" state here.

window.drawables: DrawableListdrawables ​

Drawables that move and resize with the window's box. See DrawableList.

  • z of 0 or more is in front of the window and below its popups. Below 0 is behind it.
  • Assigning a table of drawables replaces them all.
  • They go when the window does, after its close animation.
lua
window.drawables:add(Drawable.rect({
	left = -2, right = -2, top = -2, bottom = -2,
	z = -1, radius = 10, fill = "#88c0d0ff",
}))

Methods ​

window:close() -> nilclose ​

Asks the app to close the window. The app may ask to save first, or ignore it.

  • The window is gone only when closed fires.
lua
wm.input:on("key", function(event)
	if event.state == "Pressed" and event.modifiers.super and event.raw_key == Key.q then
		local focused = seat:focused()
		if focused and focused.kind == "window" then focused:close() end
		event:prevent_default()
	end
end)

window:request_size(size: Size | Animated | nil) -> nilrequest_size ​

Tells the app what size to draw at. window.size reads what it drew, which may differ: a terminal keeps to whole cells.

  • nil lets the app pick its own size.
  • A new request replaces a running animated one.
  • An animated size is sent each time it changes. Wrap it in Anim.throttle to send fewer.
lua
window:request_size(Anim.throttle(Anim.spring({ width = 900, height = 600 }), 50))

window:on(event: string, fn: function) -> functionon ​

Listens for one of the events or requests.

  • Listeners are dropped after closed.

Errors for an unknown event name. The message lists the known ones.

Events ​

Listen with window:on(name, fn), which returns a function that stops listening. The window is always the last argument.

"first_content" (window: Window)first_content ​

The app drew for the first time. window.size is known from here on, so this is where to place the window.

  • After a reload it fires again for every window that has drawn, right after new_window.
lua
window:on("first_content", function(window)
	local area = wm.outputs:get(1):usable_area()
	window.box.position = Point(area.x + 40, area.y + 40)
end)

"resized" (window: Window)resized ​

window.size changed, on the app's own or in answer to request_size.

  • Not for moves, and not with the first content.
lua
window:on("resized", function(window)
	window.box.size = Anim.spring(window.size)
end)

"closed" (window: Window)closed ​

The app destroyed the window.

  • It is already gone from wm.windows.
  • Its listeners are dropped after this event.

Without animations it disappears at once, drawables too. If the handler starts animations on it:

  • its last frame stays on screen, without input, until they settle;
  • it keeps its place in the stack;
  • its drawables stay until then too.
lua
window:on("closed", function(window)
	local drawn = window.box.size:now()
	window.box.opacity = Anim.tween(0, 160)
	window.box.size = Anim.tween({ width = drawn.width * 0.9, height = drawn.height * 0.9 }, 160)
end)

"focus" (seat: Seat, focused: boolean, window: Window)focus ​

seat's keyboard focus moved onto the window (focused is true) or off it.

  • When focus moves between windows, the one it left hears first.
  • window.focused_seats is already up to date.

"title_changed" (window: Window)title_changed ​

The app set a new title.

lua
window:on("title_changed", function(window)
	print(window.title)
end)

"app_id_changed" (window: Window)app_id_changed ​

The app set a new app_id.

"parent_changed" (window: Window)parent_changed ​

The app set or cleared its parent.

"bell" (window: Window)bell ​

The app rang the bell for this window or one of its popups. Hazel makes no sound itself.

Requests ​

The app, or a taskbar, asks for something. Nothing happens unless the handler does it. Listen with window:on(name, fn), as for events.

"move_request" (request: WindowDragRequest, window: Window)move_request ​

The app asks to be dragged, usually because its title bar was pressed. See WindowDragRequest.

  • Only comes while a pointer button is held.
lua
window:on("move_request", function(request, window)
	local start = request.pointer:position()
	local origin = window.box.position:target()
	local stop
	stop = wm.input:on("pointer_move", function(event)
		if #event.pointer:buttons() == 0 then return stop() end
		window.box.position = origin + (event.position - start)
	end)
end)

"resize_request" (request: WindowDragRequest, window: Window)resize_request ​

The app asks to be resized by dragging an edge. request.edges says which.

  • Only comes while a pointer button is held.

"maximize_request" (window: Window)maximize_request ​

The app or a taskbar asks for the window to be maximized.

lua
window:on("maximize_request", function(window)
	local area = wm.outputs:get(1):usable_area()
	window.states:set("maximized", true)
	window.box.position = Point(area.x, area.y)
	window:request_size({ width = area.width, height = area.height })
end)

"unmaximize_request" (window: Window)unmaximize_request ​

The app or a taskbar asks to leave maximized.

"fullscreen_request" (output: Output?, window: Window)fullscreen_request ​

The app or a taskbar asks for fullscreen.

  • output is the output it asked for. nil when it left the choice to the config.

"unfullscreen_request" (window: Window)unfullscreen_request ​

The app or a taskbar asks to leave fullscreen.

"minimize_request" (window: Window)minimize_request ​

The app or a taskbar asks to be minimized. Hiding the window with box.visible shows it as minimized to taskbars.

lua
window:on("minimize_request", function(window)
	window.box.visible = false
end)

"unminimize_request" (window: Window)unminimize_request ​

A taskbar asks for a minimized window back. Apps can't send this one.

"activation_request" (token_age_ms: integer?, window: Window)activation_request ​

The app, or another app on its behalf, asks for the window to get focus.

  • token_age_ms is the age of the app's activation token. Old tokens come through too, so the handler decides what is too old.
  • nil when a taskbar asks.
lua
window:on("activation_request", function(token_age_ms, window)
	if token_age_ms == nil or token_age_ms < 3000 then
		seat:focus(window)
		window.box:raise()
	end
end)

"decoration_request" (window: Window)decoration_request ​

The app asked about decorations. The mode it wants isn't passed on.