[poincare/lyt_cursor] When selecting, we add one full layout each time

This commit is contained in:
Léa Saviot
2019-11-19 17:52:17 +01:00
parent 35fe6f0bc9
commit a959f9fa98
4 changed files with 30 additions and 36 deletions

View File

@@ -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();

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -79,15 +79,13 @@ bool IsBefore(Layout& l1, Layout& l2) {
return reinterpret_cast<char *>(l1.node()) <= reinterpret_cast<char *>(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 */