Add support for cursor position change on mouse click

This commit is contained in:
Francisco Vallarino 2020-09-07 01:40:19 -03:00
parent 2992a7f12e
commit 9fa3fbb233
2 changed files with 25 additions and 6 deletions

View File

@ -13,7 +13,7 @@ import Control.Monad
import Control.Lens (ALens', (&), (.~), (^.), (^?))
import Data.Default
import Data.Maybe
import Data.Sequence (Seq)
import Data.Sequence (Seq, (|>))
import Data.Text (Text)
import Data.Typeable
@ -168,8 +168,25 @@ makeTextField config state = widget where
| otherwise = idx
handleEvent wenv target evt inst = case evt of
Click (Point x y) _ -> Just $ resultReqs reqs inst where
reqs = [SetFocus $ _wiPath inst]
Click (Point x y) _ -> result where
style = activeStyle wenv inst
rect = getContentRect style inst
localX = x - _rX rect + _tfOffset state
textLen = glyphsLength (_tfGlyphs state)
glyphs = _tfGlyphs state |> GlyphPos textLen 0 0
zipper i g = (i, abs (_glpXMin g - localX))
idxs = Seq.fromList [0..length glyphs]
pairs = Seq.zipWith zipper idxs glyphs
cpm (_, g1) (_, g2) = compare g1 g2
diffs = Seq.sortBy cpm pairs
newPos = maybe 0 fst (Seq.lookup 0 diffs)
newState = newTextState wenv inst state currText newPos Nothing
newInst = inst {
_wiWidget = makeTextField config newState
}
result
| isFocused wenv inst = Just $ resultWidget newInst
| otherwise = Just $ resultReqs [SetFocus $ _wiPath inst] inst
KeyAction mod code KeyPressed -> Just $ resultReqs reqs newInstance where
(newText, newPos, newSel) = handleKeyPress wenv mod code

View File

@ -159,12 +159,14 @@
- Instead of passing Point, pass WidgetQuery ADT. Currently it would support... PointQuery
- Do we need this?
- It is implemented in chore/unify-query. I honestly don't think it's an improvement
- Pending
- Further textField improvements
- Handle long text and cursor position
- Scissor needed?
- Add regex support for validation
- Add support for changing cursor position with mouse click
- Pending
- Further textField improvements
- Add support for validation
- Rethink Image handling
- Should image component keep a copy around?
- Should it be provided to renderer every time and, if removed, put back in the list?