packages feed

nano-ui-0.2.0.0: lib/NanoUI/Internal/Layout/ScrollGeometry.hs

-- | Scrollbar geometry: gutters, viewport clips, and track and thumb layout.
module NanoUI.Internal.Layout.ScrollGeometry
  ( ScrollPolicy (..)
  , ScrollConfig (..)
  , defaultScrollConfig
  , scroll2DConfig
  , ScrollBarLayout (..)
  , ScrollNode (..)
  , scrollNodeViewport
  , scrollNodeBars
  , scrollChromeLane
  , scrollBarLayout
  , scrollBarLayoutIn
  , scrollThumbSpan
  , scrollAxisRange
  , scrollOffsetFromThumb
  , onScrollBar
  , padContentClip
  , encodeScrollConfig
  , decodeScrollConfig
  , scrollConfigNative2D
  , scrollDefault1D
  , scrollVerticalHidden
  , scrollHorizontalHidden
  , scrollAxisGutter
  , scrollChromeSuppressed
  , scrollWheelSuppressed
  , isScrollStyle2D
  , tagClippedSpans
  , padTextClipRect
  , borderContentClip
  ) where

import Data.Bits ((.|.))
import Data.Maybe (mapMaybe)
import Data.Text (Text)
import Data.Word (Word64)
import NanoUI.Internal.Font
import NanoUI.Internal.Types (Color, Rect (..), V2 (..), clamp, rectContains, rectH, rectIntersect, rectW, rectX, rectY, v2X, v2Y)
import NanoUI.Internal.Layout.Arena (DirTag (..))
import NanoUI.Internal.Style (Direction (..), Padding (..), Side (..), Style (..), styleBorderWidth, styleHasSide, windowPad)
import qualified NanoUI.Internal.StyleBits as SB

-- | Axis scrollbar visibility and interaction policy.
data ScrollPolicy
  = ScrollAuto
  | ScrollAlways
  | ScrollNone
  | ScrollHidden
  | -- | Paint a floating thumb without a background track or reserved cross-axis
    -- gutter, so content uses the full viewport width and height.
    ScrollOverlay
  deriving (Eq, Show, Enum, Bounded)

-- | 2D scroll configuration (policy per axis).
data ScrollConfig = ScrollConfig
  { scrollPolicyX :: !ScrollPolicy
  , scrollPolicyY :: !ScrollPolicy
  , scrollBare :: !Bool
  -- ^ Paint only clipped children, without a background or border. Scrollbar
  -- policies still apply; combine with 'ScrollHidden' to omit all chrome. A
  -- bare scroller with something to scroll also keeps the wheel: an axis it
  -- cannot move passes to no scroller around or inside it.
  , scrollFocusable :: !Bool
  -- ^ Register the scroll container as a keyboard Tab stop and focus it on a
  -- click inside it that did not focus a text field, select, or focusable
  -- drawing, for keyboard-driven list views ('NanoUI.useListNav').
  }
  deriving (Eq, Show)

-- | Automatic vertical scrollbar, no horizontal scrollbar, painted background,
-- and not focusable.
defaultScrollConfig :: ScrollConfig
defaultScrollConfig = ScrollConfig ScrollNone ScrollAuto False False

-- | Automatic scrollbars on both axes, painted background, and not focusable.
scroll2DConfig :: ScrollConfig
scroll2DConfig = ScrollConfig ScrollAuto ScrollAuto False False

scrollConfigNative2D :: ScrollConfig -> Bool
scrollConfigNative2D cfg = scrollPolicyX cfg /= ScrollNone && scrollPolicyY cfg /= ScrollNone

-- | A scroll node's style index. 'SB.scrollMarker' is always set, so that
-- no scroll node's style index is 0.
encodeScrollConfig :: ScrollConfig -> Word64
encodeScrollConfig (ScrollConfig px py bare focusable) =
  SB.fieldMask SB.scrollMarker
    .|. SB.placeField SB.scrollPolicyX (fromEnum px)
    .|. SB.placeField SB.scrollPolicyY (fromEnum py)
    .|. (if bare then SB.fieldMask SB.scrollBare else 0)
    .|. (if focusable then SB.fieldMask SB.scrollFocusable else 0)

decodeScrollConfig :: Word64 -> ScrollConfig
decodeScrollConfig bits =
  ScrollConfig
    (SB.decodeStyleEnum SB.scrollPolicyX ScrollAuto bits)
    (SB.decodeStyleEnum SB.scrollPolicyY ScrollAuto bits)
    (SB.testField SB.scrollBare bits)
    (SB.testField SB.scrollFocusable bits)

scrollDefault1D :: Direction -> ScrollConfig
scrollDefault1D Column = defaultScrollConfig
scrollDefault1D Row = ScrollConfig ScrollAuto ScrollNone False False

scrollVerticalHidden :: ScrollConfig
scrollVerticalHidden = ScrollConfig ScrollNone ScrollHidden False False

scrollHorizontalHidden :: ScrollConfig
scrollHorizontalHidden = ScrollConfig ScrollHidden ScrollNone False False

-- | Cross-axis gutter for one bar. @trailPad@ is the scroller's padding on
-- the bar's side (right for the vertical bar, bottom for the horizontal one).
scrollAxisGutter ::
  ScrollPolicy ->
  ScrollBarSlot ->
  Float ->
  Float ->
  Float ->
  Float
scrollAxisGutter policy slot trailPad contentSize innerMain =
  case policy of
    ScrollAuto -> if scrollOverflows contentSize innerMain then scrollBarGutter slot trailPad else 0
    ScrollAlways -> scrollBarGutter slot trailPad
    _ -> 0

-- Vertical bar takes width. Horizontal bar takes height. Second pass
-- covers the corner case where one bar makes the other axis overflow.
scrollGutters2D ::
  ScrollBarSlot ->
  ScrollConfig ->
  Padding ->
  Float ->
  Float ->
  Float ->
  Float ->
  (Float, Float)
scrollGutters2D slot cfg pad contentW contentH innerW innerH =
  let gVert inner = scrollAxisGutter (scrollPolicyY cfg) slot (padR pad) contentH inner
      gHorz inner = scrollAxisGutter (scrollPolicyX cfg) slot (padB pad) contentW inner
      gW0 = gVert innerH
      gH0 = gHorz innerW
      gW = gVert (max 0 (innerH - gH0))
      gH = gHorz (max 0 (innerW - gW0))
   in (gW, gH)

isScrollStyle2D :: Word64 -> Bool
isScrollStyle2D si = si /= 0 && scrollConfigNative2D (decodeScrollConfig si)

-- | The policy of the axis a scroller laid out along @dir@ scrolls on.
{-# INLINE scrollPolicyFor #-}
scrollPolicyFor :: ScrollConfig -> DirTag -> ScrollPolicy
scrollPolicyFor cfg = \case
  DirColumn -> scrollPolicyY cfg
  DirRow -> scrollPolicyX cfg

-- | Whether the bar along @dir@ is never painted or grabbed.
scrollChromeSuppressed :: ScrollConfig -> DirTag -> Bool
scrollChromeSuppressed cfg dir =
  let p = scrollPolicyFor cfg dir in p /= ScrollAuto && p /= ScrollAlways && p /= ScrollOverlay

-- | Wheel eligibility is wider than chrome eligibility: a hidden bar never
-- paints or drags, but it still scrolls. Only a dead axis ('ScrollNone')
-- ignores the wheel outright. Native 2D scrollers always keep both axes
-- live by construction.
scrollWheelSuppressed :: ScrollConfig -> Bool -> DirTag -> Bool
scrollWheelSuppressed cfg native2D dir = not native2D && scrollPolicyFor cfg dir == ScrollNone

-- | Scroll range along one axis. Content that fits (modulo the trailing
-- padding, which must not surface a bar by itself) does not scroll; genuine
-- overflow extends the range past the last child by the trailing padding so
-- scrolling to the end still reveals it. Stored content sizes exclude the
-- trailing padding (see positionScrollChildren); this is where it is added
-- back into the reachable range.
scrollAxisRange :: Float -> Float -> Float -> Float
scrollAxisRange contentSize innerMain trailingPad
  | scrollOverflows contentSize innerMain = max 0 (contentSize + trailingPad - innerMain)
  | otherwise = 0

-- | Whether content of @contentSize@ overflows @innerMain@ by more than half
-- a pixel, the rounding a fractional layout leaves. The range and the gutter
-- both ask this, so an auto bar never reserves a lane it does not paint.
{-# INLINE scrollOverflows #-}
scrollOverflows :: Float -> Float -> Bool
scrollOverflows contentSize innerMain = contentSize > innerMain + 0.5

-- | Logical window-space track/thumb bounds and the maximum scroll offset.
data ScrollBarLayout = ScrollBarLayout
  { sbTrack :: Rect
  , sbThumb :: Rect
  , sbMaxOff :: Float
  }
  deriving (Eq, Show)

padContentClip :: Float -> Float -> Float -> Float -> Padding -> Rect
padContentClip x y w h pad =
  Rect
    (x + padL pad)
    (y + padT pad)
    (max 0 (w - padL pad - padR pad))
    (max 0 (h - padT pad - padB pad))

-- | What the scroll passes read off a scroll container: its bar slot, scroll
-- config, whether it scrolls natively in 2D, direction, padding, the content
-- extent along its main axis (the content height for 2D) and, for 2D, the
-- content width.
data ScrollNode = ScrollNode
  { snSlot :: !ScrollBarSlot
  , snConfig :: !ScrollConfig
  , sn2D :: !Bool
  , snDir :: !DirTag
  , snPad :: {-# UNPACK #-} !Padding
  , snContentMain :: {-# UNPACK #-} !Float
  , snContentW :: {-# UNPACK #-} !Float
  }

-- | Content viewport of a scroll node placed at @x y w h@: its padding box
-- minus the live scrollbar gutters.
{-# INLINE scrollNodeViewport #-}
scrollNodeViewport :: ScrollNode -> Float -> Float -> Float -> Float -> Rect
scrollNodeViewport (ScrollNode slot cfg native2D dir pad contentMain contentW) x y w h
  | native2D = Rect bx by (max 0 (innerW - gutterW)) (max 0 (innerH - gutterH))
  | dir == DirColumn = Rect bx by (max 0 (innerW - gutter (padR pad) innerH)) innerH
  | otherwise = Rect bx by innerW (max 0 (innerH - gutter (padB pad) innerW))
  where
    Rect bx by innerW innerH = padContentClip x y w h pad
    (gutterW, gutterH) = scrollGutters2D slot cfg pad contentW contentMain innerW innerH
    -- Strict, so its two calls pass unboxed floats (measured: boxing them
    -- cost the pointer scene 100 kB).
    gutter !trailPad !innerMain = scrollAxisGutter (scrollPolicyFor cfg dir) slot trailPad contentMain innerMain

-- | The strip a bar sits in. A list bar sits one gap (see 'scrollBarGap')
-- inside its well's edge. A page bar sits a side gap inside the page's edge,
-- and a window body's bar a side gap inside the window's edge, out in the
-- window's padding. The gutter keeps the content one gap before each of them.
scrollChromeLane ::
  ScrollBarSlot -> DirTag -> Float -> Float -> Float -> Float -> Padding -> Rect
scrollChromeLane slot dir x y w h pad =
  let (barW, _) = scrollBarGeomFor slot
      -- From the scroller's edge in to the bar's far side. Window and modal
      -- bodies only scroll vertically, so the window's side padding is the
      -- one that places their bar.
      inset trailPad = case slot of
        ScrollBarList -> scrollBarGap trailPad
        ScrollBarPage -> scrollBarSideGap
        ScrollBarWindow -> scrollBarSideGap - padR windowPad
   in case dir of
        DirColumn ->
          Rect (max x (x + w - inset (padR pad) - barW)) (y + padT pad) barW (max 0 (h - padT pad - padB pad))
        DirRow ->
          Rect (x + padL pad) (max y (y + h - inset (padB pad) - barW)) (max 0 (w - padL pad - padR pad)) barW

-- | Track and thumb from slot, axis, x/y/width/height, padding, content extent,
-- and current offset. Lengths use logical pixels; 'Nothing' when the content
-- does not overflow or the track is too short for the smallest thumb.
scrollBarLayout ::
  ScrollBarSlot ->
  DirTag ->
  Float ->
  Float ->
  Float ->
  Float ->
  Padding ->
  Float ->
  Float ->
  Maybe ScrollBarLayout
scrollBarLayout slot dir x y w h pad = case dir of
  DirColumn -> scrollBarLayoutIn slot dir (Rect x y w h) pad (h - padT pad - padB pad) (padB pad)
  DirRow -> scrollBarLayoutIn slot dir (Rect x y w h) pad (w - padL pad - padR pad) (padR pad)

-- | Thumb offset along a track of @trackLen@ and clamped thumb length, from a
-- minimum thumb length, viewport numerator @num@, content denominator @den@,
-- scroll offset @off@, and range @maxOff@.
{-# INLINE scrollThumbSpan #-}
scrollThumbSpan :: Float -> Float -> Float -> Float -> Float -> Float -> (Float, Float)
scrollThumbSpan trackLen minThumb num den off maxOff =
  let len = max 1 trackLen
      size = clamp (min minThumb len) len (len * num / den)
      clamped = clamp 0 maxOff off
   in (if maxOff > 0 then (len - size) * clamped / maxOff else 0, size)

-- | 'scrollBarLayout' with an explicit visible main extent and the trailing
-- padding the content scrolls past. A native 2D scroller passes the padding
-- box minus the cross-axis lane (see 'scrollGutters2D'), so its reachable
-- range and thumb reflect the viewport that is actually visible rather than
-- the lane-underlapped padding box. On a one-dimensional scroller @viewMain@
-- is just the padding box on that axis. A text area's padding only places
-- its bars, so it passes no trailing padding.
scrollBarLayoutIn ::
  ScrollBarSlot
  -> DirTag
  -> Rect
  -> Padding
  -> Float
  -> Float
  -> Float
  -> Float
  -> Maybe ScrollBarLayout
scrollBarLayoutIn slot dir (Rect x y w h) pad viewMain trailing contentSize off =
  let
    (barW, barMargin) = scrollBarGeomFor slot
    minThumb = 16
    lane = scrollChromeLane slot dir x y w h pad
    origin = case dir of
      DirColumn -> y + padT pad
      DirRow -> x + padL pad
    maxOff = scrollAxisRange contentSize viewMain trailing
    trackStart = origin + barMargin
    trackSize = max 0 (viewMain - 2 * barMargin)
    (thumbRel, thumbSize) = scrollThumbSpan trackSize minThumb viewMain (contentSize + trailing) off maxOff
    thumbStart = trackStart + thumbRel
    band start size = case dir of
      DirColumn -> Rect (rectX lane) start barW size
      DirRow -> Rect start (rectY lane) size barW
   in
    if maxOff <= 0 || trackSize < minThumb
      then Nothing
      else
        Just
          (ScrollBarLayout (band trackStart trackSize) (band thumbStart thumbSize) maxOff)

-- | The vertical and the horizontal bar a scroll node placed at @x y w h@
-- shows at offsets @offX offY@ (a 1D scroller's offset is @offY@, whichever
-- way it runs). An axis whose chrome is suppressed shows none. A native 2D
-- scroller's axes each take their visible main extent from the viewport, which
-- the other axis's live gutter has narrowed, so the range and thumb are
-- computed against the viewport minus the opposite scrollbar lane.
scrollNodeBars ::
  ScrollNode -> Float -> Float -> Float -> Float -> Float -> Float -> (Maybe ScrollBarLayout, Maybe ScrollBarLayout)
scrollNodeBars sn@(ScrollNode slot cfg native2D dir pad contentMain contentW) x y w h offX offY
  | native2D =
      let Rect _ _ viewW viewH = scrollNodeViewport sn x y w h
       in ( shown DirColumn (scrollBarLayoutIn slot DirColumn (Rect x y w h) pad viewH (padB pad) contentMain offY)
          , shown DirRow (scrollBarLayoutIn slot DirRow (Rect x y w h) pad viewW (padR pad) contentW offX)
          )
  | dir == DirColumn = (shown dir bar, Nothing)
  | otherwise = (Nothing, shown dir bar)
  where
    bar = scrollBarLayout slot dir x y w h pad contentMain offY
    shown d l = if scrollChromeSuppressed cfg d then Nothing else l

onScrollBar :: V2 -> ScrollBarLayout -> Bool
onScrollBar mouse l = rectContains (sbThumb l) mouse || rectContains (sbTrack l) mouse

scrollOffsetFromThumb :: DirTag -> ScrollBarLayout -> Float -> V2 -> Float
scrollOffsetFromThumb dir layout grabOff mouse =
  let
    maxOff = sbMaxOff layout
    track = sbTrack layout
    thumb = sbThumb layout
    (trackStart, trackSize, thumbSize, pointer) = case dir of
      DirColumn -> (rectY track, rectH track, rectH thumb, v2Y mouse)
      DirRow -> (rectX track, rectW track, rectW thumb, v2X mouse)
    ratio = (pointer - grabOff - trackStart) / max 1 (trackSize - thumbSize)
   in
    clamp 0 maxOff (ratio * maxOff)

textClipSlop :: Float
textClipSlop = 4

tagClippedSpans :: Rect -> [(Rect, Text, Color, Color)] -> [(Rect, Text, Color, Color, Rect)]
tagClippedSpans clip =
  mapMaybe (\(rect, txt, fg, bg) -> (rect, txt, fg, bg,) <$> rectIntersect clip (padTextClipRect rect))

padTextClipRect :: Rect -> Rect
padTextClipRect (Rect x y w h) = Rect x y (w + textClipSlop) h

borderContentClip :: Style -> Rect -> Rect
borderContentClip style (Rect x y w h) =
  if styleBorderWidth style <= 0
    then Rect x y w h
    else
      let inset side = if styleHasSide side style then max 1 (styleBorderWidth style) else 0
       in padContentClip x y w h (Padding (inset SideLeft) (inset SideRight) (inset SideTop) (inset SideBottom))