{-# LANGUAGE OverloadedStrings #-}

module Brick.Widgets.BetterDialog ( dialog )where

import Brick
import Brick.Widgets.Border
import Graphics.Vty
import Data.Text (Text)
import Lens.Micro

dialog :: Text -> Text -> Widget n
dialog :: forall n. Text -> Text -> Widget n
dialog Text
title = forall n. Widget n -> Widget n
center forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. Text -> Text -> Widget n
dialog' Text
title

-- TODO Remove duplication from HelpMessage
center :: Widget n -> Widget n
center :: forall n. Widget n -> Widget n
center Widget n
w = forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed forall a b. (a -> b) -> a -> b
$ do
  Context n
c <- forall n. RenderM n (Context n)
getContext
  Result n
res <- forall n. Widget n -> RenderM n (Result n)
render Widget n
w
  let rWidth :: Int
rWidth = Result n
resforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Result n) Image
imageLforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. (s -> a) -> SimpleGetter s a
to Image -> Int
imageWidth
      rHeight :: Int
rHeight = Result n
resforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Result n) Image
imageLforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. (s -> a) -> SimpleGetter s a
to Image -> Int
imageHeight
      x :: Int
x = (Context n
cforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Context n) Int
availWidthL forall a. Integral a => a -> a -> a
`div` Int
2) forall a. Num a => a -> a -> a
- (Int
rWidth forall a. Integral a => a -> a -> a
`div` Int
2)
      y :: Int
y = (Context n
cforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Context n) Int
availHeightL forall a. Integral a => a -> a -> a
`div` Int
2) forall a. Num a => a -> a -> a
- (Int
rHeight forall a. Integral a => a -> a -> a
`div` Int
2)

  forall n. Widget n -> RenderM n (Result n)
render forall a b. (a -> b) -> a -> b
$ forall n. Location -> Widget n -> Widget n
translateBy ((Int, Int) -> Location
Location (Int
x,Int
y)) forall a b. (a -> b) -> a -> b
$ forall n. Image -> Widget n
raw (Result n
resforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Result n) Image
imageL)

dialog' :: Text -> Text -> Widget n
dialog' :: forall n. Text -> Text -> Widget n
dialog' Text
title Text
content = forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed forall a b. (a -> b) -> a -> b
$ do
  Context n
c <- forall n. RenderM n (Context n)
getContext

  forall n. Widget n -> RenderM n (Result n)
render forall a b. (a -> b) -> a -> b
$
    forall n. Int -> Widget n -> Widget n
hLimit (forall a. Ord a => a -> a -> a
min Int
80 forall a b. (a -> b) -> a -> b
$ Context n
cforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Context n) Int
availWidthL) forall a b. (a -> b) -> a -> b
$
    forall n. Int -> Widget n -> Widget n
vLimit (forall a. Ord a => a -> a -> a
min Int
30 forall a b. (a -> b) -> a -> b
$ Context n
cforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Context n) Int
availHeightL) forall a b. (a -> b) -> a -> b
$
    forall n. Widget n -> Widget n -> Widget n
borderWithLabel (forall n. Text -> Widget n
txt Text
title) forall a b. (a -> b) -> a -> b
$
          forall n. Text -> Widget n
txt Text
" "
      forall n. Widget n -> Widget n -> Widget n
<=> (forall n. Text -> Widget n
txt Text
" " forall n. Widget n -> Widget n -> Widget n
<+> forall n. Text -> Widget n
txt Text
content forall n. Widget n -> Widget n -> Widget n
<+> forall n. Text -> Widget n
txt Text
" ")
      forall n. Widget n -> Widget n -> Widget n
<=> forall n. Text -> Widget n
txt Text
" "