packages feed

layoutz-1.0.0.0: examples/FocusMini.hs

module Main where

import Layoutz

data Msg = Add Int Int | Next Int | Reset | Focused String

main :: IO ()
main = runApp LayoutzApp
  { appInit = (([0, 0, 0], "a"), cmdNone)
  , appUpdate = \msg (ns, f) -> case msg of
      Add i d   -> ((bump i d ns, f), cmdNone)
      Next i    -> ((ns, f), cmdFocus (names !! ((i + 1) `mod` 3)))
      Reset     -> ((map (const 0) ns, f), cmdNone)
      Focused n -> ((ns, n), cmdNone)
  , appSubscriptions = const (subFocus Focused)
  , appView = \(ns, f) -> vstack
      [ row (zipWith3 pane [0 ..] names ns)
      , text ("focus: " <> f) & styleDim
      ] & onKey (\k -> if k == KeyChar 'r' then Just Reset else Nothing)
  }
  where
    names = ["a", "b", "c"]
    bump i d = zipWith (\j n -> if i == j then n + d else n) [0 ..]
    pane i name n = box name [display n] & fixedWidth 10
      & focusable name
      & focusStyle (fg ColorCyan . border BorderDouble)
      & onKey (\k -> case k of
          KeyUp    -> Just (Add i 1)
          KeyDown  -> Just (Add i (-1))
          KeyEnter -> Just (Next i)
          _        -> Nothing)