{-# LANGUAGE RankNTypes #-}
module Matterhorn.Events.UrlSelect where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick.Keybindings
import           Brick.Widgets.List
import qualified Graphics.Vty as Vty
import           Lens.Micro.Platform ( Lens' )

import           Matterhorn.State.UrlSelect
import           Matterhorn.State.SaveAttachmentWindow
import           Matterhorn.Types


onEventUrlSelect :: Lens' ChatState (MessageInterface Name i) -> Vty.Event -> MH Bool
onEventUrlSelect :: forall i.
Lens' ChatState (MessageInterface Name i) -> Event -> MH Bool
onEventUrlSelect 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
urlSelectKeybindings 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) (URLList n)
miUrlListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n1 n2.
Lens
  (URLList n1)
  (URLList n2)
  (List n1 (Int, LinkChoice))
  (List n2 (Int, LinkChoice))
ulList) forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> EventM n (GenericList n t e) ()
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
                    ]

urlSelectKeybindings :: Lens' ChatState (MessageInterface Name i) -> KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH
urlSelectKeybindings :: forall i.
Lens' ChatState (MessageInterface Name i)
-> KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH
urlSelectKeybindings 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]
urlSelectKeyHandlers Lens' ChatState (MessageInterface Name i)
which)

urlSelectKeyHandlers :: Lens' ChatState (MessageInterface Name i) -> [MHKeyEventHandler]
urlSelectKeyHandlers :: forall i.
Lens' ChatState (MessageInterface Name i) -> [MHKeyEventHandler]
urlSelectKeyHandlers Lens' ChatState (MessageInterface Name i)
which =
    [ forall a (m :: * -> *) k.
ToBinding a =>
a -> Text -> m () -> KeyEventHandler k m
onKey (forall a. ToBinding a => a -> Binding
bind Key
Vty.KEnter)
          Text
"Open the selected URL, if any" forall a b. (a -> b) -> a -> b
$
          forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
openSelectedURL Lens' ChatState (MessageInterface Name i)
which

    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
SaveAttachmentEvent Text
"Save the selected attachment" forall a b. (a -> b) -> a -> b
$
        forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
openSaveAttachmentWindow Lens' ChatState (MessageInterface Name i)
which

    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
CancelEvent Text
"Cancel URL selection" forall a b. (a -> b) -> a -> b
$ forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
stopUrlSelect Lens' ChatState (MessageInterface Name i)
which

    , 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) (URLList n)
miUrlListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n1 n2.
Lens
  (URLList n1)
  (URLList n2)
  (List n1 (Int, LinkChoice))
  (List n2 (Int, LinkChoice))
ulList) forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> EventM n (GenericList n t e) ()
handleListEvent (Key -> [Modifier] -> Event
Vty.EvKey Key
Vty.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) (URLList n)
miUrlListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n1 n2.
Lens
  (URLList n1)
  (URLList n2)
  (List n1 (Int, LinkChoice))
  (List n2 (Int, LinkChoice))
ulList) forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> EventM n (GenericList n t e) ()
handleListEvent (Key -> [Modifier] -> Event
Vty.EvKey Key
Vty.KDown [])

    , forall a (m :: * -> *) k.
ToBinding a =>
a -> Text -> m () -> KeyEventHandler k m
onKey (forall a. ToBinding a => a -> Binding
bind Char
'q')
         Text
"Cancel URL selection" forall a b. (a -> b) -> a -> b
$
         forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
stopUrlSelect Lens' ChatState (MessageInterface Name i)
which
    ]