Skip to content

Commit c91bfad

Browse files
Refactor BoxText to use new functionality in TextINputState
1 parent ae41cad commit c91bfad

File tree

1 file changed

+26
-78
lines changed

1 file changed

+26
-78
lines changed

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

+26-78
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ 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+
4243
getSBox :: CanvasSelection -> (REltId, SBox)
4344
getSBox selection = case superOwl_toSElt_hack sowl of
4445
SEltBox sbox -> (rid, sbox)
@@ -72,70 +73,30 @@ getSBoxTextBox sbox = r where
7273
else box'
7374

7475

75-
updateTextInputStateWithSBox :: SBox -> TextInputState -> TextInputState
76-
updateTextInputStateWithSBox sbox btis = r where
77-
alignment = convertTextAlignToTextZipperTextAlignment . _textStyle_alignment . _sBoxText_style . _sBox_text $ sbox
78-
CanonicalLBox _ _ newBox@(LBox _ (V2 width _)) = getSBoxTextBox sbox
79-
r = btis {
80-
_textInputState_box = newBox
81-
, _textInputState_displayLines = TZ.displayLinesWithAlignment alignment width () () (_textInputState_zipper btis)
82-
}
83-
84-
-- TODO I think you need to pad empty lines in the zipper to fill out the box D:
85-
-- ok, no you don't, that's only for the non-paragraph text area that we don't actually have yet
86-
makeTextInputState :: REltId -> SBox -> RelMouseDrag -> TextInputState
87-
makeTextInputState rid sbox rmd = r where
88-
ogtext = _sBoxText_text . _sBox_text $ sbox
89-
ogtz = TZ.fromText ogtext
90-
r' = TextInputState {
91-
_textInputState_rid = rid
92-
, _textInputState_original = Just ogtext
93-
, _textInputState_zipper = ogtz
76+
makeBoxTextController :: Text -> Text -> Controller
77+
makeBoxTextController orig new = CTagBoxText :=> (Identity $ CBoxText {
78+
_cBoxText_deltaText = (orig, new)
79+
})
9480

95-
-- these fields get updated in next pass
96-
, _textInputState_box = error "expected to be filled"
97-
, _textInputState_displayLines = error "expected to be filled"
81+
boxTextImpl :: TextImpl SBox
82+
boxTextImpl = TextImpl {
83+
_textImpl_mustGetOwlItem = getSBox
84+
--, _textImpl_updateTextInputStateWithOwlItem = updateTextInputStateWithSBox
85+
, _textImpl_owlItemText = _sBoxText_text . _sBox_text
86+
, _textImpl_owlItemBox = getSBoxTextBox
87+
, _textImpl_owlItemAlignment = _textStyle_alignment . _sBoxText_style . _sBox_text
88+
, _textImpl_inputOwlItemZipper = inputBoxTextZipper
89+
, _textImpl_makeController = makeBoxTextController
90+
}
9891

99-
--, _textInputState_selected = 0
100-
}
101-
r'' = updateTextInputStateWithSBox sbox r'
102-
r = mouseText r'' rmd
92+
updateTextInputStateWithSBox :: SBox -> TextInputState -> TextInputState
93+
updateTextInputStateWithSBox = updateTextInputStateWithOwlItem boxTextImpl
10394

104-
-- TODO support shift selecting text someday meh
105-
-- | returns zipper in TextInputState after keyboard input has been applied
106-
-- Bool indicates if there was any real input
107-
inputBoxTextZipper :: TextInputState -> KeyboardKey -> (Bool, TextInputState)
108-
inputBoxTextZipper tais kk = (changed, tais { _textInputState_zipper = newZip }) where
109-
110-
oldZip = _textInputState_zipper tais
111-
(changed, newZip) = case kk of
112-
KeyboardKey_Left -> (False, TZ.left oldZip)
113-
KeyboardKey_Right -> (False, TZ.right oldZip)
114-
KeyboardKey_Up -> (False, TZ.up oldZip)
115-
KeyboardKey_Down -> (False, TZ.down oldZip)
116-
KeyboardKey_Home -> (False, TZ.home oldZip)
117-
KeyboardKey_End -> (False, TZ.end oldZip)
118-
KeyboardKey_PageUp -> (False, TZ.pageUp 5 oldZip)
119-
KeyboardKey_PageDown -> (False, TZ.pageDown 5 oldZip)
120-
121-
KeyboardKey_Return -> (True, TZ.insertChar '\n' oldZip)
122-
KeyboardKey_Space -> (True, TZ.insertChar ' ' oldZip)
123-
KeyboardKey_Delete -> (True, TZ.deleteRight oldZip)
124-
KeyboardKey_Backspace -> (True, TZ.deleteLeft oldZip)
125-
KeyboardKey_Char c -> (True, TZ.insertChar c oldZip)
126-
KeyboardKey_Paste t -> (True, TZ.insert t oldZip)
127-
128-
k -> error $ "unexpected keyboard char (event should have been handled outside of this handler)" <> show k
95+
makeTextInputState :: REltId -> SBox -> RelMouseDrag -> TextInputState
96+
makeTextInputState = makeOwlItemTextInputState boxTextImpl
12997

13098
inputBoxText :: TextInputState -> SuperOwl -> KeyboardKey -> (TextInputState, Maybe Llama)
131-
inputBoxText tais sowl kk = (newtais, mop) where
132-
(changed, newtais) = inputBoxTextZipper tais kk
133-
controller = CTagBoxText :=> (Identity $ CBoxText {
134-
_cBoxText_deltaText = (fromMaybe "" (_textInputState_original tais), TZ.value (_textInputState_zipper newtais))
135-
})
136-
mop = if changed
137-
then Just $ makePFCLlama . OwlPFCManipulate $ IM.fromList [(_superOwl_id sowl,controller)]
138-
else Nothing
99+
inputBoxText tais sowl kk = inputOwlItem boxTextImpl tais sowl kk
139100

140101
data BoxTextHandler = BoxTextHandler {
141102
-- TODO Delete this
@@ -152,32 +113,17 @@ data BoxTextHandler = BoxTextHandler {
152113
makeBoxTextHandler :: Bool -> SomePotatoHandler -> CanvasSelection -> RelMouseDrag -> BoxTextHandler
153114
makeBoxTextHandler commit prev selection rmd = BoxTextHandler {
154115
_boxTextHandler_isActive = False
155-
, _boxTextHandler_state = uncurry makeTextInputState (getSBox selection) rmd
116+
, _boxTextHandler_state = uncurry makeTextInputState (_textImpl_mustGetOwlItem boxTextImpl $ selection) rmd
156117
, _boxTextHandler_prevHandler = prev
157118
, _boxTextHandler_undoFirst = False
158119
, _boxTextHandler_commitOnMouseUp = commit
159120
}
160121

161122
updateBoxTextHandlerState :: Bool -> CanvasSelection -> BoxTextHandler -> BoxTextHandler
162-
updateBoxTextHandlerState reset selection tah@BoxTextHandler {..} = assert tzIsCorrect r where
163-
(_, sbox) = getSBox selection
164-
165-
newText = _sBoxText_text . _sBox_text $ sbox
166-
167-
recomputetz = TZ.fromText newText
168-
oldtz = _textInputState_zipper _boxTextHandler_state
169-
-- NOTE that recomputetz won't have the same cursor position
170-
-- TODO delete this check, not very meaningful, but good for development purposes I guess
171-
tzIsCorrect = TZ.value oldtz == TZ.value recomputetz
172-
173-
nextstate = updateTextInputStateWithSBox sbox _boxTextHandler_state
174-
123+
updateBoxTextHandlerState reset selection tah@BoxTextHandler {..} = r where
124+
nextstate = updateOwlItemTextInputState boxTextImpl reset selection _boxTextHandler_state
175125
r = tah {
176-
_boxTextHandler_state = if reset
177-
then nextstate {
178-
_textInputState_original = Just newText
179-
}
180-
else nextstate
126+
_boxTextHandler_state = nextstate
181127
, _boxTextHandler_undoFirst = if reset
182128
then False
183129
else _boxTextHandler_undoFirst
@@ -305,6 +251,8 @@ lBox_to_boxLabelBox lbx = r where
305251
r = LBox (V2 (x+1) y) (V2 width 1)
306252

307253

254+
255+
308256
updateBoxLabelInputStateWithSBox :: SBox -> TextInputState -> TextInputState
309257
updateBoxLabelInputStateWithSBox sbox btis = r where
310258
alignment = convertTextAlignToTextZipperTextAlignment . _sBoxTitle_align . _sBox_title $ sbox

0 commit comments

Comments
 (0)