« get me outta code hell

Mouse support - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/ui/form/ListScrollForm.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-12-08 02:37:15 -0400
committerFlorrie <towerofnix@gmail.com>2018-12-08 02:37:15 -0400
commit1f434c1ef11fa55bab1718ea4e3ca8d115c0dfb1 (patch)
tree341321a95d376faf720dda8d4e0ca4b69880f3e6 /ui/form/ListScrollForm.js
parent013835d81e5e56f59faf17e215a73f9e8dc4310b (diff)
Mouse support
Not exactly the most elegant implementation, but it definitely works and
isn't really difficult to code around!
Diffstat (limited to 'ui/form/ListScrollForm.js')
-rw-r--r--ui/form/ListScrollForm.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/ui/form/ListScrollForm.js b/ui/form/ListScrollForm.js
index 3f16416..77bebcd 100644
--- a/ui/form/ListScrollForm.js
+++ b/ui/form/ListScrollForm.js
@@ -24,6 +24,8 @@ module.exports = class ListScrollForm extends Form {
   }
 
   fixLayout() {
+    this.keepScrollInBounds()
+
     // The scrollItems property represents the item to the very left of where
     // we've scrolled, so we know right away that none of those will be
     // visible and we won't bother iterating over them.
@@ -96,6 +98,34 @@ module.exports = class ListScrollForm extends Form {
     return ret
   }
 
+  clicked(button) {
+    // Old code for changing the actual selected item...maybe an interesting
+    // functionality to explore later?
+    /*
+    if (button === 'scroll-up') {
+      this.previousInput()
+      this.scrollSelectedElementIntoView()
+    } else if (button === 'scroll-down') {
+      this.nextInput()
+      this.scrollSelectedElementIntoView()
+    }
+    */
+
+    // Scrolling is typically pretty slow with a mouse wheel when it's by
+    // a single line, so scroll at 3x that speed.
+    for (let i = 0; i < 3; i++) {
+      if (button === 'scroll-up') {
+        this.scrollItems--
+      } else if (button === 'scroll-down') {
+        this.scrollItems++
+      } else {
+        return
+      }
+    }
+
+    this.fixLayout()
+  }
+
   scrollSelectedElementIntoView() {
     const sel = this.inputs[this.curIndex]
 
@@ -152,6 +182,11 @@ module.exports = class ListScrollForm extends Form {
     this.fixLayout()
   }
 
+  keepScrollInBounds() {
+    this.scrollItems = Math.max(this.scrollItems, 0)
+    this.scrollItems = Math.min(this.scrollItems, this.getScrollItemsLength())
+  }
+
   getScrollItemsLength() {
     if (typeof this._scrollItemsLength === 'undefined') {
       const lastInput = this.inputs[this.inputs.length - 1]