App¶
The main application class for Nib menu bar applications. App manages the entire lifecycle of a Nib application, including window configuration, view rendering, event handling, system integration, and communication with the Swift runtime.
Constructor¶
nib.App(title=None, icon=None, identifier=None)
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
title |
str | None |
None |
Text displayed in the menu bar next to the icon |
icon |
str | SFSymbol | View | None |
None |
Menu bar icon. An SF Symbol name, an SFSymbol instance, or any View |
identifier |
str | None |
None |
Bundle identifier, used as the UserDefaults domain. Defaults to com.nib.<slug of the title>; a built app uses its bundle identifier. |
Properties¶
Window and Appearance¶
| Property | Type | Description |
|---|---|---|
title |
str |
Text displayed in the menu bar. Readable and writable |
icon |
str | SFSymbol | View |
Menu bar icon. An SF Symbol name, an SFSymbol instance, or any View. A view is rendered at the menu bar height of 22 points, width from its fitting size, and updates when its properties change |
identifier |
str |
Bundle identifier and UserDefaults domain. Derived from the title (com.nib.<slug>) unless set; inside a built app it is the bundle identifier. Set it in App() or nib.run(): the runtime takes it at launch, so assigning it once connected raises RuntimeError. |
width |
float |
Popover window width in points |
height |
float |
Popover window height in points |
float_on_top |
bool |
Keep the popover open on outside clicks. When True it closes only from code or by clicking the status bar icon again. Default False |
show_quit_button |
bool |
When True, appends a styled Quit button at the bottom of the popover |
Menu and Fonts¶
| Property | Type | Description |
|---|---|---|
menu |
list[MenuItem] |
Right-click context menu items for the status bar icon |
fonts |
dict[str, str] |
Custom fonts registered for the app. Maps font names to file paths or URLs. Fonts placed in assets/fonts/ are auto-detected |
Lifecycle Callbacks¶
| Property | Type | Description |
|---|---|---|
on_appear |
Callable[[], None] |
Called every time the popover opens |
on_disappear |
Callable[[], None] |
Called every time the popover closes |
on_quit |
Callable[[], None] |
Called once when the app quits and, under nib run, when a reload replaces the app |
on_sleep |
Callable[[], None] |
Called when the Mac is about to sleep |
on_wake |
Callable[[], None] |
Called when the Mac wakes |
on_screen_lock |
Callable[[], None] |
Called when the screen locks |
on_screen_unlock |
Callable[[], None] |
Called when the screen unlocks |
on_appearance_change |
Callable[[bool], None] |
Called with dark when the system switches between light and dark |
on_displays_change |
Callable[[], None] |
Called when a display is connected, removed or rearranged |
is_running |
bool |
Read-only. False once the app has quit or been replaced by a reload; background threads should check it |
Settings¶
| Property | Type | Description |
|---|---|---|
settings |
Settings |
The app's saved settings. Assign a dict of defaults to create them; see Settings |
settings_window |
SettingsWindow |
The preferences window, opened with app.settings_window.open(); see SettingsWindow |
defaults |
UserDefaults |
Key-value storage in the app's UserDefaults domain, for values that are not settings; see UserDefaults |
file_picker |
FilePicker |
Open and save dialogs; see FilePicker |
service_timeout |
float |
Seconds a service call waits for the runtime before raising nib.ServiceTimeoutError; 10 by default. A call's own timeout= overrides it |
clipboard |
str |
Write-only: assigning puts the text on the clipboard. Read it with read_clipboard(). See Clipboard |
Service Properties (read-only)¶
| Property | Type | Description |
|---|---|---|
notifications |
NotificationManager |
Push, schedule, and manage macOS notifications |
battery |
Battery |
Read battery level and charging state |
connectivity |
Connectivity |
Check network connectivity status |
screen |
Screen |
Get display info and control brightness |
keychain |
Keychain |
Secure storage for passwords and tokens |
camera |
Camera |
List devices, capture photos, access video frames |
launch_at_login |
LaunchAtLogin |
Control whether the app starts on login |
permissions |
Permissions |
Check and request Camera, Microphone, and Notification permissions |
Methods¶
View Management¶
build(view)¶
Set the root view of the application. If the app is already running, triggers an immediate re-render.
app.build(view: View) -> None
| Parameter | Type | Description |
|---|---|---|
view |
View |
The root view to display in the popover |
body()¶
Override this method in a subclass to define the UI. Returns the root view. Only used in the class-based approach.
app.body() -> View
update()¶
Manually trigger a UI re-render. Use this to force an update when the automatic reactivity system might not detect a change, or to batch multiple changes into a single render pass.
app.update() -> None
Application Lifecycle¶
run()¶
Start the application. Connects to the Swift runtime, performs the initial render, and enters the main event loop. Blocks until the app quits.
app.run() -> None
quit()¶
Quit the application and clean up resources.
app.quit() -> None
Notifications¶
notify(title, body, subtitle, sound, id)¶
Send a macOS system notification.
app.notify(
title: str,
body: str | None = None,
subtitle: str | None = None,
sound: bool = True,
id: str | None = None,
) -> None
| Parameter | Type | Default | Description |
|---|---|---|---|
title |
str |
none | Notification title (required) |
body |
str | None |
None |
Notification body text |
subtitle |
str | None |
None |
Subtitle shown below the title |
sound |
bool |
True |
Play the default notification sound |
id |
str | None |
None |
The notification's id, to update or remove it later; a UUID by default |
Clipboard¶
Only text is supported: a clipboard holding an image or files reads as "".
app.clipboard = "Hello, World!" # write
text = app.read_clipboard() # read, blocks up to 2 seconds
read_clipboard()¶
The clipboard text. Asks the runtime and waits up to two seconds; raises nib.ServiceTimeoutError without a reply and nib.NotConnectedError before the runtime is connected.
app.read_clipboard() -> str
Timers¶
every(seconds, fn)¶
Call fn every seconds, on the event thread, like a button action: its view mutations go out as one frame and an exception is logged. The first call comes after one interval; a call that runs longer than the interval delays the next one, so calls never overlap. The timer stops with the returned Subscription, and when the app quits or a reload replaces it.
app.every(seconds: float, fn: Callable[[], None]) -> Subscription
import time
def tick():
app.title = time.strftime("%H:%M:%S")
app.every(1.0, tick)
after(seconds, fn)¶
Call fn once, seconds from now, on the event thread. stop() on the returned Subscription cancels it.
app.after(seconds: float, fn: Callable[[], None]) -> Subscription
Hotkeys¶
on_hotkey(shortcut, callback)¶
Register a global keyboard shortcut.
app.on_hotkey(shortcut: str, callback: Callable[[], None]) -> Subscription
| Parameter | Type | Description |
|---|---|---|
shortcut |
str |
Key combination, e.g. "cmd+shift+n", "cmd+k" |
callback |
Callable |
Function called when the hotkey is pressed |
import nib
def main(app: nib.App):
app.title = "Hotkey Demo"
app.icon = nib.SFSymbol("keyboard")
status = nib.Text("Press Cmd+Shift+N", font=nib.Font.HEADLINE)
def on_shortcut():
status.content = "Hotkey pressed!"
app.on_hotkey("cmd+shift+n", on_shortcut)
app.build(nib.VStack(controls=[status], padding=24))
nib.run(main)
A shortcut is modifiers and one key joined with +. Case and modifier order do not matter, so "shift+cmd+N" and "cmd+shift+n" are the same shortcut.
| Modifier | Names |
|---|---|
| Command | cmd, command, meta, super |
| Shift | shift |
| Control | ctrl, control |
| Option | opt, option, alt |
A string with no key or with two keys raises ValueError. Registering a shortcut again replaces its callback. The returned Subscription unregisters it with stop(), as does remove_hotkey().
While another app is in front the shortcut is observed, not consumed: the front app also receives the keystroke.
Accessibility access
Shortcuts pressed while another app is in front need Accessibility access for the app under System Settings > Privacy & Security > Accessibility. Without it the runtime logs a warning, Python receives a hotkey:unavailable event, and shortcuts only work while the popover has focus.
A menu item shortcut= is different: it works only while the menu is open.
remove_hotkey(shortcut)¶
Unregister a shortcut. A shortcut that is not registered is ignored.
app.remove_hotkey(shortcut: str) -> None
hotkey(shortcut)¶
Decorator form of on_hotkey. It returns the function, so the function can still be called directly; unregister with remove_hotkey().
@app.hotkey("cmd+shift+n")
def show_window():
pass
System Events¶
event(name)¶
Decorator form of the on_<event> lifecycle properties, the way hotkey is for on_hotkey. Assigning the property and using the decorator do the same thing; each event holds one callback.
@app.event("wake")
def refresh():
pass
@app.event("appearance")
def themed(dark: bool):
pass
| Name | Property |
|---|---|
appear, disappear |
on_appear, on_disappear |
sleep, wake |
on_sleep, on_wake |
screen_lock, screen_unlock |
on_screen_lock, on_screen_unlock |
appearance |
on_appearance_change |
displays |
on_displays_change |
quit |
on_quit |
An unknown name raises ValueError.
A menu bar app runs for days, so these events let it pause, refresh or restyle without polling:
import nib
def main(app: nib.App):
app.title = "Uptime"
status = nib.Text("Running")
def paused():
status.content = "Paused"
@app.event("wake")
def resumed():
status.content = "Running"
app.on_sleep = paused
app.build(status)
nib.run(main)
- Callbacks run on the event thread, like button actions. Mutating views from them triggers the usual patch.
- An exception in a callback is logged and does not stop the app.
- System events fire whether or not the popover is open.
- Under
nib run, a reload re-runsmain, so callbacks are registered again. - To read the current state rather than wait for a change, use
app.screen.dark_mode()andapp.screen.displays().
Class Methods¶
set_assets_dir(path)¶
Set the assets directory for the application. Relative paths are resolved from the main script directory.
App.set_assets_dir(path: str | Path | None) -> None
resolve_asset(relative_path)¶
Resolve a relative asset path to an absolute path. Returns the input unchanged for absolute paths and URLs. Returns an empty string if the asset is not found.
App.resolve_asset(relative_path: str) -> str
Examples¶
Function-based app (recommended)¶
import nib
def main(app: nib.App):
app.title = "Counter"
app.icon = nib.SFSymbol("number.circle")
app.width = 280
app.height = 150
label = nib.Text("0", font=nib.Font.TITLE)
def increment():
label.content = str(int(label.content) + 1)
app.build(
nib.VStack(
controls=[
label,
nib.Button("Increment", on_click=increment),
],
spacing=12,
padding=20,
)
)
nib.run(main)
Class-based app¶
import nib
class CounterApp(nib.App):
count = nib.State(0)
def body(self):
return nib.VStack(
controls=[
nib.Text(f"Count: {self.count}", font=nib.Font.TITLE),
nib.Button("Increment", on_click=self.increment),
],
spacing=12,
padding=20,
)
def increment(self):
self.count += 1
CounterApp(title="Counter", icon="number.circle").run()
View as the menu bar icon¶
Any view works as the icon, and it stays reactive. Full demo: examples/demos/views/custom_icon.py.
import random
import nib
def main(app: nib.App):
chart = nib.Chart(
data=[{"x": i, "y": v} for i, v in enumerate([4, 6, 12, 14, 10, 8, 11, 9])],
marks=[nib.LineMark(x="x", y="y", foreground_style="#ffffff", line_width=1.0)],
x_axis=nib.ChartAxis(hidden=True),
y_axis=nib.ChartAxis(hidden=True),
legend=nib.ChartLegend(hidden=True),
width=30,
height=16,
)
app.icon = chart
def randomize():
chart.data = [{"x": i, "y": random.randint(4, 16)} for i in range(8)]
app.build(nib.Button("Randomize", on_click=randomize, padding=20))
nib.run(main)
Related¶
- run(): Recommended entry point for function-based apps
- SFSymbol: Menu bar icons
- Menus: status bar menu and view context menus
- Settings: saved preferences
- SettingsWindow: Preferences window
- Architecture: process model
- Reactivity: patch model
- Services:
app.battery,app.keychain, ...