This guide helps you translate common Pebble C SDK patterns into idiomatic Nebble Nim code.
---
Nebble uses Managed Types (handles) which leverage Nim's ARC memory management to automatically handle resource cleanup. These are your default choice for building safe apps.
Manual C API Type | Managed Nim Handle | Creation | Destruction |
|-------------------|--------------------|----------|-------------|
Window* | WindowHandle | newWindow() | Automatic (Stack-Aware) |
TextLayer* | TextLayerHandle | newTextLayer() | Automatic (Hierarchy-Aware) |
BitmapLayer* | BitmapLayerHandle | newBitmapLayer() | Automatic (Hierarchy-Aware) |
ActionBarLayer* | ActionBarLayerHandle | newActionBarLayer() | Automatic (Stack-Aware) |
MenuLayer* | MenuLayerHandle | newMenuLayer() | Automatic (Hierarchy-Aware) |
ScrollLayer* | ScrollLayerHandle | newScrollLayer() | Automatic (Hierarchy-Aware) |
GPath* | GPathHandle | newGPath() | Automatic |
DataLoggingSessionRef | DataLoggingSessionHandle | newDataLoggingSession() | Automatic |
AppSync | AppSyncHandle | newAppSync() | Automatic |
ActionMenu* | ActionMenuHandle | openActionMenu() | Automatic |
AppTimer* | TimerHandle | after() | Automatic (RAII Cancel) |
GBitmap* | GBitmapRef | newBitmapRef() | Automatic (Ref-Counted) |
---
The most idiomatic way to build Nebble apps is using the nebbleApp macro (or its alias nebbleWatchface). It eliminates almost all boilerplate by generating the window, layers, and lifecycle automatically.
For a comprehensive guide on the DSL features and responsive layout, see Declarative DSL.
Nim (Modern DSL):
import nebble proc selectHandler(recognizer: ClickRecognizerRef; context: pointer) {.cdecl.} = vibes.shortPulse() nebbleApp: window: backgroundColor = GColorBlack textLayer: id = myLabel fullWidth = true y = center h = 40 text = "Hello Nim!" alignment = GTextAlignmentCenter clicks: BUTTON_ID_SELECT = selectHandler
---
Nebble's high-level API is organized to match the Pebble SDK structure. All high-level modules are re-exported by the top-level import nebble.
nebble/ ├── foundation/ # Core SDK Foundation APIs │ ├── app.nim # App lifecycle, pebbleApp macro │ ├── time.nim # Wall time functions │ └── events/ # Event Services (accel, battery, health...) ├── ui/ # User Interface (window, layers, animation) ├── graphics/ # Drawing, fonts, paths └── comms/ # Messaging and Sync
---
If you aren't using the DSL, use WindowHandle:
C (Manual):
static Window *window; window = window_create(); window_stack_push(window, true); // Must destroy in window_unload or deinit
Nim (Managed):
import nebble/ui/window var window: WindowHandle window = newWindow() window.push(animated = true) # No destroy() needed! ARC handles it.
---
C:
layer_add_child(root_layer, text_layer_get_layer(text_layer));
Nim (Modern):
# Hierarchy operations are generic and handle ownership automatically. # Works with any managed handle (TextLayer, BitmapLayer, etc.) parent.addChild(child)
---
C:
static char buffer[32]; snprintf(buffer, sizeof(buffer), "Count: %d", counter); text_layer_set_text(text_layer, buffer);
Nim (Modern):
var displayStr: FixedString[32] displayStr.f("Count: ", counter) textLayer.text = displayStr
---
DSL Style (Recommended):
nebbleWatchface: clicks: BUTTON_ID_SELECT = myHandler
Manual Style:
import nebble/ui/clicks onClick(BUTTON_ID_SELECT, selectHandler)
Note: All callback procs must have the {.cdecl.} pragma!
---
---