Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

State & Var

Because a view runs fresh on every frame, any value that needs to survive from one frame to the next must be stored explicitly. In nano-ui, most mutable state lives in a Var, a typed mutable reference. Input widgets take a Var directly and update it in place, so you do not need to wire up onChange callbacks.

counter :: NanoUI ()
counter = rowWith (gap 8 . alignMiddle) $ do
  n <- useVar (0 :: Int)
  whenM (button "-") (modifyVar n (subtract 1))
  label . T.pack . show =<< readVar n
  whenM (button "+") (modifyVar n (+ 1))

Store state, derive everything else

Keep a value in a Var only when it cannot be recomputed from other state. Anything you can calculate from existing state belongs in a regular Haskell binding computed fresh each frame:

appView :: App -> NanoUI ()
appView app = do
  -- Keep: the inputs to the next frame.
  draft <- useVar ""
  settings <- readVar app.appSettings
  textInput draft

  -- Derive: computed from what was kept.
  len <- T.length <$> readVar draft
  label ("Signed in as " <> settings.nickname)
  label ("Draft: " <> T.pack (show len) <> " characters")

Duplicating a derived value into a second Var invites bugs as soon as one code path updates the source and forgets the copy.

Creating Vars

You have two main ways to create a Var, plus VarMap for dynamic keyed collections:

useVarnewVar
Where you call itInside the viewIn IO, during setup
Supported typesInt, Float, Double, Bool, Text, SortCol (useEnumVar handles any Bounded/Enum type)Any type
IdentityCall position in the viewThe reference itself
Good fit forView-local state (input drafts, open flags, filters)Shared or app-wide state (documents, lists, settings records)

useVar needs no boilerplate. Its initial value is only used the first time that slot is created; to reset it later, call writeVar. Although useVar tracks its slots separately from widget IDs, a conditional useVar still needs scope so slots after it keep their positions:

advanced :: NanoUI ()
advanced = do
  showAdvanced <- useVar False
  checkbox "Advanced" showAdvanced
  scope . whenM (readVar showAdvanced) $ do
    level <- useVar (3 :: Float)
    slider 0 10 level

newVar works for any Haskell type. Create it once in IO before starting the window loop and pass it into your view:

data App = App
  { appSettings :: Var Settings
  , appTodos :: Var [Todo]
  }
  deriving (Generic)

newApp :: IO App
newApp = App <$> newVar (Settings 40 False "Ada") <*> newVar []

main :: IO ()
main = do
  app <- newApp
  runSdlApp defaultSdlOptions (appView app)

Passing the same Var to multiple widgets lets them share state directly. Never call liftIO (newVar ...) inside the view, or you will allocate a brand-new Var on every pass.

For collections whose items come and go, VarMap stores one Var per key:

data RowState = RowState {editing :: Bool, rowDraft :: Text}
  deriving (Eq, Generic)

newRows :: IO (VarMap Int RowState)
newRows = newVarMap

todoRows :: VarMap Int RowState -> [Int] -> NanoUI ()
todoRows rows liveIds = do
  for_ liveIds $ \todoId -> withKey todoId $ do
    rowState <- varAt rows todoId (RowState False "")
    checkbox "Editing" rowState.editing
  retainVars (`elem` liveIds) rows

varAt looks up the Var for a key (allocating one if needed), dropVar removes a single entry, and retainVars keep clears out any key that fails the predicate.

Projections and read-only views

If a Var holds a record that derives Generic, OverloadedRecordDot projects individual fields into their own Vars:

data Settings = Settings
  { volume :: !Float
  , silent :: !Bool
  , nickname :: !Text
  }
  deriving (Eq, Generic)

settingsView :: Var Settings -> NanoUI ()
settingsView settings = do
  slider 0 100 settings.volume
  checkbox "Silent" settings.silent
  textInput settings.nickname

Here settings.volume is a Var Float targeting just that field. Dragging the volume slider will not repaint widgets that only read settings.nickname.

Several helpers cover other shapes:

  • constVar x builds a read-only Var whose writes are no-ops. Use it when you want to display a derived value inside an input widget and handle changes through the widget’s primed Response.
  • withValue val widget runs a Var-based widget against a temporary Var and returns both the widget result and the updated value. nano-ui-emit builds emitChanged on top of it.
  • zoomVar getter setter projects a custom slice of a larger Var, and zoomVarL does the same using a lens.
  • forEachVar key list body gives each item of a Var [a] its own Var a keyed across reorders. Use withElemVars when you need to filter or sort the list yourself before drawing.

What a write costs

Calling readVar records a dependency for the current frame pass. Whenever a write changes the stored value, nano-ui handles it in one of three ways:

Write kindWhen it happensCost
ForwardWritten before anything reads it this passSingle pass; later reads immediately see the new value
BackwardWritten after being read earlier in the same passTriggers a second view pass (with one-shot input events cleared)
UnchangedNew value equals (==) old valueNo-op: no extra pass and no repaint

Two practical habits follow from this:

  • Put readVar below the widget that edits the value whenever the layout allows it. Typing or dragging then finishes in a single pass rather than running a second pass per change.
  • Do not call readVar just to pass the value back into a widget that already takes the Var.

Watch out for floating-point NaN: since NaN /= NaN, writing a freshly computed NaN every frame looks like a new value each time and keeps the window redrawing endlessly. Guard float calculations that can produce NaN.

Tip

Updating a Var only repaints the widgets whose text, value, or style actually changed, whether they are bound to the Var directly or render something computed from readVar.

If you need to inspect a Var without recording a read dependency, use peekVar. From worker threads or plain IO, use the thread-safe functions readVarIO, writeVarIO, and modifyVarIO, then invoke the wake callback from askWake to schedule a frame. Never touch the UI Context from a background thread.

Lifetimes and external handles

Tasks, streams, image handles, drag handles, plot caches, and file-dialog IDs follow the same rule as newVar: create them once in IO during setup and keep them in your application state record.

data Editor = Editor
  { editorDocument :: Var TextDocument
  , editorSearch :: Task Text [Text]
  }

newEditor :: IO Editor
newEditor = Editor <$> newVar emptyDocument <*> newTask

Allocating any of these inside the view leaks resources on every frame. When a resource needs to track dynamic keys, either allocate the handle at setup and key its hook (useTask, useStream, useImageRgba), or let a VarMap manage it.

Where should a piece of state live?

If the value…Store it in
Is a local draft, toggle, selection, or filter for one viewuseVar or useEnumVar
Is a list, document, settings record, or async handlenewVar in IO, held in your app record
Belongs to a single row in a dynamic listA keyed Var (forEachVar, withElemVars, or VarMap)
Already lives in an external store or workerAn IORef or app record, waking the UI with askWake
Fits a pure msg -> model -> model state machineAn Elm-style reducer

Next: read Widget Identity & Keys to see how keyed lists and conditional UI preserve widget state, or check out Elm-Style State for the reducer alternative.