Skip to content

Drawing ​

Hazel draws rectangles, text and PNG images for the config. They are called drawables, and each one is placed against a window, an output, the cursor or the desktop, much like CSS absolute positioning: left, right, top, bottom, width and height, with negative values reaching outside.

All of these build on your first config.

Title bars ​

A bar above each window with its title, darker when the window isn't focused.

lua
local title_bar_height = 26
local title_bar_colors = { focused = "#5e81ac", unfocused = "#3b4252", text = "#eceff4" }

wm.windows:on("new_window", function(window)
	window.decoration = "server"
	local bar = Drawable.rect({
		left = 0, right = 0, top = -title_bar_height, height = title_bar_height + 8,
		z = -1, radius = 8, fill = title_bar_colors.unfocused,
	})
	local title = Drawable.text({
		left = 10, right = 10, top = -title_bar_height + 5,
		text = window.title or "", font = { size = 13, weight = 600 }, color = title_bar_colors.text,
	})
	window.drawables = { bar, title }
	window:on("title_changed", function(window) title.text = window.title or "" end)
	window:on("resized", function(window)
		title.max_width = math.max(0, window.size.width - 20)
		bar.visible = not window.states:has("fullscreen")
		title.visible = bar.visible
	end)
	window:on("focus", function(_, _, window)
		local focused = #window.focused_seats > 0
		bar.fill = Anim.tween(focused and title_bar_colors.focused or title_bar_colors.unfocused, 150)
	end)
end)
  • The bar reaches 8 pixels down behind the window (z = -1 is behind it), so its rounded bottom corners are hidden.
  • max_width cuts a long title with "…". It follows the window's width.
  • With Tiling, raise gap to at least title_bar_height plus a few pixels, or the bar covers the window above it.

A wallpaper ​

A PNG stretched over every output, behind everything. Relative paths are looked up in the config directory.

lua
local wallpaper = "wallpaper.png"

wm.outputs:on("added", function(output)
	output.drawables:add(Drawable.image({
		path = wallpaper, left = 0, right = 0, top = 0, bottom = 0, layer = "background",
	}))
end)

Drawable.image reads PNG files only. For other formats, or to keep the picture's aspect ratio, start swaybg from Session instead. For a plain colour, use Drawable.rect with a fill in the same place.

A clock ​

The time in the top-right corner of every output. The timer checks it every second, so the minute turns over on time.

lua
local clocks = {}

wm.outputs:on("added", function(output)
	local clock = Drawable.text({
		right = 16, width = 200, top = 10, align = "right",
		text = os.date("%H:%M"), font = { family = "monospace", size = 15 }, color = "#eceff4",
	})
	output.drawables:add(clock)
	clocks[output.name] = clock
end)

wm.outputs:on("removed", function(output) clocks[output.name] = nil end)

Timer.every(1000, function()
	local now = os.date("%H:%M")
	for _, clock in pairs(clocks) do clock.text = now end
end)

Drawables on an output sit in front of windows by default (layer = "top"), the same depth as most bars. A bar at the top of the screen, such as waybar from Panels and lock screens, covers the clock. Use bottom = 10 instead of top = 10 then, or leave the clock out.

A workspace indicator ​

A row of dots at the bottom of every output, the current workspace lit.

Needs Workspaces.

lua
local dot_size, dot_gap = 8, 6
local indicators = {}

local function light_dots(current)
	for _, dots in pairs(indicators) do
		for number, dot in ipairs(dots) do
			dot.fill = Anim.tween(number == current and "#eceff4" or "#eceff440", 150)
		end
	end
end

wm.outputs:on("added", function(output)
	local dots = {}
	local row_width = 9 * dot_size + 8 * dot_gap
	local area = output:area()
	for number = 1, 9 do
		dots[number] = Drawable.rect({
			left = (area.width - row_width) / 2 + (number - 1) * (dot_size + dot_gap),
			bottom = 12, width = dot_size, height = dot_size, radius = dot_size / 2,
		})
		output.drawables:add(dots[number])
	end
	indicators[output.name] = dots
	light_dots(wm.store.workspaces and wm.store.workspaces.current or 1)
end)

wm.outputs:on("removed", function(output) indicators[output.name] = nil end)
wm:on("workspace_changed", light_dots)

The dots are placed when the output is added. If you change an output's scale or mode, the next reload centres them again.

All of them ​

lua
local title_bar_height = 26
local title_bar_colors = { focused = "#5e81ac", unfocused = "#3b4252", text = "#eceff4" }

wm.windows:on("new_window", function(window)
	window.decoration = "server"
	local bar = Drawable.rect({
		left = 0, right = 0, top = -title_bar_height, height = title_bar_height + 8,
		z = -1, radius = 8, fill = title_bar_colors.unfocused,
	})
	local title = Drawable.text({
		left = 10, right = 10, top = -title_bar_height + 5,
		text = window.title or "", font = { size = 13, weight = 600 }, color = title_bar_colors.text,
	})
	window.drawables = { bar, title }
	window:on("title_changed", function(window) title.text = window.title or "" end)
	window:on("resized", function(window)
		title.max_width = math.max(0, window.size.width - 20)
		bar.visible = not window.states:has("fullscreen")
		title.visible = bar.visible
	end)
	window:on("focus", function(_, _, window)
		local focused = #window.focused_seats > 0
		bar.fill = Anim.tween(focused and title_bar_colors.focused or title_bar_colors.unfocused, 150)
	end)
end)

local wallpaper = "wallpaper.png"

wm.outputs:on("added", function(output)
	output.drawables:add(Drawable.image({
		path = wallpaper, left = 0, right = 0, top = 0, bottom = 0, layer = "background",
	}))
end)

local clocks = {}

wm.outputs:on("added", function(output)
	local clock = Drawable.text({
		right = 16, width = 200, top = 10, align = "right",
		text = os.date("%H:%M"), font = { family = "monospace", size = 15 }, color = "#eceff4",
	})
	output.drawables:add(clock)
	clocks[output.name] = clock
end)

wm.outputs:on("removed", function(output) clocks[output.name] = nil end)

Timer.every(1000, function()
	local now = os.date("%H:%M")
	for _, clock in pairs(clocks) do clock.text = now end
end)

local dot_size, dot_gap = 8, 6
local indicators = {}

local function light_dots(current)
	for _, dots in pairs(indicators) do
		for number, dot in ipairs(dots) do
			dot.fill = Anim.tween(number == current and "#eceff4" or "#eceff440", 150)
		end
	end
end

wm.outputs:on("added", function(output)
	local dots = {}
	local row_width = 9 * dot_size + 8 * dot_gap
	local area = output:area()
	for number = 1, 9 do
		dots[number] = Drawable.rect({
			left = (area.width - row_width) / 2 + (number - 1) * (dot_size + dot_gap),
			bottom = 12, width = dot_size, height = dot_size, radius = dot_size / 2,
		})
		output.drawables:add(dots[number])
	end
	indicators[output.name] = dots
	light_dots(wm.store.workspaces and wm.store.workspaces.current or 1)
end)

wm.outputs:on("removed", function(output) indicators[output.name] = nil end)
wm:on("workspace_changed", light_dots)