packages feed

nano-ui-0.2.0.0: lib/NanoUI/Internal/Frame/TextInput.hs

-- | Single-line text fields: field geometry, horizontal scroll, caret and
-- selection painting, and mouse selection. Also holds the line painting, word
-- bounds and mouse selection the text area shares, and input-method
-- composition for both.
module NanoUI.Internal.Frame.TextInput
  ( nodeTextFieldGeom
  , TextInputFrame (..)
  , resolveTextInputFrame
  , FieldEdit
  , readFieldEdit
  , fieldEditLine
  , drawTextInputSelection
  , drawTextInputCaret
  , pushFieldText
  , fieldTextColor
  , textInputFg
  , nodeTextColor
  , drawLineSelection
  , drawLineCaret
  , drawCaretAt
  , drawLinePreedit
  , searchClearHit
  , textWordBounds
  , FieldDoc (..)
  , selectWithMouse
  , textInputMouse
    -- * Input-method composition
  , claimComposition
  , settleInputMethod
  , Preedit (..)
  , splicePreedit
  , preeditSourceIndex
  ) where

import Control.Monad (forM_, mfilter, when)
import Data.Char (isAlphaNum, isMark, isSpace)
import Data.IORef (readIORef, writeIORef)
import Data.Maybe (isJust)
import Data.Sequence qualified as Seq
import Data.Text (Text)
import qualified Data.Text as T
import Data.Word (Word64)
import NanoUI.Internal.Context
import NanoUI.Internal.Draw (DrawArena, pushPreparedTextStyled, pushRect)
import NanoUI.Internal.Font (FontMetrics (..), caretXIO, centeredTextY, lineWidthIO, prepareFontMetrics, revealSpanInViewport, selectionSpans, textDecorationMetrics, textIndexAtX, widgetContentInset)
import NanoUI.Internal.Frame.Chrome (maskPassword, textInputFocused, textInputValue)
import NanoUI.Internal.Frame.Hit (withWidgetNode)
import NanoUI.Internal.Frame.Node (nodeAdornmentInsets, nodeFontMetrics, resolveFontFor)
import NanoUI.Internal.Id (WidgetId (..), hashWidgetId)
import NanoUI.Internal.Input
import NanoUI.Internal.Layout.Arena (NodeIdx, NodeType (..), getNodeFontColor, getNodeFontSize, getNodeRect, getNodeType, getOptions, getStyleIdx, getWidgetId, nodeFontBits)
import NanoUI.Internal.Monad (ifM, unlessM, (<&&>))
import NanoUI.Internal.Store (fieldSelection, fieldSelectionWrite, fieldText, findSpec, insertSlot, insertSpec, setFieldSelection)
import NanoUI.Internal.Store.Slots (slotTextInputScroll)
import NanoUI.Internal.Style (FontStyle (..), FontWeight (..), themeSelection, toneColor)
import NanoUI.Internal.Types (Color (..), DamageBounds (..), Rect (..), V2 (..), clamp, lerpColor, rectContains, rectW)
import NanoUI.Internal.WidgetText
import NanoUI.Widgets.TextBuffer qualified as TB

textInputFieldRect :: FontMetrics -> Float -> Float -> Float -> Float -> Rect
textInputFieldRect fm x y w h =
  let fieldH = if h > 0 then h else textInputFieldHeight fm
   in Rect x y w fieldH

-- | Where plain field @idx@'s text goes in its box @field@: inside its own
-- font's content inset and clear of its adornments.
plainFieldTextClip :: Context -> NodeIdx -> Rect -> IO Rect
plainFieldTextClip ctx idx (Rect fx fy fw fh) = do
  fm <- nodeFontMetrics ctx idx
  (lead, trail) <- nodeAdornmentInsets (ctxNodeArena ctx) idx fx fw
  let (ix, _) = widgetContentInset fm
  pure (textClipBetween fm (max ix lead) (max ix trail) fx fy fw fh)

-- | Resolve the box a field paints/hits and the clip its text is confined to.
-- Search fields are caption-less: the whole node rect is the box and text is
-- clipped around the magnifier / clear chrome. Combo boxes (search fields
-- carrying dropdown options) clip to the left of the chevron instead. A plain
-- field clips its text short of its adornments.
nodeTextFieldGeom :: Context -> NodeIdx -> Float -> Float -> Float -> Float -> IO (Rect, Rect)
nodeTextFieldGeom ctx@Context {ctxFontMetrics = fm} idx x y w h = do
  si <- getStyleIdx (ctxNodeArena ctx) idx
  opts <- getOptions (ctxNodeArena ctx) idx
  let box = Rect x y w h
      field = textInputFieldRect fm x y w h
  if hasFlag textInputFlagSelectable si
    then pure (box, box)
    else if hasFlag textInputFlagNumeric si
      then pure (box, numericTextClip fm x y w h)
      else if hasFlag textInputFlagSearch si
        then pure (box, if null opts then searchInputTextClip fm x y w h else comboTextClip fm x y w h)
        else (field,) <$> plainFieldTextClip ctx idx field

-- | Whether the pointer is over the clear (×) button of a non-empty search
-- field. Search fields reserve that slot even when empty, but the button is
-- only active when there is text to clear.
searchClearHit :: Context -> WidgetId -> V2 -> IO Bool
searchClearHit ctx wid mouse = do
  withWidgetNode ctx wid False $ \idx -> do
    si <- getStyleIdx (ctxNodeArena ctx) idx
    opts <- getOptions (ctxNodeArena ctx) idx
    pure (hasFlag textInputFlagSearch si && null opts)
      <&&> (not . T.null <$> textInputValue ctx idx)
      <&&> do
        Rect x y w h <- getNodeRect (ctxNodeArena ctx) idx
        let (_, clearRect) = searchInputIconRects (ctxFontMetrics ctx) x y w h
        pure (rectContains clearRect mouse)

-- | Clear a search field. The debounced pulse picks the empty text up as an
-- immediate (empty) commit on the next frame, which the store write wakes.
clearSearchInput :: Context -> WidgetId -> IO ()
clearSearchInput ctx wid = do
  let key = intKey wid
  modifyStore ctx $
    insertSlot fieldText key "" . setFieldSelection key 0 0

-- | A field's or text area's text with its pen at @(x, y)@, in the weight,
-- slant and decoration packed in its style @si@, as a label draws them. The
-- weight and slant are synthesized unless the host drew the face natively
-- (@native@, from 'NanoUI.Internal.Frame.Node.resolveFontFor').
{-# INLINE pushFieldText #-}
pushFieldText :: DrawArena -> FontMetrics -> Bool -> Word64 -> Float -> Float -> Text -> Color -> IO ()
pushFieldText da fm native si x y txt col = do
  prepared <- prepareFontMetrics fm txt
  let !weight = if native then WeightNormal else textNodeFontWeight si
      !slant = if native then FontStyleNormal else textNodeFontStyle si
  pushPreparedTextStyled da prepared weight slant (textNodeTextDecoration si) x y txt col

-- | The colour of field or text area @idx@'s text: the layout's
-- 'fontColor', else its tone's ('fontTone', 'fontMuted' and the like), else
-- @fg@.
{-# INLINE fieldTextColor #-}
fieldTextColor :: Context -> NodeIdx -> Color -> IO Color
fieldTextColor ctx = nodeTextColor ctx NodeTextArea

-- | The colour of a text input's text ('fieldTextColor'), faded toward @bg@
-- while @value@ is empty and unfocused.
{-# INLINE textInputFg #-}
textInputFg :: Context -> NodeIdx -> Color -> Color -> Text -> Bool -> IO Color
textInputFg ctx idx fgCol bgCol value focus = do
  fg <- fieldTextColor ctx idx fgCol
  pure (if T.null value && not focus then lerpColor fg bgCol 0.40 else fg)

-- | 'fieldTextColor' for node @idx@ of type @nt@, whose tone sits in its
-- font bits: all of a text area's or field's style index, the label font of
-- a button or select ('nodeFontBits').
{-# INLINE nodeTextColor #-}
nodeTextColor :: Context -> NodeType -> NodeIdx -> Color -> IO Color
nodeTextColor ctx nt idx fg =
  getNodeFontColor (ctxNodeArena ctx) idx >>= \case
    Just c -> pure c
    Nothing -> do
      si <- getStyleIdx (ctxNodeArena ctx) idx
      let font = if nt == NodeTextArea then si else nodeFontBits nt si
      maybe (pure fg) (\t -> (`toneColor` t) <$> nodeTheme ctx idx) (textNodeFontTone font)

-- | The selection highlight behind characters @lo@ to @hi@ of @line@, whose
-- pen starts at @x@, on a row at @y@ of height @lineH@.
drawLineSelection :: DrawArena -> FontMetrics -> Text -> Int -> Int -> Float -> Float -> Float -> Color -> IO ()
drawLineSelection da fm line lo hi x y lineH color = do
  prepared <- prepareFontMetrics fm line
  forM_ (selectionSpans prepared line lo hi) $ \(wLo, wHi) ->
    when (wHi > wLo) $
      pushRect da (Rect (x + wLo) y (max 1 (wHi - wLo)) (max 4 lineH)) color

-- | The caret before character @col@ of @line@, whose pen starts at @x@, on a
-- row at @y@ of height @lineH@.
drawLineCaret :: DrawArena -> FontMetrics -> Text -> Int -> Float -> Float -> Float -> Color -> IO ()
drawLineCaret da fm line col x y lineH fg = do
  pw <- caretXIO fm line col
  drawCaretAt da (x + pw) y lineH fg

-- | A caret at @x@ on a row at @y@ of height @lineH@.
drawCaretAt :: DrawArena -> Float -> Float -> Float -> Color -> IO ()
drawCaretAt da x y lineH = pushRect da (Rect x (y + 1) 1 (max 4 (lineH - 2)))

-- | Underline the composition in 'preeditLine' (pen at @x@, row at @y@ of
-- height @lineH@) and draw the input method's caret when it has no
-- selection; a selection is drawn separately ('drawLineSelection'). The
-- underline follows bidi runs, so right-to-left compositions are underlined
-- where they are drawn.
drawLinePreedit :: DrawArena -> FontMetrics -> Preedit -> Float -> Float -> Float -> Color -> IO ()
drawLinePreedit da fm p x y lineH fg = do
  let line = preeditLine p
  prepared <- prepareFontMetrics fm line
  let (thick, underOff, _) = textDecorationMetrics lineH (fmAscent fm)
      underY = y + underOff
  forM_ (selectionSpans prepared line (preeditStart p) (preeditEnd p)) $ \(wLo, wHi) ->
    pushRect da (Rect (x + wLo) underY (max 1 (wHi - wLo)) thick) fg
  when (preeditSelectionEnd p == preeditCaret p) $
    drawLineCaret da fm line (preeditCaret p) x y lineH fg

-- | Sync horizontal scroll for a field whose font, edit line, and clip width
-- are already resolved. Writes the cache directly without scheduling an extra
-- frame.
syncTextInputScrollWith :: Context -> Int -> WidgetStore -> FontMetrics -> Bool -> Float -> Text -> Int -> IO Float
syncTextInputScrollWith ctx key store fm focus viewW shown cursor = do
  let oldScroll = findSpec slotTextInputScroll 0 key store
  newScroll <-
    if not focus || viewW <= 0
      then pure 0
      else do
        caretRelX <- caretXIO fm shown cursor
        totalTextW <- lineWidthIO fm shown
        pure (revealSpanInViewport oldScroll viewW (totalTextW + 1) caretRelX 1)
  when (newScroll /= oldScroll) $
    writeIORef (ctxStore ctx) $! insertSpec slotTextInputScroll key newScroll store
  pure newScroll

-- | The horizontal scroll that keeps field @idx@'s caret in view, zero while
-- it is unfocused, stored as it changes.
syncTextInputScroll :: Context -> NodeIdx -> Float -> Float -> Float -> Float -> IO Float
syncTextInputScroll ctx idx x y w h = do
  si <- getStyleIdx (ctxNodeArena ctx) idx
  if hasFlag textInputFlagSelectable si
    then pure 0
    else do
      wid <- getWidgetId (ctxNodeArena ctx) idx
      store <- getStore ctx
      focus <- textInputFocused ctx idx
      viewW <- if focus then rectW . snd <$> nodeTextFieldGeom ctx idx x y w h else pure 0
      if not focus || viewW <= 0
        then syncTextInputScrollWith ctx (intKey wid) store (ctxFontMetrics ctx) False 0 "" 0
        else do
          fm <- nodeFontMetrics ctx idx
          (shown, cursor, _, _) <- fieldEditLine ctx idx
          syncTextInputScrollWith ctx (intKey wid) store fm True viewW shown cursor

-- | Single-pass resolved state for painting a single-line text field.
data TextInputFrame = TextInputFrame
  { tifFont :: !FontMetrics
  , tifNative :: !Bool
  , tifValue :: !Text
  , tifShown :: !Text
  , tifPenX :: !Float
  , tifRowY :: !Float
  , tifEdit :: !(Maybe FieldEdit)
  }

-- | Resolve font, masked value, composition line, horizontal scroll, centered
-- row Y, and optional 'FieldEdit' in one pass for a captionless or plain field.
resolveTextInputFrame :: Context -> NodeIdx -> Word64 -> Bool -> Rect -> Rect -> IO TextInputFrame
resolveTextInputFrame ctx idx si focus (Rect _ boxY _ boxH) (Rect clipX _ clipW _) = do
  let na = ctxNodeArena ctx
  fontSizeVal <- getNodeFontSize na idx
  (fm, native, _) <- resolveFontFor ctx NodeTextInput fontSizeVal si
  wid <- getWidgetId na idx
  store <- getStore ctx
  value <- textInputValue ctx idx
  (shown, cursor, anchor, preedit) <- fieldEditLineWith ctx wid store si value
  scrollX <- syncTextInputScrollWith ctx (intKey wid) store fm focus clipW shown cursor
  let !penX = clipX - scrollX
      !rowY = centeredTextY fm boxY boxH (fmLineHeight fm)
      !edit = if focus then Just (FieldEdit shown cursor anchor fm rowY penX preedit) else Nothing
  pure (TextInputFrame fm native value shown penX rowY edit)

-- | What a focused single-line field paints its selection and caret from: the
-- displayed value, cursor and anchor, the node font, the top of its text row,
-- the x its text starts at with the scroll applied, and any composition.
data FieldEdit = FieldEdit !Text !Int !Int !FontMetrics !Float !Float !(Maybe Preedit)

-- | Editing state of field @idx@, whose box is @box@ ('nodeTextFieldGeom')
-- and whose text starts at @penX@, scroll applied, or Nothing while it is
-- unfocused.
readFieldEdit :: Context -> NodeIdx -> Rect -> Float -> IO (Maybe FieldEdit)
readFieldEdit ctx idx (Rect _ boxY _ boxH) penX = do
  focus <- textInputFocused ctx idx
  if not focus
    then pure Nothing
    else do
      (value, cursor, anchor, preedit) <- fieldEditLine ctx idx
      fm <- nodeFontMetrics ctx idx
      pure $! Just (FieldEdit value cursor anchor fm (centeredTextY fm boxY boxH (fmLineHeight fm)) penX preedit)

-- | Resolve the displayed edit line, cursor, anchor, and optional preedit from
-- pre-fetched field metadata (shared by 'resolveTextInputFrame' and 'fieldEditLine').
fieldEditLineWith :: Context -> WidgetId -> WidgetStore -> Word64 -> Text -> IO (Text, Int, Int, Maybe Preedit)
fieldEditLineWith ctx wid store si value = do
  -- Lazy so callers that only need the text skip the selection lookup.
  let (anchor, cursor) = fieldSelection store (intKey wid) value
  fieldComposition ctx wid >>= \case
    Nothing -> pure (value, cursor, anchor, Nothing)
    Just c ->
      -- Password fields mask the composition too.
      let shown
            | hasFlag textInputFlagPassword si = c {compositionText = maskPassword (compositionText c)}
            | otherwise = c
          p = splicePreedit shown value (min anchor cursor) (max anchor cursor)
       in pure (preeditLine p, preeditCaret p, preeditSelectionEnd p, Just p)

-- | The line field @idx@ displays ('textInputValue') with its cursor and
-- anchor. During composition ('fieldComposition') the composition replaces
-- the selection (which a commit will replace), and cursor and anchor are the
-- input method's caret and selection end.
fieldEditLine :: Context -> NodeIdx -> IO (Text, Int, Int, Maybe Preedit)
fieldEditLine ctx idx = do
  value <- textInputValue ctx idx
  wid <- getWidgetId (ctxNodeArena ctx) idx
  store <- getStore ctx
  si <- getStyleIdx (ctxNodeArena ctx) idx
  fieldEditLineWith ctx wid store si value

drawTextInputSelection :: DrawArena -> Context -> NodeIdx -> FieldEdit -> IO ()
drawTextInputSelection da ctx idx (FieldEdit value cursor anchor fm rowY textX _) =
  when (anchor /= cursor) $ do
    theme <- nodeTheme ctx idx
    drawLineSelection da fm value (min anchor cursor) (max anchor cursor) textX rowY (fmLineHeight fm) (themeSelection theme)

-- | The field's caret, or the composition underline and input-method caret
-- while composing ('drawLinePreedit').
drawTextInputCaret :: DrawArena -> FieldEdit -> Color -> IO ()
drawTextInputCaret da (FieldEdit value cursor _ fm rowY textX preedit) fg =
  case preedit of
    Just p -> drawLinePreedit da fm p textX rowY (fmLineHeight fm) fg
    Nothing -> drawLineCaret da fm value cursor textX rowY (fmLineHeight fm) fg

data CharClass = WordChar | SpaceChar | OtherChar
  deriving (Eq)

-- | The run of word characters (letters, digits, underscores, combining
-- marks and the zero-width joiners), spaces or other characters around
-- character @raw@ of @text@, clamped into it. Positions count characters,
-- not UTF-8 bytes.
textWordBounds :: Text -> Int -> (Int, Int)
textWordBounds text raw
  | T.null text = (0, 0)
  | otherwise =
      -- Split once: repeatedly indexing UTF-8 text makes long-word selection
      -- quadratic. The clamped index guarantees a non-empty suffix.
      let i = clamp 0 (T.length text - 1) raw
          (before, after) = T.splitAt i text
          sameClass = (== charClass (T.head after)) . charClass
       in ( i - T.length (T.takeWhileEnd sameClass before)
          , i + T.length (T.takeWhile sameClass after)
          )
 where
  charClass c
    | isAlphaNum c || isMark c || c == '_' || c == '\x200C' || c == '\x200D' = WordChar
    | isSpace c = SpaceChar
    | otherwise = OtherChar

-- | A field's document at the pointer: the position under it, the document,
-- and how the field stores a selection (anchor, then cursor).
data FieldDoc = FieldDoc !TB.Cursor !TB.TextBuffer (TB.Cursor -> TB.Cursor -> IO ())

-- | Mouse selection in focused field @wid@: a press in @box@, with word and
-- whole-document multi-clicks, and the drag it starts. On a press
-- @chromePress@ runs first and takes the press when it lands on the field's
-- own chrome. @atMouse@ reads the document at the pointer.
selectWithMouse :: Context -> Input -> WidgetId -> Rect -> IO Bool -> IO FieldDoc -> IO ()
selectWithMouse ctx inp wid box chromePress atMouse
  | pressedIn MouseLeft inp && rectContains box (inputMousePos inp) =
      unlessM chromePress $ do
        FieldDoc pos buf select <- atMouse
        -- A press counts as a multi-click only on the cell of the press before.
        prev <- getsInteraction ctx isTextFieldClickCell
        let cell = Just (TextFieldClickCell wid pos)
            clicks = if prev == cell then max 1 (inputMouseClicks inp) else 1
        uncurry select (dragSelection buf pos pos clicks)
        modifyInteraction ctx $ \s ->
          s {isTextFieldClickCell = cell, isTextInputDrag = Just (TextInputDrag wid pos clicks)}
  | heldIn MouseLeft inp || releasedIn MouseLeft inp =
      getsInteraction ctx isTextInputDrag >>= \case
        Just (TextInputDrag dragWid anchor clicks) | dragWid == wid -> do
          FieldDoc pos buf select <- atMouse
          uncurry select (dragSelection buf anchor pos clicks)
        _ -> pure ()
  | otherwise = pure ()

-- | The selection a drag from @anchor@ to @pos@ makes after @clicks@ clicks:
-- characters, whole words, or the whole document. A click is a drag that has
-- not moved. Words run from the start of the earlier word to the end of the
-- later one, which may be on different rows.
dragSelection :: TB.TextBuffer -> TB.Cursor -> TB.Cursor -> Int -> (TB.Cursor, TB.Cursor)
dragSelection buf anchor@(TB.Cursor ar ac) pos@(TB.Cursor r c) clicks
  | clicks >= 3 = (TB.Cursor 0 0, TB.documentEnd buf)
  | clicks == 2 =
      let (a0, a1) = textWordBounds (TB.lineAt ar buf) ac
          (c0, c1) = textWordBounds (TB.lineAt r buf) c
       in if pos < anchor
            then (TB.Cursor ar a1, TB.Cursor r c0)
            else (TB.Cursor ar a0, TB.Cursor r c1)
  | otherwise = (anchor, pos)

-- | Mouse selection in single-line field @wid@ at @idx@ ('selectWithMouse'),
-- and a search field's clear button. A press on a control drawn inside the
-- field (@onControl@) is the control's, and leaves the caret alone.
textInputMouse :: Context -> Input -> Bool -> WidgetId -> NodeIdx -> IO ()
textInputMouse ctx inp onControl wid idx = do
  Rect x y w h <- getNodeRect (ctxNodeArena ctx) idx
  (box, Rect clipX _ _ _) <- nodeTextFieldGeom ctx idx x y w h
  scrollX <- syncTextInputScroll ctx idx x y w h
  let mouse@(V2 mouseX _) = inputMousePos inp
      key = intKey wid
      clearPress = ifM (searchClearHit ctx wid mouse) (True <$ clearSearchInput ctx wid) (pure False)
  selectWithMouse ctx inp wid box (if onControl then pure True else clearPress) $ do
    fm <- nodeFontMetrics ctx idx
    value <- textInputValue ctx idx
    -- Hit-test the displayed text, then map back to a position in the value.
    (shown, _, _, preedit) <- fieldEditLine ctx idx
    prepared <- prepareFontMetrics fm shown
    let atX = textIndexAtX prepared shown (max 0 (mouseX - (clipX - scrollX)))
        pos = TB.Cursor 0 (maybe atX (`preeditSourceIndex` atX) preedit)
    pure $ FieldDoc pos (TB.fromLines (Seq.singleton value)) $ \(TB.Cursor _ anchor) (TB.Cursor _ cursor) ->
      writeSlots ctx (fieldSelectionWrite key value anchor cursor)

-- | Before the view runs, decide which widget shows the frame's composition
-- ('inputComposition'). The owner must be focused and must have requested
-- the input method last view ('requestInputMethod'). A composition keeps its
-- owner while unchanged; if focus leaves, it shows nowhere until the input
-- method updates it, so it never jumps to the next widget. Damages the old
-- and new owners.
--
-- Returns the frame's input and whether the input method owns the focused
-- widget's keys ('FocusComposing'). While a composition shows, all key
-- state is dropped so nothing edits the field or fires a shortcut; the
-- commit still arrives as typed text. So it is on the frame a composition
-- ends with nothing committed, whose key the input method took (an Escape
-- that cancels it, a Backspace on its last character). On the commit frame,
-- keys the input method passes on (an arrow after a Korean syllable) reach
-- the field but fire no shortcut.
--
-- Inlined into its single caller so the result pair is never allocated.
{-# INLINE claimComposition #-}
claimComposition :: Context -> Input -> IO (Input, Bool)
claimComposition ctx !inp = do
  focus <- readIORef (ctxFocusId ctx)
  held <- getsInteraction ctx isComposition
  taker <- maybe (WidgetId 0) imrWidget <$> readIORef (ctxInputMethod ctx)
  let new = mfilter (not . T.null . compositionText) (inputComposition inp)
      -- An unchanged composition keeps its owner; a new one goes to focus.
      claimant = case held of
        Just (old, o) | Just old == new -> if o == focus then o else WidgetId 0
        _ -> focus
      showing = maybe False (\(_, o) -> o == focus && hashWidgetId o /= 0)
      !owner = if isJust new && claimant == taker then claimant else WidgetId 0
      next = (,owner) <$> new
  when (next /= held) $ do
    modifyInteraction ctx (\s -> s {isComposition = next})
    mapM_ (\(_, old) -> damageWidget ctx old DamageSelf) held
    damageWidget ctx owner DamageSelf
  let !ended = showing held && not (showing next)
      !committing = ended && not (T.null (inputChars inp))
  pure $
    if showing next || (ended && not committing)
      then (inp {inputKeys = mempty, inputKeysNew = mempty, inputKeysReleased = mempty, inputKeysHeld = mempty}, True)
      else (inp, committing)

-- | At the end of a frame, move the input-method request to follow focus if
-- focus left the requesting widget ('requestInputMethod'), or left @before@
-- when nobody asked. The newly focused text field gets a request (or none
-- if it is not a field) until it asks for itself next view, so the backend
-- routes text and the next composition to it from this frame on.
settleInputMethod :: Context -> WidgetId -> IO ()
settleInputMethod ctx@Context {ctxNodeArena = na} before = do
  focus <- readIORef (ctxFocusId ctx)
  asked <- readIORef (ctxInputMethod ctx)
  when (maybe (focus /= before) ((/= focus) . imrWidget) asked) $ do
    let request purpose = Just (InputMethodRequest focus Nothing purpose)
        fieldRequest si
          | hasFlag textInputFlagSelectable si = Nothing
          | hasFlag textInputFlagPassword si = request InputSecure
          | hasFlag textInputFlagNumeric si = request InputNumeric
          | otherwise = request InputNormal
    field <-
      ifM (isDisabled ctx focus) (pure Nothing) . withWidgetNode ctx focus Nothing $ \idx ->
        getNodeType na idx >>= \case
          NodeTextArea -> pure (request InputNormal)
          NodeTextInput -> fieldRequest <$> getStyleIdx na idx
          _ -> pure Nothing
    writeIORef (ctxInputMethod ctx) field

-- | A line with a composition spliced in, as shown while composing.
-- Positions are character indices into 'preeditLine'.
data Preedit = Preedit
  { preeditLine :: !Text
  , preeditStart :: !Int
  , preeditEnd :: !Int
  -- ^ Composition span.
  , preeditCaret :: !Int
  , preeditSelectionEnd :: !Int
  -- ^ Input-method caret; the selection runs from the caret to here.
  , preeditReplaced :: !Int
  -- ^ Number of original characters the composition replaces.
  }

-- | Splice a composition into @line@ over characters @lo@ to @hi@ (the
-- selection, or an empty span at the caret). Positions are clamped.
splicePreedit :: Composition -> Text -> Int -> Int -> Preedit
splicePreedit (Composition txt cursor sel) line lo0 hi0 =
  let n = T.length line
      lo = clamp 0 n lo0
      hi = clamp lo n hi0
      len = T.length txt
      caret = lo + clamp 0 len cursor
   in Preedit
        { preeditLine = T.take lo line <> txt <> T.drop hi line
        , preeditStart = lo
        , preeditEnd = lo + len
        , preeditCaret = caret
        , preeditSelectionEnd = min (lo + len) (caret + max 0 sel)
        , preeditReplaced = hi - lo
        }

-- | Map a position in the displayed line back to the original line.
-- Positions inside the composition map to its start.
preeditSourceIndex :: Preedit -> Int -> Int
preeditSourceIndex p i
  | i <= preeditStart p = i
  | i >= preeditEnd p = i - (preeditEnd p - preeditStart p) + preeditReplaced p
  | otherwise = preeditStart p