From 2cf1e5ee44eb598f7f2ff1b495a41e48258a89bf Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 6 Jun 2013 05:39:44 -0500 Subject: [PATCH] fix mousewheel on scrollables. --- lib/widget.js | 38 +++++++++++++++++++++++++++++--------- test/widget.js | 2 ++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/widget.js b/lib/widget.js index a5ccdef..8785046 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -942,15 +942,7 @@ function List(options) { if (this.selectedBg === -1) this.selectedBg = exports.NORMAL; if (this.selectedFg === -1) this.selectedFg = exports.NORMAL; - this.on('wheeldown', function(data) { - self.select(self.selected + 2); - self.screen.render(); - }); - - this.on('wheelup', function(data) { - self.select(self.selected - 2); - self.screen.render(); - }); + this.mouse = options.mouse || false; if (options.items) { options.items.forEach(this.add.bind(this)); @@ -981,10 +973,26 @@ List.prototype.add = function(item) { this.append(item); this.items.push(item); + if (!this.mouse) return; + item.on('click', function(data) { self.select(item); self.screen.render(); }); + + // Temporary workaround: + // We cannot bind to clickable events on the list because the parent will + // receive *all* clickable events in that particular area of the screen. + // Possibly fix in the future by emitting events through tree traversal as + // well as bubbling events. + item.on('wheeldown', function(data) { + self.select(self.selected + 2); + self.screen.render(); + }); + item.on('wheelup', function(data) { + self.select(self.selected - 2); + self.screen.render(); + }); }; List.prototype._remove = List.prototype.remove; @@ -1072,6 +1080,18 @@ List.prototype.down = function(offset) { function ScrollableText(options) { options.alwaysScroll = true; ScrollableBox.call(this, options); + + if (options.mouse) { + var self = this; + this.on('wheeldown', function(data) { + self.scroll(self.height / 2 | 0 || 1); + self.screen.render(); + }); + this.on('wheelup', function(data) { + self.scroll(-(self.height / 2 | 0) || -1); + self.screen.render(); + }); + } } ScrollableText.prototype.__proto__ = ScrollableBox.prototype; diff --git a/test/widget.js b/test/widget.js index 9c12ecb..a392a0e 100644 --- a/test/widget.js +++ b/test/widget.js @@ -66,6 +66,7 @@ screen.children[0].append(new blessed.Box({ var list = new blessed.List({ screen: screen, parent: screen, + mouse: true, fg: 4, bg: -1, border: { @@ -140,6 +141,7 @@ var lorem = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ei var stext = new blessed.ScrollableText({ screen: screen, parent: screen, + mouse: true, content: lorem, fg: 4, bg: -1,