From 11c39b6206da8ff2c079fc59c8acb3691738966a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 19 May 2020 14:17:26 +0200 Subject: [PATCH] [apps/calculation] Calculation::height has two types of margin arguments One for the margin between layouts and on top / at the bottom of the cell, the other for the margin surrounding each layout. --- .../illustrated_list_controller.cpp | 2 +- .../scrollable_three_expressions_cell.h | 3 ++- apps/calculation/calculation.cpp | 22 +++++++++++-------- apps/calculation/calculation.h | 2 +- apps/calculation/history_controller.cpp | 8 ++++++- apps/calculation/history_view_cell.cpp | 4 ++-- apps/calculation/history_view_cell.h | 4 ++-- .../scrollable_multiple_expressions_view.cpp | 20 ++++++++++------- .../scrollable_multiple_expressions_view.h | 10 +++++++++ 9 files changed, 50 insertions(+), 25 deletions(-) diff --git a/apps/calculation/additional_outputs/illustrated_list_controller.cpp b/apps/calculation/additional_outputs/illustrated_list_controller.cpp index e8b3fbb8d..ba792c161 100644 --- a/apps/calculation/additional_outputs/illustrated_list_controller.cpp +++ b/apps/calculation/additional_outputs/illustrated_list_controller.cpp @@ -78,7 +78,7 @@ KDCoordinate IllustratedListController::rowHeight(int j) { return 0; } Shared::ExpiringPointer calculation = m_calculationStore.calculationAtIndex(calculationIndex); - return calculation->height(App::app()->localContext(), Metric::CommonSmallMargin, true, true) + Metric::CellSeparatorThickness; //TODO LEA + return calculation->height(App::app()->localContext(), Metric::CommonSmallMargin, ScrollableThreeExpressionsView::k_margin, true, true) + Metric::CellSeparatorThickness; } int IllustratedListController::typeAtLocation(int i, int j) { diff --git a/apps/calculation/additional_outputs/scrollable_three_expressions_cell.h b/apps/calculation/additional_outputs/scrollable_three_expressions_cell.h index 42c18becb..a7836d7f7 100644 --- a/apps/calculation/additional_outputs/scrollable_three_expressions_cell.h +++ b/apps/calculation/additional_outputs/scrollable_three_expressions_cell.h @@ -10,8 +10,9 @@ namespace Calculation { class ScrollableThreeExpressionsView : public Shared::AbstractScrollableMultipleExpressionsView { public: + static constexpr KDCoordinate k_margin = Metric::CommonSmallMargin; ScrollableThreeExpressionsView(Responder * parentResponder) : Shared::AbstractScrollableMultipleExpressionsView(parentResponder, &m_contentCell), m_contentCell() { - setMargins(Metric::CommonSmallMargin, Metric::CommonSmallMargin, Metric::CommonSmallMargin, Metric::CommonSmallMargin); // Left Right margins are already added by TableCell + setMargins(k_margin, k_margin, k_margin, k_margin); // Left Right margins are already added by TableCell setBackgroundColor(KDColorWhite); } void resetMemoization(); diff --git a/apps/calculation/calculation.cpp b/apps/calculation/calculation.cpp index 0ec02edb3..3e8d69107 100644 --- a/apps/calculation/calculation.cpp +++ b/apps/calculation/calculation.cpp @@ -1,5 +1,6 @@ #include "calculation.h" #include "../shared/poincare_helpers.h" +#include "../shared/scrollable_multiple_expressions_view.h" #include "../global_preferences.h" #include "../exam_mode_configuration.h" #include @@ -124,7 +125,7 @@ Layout Calculation::createApproximateOutputLayout(Context * context, bool * coul } } -KDCoordinate Calculation::height(Context * context, float verticalMargin, bool expanded, bool forceSingleLine, CanBeSingleLineFunction canBeSingleLine) { +KDCoordinate Calculation::height(Context * context, KDCoordinate verticalMarginBetweenLayouts, KDCoordinate verticalMarginAroundLayouts, bool expanded, bool forceSingleLine, CanBeSingleLineFunction canBeSingleLine) { KDCoordinate result = expanded ? m_expandedHeight : m_height; if (result >= 0) { // Height already computed @@ -161,9 +162,10 @@ KDCoordinate Calculation::height(Context * context, float verticalMargin, bool e if (singleLine) { KDCoordinate exactOutputBaseline = exactLayout.baseline(); result = std::max(inputBaseline, exactOutputBaseline) // Above the baseline - + std::max(inputHeight - inputBaseline, exactOutputHeight - exactOutputBaseline); // Below the baseline + + std::max(inputHeight - inputBaseline, exactOutputHeight - exactOutputBaseline) // Below the baseline + + 2 * verticalMarginAroundLayouts; } else { - result = inputHeight + verticalMargin + exactOutputHeight; + result = inputHeight + verticalMarginBetweenLayouts + exactOutputHeight + 4 * verticalMarginAroundLayouts; } } else { // Create the approximate output layout @@ -192,9 +194,10 @@ KDCoordinate Calculation::height(Context * context, float verticalMargin, bool e if (singleLine) { KDCoordinate approximateOutputBaseline = approximateLayout.baseline(); result = std::max(inputBaseline, approximateOutputBaseline) // Above the baseline - + std::max(inputHeight - inputBaseline, approximateOutputHeight - approximateOutputBaseline); // Below the baseline + + std::max(inputHeight - inputBaseline, approximateOutputHeight - approximateOutputBaseline) // Below the baseline + + 2 * verticalMarginAroundLayouts; } else { - result = inputHeight + verticalMargin + approximateOutputHeight; + result = inputHeight + verticalMarginBetweenLayouts + approximateOutputHeight + 4 * verticalMarginAroundLayouts; } } else { assert(displayOutput(context) == DisplayOutput::ExactAndApproximate || (displayOutput(context) == DisplayOutput::ExactAndApproximateToggle && expanded)); @@ -203,22 +206,23 @@ KDCoordinate Calculation::height(Context * context, float verticalMargin, bool e KDCoordinate exactOutputWidth = exactLayout.layoutSize().width(); KDCoordinate approximateOutputWidth = approximateLayout.layoutSize().width(); KDCoordinate approximateOutputBaseline = approximateLayout.baseline(); - bool singleLine = forceSingleLine || canBeSingleLine(inputWidth, exactOutputWidth + /*TODO LEA equal sign " = "*/ + approximateOutputWidth); + bool singleLine = forceSingleLine || canBeSingleLine(inputWidth, exactOutputWidth + AbstractScrollableMultipleExpressionsView::StandardApproximateViewAndMarginsSize() + approximateOutputWidth); if (singleLine) { result = std::max(inputBaseline, std::max(exactOutputBaseline, approximateOutputBaseline)) // Above the baseline + std::max(inputHeight - inputBaseline, // Beloxw the baseline std::max(exactOutputHeight - exactOutputBaseline, - approximateOutputHeight - approximateOutputBaseline)); + approximateOutputHeight - approximateOutputBaseline)) + + 2 * verticalMarginAroundLayouts; } else { KDCoordinate outputHeight = std::max(exactOutputBaseline, approximateOutputBaseline) // Above the baseline + std::max(exactOutputHeight - exactOutputBaseline, approximateOutputHeight - approximateOutputBaseline); // Below the baseline - result = inputHeight + verticalMargin + outputHeight; + result = inputHeight + verticalMarginBetweenLayouts + outputHeight + 4 * verticalMarginAroundLayouts; } } } // Add the top and bottom margins - result += 2 * verticalMargin; + result += 2 * verticalMarginBetweenLayouts; /* For all display outputs except ExactAndApproximateToggle, the selected * height and the usual height are identical. We update both heights in diff --git a/apps/calculation/calculation.h b/apps/calculation/calculation.h index ec7dc3895..a1685131c 100644 --- a/apps/calculation/calculation.h +++ b/apps/calculation/calculation.h @@ -85,7 +85,7 @@ public: // Memoization of height typedef bool (*CanBeSingleLineFunction)(KDCoordinate inputWidth, KDCoordinate outputWidth); - KDCoordinate height(Poincare::Context * context, float verticalMargin, bool expanded, bool forceSingleLine, CanBeSingleLineFunction canbeSingleLine = [](KDCoordinate inputWidth, KDCoordinate outputWidth) { assert(false); return true; }); + KDCoordinate height(Poincare::Context * context, KDCoordinate verticalMarginBetweenLayouts, KDCoordinate verticalMarginAroundLayouts, bool expanded, bool forceSingleLine, CanBeSingleLineFunction canbeSingleLine = [](KDCoordinate inputWidth, KDCoordinate outputWidth) { assert(false); return true; }); // Displayed output DisplayOutput displayOutput(Poincare::Context * context); diff --git a/apps/calculation/history_controller.cpp b/apps/calculation/history_controller.cpp index 18669503f..005847dd8 100644 --- a/apps/calculation/history_controller.cpp +++ b/apps/calculation/history_controller.cpp @@ -208,7 +208,13 @@ KDCoordinate HistoryController::rowHeight(int j) { return 0; } Shared::ExpiringPointer calculation = calculationAtIndex(j); - return calculation->height(App::app()->localContext(), HistoryViewCell::k_verticalMargin, j == selectedRow() && selectedSubviewType() == SubviewType::Output, false, &HistoryViewCell::CanBeSingleLine); + return calculation->height( + App::app()->localContext(), + HistoryViewCell::k_margin, + HistoryViewCell::k_inputOutputViewsVerticalMargin, + j == selectedRow() && selectedSubviewType() == SubviewType::Output, + false, + &HistoryViewCell::CanBeSingleLine); } int HistoryController::typeAtLocation(int i, int j) { diff --git a/apps/calculation/history_view_cell.cpp b/apps/calculation/history_view_cell.cpp index 8ea49b677..8c7c5cdaa 100644 --- a/apps/calculation/history_view_cell.cpp +++ b/apps/calculation/history_view_cell.cpp @@ -42,7 +42,7 @@ HistoryViewCell::HistoryViewCell(Responder * parentResponder) : m_calculationDisplayOutput(Calculation::DisplayOutput::Unknown), m_calculationAdditionInformation(Calculation::AdditionalInformationType::None), m_calculationExpanded(false), - m_inputView(this, Metric::CommonLargeMargin, Metric::CommonSmallMargin), + m_inputView(this, Metric::CommonLargeMargin, k_inputOutputViewsVerticalMargin), m_scrollableOutputView(this) { m_calculationCRC32 = 0; @@ -171,7 +171,7 @@ View * HistoryViewCell::subviewAtIndex(int index) { bool HistoryViewCell::CanBeSingleLine(KDCoordinate inputWidth, KDCoordinate outputWidth) { // k_margin is the separation between the input and output. - return (inputWidth + k_margin + outputWidth) < (Ion::Display::Width - Metric::EllipsisCellWidth - 2 * k_margin); + return (inputWidth + k_margin + outputWidth) < (Ion::Display::Width - Metric::EllipsisCellWidth - 2 * k_margin); //TODO LEA -2 ?? } void HistoryViewCell::layoutSubviews(bool force) { diff --git a/apps/calculation/history_view_cell.h b/apps/calculation/history_view_cell.h index a47b43a4d..d4a4b00e6 100644 --- a/apps/calculation/history_view_cell.h +++ b/apps/calculation/history_view_cell.h @@ -31,7 +31,8 @@ private: class HistoryViewCell : public ::EvenOddCell, public Responder { public: - constexpr static KDCoordinate k_verticalMargin = Metric::CommonLargeMargin; //TODO LEA same as k_horizontalMargin? + constexpr static KDCoordinate k_margin = Metric::CommonSmallMargin; + constexpr static KDCoordinate k_inputOutputViewsVerticalMargin = k_margin; HistoryViewCell(Responder * parentResponder = nullptr); static bool CanBeSingleLine(KDCoordinate inputWidth, KDCoordinate outputWidth); void cellDidSelectSubview(HistoryViewCellDataSource::SubviewType type, HistoryViewCellDataSource::SubviewType previousType = HistoryViewCellDataSource::SubviewType::None); @@ -54,7 +55,6 @@ public: Shared::ScrollableTwoExpressionsView * outputView(); Calculation::AdditionalInformationType additionalInformationType() const { return m_calculationAdditionInformation; } private: - constexpr static KDCoordinate k_margin = Metric::CommonSmallMargin; constexpr static KDCoordinate k_resultWidth = 80; void reloadScroll(); void reloadOutputSelection(HistoryViewCellDataSource::SubviewType previousType); diff --git a/apps/shared/scrollable_multiple_expressions_view.cpp b/apps/shared/scrollable_multiple_expressions_view.cpp index 044fad871..3a4c1b652 100644 --- a/apps/shared/scrollable_multiple_expressions_view.cpp +++ b/apps/shared/scrollable_multiple_expressions_view.cpp @@ -8,7 +8,7 @@ namespace Shared { AbstractScrollableMultipleExpressionsView::ContentCell::ContentCell() : m_rightExpressionView(), - m_approximateSign(KDFont::LargeFont, I18n::Message::AlmostEqual, 0.5f, 0.5f, Palette::GreyVeryDark), + m_approximateSign(k_font, k_defaultApproximateMessage, 0.5f, 0.5f, Palette::GreyVeryDark), m_centeredExpressionView(), m_selectedSubviewPosition(SubviewPosition::Center), m_displayCenter(true) @@ -66,13 +66,13 @@ KDSize AbstractScrollableMultipleExpressionsView::ContentCell::minimalSizeForOpt KDSize leftSize = KDSizeZero; if (leftExpressionView() && !leftExpressionView()->layout().isUninitialized()) { leftSize = leftExpressionView()->minimalSizeForOptimalDisplay(); - width += leftSize.width() + Metric::CommonLargeMargin; + width += leftSize.width() + k_horizontalMargin; } KDSize centerSize = KDSizeZero; if (displayCenter()) { centerSize = m_centeredExpressionView.minimalSizeForOptimalDisplay(); - width += centerSize.width() + 2 * Metric::CommonLargeMargin + m_approximateSign.minimalSizeForOptimalDisplay().width(); + width += centerSize.width() + 2 * k_horizontalMargin + m_approximateSign.minimalSizeForOptimalDisplay().width(); } KDSize rightSize = m_rightExpressionView.minimalSizeForOptimalDisplay(); @@ -156,6 +156,10 @@ View * AbstractScrollableMultipleExpressionsView::ContentCell::subviewAtIndex(in return views[index - leftIsVisible]; } +KDCoordinate AbstractScrollableMultipleExpressionsView::ContentCell::StandardApproximateViewAndMarginsSize() { + return 2 * k_horizontalMargin + k_font->stringSize(I18n::translate(k_defaultApproximateMessage)).width(); //TODO LEA +} + void AbstractScrollableMultipleExpressionsView::ContentCell::layoutSubviews(bool force) { // Subviews sizes KDSize leftSize = leftExpressionView() ? leftExpressionView()->minimalSizeForOptimalDisplay() : KDSizeZero; @@ -172,16 +176,16 @@ void AbstractScrollableMultipleExpressionsView::ContentCell::layoutSubviews(bool KDCoordinate currentWidth = 0; if (leftExpressionView()) { leftExpressionView()->setFrame(KDRect(currentWidth, viewBaseline - leftBaseline, leftSize), force); - currentWidth += leftSize.width() + Metric::CommonLargeMargin; + currentWidth += leftSize.width() + k_horizontalMargin; } // Layout center expression if (displayCenter()) { KDSize approximateSignSize = m_approximateSign.minimalSizeForOptimalDisplay(); m_centeredExpressionView.setFrame(KDRect(currentWidth, viewBaseline - centerBaseline, centerSize), force); - currentWidth += Metric::CommonLargeMargin + centerSize.width(); + currentWidth += k_horizontalMargin + centerSize.width(); m_approximateSign.setFrame(KDRect(currentWidth, viewBaseline - approximateSignSize.height()/2, approximateSignSize), force); - currentWidth += Metric::CommonLargeMargin + approximateSignSize.width(); + currentWidth += k_horizontalMargin + approximateSignSize.width(); } // Layout right expression @@ -243,8 +247,8 @@ bool AbstractScrollableMultipleExpressionsView::handleEvent(Ion::Events::Event e if (contentCell()->displayCenter()) { KDCoordinate centerExpressionWidth = contentCell()->centeredExpressionView()->minimalSizeForOptimalDisplay().width(); KDCoordinate signWidth = contentCell()->approximateSign()->minimalSizeForOptimalDisplay().width(); - centeredExpressionIsVisibleOnTheLeft = leftWidth + Metric::CommonLargeMargin + centerExpressionWidth - contentOffset().x() > 0; - centeredExpressionIsVisibleOnTheRight = minimalSizeForOptimalDisplay().width() - rightExpressionWidth - signWidth - centerExpressionWidth - 2*Metric::CommonLargeMargin - contentOffset().x() < bounds().width(); + centeredExpressionIsVisibleOnTheLeft = leftWidth + ContentCell::k_horizontalMargin + centerExpressionWidth - contentOffset().x() > 0; + centeredExpressionIsVisibleOnTheRight = minimalSizeForOptimalDisplay().width() - rightExpressionWidth - signWidth - centerExpressionWidth - 2 * ContentCell::k_horizontalMargin - contentOffset().x() < bounds().width(); } // Select center if ((event == Ion::Events::Left && selectedSubviewPosition() == SubviewPosition::Right && centeredExpressionIsVisibleOnTheLeft) || diff --git a/apps/shared/scrollable_multiple_expressions_view.h b/apps/shared/scrollable_multiple_expressions_view.h index d95d209fc..26eb85386 100644 --- a/apps/shared/scrollable_multiple_expressions_view.h +++ b/apps/shared/scrollable_multiple_expressions_view.h @@ -2,6 +2,7 @@ #define SHARED_SCROLLABLE_MULTIPLE_EXPRESSIONS_VIEW_H #include +#include namespace Shared { @@ -12,6 +13,11 @@ public: Center = 1, Right = 2 }; + + static KDCoordinate StandardApproximateViewAndMarginsSize() { + return ContentCell::StandardApproximateViewAndMarginsSize(); + } + AbstractScrollableMultipleExpressionsView(Responder * parentResponder, View * contentCell); ::EvenOddCell * evenOddCell() { return contentCell(); @@ -33,6 +39,8 @@ public: protected: class ContentCell : public ::EvenOddCell { public: + static KDCoordinate StandardApproximateViewAndMarginsSize(); + constexpr static KDCoordinate k_horizontalMargin = Metric::CommonLargeMargin; ContentCell(); KDColor backgroundColor() const override; void setHighlighted(bool highlight) override; @@ -60,6 +68,8 @@ protected: virtual Poincare::Layout layout() const override; KDCoordinate baseline(KDCoordinate * leftBaseline = nullptr, KDCoordinate * centerBaseline = nullptr, KDCoordinate * rightBaseline = nullptr) const; private: + constexpr static const KDFont * k_font = KDFont::LargeFont; + const static I18n::Message k_defaultApproximateMessage = I18n::Message::AlmostEqual; View * subviewAtIndex(int index) override; ExpressionView m_rightExpressionView; MessageTextView m_approximateSign;