nebble/ui/window

Search:
Group by:
Source   Edit  

ARC-Managed Window Handle (Stack-Aware)

Provides automatic memory management for Window objects with window stack lifecycle tracking to prevent destroying windows while on the stack.

Key Challenge: Windows pushed to the window stack are "owned" by the stack. Destroying them while on the stack causes undefined behavior. This handle tracks stack state to defer destruction until safe.

Usage Example:

import nebble/ui/window

var mainWindow: WindowHandle

proc myInit() =
  mainWindow = newWindowHandle()
  mainWindow.push()  # Window now on stack
  # Do NOT destroy while on stack!

proc myDeinit() =
  # Pop window from stack first
  if mainWindow.pop():
    # Now safe to destroy
    mainWindow = WindowHandle(nil)

Types

WindowHandle = object

Managed handle for Window with stack lifecycle tracking.

Safety: The destructor checks the window's stack state before destroying. If the window is still on the stack, destruction is deferred and a warning is logged (in debug mode).

Source   Edit  

Procs

proc `=copy`(dest: var WindowHandle; src: WindowHandle) {.error.}
Copying disabled - use move semantics. Source   Edit  
proc `=destroy`(h: var WindowHandle) {....raises: [], tags: [], forbids: [].}
Destructor - destroys window only if safe.
  • rsCreated: Safe to destroy
  • rsInactive: Safe to destroy
  • rsActive: Unsafe - window still on stack, log warning
  • rsDestroyed: Already destroyed, do nothing
Source   Edit  
proc `=sink`(dest: var WindowHandle; src: WindowHandle) {....raises: [], tags: [],
    forbids: [].}
Move assignment - transfers ownership. Source   Edit  
proc `=wasMoved`(h: var WindowHandle) {....raises: [], tags: [], forbids: [].}
Mark handle as moved. Source   Edit  
proc backgroundColor=(h: var WindowHandle; color: GColor8) {.inline, ...raises: [],
    tags: [], forbids: [].}
Set window background color. Source   Edit  
proc canDestroy(h: WindowHandle): bool {.inline, ...raises: [], tags: [],
    forbids: [].}
Check if window can be safely destroyed. Source   Edit  
proc clickConfig=(h: var WindowHandle; provider: ClickConfigProvider) {.inline,
    ...raises: [], tags: [], forbids: [].}
Set click configuration provider. Source   Edit  
proc clickConfig=(win: ptr Window; provider: ClickConfigProvider) {.inline,
    ...raises: [], tags: [], forbids: [].}
Set click configuration provider (raw pointer version). Source   Edit  
proc clickContext(h: WindowHandle): pointer {.inline, ...raises: [], tags: [],
    forbids: [].}
Get click configuration context. Source   Edit  
proc handlers=(h: var WindowHandle; hnd: WindowHandlers) {.inline, ...raises: [],
    tags: [], forbids: [].}
Set window lifecycle handlers. Source   Edit  
proc isLoaded(h: WindowHandle): bool {.inline, ...raises: [], tags: [], forbids: [].}
Check if window has been loaded. Source   Edit  
proc isOnStack(h: WindowHandle): bool {.inline, ...raises: [], tags: [],
                                        forbids: [].}
Check if window is currently on the window stack. Source   Edit  
proc isValid(h: WindowHandle): bool {.inline, ...raises: [], tags: [], forbids: [].}
Check if handle points to valid window. Source   Edit  
proc newWindow(): WindowHandle {.inline, ...raises: [], tags: [], forbids: [].}
Alias for newWindowHandle. Source   Edit  
proc newWindow(handlers: WindowHandlers): WindowHandle {.inline, ...raises: [],
    tags: [], forbids: [].}
Alias for newWindowHandle. Source   Edit  
proc newWindowHandle(): WindowHandle {.inline, ...raises: [], tags: [], forbids: [].}

Create a new managed Window.

Example: var win = newWindowHandle() # Window in rsCreated state - safe to configure but not yet on stack

Source   Edit  
proc newWindowHandle(handlers: WindowHandlers): WindowHandle {.inline,
    ...raises: [], tags: [], forbids: [].}
Create a new managed Window with handlers. Source   Edit  
proc pop(h: var WindowHandle): bool {.inline, ...raises: [], tags: [], forbids: [].}

Pop window from stack. Returns true if this window was popped.

Transitions: rsActive -> rsInactive (if this window was on top)

Example: if mainWindow.pop(): # Safe to destroy now mainWindow = WindowHandle(nil)

Source   Edit  
proc popAll(animated: bool = true) {.inline, ...raises: [], tags: [], forbids: [].}

Pop all windows from stack.

⚠️ Warning: This affects ALL windows, not just managed ones. Managed windows still on the stack will have their state desynchronized. You MUST manually call reset() on all managed WindowHandle objects after calling popAll to ensure they are properly destroyed.

Source   Edit  
proc push(h: var WindowHandle; animated: bool = true) {.inline, ...raises: [],
    tags: [], forbids: [].}

Push window onto window stack.

Transitions: rsCreated -> rsActive

Example: mainWindow.push() # Window now displayed

Source   Edit  
proc removeFromStack(h: var WindowHandle; animated: bool = true): bool {.inline,
    ...raises: [], tags: [], forbids: [].}

Remove window from stack (even if not on top).

Transitions: rsActive -> rsInactive (if removed)

Source   Edit  
proc reset(h: var WindowHandle) {....raises: [], tags: [], forbids: [].}
Explicitly destroy window if safe and reset handle. Source   Edit  
proc rootLayer(h: WindowHandle): ptr Layer {.inline, ...raises: [], tags: [],
    forbids: [].}
Get the root layer of the window. Source   Edit  
proc rootLayer(win: ptr Window): ptr Layer {.inline, ...raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc setClickConfigWithContext(h: var WindowHandle;
                               provider: ClickConfigProvider; context: pointer) {.
    inline, ...raises: [], tags: [], forbids: [].}
Set the click configuration provider with a context pointer. Source   Edit  
proc setHandlers(h: var WindowHandle; load: WindowHandler = nil;
                 unload: WindowHandler = nil; appear: WindowHandler = nil;
                 disappear: WindowHandler = nil) {.inline, ...raises: [], tags: [],
    forbids: [].}
Set window lifecycle handlers using named parameters. Source   Edit  
proc state(h: WindowHandle): ResourceState {.inline, ...raises: [], tags: [],
    forbids: [].}
Get current window state. Source   Edit  
proc toHandle(p: ptr Window): WindowHandle {....raises: [], tags: [], forbids: [].}
Wrap raw pointer in handle (unowned). Source   Edit  
proc userData(h: WindowHandle): pointer {.inline, ...raises: [], tags: [],
    forbids: [].}
Get user data pointer. Source   Edit  
proc userData=(h: var WindowHandle; data: pointer) {.inline, ...raises: [],
    tags: [], forbids: [].}
Set user data pointer. Source   Edit  

Converters

converter toPtr(h: WindowHandle): ptr Window {....raises: [], tags: [], forbids: [].}
Convert handle to raw pointer for C API calls. Source   Edit