{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module UI.ComposeEditor.Main
( attachmentsEditor
, renderPart
, drawHeaders
, renderConfirm
) where
import Brick.Types (Padding(..), Widget)
import Brick.Widgets.Core
((<+>), (<=>), emptyWidget, hLimit, padBottom, padLeft,
txt, vLimit)
import qualified Brick.Widgets.Edit as E
import qualified Brick.Widgets.List as L
import Brick.Widgets.Dialog (renderDialog)
import Brick.Widgets.Center (hCenter)
import Control.Lens (to, view)
import qualified Data.Text as T
import Data.Text.Zipper (currentLine)
import Data.MIME (headers)
import UI.Views (focusedViewWidget)
import UI.Draw.Main (attachmentsHeader)
import UI.Mail.Main (renderPart)
import Types
attachmentsEditor :: AppState -> Widget Name
attachmentsEditor s =
let hasFocus = ComposeListOfAttachments == focusedViewWidget s
attachmentsList =
L.renderList (\isSel -> renderPart charsets isSel . view headers) hasFocus (view (asCompose . cAttachments) s)
charsets = view (asConfig . confCharsets) s
in attachmentsHeader <=> attachmentsList
drawHeaders :: AppState -> Widget Name
drawHeaders s =
let headers' = [ComposeSubject, ComposeBcc, ComposeCc, ComposeTo, ComposeFrom]
in padBottom (Pad 1) $ foldr (drawTableRows s) (txt T.empty) headers'
drawTableRows :: AppState -> Name -> Widget Name -> Widget Name
drawTableRows s name w = w
<=> vLimit 1
(hLimit 15 (padLeft Max (makeLabel name))
<+> padLeft (Pad 1) (txt (widgetValue name s)))
makeLabel :: Name -> Widget Name
makeLabel ComposeFrom = txt "From:"
makeLabel ComposeTo = txt "To:"
makeLabel ComposeCc = txt "Cc:"
makeLabel ComposeBcc = txt "Bcc:"
makeLabel _ = txt "Subject:"
widgetValue :: Name -> AppState -> T.Text
widgetValue ComposeFrom = view (asCompose . cFrom . E.editContentsL . to currentLine)
widgetValue ComposeTo = view (asCompose . cTo . E.editContentsL . to currentLine)
widgetValue ComposeCc = view (asCompose . cCc . E.editContentsL . to currentLine)
widgetValue ComposeBcc = view (asCompose . cBcc . E.editContentsL . to currentLine)
widgetValue ComposeSubject = view (asCompose . cSubject . E.editContentsL . to currentLine)
widgetValue _ = const T.empty
renderConfirm :: AppState -> Widget Name
renderConfirm s = renderDialog (view (asCompose . cKeepDraft) s) $ hCenter emptyWidget