on develop help sign in
Use Reflex.Collection to render todo list
82d3abfAli Abrar
src-bin/example.hs+31−3
@@ -10,6 +10,8 @@
10
10
11import Control.Monad.Fix
11import Control.Monad.Fix
12import Data.Foldable
12import Data.Foldable
13+import Data.Map (Map)
14+import qualified Data.Map as Map
13import Data.Sequence (Seq)
15import Data.Sequence (Seq)
14import qualified Data.Sequence as Seq
16import qualified Data.Sequence as Seq
15import Data.Text (Text)
17import Data.Text (Text)
@@ -27,9 +29,10 @@ main = mainWidget $ do
27 tellShutdown . fforMaybe inp $ \case
29 tellShutdown . fforMaybe inp $ \case
28 V.EvKey (V.KChar 'c') [V.MCtrl] -> Just ()
30 V.EvKey (V.KChar 'c') [V.MCtrl] -> Just ()
29 _ -> Nothing
31 _ -> Nothing
30− rec let todos' = todos [] $ () <$ e
31− btn = button $ pure "Add another task"
32+ let btn = button $ pure "Add another task"
33+ rec let todos'' = todos' [Todo "First" True, Todo "Second" False, Todo "Third" False] $ () <$ e
32− (_, e) <- splitV (pure (subtract 3)) (pure (True, True)) todos' btn
34+ (m, (e, _)) <- splitV (pure (subtract 6)) (pure (True, True)) todos'' $ do
35+ splitV (pure (subtract 3)) (pure (True, True)) btn (display $ current m)
33 return ()
36 return ()
34
37
35testBoxes :: (Reflex t, MonadHold t m, MonadFix m) => VtyWidget t m ()
38testBoxes :: (Reflex t, MonadHold t m, MonadFix m) => VtyWidget t m ()
@@ -125,3 +128,28 @@ todos todos0 newTodo = do
125 ]
128 ]
126 updates <- switchHold never $ fmap (leftmost . toList . fmap snd) listOut
129 updates <- switchHold never $ fmap (leftmost . toList . fmap snd) listOut
127 return todos
130 return todos
131+
132+todos'
133+ :: (MonadHold t m, MonadFix m, Reflex t, Adjustable t m, NotReady t m, PostBuild t m)
134+ => [Todo]
135+ -> Event t ()
136+ -> VtyWidget t m (Dynamic t (Map Int Todo))
137+todos' todos0 newTodo = do
138+ let todosMap0 = Map.fromList $ zip [0..] todos0
139+ w <- displayWidth
140+ rec listOut <- listHoldWithKey todosMap0 insert $ \row t -> do
141+ let reg = fmap (\w' -> Region 0 row w' 1) w
142+ pane reg (fmap (==row) selected) $ do
143+ e <- mouseUp
144+ r <- todo t
145+ return (row <$ e, r)
146+ let selectionClick = switch . current $ fmap (leftmost . Map.elems . fmap fst) listOut
147+ selected <- holdDyn 0 $ leftmost
148+ [ selectionClick
149+ , Map.size <$> tag (current todosMap) newTodo
150+ ]
151+ let todosMap = joinDynThroughMap $ fmap (fmap snd) listOut
152+ let insert = ffor (tagPromptlyDyn todosMap newTodo) $ \m -> case Map.lookupMax m of
153+ Nothing -> Map.singleton 0 $ Just $ Todo "" False
154+ Just (k, _) -> {- Map.union (Just <$> m) $ -} Map.singleton (k+1) $ Just $ Todo "" False
155+ return todosMap
src/Reflex/Vty/Widget.hs−1
@@ -231,7 +231,6 @@ data Region = Region
231regionSize :: Region -> (Int, Int)
231regionSize :: Region -> (Int, Int)
232regionSize (Region _ _ w h) = (w, h)
232regionSize (Region _ _ w h) = (w, h)
233
233
234−
235-- | Produces an 'Image' that fills a region with space characters
234-- | Produces an 'Image' that fills a region with space characters
236regionBlankImage :: Region -> Image
235regionBlankImage :: Region -> Image
237regionBlankImage r@(Region _ _ width height) =
236regionBlankImage r@(Region _ _ width height) =