packages feed

nano-ui-0.2.0.0: lib/NanoUI/Internal/Widgets/Overlay.hs

-- | Modal dialogs and panels, and floating in-app windows.
module NanoUI.Internal.Widgets.Overlay
  ( modal
  , modal'
  , modalWith
  , modalWith'
  , modalPanel
  , modalPanel'
  , modalPanelWith
  , modalPanelWith'
  , floatingWindow
  , floatingWindow'
  , floatingWindowWith
  , floatingWindowWith'
  , windowTitleBarH
  , windowChromeSepH
  )
where

import Control.Monad (unless, void, when)
import Data.Bits ((.|.))
import Data.Text (Text)
import Data.Text qualified as T
import NanoUI.Internal.Context
import NanoUI.Internal.Input
import NanoUI.Internal.Layout.Arena (NodeType (..), addNodeFromLayout)
import NanoUI.Internal.Monad
import NanoUI.Internal.Style
import NanoUI.Internal.Types (Rect (..), Size (..), clamp)
import NanoUI.Internal.WidgetText (buttonCloseTrailing, buttonFlagClose)
import NanoUI.Internal.Widgets.Combinators (buttonStyledEx)
import NanoUI.Internal.Var.Types (Var)
import NanoUI.Internal.Widgets.Popup (bindOpen, floatingOverlay)
import NanoUI.Internal.Widgets.Layout hiding (panel)
import NanoUI.Internal.Widgets.Node

-- | A window's chrome above its body: the title bar, the 10px above it and
-- the rule under it, which the frame paints ('windowChromeSepH').
windowTitleBarH :: Float
windowTitleBarH = 28 + 10 + windowChromeSepH

windowChromeSepH :: Float
windowChromeSepH = 1

-- | A modal dialog with a title bar, shown while the Var holds 'True',
-- blocking interaction with the content behind it. Escape, a click outside
-- or its close button write 'False', so it closes in the same frame:
--
-- > asking <- useVar False
-- > whenM (button "Delete...") (writeVar asking True)
-- > modal asking "Delete the file?" $ do
-- >   whenM (button "Delete") (deleteFile >> writeVar asking False)
--
-- For an open flag derived from other state, or a dialog that must not
-- close until answered, pass a constant and act on the response from
-- 'modal''.
--
-- Open a dialog from a modal by declaring it inside that modal's body:
-- Escape, or a click on the outer modal, then closes only the inner one.
-- Modals declared side by side close in declaration order: Escape closes
-- the first even when the second is on top, and so does a click on the
-- second outside the first.
modal :: Var Bool -> Text -> NanoUI () -> NanoUI ()
modal open title body = void (modalWith' id open title body)

-- | 'modal' returning the body's result while open, and a 'Response' that
-- reports a close request as a click.
modal' :: Var Bool -> Text -> NanoUI a -> NanoUI (Response, Maybe a)
modal' = modalWith' id

-- | 'modal' with a layout modifier for its panel, which by default fits its
-- body. The title bar, the rule under it and the padding are the panel's
-- own. A panel given a fixed height holds its body rather than scrolling it,
-- and the body fills what the title bar leaves, so a body laid out with
-- @fillW . fillH@ takes exactly the panel's inside:
--
-- > winW <- windowWidth
-- > winH <- windowHeight
-- > modalWith (fixedWH (winW - 40) (winH - 40)) open "Find" $
-- >   columnWith (fillW . fillH) body
--
-- A panel is never larger than the window, less the margin every floating
-- panel keeps from its edge.
modalWith :: (Layout -> Layout) -> Var Bool -> Text -> NanoUI () -> NanoUI ()
modalWith f open title body = void (modalWith' f open title body)

-- | 'modalWith' returning the body's result and the 'Response'.
modalWith' :: (Layout -> Layout) -> Var Bool -> Text -> NanoUI a -> NanoUI (Response, Maybe a)
modalWith' f open title body = bindOpen open (\shown -> overlay True True f shown title body)

-- | A modal panel without a title bar, close button or surrounding padding.
-- The panel surface and modal backdrop remain; Escape or a click outside
-- writes 'False'.
modalPanel :: Var Bool -> NanoUI () -> NanoUI ()
modalPanel = modalPanelWith id

-- | 'modalPanel' returning the body's result and the 'Response'.
modalPanel' :: Var Bool -> NanoUI a -> NanoUI (Response, Maybe a)
modalPanel' = modalPanelWith' id

-- | 'modalPanel' with a layout modifier for its panel. A fixed-size panel's
-- body fills the panel, so a body laid out with @fillW . fillH@ takes exactly
-- the panel's inside. Use a small amount of padding only when the body should
-- sit clear of the panel's border.
modalPanelWith :: (Layout -> Layout) -> Var Bool -> NanoUI () -> NanoUI ()
modalPanelWith shape open body = void (modalPanelWith' shape open body)

-- | 'modalPanelWith' returning the body's result and the 'Response'.
modalPanelWith' :: (Layout -> Layout) -> Var Bool -> NanoUI a -> NanoUI (Response, Maybe a)
modalPanelWith' shape open body = bindOpen open (\shown -> overlay True False shape shown "" body)

-- | A draggable, resizable in-app floating window with a scrolling body, shown
-- while the Var holds 'True'. Its close button writes 'False'. Other windows
-- and the page remain interactive outside its bounds.
floatingWindow :: Var Bool -> Text -> NanoUI () -> NanoUI ()
floatingWindow = floatingWindowWith id

-- | 'floatingWindow' returning the body's result while open, and a 'Response'
-- that reports a close request as a click.
floatingWindow' :: Var Bool -> Text -> NanoUI a -> NanoUI (Response, Maybe a)
floatingWindow' = floatingWindowWith' id

-- | 'floatingWindow' with a layout modifier for its panel (for example,
-- 'fixedWH' or 'minW').
floatingWindowWith :: LayoutModifier -> Var Bool -> Text -> NanoUI () -> NanoUI ()
floatingWindowWith f open title body = void (floatingWindowWith' f open title body)

-- | 'floatingWindowWith' returning the body's result and the 'Response'.
floatingWindowWith' :: LayoutModifier -> Var Bool -> Text -> NanoUI a -> NanoUI (Response, Maybe a)
floatingWindowWith' f open title body = bindOpen open (\shown -> overlay False True f shown title body)

overlay ::
  Bool -> Bool -> (Layout -> Layout) -> Bool -> Text -> NanoUI a -> NanoUI (Response, Maybe a)
overlay isModal hasChrome shape open title child = do
  ctx <- askContext
  inp <- askInput
  let
    Size winW winH = inputWindowSize inp
    margin = windowMargin
    availW = max 1 (winW - 2 * margin)
    availH = max 1 (winH - 2 * margin)
    -- Chrome-bearing modals share the window's side padding. The body's
    -- scrollbar sits out in it just inside the panel's edge, that padding
    -- from the content. A bare modal panel has no padding of its own.
    padding
      | not hasChrome = Padding 0 0 0 0
      | isModal = windowPad {padB = 12}
      | otherwise = windowPad
    barH
      | not hasChrome = 0
      | isModal = 40
      | otherwise = windowTitleBarH
    -- Windows keep a side-pad between the chrome and the body. Standard
    -- modals keep their larger gap; a bare modal panel has none.
    bodyGap
      | not hasChrome = 0
      | isModal = 8
      | otherwise = 10
    minWidth = clamp 1 availW (if isModal then 260 else 280)
    minHeight =
      if isModal
        then 0
        else
          min availH (padT padding + windowTitleBarH + bodyGap + padB padding)
    -- The panel's own layout, as the caller shapes it. Its sizes are held
    -- inside the window, whatever the caller asked for.
    panel =
      shape
        defaultLayout
          { layoutDirection = Column
          , layoutWidth = Fit
          , layoutHeight = Fit
          , layoutPadding = padding
          , layoutGap = bodyGap
          , layoutMinW = minWidth
          , layoutMinH = minHeight
          , layoutMaxW = availW
          , layoutMaxH = availH
          , layoutAlignX = AlignStart
          , layoutAlignY = AlignTop
          }
    within avail = \case
      Fixed v -> Fixed (min avail v)
      sz -> sz
    panelW = within availW (layoutWidth panel)
    panelH = within availH (layoutHeight panel)
    addOverlayNode _ parent =
      addNodeFromLayout
        (ctxNodeArena ctx)
        (if isModal then NodeModal else NodeWindow)
        parent
        panel
          { layoutWidth = panelW
          , layoutHeight = panelH
          , layoutMinW = min availW (layoutMinW panel)
          , layoutMinH = min availH (layoutMinH panel)
          , layoutMaxW = min availW (layoutMaxW panel)
          , layoutMaxH = min availH (layoutMaxH panel)
          }
    enter = when isModal (beginModal ctx)
    titleLayout =
      (fixedH barH . alignMiddle . tight) defaultLayout {layoutMinH = barH, layoutMaxH = barH}
  floatingOverlay open (if isModal then Just (Rect 0 0 0 0) else Nothing) addOverlayNode enter $ do
    closed <-
      if hasChrome
        then do
          close <-
            row' (tight . gap 6 . itemsCenter . fixedH barH . fillW $ defaultLayout) $ do
              unless (T.null title) $
                (if isModal then id else withKey title) (void (labelEx titleLayout title))
              flex
              withKey ("close" :: Text) $
                buttonStyledEx True "" 0 (tight . fixedWH 24 24 . alignMiddle $ defaultLayout) $
                  buttonFlagClose .|. buttonCloseTrailing
          when (isModal && not (T.null title)) separator
          pure (respClicked close)
        else pure False
    -- A panel of a fixed height holds its body, which fills what the title
    -- bar leaves when there is one; any other panel scrolls a body taller
    -- than the window.
    r <- case panelH of
      Fixed _ -> columnWith (tight . grow . fillW) child
      _ -> scrollWith (tight . grow) child
    when isModal (liftIO (endModal ctx))
    pure (closed, r)