on develop help sign in
44 lines 1.7 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ExplicitForAll #-}
3{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE RecursiveDo #-}
5#ifdef USE_REFLEX_OPTIMIZER
6{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
7#endif
9module Reflex.Widget.Basic where
11import Control.Monad.Fix (MonadFix)
12import Data.Map (Map)
14import Reflex.Class
15import Reflex.Adjustable.Class
16import Data.Patch.MapWithMove
19-- | Build sortable content in such a way that re-sorting it can cause minimal
20-- disruption to an existing context.
21--
22-- Naively re-sorting a list of images would destroy every image and add them back
23-- in the new order. This framework is able to avoid that by preserving the
24-- identity of each image and simply moving it to the new location.
25--
26-- Example:
27--
28-- > let sortByFst = buttonA $> comparing fst
29-- > sortBySnd = buttonB $> comparing snd
30-- > sortEvent = leftmost [sortByFst, sortBySnd]
31-- > sortableList
32-- > (\k v -> text $ "\n" ++ show k ++ " " ++ v) -- show each element on a new line
33-- > (Map.fromList $ zip [0..] [(3, "a"), (2, "b"), (1, "c")])
34-- > sortEvent
35sortableList :: forall t m k v a. (MonadHold t m, MonadFix m, Adjustable t m, Ord k)
36 => (k -> v -> m a) -- ^ Function to render the content for each key/value pair
37 -> Map k v -- ^ The sortable list with an initial ordering determined by the @Map@ keys in ascending order
38 -> Event t (v -> v -> Ordering) -- ^ An event carrying a sort function for the list
39 -> m (Map k a)
40sortableList f m0 reSortFunc = do
41 rec let reSortPatch = attachWith (flip patchThatSortsMapWith) (currentIncremental m) reSortFunc
42 m <- holdIncremental m0 reSortPatch
43 (results, _) <- mapMapWithAdjustWithMove f m0 reSortPatch
44 pure results