[apps/escher] Fix "Clear" event handling in TextFieldWithExtension

This commit is contained in:
Léa Saviot
2018-11-06 15:20:20 +01:00
committed by Émilie Feral
parent 0e2201b273
commit aea98de6ec
6 changed files with 57 additions and 3 deletions

View File

@@ -11,4 +11,30 @@ void TextFieldWithExtension::willSetCursorLocation(int * location) {
}
}
bool TextFieldWithExtension::privateRemoveEndOfLine() {
return removeTextBeforeExtension(false);
}
void TextFieldWithExtension::removeWholeText() {
removeTextBeforeExtension(true);
scrollToCursor();
}
bool TextFieldWithExtension::removeTextBeforeExtension(bool whole) {
int extensionIndex = strlen(text()) - m_extensionLength;
assert(extensionIndex >= 0 && extensionIndex < ContentView::k_maxBufferSize - m_extensionLength);
size_t destinationIndex = whole ? 0 : cursorLocation();
if (destinationIndex == extensionIndex) {
return false;
}
assert(destinationIndex >= 0);
assert(destinationIndex < extensionIndex);
m_contentView.willModifyTextBuffer();
strlcpy(&(m_contentView.textBuffer()[destinationIndex]), &(m_contentView.textBuffer()[extensionIndex]), ContentView::k_maxBufferSize);
m_contentView.setCursorLocation(destinationIndex);
m_contentView.didModifyTextBuffer();
layoutSubviews();
return true;
}
}

View File

@@ -25,6 +25,9 @@ public:
{}
private:
void willSetCursorLocation(int * location) override;
bool privateRemoveEndOfLine() override;
void removeWholeText() override;
bool removeTextBeforeExtension(bool whole);
size_t m_extensionLength;
};

View File

@@ -56,6 +56,8 @@ protected:
KDSize minimalSizeForOptimalDisplay() const override;
bool removeChar() override;
bool removeEndOfLine() override;
void willModifyTextBuffer();
void didModifyTextBuffer();
/* In some app (ie Calculation), text fields record expression results whose
* lengths can reach 70 (ie
* [[1.234567e-123*e^(1.234567e-123*i), 1.234567e-123*e^(1.234567e-123*i)]]).
@@ -80,6 +82,7 @@ protected:
private:
bool privateHandleEvent(Ion::Events::Event event);
bool privateHandleMoveEvent(Ion::Events::Event event);
virtual void removeWholeText();
bool m_hasTwoBuffers;
TextFieldDelegate * m_delegate;
};

View File

@@ -46,13 +46,14 @@ protected:
* buffer, nothing is done (not even adding few letters from the text to reach
* the maximum buffer capacity) and false is returned. */
bool insertTextAtLocation(const char * textBuffer, int location);
virtual bool removeEndOfLine();
bool removeEndOfLine();
ContentView * contentView() {
return const_cast<ContentView *>(nonEditableContentView());
}
virtual const ContentView * nonEditableContentView() const = 0;
private:
virtual void willSetCursorLocation(int * location) {}
virtual bool privateRemoveEndOfLine();
};
#endif

View File

@@ -162,6 +162,19 @@ bool TextField::ContentView::removeEndOfLine() {
return true;
}
void TextField::ContentView::willModifyTextBuffer() {
/* This method should be called when the buffer is modified outside the
* content view, for instance from the textfield directly. */
reloadRectFromCursorPosition(0);
}
void TextField::ContentView::didModifyTextBuffer() {
/* This method should be called when the buffer is modified outside the
* content view, for instance from the textfield directly. */
m_currentDraftTextLength = strlen(m_draftTextBuffer);
layoutSubviews();
}
void TextField::ContentView::layoutSubviews() {
if (!m_isEditing) {
m_cursorView.setFrame(KDRectZero);
@@ -287,7 +300,7 @@ bool TextField::privateHandleEvent(Ion::Events::Event event) {
}
if (event == Ion::Events::Clear && isEditing()) {
if (!removeEndOfLine()) {
setEditing(true, true);
removeWholeText();
}
return true;
}
@@ -448,3 +461,7 @@ bool TextField::handleEventWithText(const char * eventText, bool indentation, bo
setCursorLocation(nextCursorLocation);
return m_delegate->textFieldDidHandleEvent(this, true, strlen(text()) != previousTextLength);
}
void TextField::removeWholeText() {
setEditing(true, true);
}

View File

@@ -96,9 +96,13 @@ bool TextInput::insertTextAtLocation(const char * text, int location) {
}
bool TextInput::removeEndOfLine() {
if (contentView()->removeEndOfLine()) {
if (privateRemoveEndOfLine()) {
scrollToCursor();
return true;
}
return false;
}
bool TextInput::privateRemoveEndOfLine() {
return contentView()->removeEndOfLine();
}