on develop help sign in
Add titled box widget
b900ff6Elvecent
src-bin/example.hs+2−1
@@ -97,7 +97,8 @@ testBoxes = do
97 { _textInputConfig_initialValue =
97 { _textInputConfig_initialValue =
98 "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."
98 "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."
99 }
99 }
100+ textBox = boxTitle (pure roundedBoxStyle) "Text Edit" $
100− textBox = boxStatic roundedBoxStyle $ multilineTextInput cfg
101+ multilineTextInput cfg
101 dragBox = boxStatic roundedBoxStyle dragTest
102 dragBox = boxStatic roundedBoxStyle dragTest
102 in splitVDrag (hRule doubleBoxStyle) textBox dragBox
103 in splitVDrag (hRule doubleBoxStyle) textBox dragBox
103 return ()
104 return ()
src/Reflex/Vty/Widget.hs+24−4
@@ -44,6 +44,7 @@ module Reflex.Vty.Widget
44 , pane
44 , pane
45 , splitV
45 , splitV
46 , splitVDrag
46 , splitVDrag
47+ , boxTitle
47 , box
48 , box
48 , boxStatic
49 , boxStatic
49 , RichTextConfig(..)
50 , RichTextConfig(..)
@@ -563,12 +564,13 @@ doubleBoxStyle = BoxStyle '╔' '═' '╗' '║' '╝' '═' '╚' '║'
563roundedBoxStyle :: BoxStyle
564roundedBoxStyle :: BoxStyle
564roundedBoxStyle = BoxStyle '╭' '─' '╮' '│' '╯' '─' '╰' '│'
565roundedBoxStyle = BoxStyle '╭' '─' '╮' '│' '╯' '─' '╰' '│'
565
566
566−-- | Draws a box in the provided style and a child widget inside of that box
567+-- | Draws a titled box in the provided style and a child widget inside of that box
567−box :: (Monad m, Reflex t, MonadNodeId m)
568+boxTitle :: (Monad m, Reflex t, MonadNodeId m)
568 => Behavior t BoxStyle
569 => Behavior t BoxStyle
570+ -> Text
569 -> VtyWidget t m a
571 -> VtyWidget t m a
570 -> VtyWidget t m a
572 -> VtyWidget t m a
571−box boxStyle child = do
573+boxTitle boxStyle title child = do
572 dh <- displayHeight
574 dh <- displayHeight
573 dw <- displayWidth
575 dw <- displayWidth
574 let boxReg = DynRegion (pure 0) (pure 0) dw dh
576 let boxReg = DynRegion (pure 0) (pure 0) dw dh
@@ -583,7 +585,8 @@ box boxStyle child = do
583 bottom = top + height - 1
585 bottom = top + height - 1
584 sides =
586 sides =
585 [ withinImage (Region (left + 1) top (width - 2) 1) $
587 [ withinImage (Region (left + 1) top (width - 2) 1) $
588+ V.text' V.defAttr $
586− V.charFill V.defAttr (_boxStyle_n style) (width - 2) 1
589+ hPadText title (_boxStyle_n style) (width - 2)
587 , withinImage (Region right (top + 1) 1 (height - 2)) $
590 , withinImage (Region right (top + 1) 1 (height - 2)) $
588 V.charFill V.defAttr (_boxStyle_e style) 1 (height - 2)
591 V.charFill V.defAttr (_boxStyle_e style) 1 (height - 2)
589 , withinImage (Region (left + 1) bottom (width - 2) 1) $
592 , withinImage (Region (left + 1) bottom (width - 2) 1) $
@@ -602,6 +605,23 @@ box boxStyle child = do
602 V.char V.defAttr (_boxStyle_sw style)
605 V.char V.defAttr (_boxStyle_sw style)
603 ]
606 ]
604 in sides ++ if width > 1 && height > 1 then corners else []
607 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
605
625
606-- | A box whose style is static
626-- | A box whose style is static
607boxStatic
627boxStatic