diff --git a/escher/include/escher/layout_field.h b/escher/include/escher/layout_field.h index 8008a47ec..3b00d796f 100644 --- a/escher/include/escher/layout_field.h +++ b/escher/include/escher/layout_field.h @@ -75,7 +75,7 @@ private: // Selection Poincare::Layout * selectionStart() { return &m_selectionStart; } Poincare::Layout * selectionEnd() { return &m_selectionEnd; } - void addSelection(Poincare::Layout left, Poincare::Layout right); + void addSelection(Poincare::Layout addedLayout); bool resetSelection(); // returns true if the selection was indeed reset bool selectionIsEmpty() const; size_t deleteSelection(); diff --git a/escher/src/layout_field.cpp b/escher/src/layout_field.cpp index 3fb7d428f..4bd843f30 100644 --- a/escher/src/layout_field.cpp +++ b/escher/src/layout_field.cpp @@ -56,15 +56,15 @@ bool IsBefore(Layout& l1, Layout& l2, bool strict) { return strict ? (node1 < node2) : (node1 <= node2); } -void LayoutField::ContentView::addSelection(Layout left, Layout right) { +void LayoutField::ContentView::addSelection(Layout addedLayout) { if (selectionIsEmpty()) { /* * ---------- -> +++ is the previous previous selection * ( ) -> added selection * ---+++++-- -> next selection * */ - m_selectionStart = left; - m_selectionEnd = right; + m_selectionStart = addedLayout; + m_selectionEnd = addedLayout; } #if 0 else if (left == m_selectionEnd) { @@ -83,32 +83,35 @@ void LayoutField::ContentView::addSelection(Layout left, Layout right) { m_selectionStart = left; } #endif - else if (IsBefore(m_selectionEnd, left, false) && !left.hasAncestor(m_selectionEnd, true)) { + else if (IsBefore(m_selectionEnd, addedLayout, false) && !addedLayout.hasAncestor(m_selectionEnd, true)) { /* * +++------- -> +++ is the previous previous selection * ( ) -> added selection * ++++++++++ -> next selection * */ - m_selectionEnd = right; - } else if (IsBefore(right, m_selectionStart, true)) { + m_selectionEnd = addedLayout; + } else if (IsBefore(addedLayout, m_selectionStart, true)) { /* * -------+++ -> +++ is the previous previous selection * ( ) -> added selection * ++++++++++ -> next selection * */ - m_selectionStart = left; - } else if (m_selectionEnd == right) { + if (m_selectionStart.hasAncestor(addedLayout, true)) { + m_selectionEnd = addedLayout; + } + m_selectionStart = addedLayout; + } else if (m_selectionEnd == addedLayout) { /* * ++++++++++ -> +++ is the previous previous selection * ( ) -> added selection * +++++----- -> next selection * */ - LayoutCursor c1 = LayoutCursor(left, LayoutCursor::Position::Left); + LayoutCursor c1 = LayoutCursor(addedLayout, LayoutCursor::Position::Left); if (c1.layoutReference() == m_selectionStart) { m_selectionStart = Layout(); m_selectionEnd = Layout(); } else { - LayoutCursor c2 = left.equivalentCursor(&c1); + LayoutCursor c2 = addedLayout.equivalentCursor(&c1); Layout c2Layout = c2.layoutReference(); if (c2.position() == LayoutCursor::Position::Right) { assert(IsBefore(m_selectionStart, c2Layout, false)); @@ -119,18 +122,18 @@ void LayoutField::ContentView::addSelection(Layout left, Layout right) { } } } else { - assert(m_selectionStart == left); + assert(m_selectionStart == addedLayout); /* * ++++++++++ -> +++ is the previous previous selection * ( ) -> added selection * -----+++++ -> next selection * */ - LayoutCursor c1 = LayoutCursor(right, LayoutCursor::Position::Right); + LayoutCursor c1 = LayoutCursor(addedLayout, LayoutCursor::Position::Right); if (c1.layoutReference() == m_selectionEnd) { m_selectionStart = Layout(); m_selectionEnd = Layout(); } else { - LayoutCursor c2 = right.equivalentCursor(&c1); + LayoutCursor c2 = addedLayout.equivalentCursor(&c1); Layout c2Layout = c2.layoutReference(); if (c2.position() == LayoutCursor::Position::Left) { assert(IsBefore(c2Layout, m_selectionEnd, false)); @@ -412,11 +415,10 @@ bool LayoutField::privateHandleMoveEvent(Ion::Events::Event event, bool * should bool LayoutField::privateHandleSelectionEvent(Ion::Events::Event event, bool * shouldRecomputeLayout) { LayoutCursor result; if (event == Ion::Events::ShiftLeft || event == Ion::Events::ShiftRight) { - Layout addedSelectionLeft; - Layout addedSelectionRight; - result = m_contentView.cursor()->selectAtDirection(event == Ion::Events::ShiftLeft ? LayoutCursor::MoveDirection::Left : LayoutCursor::MoveDirection::Right, shouldRecomputeLayout, &addedSelectionLeft, &addedSelectionRight); - if (!addedSelectionLeft.isUninitialized() && !addedSelectionRight.isUninitialized()) { //TODO LEA assert? - m_contentView.addSelection(addedSelectionLeft, addedSelectionRight); + Layout addedSelection; + result = m_contentView.cursor()->selectAtDirection(event == Ion::Events::ShiftLeft ? LayoutCursor::MoveDirection::Left : LayoutCursor::MoveDirection::Right, shouldRecomputeLayout, &addedSelection); + if (!addedSelection.isUninitialized()) { //TODO LEA assert? + m_contentView.addSelection(addedSelection); } else { return false; } diff --git a/poincare/include/poincare/layout_cursor.h b/poincare/include/poincare/layout_cursor.h index c0e4905ce..f78ddc1c3 100644 --- a/poincare/include/poincare/layout_cursor.h +++ b/poincare/include/poincare/layout_cursor.h @@ -102,10 +102,10 @@ public: } /* Select */ - void select(MoveDirection direction, bool * shouldRecomputeLayout, Layout * selectionLeft, Layout * selectionRight); - LayoutCursor selectAtDirection(MoveDirection direction, bool * shouldRecomputeLayout, Layout * selectionLeft, Layout * selectionRight) { + void select(MoveDirection direction, bool * shouldRecomputeLayout, Layout * selection); + LayoutCursor selectAtDirection(MoveDirection direction, bool * shouldRecomputeLayout, Layout * selection) { LayoutCursor result = clone(); - result.select(direction, shouldRecomputeLayout, selectionLeft, selectionRight); + result.select(direction, shouldRecomputeLayout, selection); return result; } diff --git a/poincare/src/layout_cursor.cpp b/poincare/src/layout_cursor.cpp index 0ce326146..94f95e771 100644 --- a/poincare/src/layout_cursor.cpp +++ b/poincare/src/layout_cursor.cpp @@ -79,15 +79,13 @@ bool IsBefore(Layout& l1, Layout& l2) { return reinterpret_cast(l1.node()) <= reinterpret_cast(l2.node()); } -void LayoutCursor::select(MoveDirection direction, bool * shouldRecomputeLayout, Layout * selectionLeft, Layout * selectionRight) { +void LayoutCursor::select(MoveDirection direction, bool * shouldRecomputeLayout, Layout * selection) { assert(!m_layout.isUninitialized()); // Compute ingoing / outgoing positions Position ingoingPosition = direction == MoveDirection::Right ? Position::Left : Position::Right; Position outgoingPosition = direction == MoveDirection::Right ? Position::Right : Position::Left; - Layout * ingoingLayout = direction == MoveDirection::Right ? selectionLeft : selectionRight; - Layout * outgoingLayout = direction == MoveDirection::Right ? selectionRight : selectionLeft; // Find the layout to select @@ -103,11 +101,11 @@ void LayoutCursor::select(MoveDirection direction, bool * shouldRecomputeLayout, * instance, in the layout |1234 , the cursor should be left of the 1, * not left of the horizontal layout. */ assert(equivalentCursor.position() == ingoingPosition); - *ingoingLayout = equivalentLayout; + *selection = equivalentLayout; } else { /* If there is no adequate equivalent position, just set the ingoing * layout on the current layout. */ - *ingoingLayout = m_layout; + *selection = m_layout; } } else { assert(currentLayoutIsEmpty || m_position == outgoingPosition); @@ -116,7 +114,7 @@ void LayoutCursor::select(MoveDirection direction, bool * shouldRecomputeLayout, if (!currentLayoutIsEmpty && !equivalentLayout.isUninitialized() && equivalentCursor.position() == ingoingPosition) { /* If there is an equivalent layout positionned on the ingoing position, * select it. */ - *ingoingLayout = equivalentLayout; + *selection = equivalentLayout; } else { // Else, find the first non horizontal ancestor and select it. Layout notHorizontalAncestor = m_layout.parent(); @@ -126,18 +124,12 @@ void LayoutCursor::select(MoveDirection direction, bool * shouldRecomputeLayout, notHorizontalAncestor = notHorizontalAncestor.parent(); } if (!notHorizontalAncestor.isUninitialized()) { - *ingoingLayout = notHorizontalAncestor; - *outgoingLayout = notHorizontalAncestor; - m_layout = notHorizontalAncestor; - m_position = outgoingPosition; + *selection = notHorizontalAncestor; } - return; } } - *outgoingLayout = *ingoingLayout; - m_layout = *ingoingLayout; + m_layout = *selection; m_position = outgoingPosition; - return; } /* Layout modification */