From b305546950a6742f25023e2ffece423905e1bda8 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 28 Dec 2024 20:27:04 +0530 Subject: Refactor mode data type --- src/Chelleport/View.hs | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) (limited to 'src/Chelleport/View.hs') diff --git a/src/Chelleport/View.hs b/src/Chelleport/View.hs index 790b3f7..03c12d3 100644 --- a/src/Chelleport/View.hs +++ b/src/Chelleport/View.hs @@ -11,34 +11,29 @@ import Foreign.C (CInt) render :: (MonadDraw m) => State -> m () render state = case stateMode state of - ModeHints -> renderHintsView state - ModeSearch {searchFilteredWords, searchHighlightedIndex} -> - renderSearchView state searchFilteredWords searchHighlightedIndex - -getSearchText :: State -> String -getSearchText state = case stateMode state of - ModeHints -> "" - ModeSearch {searchInputText, searchFilteredWords, searchHighlightedIndex} -> + ModeHints _ -> renderHintsView state + ModeSearch modeSearchData -> renderSearchView state modeSearchData + +getSearchText :: State -> ModeSearchData -> String +getSearchText state (ModeSearchData {searchInputText, searchFilteredWords, searchHighlightedIndex}) = searchText + where searchText - where - searchText - | stateIsModeInitialized state = "Searching (" ++ matchCount ++ "): " ++ searchInputText - | otherwise = "Loading..." - matchCount - | isEmpty searchFilteredWords = "0/0" - | otherwise = show (searchHighlightedIndex + 1) ++ "/" ++ show (length searchFilteredWords) - -renderSearchView :: (MonadDraw m) => State -> [OCRMatch] -> Int -> m () -renderSearchView state matches highlightedIndex = do + | stateIsModeInitialized state = "Searching (" ++ matchCount ++ "): " ++ searchInputText + | otherwise = "Loading..." + matchCount + | isEmpty searchFilteredWords = "0/0" + | otherwise = show (searchHighlightedIndex + 1) ++ "/" ++ show (length searchFilteredWords) + +renderSearchView :: (MonadDraw m) => State -> ModeSearchData -> m () +renderSearchView state searchData@(ModeSearchData {searchFilteredWords, searchHighlightedIndex}) = do renderGridLines state - forM_ (zip [0 ..] matches) $ \(index, OCRMatch {matchStartX, matchStartY, matchEndX, matchEndY}) -> do - setDrawColor $ if highlightedIndex == index then colorAccent else colorLightGray + forM_ (zip [0 ..] searchFilteredWords) $ \(index, OCRMatch {matchStartX, matchStartY, matchEndX, matchEndY}) -> do + setDrawColor $ if searchHighlightedIndex == index then colorAccent else colorLightGray fillRectVertices (matchStartX, matchStartY) (matchEndX, matchEndY) (w, h) <- windowSize - drawText (w `div` 2, h `div` 2) colorAccent FontSM (Text.pack $ getSearchText state) - pure () + void $ drawText (w `div` 2, h `div` 2) colorWhite FontSM (Text.pack $ getSearchText state searchData) renderHintsView :: (MonadDraw m) => State -> m () renderHintsView state = do -- cgit v1.3.1