From cde8969b3ed06aecb794cb644485044fa759e09f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 25 Apr 2017 11:53:12 +0200 Subject: [PATCH] [escher] Cleaner code in textfield Change-Id: If74fd9b9a70f68083c56033014bd49bb17a40d6d --- escher/src/text_field.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/escher/src/text_field.cpp b/escher/src/text_field.cpp index eee225943..ba4cd551f 100644 --- a/escher/src/text_field.cpp +++ b/escher/src/text_field.cpp @@ -312,15 +312,15 @@ bool TextField::handleEvent(Ion::Events::Event event) { setCursorLocation(cursorLoc); return true; } - if (event == Ion::Events::OK) { + if (event == Ion::Events::OK && !isEditing()) { 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(); if (insertTextAtLocation(m_contentView.textBuffer(), cursorLocation())) { - setCursorLocation(strlen(m_contentView.draftTextBuffer())); - } else { - /* If the text could not be inserted (buffer is full), we set the cursor - * at the end of the text. */ - setCursorLocation(textLength()); + nextCursorLocation = strlen(m_contentView.draftTextBuffer()); } + setCursorLocation(nextCursorLocation); return true; } if (event == Ion::Events::Backspace && isEditing()) { @@ -332,14 +332,14 @@ bool TextField::handleEvent(Ion::Events::Event event) { if (!isEditing()) { setEditing(true); } + int nextCursorLocation = textLength(); 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 */ int cursorDelta = strlen(event.text()) > 2 ? -1 : 0; - setCursorLocation(cursorLocation() + strlen(event.text()) + cursorDelta); - } else { - setCursorLocation(maxBufferSize()); + nextCursorLocation = cursorLocation() + strlen(event.text()) + cursorDelta; } + setCursorLocation(nextCursorLocation); layoutSubviews(); return true; }