docs/QUICK_REFERENCE

Nebble API Quick Reference

This guide provides a quick look at the most commonly used high-level APIs in Nebble.

1. Managed Handles (Memory Safety)

All UI and system resources use Managed Handles which utilize Nim's ARC for automatic cleanup.

Handle TypeCreationUnderlying Layer Access
WindowHandlenewWindow()h.rootLayer()
TextLayerHandlenewTextLayer(frame)h.getLayer()
BitmapLayerHandlenewBitmapLayer(frame)h.getLayer()
ActionBarLayerHandlenewActionBarLayer()h.getLayer()
MenuLayerHandlenewMenuLayer(frame)h.getLayer()
SimpleMenuLayerHandlenewSimpleMenuLayer(...)h.getLayer()
ScrollLayerHandlenewScrollLayer(frame)h.getLayer()
StatusBarLayerHandlenewStatusBarLayer()h.getLayer()
RotBitmapLayerHandlenewRotBitmapLayer(bmp)h.getLayer()
GPathHandlenewGPath(points)N/A
TimerHandleafter(ms, callback)N/A
DataLoggingSessionHandlenewDataLoggingSession(...)N/A
AppSyncHandlenewAppSyncHandle(...)N/A
ActionMenuHandleopenActionMenu(config)N/A
ContentIndicatorHandlenewContentIndicator()N/A
GBitmapRefnewBitmapRef(resource)h.bitmap

2. Declarative UI (nebbleApp)

The recommended way to define your app structure.

nebbleApp:
  window:
    backgroundColor = GColorBlack
  
  textLayer:
    id = titleLabel
    fullWidth = true
    y = center
    h = 30
    text = "Hello!"
    alignment = GTextAlignmentCenter
  
  clicks:
    BUTTON_ID_SELECT = selectHandler

Layout Properties

3. String Formatting (Heap-Free)

Use FixedString to avoid memory fragmentation. Use .cstr to safely convert to cstring for Pebble APIs.

var s: FixedString[32]
s.f("Steps: ", steps)
label.text = s.cstr  # Use .cstr template for safety

Time Formatting

var timeStr: FixedString[16]
timeStr.formatTime(tickTime)  # Uses default format
# or
timeStr.formatTime("%H:%M", tickTime)  # Custom format
timeLayer.text = timeStr.cstr

4. Graphics & Drawing

Inside a LayerUpdateProc:

5. Event Services

6. Communication

AppMessage (Result-based API)

# Send a message
let outbox = beginOutbox()
if outbox.success:
  discard dictWriteInt(outbox.iter, key, value)
  discard outboxSend()

# Receive
onInboxReceived(proc(iter: ptr DictionaryIterator, ctx: pointer) {.cdecl.} =
  let value = readInt32(iter, amkMyKey)
)

7. Phone Component (PKJS in Nim)

In src/pkjs.nim:

Important Note: Callbacks

All callbacks MUST use the {.cdecl.} pragma to be compatible with the Pebble C SDK.

proc myHandler(r: ClickRecognizerRef, c: pointer) {.cdecl.} = discard