diff --git a/apps/regression/graph_controller.cpp b/apps/regression/graph_controller.cpp index a6934e328..5344fa600 100644 --- a/apps/regression/graph_controller.cpp +++ b/apps/regression/graph_controller.cpp @@ -2,6 +2,7 @@ #include "../shared/poincare_helpers.h" #include "../shared/text_helpers.h" #include "../apps_container.h" +#include "../shared/poincare_helpers.h" #include #include #include @@ -259,9 +260,9 @@ void GraphController::initCursorParameters() { *m_selectedDotIndex = m_store->numberOfPairsOfSeries(*m_selectedSeriesIndex); } -bool GraphController::isCursorHanging() { +bool GraphController::cursorMatchesModel() { if (m_store->seriesIsEmpty(*m_selectedSeriesIndex)) { - return true; + return false; } Coordinate2D xy; if (*m_selectedDotIndex == -1) { @@ -271,9 +272,7 @@ bool GraphController::isCursorHanging() { } else { xy = Coordinate2D(m_store->get(*m_selectedSeriesIndex, 0, *m_selectedDotIndex), m_store->get(*m_selectedSeriesIndex, 1, *m_selectedDotIndex)); } - // NaN != Nan returns true, but cursor is not hanging if both values are NaN - return (xy.x1() != m_cursor->x() && !(std::isnan(xy.x1()) && std::isnan(m_cursor->x()))) - || (xy.x2() != m_cursor->y() && !(std::isnan(xy.x2()) && std::isnan(m_cursor->y()))); + return PoincareHelpers::equalOrBothNan(xy.x1(), m_cursor->x()) && PoincareHelpers::equalOrBothNan(xy.x2(), m_cursor->y()); } bool GraphController::moveCursorVertically(int direction) { diff --git a/apps/regression/graph_controller.h b/apps/regression/graph_controller.h index 622ac76bc..570c91ff5 100644 --- a/apps/regression/graph_controller.h +++ b/apps/regression/graph_controller.h @@ -40,7 +40,7 @@ private: // InteractiveCurveViewController void initCursorParameters() override; - bool isCursorHanging() override; + bool cursorMatchesModel() override; uint32_t rangeVersion() override; int selectedCurveIndex() const override { return *m_selectedSeriesIndex; } bool closestCurveIndexIsSuitable(int newIndex, int currentIndex) const override; diff --git a/apps/shared/function_graph_controller.cpp b/apps/shared/function_graph_controller.cpp index 528013d4d..f43985e9f 100644 --- a/apps/shared/function_graph_controller.cpp +++ b/apps/shared/function_graph_controller.cpp @@ -1,5 +1,6 @@ #include "function_graph_controller.h" #include "function_app.h" +#include "poincare_helpers.h" #include "../apps_container.h" #include #include @@ -123,16 +124,14 @@ bool FunctionGraphController::moveCursorVertically(int direction) { return true; } -bool FunctionGraphController::isCursorHanging() { +bool FunctionGraphController::cursorMatchesModel() { Poincare::Context * context = textFieldDelegateApp()->localContext(); if (indexFunctionSelectedByCursor() >= functionStore()->numberOfActiveFunctions()) { - return true; + return false; } ExpiringPointer f = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(indexFunctionSelectedByCursor())); Coordinate2D xy = f->evaluateXYAtParameter(m_cursor->t(), context); - // NaN != Nan returns true, but cursor is not hanging if both values are NaN - return (xy.x1() != m_cursor->x() && !(std::isnan(xy.x1()) && std::isnan(m_cursor->x()))) - || (xy.x2() != m_cursor->y() && !(std::isnan(xy.x2()) && std::isnan(m_cursor->y()))); + return PoincareHelpers::equalOrBothNan(xy.x1(), m_cursor->x()) && PoincareHelpers::equalOrBothNan(xy.x2(), m_cursor->y()); } CurveView * FunctionGraphController::curveView() { diff --git a/apps/shared/function_graph_controller.h b/apps/shared/function_graph_controller.h index c78086b96..cac996fe8 100644 --- a/apps/shared/function_graph_controller.h +++ b/apps/shared/function_graph_controller.h @@ -37,7 +37,7 @@ protected: Poincare::Coordinate2D xyValues(int curveIndex, double t, Poincare::Context * context) const override; int numberOfCurves() const override; void initCursorParameters() override; - bool isCursorHanging() override; + bool cursorMatchesModel() override; CurveView * curveView() override; void yRangeForCursorFirstMove(Shared::InteractiveCurveViewRange * range) const; diff --git a/apps/shared/interactive_curve_view_controller.cpp b/apps/shared/interactive_curve_view_controller.cpp index 59492e4d0..30d77a3ce 100644 --- a/apps/shared/interactive_curve_view_controller.cpp +++ b/apps/shared/interactive_curve_view_controller.cpp @@ -136,7 +136,7 @@ void InteractiveCurveViewController::viewWillAppear() { /* Warning: init cursor parameter before reloading banner view. Indeed, * reloading banner view needs an updated cursor to load the right data. */ uint32_t newRangeVersion = rangeVersion(); - if ((*m_rangeVersion != newRangeVersion && !isCursorVisible()) || isCursorHanging()) { + if ((*m_rangeVersion != newRangeVersion && !isCursorVisible()) || !cursorMatchesModel()) { initCursorParameters(); } *m_rangeVersion = newRangeVersion; diff --git a/apps/shared/interactive_curve_view_controller.h b/apps/shared/interactive_curve_view_controller.h index 7ca1b9d81..a64db50c0 100644 --- a/apps/shared/interactive_curve_view_controller.h +++ b/apps/shared/interactive_curve_view_controller.h @@ -41,8 +41,8 @@ protected: virtual bool moveCursorVertically(int direction) = 0; virtual uint32_t rangeVersion() = 0; bool isCursorVisible(); - // The cursor is hanging if the selected function has been edited or deleted - virtual bool isCursorHanging() = 0; + // The cursor does not match if selected model has been edited or deleted + virtual bool cursorMatchesModel() = 0; // Closest vertical curve helper int closestCurveIndexVertically(bool goingUp, int currentSelectedCurve, Poincare::Context * context) const; diff --git a/apps/shared/poincare_helpers.h b/apps/shared/poincare_helpers.h index 372c66add..254217321 100644 --- a/apps/shared/poincare_helpers.h +++ b/apps/shared/poincare_helpers.h @@ -108,6 +108,8 @@ inline typename Poincare::Coordinate2D NextIntersection(const Poincare:: return e.nextIntersection(symbol, start, step, max, context, complexFormat, preferences->angleUnit(), expression); } +inline bool equalOrBothNan(double a, double b) { return a == b || (std::isnan(a) && std::isnan(b)); } + } }