Desktop Windows
This chapter covers configuring native windows, reacting to window events, opening secondary OS windows, building frameless custom title bars, and capturing screenshots.
Configuring and running a window
main :: IO ()
main = do
docName <- newVar ("untitled.txt" :: Text)
runSdlApp opts (editor docName)
opts :: SdlOptions
opts =
defaultSdlOptions
{ sdlWindowSettings =
defaultWindowSettings
{ wsTitle = "Main Window"
, wsSize = Size 1280 800
, wsMinSize = Just (Size 640 480)
, wsMode = Windowed -- Windowed | Fullscreen | Hidden
, wsTransparent = False
, wsResizable = True
}
}
editor :: Var Text -> NanoUI ()
editor docName = column $ do
textInput docName
name <- readVar docName
setWindowTitle (name <> " - My App") -- setter: every frame
whenM (button "Maximize") toggleMaximized -- command: on click only
whenM (button "Quit") quit
runSdlApp :: SdlOptions -> NanoUI () -> IO () opens a native window using opts and runs the event loop until the window closes. On each frame, it runs your view on the main display thread, solves layout, diffs the draw commands against the previous frame, and repaints only the changed rectangles. (If your view modifies a Var it already read earlier in the same pass, it runs a second pass before layout.)
Frames only run when an input event, window event, timer, animation, or background task wakes the loop. When a window sits idle, no frames run at all.
NanoUI wraps an effectful Eff stack carrying frame input, the layout tree, and window handles. Because only a backend runner (or NanoUI.Testing) executes it, allocate long-lived state like docName in IO before starting the loop and pass it into your view.
All window dimensions use logical layout units. Physical pixel dimensions are logical units multiplied by winScale, which the backend queries from the OS display. Leaving wsPosition at WindowPositionDefault lets the window manager choose the initial placement. On SDL, wsOpacity controls overall window translucency.
Controlling the window from a view
Because the OS window persists across the entire session while your view runs every frame, window actions fall into two groups: declarative setters and imperative commands.
Declarative setters
Setters remember the last value they sent to the OS and skip the backend call when nothing changed. You can call them unconditionally on every frame (as editor above does with setWindowTitle), and skipping a setter simply leaves the current value in place:
setWindowTitlesetWindowModesetWindowMinSize/setWindowMaxSizesetWindowOpacitysetWindowIcon
A repeated setter call costs only an Eq check. For setWindowIcon, build the RgbaPixels once at startup and reuse that same value every frame: ByteString equality returns immediately when both sides share the same buffer, whereas freshly decoded icons compare byte by byte.
Imperative commands
Commands forward directly to the OS on every call, because the user can also move, resize, or maximize the window independently and there is no single desired value to cache. Always guard commands behind one-shot events such as button:
moveWindow(takes desktop coordinates on SDL, physical pixels on RGFW)centerWindowresizeWindowminimizeWindow,maximizeWindow,restoreWindow,toggleMaximized
Keep in mind that SDL applies commands asynchronously and reports the updated geometry back through an OS event, so askWindow still sees the old size during the frame the command ran and updates on the following frame.
Call quit to close the window after the current frame finishes painting (runSdlApp then returns). Inside a secondary nativeWindow, quit closes only that child window.
Reading window state and updating from worker threads
Window operations read the active session from NanoUI, which is only live on the UI thread during a frame. To update a window from a background thread, write to a Var with writeVarIO, wake the loop with askWake, and let the view apply the setter on the next frame (see State & Var).
askWindow :: NanoUI WindowState returns the current window metrics captured at the start of the frame: logical size, display scale, position, focus, maximized/fullscreen flags, and winCloseRequested. Calling askWindow opts your view into redrawing whenever window metadata changes (focus, scale, maximize or fullscreen toggles). Pure position moves update winPosition without forcing a repaint, and views that never call askWindow skip metadata redraws completely.
Confirming before closing
By default, clicking the OS close button ends the session immediately. To show an “unsaved changes” prompt instead, set wsExitOnCloseRequest = False.
When the user requests a close, the backend keeps the window open, sets winCloseRequested to True for a single frame, and then clears it. Copy that signal into a Var so your confirmation modal stays open across subsequent frames:
confirmMain :: IO ()
confirmMain =
runSdlApp
defaultSdlOptions
{ sdlWindowSettings = defaultWindowSettings {wsExitOnCloseRequest = False} }
appView
appView :: NanoUI ()
appView = do
confirming <- useVar False
mainContent
-- True on one frame only: keep it in a Var so the modal stays open.
whenM (winCloseRequested <$> askWindow) (writeVar confirming True)
modal confirming "Quit?" $ do
label "Unsaved changes will be lost."
whenM (button "Quit") quit
whenM (button "Cancel") (writeVar confirming False)
For a full example that only prompts when the buffer is dirty, see Tutorial 2: Build a Notepad.
Multiple native windows
nativeWindow :: Var Bool -> WindowSettings -> NanoUI () -> NanoUI () opens a second OS window whenever its Var Bool is True and runs body as that window’s view. Just like modal, call nativeWindow unconditionally on every frame. The backend creates the OS window on the first frame the flag turns True and destroys it when the flag turns False (or if a frame skips calling it). Clicking the secondary window’s close button sets the Var back to False.
inspectorMain :: IO ()
inspectorMain = newVar False >>= runSdlApp defaultSdlOptions . editorView
editorView :: Var Bool -> NanoUI ()
editorView showInspector = do
checkbox "Inspector" showInspector
nativeWindow showInspector
defaultWindowSettings {wsTitle = "Inspector", wsSize = Size 360 480}
inspectorView
A few important details about secondary windows:
- Independent focus and IDs. Each native window owns its own keyboard focus, text carets, scroll positions, and hover state.
useVarcalls insidebodycount from zero independently of the parent window. - Initial settings apply once.
nativeWindowreadsWindowSettingsonly when opening the window. To update the title or mode afterward, call setters likesetWindowTitleinsidebody. - Closures stay fresh. Every frame of the parent view also re-evaluates each open child
body, so any local values captured from the parent stay up to date. - Watch parent animation rates. Because child views re-evaluate when the parent renders, a parent animating at 60 fps also lays out open child windows at 60 fps (though a child only repaints pixels when its draw list actually changes).
- Nesting and shared state. Child windows can open further windows of their own, and all windows share state directly through
Vars (a write in one window shows up in the others on their next frame). - Elm-style usage. With
NanoUIE(Elm-Style State), child windows emit to the same reducer. To store the open flag in your model, passconstVar model.inspectorOpen, setwsExitOnCloseRequest = False, and emit a close message whenwinCloseRequestedfires. - Platform notes. Both SDL and RGFW support
nativeWindow, while headlessNanoUI.Testingignores it without runningbody. On Windows, Win32 blocks the message pump during a window resize drag, so sibling windows pause redrawing until the drag ends.
Run cabal run nano-ui-example-windows to try a three-window example with shared state.
Custom title bars
On SDL, sdlWindowDecorations hides the default OS title bar so you can draw your own. Pair it with windowCaption :: [Rect] -> NanoUI Bool (exported from nano-ui-sdl), which renders minimize, maximize, and close buttons and returns True when the close button is clicked (minimize and maximize are handled automatically):
framelessOpts :: SdlOptions
framelessOpts = defaultSdlOptions
{ sdlWindowDecorations = DecorationsNone
, sdlWindowSettings = defaultWindowSettings {wsSize = Size 1000 720}
}
framelessView :: NanoUI ()
framelessView = columnWith (fillW . fillH) $ do
titleBar
mainContent
titleBar :: NanoUI ()
titleBar = rowWith (fixedH 32 . fillW . alignMiddle) $ do
menuResp <- button' "File" -- interactive: exclude from dragging
label "My Application"
spacer (Grow 1) Fit
closeClicked <- windowCaption [respRect menuResp]
when closeClicked quit
Any remaining strip across the top of the window acts as a native drag handle. Specifically, windowCaption starts with the full width of the caption row, subtracts the caption buttons, any open floating panels (menus or modals), and the interactive exclusion rectangles you pass in ([respRect menuResp] above), and registers the leftover spans with SDL’s hit-test callback. When the user drags one of those spans, the OS moves the window natively without sending a press to the view.
Pass the respRect of every clickable control sitting in your custom title bar. windowCaption scales those logical rectangles by winScale automatically. Note that because respRect reflects the previous frame’s layout, exclusion zones trail a moving widget by one frame, and any frame that omits windowCaption clears the drag spans.
Choose between two decoration modes:
DecorationsFramehides the title bar while preserving the native OS resize border.DecorationsNoneremoves both the title bar and OS border (on resizable windows, the outer edges still act as resize borders).
Use windowCaptionWith to customize button appearance or resize border thickness. windowFrame (from NanoUI.Chrome) wraps your view in a full-window panel with a rounded border frameWidth thick and insets the view by frameWidth:
framedView :: NanoUI ()
framedView = windowFrame (defaultWindowFrame (colorRGBA 70 70 80 255)) $ do
mainContent
Screenshots
nano-ui gives you three ways to capture rendered frames, depending on where you handle the pixels.
Fire-and-forget callback (requestScreenshot)
requestScreenshot :: (Maybe Screenshot -> IO ()) -> NanoUI () schedules a pixel readback for the frame currently being built. Right after presenting the frame to the screen, the backend copies the pixels and invokes your callback (passing Nothing on headless or unsupported backends):
screenshotButton :: NanoUI ()
screenshotButton =
whenM (button "Screenshot") $
requestScreenshot $ \maybeScreenshot -> for_ maybeScreenshot saveShot
Reading back a full GPU frame copies 4 bytes per physical pixel (around 16 MB for a 1280x800 window at 2x scale). Because the callback runs on the display thread before the next frame begins, keep it brief: stash the pixels in an IORef or forkIO the image encoding.
Background capture (askScreenshot)
askScreenshot :: NanoUI (IO (Maybe Screenshot)) returns a blocking IO action bound to the current window. Call that action from a background thread: it wakes the loop and waits until the next frame is presented. Never invoke it on the display thread, or the loop will deadlock waiting for itself.
Declarative capture (useScreenshot)
useScreenshot :: Eq k => Task k (Maybe Screenshot) -> k -> NanoUI (Maybe Screenshot) captures one screenshot per key and hands the result back inside your view, using a Task allocated during setup. When the key changes, it kicks off a capture and returns the previous screenshot until the new one finishes a frame or two later:
data App = App
{ appShots :: Task Int (Maybe Screenshot)
, appCount :: Var Int
}
screenshotMain :: IO ()
screenshotMain = newApp >>= runSdlApp defaultSdlOptions . view
newApp :: IO App
newApp = App <$> newTask <*> newVar 0
view :: App -> NanoUI ()
view app = do
whenM (button "Screenshot") (modifyVar (appCount app) (+ 1))
shotKey <- readVar (appCount app)
shot <- if shotKey == 0 then pure Nothing else useScreenshot (appShots app) shotKey
for_ shot showShot
If you are working directly with SdlEnv (from askSdlEnv), nano-ui-sdl also exposes saveScreenshot env path (writes a BMP file) and captureScreenshot env (returns raw pixels) for the last presented frame. Call both on the display thread, and encode formats like PNG with JuicyPixels on a worker thread.
Testing views that use window APIs
Window setters and commands become no-ops when no OS window is attached, so views that call them still run under headless NanoUI.Testing. Testing screenshots or custom title bars requires a real SDL window (see the hidden-window tests in nano-ui-demo).