module SelectionIntMap where import Data.Foldable import Data.Maybe import Color import Regex import qualified Data.IntMap.Strict as IM import Dodge.Data.SelectionList import LensHelp setShownIntMap :: SelectionIntMap a -> SelectionIntMap a setShownIntMap sm = case sm ^. smRegex of Nothing -> sm & smShownItems .~ IM.fromAscList (zip [0..] allitms) Just str -> sm & smShownItems .~ IM.fromAscList (zip [0..] (f str : filter (regexList str . _siPictures) allitms)) where allitms = sm ^. smItems f str = SelectionRegex { _siPictures = ["FILTER: " ++ str] , _siHeight = 1 , _siIsSelectable = True , _siColor = white , _siOffX = 0 } initialSelPos :: SelectionIntMap a -> SelectionIntMap a initialSelPos sm = sm & smSelPos .~ mi where mi = case sm ^? smShownItems . ix 0 of Nothing -> Nothing Just SelectionRegex {} -> case sm ^? smShownItems . ix 1 of Nothing -> Just 0 Just _ -> Just 1 _ -> Just 0 -- assumes that at least one item is selectable! -- also assumes that the integer is 1 or -1 moveSelectionMapStep :: Int -> SelectionIntMap a -> SelectionIntMap a moveSelectionMapStep x sm = fromMaybe sm $ do i <- _smSelPos sm (n,_) <- IM.lookupMax (sm ^. smShownItems) let j = (i - x) `mod` (n + 1) case sm ^? smShownItems . ix j . siIsSelectable of Just True -> Just $ sm & smSelPos ?~ j _ -> Just $ moveSelectionMapStep x (sm & smSelPos ?~ j) moveSelectionMapSelection :: Int -> SelectionIntMap a -> SelectionIntMap a moveSelectionMapSelection i sm | any _siIsSelectable (sm ^. smShownItems) = foldl' (&) sm (replicate (abs i) (moveSelectionMapStep (signum i))) | otherwise = sm & smSelPos .~ Nothing getIthPos :: Int -> IM.IntMap (SelectionItem a) -> Int getIthPos i sm = sum . fmap _siHeight . fst $ IM.split i sm getIthYOffset :: Float -> Float -> SelectionIntMap a -> Int -> Float getIthYOffset ygap scale sm i = (scale * 10 + ygap) * (fromIntegral yint + 1) where yint = getIthPos i (sm ^. smShownItems)