1module Matterhorn.Draw.ManageAttachments
2  ( drawManageAttachments
3  )
4where
5
6import           Prelude ()
7import           Matterhorn.Prelude
8
9import           Brick
10import           Brick.Widgets.Border
11import           Brick.Widgets.Center
12import qualified Brick.Widgets.FileBrowser as FB
13import           Brick.Widgets.List
14import           Data.Maybe ( fromJust )
15
16import           Matterhorn.Types
17import           Matterhorn.Types.KeyEvents
18import           Matterhorn.Events.Keybindings ( getFirstDefaultBinding )
19import           Matterhorn.Themes
20
21
22drawManageAttachments :: ChatState -> Widget Name
23drawManageAttachments st =
24    topLayer
25    where
26        topLayer = case st^.csCurrentTeam.tsMode of
27            ManageAttachments -> drawAttachmentList st
28            ManageAttachmentsBrowseFiles -> drawFileBrowser st
29            _ -> error "BUG: drawManageAttachments called in invalid mode"
30
31drawAttachmentList :: ChatState -> Widget Name
32drawAttachmentList st =
33    let addBinding = ppBinding $ getFirstDefaultBinding AttachmentListAddEvent
34        delBinding = ppBinding $ getFirstDefaultBinding AttachmentListDeleteEvent
35        escBinding = ppBinding $ getFirstDefaultBinding CancelEvent
36        openBinding = ppBinding $ getFirstDefaultBinding AttachmentOpenEvent
37    in centerLayer $
38       hLimit 60 $
39       vLimit 15 $
40       joinBorders $
41       borderWithLabel (withDefAttr clientEmphAttr $ txt "Attachments") $
42       vBox [ renderList renderAttachmentItem True (st^.csCurrentTeam.tsEditState.cedAttachmentList)
43            , hBorder
44            , hCenter $ withDefAttr clientMessageAttr $
45                        txt $ addBinding <> ":add " <>
46                              delBinding <> ":delete " <>
47                              openBinding <> ":open " <>
48                              escBinding <> ":close"
49            ]
50
51renderAttachmentItem :: Bool -> AttachmentData -> Widget Name
52renderAttachmentItem _ d =
53    padRight Max $ str $ FB.fileInfoSanitizedFilename $ attachmentDataFileInfo d
54
55drawFileBrowser :: ChatState -> Widget Name
56drawFileBrowser st =
57    centerLayer $
58    hLimit 60 $
59    vLimit 20 $
60    borderWithLabel (withDefAttr clientEmphAttr $ txt "Attach File") $
61    -- invariant: cedFileBrowser is not Nothing if appMode is
62    -- ManageAttachmentsBrowseFiles, and that is the only way to reach
63    -- this code, ergo the fromJust.
64    FB.renderFileBrowser True $ fromJust (st^.csCurrentTeam.tsEditState.cedFileBrowser)
65