diff --git a/escher/include/escher/text_input.h b/escher/include/escher/text_input.h index 59c576132..633fcd790 100644 --- a/escher/include/escher/text_input.h +++ b/escher/include/escher/text_input.h @@ -50,7 +50,7 @@ protected: virtual const char * editedText() const = 0; virtual size_t editedTextLength() const = 0; }; -protected: + /* If the text to be appended is too long to be added without overflowing the * buffer, nothing is done (not even adding few letters from the text to reach * the maximum buffer capacity) and false is returned. */ @@ -60,6 +60,8 @@ protected: return const_cast(nonEditableContentView()); } virtual const ContentView * nonEditableContentView() const = 0; + bool moveCursorLeft(); + bool moveCursorRight(); private: virtual void willSetCursorLocation(const char * * location) {} virtual bool privateRemoveEndOfLine(); diff --git a/escher/src/text_area.cpp b/escher/src/text_area.cpp index 34254fb70..2aac54475 100644 --- a/escher/src/text_area.cpp +++ b/escher/src/text_area.cpp @@ -99,18 +99,9 @@ bool TextArea::handleEvent(Ion::Events::Event event) { } else if (handleBoxEvent(app(), event)) { return true; } else if (event == Ion::Events::Left) { - if (cursorLocation() <= text()) { - assert(cursorLocation() == text()); - return false; - } - UTF8Decoder decoder(text(), cursorLocation()); - return setCursorLocation(decoder.previousGlyphPosition()); + return TextInput::moveCursorLeft(); } else if (event == Ion::Events::Right) { - if (UTF8Helper::CodePointIs(cursorLocation(), UCodePointNull)) { - return false; - } - UTF8Decoder decoder(cursorLocation()); - return setCursorLocation(decoder.nextGlyphPosition()); + return TextInput::moveCursorRight(); } else if (event == Ion::Events::Up) { contentView()->moveCursorGeo(0, -1); } else if (event == Ion::Events::Down) { diff --git a/escher/src/text_field.cpp b/escher/src/text_field.cpp index 122727cee..8d23fcc54 100644 --- a/escher/src/text_field.cpp +++ b/escher/src/text_field.cpp @@ -432,23 +432,21 @@ void TextField::scrollToCursor() { } bool TextField::privateHandleMoveEvent(Ion::Events::Event event) { - if (event == Ion::Events::Left && isEditing() && cursorLocation() > m_contentView.draftTextBuffer()) { - assert(isEditing()); - UTF8Decoder decoder(m_contentView.draftTextBuffer(), cursorLocation()); - return setCursorLocation(decoder.previousGlyphPosition()); + if (!isEditing()) { + return false; } - if (event == Ion::Events::ShiftLeft && isEditing()) { - assert(isEditing()); - return setCursorLocation(m_contentView.draftTextBuffer()); + const char * draftBuffer = m_contentView.draftTextBuffer(); + if (event == Ion::Events::Left && cursorLocation() > draftBuffer) { + return TextInput::moveCursorLeft(); } - if (event == Ion::Events::Right && isEditing() && cursorLocation() < m_contentView.draftTextBuffer() + draftTextLength()) { - assert(isEditing()); - UTF8Decoder decoder(cursorLocation()); - return setCursorLocation(decoder.nextGlyphPosition()); + if (event == Ion::Events::Right && cursorLocation() < draftBuffer + draftTextLength()) { + return TextInput::moveCursorRight(); } - if (event == Ion::Events::ShiftRight && isEditing()) { - assert(isEditing()); - return setCursorLocation(m_contentView.draftTextBuffer() + draftTextLength()); + if (event == Ion::Events::ShiftLeft) { + return setCursorLocation(draftBuffer); + } + if (event == Ion::Events::ShiftRight) { + return setCursorLocation(draftBuffer + draftTextLength()); } return false; } diff --git a/escher/src/text_input.cpp b/escher/src/text_input.cpp index 46fd78484..a5cb7f293 100644 --- a/escher/src/text_input.cpp +++ b/escher/src/text_input.cpp @@ -96,6 +96,23 @@ bool TextInput::removeEndOfLine() { return false; } +bool TextInput::moveCursorLeft() { + if (cursorLocation() <= text()) { + assert(cursorLocation() == text()); + return false; + } + UTF8Decoder decoder(text(), cursorLocation()); + return setCursorLocation(decoder.previousGlyphPosition()); +} + +bool TextInput::moveCursorRight() { + if (UTF8Helper::CodePointIs(cursorLocation(), UCodePointNull)) { + return false; + } + UTF8Decoder decoder(cursorLocation()); + return setCursorLocation(decoder.nextGlyphPosition()); +} + bool TextInput::privateRemoveEndOfLine() { return contentView()->removeEndOfLine(); }