brick/programs/ViewportScrollDemo.hs

85 lines
2.7 KiB
Haskell
Raw Normal View History

{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
2015-07-09 21:03:48 +03:00
module Main where
import Control.Monad (void)
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid ((<>))
#endif
import qualified Graphics.Vty as V
2015-07-09 21:03:48 +03:00
import qualified Brick.Types as T
import qualified Brick.Main as M
import qualified Brick.Widgets.Center as C
import qualified Brick.Widgets.Border as B
import Brick.Types
( Widget
, ViewportType(Horizontal, Vertical, Both)
)
2017-01-25 00:48:45 +03:00
import Brick.AttrMap
( attrMap
)
import Brick.Widgets.Core
( hLimit
, vLimit
, hBox
, vBox
, viewport
, str
)
2015-07-09 21:03:48 +03:00
data Name = VP1
| VP2
| VP3
deriving (Ord, Show, Eq)
2015-07-09 21:03:48 +03:00
drawUi :: () -> [Widget Name]
2015-07-09 21:03:48 +03:00
drawUi = const [ui]
where
ui = C.center $ B.border $ hLimit 60 $ vLimit 21 $
vBox [ pair, B.hBorder, singleton ]
singleton = viewport VP3 Both $
vBox $ str "Press ctrl-arrow keys to scroll this viewport horizontally and vertically."
: (str <$> [ "Line " <> show i | i <- [2..25::Int] ])
pair = hBox [ viewport VP1 Vertical $
vBox $ str "Press up and down arrow keys" :
str "to scroll this viewport." :
(str <$> [ "Line " <> (show i) | i <- [3..50::Int] ])
, B.vBorder
, viewport VP2 Horizontal $
str "Press left and right arrow keys to scroll this viewport."
]
2015-07-09 21:03:48 +03:00
vp1Scroll :: M.ViewportScroll Name
vp1Scroll = M.viewportScroll VP1
2015-07-09 21:03:48 +03:00
vp2Scroll :: M.ViewportScroll Name
vp2Scroll = M.viewportScroll VP2
2015-07-09 21:03:48 +03:00
vp3Scroll :: M.ViewportScroll Name
vp3Scroll = M.viewportScroll VP3
appEvent :: T.BrickEvent Name e -> T.EventM Name () ()
appEvent (T.VtyEvent (V.EvKey V.KDown [V.MCtrl])) = M.vScrollBy vp3Scroll 1
appEvent (T.VtyEvent (V.EvKey V.KUp [V.MCtrl])) = M.vScrollBy vp3Scroll (-1)
appEvent (T.VtyEvent (V.EvKey V.KRight [V.MCtrl])) = M.hScrollBy vp3Scroll 1
appEvent (T.VtyEvent (V.EvKey V.KLeft [V.MCtrl])) = M.hScrollBy vp3Scroll (-1)
appEvent (T.VtyEvent (V.EvKey V.KDown [])) = M.vScrollBy vp1Scroll 1
appEvent (T.VtyEvent (V.EvKey V.KUp [])) = M.vScrollBy vp1Scroll (-1)
appEvent (T.VtyEvent (V.EvKey V.KRight [])) = M.hScrollBy vp2Scroll 1
appEvent (T.VtyEvent (V.EvKey V.KLeft [])) = M.hScrollBy vp2Scroll (-1)
appEvent (T.VtyEvent (V.EvKey V.KEsc [])) = M.halt
appEvent _ = return ()
2015-07-09 21:03:48 +03:00
app :: M.App () e Name
2015-07-09 21:03:48 +03:00
app =
M.App { M.appDraw = drawUi
, M.appStartEvent = return ()
, M.appHandleEvent = appEvent
2017-01-25 00:48:45 +03:00
, M.appAttrMap = const $ attrMap V.defAttr []
, M.appChooseCursor = M.neverShowCursor
}
2015-07-09 21:03:48 +03:00
main :: IO ()
main = void $ M.defaultMain app ()