« get me outta code hell

Select already selected option from dropdown menu - 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>2019-08-25 19:47:12 -0300
committerFlorrie <towerofnix@gmail.com>2019-08-25 19:47:12 -0300
commit1337e00646d1b888db6ad8827faf1db9a0329cf3 (patch)
treeda140a9feddc5489b2f5363d835b5cdd6f602d98
parentc814d5d140ea2383b7b1719a31abf731753c3fd6 (diff)
Select already selected option from dropdown menu
...by default.
-rw-r--r--ui.js15
1 files changed, 12 insertions, 3 deletions
diff --git a/ui.js b/ui.js
index 840c7ee..e0ab4f6 100644
--- a/ui.js
+++ b/ui.js
@@ -1821,7 +1821,8 @@ class InlineListPickerElement extends FocusElement {
           label: label,
           action: () => {
             this.curIndex = index
-          }
+          },
+          isDefault: index === this.curIndex
         }))
       })
     } else {
@@ -2919,7 +2920,7 @@ class ContextMenu extends FocusElement {
     this.submenu = null
   }
 
-  show({x = 0, y = 0, items}) {
+  show({x = 0, y = 0, defaultIndex = 0, items}) {
     items = items.filter(Boolean)
     if (!items.length) {
       return
@@ -2951,6 +2952,9 @@ class ContextMenu extends FocusElement {
         addDividerIfWanted()
         this.form.addInput(item.element)
         item.element.showContextMenu = this.showSubmenu
+        if (item.isDefault) {
+          this.root.select(item.element)
+        }
       } else if (item.divider) {
         wantDivider = true
       } else {
@@ -2964,12 +2968,17 @@ class ContextMenu extends FocusElement {
           })
         }
         this.form.addInput(button)
+        if (item.isDefault) {
+          this.root.select(button)
+        }
       }
     }
 
     this.fixLayout()
 
-    this.form.firstInput()
+    if (!items.some(item => item.isDefault)) {
+      this.form.firstInput()
+    }
 
     this.keyboardSelector.reset()
   }