Skip to content

Windows ​

Borders, rules per app, fullscreen, and dragging windows around.

All of them build on your first config.

Borders that show focus ​

Apps leave out their own title bar and borders, and Hazel draws a border that lights up on the focused window.

lua
local border_width = 2
local border_colors = { focused = "#88c0d0", unfocused = "#4c566a" }

wm.windows:on("new_window", function(window)
	window.decoration = "server"
	local border = Drawable.rect({
		left = -border_width, right = -border_width, top = -border_width, bottom = -border_width,
		border_width = border_width, border_color = border_colors.unfocused,
	})
	window.drawables:add(border)
	window:on("focus", function(_, _, window)
		local focused = #window.focused_seats > 0
		window.states:set("activated", focused)
		border.border_color = focused and border_colors.focused or border_colors.unfocused
	end)
	window:on("resized", function(window)
		border.visible = not window.states:has("fullscreen")
	end)
end)
  • decoration = "server" asks the app to leave out its title bar. Apps that don't support it, and some GTK apps, keep theirs.
  • The border is a drawable on the window, so it follows the window as it moves and resizes. It sits just outside the window's edges.
  • The activated state is what apps use to draw themselves as the active window.
  • A fullscreen window hides its border, so it doesn't show on the monitor next to it.

Drawing adds a title bar.

Window rules ​

A list of rules, each matched on the app id, the title, or both. The title is a Lua pattern, so - and . need a % in front.

lua
local rules = {
	{ app_id = "mpv", z = 1 },
	{ app_id = "firefox", title = "^Picture%-in%-Picture$", z = 1, float = true },
	{ app_id = "foot", opacity = 0.95 },
	{ app_id = "org.keepassxc.KeePassXC", workspace = 9 },
}

local function rule_matches(rule, window)
	return (rule.app_id == nil or window.app_id == rule.app_id)
		and (rule.title == nil or (window.title or ""):find(rule.title) ~= nil)
end

wm.windows:on("new_window", function(window)
	window:on("first_content", function(window)
		local id = tostring(window.id)
		for _, rule in ipairs(rules) do
			if rule_matches(rule, window) then
				if rule.z then window.box.z = rule.z end
				if rule.opacity then window.box.opacity = rule.opacity end
				if rule.float and wm.store.tiling then wm.store.tiling.floating[id] = true end
				if rule.workspace and wm.store.workspaces then
					wm.store.workspaces.of_window[id] = rule.workspace
					window.box.visible = rule.workspace == wm.store.workspaces.current
				end
			end
		end
		wm:emit("arrange")
	end)
end)
  • z stacks windows: a window with a higher z stays in front of the rest. 1 keeps it on top.
  • float needs Tiling and workspace needs Workspaces. Without them those fields do nothing.
  • Rules run on first_content, when the app has drawn once and set its id and title. An app that changes its title later isn't matched again. Listen to title_changed for that.

Fullscreen and maximize ​

mod + F toggles fullscreen and mod + M maximizes. Apps can ask for both too, like a video player going fullscreen.

lua
local frames_before_filling = {}

local function fill(window, state, on)
	if window.states:has(state) == on then return end
	local id = tostring(window.id)
	if on and not (window.states:has("fullscreen") or window.states:has("maximized")) then
		frames_before_filling[id] = { position = window.box.position:target(), size = window.size }
	end
	local output = output_of(window)
	window.states:set(state, on)
	local area
	if window.states:has("fullscreen") then
		area = output:area()
	elseif window.states:has("maximized") then
		area = output:usable_area()
	end
	if area then
		window.box.size = nil
		window.box.position = Point(area.x, area.y)
		window:request_size({ width = area.width, height = area.height })
		window.box:raise()
	else
		local before = frames_before_filling[id]
		frames_before_filling[id] = nil
		if before then
			window.box.position = before.position
			window:request_size(before.size)
		end
	end
	wm:emit("arrange")
end

wm.windows:on("new_window", function(window)
	window:on("fullscreen_request", function(_, window) fill(window, "fullscreen", true) end)
	window:on("unfullscreen_request", function(window) fill(window, "fullscreen", false) end)
	window:on("maximize_request", function(window) fill(window, "maximized", true) end)
	window:on("unmaximize_request", function(window) fill(window, "maximized", false) end)
	window:on("closed", function() frames_before_filling[tostring(window.id)] = nil end)
end)

bind(mod, "f", function()
	local window = focused_window()
	if window then fill(window, "fullscreen", not window.states:has("fullscreen")) end
end)
bind(mod, "m", function()
	local window = focused_window()
	if window then fill(window, "maximized", not window.states:has("maximized")) end
end)
  • The states tell the app how it is shown. Hazel draws a fullscreen window in front of bars, but moving and sizing it is up to the config.
  • Fullscreen covers the whole output. Maximized leaves room for bars.
  • Tiling leaves fullscreen and maximized windows alone, and takes them back when they return.

Focus when an app asks ​

Clicking a link in a chat app should bring the browser forward. Apps ask with an activation token. This focuses the window when the token is less than three seconds old, so an app can't grab focus long after you clicked something.

lua
wm.windows:on("new_window", function(window)
	window:on("activation_request", function(token_age_ms, window)
		if token_age_ms == nil or token_age_ms < 3000 then focus(window) end
	end)
end)

A window on another workspace stays there. Focus only reaches visible windows.

Dragging windows ​

mod + left drag moves a window, mod + right drag resizes it. Apps that draw their own title bar can be dragged by it, and resized by their edges.

lua
local drag

local function start_drag(window, pointer, edges, from_app)
	drag = {
		window = window, pointer = pointer, edges = edges, from_app = from_app,
		start = pointer:position(), position = window.box.position:target(),
		size = window.size or { width = 0, height = 0 },
	}
	pointer:grab()
	window.box:raise()
end

local function drag_to(point)
	local moved = point - drag.start
	if not drag.edges then
		drag.window.box.position = drag.position + moved
		return
	end
	local x, y = drag.position.x, drag.position.y
	local width, height = drag.size.width, drag.size.height
	if drag.edges:find("right") then width = width + moved.x end
	if drag.edges:find("bottom") then height = height + moved.y end
	if drag.edges:find("left") then width, x = width - moved.x, x + moved.x end
	if drag.edges:find("top") then height, y = height - moved.y, y + moved.y end
	drag.window.box.position = Point(x, y)
	drag.window:request_size({ width = math.max(width, 100), height = math.max(height, 60) })
end

wm.input:on("pointer_button", function(event)
	if drag and event.state == "Released" then
		if not drag.from_app then event:prevent_default() end
		drag.pointer:ungrab()
		drag = nil
		return
	end
	if drag or event.state ~= "Pressed" or not event.modifiers[mod] then return end
	if event.button ~= Button.Left and event.button ~= Button.Right then return end
	local window = event.pointer:window_under()
	if not window then return end
	event:prevent_default()
	focus(window)
	start_drag(window, event.pointer, event.button == Button.Right and "bottom_right" or nil, false)
end)

wm.input:on("pointer_move", function(event)
	if drag then drag_to(event.position) end
end)

wm.windows:on("new_window", function(window)
	window:on("move_request", function(request, window)
		if not drag then start_drag(window, request.pointer, nil, true) end
	end)
	window:on("resize_request", function(request, window)
		local edges = request.edges == "none" and "bottom_right" or request.edges
		if not drag then start_drag(window, request.pointer, edges, true) end
	end)
end)
  • pointer:grab() keeps the pointer from apps while you drag, so the window under it doesn't react.
  • A press with mod is kept from the app, and so is its release. A drag the app started keeps its release, since the app saw the press.
  • With Tiling, a tiled window goes back to its tile at the next re-tile. Float it first with mod + Shift + Space.

All of them ​

lua
local border_width = 2
local border_colors = { focused = "#88c0d0", unfocused = "#4c566a" }

wm.windows:on("new_window", function(window)
	window.decoration = "server"
	local border = Drawable.rect({
		left = -border_width, right = -border_width, top = -border_width, bottom = -border_width,
		border_width = border_width, border_color = border_colors.unfocused,
	})
	window.drawables:add(border)
	window:on("focus", function(_, _, window)
		local focused = #window.focused_seats > 0
		window.states:set("activated", focused)
		border.border_color = focused and border_colors.focused or border_colors.unfocused
	end)
	window:on("resized", function(window)
		border.visible = not window.states:has("fullscreen")
	end)
end)

local rules = {
	{ app_id = "mpv", z = 1 },
	{ app_id = "firefox", title = "^Picture%-in%-Picture$", z = 1, float = true },
	{ app_id = "foot", opacity = 0.95 },
	{ app_id = "org.keepassxc.KeePassXC", workspace = 9 },
}

local function rule_matches(rule, window)
	return (rule.app_id == nil or window.app_id == rule.app_id)
		and (rule.title == nil or (window.title or ""):find(rule.title) ~= nil)
end

wm.windows:on("new_window", function(window)
	window:on("first_content", function(window)
		local id = tostring(window.id)
		for _, rule in ipairs(rules) do
			if rule_matches(rule, window) then
				if rule.z then window.box.z = rule.z end
				if rule.opacity then window.box.opacity = rule.opacity end
				if rule.float and wm.store.tiling then wm.store.tiling.floating[id] = true end
				if rule.workspace and wm.store.workspaces then
					wm.store.workspaces.of_window[id] = rule.workspace
					window.box.visible = rule.workspace == wm.store.workspaces.current
				end
			end
		end
		wm:emit("arrange")
	end)
end)

local frames_before_filling = {}

local function fill(window, state, on)
	if window.states:has(state) == on then return end
	local id = tostring(window.id)
	if on and not (window.states:has("fullscreen") or window.states:has("maximized")) then
		frames_before_filling[id] = { position = window.box.position:target(), size = window.size }
	end
	local output = output_of(window)
	window.states:set(state, on)
	local area
	if window.states:has("fullscreen") then
		area = output:area()
	elseif window.states:has("maximized") then
		area = output:usable_area()
	end
	if area then
		window.box.size = nil
		window.box.position = Point(area.x, area.y)
		window:request_size({ width = area.width, height = area.height })
		window.box:raise()
	else
		local before = frames_before_filling[id]
		frames_before_filling[id] = nil
		if before then
			window.box.position = before.position
			window:request_size(before.size)
		end
	end
	wm:emit("arrange")
end

wm.windows:on("new_window", function(window)
	window:on("fullscreen_request", function(_, window) fill(window, "fullscreen", true) end)
	window:on("unfullscreen_request", function(window) fill(window, "fullscreen", false) end)
	window:on("maximize_request", function(window) fill(window, "maximized", true) end)
	window:on("unmaximize_request", function(window) fill(window, "maximized", false) end)
	window:on("closed", function() frames_before_filling[tostring(window.id)] = nil end)
end)

bind(mod, "f", function()
	local window = focused_window()
	if window then fill(window, "fullscreen", not window.states:has("fullscreen")) end
end)
bind(mod, "m", function()
	local window = focused_window()
	if window then fill(window, "maximized", not window.states:has("maximized")) end
end)

wm.windows:on("new_window", function(window)
	window:on("activation_request", function(token_age_ms, window)
		if token_age_ms == nil or token_age_ms < 3000 then focus(window) end
	end)
end)

local drag

local function start_drag(window, pointer, edges, from_app)
	drag = {
		window = window, pointer = pointer, edges = edges, from_app = from_app,
		start = pointer:position(), position = window.box.position:target(),
		size = window.size or { width = 0, height = 0 },
	}
	pointer:grab()
	window.box:raise()
end

local function drag_to(point)
	local moved = point - drag.start
	if not drag.edges then
		drag.window.box.position = drag.position + moved
		return
	end
	local x, y = drag.position.x, drag.position.y
	local width, height = drag.size.width, drag.size.height
	if drag.edges:find("right") then width = width + moved.x end
	if drag.edges:find("bottom") then height = height + moved.y end
	if drag.edges:find("left") then width, x = width - moved.x, x + moved.x end
	if drag.edges:find("top") then height, y = height - moved.y, y + moved.y end
	drag.window.box.position = Point(x, y)
	drag.window:request_size({ width = math.max(width, 100), height = math.max(height, 60) })
end

wm.input:on("pointer_button", function(event)
	if drag and event.state == "Released" then
		if not drag.from_app then event:prevent_default() end
		drag.pointer:ungrab()
		drag = nil
		return
	end
	if drag or event.state ~= "Pressed" or not event.modifiers[mod] then return end
	if event.button ~= Button.Left and event.button ~= Button.Right then return end
	local window = event.pointer:window_under()
	if not window then return end
	event:prevent_default()
	focus(window)
	start_drag(window, event.pointer, event.button == Button.Right and "bottom_right" or nil, false)
end)

wm.input:on("pointer_move", function(event)
	if drag then drag_to(event.position) end
end)

wm.windows:on("new_window", function(window)
	window:on("move_request", function(request, window)
		if not drag then start_drag(window, request.pointer, nil, true) end
	end)
	window:on("resize_request", function(request, window)
		local edges = request.edges == "none" and "bottom_right" or request.edges
		if not drag then start_drag(window, request.pointer, edges, true) end
	end)
end)