ghcup-hs/lib-tui/GHCup/Brick/Widgets/Menus/Context.hs

78 lines
2.6 KiB
Haskell
Raw Normal View History

2024-02-28 08:37:17 +00:00
{-# LANGUAGE OverloadedStrings #-}
module GHCup.Brick.Widgets.Menus.Context (ContextMenu, create, draw, handler) where
import Brick (
Widget (..), BrickEvent, EventM,
)
import Data.Function ((&))
import Prelude hiding (appendFile)
import Data.Versions (prettyVer)
2024-02-28 16:11:00 +00:00
import GHCup.List ( ListResult(..) )
import GHCup.Types (KeyCombination, Tool (..))
import qualified GHCup.Brick.Common as Common
import qualified GHCup.Brick.Widgets.Menu as Menu
2024-02-28 08:37:17 +00:00
import GHCup.Brick.Common (Name (..))
import GHCup.Brick.Widgets.Menu (Menu)
2024-02-28 16:11:00 +00:00
import qualified Brick.Widgets.Core as Brick
import qualified Brick.Widgets.Border as Border
import qualified Brick.Focus as F
import Brick.Widgets.Core ((<+>))
2024-02-28 08:37:17 +00:00
import Optics (to)
import Optics.Operators ((.~), (^.))
import Optics.Optic ((%))
2024-02-28 16:11:00 +00:00
import Data.Foldable (foldl')
2024-02-28 08:37:17 +00:00
type ContextMenu = Menu ListResult Name
create :: ListResult -> KeyCombination -> ContextMenu
create lr exit_key = Menu.createMenu Common.ContextBox lr exit_key buttons []
where
advInstallButton =
Menu.createButtonField (MenuElement Common.AdvanceInstallButton)
& Menu.fieldLabelL .~ "Install"
& Menu.fieldHelpMsgL .~ "Advance Installation Settings"
compileButton =
2024-02-28 09:29:39 +00:00
Menu.createButtonField (MenuElement Common.CompilieButton)
2024-02-28 08:37:17 +00:00
& Menu.fieldLabelL .~ "Compile"
2024-02-28 16:11:00 +00:00
& Menu.fieldHelpMsgL .~ "Compile tool from source"
2024-02-28 08:37:17 +00:00
buttons =
case lTool lr of
GHC -> [advInstallButton, compileButton]
HLS -> [advInstallButton, compileButton]
_ -> [advInstallButton]
draw :: ContextMenu -> Widget Name
2024-02-28 16:11:00 +00:00
draw menu =
2024-02-28 08:37:17 +00:00
Common.frontwardLayer
2024-02-28 16:11:00 +00:00
("Context Menu for " <> tool_str <> " " <> prettyVer (lVer $ menu ^. Menu.menuStateL))
$ Brick.vBox
[ Brick.vBox buttonWidgets
, Brick.txt " "
, Brick.padRight Brick.Max $
Brick.txt "Press "
<+> Common.keyToWidget (menu ^. Menu.menuExitKeyL)
<+> Brick.txt " to go back"
]
where
buttonLabels = [button & Menu.fieldLabel | button <- menu ^. Menu.menuButtonsL]
maxWidth = foldl' max 5 (fmap Brick.textWidth buttonLabels)
buttonAmplifiers =
let buttonAsWidgets = fmap Menu.renderAslabel buttonLabels
in fmap (\f b -> ((Menu.leftify (maxWidth + 10) . Border.border $ f b) <+>) ) buttonAsWidgets
drawButtons = fmap Menu.drawField buttonAmplifiers
buttonWidgets = zipWith (F.withFocusRing (menu ^. Menu.menuFocusRingL)) drawButtons (menu ^. Menu.menuButtonsL)
tool_str =
case menu ^. Menu.menuStateL % to lTool of
GHC -> "GHC"
GHCup -> "GHCup"
Cabal -> "Cabal"
HLS -> "HLS"
Stack -> "Stack"
2024-02-28 08:37:17 +00:00
handler :: BrickEvent Name e -> EventM Name ContextMenu ()
handler = Menu.handlerMenu