{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
module Matterhorn.Events.ManageAttachments
  ( onEventAttachmentList
  , onEventBrowseFile
  , attachmentListKeybindings
  , attachmentBrowseKeyHandlers
  , attachmentBrowseKeybindings
  , attachmentListKeyHandlers
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick.Keybindings
import qualified Brick.Widgets.FileBrowser as FB
import qualified Brick.Widgets.List as L
import qualified Data.Text as T
import qualified Data.Vector as Vector
import qualified Graphics.Vty as V
import           Lens.Micro.Platform ( (?=), (%=), to, Lens', (.=) )

import           Matterhorn.Types
import           Matterhorn.State.Attachments
import           Matterhorn.State.Common


onEventAttachmentList :: Lens' ChatState (MessageInterface Name i)
                      -> V.Event
                      -> MH Bool
onEventAttachmentList :: forall i.
Lens' ChatState (MessageInterface Name i) -> Event -> MH Bool
onEventAttachmentList Lens' ChatState (MessageInterface Name i)
which =
    [Event -> MH Bool] -> Event -> MH Bool
handleEventWith [ (KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH)
-> Event -> MH Bool
mhHandleKeyboardEvent (forall i.
Lens' ChatState (MessageInterface Name i)
-> KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH
attachmentListKeybindings Lens' ChatState (MessageInterface Name i)
which)
                    , \Event
e -> forall b e.
Lens' ChatState b -> (e -> EventM Name b ()) -> e -> MH ()
mhZoom (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentList) forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> EventM n (GenericList n t e) ()
L.handleListEvent Event
e forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                    ]

attachmentListKeybindings :: Lens' ChatState (MessageInterface Name i)
                          -> KeyConfig KeyEvent
                          -> KeyDispatcher KeyEvent MH
attachmentListKeybindings :: forall i.
Lens' ChatState (MessageInterface Name i)
-> KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH
attachmentListKeybindings Lens' ChatState (MessageInterface Name i)
which KeyConfig KeyEvent
kc = forall k (m :: * -> *).
Ord k =>
KeyConfig k -> [KeyEventHandler k m] -> KeyDispatcher k m
unsafeKeyDispatcher KeyConfig KeyEvent
kc (forall i.
Lens' ChatState (MessageInterface Name i) -> [MHKeyEventHandler]
attachmentListKeyHandlers Lens' ChatState (MessageInterface Name i)
which)

attachmentListKeyHandlers :: Lens' ChatState (MessageInterface Name i)
                          -> [MHKeyEventHandler]
attachmentListKeyHandlers :: forall i.
Lens' ChatState (MessageInterface Name i) -> [MHKeyEventHandler]
attachmentListKeyHandlers Lens' ChatState (MessageInterface Name i)
which =
    [ forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
CancelEvent Text
"Close attachment list" forall a b. (a -> b) -> a -> b
$
          Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) MessageInterfaceMode
miMode forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= MessageInterfaceMode
Compose
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
SelectUpEvent Text
"Move cursor up" forall a b. (a -> b) -> a -> b
$
          forall b e.
Lens' ChatState b -> (e -> EventM Name b ()) -> e -> MH ()
mhZoom (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentList) forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> EventM n (GenericList n t e) ()
L.handleListEvent (Key -> [Modifier] -> Event
V.EvKey Key
V.KUp [])
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
SelectDownEvent Text
"Move cursor down" forall a b. (a -> b) -> a -> b
$
          forall b e.
Lens' ChatState b -> (e -> EventM Name b ()) -> e -> MH ()
mhZoom (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentList) forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> EventM n (GenericList n t e) ()
L.handleListEvent (Key -> [Modifier] -> Event
V.EvKey Key
V.KDown [])
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
AttachmentListAddEvent Text
"Add a new attachment to the attachment list" forall a b. (a -> b) -> a -> b
$
          forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
showAttachmentFileBrowser Lens' ChatState (MessageInterface Name i)
which
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
AttachmentOpenEvent Text
"Open the selected attachment using the URL open command" forall a b. (a -> b) -> a -> b
$
          forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
openSelectedAttachment Lens' ChatState (MessageInterface Name i)
which
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
AttachmentListDeleteEvent Text
"Delete the selected attachment from the attachment list" forall a b. (a -> b) -> a -> b
$
          forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
deleteSelectedAttachment Lens' ChatState (MessageInterface Name i)
which
    ]

attachmentBrowseKeybindings :: Lens' ChatState (MessageInterface Name i)
                            -> KeyConfig KeyEvent
                            -> KeyDispatcher KeyEvent MH
attachmentBrowseKeybindings :: forall i.
Lens' ChatState (MessageInterface Name i)
-> KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH
attachmentBrowseKeybindings Lens' ChatState (MessageInterface Name i)
which KeyConfig KeyEvent
kc =
    forall k (m :: * -> *).
Ord k =>
KeyConfig k -> [KeyEventHandler k m] -> KeyDispatcher k m
unsafeKeyDispatcher KeyConfig KeyEvent
kc (forall i.
Lens' ChatState (MessageInterface Name i) -> [MHKeyEventHandler]
attachmentBrowseKeyHandlers Lens' ChatState (MessageInterface Name i)
which)

attachmentBrowseKeyHandlers :: Lens' ChatState (MessageInterface Name i)
                            -> [MHKeyEventHandler]
attachmentBrowseKeyHandlers :: forall i.
Lens' ChatState (MessageInterface Name i) -> [MHKeyEventHandler]
attachmentBrowseKeyHandlers Lens' ChatState (MessageInterface Name i)
which =
    [ forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
CancelEvent Text
"Cancel attachment file browse" forall a b. (a -> b) -> a -> b
$
      forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
cancelAttachmentBrowse Lens' ChatState (MessageInterface Name i)
which
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
AttachmentOpenEvent Text
"Open the selected file using the URL open command" forall a b. (a -> b) -> a -> b
$
      forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
openSelectedBrowserEntry Lens' ChatState (MessageInterface Name i)
which
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserBeginSearchEvent Text
"Begin search for name in list" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. EventM n (FileBrowser n) ()
FB.actionFileBrowserBeginSearch
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserSelectEnterEvent Text
"Select file or enter directory" forall a b. (a -> b) -> a -> b
$ do
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. EventM n (FileBrowser n) ()
FB.actionFileBrowserSelectEnter
      forall i.
Lens' ChatState (MessageInterface Name i)
-> (FileBrowser Name -> MH ()) -> MH ()
withFileBrowser Lens' ChatState (MessageInterface Name i)
which (forall i.
Lens' ChatState (MessageInterface Name i) -> [FileInfo] -> MH ()
tryAddAttachment Lens' ChatState (MessageInterface Name i)
which forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. FileBrowser n -> [FileInfo]
FB.fileBrowserSelection)
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserSelectCurrentEvent Text
"Select file" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. EventM n (FileBrowser n) ()
FB.actionFileBrowserSelectCurrent
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserListPageUpEvent Text
"Move cursor one page up" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. Ord n => EventM n (FileBrowser n) ()
FB.actionFileBrowserListPageUp
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserListPageDownEvent Text
"Move cursor one page down" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. Ord n => EventM n (FileBrowser n) ()
FB.actionFileBrowserListPageDown
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserListHalfPageUpEvent Text
"Move cursor one-half page up" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. Ord n => EventM n (FileBrowser n) ()
FB.actionFileBrowserListHalfPageUp
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserListHalfPageDownEvent Text
"Move cursor one-half page down" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. Ord n => EventM n (FileBrowser n) ()
FB.actionFileBrowserListHalfPageDown
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserListTopEvent Text
"Move cursor to top of list" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. Ord n => EventM n (FileBrowser n) ()
FB.actionFileBrowserListTop
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserListBottomEvent Text
"Move cursor to bottom of list" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. Ord n => EventM n (FileBrowser n) ()
FB.actionFileBrowserListBottom
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserListNextEvent Text
"Move cursor down" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. Ord n => EventM n (FileBrowser n) ()
FB.actionFileBrowserListNext
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FileBrowserListPrevEvent Text
"Move cursor up" forall a b. (a -> b) -> a -> b
$
      forall b. Lens' ChatState b -> EventM Name b () -> MH ()
mhZoom' (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser)
        forall n. Ord n => EventM n (FileBrowser n) ()
FB.actionFileBrowserListPrev
    ]

withFileBrowser :: Lens' ChatState (MessageInterface Name i)
                -> ((FB.FileBrowser Name) -> MH ())
                -> MH ()
withFileBrowser :: forall i.
Lens' ChatState (MessageInterface Name i)
-> (FileBrowser Name -> MH ()) -> MH ()
withFileBrowser Lens' ChatState (MessageInterface Name i)
which FileBrowser Name -> MH ()
f = do
    forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (Maybe (FileBrowser n))
esFileBrowser) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Maybe (FileBrowser Name)
Nothing -> do
            -- The widget has not been created yet.  This should
            -- normally not occur, because the ManageAttachments
            -- events should not fire when there is no FileBrowser
            -- Widget active to cause Brick to generate these events.
            -- This could therefore be implemented as an `error "BUG:
            -- ..."` handler, but the more benign approach is to
            -- simply create an available FileBrowser at this stage.
            ChannelId
cId <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) ChannelId
esChannelId)
            FileBrowser Name
new_b <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall n.
(FileInfo -> Bool) -> n -> Maybe FilePath -> IO (FileBrowser n)
FB.newFileBrowser FileInfo -> Bool
FB.selectNonDirectories (ChannelId -> Name
AttachmentFileBrowser ChannelId
cId) forall a. Maybe a
Nothing
            Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (Maybe (FileBrowser n))
esFileBrowser forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a (Maybe b) -> b -> m ()
?= FileBrowser Name
new_b
            FileBrowser Name -> MH ()
f FileBrowser Name
new_b
        Just FileBrowser Name
b -> FileBrowser Name -> MH ()
f FileBrowser Name
b

openSelectedAttachment :: Lens' ChatState (MessageInterface Name i) -> MH ()
openSelectedAttachment :: forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
openSelectedAttachment Lens' ChatState (MessageInterface Name i)
which = do
    Maybe (Int, AttachmentData)
cur <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. (s -> a) -> SimpleGetter s a
to forall (t :: * -> *) e n.
(Splittable t, Traversable t, Semigroup (t e)) =>
GenericList n t e -> Maybe (Int, e)
L.listSelectedElement)
    case Maybe (Int, AttachmentData)
cur of
        Maybe (Int, AttachmentData)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just (Int
_, AttachmentData
entry) -> forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ FilePath -> MH ()
openFilePath (FileInfo -> FilePath
FB.fileInfoFilePath forall a b. (a -> b) -> a -> b
$
                                                AttachmentData -> FileInfo
attachmentDataFileInfo AttachmentData
entry)

openSelectedBrowserEntry :: Lens' ChatState (MessageInterface Name i) -> MH ()
openSelectedBrowserEntry :: forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
openSelectedBrowserEntry Lens' ChatState (MessageInterface Name i)
which = forall i.
Lens' ChatState (MessageInterface Name i)
-> (FileBrowser Name -> MH ()) -> MH ()
withFileBrowser Lens' ChatState (MessageInterface Name i)
which forall a b. (a -> b) -> a -> b
$ \FileBrowser Name
b ->
    case forall n. FileBrowser n -> Maybe FileInfo
FB.fileBrowserCursor FileBrowser Name
b of
        Maybe FileInfo
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just FileInfo
entry -> forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ FilePath -> MH ()
openFilePath (FileInfo -> FilePath
FB.fileInfoFilePath FileInfo
entry)

onEventBrowseFile :: Lens' ChatState (MessageInterface Name i) -> V.Event -> MH Bool
onEventBrowseFile :: forall i.
Lens' ChatState (MessageInterface Name i) -> Event -> MH Bool
onEventBrowseFile Lens' ChatState (MessageInterface Name i)
which Event
e = do
    forall i.
Lens' ChatState (MessageInterface Name i)
-> (FileBrowser Name -> MH ()) -> MH ()
withFileBrowser Lens' ChatState (MessageInterface Name i)
which forall a b. (a -> b) -> a -> b
$ \FileBrowser Name
b -> do
        case forall n. FileBrowser n -> Bool
FB.fileBrowserIsSearching FileBrowser Name
b of
            Bool
False ->
                forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ [Event -> MH Bool] -> Event -> MH Bool
handleEventWith [ (KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH)
-> Event -> MH Bool
mhHandleKeyboardEvent (forall i.
Lens' ChatState (MessageInterface Name i)
-> KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH
attachmentBrowseKeybindings Lens' ChatState (MessageInterface Name i)
which)
                                       , \Event
_ -> forall i.
Lens' ChatState (MessageInterface Name i) -> Event -> MH ()
handleFileBrowserEvent Lens' ChatState (MessageInterface Name i)
which Event
e forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                                       ] Event
e
            Bool
True ->
                forall i.
Lens' ChatState (MessageInterface Name i) -> Event -> MH ()
handleFileBrowserEvent Lens' ChatState (MessageInterface Name i)
which Event
e

    -- n.b. the FileBrowser may have been updated above, so re-acquire it
    forall i.
Lens' ChatState (MessageInterface Name i)
-> (FileBrowser Name -> MH ()) -> MH ()
withFileBrowser Lens' ChatState (MessageInterface Name i)
which forall a b. (a -> b) -> a -> b
$ \FileBrowser Name
b -> do
        case forall n. FileBrowser n -> Maybe IOException
FB.fileBrowserException FileBrowser Name
b of
            Maybe IOException
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
            Just IOException
ex -> do
                LogCategory -> Text -> MH ()
mhLog LogCategory
LogError forall a b. (a -> b) -> a -> b
$ FilePath -> Text
T.pack forall a b. (a -> b) -> a -> b
$ FilePath
"FileBrowser exception: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> FilePath
show IOException
ex

    forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True

cancelAttachmentBrowse :: Lens' ChatState (MessageInterface Name i) -> MH ()
cancelAttachmentBrowse :: forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
cancelAttachmentBrowse Lens' ChatState (MessageInterface Name i)
which = do
    Vector AttachmentData
es <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n (t1 :: * -> *) e1 (t2 :: * -> *) e2.
Lens (GenericList n t1 e1) (GenericList n t2 e2) (t1 e1) (t2 e2)
L.listElementsL)
    Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) MessageInterfaceMode
miMode forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= case forall (t :: * -> *) a. Foldable t => t a -> Int
length Vector AttachmentData
es of
        Int
0 -> MessageInterfaceMode
Compose
        Int
_ -> MessageInterfaceMode
ManageAttachments

handleFileBrowserEvent :: Lens' ChatState (MessageInterface Name i) -> V.Event -> MH ()
handleFileBrowserEvent :: forall i.
Lens' ChatState (MessageInterface Name i) -> Event -> MH ()
handleFileBrowserEvent Lens' ChatState (MessageInterface Name i)
which Event
e = do
    forall b e.
Lens' ChatState b -> (e -> EventM Name b ()) -> e -> MH ()
mhZoom (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (FileBrowser n)
unsafeEsFileBrowser) forall n. Ord n => Event -> EventM n (FileBrowser n) ()
FB.handleFileBrowserEvent Event
e
    -- TODO: Check file browser exception state
    forall i.
Lens' ChatState (MessageInterface Name i)
-> (FileBrowser Name -> MH ()) -> MH ()
withFileBrowser Lens' ChatState (MessageInterface Name i)
which forall a b. (a -> b) -> a -> b
$ \FileBrowser Name
b ->
        forall i.
Lens' ChatState (MessageInterface Name i) -> [FileInfo] -> MH ()
tryAddAttachment Lens' ChatState (MessageInterface Name i)
which (forall n. FileBrowser n -> [FileInfo]
FB.fileBrowserSelection FileBrowser Name
b)

deleteSelectedAttachment :: Lens' ChatState (MessageInterface Name i) -> MH ()
deleteSelectedAttachment :: forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
deleteSelectedAttachment Lens' ChatState (MessageInterface Name i)
which = do
    Vector AttachmentData
es <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n (t1 :: * -> *) e1 (t2 :: * -> *) e2.
Lens (GenericList n t1 e1) (GenericList n t2 e2) (t1 e1) (t2 e2)
L.listElementsL)
    Maybe (Int, AttachmentData)
mSel <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. (s -> a) -> SimpleGetter s a
to forall (t :: * -> *) e n.
(Splittable t, Traversable t, Semigroup (t e)) =>
GenericList n t e -> Maybe (Int, e)
L.listSelectedElement)
    case Maybe (Int, AttachmentData)
mSel of
        Maybe (Int, AttachmentData)
Nothing ->
            forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just (Int
pos, AttachmentData
_) -> do
            Maybe Int
oldIdx <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL)
            let idx :: Maybe Int
idx = if forall a. Vector a -> Int
Vector.length Vector AttachmentData
es forall a. Eq a => a -> a -> Bool
== Int
1
                      then forall a. Maybe a
Nothing
                      else case Maybe Int
oldIdx of
                          Maybe Int
Nothing -> forall a. a -> Maybe a
Just Int
0
                          Just Int
old -> if Int
pos forall a. Ord a => a -> a -> Bool
>= Int
old
                                      then forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Int
pos forall a. Num a => a -> a -> a
- Int
1
                                      else forall a. a -> Maybe a
Just Int
pos
            Lens' ChatState (MessageInterface Name i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (EditState n)
miEditorforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentList forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= forall (t :: * -> *) e n.
(Foldable t, Splittable t) =>
t e -> Maybe Int -> GenericList n t e -> GenericList n t e
L.listReplace (forall a. Int -> Vector a -> Vector a
deleteAt Int
pos Vector AttachmentData
es) Maybe Int
idx

deleteAt :: Int -> Vector.Vector a -> Vector.Vector a
deleteAt :: forall a. Int -> Vector a -> Vector a
deleteAt Int
p Vector a
as | Int
p forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
p forall a. Ord a => a -> a -> Bool
>= forall (t :: * -> *) a. Foldable t => t a -> Int
length Vector a
as = Vector a
as
              | Bool
otherwise = forall a. Int -> Vector a -> Vector a
Vector.take Int
p Vector a
as forall a. Semigroup a => a -> a -> a
<> forall a. Int -> Vector a -> Vector a
Vector.drop (Int
p forall a. Num a => a -> a -> a
+ Int
1) Vector a
as