packages feed

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

-- | View-level tween and spring hooks, continuous animation, and timed frame requests.
module NanoUI.Internal.Widgets.Animate
  ( Transition (..)
  , animate
  , animateTo
  , animateToA
  , pulse
  , keepAnimating
  , wakeAfter
  )
where

import Control.Monad (when)
import Data.Maybe (isNothing)
import NanoUI.Internal.Animatable (Animatable (..))
import NanoUI.Internal.Context
import NanoUI.Internal.Monad (NanoUI, nextId, scope, uiTime, withContext, withKey)
import NanoUI.Internal.Widgets.Node (HasResponse, respId)

-- | How an animated value moves.
data Transition
  = -- | Eased tween: duration and start delay, in seconds.
    Tween !Ease !Float !Float
  | -- | Damped spring; retargets from its current position and velocity.
    Spring !SpringParams
  deriving (Eq, Show)

-- | Animate from @from@ to @to@. It starts over from @from@ once it has
-- finished (a tween completes, a spring settles) or its tween changes, so
-- calling it every frame cycles. A frame that leaves it out stops it.
animate :: Transition -> Float -> Float -> NanoUI Float
animate transition from to = do
  wid <- nextId
  withContext $ \ctx -> do
    case transition of
      Tween ease dur delay -> startAnimationEaseDelay ctx wid from to dur ease delay
      Spring params -> do
        running <- lookupAnimation ctx wid
        when (isNothing running) (setAnimationValue ctx wid from)
        startSpring ctx wid params to
    getViewAnimationValue ctx wid

-- | Animate from the current value toward @target@. An unchanged target keeps
-- the running animation; a new one retargets from wherever the value is. A
-- frame that leaves it out stops it where it is.
animateTo :: Transition -> Float -> NanoUI Float
animateTo transition target = do
  wid <- nextId
  withContext $ \ctx -> do
    case transition of
      Tween ease dur delay -> do
        cur <- getAnimationValue ctx wid
        manim <- lookupAnimation ctx wid
        case manim of
          -- Still running: its key is not attached to a widget node, so request
          -- a full-frame repaint just as 'startAnimationEaseDelay' does.
          Just a | easeSameSpec a ease dur delay target -> repaintIfOrphan ctx (intKey wid)
          Nothing | approxEq cur target -> pure ()
          _ -> startAnimationEaseDelay ctx wid cur target dur ease delay
      Spring params -> startSpring ctx wid params target
    getViewAnimationValue ctx wid

-- | 'animateTo' for every component of a composite value. Component keys are
-- local to the value, not its parent widget.
animateToA :: Animatable a => Transition -> a -> NanoUI a
animateToA transition =
  scope . traverseChannels (\index value -> withKey index (animateTo transition value))

-- | A smoothly oscillating value in @[0,1]@ driven by the real-time clock, with
-- the given period in seconds (e.g. @pulse 6@ sweeps once every six seconds).
-- The time is captured in 'Double' (see 'NanoUI.uiTime'), so the sweep
-- stays sub-frame smooth even on long-running processes. Unlike 'animate', it
-- asks for no frames: pair it with 'keepAnimating' for continuous motion, or
-- 'wakeAfter' for stepped updates, or it changes only when something else
-- draws a frame.
pulse :: Float -> NanoUI Float
pulse periodSec = do
  t <- uiTime
  let
    period = max 0.001 (realToFrac periodSec :: Double)
  pure (realToFrac (0.5 + 0.5 * sin (2 * pi * t / period)) :: Float)

-- | Keep a widget animating for as long as this is called, so the frame loop
-- does not idle. Widgets driven by the wall clock ('pulse', or drawing from
-- 'NanoUI.uiTime') rather than by a frame-counted animation would
-- otherwise stop repainting once other animations settle. Call it every
-- frame the widget is shown: the loop goes back to sleep on the first frame
-- that leaves it out.
--
-- > bar <- progressBarWith' id 12 =<< pulse 6
-- > keepAnimating bar
keepAnimating :: HasResponse r => r -> NanoUI ()
keepAnimating resp = withContext (\ctx -> keepAnimationAlive ctx (respId resp))

-- | Ask for another frame after this many seconds, even if no input arrives.
-- The loop sleeps until then. Call it on every frame that still needs the
-- later one, as a clock label would to tick once a second:
--
-- > clock = do
-- >   label =<< currentTimeText
-- >   wakeAfter 1
--
-- Use it instead of 'keepAnimating' when a view changes on a schedule and not
-- continuously: 'keepAnimating' runs a frame for every display refresh.
wakeAfter :: Double -> NanoUI ()
wakeAfter sec = withContext (\ctx -> requestWakeAfter ctx sec)