Class Drawable
Something Hazel draws for the config: a rectangle, text, a PNG image or a live picture of a window.
Subclasses: DrawableRect, DrawableText, DrawableImage, DrawableWindow
Making a drawable draws nothing. It is drawn in every DrawableList it is in, placed against that list's window, output, cursor or the desktop. One drawable can be in several lists. A change to it shows in all of them.
A frame and a title bar that follow a window as it moves and resizes:
local frame = Drawable.rect({
left = -2, right = -2, top = -28, bottom = -2,
z = -1, radius = 8, fill = "#2e3440",
})
local title = Drawable.text({
left = 10, right = 10, top = -22,
text = window.title or "", font = { size = 13, weight = 600 }, color = "#eceff4",
max_width = 400,
})
window.drawables = { frame, title }
window:on("title_changed", function(changed) title.text = changed.title or "" end)
window:on("focus", function(_, focused)
frame.fill = Anim.tween(focused and "#5e81ac" or "#2e3440", 150)
end)Functions
The spec table sets fields by name, as if assigned one by one after making the drawable: the kind's own fields and the shared properties.
Errors for a field the kind doesn't have, and for kind.
Drawable.rect(spec: table?) -> DrawableRectrect
A rectangle with a fill, rounded corners and a border. See DrawableRect.
Drawable.text(spec: table?) -> DrawableTexttext
Text on one line, or several with wrap. See DrawableText.
Drawable.image(spec: table) -> DrawableImageimage
A PNG file. See DrawableImage.
Errors without spec.path, and when the file is missing or isn't a readable PNG.
Drawable.window(spec: table) -> DrawableWindowwindow
A live picture of a window. See DrawableWindow.
Errors without spec.window.
Anchoring
A drawable is placed inside its parent's box like CSS absolute positioning. Each axis takes any two of its three fields: left, right and width across, top, bottom and height down.
leftandright: stretched between the two edges.leftandwidth, orrightandwidth: that wide, pinned to that side.widthalone: at the left edge.leftorrightalone, or neither: the drawable's own width. That is 0 for a rect, the PNG's width for an image, the window's width for a window picture and the text's width for text.
top, bottom and height work the same way down. Text has no height and takes top or bottom, not both.
Errors when a third field is set on an axis that has two (clear one with nil first), and for right or bottom on a drawable in wm.drawables.
The parent's box is:
- in
window.drawables, the window's box as drawn, animations included. - in
output.drawables, the whole output. - in
wm.drawables, a point at 0,0 in global coordinates, with no right or bottom edge. - in
cursor.drawables, the pointer's hotspot, with no right or bottom edge.
local underline = Drawable.rect({ left = 0, right = 0, bottom = -3, height = 2, fill = "#88c0d0" })
window.drawables:add(underline)
underline.right = nil
underline.width = Anim.spring(120)Properties
left, right, top, bottom, width and height read back as an Animated or nil. nil or false clears one.
Errors for a value outside a field's limits, and for a field the drawable's kind doesn't have.
drawable.kind: "rect" | "text" | "image" | "window"kind read only
Which function made it. To change kind, make a new drawable.
drawable.left: number | Animated | nilleft
Distance in from the parent's left edge. Negative goes outside it.
- Default: nil.
- -16384 to 16384.
drawable.right: number | Animated | nilright
Distance in from the parent's right edge.
- Default: nil.
- -16384 to 16384.
- Not in
wm.drawablesor a cursor's list.
drawable.width: number | Animated | nilwidth
The drawable's width.
- Default: nil, the drawable's own width.
- 0 to 16384.
- On text it is the box the text is aligned in, not a limit. See
align.
drawable.top: number | Animated | niltop
Distance in from the parent's top edge. Negative goes outside it.
- Default: nil.
- -16384 to 16384.
drawable.bottom: number | Animated | nilbottom
Distance in from the parent's bottom edge.
- Default: nil.
- -16384 to 16384.
- Not in
wm.drawablesor a cursor's list.
drawable.height: number | Animated | nilheight
The drawable's height. Every kind but DrawableText has it; text is as tall as its lines.
- Default: nil, the drawable's own height.
- 0 to 16384.
drawable.z: integerz
Order within the list. A higher z is in front.
- Default: 0.
- Drawables with the same z keep their list order, later in front.
- In a window's list, below 0 is behind the window. 0 or more is in front of it and behind its popups.
- In
wm.drawables, below 0 is behind every window and 0 or more is in front of every window. Fullscreen windows cover both.
drawable.layer: "background" | "bottom" | "top" | "overlay" | nillayer
Where the drawable sits among windows and layer surfaces. Only an output's list uses it.
- Default: nil, the same as
"top". - Front to back: overlay, fullscreen windows, top, other windows, bottom, background.
- Within one layer, layer surfaces are in front of drawables.
Use "overlay" to cover fullscreen windows too, and "background" for a wallpaper.
drawable.visible: booleanvisible
Whether the drawable is drawn.
- Default: true.
- A hidden drawable stays in its lists.
Methods
drawable:drawn_rect() -> Rect?drawn_rect
The Rect the drawable was last drawn at, in global coordinates. Use it to hit-test a drawable.
- nil before it is first drawn, while
visibleis false, and once it is in no list. - In several lists, it is the one drawn last.
- For text, it is the box the text is aligned in.
local close_button = Drawable.rect({ right = 8, top = -24, width = 16, height = 16, radius = 8, fill = "#bf616a" })
window.drawables:add(close_button)
wm.input:on("pointer_button", function(event)
if event.button ~= Button.Left or event.state ~= "Pressed" then return end
local area = close_button:drawn_rect()
if area and area:contains(event.pointer:position()) then
event:prevent_default()
window:close()
end
end)Operators
drawable == other -> booleandrawable == other
True when both are the same drawable, including one read back from a list's all().