-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
{-# LANGUAGE TemplateHaskell #-}

-- |
-- SPDX-License-Identifier: BSD-3-Clause
--
-- A small custom "panel widget" for use in the Swarm TUI. Panels draw
-- a border around some content, with the color of the border
-- depending on whether the panel is currently focused.  Panels exist
-- within a 'FocusRing' such that the user can cycle between the
-- panels (using /e.g./ the @Tab@ key).
module Swarm.TUI.Panel (
  panel,
) where

import Brick
import Brick.Focus
import Brick.Widgets.Border
import Control.Lens
import Swarm.TUI.Border

data Panel n = Panel
  {forall n. Panel n -> n
_panelName :: n, forall n. Panel n -> BorderLabels n
_panelLabels :: BorderLabels n, forall n. Panel n -> Widget n
_panelContent :: Widget n}

makeLenses ''Panel

instance Named (Panel n) n where
  getName :: Panel n -> n
getName = forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall n. Lens' (Panel n) n
panelName

drawPanel :: Eq n => AttrName -> FocusRing n -> Panel n -> Widget n
drawPanel :: forall n. Eq n => AttrName -> FocusRing n -> Panel n -> Widget n
drawPanel AttrName
attr FocusRing n
fr = forall n a b.
(Eq n, Named a n) =>
FocusRing n -> (Bool -> a -> b) -> a -> b
withFocusRing FocusRing n
fr forall n. Bool -> Panel n -> Widget n
drawPanel'
 where
  drawPanel' :: Bool -> Panel n -> Widget n
  drawPanel' :: forall n. Bool -> Panel n -> Widget n
drawPanel' Bool
focused Panel n
p =
    (if Bool
focused then forall n. AttrName -> AttrName -> Widget n -> Widget n
overrideAttr AttrName
borderAttr AttrName
attr else forall a. a -> a
id) forall a b. (a -> b) -> a -> b
$
      forall n. BorderLabels n -> Widget n -> Widget n
borderWithLabels (Panel n
p forall s a. s -> Getting a s a -> a
^. forall n. Lens' (Panel n) (BorderLabels n)
panelLabels) (Panel n
p forall s a. s -> Getting a s a -> a
^. forall n. Lens' (Panel n) (Widget n)
panelContent)

-- | Create a panel.
panel ::
  Eq n =>
  -- | Border attribute to use when the panel is focused.
  AttrName ->
  -- | Focus ring the panel should be part of.
  FocusRing n ->
  -- | The name of the panel. Must be unique.
  n ->
  -- | The labels to use around the border.
  BorderLabels n ->
  -- | The content of the panel.
  Widget n ->
  Widget n
panel :: forall n.
Eq n =>
AttrName
-> FocusRing n -> n -> BorderLabels n -> Widget n -> Widget n
panel AttrName
attr FocusRing n
fr n
nm BorderLabels n
labs Widget n
w = forall n. Eq n => AttrName -> FocusRing n -> Panel n -> Widget n
drawPanel AttrName
attr FocusRing n
fr (forall n. n -> BorderLabels n -> Widget n -> Panel n
Panel n
nm BorderLabels n
labs Widget n
w)