Skip to content

Commit b8ba802

Browse files
migrate BoxLabel to use TextINputState stuff
1 parent c91bfad commit b8ba802

File tree

1 file changed

+29
-60
lines changed

1 file changed

+29
-60
lines changed

src/Potato/Flow/Controller/Manipulator/BoxText.hs

+29-60
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ import qualified Potato.Data.Text.Zipper as TZ
3939
import qualified Text.Pretty.Simple as Pretty
4040
import qualified Data.Text.Lazy as LT
4141

42+
data BoxTextHandler = BoxTextHandler {
43+
-- TODO Delete this
44+
_boxTextHandler_isActive :: Bool
45+
46+
, _boxTextHandler_state :: TextInputState
47+
-- TODO you can prob delete this now, we don't persist state between sub handlers in this case
48+
, _boxTextHandler_prevHandler :: SomePotatoHandler
49+
, _boxTextHandler_undoFirst :: Bool
50+
51+
, _boxTextHandler_commitOnMouseUp :: Bool
52+
}
4253

4354
getSBox :: CanvasSelection -> (REltId, SBox)
4455
getSBox selection = case superOwl_toSElt_hack sowl of
@@ -98,18 +109,6 @@ makeTextInputState = makeOwlItemTextInputState boxTextImpl
98109
inputBoxText :: TextInputState -> SuperOwl -> KeyboardKey -> (TextInputState, Maybe Llama)
99110
inputBoxText tais sowl kk = inputOwlItem boxTextImpl tais sowl kk
100111

101-
data BoxTextHandler = BoxTextHandler {
102-
-- TODO Delete this
103-
_boxTextHandler_isActive :: Bool
104-
105-
, _boxTextHandler_state :: TextInputState
106-
-- TODO you can prob delete this now, we don't persist state between sub handlers in this case
107-
, _boxTextHandler_prevHandler :: SomePotatoHandler
108-
, _boxTextHandler_undoFirst :: Bool
109-
110-
, _boxTextHandler_commitOnMouseUp :: Bool
111-
}
112-
113112
makeBoxTextHandler :: Bool -> SomePotatoHandler -> CanvasSelection -> RelMouseDrag -> BoxTextHandler
114113
makeBoxTextHandler commit prev selection rmd = BoxTextHandler {
115114
_boxTextHandler_isActive = False
@@ -250,78 +249,48 @@ lBox_to_boxLabelBox lbx = r where
250249
width = max 0 (w - 2)
251250
r = LBox (V2 (x+1) y) (V2 width 1)
252251

252+
makeBoxLableController :: Text -> Text -> Controller
253+
makeBoxLableController orig new = CTagBoxLabelText :=> (Identity $ CMaybeText (DeltaMaybeText (if orig == "" then Nothing else Just orig, if new == "" then Nothing else Just new)))
253254

254-
255+
boxLabelImpl :: TextImpl SBox
256+
boxLabelImpl = TextImpl {
257+
_textImpl_mustGetOwlItem = getSBox
258+
, _textImpl_owlItemText = fromMaybe "" . _sBoxTitle_title . _sBox_title
259+
, _textImpl_owlItemBox = canonicalLBox_from_lBox . lBox_to_boxLabelBox . _sBox_box
260+
, _textImpl_owlItemAlignment = _sBoxTitle_align . _sBox_title
261+
, _textImpl_inputOwlItemZipper = inputSingleLineZipper
262+
, _textImpl_makeController = makeBoxLableController
263+
}
255264

256265
updateBoxLabelInputStateWithSBox :: SBox -> TextInputState -> TextInputState
257-
updateBoxLabelInputStateWithSBox sbox btis = r where
258-
alignment = convertTextAlignToTextZipperTextAlignment . _sBoxTitle_align . _sBox_title $ sbox
259-
newBox = lBox_to_boxLabelBox $ _sBox_box sbox
260-
width = maxBound :: Int -- box label text always overflows
261-
r = btis {
262-
_textInputState_box = newBox
263-
, _textInputState_displayLines = TZ.displayLinesWithAlignment alignment width () () (_textInputState_zipper btis)
264-
}
266+
updateBoxLabelInputStateWithSBox = updateTextInputStateWithOwlItem boxLabelImpl
265267

266268
makeBoxLabelInputState :: REltId -> SBox -> RelMouseDrag -> TextInputState
267-
makeBoxLabelInputState rid sbox rmd = r where
268-
mogtext = _sBoxTitle_title . _sBox_title $ sbox
269-
ogtz = TZ.fromText (fromMaybe "" mogtext)
270-
r' = TextInputState {
271-
_textInputState_rid = rid
272-
, _textInputState_original = mogtext
273-
, _textInputState_zipper = ogtz
274-
275-
-- these fields get updated in next pass
276-
, _textInputState_box = error "expected to be filled"
277-
, _textInputState_displayLines = error "expected to be filled"
278-
}
279-
r'' = updateBoxLabelInputStateWithSBox sbox r'
280-
r = mouseText r'' rmd
269+
makeBoxLabelInputState = makeOwlItemTextInputState boxLabelImpl
281270

282271
makeBoxLabelHandler :: SomePotatoHandler -> CanvasSelection -> RelMouseDrag -> BoxLabelHandler
283272
makeBoxLabelHandler prev selection rmd = BoxLabelHandler {
284273
_boxLabelHandler_active = False
285-
, _boxLabelHandler_state = uncurry makeBoxLabelInputState (getSBox selection) rmd
274+
, _boxLabelHandler_state = uncurry makeBoxLabelInputState (_textImpl_mustGetOwlItem boxLabelImpl selection) rmd
286275
, _boxLabelHandler_prevHandler = prev
287276
, _boxLabelHandler_undoFirst = False
288277
}
289278

290279

291280
-- UNTESTED
292281
updateBoxLabelHandlerState :: Bool -> CanvasSelection -> BoxLabelHandler -> BoxLabelHandler
293-
updateBoxLabelHandlerState reset selection tah@BoxLabelHandler {..} = assert tzIsCorrect r where
294-
(_, sbox) = getSBox selection
295-
296-
mNewText = _sBoxTitle_title . _sBox_title $ sbox
297-
298-
recomputetz = TZ.fromText (fromMaybe "" mNewText)
299-
oldtz = _textInputState_zipper _boxLabelHandler_state
300-
-- NOTE that recomputetz won't have the same cursor position
301-
-- TODO delete this check, not very meaningful, but good for development purposes I guess
302-
tzIsCorrect = TZ.value oldtz == TZ.value recomputetz
303-
nextstate = updateBoxLabelInputStateWithSBox sbox _boxLabelHandler_state
304-
282+
updateBoxLabelHandlerState reset selection tah@BoxLabelHandler {..} = r where
283+
nextstate = updateOwlItemTextInputState boxLabelImpl reset selection _boxLabelHandler_state
305284
r = tah {
306-
_boxLabelHandler_state = if reset
307-
then nextstate {
308-
_textInputState_original = mNewText
309-
}
310-
else nextstate
285+
_boxLabelHandler_state = nextstate
311286
, _boxLabelHandler_undoFirst = if reset
312287
then False
313288
else _boxLabelHandler_undoFirst
314289
}
315290

316291

317292
inputBoxLabel :: TextInputState -> SuperOwl -> KeyboardKey -> (TextInputState, Maybe Llama)
318-
inputBoxLabel tais sowl kk = (newtais, mop) where
319-
(changed, newtais) = inputSingleLineZipper tais kk
320-
newtext = TZ.value (_textInputState_zipper newtais)
321-
controller = CTagBoxLabelText :=> (Identity $ CMaybeText (DeltaMaybeText (_textInputState_original tais, if newtext == "" then Nothing else Just newtext)))
322-
mop = if changed
323-
then Just $ makePFCLlama . OwlPFCManipulate $ IM.fromList [(_superOwl_id sowl,controller)]
324-
else Nothing
293+
inputBoxLabel tais sowl kk = inputOwlItem boxLabelImpl tais sowl kk
325294

326295

327296
-- | just a helper for pHandleMouse

0 commit comments

Comments
 (0)