Files
Upsilon/apps/calculation/history_view_cell.cpp
Émilie Feral 05a235803f [calculation] HistoryViewCell: fix scroll reloading and right or left
outputs selection (the order of events here matters)
2019-05-03 15:54:21 +02:00

181 lines
7.1 KiB
C++

#include "history_view_cell.h"
#include "app.h"
#include "../constant.h"
#include "selectable_table_view.h"
#include <assert.h>
#include <string.h>
namespace Calculation {
static inline KDCoordinate minCoordinate(KDCoordinate x, KDCoordinate y) { return x < y ? x : y; }
static inline KDCoordinate maxCoordinate(KDCoordinate x, KDCoordinate y) { return x > y ? x : y; }
/* HistoryViewCellDataSource */
HistoryViewCellDataSource::HistoryViewCellDataSource() :
m_selectedSubviewType(SubviewType::Output) {}
void HistoryViewCellDataSource::setSelectedSubviewType(SubviewType subviewType, HistoryViewCell * cell) {
m_selectedSubviewType = subviewType;
if (cell) {
cell->setHighlighted(cell->isHighlighted());
cell->cellDidSelectSubview(subviewType);
}
historyViewCellDidChangeSelection();
}
/* HistoryViewCell */
HistoryViewCell::HistoryViewCell(Responder * parentResponder) :
Responder(parentResponder),
m_calculation(),
m_calculationSelected(false),
m_inputView(this),
m_scrollableOutputView(this)
{
}
Shared::ScrollableExactApproximateExpressionsView * HistoryViewCell::outputView() {
return &m_scrollableOutputView;
}
void HistoryViewCell::setEven(bool even) {
EvenOddCell::setEven(even);
m_inputView.setBackgroundColor(backgroundColor());
m_scrollableOutputView.setBackgroundColor(backgroundColor());
m_scrollableOutputView.evenOddCell()->setEven(even);
}
void HistoryViewCell::setHighlighted(bool highlight) {
assert(m_dataSource);
m_highlighted = highlight;
m_inputView.setExpressionBackgroundColor(backgroundColor());
m_scrollableOutputView.evenOddCell()->setHighlighted(false);
if (isHighlighted()) {
if (m_dataSource->selectedSubviewType() == HistoryViewCellDataSource::SubviewType::Input) {
m_inputView.setExpressionBackgroundColor(Palette::Select);
} else {
m_scrollableOutputView.evenOddCell()->setHighlighted(true);
}
}
}
Poincare::Layout HistoryViewCell::layout() const {
assert(m_dataSource);
if (m_dataSource->selectedSubviewType() == HistoryViewCellDataSource::SubviewType::Input) {
return m_inputView.layout();
} else {
return m_scrollableOutputView.layout();
}
}
void HistoryViewCell::reloadCell() {
m_scrollableOutputView.evenOddCell()->reloadCell();
layoutSubviews();
// Reload subviews' scrolls
m_inputView.reloadScroll();
m_scrollableOutputView.reloadScroll();
}
void HistoryViewCell::cellDidSelectSubview(HistoryViewCellDataSource::SubviewType type) {
if (type == HistoryViewCellDataSource::SubviewType::Output) {
/* Select the right output according to the calculation display output. This
* will reload the scroll to display the selected output. */
App * calculationApp = (App *)app();
Calculation::DisplayOutput display = m_calculation.displayOutput(calculationApp->localContext());
if (display == Calculation::DisplayOutput::ExactAndApproximate) {
m_scrollableOutputView.setSelectedSubviewPosition(Shared::ScrollableExactApproximateExpressionsView::SubviewPosition::Left);
} else {
assert(display == Calculation::DisplayOutput::ApproximateOnly || display == Calculation::DisplayOutput::ExactAndApproximateToggle || display == Calculation::DisplayOutput::ExactOnly);
m_scrollableOutputView.setSelectedSubviewPosition(Shared::ScrollableExactApproximateExpressionsView::SubviewPosition::Right);
}
}
}
KDColor HistoryViewCell::backgroundColor() const {
KDColor background = m_even ? KDColorWhite : Palette::WallScreen;
return background;
}
int HistoryViewCell::numberOfSubviews() const {
return 2;
}
View * HistoryViewCell::subviewAtIndex(int index) {
View * views[2] = {&m_inputView, &m_scrollableOutputView};
return views[index];
}
void HistoryViewCell::layoutSubviews() {
KDCoordinate maxFrameWidth = bounds().width();
KDSize inputSize = m_inputView.minimalSizeForOptimalDisplay();
m_inputView.setFrame(KDRect(
0,
0,
minCoordinate(maxFrameWidth, inputSize.width()),
inputSize.height()
));
KDSize outputSize = m_scrollableOutputView.minimalSizeForOptimalDisplay();
m_scrollableOutputView.setFrame(KDRect(
maxCoordinate(0, maxFrameWidth - outputSize.width()),
inputSize.height(),
minCoordinate(maxFrameWidth, outputSize.width()),
bounds().height() - inputSize.height()
));
}
void HistoryViewCell::setCalculation(Calculation * calculation, bool isSelected) {
if (m_calculationSelected == isSelected && *calculation == m_calculation) {
return;
}
// Memoization
m_calculation = *calculation;
m_calculationSelected = isSelected;
App * calculationApp = (App *)app();
Calculation::DisplayOutput display = calculation->displayOutput(calculationApp->localContext());
m_inputView.setLayout(calculation->createInputLayout());
/* Both output expressions have to be updated at the same time. Otherwise,
* when updating one layout, if the second one still points to a deleted
* layout, calling to layoutSubviews() would fail. */
Poincare::Layout leftOutputLayout = Poincare::Layout();
Poincare::Layout rightOutputLayout;
if (display == Calculation::DisplayOutput::ExactOnly || (display == Calculation::DisplayOutput::ExactAndApproximateToggle && !isSelected && calculation->toggleDisplayExact())) {
rightOutputLayout = calculation->createExactOutputLayout();
} else {
rightOutputLayout = calculation->createApproximateOutputLayout(calculationApp->localContext());
if (display == Calculation::DisplayOutput::ExactAndApproximate || (display == Calculation::DisplayOutput::ExactAndApproximateToggle && isSelected)) {
leftOutputLayout = calculation->createExactOutputLayout();
}
}
m_scrollableOutputView.setLayouts(rightOutputLayout, leftOutputLayout);
I18n::Message equalMessage = calculation->exactAndApproximateDisplayedOutputsAreEqual(calculationApp->localContext()) == Calculation::EqualSign::Equal ? I18n::Message::Equal : I18n::Message::AlmostEqual;
m_scrollableOutputView.setEqualMessage(equalMessage);
}
void HistoryViewCell::didBecomeFirstResponder() {
assert(m_dataSource);
if (m_dataSource->selectedSubviewType() == HistoryViewCellDataSource::SubviewType::Input) {
app()->setFirstResponder(&m_inputView);
} else {
app()->setFirstResponder(&m_scrollableOutputView);
}
}
bool HistoryViewCell::handleEvent(Ion::Events::Event event) {
assert(m_dataSource);
if ((event == Ion::Events::Down && m_dataSource->selectedSubviewType() == HistoryViewCellDataSource::SubviewType::Input) ||
(event == Ion::Events::Up && m_dataSource->selectedSubviewType() == HistoryViewCellDataSource::SubviewType::Output)) {
HistoryViewCellDataSource::SubviewType otherSubviewType = m_dataSource->selectedSubviewType() == HistoryViewCellDataSource::SubviewType::Input ? HistoryViewCellDataSource::SubviewType::Output : HistoryViewCellDataSource::SubviewType::Input;
CalculationSelectableTableView * tableView = (CalculationSelectableTableView *)parentResponder();
tableView->scrollToSubviewOfTypeOfCellAtLocation(otherSubviewType, tableView->selectedColumn(), tableView->selectedRow());
m_dataSource->setSelectedSubviewType(otherSubviewType, this);
app()->setFirstResponder(this);
return true;
}
return false;
}
}