From 23b8b3cc9e6208becb6f88fc33e1f4f75369fc6a Mon Sep 17 00:00:00 2001
From: casey langen <casey.langen@gmail.com>
Date: Mon, 3 Apr 2017 20:55:34 -0700
Subject: [PATCH] Fixed weird auto scroll behavior in NowPlayingLayout when
 manually selecting a new play index. Also, added
 ListWindow::IsEntryVisible().

---
 src/musikbox/app/layout/NowPlayingLayout.cpp | 17 ++++++++++-------
 src/musikbox/app/window/TrackListView.cpp    |  5 +----
 src/musikbox/cursespp/ListWindow.cpp         |  7 +++++++
 src/musikbox/cursespp/ListWindow.h           |  1 +
 4 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/src/musikbox/app/layout/NowPlayingLayout.cpp b/src/musikbox/app/layout/NowPlayingLayout.cpp
index 504936651..2eaba4ed5 100755
--- a/src/musikbox/app/layout/NowPlayingLayout.cpp
+++ b/src/musikbox/app/layout/NowPlayingLayout.cpp
@@ -68,7 +68,8 @@ NowPlayingLayout::NowPlayingLayout(
 : LayoutBase()
 , playback(playback)
 , library(library)
-, lastPlaylistQueryId(-1) {
+, lastPlaylistQueryId(-1)
+, reselectIndex(-1) {
     this->InitializeWindows();
 
     this->playback.QueueEdited.connect(this, &NowPlayingLayout::RequeryTrackList);
@@ -164,7 +165,11 @@ void NowPlayingLayout::OnTrackListRequeried(musik::core::db::local::TrackListQue
                 this->trackListView->SetSelectedIndex(0);
             }
             else { /* playing... */
-                this->trackListView->ScrollTo(index == 0 ? index : index - 1);
+                size_t scrollToIndex = index == 0 ? index : index - 1;
+                if (!this->trackListView->IsEntryVisible(scrollToIndex)) {
+                    this->trackListView->ScrollTo(scrollToIndex);
+                }
+
                 this->trackListView->SetSelectedIndex(index);
             }
         }
@@ -176,13 +181,11 @@ void NowPlayingLayout::OnTrackListRequeried(musik::core::db::local::TrackListQue
             scrolled into view */
             this->reselectIndex = std::min((int) this->trackListView->Count() - 1, this->reselectIndex);
             this->trackListView->SetSelectedIndex((size_t)this->reselectIndex);
-            auto pos = this->trackListView->GetScrollPosition();
-            int first = (int)pos.firstVisibleEntryIndex;
-            int last = (int)first + pos.visibleEntryCount;
-            int index = (int)this->reselectIndex;
-            if (index < first || index >= last) {
+
+            if (!this->trackListView->IsEntryVisible((size_t) this->reselectIndex)) {
                 this->trackListView->ScrollTo((size_t)this->reselectIndex);
             }
+
             this->reselectIndex = -1;
         }
 
diff --git a/src/musikbox/app/window/TrackListView.cpp b/src/musikbox/app/window/TrackListView.cpp
index d7b9a64ab..afc93ee8f 100755
--- a/src/musikbox/app/window/TrackListView.cpp
+++ b/src/musikbox/app/window/TrackListView.cpp
@@ -179,10 +179,7 @@ void TrackListView::ScrollToPlaying() {
                 size_t rawIndex = headers.TrackListToAdapterIndex(i);
                 this->SetSelectedIndex(rawIndex);
 
-                auto pos = this->GetScrollPosition();
-                size_t first = pos.firstVisibleEntryIndex;
-                size_t last = first + pos.visibleEntryCount;
-                if (rawIndex < first || rawIndex >= last) {
+                if (!this->IsEntryVisible(rawIndex)) {
                     this->ScrollTo(rawIndex);
                 }
 
diff --git a/src/musikbox/cursespp/ListWindow.cpp b/src/musikbox/cursespp/ListWindow.cpp
index 44126e49b..4e9e359a2 100755
--- a/src/musikbox/cursespp/ListWindow.cpp
+++ b/src/musikbox/cursespp/ListWindow.cpp
@@ -200,6 +200,13 @@ void ListWindow::OnSelectionChanged(size_t newIndex, size_t oldIndex) {
     /* for subclass use */
 }
 
+bool ListWindow::IsEntryVisible(size_t index) {
+    auto pos = this->GetScrollPosition();
+    size_t first = pos.firstVisibleEntryIndex;
+    size_t last = first + pos.visibleEntryCount;
+    return (index >= first && index < last);
+}
+
 void ListWindow::SetSelectedIndex(size_t index) {
     if (this->selectedIndex != index) {
         if (index > this->GetScrollAdapter().GetEntryCount() &&
diff --git a/src/musikbox/cursespp/ListWindow.h b/src/musikbox/cursespp/ListWindow.h
index 779fc3e16..bbeae01c6 100755
--- a/src/musikbox/cursespp/ListWindow.h
+++ b/src/musikbox/cursespp/ListWindow.h
@@ -67,6 +67,7 @@ namespace cursespp {
 
             virtual size_t GetSelectedIndex();
             virtual void SetSelectedIndex(size_t index);
+            virtual bool IsEntryVisible(size_t index);
 
             virtual void OnAdapterChanged();