HSUIWindow
TypeHSUIWindow
A custom window with declarative UI building
HSUIWindow allows you to create custom windows with a SwiftUI-like
declarative syntax. Build interfaces using shapes, text, images, and layout containers.
** Note: ** A shown window stays on screen, with its callbacks, until it's hidden or destroyed,
even if you don't keep a reference to it. Clicking the macOS close button only hides the
window (firing the onHide() callback, if you have one configured) — it does not destroy the
window while you hold a reference to it in JavaScript, so you can show it again. Call
destroy() explicitly (for example from within an onHide() handler) if you want to release
it sooner. See onShow(), onHide(), and onDestroy() below for the full set of lifecycle
callbacks.
Building UI Elements
- Shapes:
rectangle(),circle() - Text:
text(content) - Buttons:
button(label)— uses SwiftUI's native Button for press-state feedback - Images:
image(imageValue) - Video:
video(videoValue) - Layout:
vstack(),hstack(),zstack(),spacer()
Modifying Elements
- Shape modifiers:
fill(),stroke(),strokeWidth(),cornerRadius() - Text modifiers:
font(),foregroundColor() - Image modifiers:
resizable(),aspectRatio(mode) - Layout modifiers:
frame(),opacity(),padding(),spacing()
Examples
Simple window with text and shapes:
hs.ui.window({x: 100, y: 100, w: 300, h: 200})
.vstack()
.spacing(10)
.padding(20)
.text("Dashboard")
.font(HSFont.largeTitle())
.foregroundColor("#FFFFFF")
.rectangle()
.fill("#4A90E2")
.cornerRadius(10)
.frame({w: "90%", h: 80})
.end()
.backgroundColor("#2C3E50")
.show();
Window with image:
const img = HSImage.fromPath("~/Pictures/photo.jpg")
hs.ui.window({x: 100, y: 100, w: 400, h: 300})
.vstack()
.padding(20)
.image(img)
.resizable()
.aspectRatio("fit")
.frame({w: 360, h: 240})
.end()
.show();
Properties
This type has no properties.
Methods
show() -> HSUIWindow
Show the window, bringing it to the front and restoring it if it's minimized
show() -> HSUIWindow
hide() -> None
Hide the window without destroying it
hide() -> None
onShow(callback) -> HSUIWindow
Set a callback to fire after the window is shown
onShow(callback) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| callback | function | A JavaScript function called after the window becomes visible |
hs.ui.window({x: 100, y: 100, w: 300, h: 200})
.text("Hello")
.onShow(() => console.log("Window shown"))
.show()
onHide(callback) -> HSUIWindow
Set a callback to fire when the window is hidden
Fires when hide() is called, and when the user clicks the macOS close
button — clicking that button only hides the window from Hammerspoon's
perspective (see the class-level note above), so this is the callback that reacts
to it. Does not fire when the window is destroyed via destroy() — use
onDestroy() for that.
onHide(callback) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| callback | function | A JavaScript function called after the window is hidden |
const win = hs.ui.window({x: 100, y: 100, w: 300, h: 200})
.text("Hello")
.onHide(() => console.log("Window hidden (via hide() or the close button)"))
.show()
onDestroy(callback) -> HSUIWindow
Set a callback to fire after the window is destroyed via destroy()
Only fires when destroy() is called explicitly — whether directly, or from
within an onHide() handler. It does not fire when the user clicks the macOS
close button by itself; that only hides the window, so use onHide() to react to
the button click, and call destroy() from that handler if you also want to
release the window.
onDestroy(callback) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| callback | function | A JavaScript function called after the window is destroyed |
const win = hs.ui.window({x: 100, y: 100, w: 300, h: 200})
.text("Hello")
.onDestroy(() => console.log("Window destroyed"))
.show()
win.destroy()
titled(show) -> HSUIWindow
Show or hide the window's title bar
By default windows have a title bar. Pass false to create a borderless window.
.closable(), .miniaturizable(), and .allowResize() only take visual effect
when the window is titled.
titled(show) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| show | boolean | Pass false to make the window borderless |
// Borderless floating overlay
hs.ui.window({x: 100, y: 100, w: 400, h: 300})
.titled(false)
.level("floating")
.show()
closable(show) -> HSUIWindow
Show or hide the close button on the window
Requires .titled(true) to be visible. Enabled by default.
closable(show) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| show | boolean | Pass false to hide the close button |
hs.ui.window({x: 100, y: 100, w: 800, h: 600})
.closable(false).show()
miniaturizable(show) -> HSUIWindow
Show or hide the miniaturize (yellow) button on the window
Requires .titled(true) to be visible. Enabled by default.
miniaturizable(show) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| show | boolean | Pass false to hide the miniaturize button |
hs.ui.window({x: 100, y: 100, w: 800, h: 600})
.miniaturizable(false).show()
allowResize(enable) -> HSUIWindow
Allow or prevent the user from resizing the window
Enabled by default. Only has a visual effect when .titled(true) is also set.
allowResize(enable) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| enable | boolean | Pass false to prevent the user from resizing the window |
hs.ui.window({x: 100, y: 100, w: 800, h: 600})
.allowResize(false).show()
windowTitle(text) -> HSUIWindow
Set the text shown in the window's title bar
Only visible when .titled(true) is set (the default). Can be called before or after show().
windowTitle(text) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| text | string | The title bar text |
hs.ui.window({x: 100, y: 100, w: 800, h: 600})
.windowTitle("My Browser").show()
level(name) -> HSUIWindow
Set the window stacking level
Controls where this window sits in the macOS window hierarchy. Supported values:
"normal"— regular app window, sits with other app windows (default)"floating"— floats above all normal windows"screenSaver"— above the screen saver layer"dock"— same level as the Dock"status"— status bar level"popUpMenu"— pop-up menu level
Can be called before or after show().
level(name) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| name | string | The level name |
hs.ui.window({x: 100, y: 100, w: 800, h: 600})
.level("floating").show()
behavior(name) -> HSUIWindow
Set how the window behaves with Spaces, fullscreen apps and window cycling, from a single behavior name
The names (which map to NSWindow.CollectionBehavior) are:
"default"— the system default"canJoinAllSpaces"— appears on every Space"moveToActiveSpace"— moves to the active Space when shown"managed"— participates in Spaces and Exposé (the default for normal windows)"transient"— floats in Spaces and is hidden by Exposé"stationary"— unaffected by Exposé"participatesInCycle"/"ignoresCycle"— whether ⌘` cycles to the window"fullScreenPrimary"/"fullScreenAuxiliary"/"fullScreenNone"— how the window relates to fullscreen apps;"fullScreenAuxiliary"lets it appear over another app's fullscreen Space"fullScreenAllowsTiling"/"fullScreenDisallowsTiling"— whether it can be tiled in fullscreen
Use behaviorList() to combine several. An unknown name is logged and leaves the behavior unchanged.
Can be called before or after show().
behavior(name) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| name | string | The behavior name |
hs.ui.window({x: 100, y: 100, w: 400, h: 200})
.behavior("canJoinAllSpaces")
.text("On every Space")
.show()
behaviorList(names) -> HSUIWindow
Set how the window behaves with Spaces, fullscreen apps and window cycling, from a combination of behavior names
See behavior() for the names. Some can't be combined, because AppKit treats them as mutually exclusive:
"canJoinAllSpaces" with "moveToActiveSpace"; any two of "managed", "transient" and "stationary";
"participatesInCycle" with "ignoresCycle"; any two of the "fullScreen…" Primary/Auxiliary/None names;
and "fullScreenAllowsTiling" with "fullScreenDisallowsTiling". Combining them throws, leaving the
behavior unchanged. Unknown names are logged and ignored. Can be called before or after show().
behaviorList(names) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| names | string[] | The behavior names to combine, or an empty array for the system default |
// An alert that appears above everything, including fullscreen apps, on every Space
hs.ui.window({x: 100, y: 100, w: 400, h: 200})
.level("screenSaver")
.behaviorList(["canJoinAllSpaces", "fullScreenAuxiliary"])
.text("Meeting starts now")
.show()
backgroundColor(colorValue) -> HSUIWindow
Set the window's background color
backgroundColor(colorValue) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| colorValue | JSValue | A hex color string (e.g. "#FF0000") or an HSColor object |
rectangle() -> HSUIWindow
Add a rectangle shape
rectangle() -> HSUIWindow
fill(), frame())circle() -> HSUIWindow
Add a circle shape
circle() -> HSUIWindow
fill(), frame())text(content) -> HSUIWindow
Add a text element
text(content) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| content | JSValue | The text to display — a plain JS string for static text, |
font(), foregroundColor())image(imageValue) -> HSUIWindow
Add an image element
image(imageValue) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| imageValue | HSImage | Image as HSImage object |
resizable(), aspectRatio(), frame())video(videoValue) -> HSUIWindow
Add a video element
Renders a SwiftUI VideoPlayer for the given HSVideo. Keep a reference to the
HSVideo object to control playback (play(), pause(), seek(), volume) after
the window is shown.
video(videoValue) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| videoValue | HSVideo | Video as an HSVideo object |
frame(), opacity())const clip = HSVideo.fromURLs(["~/Movies/clip.mp4"])
hs.ui.window({x: 100, y: 100, w: 640, h: 360})
.video(clip)
.frame({w: 640, h: 360})
.show()
clip.play()
button(label) -> HSUIWindow
Add a button element
button(label) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| label | JSValue | The button label — a plain JS string for static text, |
.fill(), .cornerRadius(), .font(),vstack() -> HSUIWindow
Begin a vertical stack (elements arranged top to bottom)
vstack() -> HSUIWindow
end() when done)hstack() -> HSUIWindow
Begin a horizontal stack (elements arranged left to right)
hstack() -> HSUIWindow
end() when done)zstack() -> HSUIWindow
Begin a z-stack (overlapping elements)
zstack() -> HSUIWindow
end() when done)spacer() -> HSUIWindow
Add flexible spacing that expands to fill available space
spacer() -> HSUIWindow
webview(element) -> HSUIWindow
Embed a web browser element created with hs.ui.webview() (macOS 26+)
The element fills the available space in the window layout. Keep a reference to the element to call navigation methods after the window is shown.
webview(element) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| element | UIWebView | A UIWebView created via hs.ui.webview() |
const wv = hs.ui.webview()
.toolbar(["back", "forward", "reload", "url"])
.loadURL("https://apple.com")
hs.ui.window({x: 100, y: 100, w: 1024, h: 768})
.webview(wv)
.show()
end() -> HSUIWindow
End the current layout container
end() -> HSUIWindow
fill(colorValue) -> HSUIWindow
Fill a shape with a color
fill(colorValue) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| colorValue | JSValue | A hex color string (e.g. "#FF0000") or an HSColor object |
stroke(colorValue) -> HSUIWindow
Add a stroke (border) to a shape
stroke(colorValue) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| colorValue | JSValue | A hex color string (e.g. "#FF0000") or an HSColor object |
strokeWidth(width) -> HSUIWindow
Set the stroke width
strokeWidth(width) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| width | number | Width in points |
cornerRadius(radius) -> HSUIWindow
Round the corners of a shape
cornerRadius(radius) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| radius | number | Corner radius in points |
frame(dict) -> HSUIWindow
Set the frame (size) of an element
frame(dict) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| dict | {[key: string]: any} | Dictionary with w and/or h (can be numbers or percentage strings like "50%") |
opacity(value) -> HSUIWindow
Set the opacity of an element
opacity(value) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| value | number | Opacity from 0.0 (transparent) to 1.0 (opaque) |
font(font) -> HSUIWindow
Set the font for a text element
font(font) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| font | HSFont | An HSFont object (e.g., HSFont.title()) |
foregroundColor(colorValue) -> HSUIWindow
Set the text color
foregroundColor(colorValue) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| colorValue | JSValue | A hex color string (e.g. "#FF0000") or an HSColor object |
resizable() -> HSUIWindow
Make an image resizable (allows it to scale with frame size)
resizable() -> HSUIWindow
aspectRatio(mode) -> HSUIWindow
Set the aspect ratio mode for an image
aspectRatio(mode) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| mode | string | "fit" (scales to fit within frame) or "fill" (scales to fill frame) |
padding(value) -> HSUIWindow
Add padding around a layout container
padding(value) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| value | number | Padding in points |
spacing(value) -> HSUIWindow
Set spacing between elements in a stack
spacing(value) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| value | number | Spacing in points |
onClick(callback) -> HSUIWindow
Set a callback to fire when the element is clicked
onClick(callback) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| callback | function | A JavaScript function to call on click |
onHover(callback) -> HSUIWindow
Set a callback to fire when the cursor enters or leaves the element
onHover(callback) -> HSUIWindow
| Name | Type | Description |
|---|---|---|
| callback | function | A JavaScript function called with true when the cursor enters and false when it leaves |