Enhance MouseDemo to show interaction between clickable and viewport.

This commit is contained in:
Kevin Quick 2016-11-30 00:06:44 -07:00
parent f3a37615b6
commit 3e51e2e5c2

View File

@ -12,17 +12,19 @@ import qualified Graphics.Vty as V
import qualified Brick.Types as T
import Brick.AttrMap
import Brick.Util
import Brick.Types (Widget)
import Brick.Types (Widget, ViewportType(Vertical))
import qualified Brick.Main as M
import qualified Brick.Widgets.Center as C
import qualified Brick.Widgets.Border as B
import Brick.Widgets.Core
data Name = Info | Button1 | Button2 | Button3 deriving (Show, Ord, Eq)
data Name = Info | Button1 | Button2 | Button3 | Prose
deriving (Show, Ord, Eq)
data St =
St { _clicked :: [T.Extent Name]
, _lastReportedClick :: Maybe (Name, T.Location)
, _prose :: String
}
makeLenses ''St
@ -30,6 +32,7 @@ makeLenses ''St
drawUi :: St -> [Widget Name]
drawUi st =
[ buttonLayer st
, proseLayer st
, infoLayer st
]
@ -51,6 +54,18 @@ buttonLayer st =
padLeftRight (if wasClicked then 2 else 3) $
str (if wasClicked then "<" <> label <> ">" else label)
proseLayer :: St -> Widget Name
proseLayer st =
B.border $
C.hCenterLayer $
vLimit 8 $
-- n.b. if clickable and viewport are inverted here, click event
-- coordinates will only identify the viewable range, not the actual
-- editor widget coordinates.
viewport Prose Vertical $
clickable Prose $
vBox $ map str $ lines (st^.prose)
infoLayer :: St -> Widget Name
infoLayer st = T.Widget T.Fixed T.Fixed $ do
c <- T.getContext
@ -66,6 +81,8 @@ appEvent :: St -> T.BrickEvent Name e -> T.EventM Name (T.Next St)
appEvent st (T.MouseDown n _ _ loc) = M.continue $ st & lastReportedClick .~ Just (n, loc)
appEvent st (T.MouseUp _ _ _) = M.continue $ st & lastReportedClick .~ Nothing
appEvent st (T.VtyEvent (V.EvMouseUp _ _ _)) = M.continue $ st & lastReportedClick .~ Nothing
appEvent st (T.VtyEvent (V.EvKey V.KUp [])) = M.vScrollBy (M.viewportScroll Prose) (-1) >> M.continue st
appEvent st (T.VtyEvent (V.EvKey V.KDown [])) = M.vScrollBy (M.viewportScroll Prose) 1 >> M.continue st
appEvent st (T.VtyEvent (V.EvKey V.KEsc [])) = M.halt st
appEvent st _ = M.continue st
@ -94,3 +111,22 @@ main = do
return v
void $ M.customMain buildVty Nothing app $ St [] Nothing
$ "Press up and down arrow keys to scroll, ESC to quit.\n\
\Observe the click coordinates identify the\n\
\underlying widget coordinates.\n\
\\n\
\Lorem ipsum dolor sit amet,\n\
\consectetur adipiscing elit,\n\
\sed do eiusmod tempor incididunt ut labore\n\
\et dolore magna aliqua.\n\
\ \n\
\Ut enim ad minim veniam\n\
\quis nostrud exercitation ullamco laboris\n\
\nisi ut aliquip ex ea commodo consequat.\n\
\\n\
\Duis aute irure dolor in reprehenderit\n\
\in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n\
\\n\
\Excepteur sint occaecat cupidatat not proident,\n\
\sunt in culpa qui officia deserunt mollit\n\
\anim id est laborum.\n"