2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE LambdaCase #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE RankNTypes #-}
6{-# LANGUAGE RecursiveDo #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8{-# LANGUAGE TypeFamilies #-}
9{-# LANGUAGE UndecidableInstances #-}
10#ifdef USE_REFLEX_OPTIMIZER
11{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
18module Reflex.Collection
20 -- * Widgets on Collections
24 , listWithKeyShallowDiff
26 , selectViewListWithKey
27 , selectViewListWithKey_
33import Control.Monad.Identity
35import Data.Functor.Misc
37import qualified Data.Map as Map
42import Reflex.Adjustable.Class
44import Reflex.PostBuild.Class
46-- | Create a set of widgets based on the provided 'Map'. When the
47-- input 'Event' fires, remove widgets for keys with the value 'Nothing'
48-- and add/replace widgets for keys with 'Just' values.
51 . (Ord k, Adjustable t m, MonadHold t m)
53 -> Event t (Map k (Maybe v))
55 -> m (Dynamic t (Map k a))
56listHoldWithKey m0 m' f = do
57 let dm0 = mapWithFunctorToDMap $ Map.mapWithKey f m0
59 (PatchDMap . mapWithFunctorToDMap . Map.mapWithKey
60 (\k v -> ComposeMaybe $ fmap (f k) v)
63 (a0, a') <- sequenceDMapWithAdjust dm0 dm'
65 --TODO: Move the dmapToMap to the righthand side so it doesn't get
66 --fully redone every time
67 fmap dmapToMap . incrementalToDynamic <$> holdIncremental a0 a'
69--TODO: Something better than Dynamic t (Map k v) - we want something
70--where the Events carry diffs, not the whole value
73 . (Ord k, Adjustable t m, PostBuild t m, MonadFix m, MonadHold t m)
74 => Dynamic t (Map k v)
75 -> (k -> Dynamic t v -> m a)
76 -> m (Dynamic t (Map k a))
77listWithKey vals mkChild = do
78 postBuild <- getPostBuild
79 let childValChangedSelector = fanMap $ updated vals
81 -- We keep track of changes to children values in the mkChild
82 -- function we pass to listHoldWithKey The other changes we need
83 -- to keep track of are child insertions and
84 -- deletions. diffOnlyKeyChanges keeps track of insertions and
85 -- deletions but ignores value changes, since they're already
87 diffOnlyKeyChanges olds news =
88 flip Map.mapMaybe (align olds news) $ \case
89 This _ -> Just Nothing
91 That new -> Just $ Just new
92 rec sentVals :: Dynamic t (Map k v) <- foldDyn applyMap Map.empty changeVals
93 let changeVals :: Event t (Map k (Maybe v))
95 attachWith diffOnlyKeyChanges (current sentVals) $ leftmost
98 -- TODO: This should probably be added to the
99 -- attachWith, not to the updated; if we were using
100 -- diffMap instead of diffMapNoEq, I think it might not
102 , tag (current vals) postBuild
104 listHoldWithKey Map.empty changeVals $ \k v ->
105 mkChild k =<< holdDyn v (select childValChangedSelector $ Const2 k)
107{-# DEPRECATED listWithKey' "listWithKey' has been renamed to listWithKeyShallowDiff; also, its behavior has changed to fix a bug where children were always rebuilt (never updated)" #-}
109 :: (Ord k, Adjustable t m, MonadFix m, MonadHold t m)
111 -> Event t (Map k (Maybe v))
112 -> (k -> v -> Event t v -> m a)
113 -> m (Dynamic t (Map k a))
114listWithKey' = listWithKeyShallowDiff
116-- | Display the given map of items (in key order) using the builder
117-- function provided, and update it with the given event. 'Nothing'
118-- update entries will delete the corresponding children, and 'Just'
119-- entries will create them if they do not exist or send an update
120-- event to them if they do.
121listWithKeyShallowDiff
122 :: (Ord k, Adjustable t m, MonadFix m, MonadHold t m)
124 -> Event t (Map k (Maybe v))
125 -> (k -> v -> Event t v -> m a)
126 -> m (Dynamic t (Map k a))
127listWithKeyShallowDiff initialVals valsChanged mkChild = do
128 let childValChangedSelector = fanMap $ fmap (Map.mapMaybe id) valsChanged
129 sentVals <- foldDyn applyMap (void initialVals) $ fmap (fmap void) valsChanged
130 let relevantPatch patch _ = case patch of
132 -- Even if we let a Nothing through when the element doesn't
133 -- already exist, this doesn't cause a problem because it is
135 Nothing -> Just Nothing
137 -- We don't want to let spurious re-creations of items through
141 (attachWith (flip (Map.differenceWith relevantPatch))
145 $ \k v -> mkChild k v $ select childValChangedSelector $ Const2 k
147--TODO: Something better than Dynamic t (Map k v) - we want something
148--where the Events carry diffs, not the whole value
149-- | Create a dynamically-changing set of Event-valued widgets. This
150-- is like 'listWithKey', specialized for widgets returning @/Event t a/@.
151-- 'listWithKey' would return @/Dynamic t (Map k (Event t a))/@ in
152-- this scenario, but 'listViewWithKey' flattens this to
153-- @/Event t (Map k a)/@ via 'switch'.
155 :: (Ord k, Adjustable t m, PostBuild t m, MonadHold t m, MonadFix m)
156 => Dynamic t (Map k v)
157 -> (k -> Dynamic t v -> m (Event t a))
158 -> m (Event t (Map k a))
159listViewWithKey vals mkChild =
160 switch . fmap mergeMap <$> listViewWithKey' vals mkChild
163 :: (Ord k, Adjustable t m, PostBuild t m, MonadHold t m, MonadFix m)
164 => Dynamic t (Map k v)
165 -> (k -> Dynamic t v -> m a)
166 -> m (Behavior t (Map k a))
167listViewWithKey' vals mkChild = current <$> listWithKey vals mkChild
169-- | Create a dynamically-changing set of widgets, one of which is
170-- selected at any time.
173 . (Adjustable t m, Ord k, PostBuild t m, MonadHold t m, MonadFix m)
175 -- ^ Current selection key
176 -> Dynamic t (Map k v)
177 -- ^ Dynamic key/value map
178 -> (k -> Dynamic t v -> Dynamic t Bool -> m (Event t a))
179 -- ^ Function to create a widget for a given key from Dynamic value
180 -- and Dynamic Bool indicating if this widget is currently selected
181 -> m (Event t (k, a))
182 -- ^ Event that fires when any child's return Event fires. Contains
183 -- key of an arbitrary firing widget.
184selectViewListWithKey selection vals mkChild = do
185 -- For good performance, this value must be shared across all children
186 let selectionDemux = demux selection
187 selectChild <- listWithKey vals $ \k v -> do
188 let selected = demuxed selectionDemux k
189 selectSelf <- mkChild k v selected
190 return $ fmap ((,) k) selectSelf
191 return $ switchPromptlyDyn $ leftmost . Map.elems <$> selectChild
193-- | Like 'selectViewListWithKey' but discards the value of the list
194-- item widget's output 'Event'.
195selectViewListWithKey_
197 . (Adjustable t m, Ord k, PostBuild t m, MonadHold t m, MonadFix m)
199 -- ^ Current selection key
200 -> Dynamic t (Map k v)
201 -- ^ Dynamic key/value map
202 -> (k -> Dynamic t v -> Dynamic t Bool -> m (Event t a))
203 -- ^ Function to create a widget for a given key from Dynamic value
204 -- and Dynamic Bool indicating if this widget is currently selected
206 -- ^ Event that fires when any child's return Event fires. Contains
207 -- key of an arbitrary firing widget.
208selectViewListWithKey_ selection vals mkChild =
209 fmap fst <$> selectViewListWithKey selection vals mkChild
211-- | Create a dynamically-changing set of widgets from a Dynamic
212-- key/value map. Unlike the 'withKey' variants, the child widgets
213-- are insensitive to which key they're associated with.
215 :: (Ord k, Adjustable t m, MonadHold t m, PostBuild t m, MonadFix m)
216 => Dynamic t (Map k v)
217 -> (Dynamic t v -> m a)
218 -> m (Dynamic t (Map k a))
219list dm mkChild = listWithKey dm (\_ dv -> mkChild dv)
221-- | Create a dynamically-changing set of widgets from a Dynamic list.
223 :: (Adjustable t m, MonadHold t m, PostBuild t m, MonadFix m)
225 -> (Dynamic t v -> m a)
227simpleList xs mkChild =
228 fmap (fmap (map snd . Map.toList)) $ flip list mkChild $ fmap
229 (Map.fromList . zip [(1 :: Int) ..])
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list