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:
useVar | newVar | |
|---|---|---|
| Where you call it | Inside the view | In IO, during setup |
| Supported types | Int, Float, Double, Bool, Text, SortCol (useEnumVar handles any Bounded/Enum type) | Any type |
| Identity | Call position in the view | The reference itself |
| Good fit for | View-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 xbuilds a read-onlyVarwhose 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 primedResponse.withValue val widgetruns aVar-based widget against a temporaryVarand returns both the widget result and the updated value.nano-ui-emitbuildsemitChangedon top of it.zoomVar getter setterprojects a custom slice of a largerVar, andzoomVarLdoes the same using a lens.forEachVar key list bodygives each item of aVar [a]its ownVar akeyed across reorders. UsewithElemVarswhen 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 kind | When it happens | Cost |
|---|---|---|
| Forward | Written before anything reads it this pass | Single pass; later reads immediately see the new value |
| Backward | Written after being read earlier in the same pass | Triggers a second view pass (with one-shot input events cleared) |
| Unchanged | New value equals (==) old value | No-op: no extra pass and no repaint |
Two practical habits follow from this:
- Put
readVarbelow 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
readVarjust to pass the value back into a widget that already takes theVar.
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
Varonly repaints the widgets whose text, value, or style actually changed, whether they are bound to theVardirectly or render something computed fromreadVar.
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 view | useVar or useEnumVar |
| Is a list, document, settings record, or async handle | newVar in IO, held in your app record |
| Belongs to a single row in a dynamic list | A keyed Var (forEachVar, withElemVars, or VarMap) |
| Already lives in an external store or worker | An IORef or app record, waking the UI with askWake |
Fits a pure msg -> model -> model state machine | An 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.