System Monitor¶
A popover in the style of macOS Control Center that shows battery, battery health, thermal, display and network information from Nib's system services. It refreshes every time the popover opens.
What it shows¶
app.battery,app.connectivity,app.screenapp.on_appearto refresh when the popover opens- Control Center modules: a
VStackon a translucentRoundedRectangle - Icon badges that change symbol and colour with the data
ProgressViewbars for battery level and brightness

import time
import nib
def main(app: nib.App):
app.title = "System Info"
app.icon = nib.SFSymbol("info.circle.fill")
app.width = 340
app.height = 470
# Control Center colours
white = nib.Color.WHITE
secondary = white.with_opacity(0.55)
inactive = white.with_opacity(0.18)
blue = "#0A84FF"
green = "#30D158"
orange = "#FF9F0A"
red = "#FF453A"
yellow = "#FFD60A"
# ── Helpers ──────────────────────────────────────────────
def module(*controls, spacing=10, **kwargs):
"""A Control Center module: a translucent rounded panel."""
return nib.VStack(
controls=list(controls),
alignment=nib.HorizontalAlignment.LEADING,
spacing=spacing,
padding=14,
max_width="infinity",
background=nib.RoundedRectangle(
corner_radius=18,
fill=white.with_opacity(0.07),
stroke_color=white.with_opacity(0.09),
stroke_width=1,
),
**kwargs,
)
def badge_controls(symbol, color):
return [
nib.Circle(fill=color, width=32, height=32),
nib.SFSymbol(
symbol,
font=nib.Font.system(14, weight=nib.FontWeight.SEMIBOLD),
foreground_color=white,
),
]
def badge(symbol, color=inactive):
"""A white symbol in a coloured circle. Reassign `controls` to change it."""
return nib.ZStack(controls=badge_controls(symbol, color))
def title(text):
return nib.Text(text, font=nib.Font.system(14, weight=nib.FontWeight.SEMIBOLD))
def subtitle(text="--"):
return nib.Text(text, font=nib.Font.system(12), foreground_color=secondary)
def item(icon, heading, detail):
"""A badge next to a title and a subtitle, like Wi-Fi in Control Center."""
return nib.HStack(
controls=[
icon,
nib.VStack(
controls=[title(heading), detail],
alignment=nib.HorizontalAlignment.LEADING,
spacing=1,
),
nib.Spacer(),
],
spacing=10,
)
# ── Network ──────────────────────────────────────────────
net_icon = badge("wifi")
net_detail = subtitle()
def refresh_network():
info = app.connectivity.status()
if info.is_connected:
symbol = "cable.connector" if info.type.value == "ethernet" else "wifi"
net_icon.controls = badge_controls(symbol, blue)
net_detail.content = {"wifi": "Wi-Fi", "ethernet": "Ethernet"}.get(
info.type.value, "Connected"
)
else:
net_icon.controls = badge_controls("wifi.slash", inactive)
net_detail.content = "Offline"
# ── Battery ──────────────────────────────────────────────
power_icon = badge("bolt.fill")
power_detail = subtitle()
battery_level = subtitle("")
def level_bar(value, color):
return nib.ProgressView(value=value, style=nib.ProgressStyle.LINEAR, tint=color)
battery_bar = nib.HStack(controls=[level_bar(0, green)])
battery_time = subtitle("")
battery_temp = subtitle("")
def refresh_battery():
info = app.battery.status()
if not info.has_battery or info.level is None:
power_icon.controls = badge_controls("powerplug.fill", blue)
power_detail.content = "No Battery"
return
level = info.level
color = green if level >= 50 else orange if level >= 20 else red
power_icon.controls = badge_controls(
"bolt.fill" if info.is_charging else "battery.100", color
)
power_detail.content = {
"charging": "Charging",
"full": "Charged",
"pluggedNotCharging": "Plugged In",
"acPower": "Power Adapter",
}.get(info.state.value, "On Battery")
battery_level.content = f"{level:.0f}%"
battery_bar.controls = [level_bar(level / 100, color)]
if info.is_charging and info.time_to_full_formatted:
battery_time.content = f"Full in {info.time_to_full_formatted}"
elif info.time_remaining_formatted:
battery_time.content = f"{info.time_remaining_formatted} remaining"
else:
battery_time.content = ""
# ── Battery health ───────────────────────────────────────
health_detail = subtitle()
cycles_detail = subtitle()
def refresh_health():
h = app.battery.health()
if h.health_percent is not None:
health_detail.content = f"{h.health_percent:.0f}%, {h.condition or 'N/A'}"
else:
health_detail.content = "N/A"
cycles_detail.content = str(h.cycle_count) if h.cycle_count else "N/A"
if h.temperature_celsius:
battery_temp.content = f"{h.temperature_celsius:.1f} °C"
# ── Thermal ──────────────────────────────────────────────
thermal_icon = badge("thermometer.medium")
thermal_detail = subtitle()
def refresh_thermal():
state = app.battery.thermal_state().state
if state == nib.ThermalState.NOMINAL:
color = green
elif state == nib.ThermalState.FAIR:
color = orange
else:
color = red
thermal_icon.controls = badge_controls("thermometer.medium", color)
thermal_detail.content = state.value.title()
# ── Display ──────────────────────────────────────────────
display_name = subtitle("")
display_bar = nib.ProgressView(value=0, style=nib.ProgressStyle.LINEAR, tint=yellow)
display_detail = subtitle()
def refresh_display():
info = app.screen.info()
display_name.content = info.name or ""
if info.brightness is not None:
display_bar.value = info.brightness
rate = f", {info.refresh_rate:.0f} Hz" if info.refresh_rate else ""
display_detail.content = (
f"{info.width:.0f} x {info.height:.0f} @{info.scale:.0f}x{rate}"
)
# ── Refresh all ──────────────────────────────────────────
updated = subtitle("")
def refresh_all():
for refresh in (
refresh_network,
refresh_battery,
refresh_health,
refresh_thermal,
refresh_display,
):
try:
refresh()
except Exception:
pass # a service the Mac lacks keeps its "--"
updated.content = "Updated " + time.strftime("%H:%M")
app.on_appear = refresh_all
# ── Modules ──────────────────────────────────────────────
status = module(
item(net_icon, "Network", net_detail),
item(power_icon, "Power", power_detail),
item(thermal_icon, "Thermal", thermal_detail),
spacing=14,
max_height="infinity",
)
health = module(
item(badge("heart.fill"), "Health", health_detail),
max_height="infinity",
)
cycles = module(
item(badge("arrow.triangle.2.circlepath"), "Cycles", cycles_detail),
max_height="infinity",
)
def bar_row(symbol, bar):
return nib.HStack(
controls=[
nib.SFSymbol(
symbol, font=nib.Font.system(13), foreground_color=secondary
),
bar,
],
spacing=10,
)
display = module(
nib.HStack(controls=[title("Display"), nib.Spacer(), display_name]),
bar_row("sun.max.fill", display_bar),
display_detail,
)
battery = module(
nib.HStack(controls=[title("Battery"), nib.Spacer(), battery_level]),
bar_row("battery.100", battery_bar),
nib.HStack(controls=[battery_time, nib.Spacer(), battery_temp]),
)
footer = module(
nib.HStack(
controls=[
nib.ZStack(
controls=[
nib.RoundedRectangle(
corner_radius=8, fill=white.with_opacity(0.12),
width=40, height=40,
),
nib.SFSymbol("info.circle.fill", font=nib.Font.system(18)),
]
),
nib.VStack(
controls=[title("System Info"), updated],
alignment=nib.HorizontalAlignment.LEADING,
spacing=1,
),
nib.Spacer(),
nib.Button(
content=nib.SFSymbol(
"arrow.clockwise",
font=nib.Font.system(15, weight=nib.FontWeight.SEMIBOLD),
foreground_color=secondary,
),
on_click=refresh_all,
style=nib.ButtonStyle.PLAIN,
),
],
spacing=12,
),
)
app.build(
nib.VStack(
controls=[
nib.HStack(
controls=[
status,
nib.VStack(controls=[health, cycles], spacing=10),
],
spacing=10,
height=160,
),
display,
battery,
footer,
],
spacing=10,
padding=12,
)
)
nib.run(main)
Walkthrough¶
Services¶
Each module reads one service:
| Module | Service | Method |
|---|---|---|
| Network, Power | app.connectivity, app.battery |
status() |
| Thermal | app.battery |
thermal_state() |
| Health, Cycles, battery temperature | app.battery |
health() |
| Display | app.screen |
info() |
| Battery | app.battery |
status() |
Services are synchronous. Call the method and use the result:
info = app.battery.status()
battery_level.content = f"{info.level:.0f}%"
battery_bar.value = info.level / 100
Refreshing on popover open¶
app.on_appear = refresh_all
on_appear runs every time the popover opens, so the values are current whenever the user looks, with no polling in the background. The refresh button in the last module calls the same function.
Modules¶
Control Center groups its controls in rounded, translucent panels. module builds one: a leading-aligned VStack that fills the width, with a RoundedRectangle as its background.
def module(*controls, spacing=10, **kwargs):
return nib.VStack(
controls=list(controls),
alignment=nib.HorizontalAlignment.LEADING,
spacing=spacing,
padding=14,
max_width="infinity",
background=nib.RoundedRectangle(
corner_radius=18,
fill=white.with_opacity(0.07),
stroke_color=white.with_opacity(0.09),
stroke_width=1,
),
**kwargs,
)
The top row is an HStack of the status module and a VStack of two smaller modules. Both sides take max_height="infinity", and the row has a fixed height, so the two columns end at the same line.
Icon badges¶
A badge is a white SF Symbol on a coloured circle, layered with a ZStack:
def badge_controls(symbol, color):
return [
nib.Circle(fill=color, width=32, height=32),
nib.SFSymbol(symbol, font=nib.Font.system(14, weight=nib.FontWeight.SEMIBOLD),
foreground_color=white),
]
net_icon = nib.ZStack(controls=badge_controls("wifi", inactive))
To change the symbol and the colour, the refresh function assigns new children to the ZStack. controls is reactive, so the badge is patched in place:
net_icon.controls = badge_controls("wifi.slash", inactive)
The Power badge turns green, orange or red with the battery level, and the Thermal badge with the thermal state. The battery bar is replaced the same way, so it takes the level's colour:
battery_bar.controls = [level_bar(level / 100, color)]
Missing hardware¶
for refresh in (refresh_network, refresh_battery, ...):
try:
refresh()
except Exception:
pass
A desktop Mac has no battery, and some displays report no brightness. Each refresh function runs on its own, so a service that fails leaves its module at -- and the others still update.
Related¶
- Battery
- Connectivity
- Screen
- App:
on_appear - ProgressView
- Layout: stacks and sizing