From 05d6b5f93cd1ed5e9c4f4f9d63cf392ada048afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Mon, 26 Aug 2013 13:44:44 -0300 Subject: towards lazy reading (broken) --- Handlers.hs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Handlers.hs (limited to 'Handlers.hs') diff --git a/Handlers.hs b/Handlers.hs new file mode 100644 index 0000000..566fca9 --- /dev/null +++ b/Handlers.hs @@ -0,0 +1,61 @@ +{- Event handlers for Lazymail + - + - Copyright 2013 Raúl Benencia + - + - Licensed under the GNU GPL version 3 or higher + -} + +module Handlers where + +import Control.Monad.State + +import Maildir +import State +import Types (LazymailCurses) + +previousMode :: Mode -> LazymailCurses () +previousMode IndexMode = (=<<) put $ get >>= \st -> return st { mode = MaildirMode } +previousMode EmailMode = (=<<) put $ get >>= \st -> return st { mode = IndexMode } +previousMode MaildirMode = (=<<) put $ get >>= \st -> return st { exitRequested = True } + +changeMode IndexMode = (=<<) put $ get >>= \st -> return st { mode = EmailMode } +changeMode EmailMode = return () +changeMode MaildirMode = do + st <- get + selectedEmails' <- liftIO $ do + let md = (selectedMD . maildirState) $ st + getMaildirEmails md + let indexState' = (indexState st) { + selectedEmails = selectedEmails' + , currentInLen = length selectedEmails' + , scrollBufferIn = scrollCrop (scrollRowIn . indexState $ st) (screenRows st) selectedEmails' + } + put $ st { mode = IndexMode, indexState = indexState' } + +incSelectedRow IndexMode = do + st <- get + let inSt = indexState st + if (selectedRowIn inSt) > (div (screenRows st) 2) + then do + let scrollRowIn' = scrollRowIn inSt + 1 + let scrollBufferIn' = scrollCrop scrollRowIn' (screenRows st) $ selectedEmails inSt + let inSt' = inSt { scrollRowIn = scrollRowIn', scrollBufferIn = scrollBufferIn' } + put st { indexState = inSt' } + else put $ incrementSelectedRow st +incSelectedRow _ = (=<<) put $ get >>= \st -> return $ incrementSelectedRow st + +decSelectedRow IndexMode = do + st <- get + let inSt = indexState st + if (scrollRowIn inSt) > 0 + then do + let scrollRowIn' = scrollRowIn inSt - 1 + let scrollBufferIn' = scrollCrop scrollRowIn' (screenRows st) $ selectedEmails inSt + let inSt' = inSt { scrollRowIn = scrollRowIn', scrollBufferIn = scrollBufferIn' } + put st { indexState = inSt' } + else put $ decrementSelectedRow st +decSelectedRow _ = (=<<) put $ get >>= \st -> return $ decrementSelectedRow st + +{- Given a list, it returns the elements that will be in the next screen refresh + - TODO: find a better name -} +scrollCrop top rows xs = take rows $ drop top xs -- cgit v1.2.3