nimphea/ui/menu_builder

Source   Edit  

Menu Builder DSL

Nim-friendly, zero-allocation DSL for building menus declaratively.

This module provides a compile-time macro defineMenu that generates a fully initialized BuiltMenu with static storage, ensuring memory safety and adhering to the project's zero-heap-allocation policy.

Features:

  • Zero heap allocation (uses static arrays)
  • Compile-time validation
  • Type-safe integration with variables
  • Automatic C++ pointer handling

Example:

var volume: cfloat = 50.0
var mute: bool = false

# Generates 'mainMenu' variable in current scope
defineMenu mainMenu:
  value "Volume", volume, 0.0, 100.0
  checkbox "Mute", mute
  action "Save", saveSettings
  submenu "Advanced", advancedMenu
  close "Back"

Usage: Pass mainMenu (or mainMenu.menu) to ui.openPage().

Types

BuiltMenu[N] = object
  menu*: FullScreenItemMenu
  items*: array[N, MenuItemConfig]
Static container for menu and its items Source   Edit  

Converters

converter toMenu[N](bm: var BuiltMenu[N]): var FullScreenItemMenu
Source   Edit  
converter toMenuPtr[N](bm: var BuiltMenu[N]): ptr FullScreenItemMenu
Source   Edit  

Macros

macro defineMenu(varName: untyped; body: untyped): untyped

Define a statically allocated menu.

Parameters:

  • varName: Name of the variable to declare
  • body: DSL block defining menu items

Generates:

  • var varName: BuiltMenu[N]
  • Initialization code
Source   Edit  

Templates

template linear(min, max, initial: cfloat; unit: string = ""): MappedFloatValue
Source   Edit  
template logarithmic(min, max, initial: cfloat; unit: string = ""): MappedFloatValue
Source   Edit