Fix problem with inactiveLoopCounter in RowScanner 58/170558/4
authorPawel Kurowski <p.kurowski2@samsung.com>
Tue, 20 Feb 2018 20:07:36 +0000 (21:07 +0100)
committerPawel Kurowski <p.kurowski2@samsung.com>
Thu, 1 Mar 2018 15:26:39 +0000 (16:26 +0100)
Slight refactor
getFirstElementIndex takes into account 'invertDirection' argument

Change-Id: Ic0b7b21de9e8086f9e61c20927d31f222f931ea1

src/RowScanner.cpp

index 7045a6f..89c39b4 100644 (file)
@@ -109,9 +109,9 @@ private:
        void forceRender();
        void eraseFrame();
 
-       void moveToNextPosition(bool invertDirection = false);
-       int getFirstElementIndex(const std::shared_ptr<NavigationElement> &parent);
-       int getIncrementForCalculatingNextElement(const std::shared_ptr<NavigationElement> &parent, bool reverse);
+       void moveToNextPosition(bool invertDirection = false, bool movedByAutoScan = false);
+       int getFirstElementIndex(const std::shared_ptr<NavigationElement> &parent, bool invertDirection = false);
+       int getIncrementForCalculatingNextElement(const std::shared_ptr<NavigationElement> &parent, bool invertDirection = false);
 
        ecore::Timer timer;
        evas::Shape frame;
@@ -246,22 +246,23 @@ void RowScannerImpl::prev()
 
 void RowScannerImpl::startAutoTraversingUI(bool firstTime)
 {
-       if (state == State::NO_ELEMENTS) return;
-
-       state = State::ACTIVE;
+       if (state == State::NO_ELEMENTS)
+               return;
 
        if (!properties.isAutoScanEnabled()) {
                timer.reset();
                return;
        }
 
+       state = State::ACTIVE;
+       inactiveLoopCounter = 0;
+
        auto firstElementTime = properties.getAutoScanInterval();
        firstElementTime += firstTime ? properties.getPauseOnFirstElementTime() : 0;
        timer.reset({firstElementTime, properties.getAutoScanInterval()}, [this]() {
-               moveToNextPosition();
+               moveToNextPosition(false, true);
                return timer.isSet() ? ecore::TimerRepetitionPolicy::renew : ecore::TimerRepetitionPolicy::cancel;
        });
-       inactiveLoopCounter = 0;
 }
 
 void RowScannerImpl::drawFrame(Rectangle dimensions, NavigationInterface::BoxPositionMode mode, Color color)
@@ -300,23 +301,19 @@ void RowScannerImpl::eraseFrame()
        elm_win_render(Singleton<UniversalSwitch>::instance().getMainWindow()->getHandler());
 }
 
-int RowScannerImpl::getFirstElementIndex(const std::shared_ptr<NavigationElement> &parent)
+int RowScannerImpl::getFirstElementIndex(const std::shared_ptr<NavigationElement> &parent, bool invertDirection)
 {
        auto count = static_cast<int>(parent->getChildren().size());
-       if (count <= 1) return 0;
-       int pos = 0;
+       if (count <= 1)
+               return 0;
 
-       auto dir = parent->getScanningDirection();
-       if (dir == NavigationElement::Direction::VERTICAL) {
+       if (parent->getScanningDirection() == NavigationElement::Direction::VERTICAL) {
                auto direction = properties.getDirectionVertical();
-               pos = direction == VerticalScanningDirection::TO_BOTTOM ? 0 : count - 1;
-       } else {
-               ASSERT(dir == NavigationElement::Direction::HORIZONTAL);
-
-               auto direction = properties.getDirectionHorizontal();
-               pos = direction == HorizontalScanningDirection::TO_RIGHT ? 0 : count - 1;
+               return (direction == VerticalScanningDirection::TO_BOTTOM) ^ invertDirection ? 0 : count - 1;
        }
-       return pos;
+
+       auto direction = properties.getDirectionHorizontal();
+       return (direction == HorizontalScanningDirection::TO_RIGHT) ^ invertDirection ? 0 : count - 1;
 }
 
 int RowScannerImpl::getIncrementForCalculatingNextElement(const std::shared_ptr<NavigationElement> &parent, bool invertDirection)
@@ -338,32 +335,39 @@ int RowScannerImpl::getIncrementForCalculatingNextElement(const std::shared_ptr<
        return direction == HorizontalScanningDirection::TO_RIGHT ? 1 : -1;
 }
 
-void RowScannerImpl::moveToNextPosition(bool invertDirection)
+void RowScannerImpl::moveToNextPosition(bool invertDirection, bool movedByAutoScan)
 {
-       if (state == State::NO_ELEMENTS) return;
+       if (state == State::NO_ELEMENTS)
+               return;
 
        auto count = static_cast<int>(currentNavState.parent->getChildren().size());
-       Optional<int> newPos;
+       if (count == 0) {
+               ERROR("State should be equal to NO_ELEMENTS");
+               state = State::NO_ELEMENTS;
+               return;
+       }
+
        if (currentNavState.childIndex) {
                auto increment = getIncrementForCalculatingNextElement(currentNavState.parent, invertDirection);
-               auto p = *currentNavState.childIndex + increment;
-               if (p >= 0 && p < count)
-                       newPos = p;
-       } else if (count > 0) {
-               newPos = getFirstElementIndex(currentNavState.parent);
-       }
-       if (!newPos) {
-               ++inactiveLoopCounter;
-               if (inactiveLoopCounter >= properties.getLoopLimitToInaction()) {
-                       timer.reset();
-                       eraseFrame();
-                       state = State::INACTIVE;
+               auto index = *currentNavState.childIndex + increment;
+               if (index >= 0 && index < count) {
+                       selectNewElement({currentNavState.parent, index});
+                       return;
+               }
+
+               if (currentNavState.parent != rootNavigationElement || properties.isEscapeFrameEnabled()) {
+                       selectNewElement({currentNavState.parent, {}});
                        return;
                }
        }
-       if (!newPos && !properties.isEscapeFrameEnabled() && currentNavState.parent == rootNavigationElement && count > 0) {
-               newPos = getFirstElementIndex(currentNavState.parent);
+
+       if (movedByAutoScan && ++inactiveLoopCounter >= properties.getLoopLimitToInaction()) {
+               timer.reset();
+               eraseFrame();
+               currentNavState.childIndex = {};
+               state = State::INACTIVE;
+               return;
        }
 
-       selectNewElement({ currentNavState.parent, newPos });
+       selectNewElement({currentNavState.parent, getFirstElementIndex(currentNavState.parent, invertDirection)});
 }