on develop help sign in
Update splitVDrag and examples to use DynRegion
commit0e7e1f192f86f5b0fd1a55847905df2cbdd1bbd3 authorAli Abrar <aliabrar@gmail.com> dateSun 17 Feb 2019, 14:14 (8 years ago) parent5e35722
5 files changed+93−85
reflex-vty.cabal+1 src-bin/example.hs+26−21 src/Reflex/Class/Orphans.hs+19 src/Reflex/Vty/Widget.hs+44−64 src/Reflex/Vty/Widget/Input/Text.hs+3
reflex-vty.cabal+1
@@ -23,6 +23,7 @@ library
2323 , Reflex.Vty.Widget.Input.Text
2424 , Data.Text.Zipper
2525 , Control.Monad.Writer.Adjustable
26+ , Reflex.Class.Orphans
2627 , Reflex.Class.Switchable
2728 , Reflex.NotReady.Class.Orphans
2829 , Reflex.Spider.Orphans
src-bin/example.hs+26−21
@@ -21,27 +21,39 @@ import qualified Data.Text as T
2121import qualified Data.Text.Zipper as TZ
2222import qualified Graphics.Vty as V
2323import Reflex
24+import Reflex.Class.Orphans
2425import Reflex.Network
2526import Reflex.Class.Switchable
2627import Reflex.NotReady.Class
2728import Reflex.Vty
2829
30+import Data.Tree
31+
32+-- Unlimited Stack
33+ -- Parent provides orientation and maximum cross-dimension size
34+ -- Each child takes as much main-dimension space as it wants and reports what it took
35+ -- Parent offsets each child so that it does not overlap with other children
36+ -- If parent runs of out space, parent provides a scroll bar
37+
2938data Example = Example_TextEditor
3039 | Example_Todo
3140 deriving (Show, Read, Eq, Ord, Enum, Bounded)
3241
42+div' :: (Integral a, Applicative f) => f a ->f a -> f a
43+div' = liftA2 div
44+
3345main :: IO ()
3446main = mainWidget $ do
3547 inp <- input
36− taskList
3748
38−{-
49+ w <- displayWidth
50+ h <- displayHeight
3951 let buttons = do
4052 text $ pure "Select an example. Esc will bring you back here. Ctrl+c to quit."
41− let region1 = ffor size $ \(w,h) ->
42− Region (w `div` 6) (h `div` 6) (w `div` 6) (h `div` 6)
43− region2 = ffor size $ \(w,h) ->
44− Region (2 * w `div` 6) (h `div` 6) (w `div` 6) (h `div` 6)
53+ let w' = fmap (`div`6) w
54+ h' = fmap (`div`6) h
55+ region1 = DynRegion w' h' w' h'
56+ region2 = DynRegion (2 * w') h' w' h'
4557 todo' <- pane region1 (pure True) $ textButtonStatic def "Todo List"
4658 editor <- pane region2 (pure True) $ textButtonStatic def "Text Editor"
4759 return $ leftmost
@@ -59,7 +71,6 @@ main = mainWidget $ do
5971 Left Example_Todo -> escapable taskList
6072 Right () -> buttons
6173
62−-}
6374 return $ fforMaybe inp $ \case
6475 V.EvKey (V.KChar 'c') [V.MCtrl] -> Just ()
6576 _ -> Nothing
@@ -80,18 +91,16 @@ taskList = do
8091 splitV (pure (subtract 3)) (pure (True, True)) btn (display $ current m)
8192 return ()
8293
83−{-
8494testBoxes :: (Reflex t, MonadHold t m, MonadFix m) => VtyWidget t m ()
8595testBoxes = do
8696 dw <- displayWidth
8797 dh <- displayHeight
88− let region1 = liftA2 (\w h -> Region (w `div` 6) (h `div` 6) (w `div` 2) (h `div` 2)) dw dh
89− region2 = liftA2 (\w h -> Region (w `div` 4) (h `div` 4) (2 * (w `div` 3)) (2*(h `div` 3))) dw dh
98+ let region1 = DynRegion (div' dw 6) (div' dh 6) (div' dw 2) (div' dh 2)
99+ region2 = DynRegion (div' dw 4) (div' dh 4) (2 * div' dw 3) (2 * div' dh 3)
90100 pane region1 (constDyn False) . boxStatic singleBoxStyle $ debugInput
91101 _ <- pane region2 (constDyn True) . boxStatic singleBoxStyle $
92102 splitVDrag (hRule doubleBoxStyle) (boxStatic roundedBoxStyle $ multilineTextInput def) (boxStatic roundedBoxStyle dragTest)
93103 return ()
94−-}
95104
96105debugFocus :: (Reflex t, Monad m) => VtyWidget t m ()
97106debugFocus = do
@@ -137,14 +146,14 @@ todo
137146todo t0 = do
138147 w <- displayWidth
139148 rec let checkboxWidth = 3
140− checkboxRegion = pure $ Region 0 0 checkboxWidth 1
149+ checkboxRegion = DynRegion 0 0 checkboxWidth 1
141150 labelHeight = _textInput_lines ti
142− labelWidth = ffor w $ \w' -> w' - 1 - checkboxWidth
143− labelLeft = constDyn $ checkboxWidth + 1
151+ labelWidth = w - 1 - checkboxWidth
152+ labelLeft = checkboxWidth + 1
144153 labelTop = constDyn 0
145− -- labelRegion = liftA2 (\w' h -> Region (checkboxWidth + 1) 0 (w' - 1 - checkboxWidth) h) w (_textInput_lines ti)
154+ labelRegion = DynRegion labelLeft labelTop labelWidth labelHeight
146155 value <- pane checkboxRegion (pure True) $ checkbox def $ _todo_done t0
147− (ti, d) <- pane' labelLeft labelTop labelWidth labelHeight (pure True) $ do
156+ (ti, d) <- pane labelRegion (pure True) $ do
148157 i <- input
149158 v <- textInput $ def { _textInputConfig_initialValue = TZ.fromText $ _todo_label t0 }
150159 let deleteSelf = attachWithMaybe backspaceOnEmpty (current $ _textInput_value v) i
@@ -171,11 +180,7 @@ todos todos0 newTodo = do
171180 let reg = zipDynWith (\w' ts ->
172181 let l = Map.size $ Map.takeWhileAntitone (<row) ts
173182 in Region 0 l w' 1) w todosMap
174− pane'
175− (pure 0)
176− (fromMaybe 0 . Map.lookup row <$> offsets)
177− w
178− (fromMaybe 1 . Map.lookup row <$> heights)
183+ pane (DynRegion 0 (fromMaybe 0 . Map.lookup row <$> offsets) w (fromMaybe 1 . Map.lookup row <$> heights))
179184 (fmap (==row) selected) $ do
180185 e <- mouseUp
181186 r <- todo t
src/Reflex/Class/Orphans.hsnew file+19
@@ -0,0 +1,19 @@
1+{-
2+Module: Reflex.Class.Orphans
3+Description: Orphan instances for Dynamic. These should be upstreamed.
4+-}
5+module Reflex.Class.Orphans where
6+
7+import Control.Applicative
8+import Reflex.Class
9+
10+instance (Num a, Reflex t) => Num (Dynamic t a) where
11+ (+) = liftA2 (+)
12+ (*) = liftA2 (*)
13+ abs = fmap abs
14+ signum = fmap signum
15+ fromInteger = pure . fromInteger
16+ negate = fmap negate
17+ (-) = liftA2 (-)
18+
19+
src/Reflex/Vty/Widget.hs+44−64
@@ -25,6 +25,8 @@ module Reflex.Vty.Widget
2525 , HasDisplaySize(..)
2626 , HasFocus(..)
2727 , HasVtyInput(..)
28+ , DynRegion(..)
29+ , currentRegion
2830 , Region(..)
2931 , regionSize
3032 , regionBlankImage
@@ -35,10 +37,9 @@ module Reflex.Vty.Widget
3537 , mouseDown
3638 , mouseUp
3739 , pane
38− , pane'
3940 , tellImages
4041 , splitV
41− -- , splitVDrag TODO
42+ , splitVDrag
4243 , box
4344 , boxStatic
4445 , RichTextConfig(..)
@@ -66,10 +67,9 @@ import qualified Data.Text.Zipper as TZ
6667import Graphics.Vty (Image)
6768import qualified Graphics.Vty as V
6869import Reflex
70+import Reflex.Class.Orphans
6971import Reflex.NotReady.Class
7072import Reflex.NotReady.Class.Orphans ()
71−import Reflex.BehaviorWriter.Class (tellBehavior)
72−import Reflex.BehaviorWriter.Base (BehaviorWriterT, runBehaviorWriterT)
7373
7474import Reflex.Vty.Host
7575
@@ -194,6 +194,14 @@ data Region = Region
194194 }
195195 deriving (Show, Read, Eq, Ord)
196196
197+-- | A dynamic chunk of the display area
198+data DynRegion t = DynRegion
199+ { _dynRegion_left :: Dynamic t Int
200+ , _dynRegion_top :: Dynamic t Int
201+ , _dynRegion_width :: Dynamic t Int
202+ , _dynRegion_height :: Dynamic t Int
203+ }
204+
197205-- | The width and height of a 'Region'
198206regionSize :: Region -> (Int, Int)
199207regionSize (Region _ _ w h) = (w, h)
@@ -203,6 +211,10 @@ regionBlankImage :: Region -> Image
203211regionBlankImage r@(Region _ _ width height) =
204212 withinImage r $ V.charFill V.defAttr ' ' width height
205213
214+-- | A behavior of the current display area represented by a 'DynRegion'
215+currentRegion :: Reflex t => DynRegion t -> Behavior t Region
216+currentRegion (DynRegion l t w h) = Region <$> current l <*> current t <*> current w <*> current h
217+
206218-- | Translates and crops an 'Image' so that it is contained by
207219-- the given 'Region'.
208220withinImage
@@ -222,53 +234,11 @@ withinImage (Region left top width height)
222234-- that (0,0) is the top-left corner of the region
223235pane
224236 :: (Reflex t, Monad m)
225− => Dynamic t Region -- ^ Region into which we should draw the widget (in coordinates relative to our own)
237+ => DynRegion t
226238 -> Dynamic t Bool -- ^ Whether the widget should be focused when the parent is.
227239 -> VtyWidget t m a
228240 -> VtyWidget t m a
229−pane reg foc child = VtyWidget $ do
230− ctx <- lift ask
231− let ctx' = VtyWidgetCtx
232− { _vtyWidgetCtx_input = leftmost -- TODO: think about this leftmost more.
233− [ fmapMaybe id $
234− attachWith (\(r,f) e -> filterInput r f e)
235− (liftA2 (,) (current reg) (current foc))
236− (_vtyWidgetCtx_input ctx)
237− ]
238− , _vtyWidgetCtx_focus = liftA2 (&&) (_vtyWidgetCtx_focus ctx) foc
239− , _vtyWidgetCtx_width = fmap (fst . regionSize) reg
240− , _vtyWidgetCtx_height = fmap (snd . regionSize) reg
241− }
242− (result, images) <- lift . lift $ runVtyWidget ctx' child
243− let images' = liftA2 (\r is -> map (withinImage r) is) (current reg) images
244− tellImages images'
245− return result
246− where
247− filterInput :: Region -> Bool -> VtyEvent -> Maybe VtyEvent
248− filterInput (Region l t w h) focused e = case e of
249− V.EvKey _ _ | not focused -> Nothing
250− V.EvMouseDown x y btn m -> mouse (\u v -> V.EvMouseDown u v btn m) x y
251− V.EvMouseUp x y btn -> mouse (\u v -> V.EvMouseUp u v btn) x y
252− _ -> Just e
253− where
254− mouse con x y
255− | or [ x < l
256− , y < t
257− , x >= l + w
258− , y >= t + h ] = Nothing
259− | otherwise =
260− Just (con (x - l) (y - t))
261−
262−pane'
263− :: (Reflex t, Monad m)
264− => Dynamic t Int
265− -> Dynamic t Int
266− -> Dynamic t Int
267− -> Dynamic t Int
268− -> Dynamic t Bool -- ^ Whether the widget should be focused when the parent is.
269− -> VtyWidget t m a
270− -> VtyWidget t m a
271−pane' l t w h foc child = VtyWidget $ do
241+pane (DynRegion l t w h) foc child = VtyWidget $ do
272242 ctx <- lift ask
273243 let reg = Region <$> l <*> t <*> w <*> h
274244 let ctx' = VtyWidgetCtx
@@ -398,13 +368,22 @@ splitV :: (Reflex t, Monad m)
398368splitV sizeFunD focD wA wB = do
399369 dw <- displayWidth
400370 dh <- displayHeight
401− let regA = (\f w h -> Region 0 0 w (f h)) <$> sizeFunD <*> dw <*> dh
402− regB = (\w h (Region _ _ _ hA) -> Region 0 hA w (h - hA)) <$> dw <*> dh <*> regA
371+ let regA = DynRegion
372+ { _dynRegion_left = pure 0
373+ , _dynRegion_top = pure 0
374+ , _dynRegion_width = dw
375+ , _dynRegion_height = sizeFunD <*> dh
376+ }
377+ regB = DynRegion
378+ { _dynRegion_left = pure 0
379+ , _dynRegion_top = _dynRegion_height regA
380+ , _dynRegion_width = dw
381+ , _dynRegion_height = liftA2 (-) dh (_dynRegion_height regA)
382+ }
403383 ra <- pane regA (fst <$> focD) wA
404384 rb <- pane regB (snd <$> focD) wB
405385 return (ra,rb)
406386
407−{-
408387-- | A split of the available space into two parts with a draggable separator.
409388-- Starts with half the space allocated to each, and the first pane has focus.
410389-- Clicking in a pane switches focus.
@@ -414,22 +393,23 @@ splitVDrag :: (Reflex t, MonadFix m, MonadHold t m)
414393 -> VtyWidget t m b
415394 -> VtyWidget t m (a,b)
416395splitVDrag wS wA wB = do
417− sz <- displaySize
418− (_, h0) <- sample $ current sz
396+ dh <- displayHeight
397+ dw <- displayWidth
398+ h0 <- sample $ current dh -- TODO
419399 dragE <- drag V.BLeft
420400 let splitter0 = h0 `div` 2
421401 rec splitterCheckpoint <- holdDyn splitter0 $ leftmost [fst <$> ffilter snd dragSplitter, resizeSplitter]
422402 splitterPos <- holdDyn splitter0 $ leftmost [fst <$> dragSplitter, resizeSplitter]
423− splitterFrac <- holdDyn ((1::Double) / 2) $ ffor (attach (current sz) (fst <$> dragSplitter)) $ \((_,h),x) ->
403+ splitterFrac <- holdDyn ((1::Double) / 2) $ ffor (attach (current dh) (fst <$> dragSplitter)) $ \(h, x) ->
424404 fromIntegral x / fromIntegral h
425405 let dragSplitter = fforMaybe (attach (current splitterCheckpoint) dragE) $
426406 \(splitterY, Drag (_, fromY) (_, toY) _ _ end) ->
427407 if splitterY == fromY then Just (toY, end) else Nothing
428− regA = (\(w,_) sp -> Region 0 0 w sp) <$> sz <*> splitterPos
429− regS = (\(w,_) sp -> Region 0 sp w 1) <$> sz <*> splitterPos
430− regB = (\(w,h) sp -> Region 0 (sp + 1) w (h - sp - 1)) <$> sz <*> splitterPos
431− resizeSplitter = ffor (attach (current splitterFrac) (updated sz)) $
432− \(frac, (_,h)) -> round (frac * fromIntegral h)
408+ regA = DynRegion 0 0 dw splitterPos
409+ regS = DynRegion 0 splitterPos dw 1
410+ regB = DynRegion 0 (splitterPos + 1) dw (dh - splitterPos - 1)
411+ resizeSplitter = ffor (attach (current splitterFrac) (updated dh)) $
412+ \(frac, h) -> round (frac * fromIntegral h)
433413 focA <- holdDyn True $ leftmost
434414 [ True <$ mA
435415 , False <$ mB
@@ -443,7 +423,7 @@ splitVDrag wS wA wB = do
443423 m <- mouseDown V.BLeft
444424 x' <- x
445425 return (m, x')
446−-}
426+
447427-- | Fill the background with a particular character.
448428fill :: (Reflex t, Monad m) => Char -> VtyWidget t m ()
449429fill c = do
@@ -501,10 +481,10 @@ box :: (Monad m, Reflex t)
501481box boxStyle child = do
502482 dh <- displayHeight
503483 dw <- displayWidth
504− let boxReg = liftA2 (\w h -> Region 0 0 w h) dw dh
505− innerReg = liftA2 (\w h -> Region 1 1 (w - 2) (h - 2)) dw dh
506− tellImages (boxImages <$> boxStyle <*> current boxReg)
507− tellImages (fmap (\r -> [regionBlankImage r]) (current innerReg))
484+ let boxReg = DynRegion (pure 0) (pure 0) dw dh
485+ innerReg = DynRegion (pure 1) (pure 1) (subtract 2 <$> dw) (subtract 2 <$> dh)
486+ tellImages (boxImages <$> boxStyle <*> currentRegion boxReg)
487+ tellImages (fmap (\r -> [regionBlankImage r]) (currentRegion innerReg))
508488 pane innerReg (pure True) child
509489 where
510490 boxImages :: BoxStyle -> Region -> [Image]
src/Reflex/Vty/Widget/Input/Text.hs+3
@@ -32,6 +32,9 @@ data TextInputConfig t = TextInputConfig
3232instance Reflex t => Default (TextInputConfig t) where
3333 def = TextInputConfig empty never 4
3434
35+-- | The output produced by text input widgets, including the text
36+-- value and the number of display lines (post-wrapping). Note that some
37+-- display lines may not be visible due to scrolling.
3538data TextInput t = TextInput
3639 { _textInput_value :: Dynamic t Text
3740 , _textInput_lines :: Dynamic t Int