From 0575cd2d60f78c97b89ff8e24b211531fd787503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 9 Jan 2018 09:50:04 +0100 Subject: [PATCH] [poincare] addBrother should first assess if the layout is horizontal. Before, if the cursor was Right of an HorizontalLayout, pressing "/" might put the whole HorizontalLayout as the numerator of the new fraction, because the fraction layout was added as a brother of the HorizontalLayout, not a child. Change-Id: I3b58920e48db49ccd4204c405362d148260461d4 --- poincare/src/layout/expression_layout.cpp | 31 +++++++++++++---------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/poincare/src/layout/expression_layout.cpp b/poincare/src/layout/expression_layout.cpp index c46fba418..4c0c196ee 100644 --- a/poincare/src/layout/expression_layout.cpp +++ b/poincare/src/layout/expression_layout.cpp @@ -110,32 +110,35 @@ bool ExpressionLayout::hasAncestor(const ExpressionLayout * e) const { } void ExpressionLayout::addBrother(ExpressionLayoutCursor * cursor, ExpressionLayout * brother) { - if (m_parent) { - int brotherIndex = cursor->position() == ExpressionLayoutCursor::Position::Left ? m_parent->indexOfChild(this) : m_parent->indexOfChild(this) + 1; - if (m_parent->isHorizontal()) { - static_cast(m_parent)->addOrMergeChildAtIndex(brother, brotherIndex); - return; + // First, assess the special case when the layout is horizontal. + // If so, add the "brother" as a child. + if (isHorizontal()) { + // If there is only one empty child, remove it before adding the layout. + if (numberOfChildren() == 1 && editableChild(0)->isEmpty()) { + removeChildAtIndex(0, true); } if (cursor->position() == ExpressionLayoutCursor::Position::Left) { - replaceWithJuxtapositionOf(brother, this, false); + addChildAtIndex(brother, 0); return; } assert(cursor->position() == ExpressionLayoutCursor::Position::Right); - replaceWithJuxtapositionOf(this, brother, false); + addChildAtIndex(brother, numberOfChildren()); return; } - // If there is no parent, the pointed layout is the main horizontal layout. - // Add the "brother" as a child. - // If there is only one empty child, remove it before adding the layout. - if (numberOfChildren() == 1 && editableChild(0)->isEmpty()) { - removeChildAtIndex(0, true); + + // If the layout is not horizontal, it must have a parent. + assert(m_parent); + int brotherIndex = cursor->position() == ExpressionLayoutCursor::Position::Left ? m_parent->indexOfChild(this) : m_parent->indexOfChild(this) + 1; + if (m_parent->isHorizontal()) { + static_cast(m_parent)->addOrMergeChildAtIndex(brother, brotherIndex); + return; } if (cursor->position() == ExpressionLayoutCursor::Position::Left) { - addChildAtIndex(brother, 0); + replaceWithJuxtapositionOf(brother, this, false); return; } assert(cursor->position() == ExpressionLayoutCursor::Position::Right); - addChildAtIndex(brother, numberOfChildren()); + replaceWithJuxtapositionOf(this, brother, false); return; }