« get me outta code hell

remember scroll position of each group - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-05-03 15:17:09 -0300
committerFlorrie <towerofnix@gmail.com>2020-05-03 15:17:09 -0300
commit9a5dfd47147aaeb844fbfc9ae8137e67f735fff1 (patch)
treec4287a3f5366f26b98027acc0744b8844aa2a51e
parent75b8fdc3aa238f34825cd08faa6906e9b818f47c (diff)
remember scroll position of each group
-rw-r--r--todo.txt1
-rw-r--r--ui.js26
2 files changed, 26 insertions, 1 deletions
diff --git a/todo.txt b/todo.txt
index c99c70d..168c69f 100644
--- a/todo.txt
+++ b/todo.txt
@@ -277,6 +277,7 @@ TODO: Remember the scroll position of each group. This should probably be done
       per-listing, but make sure the data disappears when the listing is
       destroyed (e.g. tab closed)! Have maps on the listings instead of the
       grouplikes themselves.
+      (Done!)
 
 TODO: Apparently shift-up/down selecting doesn't keep the selected item within
       the scroll view. Fix that.
diff --git a/ui.js b/ui.js
index 0172fc2..bf16e49 100644
--- a/ui.js
+++ b/ui.js
@@ -1647,6 +1647,8 @@ class GrouplikeListingElement extends Form {
     this.commentLabel = new WrapLabel()
     this.addChild(this.commentLabel)
 
+    this.grouplikeData = new WeakMap()
+
     this.autoscrollOffset = null
   }
 
@@ -1724,11 +1726,32 @@ class GrouplikeListingElement extends Form {
   }
 
   loadGrouplike(grouplike, resetIndex = true) {
+    this.saveGrouplikeData()
     this.grouplike = grouplike
     this.buildItems(resetIndex)
+    this.restoreGrouplikeData()
+
     if (this.root.select) this.hideJumpElement()
   }
 
+  saveGrouplikeData() {
+    if (isGroup(this.grouplike)) {
+      this.grouplikeData.set(this.grouplike, {
+        scrollItems: this.form.scrollItems,
+        currentItem: this.currentItem
+      })
+    }
+  }
+
+  restoreGrouplikeData() {
+    if (this.grouplikeData.has(this.grouplike)) {
+      const data = this.grouplikeData.get(this.grouplike)
+      this.form.scrollItems = data.scrollItems
+      this.form.selectAndShow(data.currentItem)
+      this.form.fixLayout()
+    }
+  }
+
   selectNone() {
     // nb: this is unrelated to the actual track selection system!
     // just clears the form selection
@@ -1806,7 +1829,7 @@ class GrouplikeListingElement extends Form {
     if (this.grouplike.items.length) {
       // Add an element for controlling this whole group. Particularly handy
       // for operating on the top-level group, which itself is not contained
-      // within any groups (so you can't bworse a parent and access its menu
+      // within any groups (so you can't browse a parent and access its menu
       // from there).
       if (!this.grouplike.isTheQueue) {
         const ownElement = new BasicGrouplikeItemElement(`This group: ${this.grouplike.name || '(Unnamed group)'}`)
@@ -1898,6 +1921,7 @@ class GrouplikeListingElement extends Form {
       } else {
         form.curIndex = form.firstItemIndex
       }
+      this.restoreGrouplikeData()
       form.updateSelectedElement()
       form.scrollSelectedElementIntoView()
     }