From 24a10a7807c24b691a8f0820b58c387e2e48aa37 Mon Sep 17 00:00:00 2001
From: Leon Styhre <leon@leonstyhre.com>
Date: Tue, 20 Sep 2022 21:16:39 +0200
Subject: [PATCH] Some improvements to the cursor logic in SystemView.

---
 es-app/src/views/SystemView.cpp                    | 13 +++++++++++--
 es-app/src/views/SystemView.h                      |  1 +
 es-core/src/components/primary/CarouselComponent.h |  5 ++++-
 es-core/src/components/primary/TextListComponent.h |  8 ++++++--
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/es-app/src/views/SystemView.cpp b/es-app/src/views/SystemView.cpp
index ac4138bad..790e249b1 100644
--- a/es-app/src/views/SystemView.cpp
+++ b/es-app/src/views/SystemView.cpp
@@ -25,6 +25,7 @@ SystemView::SystemView()
     : mRenderer {Renderer::getInstance()}
     , mPrimary {nullptr}
     , mPrimaryType {PrimaryType::CAROUSEL}
+    , mLastCursor {-1}
     , mCamOffset {0.0f}
     , mFadeOpacity {0.0f}
     , mPreviousScrollVelocity {0}
@@ -137,8 +138,10 @@ void SystemView::update(int deltaTime)
 
     mPrimary->update(deltaTime);
 
-    for (auto& video : mSystemElements[mPrimary->getCursor()].videoComponents)
-        video->update(deltaTime);
+    for (auto& video : mSystemElements[mPrimary->getCursor()].videoComponents) {
+        if (!isScrolling())
+            video->update(deltaTime);
+    }
 
     for (auto& anim : mSystemElements[mPrimary->getCursor()].lottieAnimComponents)
         anim->update(deltaTime);
@@ -213,6 +216,12 @@ void SystemView::onCursorChanged(const CursorState& state)
 {
     int cursor {mPrimary->getCursor()};
 
+    // Avoid double updates.
+    if (cursor == mLastCursor)
+        return;
+
+    mLastCursor = cursor;
+
     for (auto& selector : mSystemElements[cursor].gameSelectors) {
         if (selector->getGameSelection() == GameSelectorComponent::GameSelection::RANDOM)
             selector->setNeedsRefresh();
diff --git a/es-app/src/views/SystemView.h b/es-app/src/views/SystemView.h
index 73e5746be..2da756f7b 100644
--- a/es-app/src/views/SystemView.h
+++ b/es-app/src/views/SystemView.h
@@ -122,6 +122,7 @@ private:
     std::vector<SystemViewElements> mSystemElements;
     PrimaryComponent<SystemData*>* mPrimary;
     PrimaryType mPrimaryType;
+    int mLastCursor;
 
     // Dummy entry to keep the default SVG rating images in the texture cache so they don't
     // need to be re-rasterized for each gamelist that is loaded.
diff --git a/es-core/src/components/primary/CarouselComponent.h b/es-core/src/components/primary/CarouselComponent.h
index 92fdc7a8e..a21b292f4 100644
--- a/es-core/src/components/primary/CarouselComponent.h
+++ b/es-core/src/components/primary/CarouselComponent.h
@@ -491,8 +491,11 @@ template <typename T> bool CarouselComponent<T>::input(InputConfig* config, Inpu
         }
         else {
             if (config->isMappedLike("up", input) || config->isMappedLike("down", input) ||
-                config->isMappedLike("left", input) || config->isMappedLike("right", input))
+                config->isMappedLike("left", input) || config->isMappedLike("right", input)) {
+                if (isScrolling())
+                    onCursorChanged(CursorState::CURSOR_STOPPED);
                 List::listInput(0);
+            }
         }
     }
 
diff --git a/es-core/src/components/primary/TextListComponent.h b/es-core/src/components/primary/TextListComponent.h
index 2b0221ef8..910dcef56 100644
--- a/es-core/src/components/primary/TextListComponent.h
+++ b/es-core/src/components/primary/TextListComponent.h
@@ -268,10 +268,14 @@ template <typename T> bool TextListComponent<T>::input(InputConfig* config, Inpu
                 config->isMappedLike("rightshoulder", input) ||
                 config->isMappedLike("lefttrigger", input) ||
                 config->isMappedLike("righttrigger", input)) {
-                if constexpr (std::is_same_v<T, SystemData*>)
+                if constexpr (std::is_same_v<T, SystemData*>) {
+                    if (isScrolling())
+                        onCursorChanged(CursorState::CURSOR_STOPPED);
                     List::listInput(0);
-                else
+                }
+                else {
                     List::stopScrolling();
+                }
             }
         }
     }