[escher/text_input] Fix cursor position when deleting selection

This commit is contained in:
Léa Saviot
2020-01-08 16:03:28 +01:00
parent cb53038909
commit 3ff94b8930
2 changed files with 13 additions and 8 deletions

View File

@@ -60,6 +60,7 @@ protected:
// Alignment
void setAlignment(float horizontalAlignment, float verticalAlignment);
float horizontalAlignment() const { return m_horizontalAlignment; }
// Reload
void reloadRectFromPosition(const char * position, bool includeFollowingLines = false);

View File

@@ -151,15 +151,19 @@ void TextInput::scrollToCursor() {
}
void TextInput::deleteSelection() {
assert(!contentView()->selectionIsEmpty());
const char * previousSelectionStart = contentView()->selectionStart();
const char * previousSelectionEnd = contentView()->selectionEnd();
bool cursorIsAtEndOfSelection = previousSelectionEnd == contentView()->cursorLocation();
size_t removedLength = contentView()->deleteSelection();
if (cursorIsAtEndOfSelection) {
setCursorLocation(contentView()->cursorLocation() - removedLength);
ContentView * cv = contentView();
assert(!cv->selectionIsEmpty());
const float horizontalAlignment = cv->horizontalAlignment();
if (horizontalAlignment == 0.0f) {
cv->reloadRectFromPosition(cv->selectionStart(), true);
}
contentView()->reloadRectFromPosition(previousSelectionStart, true);
bool cursorIsAtEndOfSelection = cv->selectionEnd() == cv->cursorLocation();
size_t removedLength = cv->deleteSelection();
if (cursorIsAtEndOfSelection) {
setCursorLocation(cv->cursorLocation() - removedLength);
}
layoutSubviews(true); // Set the cursor frame by calling the subviews relayouting
scrollToCursor();
}
void TextInput::setAlignment(float horizontalAlignment, float verticalAlignment) {