diff --git a/poincare/include/poincare/layout_cursor.h b/poincare/include/poincare/layout_cursor.h index fc35d6610..761649306 100644 --- a/poincare/include/poincare/layout_cursor.h +++ b/poincare/include/poincare/layout_cursor.h @@ -8,6 +8,7 @@ namespace Poincare { class HorizontalLayoutNode; +class VerticalOffsetLayoutRef; class LayoutCursor { template @@ -108,7 +109,7 @@ public: /* Layout modification */ void addEmptyExponentialLayout() {} //TODO void addEmptyMatrixLayout() {} //TODO - void addEmptyPowerLayout() {} //TODO + void addEmptyPowerLayout(); void addEmptySquareRootLayout() {} //TODO void addEmptySquarePowerLayout(); void addEmptyTenPowerLayout() {} //TODO @@ -137,6 +138,7 @@ public: private: constexpr static KDCoordinate k_cursorHeight = 18; KDCoordinate layoutHeight(); + void privateAddEmptyPowerLayout(VerticalOffsetLayoutRef v); bool baseForNewPowerLayout(); bool privateShowHideEmptyLayoutIfNeeded(bool show); LayoutRef m_layoutRef; diff --git a/poincare/src/layout_cursor.cpp b/poincare/src/layout_cursor.cpp index 1d1deff58..abbf723d0 100644 --- a/poincare/src/layout_cursor.cpp +++ b/poincare/src/layout_cursor.cpp @@ -65,18 +65,17 @@ void LayoutCursor::moveUnder(bool * shouldRecomputeLayout) { /* Layout modification */ -void LayoutCursor::addEmptySquarePowerLayout() { - CharLayoutRef indiceLayout = CharLayoutRef('2'); - VerticalOffsetLayoutRef offsetLayout = VerticalOffsetLayoutRef(indiceLayout, VerticalOffsetLayoutNode::Type::Superscript); - // If there is already a base - if (baseForNewPowerLayout()) { - m_layoutRef.addSibling(this, offsetLayout, true); - return; +void LayoutCursor::addEmptyPowerLayout() { + VerticalOffsetLayoutRef offsetLayout = VerticalOffsetLayoutRef(EmptyLayoutRef(), VerticalOffsetLayoutNode::Type::Superscript); + privateAddEmptyPowerLayout(offsetLayout); + if (offsetLayout.parent().isDefined() && offsetLayout.numberOfChildren() == 1 && !offsetLayout.childAtIndex(0).isAllocationFailure()) { + m_layoutRef = offsetLayout.childAtIndex(0); } - // Else, add an empty base - EmptyLayoutRef child1 = EmptyLayoutRef(); - HorizontalLayoutRef newChild = HorizontalLayoutRef(child1, offsetLayout); - m_layoutRef.addSibling(this, newChild, true); +} + +void LayoutCursor::addEmptySquarePowerLayout() { + VerticalOffsetLayoutRef offsetLayout = VerticalOffsetLayoutRef(CharLayoutRef('2'), VerticalOffsetLayoutNode::Type::Superscript); + privateAddEmptyPowerLayout(offsetLayout); } void LayoutCursor::addXNTCharLayout() { @@ -170,6 +169,18 @@ KDCoordinate LayoutCursor::layoutHeight() { } +void LayoutCursor::privateAddEmptyPowerLayout(VerticalOffsetLayoutRef v) { + // If there is already a base + if (baseForNewPowerLayout()) { + m_layoutRef.addSibling(this, v, true); + return; + } + // Else, add an empty base + EmptyLayoutRef e = EmptyLayoutRef(); + HorizontalLayoutRef newChild = HorizontalLayoutRef(e, v); + m_layoutRef.addSibling(this, newChild, true); +} + bool LayoutCursor::baseForNewPowerLayout() { /* Returns true if the layout on the left of the pointed layout is suitable to * be the base of a new power layout: the base layout should be anything but