From c04a7af22f66f7e6a38eaaf844e3a4b6d1eaf233 Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Thu, 23 Jul 2020 12:19:37 +0200 Subject: [PATCH] [escher/layout_field] Change cursor behavior Some code to prevent the input field from scrolling when switching to the history in layout mode causes problems : - With a calculation in the history, type an empty matrix or fraction. Then with the cursor on an empty square, go to the history. The square will appear shifted to the right. Change-Id: I72ebee675215dc3c3a3f8432890f6fee820ef5c9 --- escher/include/escher/layout_field.h | 2 +- escher/src/layout_field.cpp | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/escher/include/escher/layout_field.h b/escher/include/escher/layout_field.h index 908d27f3f..bd36e8b14 100644 --- a/escher/include/escher/layout_field.h +++ b/escher/include/escher/layout_field.h @@ -58,7 +58,7 @@ private: constexpr static int k_maxNumberOfLayouts = 220; static_assert(k_maxNumberOfLayouts == TextField::maxBufferSize(), "Maximal number of layouts in a layout field should be equal to max number of char in text field"); void reload(KDSize previousSize); - virtual bool privateHandleEvent(Ion::Events::Event event, bool * shouldScrollAndRedraw); + virtual bool privateHandleEvent(Ion::Events::Event event); bool privateHandleMoveEvent(Ion::Events::Event event, bool * shouldRecomputeLayout); bool privateHandleSelectionEvent(Ion::Events::Event event, bool * shouldRecomputeLayout); void scrollRightOfLayout(Poincare::Layout layoutR); diff --git a/escher/src/layout_field.cpp b/escher/src/layout_field.cpp index 26f460061..a4c4c3a07 100644 --- a/escher/src/layout_field.cpp +++ b/escher/src/layout_field.cpp @@ -262,7 +262,11 @@ void LayoutField::ContentView::layoutSubviews(bool force) { void LayoutField::ContentView::layoutCursorSubview(bool force) { if (!m_isEditing) { - m_cursorView.setFrame(KDRectZero, force); + /* We keep track of the cursor's position to prevent the input field from + * scrolling to the beginning when switching to the history. This way, + * when calling scrollToCursor after layoutCursorSubview, we don't lose + * sight of the cursor. */ + m_cursorView.setFrame(KDRect(cursorRect().x(), cursorRect().y(), 0, 0), force); return; } KDPoint expressionViewOrigin = m_expressionView.absoluteDrawingOrigin(); @@ -430,7 +434,6 @@ bool LayoutField::handleEvent(Ion::Events::Event event) { if (!eventShouldUpdateInsertionCursor(event)) { m_contentView.invalidateInsertionCursor(); } - bool shouldScrollAndRedraw = true; if (privateHandleMoveEvent(event, &moveEventChangedLayout)) { if (!isEditing()) { setEditing(true); @@ -454,12 +457,7 @@ bool LayoutField::handleEvent(Ion::Events::Event event) { } shouldRecomputeLayout = m_contentView.cursor()->layout().removeGraySquaresFromAllMatrixChildren() || removedSquares || shouldRecomputeLayout; } - } else if (privateHandleEvent(event, &shouldScrollAndRedraw)) { - if (!shouldScrollAndRedraw) { - /* We escape early to avoid scrolling to the beginning of the expression, - * and to mimic the behaviour of the TextField. */ - return true; - } + } else if (privateHandleEvent(event)) { shouldRecomputeLayout = true; didHandleEvent = true; } @@ -507,10 +505,8 @@ static inline bool IsMoveEvent(Ion::Events::Event event) { static_cast(event) <= static_cast(Ion::Events::Right); } -bool LayoutField::privateHandleEvent(Ion::Events::Event event, bool * shouldScrollAndRedraw) { - assert(*shouldScrollAndRedraw); +bool LayoutField::privateHandleEvent(Ion::Events::Event event) { if (m_delegate && m_delegate->layoutFieldDidReceiveEvent(this, event)) { - *shouldScrollAndRedraw = false; return true; } if (handleBoxEvent(event)) {