on develop help sign in
Add titled box widget
commitb900ff64b0d424ea6faafee0a2b60d829d7210b8 authorElvecent <kvalyavin@ya.ru> dateMon 30 Mar 2020, 17:49 (7 years ago) parent74b505a
2 files changed+26−5
src-bin/example.hs+2−1 src/Reflex/Vty/Widget.hs+24−4
src-bin/example.hs+2−1
@@ -97,7 +97,8 @@ testBoxes = do
9797 { _textInputConfig_initialValue =
9898 "This box is a text input. The box below responds to mouse drag inputs. You can also drag the separator between the boxes to resize them."
9999 }
100− textBox = boxStatic roundedBoxStyle $ multilineTextInput cfg
100+ textBox = boxTitle (pure roundedBoxStyle) "Text Edit" $
101+ multilineTextInput cfg
101102 dragBox = boxStatic roundedBoxStyle dragTest
102103 in splitVDrag (hRule doubleBoxStyle) textBox dragBox
103104 return ()
src/Reflex/Vty/Widget.hs+24−4
@@ -44,6 +44,7 @@ module Reflex.Vty.Widget
4444 , pane
4545 , splitV
4646 , splitVDrag
47+ , boxTitle
4748 , box
4849 , boxStatic
4950 , RichTextConfig(..)
@@ -563,12 +564,13 @@ doubleBoxStyle = BoxStyle '╔' '═' '╗' '║' '╝' '═' '╚' '║'
563564roundedBoxStyle :: BoxStyle
564565roundedBoxStyle = BoxStyle '╭' '─' '╮' '│' '╯' '─' '╰' '│'
565566
566−-- | Draws a box in the provided style and a child widget inside of that box
567−box :: (Monad m, Reflex t, MonadNodeId m)
567+-- | Draws a titled box in the provided style and a child widget inside of that box
568+boxTitle :: (Monad m, Reflex t, MonadNodeId m)
568569 => Behavior t BoxStyle
570+ -> Text
569571 -> VtyWidget t m a
570572 -> VtyWidget t m a
571−box boxStyle child = do
573+boxTitle boxStyle title child = do
572574 dh <- displayHeight
573575 dw <- displayWidth
574576 let boxReg = DynRegion (pure 0) (pure 0) dw dh
@@ -583,7 +585,8 @@ box boxStyle child = do
583585 bottom = top + height - 1
584586 sides =
585587 [ withinImage (Region (left + 1) top (width - 2) 1) $
586− V.charFill V.defAttr (_boxStyle_n style) (width - 2) 1
588+ V.text' V.defAttr $
589+ hPadText title (_boxStyle_n style) (width - 2)
587590 , withinImage (Region right (top + 1) 1 (height - 2)) $
588591 V.charFill V.defAttr (_boxStyle_e style) 1 (height - 2)
589592 , withinImage (Region (left + 1) bottom (width - 2) 1) $
@@ -602,6 +605,23 @@ box boxStyle child = do
602605 V.char V.defAttr (_boxStyle_sw style)
603606 ]
604607 in sides ++ if width > 1 && height > 1 then corners else []
608+ hPadText :: T.Text -> Char -> Int -> T.Text
609+ hPadText t c l = if lt >= l
610+ then t
611+ else left <> t <> right
612+ where
613+ lt = T.length t
614+ delta = l - lt
615+ mkHalf n = T.replicate (n `div` 2) (T.singleton c)
616+ left = mkHalf $ delta + 1
617+ right = mkHalf delta
618+
619+-- | A box without a title
620+box :: (Monad m, Reflex t, MonadNodeId m)
621+ => Behavior t BoxStyle
622+ -> VtyWidget t m a
623+ -> VtyWidget t m a
624+box boxStyle = boxTitle boxStyle mempty
605625
606626-- | A box whose style is static
607627boxStatic