diff --git a/apps/calculation/edit_expression_controller.cpp b/apps/calculation/edit_expression_controller.cpp index 278f4c53b..0df8a0f17 100644 --- a/apps/calculation/edit_expression_controller.cpp +++ b/apps/calculation/edit_expression_controller.cpp @@ -83,7 +83,7 @@ void EditExpressionController::didBecomeFirstResponder() { } bool EditExpressionController::textFieldDidReceiveEvent(::TextField * textField, Ion::Events::Event event) { - if (textField->textFieldShouldFinishEditing(event) && textField->isEditing() && strlen(textField->text()) == 0 && m_calculationStore->numberOfCalculations() > 0) { + if (textField->isEditing() && textField->textFieldShouldFinishEditing(event) && textField->draftTextLength() == 0 && m_calculationStore->numberOfCalculations() > 0) { App * calculationApp = (App *)app(); const char * lastTextBody = m_calculationStore->calculationAtIndex(m_calculationStore->numberOfCalculations()-1)->inputText(); m_calculationStore->push(lastTextBody, calculationApp->localContext()); diff --git a/apps/calculation/text_field.cpp b/apps/calculation/text_field.cpp index 471ff8d54..e9349c201 100644 --- a/apps/calculation/text_field.cpp +++ b/apps/calculation/text_field.cpp @@ -17,17 +17,13 @@ bool TextField::handleEvent(Ion::Events::Event event) { setCursorLocation(cursorLocation() + strlen("ans")); return true; } - if (textLength() == 0 && + if (isEditing() && draftTextLength() == 0 && (event == Ion::Events::Multiplication || event == Ion::Events::Plus || event == Ion::Events::Power || event == Ion::Events::Square || event == Ion::Events::Division || event == Ion::Events::Sto)) { - if (!isEditing()) { - setEditing(true); - setText(""); - } insertTextAtLocation("ans", cursorLocation()); setCursorLocation(cursorLocation() + strlen("ans")); } diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index a02b368c0..df32e7ac7 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -198,7 +198,8 @@ void ConsoleController::tableViewDidChangeSelection(SelectableTableView * t, int } bool ConsoleController::textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) { - return (strlen(textField->text()) > 0 + assert(textField->isEditing()); + return (textField->draftTextLength() > 0 && (event == Ion::Events::OK || event == Ion::Events::EXE)); } diff --git a/apps/code/menu_controller.cpp b/apps/code/menu_controller.cpp index 62b44f4b1..ea6ff385b 100644 --- a/apps/code/menu_controller.cpp +++ b/apps/code/menu_controller.cpp @@ -274,7 +274,7 @@ bool MenuController::textFieldShouldFinishEditing(TextField * textField, Ion::Ev bool MenuController::textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) { if (event == Ion::Events::Right && textField->isEditing()) { int scriptExtensionLength = strlen(ScriptStore::k_scriptExtension); - if (textField->cursorLocation() > textField->textLength() - scriptExtensionLength - 1) { + if (textField->cursorLocation() > textField->draftTextLength() - scriptExtensionLength - 1) { return true; } } diff --git a/apps/shared/editable_cell_table_view_controller.cpp b/apps/shared/editable_cell_table_view_controller.cpp index 18337bcbe..ea03e457f 100644 --- a/apps/shared/editable_cell_table_view_controller.cpp +++ b/apps/shared/editable_cell_table_view_controller.cpp @@ -18,7 +18,7 @@ bool EditableCellTableViewController::textFieldShouldFinishEditing(TextField * t return TextFieldDelegate::textFieldShouldFinishEditing(textField, event) || (event == Ion::Events::Down && selectedRow() < numberOfRows()-1) || (event == Ion::Events::Up && selectedRow() > 0) - || (event == Ion::Events::Right && textField->cursorLocation() == textField->textLength() && selectedColumn() < numberOfColumns()-1) + || (event == Ion::Events::Right && textField->cursorLocation() == textField->draftTextLength() && selectedColumn() < numberOfColumns()-1) || (event == Ion::Events::Left && textField->cursorLocation() == 0 && selectedColumn() > 0); } bool EditableCellTableViewController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) { diff --git a/apps/shared/text_field_delegate_app.cpp b/apps/shared/text_field_delegate_app.cpp index 0bc1595ca..1678f234e 100644 --- a/apps/shared/text_field_delegate_app.cpp +++ b/apps/shared/text_field_delegate_app.cpp @@ -77,7 +77,7 @@ bool TextFieldDelegateApp::textFieldShouldFinishEditing(TextField * textField, I } bool TextFieldDelegateApp::textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) { - if (textField->textFieldShouldFinishEditing(event) && textField->isEditing()) { + if (textField->isEditing() && textField->textFieldShouldFinishEditing(event)) { Expression * exp = Expression::parse(textField->text()); if (exp != nullptr) { delete exp; diff --git a/escher/include/escher/text_field.h b/escher/include/escher/text_field.h index 424458f9a..9b49c9f4c 100644 --- a/escher/include/escher/text_field.h +++ b/escher/include/escher/text_field.h @@ -16,7 +16,7 @@ public: Toolbox * toolbox() override; bool isEditing() const; const char * text() const; - int textLength() const; + int draftTextLength() const; int cursorLocation() const; void setCursorLocation(int location); void setText(const char * text); @@ -45,7 +45,7 @@ protected: void reload(); bool isEditing() const { return m_isEditing; } const char * text() const; - int textLength() const; + int draftTextLength() const; int cursorLocation() const { return m_currentCursorLocation; } char * textBuffer() { return m_textBuffer; } char * draftTextBuffer() { return m_draftTextBuffer; } @@ -79,7 +79,7 @@ protected: bool m_isEditing; char * m_textBuffer; char * m_draftTextBuffer; - size_t m_currentTextLength; + size_t m_currentDraftTextLength; size_t m_currentCursorLocation; size_t m_textBufferSize; float m_horizontalAlignment; diff --git a/escher/src/text_field.cpp b/escher/src/text_field.cpp index b683eccd7..9500e0a55 100644 --- a/escher/src/text_field.cpp +++ b/escher/src/text_field.cpp @@ -9,7 +9,7 @@ TextField::ContentView::ContentView(char * textBuffer, char * draftTextBuffer, s m_isEditing(false), m_textBuffer(textBuffer), m_draftTextBuffer(draftTextBuffer), - m_currentTextLength(0), + m_currentDraftTextLength(0), m_currentCursorLocation(0), m_textBufferSize(textBufferSize), m_horizontalAlignment(horizontalAlignment), @@ -48,21 +48,22 @@ void TextField::ContentView::reload() { const char * TextField::ContentView::text() const { if (m_isEditing) { - return const_cast(m_draftTextBuffer);; + return const_cast(m_draftTextBuffer); } return const_cast(m_textBuffer); } -int TextField::ContentView::textLength() const { - assert(strlen(text()) == m_currentTextLength); - return m_currentTextLength; +int TextField::ContentView::draftTextLength() const { + assert(isEditing()); + assert(strlen(text()) == m_currentDraftTextLength); + return m_currentDraftTextLength; } void TextField::ContentView::setText(const char * text) { if (m_isEditing) { strlcpy(m_draftTextBuffer, text, m_textBufferSize); int textLength = strlen(text) >= m_textBufferSize ? m_textBufferSize-1 : strlen(text); - m_currentTextLength = textLength; + m_currentDraftTextLength = textLength; } else { strlcpy(m_textBuffer, text, m_textBufferSize); } @@ -100,29 +101,29 @@ void TextField::ContentView::setEditing(bool isEditing, bool reinitDrafBuffer) { void TextField::ContentView::reinitDraftTextBuffer() { setCursorLocation(0); m_draftTextBuffer[0] = 0; - m_currentTextLength = 0; + m_currentDraftTextLength = 0; } void TextField::ContentView::setCursorLocation(int location) { int adjustedLocation = location < 0 ? 0 : location; - adjustedLocation = adjustedLocation > (signed int)m_currentTextLength ? (signed int)m_currentTextLength : adjustedLocation; + adjustedLocation = adjustedLocation > (signed int)m_currentDraftTextLength ? (signed int)m_currentDraftTextLength : adjustedLocation; m_currentCursorLocation = adjustedLocation; layoutSubviews(); } bool TextField::ContentView::insertTextAtLocation(const char * text, int location) { int textSize = strlen(text); - if (m_currentTextLength + textSize >= m_textBufferSize || textSize == 0) { + if (m_currentDraftTextLength + textSize >= m_textBufferSize || textSize == 0) { return false; } - for (int k = m_currentTextLength; k >= location && k >= 0; k--) { + for (int k = m_currentDraftTextLength; k >= location && k >= 0; k--) { m_draftTextBuffer[k+textSize] = m_draftTextBuffer[k]; } strlcpy(&m_draftTextBuffer[location], text, textSize); if (location+textSize > 0) { m_draftTextBuffer[location+textSize-1] = text[textSize-1]; } - m_currentTextLength += textSize; + m_currentDraftTextLength += textSize; reload(); return true; } @@ -151,21 +152,21 @@ void TextField::ContentView::deleteCharPrecedingCursor() { return; } reload(); - m_currentTextLength--; + m_currentDraftTextLength--; setCursorLocation(m_currentCursorLocation-1); - for (int k = m_currentCursorLocation; k < (signed char)m_currentTextLength; k ++) { + for (int k = m_currentCursorLocation; k < (signed char)m_currentDraftTextLength; k ++) { m_draftTextBuffer[k] = m_draftTextBuffer[k+1]; } - m_draftTextBuffer[m_currentTextLength] = 0; + m_draftTextBuffer[m_currentDraftTextLength] = 0; layoutSubviews(); } bool TextField::ContentView::deleteEndOfLine() { - if (m_currentTextLength == m_currentCursorLocation) { + if (m_currentDraftTextLength == m_currentCursorLocation) { return false; } reload(); - m_currentTextLength = m_currentCursorLocation; + m_currentDraftTextLength = m_currentCursorLocation; m_draftTextBuffer[m_currentCursorLocation] = 0; layoutSubviews(); return true; @@ -219,8 +220,9 @@ const char * TextField::text() const { return m_contentView.text(); } -int TextField::textLength() const { - return m_contentView.textLength(); +int TextField::draftTextLength() const { + assert(isEditing()); + return m_contentView.draftTextLength(); } int TextField::cursorLocation() const{ @@ -236,7 +238,7 @@ void TextField::setText(const char * text) { reloadScroll(); m_contentView.setText(text); if (isEditing()) { - setCursorLocation(textLength()); + setCursorLocation(draftTextLength()); } } @@ -316,15 +318,15 @@ bool TextField::handleEvent(Ion::Events::Event event) { setCursorLocation(0); return true; } - if (event == Ion::Events::Right && isEditing() && cursorLocation() < textLength()) { + if (event == Ion::Events::Right && isEditing() && cursorLocation() < draftTextLength()) { setCursorLocation(cursorLocation()+1); return true; } if (event == Ion::Events::End && isEditing()) { - setCursorLocation(textLength()); + setCursorLocation(draftTextLength()); return true; } - if (textFieldShouldFinishEditing(event) && isEditing()) { + if (isEditing() && textFieldShouldFinishEditing(event)) { char bufferText[ContentView::k_maxBufferSize]; strlcpy(bufferText, m_contentView.textBuffer(), ContentView::k_maxBufferSize); strlcpy(m_contentView.textBuffer(), m_contentView.draftTextBuffer(), m_contentView.bufferSize()); @@ -348,7 +350,7 @@ bool TextField::handleEvent(Ion::Events::Event event) { setEditing(true); /* If the text could not be inserted (buffer is full), we set the cursor * at the end of the text. */ - int nextCursorLocation = textLength(); + int nextCursorLocation = draftTextLength(); if (insertTextAtLocation(m_contentView.textBuffer(), cursorLocation())) { nextCursorLocation = strlen(m_contentView.draftTextBuffer()); } @@ -363,7 +365,7 @@ bool TextField::handleEvent(Ion::Events::Event event) { if (!isEditing()) { setEditing(true); } - int nextCursorLocation = textLength(); + int nextCursorLocation = draftTextLength(); if (insertTextAtLocation(event.text(), cursorLocation())) { /* All events whose text is longer than 2 have parenthesis. In these cases, * we want to position the cursor before the last parenthesis */ @@ -398,7 +400,7 @@ bool TextField::handleEvent(Ion::Events::Event event) { if (!isEditing()) { setEditing(true); } - int nextCursorLocation = textLength(); + int nextCursorLocation = draftTextLength(); if (insertTextAtLocation(Clipboard::sharedClipboard()->storedText(), cursorLocation())) { nextCursorLocation = cursorLocation() + strlen(Clipboard::sharedClipboard()->storedText()); }