@@ -25,6 +25,8 @@ module Reflex.Vty.Widget
25 , HasDisplaySize(..)
25 , HasDisplaySize(..)
26 , HasFocus(..)
26 , HasFocus(..)
27 , HasVtyInput(..)
27 , HasVtyInput(..)
28 , Region(..)
30 , Region(..)
29 , regionSize
31 , regionSize
30 , regionBlankImage
32 , regionBlankImage
@@ -35,10 +37,9 @@ module Reflex.Vty.Widget
35 , mouseDown
37 , mouseDown
39 , tellImages
40 , tellImages
41− -- , splitVDrag TODO
42+ , splitVDrag
43 , boxStatic
44 , boxStatic
44 , RichTextConfig(..)
45 , RichTextConfig(..)
@@ -66,10 +67,9 @@ import qualified Data.Text.Zipper as TZ
66import Graphics.Vty (Image)
67import Graphics.Vty (Image)
67import qualified Graphics.Vty as V
68import qualified Graphics.Vty as V
68import Reflex
69import Reflex
70+import Reflex.Class.Orphans
69import Reflex.NotReady.Class
71import Reflex.NotReady.Class
70import Reflex.NotReady.Class.Orphans ()
72import Reflex.NotReady.Class.Orphans ()
71−import Reflex.BehaviorWriter.Class (tellBehavior)
72−import Reflex.BehaviorWriter.Base (BehaviorWriterT, runBehaviorWriterT)
74import Reflex.Vty.Host
74import Reflex.Vty.Host
@@ -194,6 +194,14 @@ data Region = Region
195 deriving (Show, Read, Eq, Ord)
195 deriving (Show, Read, Eq, Ord)
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
197-- | The width and height of a 'Region'
205-- | The width and height of a 'Region'
198regionSize :: Region -> (Int, Int)
206regionSize :: Region -> (Int, Int)
199regionSize (Region _ _ w h) = (w, h)
207regionSize (Region _ _ w h) = (w, h)
@@ -203,6 +211,10 @@ regionBlankImage :: Region -> Image
203regionBlankImage r@(Region _ _ width height) =
211regionBlankImage r@(Region _ _ width height) =
204 withinImage r $ V.charFill V.defAttr ' ' width height
212 withinImage r $ V.charFill V.defAttr ' ' width height
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
206-- | Translates and crops an 'Image' so that it is contained by
218-- | Translates and crops an 'Image' so that it is contained by
207-- the given 'Region'.
219-- the given 'Region'.
208withinImage
220withinImage
@@ -222,53 +234,11 @@ withinImage (Region left top width height)
222-- that (0,0) is the top-left corner of the region
234-- that (0,0) is the top-left corner of the region
224 :: (Reflex t, Monad m)
236 :: (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
226 -> Dynamic t Bool -- ^ Whether the widget should be focused when the parent is.
238 -> Dynamic t Bool -- ^ Whether the widget should be focused when the parent is.
227 -> VtyWidget t m a
239 -> VtyWidget t m a
228 -> VtyWidget t m a
240 -> VtyWidget t m a
229−pane reg foc child = VtyWidget $ do
231− let ctx' = VtyWidgetCtx
232− { _vtyWidgetCtx_input = leftmost -- TODO: think about this leftmost more.
234− attachWith (\(r,f) e -> filterInput r f e)
235− (liftA2 (,) (current reg) (current foc))
236− (_vtyWidgetCtx_input ctx)
238− , _vtyWidgetCtx_focus = liftA2 (&&) (_vtyWidgetCtx_focus ctx) foc
239− , _vtyWidgetCtx_width = fmap (fst . regionSize) reg
240− , _vtyWidgetCtx_height = fmap (snd . regionSize) reg
242− (result, images) <- lift . lift $ runVtyWidget ctx' child
243− let images' = liftA2 (\r is -> map (withinImage r) is) (current reg) images
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
258− , y >= t + h ] = Nothing
260− Just (con (x - l) (y - t))
263− :: (Reflex t, Monad m)
268− -> Dynamic t Bool -- ^ Whether the widget should be focused when the parent is.
271−pane' l t w h foc child = VtyWidget $ do
241+pane (DynRegion l t w h) foc child = VtyWidget $ do
272 ctx <- lift ask
242 ctx <- lift ask
273 let reg = Region <$> l <*> t <*> w <*> h
243 let reg = Region <$> l <*> t <*> w <*> h
274 let ctx' = VtyWidgetCtx
244 let ctx' = VtyWidgetCtx
@@ -398,13 +368,22 @@ splitV :: (Reflex t, Monad m)
398splitV sizeFunD focD wA wB = do
368splitV sizeFunD focD wA wB = do
399 dw <- displayWidth
369 dw <- displayWidth
400 dh <- displayHeight
370 dh <- displayHeight
401− let regA = (\f w h -> Region 0 0 w (f h)) <$> sizeFunD <*> dw <*> dh
371+ let regA = DynRegion
402− regB = (\w h (Region _ _ _ hA) -> Region 0 hA w (h - hA)) <$> dw <*> dh <*> regA
372+ { _dynRegion_left = pure 0
373+ , _dynRegion_top = pure 0
374+ , _dynRegion_width = dw
375+ , _dynRegion_height = sizeFunD <*> dh
378+ { _dynRegion_left = pure 0
379+ , _dynRegion_top = _dynRegion_height regA
380+ , _dynRegion_width = dw
381+ , _dynRegion_height = liftA2 (-) dh (_dynRegion_height regA)
403 ra <- pane regA (fst <$> focD) wA
383 ra <- pane regA (fst <$> focD) wA
404 rb <- pane regB (snd <$> focD) wB
384 rb <- pane regB (snd <$> focD) wB
405 return (ra,rb)
385 return (ra,rb)
408-- | A split of the available space into two parts with a draggable separator.
387-- | A split of the available space into two parts with a draggable separator.
409-- Starts with half the space allocated to each, and the first pane has focus.
388-- Starts with half the space allocated to each, and the first pane has focus.
410-- Clicking in a pane switches focus.
389-- Clicking in a pane switches focus.
@@ -414,22 +393,23 @@ splitVDrag :: (Reflex t, MonadFix m, MonadHold t m)
414 -> VtyWidget t m b
393 -> VtyWidget t m b
415 -> VtyWidget t m (a,b)
394 -> VtyWidget t m (a,b)
416splitVDrag wS wA wB = do
395splitVDrag wS wA wB = do
417− sz <- displaySize
396+ dh <- displayHeight
418− (_, h0) <- sample $ current sz
398+ h0 <- sample $ current dh -- TODO
419 dragE <- drag V.BLeft
399 dragE <- drag V.BLeft
420 let splitter0 = h0 `div` 2
400 let splitter0 = h0 `div` 2
421 rec splitterCheckpoint <- holdDyn splitter0 $ leftmost [fst <$> ffilter snd dragSplitter, resizeSplitter]
401 rec splitterCheckpoint <- holdDyn splitter0 $ leftmost [fst <$> ffilter snd dragSplitter, resizeSplitter]
422 splitterPos <- holdDyn splitter0 $ leftmost [fst <$> dragSplitter, resizeSplitter]
402 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) ->
424 fromIntegral x / fromIntegral h
404 fromIntegral x / fromIntegral h
425 let dragSplitter = fforMaybe (attach (current splitterCheckpoint) dragE) $
405 let dragSplitter = fforMaybe (attach (current splitterCheckpoint) dragE) $
426 \(splitterY, Drag (_, fromY) (_, toY) _ _ end) ->
406 \(splitterY, Drag (_, fromY) (_, toY) _ _ end) ->
427 if splitterY == fromY then Just (toY, end) else Nothing
407 if splitterY == fromY then Just (toY, end) else Nothing
428− regA = (\(w,_) sp -> Region 0 0 w sp) <$> sz <*> splitterPos
408+ regA = DynRegion 0 0 dw splitterPos
429− regS = (\(w,_) sp -> Region 0 sp w 1) <$> sz <*> splitterPos
409+ regS = DynRegion 0 splitterPos dw 1
430− regB = (\(w,h) sp -> Region 0 (sp + 1) w (h - sp - 1)) <$> sz <*> splitterPos
410+ regB = DynRegion 0 (splitterPos + 1) dw (dh - splitterPos - 1)
431− resizeSplitter = ffor (attach (current splitterFrac) (updated sz)) $
411+ resizeSplitter = ffor (attach (current splitterFrac) (updated dh)) $
432− \(frac, (_,h)) -> round (frac * fromIntegral h)
412+ \(frac, h) -> round (frac * fromIntegral h)
433 focA <- holdDyn True $ leftmost
413 focA <- holdDyn True $ leftmost
434 [ True <$ mA
414 [ True <$ mA
435 , False <$ mB
415 , False <$ mB
@@ -443,7 +423,7 @@ splitVDrag wS wA wB = do
443 m <- mouseDown V.BLeft
423 m <- mouseDown V.BLeft
445 return (m, x')
425 return (m, x')
447-- | Fill the background with a particular character.
427-- | Fill the background with a particular character.
448fill :: (Reflex t, Monad m) => Char -> VtyWidget t m ()
428fill :: (Reflex t, Monad m) => Char -> VtyWidget t m ()
449fill c = do
429fill c = do
@@ -501,10 +481,10 @@ box :: (Monad m, Reflex t)
501box boxStyle child = do
481box boxStyle child = do
502 dh <- displayHeight
482 dh <- displayHeight
503 dw <- displayWidth
483 dw <- displayWidth
504− let boxReg = liftA2 (\w h -> Region 0 0 w h) dw dh
484+ let boxReg = DynRegion (pure 0) (pure 0) dw dh
505− innerReg = liftA2 (\w h -> Region 1 1 (w - 2) (h - 2)) dw dh
485+ innerReg = DynRegion (pure 1) (pure 1) (subtract 2 <$> dw) (subtract 2 <$> dh)
506− tellImages (boxImages <$> boxStyle <*> current boxReg)
486+ tellImages (boxImages <$> boxStyle <*> currentRegion boxReg)
507− tellImages (fmap (\r -> [regionBlankImage r]) (current innerReg))
487+ tellImages (fmap (\r -> [regionBlankImage r]) (currentRegion innerReg))
508 pane innerReg (pure True) child
488 pane innerReg (pure True) child
510 boxImages :: BoxStyle -> Region -> [Image]
490 boxImages :: BoxStyle -> Region -> [Image]