nano-ui-0.2.0.0: lib/NanoUI/Internal/Widgets/Drop.hs
{-# LANGUAGE StrictData #-}
-- | Local pointer drags and operating-system drag and drop.
--
-- 'useDrop' turns the frame's 'NanoUI.Internal.Input.DropEvent's into a 'DropTarget'
-- for one rectangle. 'dropZone' wraps a panel and does the same for its rect.
--
-- > (_, _, target) <- dropZone fillW (label "Drop files here")
-- > when (dropReceived target) (mapM_ openFile (dropFiles target))
module NanoUI.Internal.Widgets.Drop
( DropTarget (..)
, useDrop
, dropZone
, Drag (..)
, DragPhase (..)
, DragHandle
, newDrag
, useDrag
, insertionIndex
) where
import Control.Applicative ((<|>))
import Control.Monad (when)
import Data.List (find)
import Data.Maybe (fromMaybe, isJust)
import Data.Text (Text)
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import NanoUI.Internal.Context
import NanoUI.Internal.Input
import NanoUI.Internal.Monad (NanoUI, askInput, freshWidget, liftIO, takeEscape, withContext)
import NanoUI.Internal.Store (deleteSpec, findSpec, insertSpec, lookupSpec)
import NanoUI.Internal.Store.Slots (setFlagSpec, slotDrop, slotDropPos)
import NanoUI.Internal.Style (Layout)
import NanoUI.Internal.Types (Rect (..), V2 (..), rectContains, rectIntersect)
import NanoUI.Internal.Widgets.Layout (panelWith')
import NanoUI.Internal.Widgets.Node (Response, respId, respRect, respPressed)
import NanoUI.Internal.Widgets.Behavior (DragAxis (..), pastDragThreshold)
-- | A thresholded pointer gesture. Terminal phases last one frame.
data DragPhase = DragStarted | Dragging | DragReleased | DragCancelled
deriving (Eq, Show)
-- | Application-owned payload and window coordinates. Map the payload to
-- adapt one gesture to another target without restarting its lifecycle.
data Drag a = Drag
{ dragPayload :: !a
, dragAt :: !V2
-- ^ Where the pointer is.
, dragFrom :: !V2
-- ^ Where the press was.
, dragPhase :: !DragPhase
}
deriving (Eq, Show, Functor)
data DragState a = DragState !a !V2 !Bool
deriving (Eq)
-- | Component-owned gesture state, allocated once before rendering. Use on
-- the UI thread in one session; each independent gesture needs its own handle.
newtype DragHandle a = DragHandle (IORef (Maybe (DragState a)))
-- | A handle for 'useDrag'. Allocate it once during setup, outside the view:
-- a handle made every frame never tracks a drag.
newDrag :: IO (DragHandle a)
newDrag = DragHandle <$> newIORef Nothing
-- | Track a drag across uniquely keyed responses. Call with the same handle
-- every frame, including for an empty collection. Only owned presses start;
-- payload identity survives reordering. Below the threshold, returns Nothing.
-- Escape, lost hold or source removal cancels; only DragReleased may commit.
useDrag :: Eq a => DragHandle a -> [(a, Response)] -> NanoUI (Maybe (Drag a))
useDrag (DragHandle ref) sources = do
inp <- askInput
let pos = inputMousePos inp
old <- liftIO (readIORef ref)
let armed = if pressedIn MouseLeft inp
then (\(a, _) -> DragState a pos False) <$> find (respPressed . snd) sources
else old
escaped <- if isJust armed then takeEscape else pure False
let step (DragState a origin hot) =
let cancelled = escaped || all ((/= a) . fst) sources
|| (not (heldIn MouseLeft inp) && not (releasedIn MouseLeft inp))
moved = hot || pastDragThreshold origin pos
released = releasedIn MouseLeft inp
phase | cancelled = DragCancelled
| released = DragReleased
| not hot = DragStarted
| otherwise = Dragging
emitted = if hot || (moved && not cancelled) then Just (Drag a pos origin phase) else Nothing
retained = if cancelled || released then Nothing else Just (DragState a origin moved)
in (retained, emitted)
(next, result) = maybe (Nothing, Nothing) step armed
when (next /= old) $ withContext $ \ctx -> do
writeIORef ref next
-- Only repaint when a drag actually became active or ended; arming and
-- releasing on a plain click below threshold needs no full-window damage.
when (isJust result || maybe False (\(DragState _ _ h) -> h) old) $ do
damageFull ctx
markDirty ctx
pure result
-- | Insertion slot in an ordered row or column, bounded by its visible
-- viewport. Pass remaining items (omit the source for a same-list move).
-- Empty targets accept slot zero. Rectangles may extend beyond the viewport
-- when scrolled; their centers still determine the original list index.
insertionIndex :: DragAxis -> Rect -> [Rect] -> V2 -> Maybe Int
insertionIndex axis bounds items pos@(V2 x y)
| not (rectContains bounds pos) = Nothing
| otherwise = Just (length (takeWhile before items))
where
before (Rect rx ry rw rh) = case axis of
DragAxisX -> x > rx + rw / 2
DragAxisY -> y > ry + rh / 2
-- | Per-frame drop state for a single rectangular drop target.
data DropTarget = DropTarget
{ dropHovered :: !Bool
-- ^ A drag is currently positioned over the target rect.
, dropReceived :: !Bool
-- ^ One or more payloads landed on the target this frame.
, dropFiles :: ![Text]
-- ^ File paths dropped on the target this frame.
, dropTexts :: ![Text]
-- ^ Text snippets dropped on the target this frame.
, dropPosition :: !(Maybe V2)
-- ^ Last known drop position in window coordinates, if any.
}
deriving (Eq, Show)
-- | The drop state for a rectangle from this frame's drop events. Hover
-- persists while the OS drag is still; payloads are reported on the frame
-- they arrive. A payload lands at the last 'DropPosition', not its own
-- coordinates, which SDL reports as (0,0) when it has none. Only the layer
-- on top where the drag is sees it there: a target behind a modal, or under
-- a floating window, sees it off the window.
useDrop :: Rect -> NanoUI DropTarget
useDrop bounds = do
(wid, ctx) <- freshWidget
inp <- askInput
let key = intKey wid
store <- liftIO (getStore ctx)
let active0 = findSpec slotDrop False key store
lastPos0 = lookupSpec slotDropPos key store
-- A drag is active from 'DropBegin' until 'DropComplete'. A payload
-- uses the position current at its point in the sequence.
step (active, pos, fs, ts) ev = case dropEventType ev of
DropBegin -> (True, pos, fs, ts)
DropPosition -> (active, dropEventPos ev <|> pos, fs, ts)
DropComplete -> (False, pos, fs, ts)
DropFile | posInside bounds pos -> (active, pos, dropEventData ev : fs, ts)
DropText | posInside bounds pos -> (active, pos, fs, dropEventData ev : ts)
_ -> (active, pos, fs, ts)
(active1, lastPos1, filesRev, textsRev) =
foldl' step (active0, lastPos0, [], []) (inputDrops inp)
files = reverse filesRev
texts = reverse textsRev
hovered = active1 && posInside bounds lastPos1
-- The payloads and 'DropComplete' arrive in one frame, which still
-- reports where they landed; only an active drag keeps the position.
stored1 = if active1 then lastPos1 else Nothing
when (active1 /= active0 || stored1 /= lastPos0) $
liftIO . modifyStore ctx $
setFlagSpec slotDrop key active1
. maybe (deleteSpec slotDropPos key) (insertSpec slotDropPos key) stored1
pure
DropTarget
{ dropHovered = hovered
, dropReceived = not (null files) || not (null texts)
, dropFiles = files
, dropTexts = texts
, dropPosition = lastPos1
}
posInside :: Rect -> Maybe V2 -> Bool
posInside bounds = maybe False (rectContains bounds)
-- | A panel that is also a drop target. Returns the body's result, the
-- panel's 'Response', and the 'DropTarget' for its rect.
dropZone :: (Layout -> Layout) -> NanoUI a -> NanoUI (a, Response, DropTarget)
dropZone f child = do
(a, resp) <- panelWith' f child
mClip <- withContext (`getPrevClipRect` respId resp)
let dropBounds = maybe (respRect resp) (fromMaybe (Rect 0 0 0 0) . rectIntersect (respRect resp)) mClip
(a, resp,) <$> useDrop dropBounds