on develop help sign in
Use Reflex.Collection to render todo list
commit82d3abfd8ad69d2e8e19b57a1e4af3fe237633e8 authorAli Abrar <aliabrar@gmail.com> dateSun 4 Nov 2018, 21:53 (8 years ago) parent04cde15
2 files changed+31−4
src-bin/example.hs+31−3 src/Reflex/Vty/Widget.hs−1
src-bin/example.hs+31−3
@@ -10,6 +10,8 @@
1010
1111import Control.Monad.Fix
1212import Data.Foldable
13+import Data.Map (Map)
14+import qualified Data.Map as Map
1315import Data.Sequence (Seq)
1416import qualified Data.Sequence as Seq
1517import Data.Text (Text)
@@ -27,9 +29,10 @@ main = mainWidget $ do
2729 tellShutdown . fforMaybe inp $ \case
2830 V.EvKey (V.KChar 'c') [V.MCtrl] -> Just ()
2931 _ -> Nothing
30− rec let todos' = todos [] $ () <$ e
31− btn = button $ pure "Add another task"
32− (_, e) <- splitV (pure (subtract 3)) (pure (True, True)) todos' btn
32+ let btn = button $ pure "Add another task"
33+ rec let todos'' = todos' [Todo "First" True, Todo "Second" False, Todo "Third" False] $ () <$ e
34+ (m, (e, _)) <- splitV (pure (subtract 6)) (pure (True, True)) todos'' $ do
35+ splitV (pure (subtract 3)) (pure (True, True)) btn (display $ current m)
3336 return ()
3437
3538testBoxes :: (Reflex t, MonadHold t m, MonadFix m) => VtyWidget t m ()
@@ -125,3 +128,28 @@ todos todos0 newTodo = do
125128 ]
126129 updates <- switchHold never $ fmap (leftmost . toList . fmap snd) listOut
127130 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
231231regionSize :: Region -> (Int, Int)
232232regionSize (Region _ _ w h) = (w, h)
233233
234−
235234-- | Produces an 'Image' that fills a region with space characters
236235regionBlankImage :: Region -> Image
237236regionBlankImage r@(Region _ _ width height) =