From c30f136fb362f948a565a5a1252f9af64de63431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Wed, 21 Nov 2018 15:37:27 +0100 Subject: [PATCH] [apps/poincare] Fix cursor position when inserting log(a,b) in 2DEdition --- apps/solver/list_controller.cpp | 3 ++- escher/src/layout_field.cpp | 22 +++++++++------------- poincare/include/poincare/layout.h | 2 +- poincare/src/layout.cpp | 11 ++++++----- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/apps/solver/list_controller.cpp b/apps/solver/list_controller.cpp index a6031b40f..88ef6ff4f 100644 --- a/apps/solver/list_controller.cpp +++ b/apps/solver/list_controller.cpp @@ -122,9 +122,10 @@ bool textRepresentsAnEquality(const char * text) { } bool layoutRepresentsAnEquality(Poincare::Layout l) { - return l.recursivelyMatches( + Poincare::Layout match = l.recursivelyMatches( [](Poincare::Layout layout) { return layout.isChar() && static_cast(layout).character() == '"'; }); + return !match.isUninitialized(); } bool ListController::textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) { diff --git a/escher/src/layout_field.cpp b/escher/src/layout_field.cpp index 008dd08f6..866875eb2 100644 --- a/escher/src/layout_field.cpp +++ b/escher/src/layout_field.cpp @@ -129,19 +129,15 @@ bool LayoutField::handleEventWithText(const char * text, bool indentation, bool } // Find the pointed layout. Layout pointedLayout; - if (strcmp(text, I18n::translate(I18n::Message::RandomCommandWithArg)) == 0) { - /* Special case: if the text is "random()", the cursor should not be set - * inside the parentheses. */ - pointedLayout = resultLayout; - } else if (resultLayout.isHorizontal()) { - /* If the layout is horizontal, pick the first open parenthesis. For now, - * all horizontal layouts in MathToolbox have parentheses. */ - for (int i = 0; i < resultLayout.numberOfChildren(); i++) { - Layout l = resultLayout.childAtIndex(i); - if (l.isLeftParenthesis()) { - pointedLayout = l; - break; - } + if (!forceCursorRightOfText) { + if (strcmp(text, I18n::translate(I18n::Message::RandomCommandWithArg)) == 0) { + /* Special case: if the text is "random()", the cursor should not be set + * inside the parentheses. */ + pointedLayout = resultLayout; + } else if (resultLayout.isHorizontal()) { + pointedLayout = resultLayout.recursivelyMatches( + [](Poincare::Layout layout) { + return layout.isLeftParenthesis() || layout.isEmpty();}); } } /* Insert the layout. If pointedLayout is uninitialized, the cursor will diff --git a/poincare/include/poincare/layout.h b/poincare/include/poincare/layout.h index af0098be0..53d3dda14 100644 --- a/poincare/include/poincare/layout.h +++ b/poincare/include/poincare/layout.h @@ -41,7 +41,7 @@ public: // Layout properties typedef bool (*LayoutTest)(const Layout l); - bool recursivelyMatches(LayoutTest test) const; + Layout recursivelyMatches(LayoutTest test) const; bool mustHaveLeftSibling() const { return const_cast(this)->node()->mustHaveLeftSibling(); } bool isEmpty() const { return const_cast(this)->node()->isEmpty(); } bool isHorizontal() const { return const_cast(this)->node()->isHorizontal(); } diff --git a/poincare/src/layout.cpp b/poincare/src/layout.cpp index 9af1faaff..fb43bfa2e 100644 --- a/poincare/src/layout.cpp +++ b/poincare/src/layout.cpp @@ -36,16 +36,17 @@ int Layout::serializeParsedExpression(char * buffer, int bufferSize) const { return e.serialize(buffer, bufferSize, Poincare::Preferences::sharedPreferences()->displayMode()); } -bool Layout::recursivelyMatches(LayoutTest test) const { +Layout Layout::recursivelyMatches(LayoutTest test) const { if (test(*this)) { - return true; + return *this; } for (int i = 0; i < numberOfChildren(); i++) { - if (childAtIndex(i).recursivelyMatches(test)) { - return true; + Layout childResult = childAtIndex(i).recursivelyMatches(test); + if (!childResult.isUninitialized()) { + return childResult; } } - return false; + return Layout(); } // Cursor