« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ui.js15
1 files changed, 12 insertions, 3 deletions
diff --git a/ui.js b/ui.js
index 3e0098f..b786838 100644
--- a/ui.js
+++ b/ui.js
@@ -199,6 +199,8 @@ export default class AppElement extends FocusElement {
 
     this.timestampDictionary = new WeakMap()
 
+    this.itemMenuPage = 0
+
     // We add this is a child later (so that it's on top of every element).
     this.menuLayer = new DisplayElement()
     this.menuLayer.clickThrough = true
@@ -1515,10 +1517,15 @@ export default class AppElement extends FocusElement {
     // TODO: Implement this! :P
     // const isMarked = false
 
-    this.showContextMenu({
+    const menu = this.showContextMenu({
       x: el.absLeft,
       y: el.absTop + 1,
-      pages
+      pages,
+      pageNum: this.itemMenuPage
+    })
+
+    menu.on('page changed', pageNum => {
+      this.itemMenuPage = pageNum
     })
   }
 
@@ -4837,6 +4844,7 @@ class ContextMenu extends FocusElement {
         }
         this.close(false)
         this.show({x, y, pages, pageNum})
+        this.emit('page changed', pageNum)
       }
     }
 
@@ -4848,6 +4856,7 @@ class ContextMenu extends FocusElement {
         }
         this.close(false)
         this.show({x, y, pages, pageNum})
+        this.emit('page changed', pageNum)
       }
     }
 
@@ -4859,7 +4868,7 @@ class ContextMenu extends FocusElement {
       if (pages.length === 0) {
         return
       }
-      itemsArg = pages[pageNum]
+      itemsArg = pages[Math.min(pages.length - 1, pageNum)]
     }
 
     let items = (typeof itemsArg === 'function') ? itemsArg() : itemsArg