From f3c6aab66958d4922e9dee2a2d5bfa06a2018d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 3 Jan 2020 12:40:39 +0100 Subject: [PATCH] [apps/calculation] HistoryViewCell: when reloading the entire table due to a cell selection, the cell used for the selected row might change. We thereby have to update the selected cell once the table has been reloaded. This fixes the following bug: add 5 times the calculation "12.2". Go up, the selected expression is the left one instead of the right one. --- apps/calculation/history_controller.cpp | 11 ++++++----- apps/calculation/history_controller.h | 2 +- apps/calculation/history_view_cell.cpp | 6 +++--- apps/calculation/history_view_cell.h | 5 +++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/apps/calculation/history_controller.cpp b/apps/calculation/history_controller.cpp index ff8025660..2f300aaef 100644 --- a/apps/calculation/history_controller.cpp +++ b/apps/calculation/history_controller.cpp @@ -112,13 +112,12 @@ void HistoryController::tableViewDidChangeSelection(SelectableTableView * t, int if (withinTemporarySelection || previousSelectedCellY == selectedRow()) { return; } - HistoryViewCell * cell = static_cast(t->selectedCell()); if (previousSelectedCellY == -1) { - setSelectedSubviewType(SubviewType::Output, cell); + setSelectedSubviewType(SubviewType::Output); } else if (selectedRow() < previousSelectedCellY) { - setSelectedSubviewType(SubviewType::Output, cell); + setSelectedSubviewType(SubviewType::Output); } else if (selectedRow() > previousSelectedCellY) { - setSelectedSubviewType(SubviewType::Input, cell); + setSelectedSubviewType(SubviewType::Input); } HistoryViewCell * selectedCell = (HistoryViewCell *)(t->selectedCell()); if (selectedCell == nullptr) { @@ -166,10 +165,12 @@ void HistoryController::scrollToCell(int i, int j) { m_selectableTableView.scrollToCell(i, j); } -void HistoryController::historyViewCellDidChangeSelection() { +HistoryViewCell * HistoryController::historyViewCellDidChangeSelection() { /* Update the whole table as the height of the selected cell row might have * changed. */ m_selectableTableView.reloadData(); + // Return the selected cell if one + return static_cast(m_selectableTableView.selectedCell()); } } diff --git a/apps/calculation/history_controller.h b/apps/calculation/history_controller.h index f4d1d61fb..f75c20015 100644 --- a/apps/calculation/history_controller.h +++ b/apps/calculation/history_controller.h @@ -30,7 +30,7 @@ private: int storeIndex(int i) { return numberOfRows() - i - 1; } Shared::ExpiringPointer calculationAtIndex(int i); CalculationSelectableTableView * selectableTableView(); - void historyViewCellDidChangeSelection() override; + HistoryViewCell * historyViewCellDidChangeSelection() override; constexpr static int k_maxNumberOfDisplayedRows = 5; CalculationSelectableTableView m_selectableTableView; HistoryViewCell m_calculationHistory[k_maxNumberOfDisplayedRows]; diff --git a/apps/calculation/history_view_cell.cpp b/apps/calculation/history_view_cell.cpp index a28d2c477..e7e6c8d8a 100644 --- a/apps/calculation/history_view_cell.cpp +++ b/apps/calculation/history_view_cell.cpp @@ -15,13 +15,13 @@ static inline KDCoordinate maxCoordinate(KDCoordinate x, KDCoordinate y) { retur HistoryViewCellDataSource::HistoryViewCellDataSource() : m_selectedSubviewType(SubviewType::Output) {} -void HistoryViewCellDataSource::setSelectedSubviewType(SubviewType subviewType, HistoryViewCell * cell) { +void HistoryViewCellDataSource::setSelectedSubviewType(SubviewType subviewType) { m_selectedSubviewType = subviewType; + HistoryViewCell * cell = historyViewCellDidChangeSelection(); if (cell) { cell->setHighlighted(cell->isHighlighted()); cell->cellDidSelectSubview(subviewType); } - historyViewCellDidChangeSelection(); } /* HistoryViewCell */ @@ -185,7 +185,7 @@ bool HistoryViewCell::handleEvent(Ion::Events::Event event) { 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; - m_dataSource->setSelectedSubviewType(otherSubviewType, this); + m_dataSource->setSelectedSubviewType(otherSubviewType); CalculationSelectableTableView * tableView = (CalculationSelectableTableView *)parentResponder(); tableView->scrollToSubviewOfTypeOfCellAtLocation(otherSubviewType, tableView->selectedColumn(), tableView->selectedRow()); Container::activeApp()->setFirstResponder(this); diff --git a/apps/calculation/history_view_cell.h b/apps/calculation/history_view_cell.h index e32572c42..e1c0f099d 100644 --- a/apps/calculation/history_view_cell.h +++ b/apps/calculation/history_view_cell.h @@ -17,13 +17,14 @@ public: Output }; HistoryViewCellDataSource(); - void setSelectedSubviewType(SubviewType subviewType, HistoryViewCell * cell = nullptr); + void setSelectedSubviewType(SubviewType subviewType); SubviewType selectedSubviewType() { return m_selectedSubviewType; } private: /* This method should belong to a delegate instead of a data source but as * both the data source and the delegate will be the same controller, we * avoid keeping 2 pointers in HistoryViewCell. */ - virtual void historyViewCellDidChangeSelection() = 0; + // It returns the selected cell at the end of the method + virtual HistoryViewCell * historyViewCellDidChangeSelection() = 0; SubviewType m_selectedSubviewType; };