@@ -1,3 +1,7 @@
2+Module: Reflex.Vty.Widget
3+Description: Basic set of widgets and building blocks for reflex-vty applications
1{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE FunctionalDependencies #-}
7{-# LANGUAGE FunctionalDependencies #-}
@@ -167,6 +171,7 @@ instance (Reflex t, Monad m) => HasFocus t (VtyWidget t m) where
168-- | A class for widgets that can produce images to draw to the display
172-- | A class for widgets that can produce images to draw to the display
169class (Reflex t, Monad m) => ImageWriter t m | m -> t where
173class (Reflex t, Monad m) => ImageWriter t m | m -> t where
174+ -- | Send images upstream for rendering
170 tellImages :: Behavior t [Image] -> m ()
175 tellImages :: Behavior t [Image] -> m ()
172instance (Reflex t, Monad m) => ImageWriter t (VtyWidget t m) where
177instance (Reflex t, Monad m) => ImageWriter t (VtyWidget t m) where
@@ -174,6 +179,7 @@ instance (Reflex t, Monad m) => ImageWriter t (VtyWidget t m) where
175-- | A class for things that can shut down the vty application
180-- | A class for things that can shut down the vty application
176class (Reflex t, Monad m) => Shutdown t m where
181class (Reflex t, Monad m) => Shutdown t m where
182+ -- | Signals that the vty application should shut down
177 tellShutdown :: Event t () -> m ()
183 tellShutdown :: Event t () -> m ()
179instance (Reflex t, Monad m) => Shutdown t (VtyWidget t m) where
185instance (Reflex t, Monad m) => Shutdown t (VtyWidget t m) where
@@ -192,6 +198,22 @@ data Region = Region
192regionSize :: Region -> (Int, Int)
198regionSize :: Region -> (Int, Int)
193regionSize (Region _ _ w h) = (w, h)
199regionSize (Region _ _ w h) = (w, h)
202+-- | Produces an 'Image' that fills a region with space characters
203+regionBlankImage :: Region -> Image
204+regionBlankImage r@(Region _ _ width height) =
205+ withinImage r $ V.charFill V.defAttr ' ' width height
207+-- | Translates and crops an 'Image' so that it is contained by
208+-- the given 'Region'.
213+withinImage (Region left top width height)
214+ | width < 0 || height < 0 = withinImage (Region left top 0 0)
215+ | otherwise = V.translate left top . V.crop width height
195-- | Low-level widget combinator that runs a child 'VtyWidget' within
217-- | Low-level widget combinator that runs a child 'VtyWidget' within
196-- a given region and context. This widget filters and modifies the input
218-- a given region and context. This widget filters and modifies the input
197-- that the child widget receives such that:
219-- that the child widget receives such that:
@@ -455,20 +477,12 @@ text msg = do
455 . concatMap (fmap (V.string attrs . T.unpack) . TZ.wrapWithOffset maxWidth 0)
477 . concatMap (fmap (V.string attrs . T.unpack) . TZ.wrapWithOffset maxWidth 0)
456 . T.split (=='\n')
478 . T.split (=='\n')
480+-- | Renders any behavior whose value can be converted to
459 :: (Reflex t, Monad m, Show a)
483 :: (Reflex t, Monad m, Show a)
460 => Behavior t a
484 => Behavior t a
461 -> VtyWidget t m ()
485 -> VtyWidget t m ()
462display a = text $ T.pack . show <$> a
486display a = text $ T.pack . show <$> a
464−regionBlankImage :: Region -> Image
465−regionBlankImage r@(Region _ _ width height) =
466− withinImage r $ V.charFill V.defAttr ' ' width height
472−withinImage (Region left top width height)
473− | width < 0 || height < 0 = withinImage (Region left top 0 0)
474− | otherwise = V.translate left top . V.crop width height