Skip to content

Layouts ​

Tiling in Hazel is a function you write: it takes the free area of an output and the windows on it, and returns a rectangle for each. These recipes build one up, then add more layouts and workspaces.

All of them build on your first config.

Tiling ​

Windows tile in a dwindle layout, as in Hyprland: each new window takes half of the space the last one had. They spring into place when the layout changes.

Dialogs, and apps in float_app_ids, stay floating. mod + Shift + Space floats or tiles the focused window.

lua
local gap = 8
local float_app_ids = { ["org.pulseaudio.pavucontrol"] = true }
local spring = { stiffness = 300, damping_ratio = 0.85 }

wm.store.tiling = wm.store.tiling or { layout = "dwindle", order = {}, outputs = {}, floating = {} }
local tiling = wm.store.tiling

local layouts = {}

function layouts.dwindle(area, windows, gap)
	local rects, rest = {}, area
	for index = 1, #windows - 1 do
		local axis = rest.width >= rest.height and "horizontal" or "vertical"
		rects[index], rest = rest:split(axis, 0.5, gap)
	end
	rects[#windows] = rest
	return rects
end

local function is_tiled(window)
	local id = tostring(window.id)
	return window.has_content and window.box.visible and window.parent == nil
		and not window.states:has("fullscreen") and not window.states:has("maximized")
		and not tiling.floating[id] and not float_app_ids[window.app_id or ""]
end

local function tiled_output(window)
	local name = tiling.outputs[tostring(window.id)]
	return name and wm.outputs:name(name) or output_of(window)
end

local function tiled_windows(output)
	local by_id = {}
	for _, window in ipairs(wm.windows:all()) do by_id[tostring(window.id)] = window end
	local windows = {}
	for _, id in ipairs(tiling.order) do
		local window = by_id[id]
		if window and is_tiled(window) and tiled_output(window) == output then
			windows[#windows + 1] = window
		end
	end
	return windows
end

local offscreen = {}

local function place(window, rect, output)
	local x, y = math.floor(rect.x), math.floor(rect.y)
	local size = { width = math.floor(rect.x + rect.width) - x, height = math.floor(rect.y + rect.height) - y }
	window.box.position = Anim.spring(Point(x, y), spring)
	window.box.size = Anim.spring(size, spring)
	window:request_size(size)
	local outside = not output:area():contains(Point(x + size.width / 2, y + size.height / 2))
	if outside ~= (offscreen[window.id] or false) then
		offscreen[window.id] = outside
		window.box.opacity = Anim.tween(outside and 0 or 1, 150)
		window.box.input = not outside
	end
end

local function arrange()
	for _, output in ipairs(wm.outputs:all()) do
		local windows = tiled_windows(output)
		if #windows > 0 then
			local rects = layouts[tiling.layout](output:usable_area():inset(gap), windows, gap, output)
			for index, window in ipairs(windows) do place(window, rects[index], output) end
		end
	end
end

wm:on("arrange", arrange)
wm:on("ready", arrange)
wm.outputs:on("changed", arrange)
wm.outputs:on("removed", function() Timer.after(0, arrange) end)

wm.layers:on("new_layer", function(layer)
	layer:on("first_content", arrange)
	layer:on("changed", arrange)
	layer:on("closed", arrange)
end)

wm.windows:on("new_window", function(window)
	local id = tostring(window.id)
	window:on("first_content", function(window)
		if not tiling.outputs[id] then
			tiling.order[#tiling.order + 1] = id
			tiling.outputs[id] = output_of(window).name
		end
		arrange()
	end)
	window:on("closed", function(window)
		for index, other in ipairs(tiling.order) do
			if other == id then table.remove(tiling.order, index) break end
		end
		tiling.outputs[id], tiling.floating[id], offscreen[window.id] = nil, nil, nil
		arrange()
	end)
	window:on("resized", function(window)
		if is_tiled(window) then window.box.size = Anim.spring(window.size, spring) end
	end)
end)

bind(mod .. " shift", "space", function()
	local window = focused_window()
	if not window then return end
	local id = tostring(window.id)
	tiling.floating[id] = not tiling.floating[id] or nil
	if tiling.floating[id] then
		window.box.size = nil
		window.box:raise()
	end
	arrange()
end)
  • wm.store keeps the order and the floating windows through a reload, so saving the config doesn't reshuffle anything.
  • The layout gets the usable area, which leaves out the space bars like waybar reserve. Layer events re-tile when a bar appears or changes size.
  • box.size scales the window while the spring runs. Once the app has drawn at its new size, resized makes the box follow it, so text stays sharp.
  • Other recipes re-tile with wm:emit("arrange").
  • When a monitor is unplugged, its tiled windows join the first output once it is gone. Timer.after(0, ...) waits for that.

Moving around the layout ​

mod + J and K focus the next and previous tiled window. Add Shift to swap the focused window with it instead.

Needs Tiling.

lua
local function neighbour(window, step)
	local windows = tiled_windows(tiled_output(window))
	for index, other in ipairs(windows) do
		if other == window then return windows[(index - 1 + step) % #windows + 1] end
	end
end

local function swap_in_order(a, b)
	local first, second
	for index, id in ipairs(tiling.order) do
		if id == tostring(a.id) then first = index end
		if id == tostring(b.id) then second = index end
	end
	tiling.order[first], tiling.order[second] = tiling.order[second], tiling.order[first]
end

for key, step in pairs({ j = 1, k = -1 }) do
	bind(mod, key, function()
		local window = focused_window()
		local next_window = window and neighbour(window, step)
		if next_window then focus(next_window) end
	end)
	bind(mod .. " shift", key, function()
		local window = focused_window()
		local next_window = window and neighbour(window, step)
		if next_window and next_window ~= window then
			swap_in_order(window, next_window)
			wm:emit("arrange")
		end
	end)
end

A master layout, and switching ​

A big window on the left and a stack on the right. mod + Space switches each layout in turn.

Needs Tiling. A layout is one more function in layouts, so you can add your own the same way.

lua
local master_ratio = 0.55

function layouts.master(area, windows, gap)
	if #windows == 1 then return { area } end
	local main, stack = area:split("horizontal", master_ratio, gap)
	local rects = { main }
	local count = #windows - 1
	local height = (stack.height - gap * (count - 1)) / count
	for index = 1, count do
		rects[index + 1] = Rect(stack.x, stack.y + (index - 1) * (height + gap), stack.width, height)
	end
	return rects
end

local layout_names = { "dwindle", "master" }

bind(mod, "space", function()
	for index, name in ipairs(layout_names) do
		if name == tiling.layout then
			tiling.layout = layout_names[index % #layout_names + 1]
			break
		end
	end
	wm:emit("arrange")
	wm:emit("layout_changed", tiling.layout)
end)

Scrolling, like niri ​

Windows sit side by side in a strip as wide as it needs to be. The strip scrolls to keep the focused window in view. Windows scrolled past the edge fade out and stop taking clicks.

Needs Tiling. To try it, set tiling.layout = "scrolling", or add it to layout_names if you have the master recipe.

lua
local column_width = 0.5
local scroll_offsets = {}

function layouts.scrolling(area, windows, gap, output)
	local width = math.floor(area.width * column_width - gap / 2)
	local focused = focused_window()
	local offset = scroll_offsets[output.name] or 0
	for index, window in ipairs(windows) do
		if window == focused then
			local left = (index - 1) * (width + gap)
			if left < offset then offset = left end
			if left + width > offset + area.width then offset = left + width - area.width end
		end
	end
	scroll_offsets[output.name] = offset
	local rects = {}
	for index = 1, #windows do
		rects[index] = Rect(area.x + (index - 1) * (width + gap) - offset, area.y, width, area.height)
	end
	return rects
end

wm.windows:on("new_window", function(window)
	window:on("focus", function(_, focused)
		if focused and tiling.layout == "scrolling" then wm:emit("arrange") end
	end)
end)

The Moving around keys move through the strip too.

Workspaces ​

Nine workspaces shared by every output. mod + 1 to 9 switches, mod + Shift + 1 to 9 sends the focused window there.

Works with or without Tiling: a window on another workspace is hidden, and tiling skips hidden windows.

lua
wm.store.workspaces = wm.store.workspaces or { current = 1, of_window = {} }
local workspaces = wm.store.workspaces

local function show_workspace(number)
	workspaces.current = number
	for _, window in ipairs(wm.windows:all()) do
		local home = workspaces.of_window[tostring(window.id)] or 1
		window.box.visible = home == number
	end
	wm:emit("arrange")
	wm:emit("workspace_changed", number)
	if not focused_window() then focus_topmost() end
end

wm:on("show_workspace", show_workspace)

wm.windows:on("new_window", function(window)
	local id = tostring(window.id)
	workspaces.of_window[id] = workspaces.of_window[id] or workspaces.current
	window.box.visible = workspaces.of_window[id] == workspaces.current
	window:on("closed", function() workspaces.of_window[id] = nil end)
end)

for number = 1, 9 do
	bind(mod, tostring(number), function() show_workspace(number) end)
	bind(mod .. " shift", tostring(number), function()
		local window = focused_window()
		if not window or number == workspaces.current then return end
		workspaces.of_window[tostring(window.id)] = number
		window.box.visible = false
		wm:emit("arrange")
		focus_topmost()
	end)
end

Other recipes switch with wm:emit("show_workspace", number), and hear about a switch with wm:on("workspace_changed", fn).

Key["1"] is the 1 key on the main row. On layouts where that row gives other symbols without Shift, such as AZERTY, use those names instead: "ampersand", "eacute" and so on.

All of them ​

The recipes on this page in one block, under your first config:

lua
local gap = 8
local float_app_ids = { ["org.pulseaudio.pavucontrol"] = true }
local spring = { stiffness = 300, damping_ratio = 0.85 }

wm.store.tiling = wm.store.tiling or { layout = "dwindle", order = {}, outputs = {}, floating = {} }
local tiling = wm.store.tiling

local layouts = {}

function layouts.dwindle(area, windows, gap)
	local rects, rest = {}, area
	for index = 1, #windows - 1 do
		local axis = rest.width >= rest.height and "horizontal" or "vertical"
		rects[index], rest = rest:split(axis, 0.5, gap)
	end
	rects[#windows] = rest
	return rects
end

local function is_tiled(window)
	local id = tostring(window.id)
	return window.has_content and window.box.visible and window.parent == nil
		and not window.states:has("fullscreen") and not window.states:has("maximized")
		and not tiling.floating[id] and not float_app_ids[window.app_id or ""]
end

local function tiled_output(window)
	local name = tiling.outputs[tostring(window.id)]
	return name and wm.outputs:name(name) or output_of(window)
end

local function tiled_windows(output)
	local by_id = {}
	for _, window in ipairs(wm.windows:all()) do by_id[tostring(window.id)] = window end
	local windows = {}
	for _, id in ipairs(tiling.order) do
		local window = by_id[id]
		if window and is_tiled(window) and tiled_output(window) == output then
			windows[#windows + 1] = window
		end
	end
	return windows
end

local offscreen = {}

local function place(window, rect, output)
	local x, y = math.floor(rect.x), math.floor(rect.y)
	local size = { width = math.floor(rect.x + rect.width) - x, height = math.floor(rect.y + rect.height) - y }
	window.box.position = Anim.spring(Point(x, y), spring)
	window.box.size = Anim.spring(size, spring)
	window:request_size(size)
	local outside = not output:area():contains(Point(x + size.width / 2, y + size.height / 2))
	if outside ~= (offscreen[window.id] or false) then
		offscreen[window.id] = outside
		window.box.opacity = Anim.tween(outside and 0 or 1, 150)
		window.box.input = not outside
	end
end

local function arrange()
	for _, output in ipairs(wm.outputs:all()) do
		local windows = tiled_windows(output)
		if #windows > 0 then
			local rects = layouts[tiling.layout](output:usable_area():inset(gap), windows, gap, output)
			for index, window in ipairs(windows) do place(window, rects[index], output) end
		end
	end
end

wm:on("arrange", arrange)
wm:on("ready", arrange)
wm.outputs:on("changed", arrange)
wm.outputs:on("removed", function() Timer.after(0, arrange) end)

wm.layers:on("new_layer", function(layer)
	layer:on("first_content", arrange)
	layer:on("changed", arrange)
	layer:on("closed", arrange)
end)

wm.windows:on("new_window", function(window)
	local id = tostring(window.id)
	window:on("first_content", function(window)
		if not tiling.outputs[id] then
			tiling.order[#tiling.order + 1] = id
			tiling.outputs[id] = output_of(window).name
		end
		arrange()
	end)
	window:on("closed", function(window)
		for index, other in ipairs(tiling.order) do
			if other == id then table.remove(tiling.order, index) break end
		end
		tiling.outputs[id], tiling.floating[id], offscreen[window.id] = nil, nil, nil
		arrange()
	end)
	window:on("resized", function(window)
		if is_tiled(window) then window.box.size = Anim.spring(window.size, spring) end
	end)
end)

bind(mod .. " shift", "space", function()
	local window = focused_window()
	if not window then return end
	local id = tostring(window.id)
	tiling.floating[id] = not tiling.floating[id] or nil
	if tiling.floating[id] then
		window.box.size = nil
		window.box:raise()
	end
	arrange()
end)

local function neighbour(window, step)
	local windows = tiled_windows(tiled_output(window))
	for index, other in ipairs(windows) do
		if other == window then return windows[(index - 1 + step) % #windows + 1] end
	end
end

local function swap_in_order(a, b)
	local first, second
	for index, id in ipairs(tiling.order) do
		if id == tostring(a.id) then first = index end
		if id == tostring(b.id) then second = index end
	end
	tiling.order[first], tiling.order[second] = tiling.order[second], tiling.order[first]
end

for key, step in pairs({ j = 1, k = -1 }) do
	bind(mod, key, function()
		local window = focused_window()
		local next_window = window and neighbour(window, step)
		if next_window then focus(next_window) end
	end)
	bind(mod .. " shift", key, function()
		local window = focused_window()
		local next_window = window and neighbour(window, step)
		if next_window and next_window ~= window then
			swap_in_order(window, next_window)
			wm:emit("arrange")
		end
	end)
end

local master_ratio = 0.55

function layouts.master(area, windows, gap)
	if #windows == 1 then return { area } end
	local main, stack = area:split("horizontal", master_ratio, gap)
	local rects = { main }
	local count = #windows - 1
	local height = (stack.height - gap * (count - 1)) / count
	for index = 1, count do
		rects[index + 1] = Rect(stack.x, stack.y + (index - 1) * (height + gap), stack.width, height)
	end
	return rects
end

local layout_names = { "dwindle", "master" }

bind(mod, "space", function()
	for index, name in ipairs(layout_names) do
		if name == tiling.layout then
			tiling.layout = layout_names[index % #layout_names + 1]
			break
		end
	end
	wm:emit("arrange")
	wm:emit("layout_changed", tiling.layout)
end)

local column_width = 0.5
local scroll_offsets = {}

function layouts.scrolling(area, windows, gap, output)
	local width = math.floor(area.width * column_width - gap / 2)
	local focused = focused_window()
	local offset = scroll_offsets[output.name] or 0
	for index, window in ipairs(windows) do
		if window == focused then
			local left = (index - 1) * (width + gap)
			if left < offset then offset = left end
			if left + width > offset + area.width then offset = left + width - area.width end
		end
	end
	scroll_offsets[output.name] = offset
	local rects = {}
	for index = 1, #windows do
		rects[index] = Rect(area.x + (index - 1) * (width + gap) - offset, area.y, width, area.height)
	end
	return rects
end

wm.windows:on("new_window", function(window)
	window:on("focus", function(_, focused)
		if focused and tiling.layout == "scrolling" then wm:emit("arrange") end
	end)
end)

wm.store.workspaces = wm.store.workspaces or { current = 1, of_window = {} }
local workspaces = wm.store.workspaces

local function show_workspace(number)
	workspaces.current = number
	for _, window in ipairs(wm.windows:all()) do
		local home = workspaces.of_window[tostring(window.id)] or 1
		window.box.visible = home == number
	end
	wm:emit("arrange")
	wm:emit("workspace_changed", number)
	if not focused_window() then focus_topmost() end
end

wm:on("show_workspace", show_workspace)

wm.windows:on("new_window", function(window)
	local id = tostring(window.id)
	workspaces.of_window[id] = workspaces.of_window[id] or workspaces.current
	window.box.visible = workspaces.of_window[id] == workspaces.current
	window:on("closed", function() workspaces.of_window[id] = nil end)
end)

for number = 1, 9 do
	bind(mod, tostring(number), function() show_workspace(number) end)
	bind(mod .. " shift", tostring(number), function()
		local window = focused_window()
		if not window or number == workspaces.current then return end
		workspaces.of_window[tostring(window.id)] = number
		window.box.visible = false
		wm:emit("arrange")
		focus_topmost()
	end)
end