From 793646955f3dcb8fea6489c6eb9f3d319d140edf Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 25 Aug 2015 20:49:59 -0700 Subject: [PATCH] stop image animation on destroy. improve test. --- lib/widgets/ansiimage.js | 17 +++++++++++------ test/widget-png.js | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/widgets/ansiimage.js b/lib/widgets/ansiimage.js index 35ab285..1f4ebd8 100644 --- a/lib/widgets/ansiimage.js +++ b/lib/widgets/ansiimage.js @@ -47,6 +47,10 @@ function ANSIImage(options) { // prevent image from blending with itself if there are alpha channels self.screen.clearRegion(lpos.xi, lpos.xl, lpos.yi, lpos.yl); }); + + this.on('destroy', function() { + self.stop(); + }); } ANSIImage.prototype.__proto__ = Box.prototype; @@ -119,26 +123,27 @@ ANSIImage.prototype.setImage = function(file) { } }; -ANSIImage.prototype.play = function(callback) { +ANSIImage.prototype.play = function() { var self = this; - return this.img.play(callback || function(bmp, cellmap) { + if (!this.img) return; + return this.img.play(function(bmp, cellmap) { self.cellmap = cellmap; self.screen.render(); }); }; ANSIImage.prototype.pause = function() { + if (!this.img) return; return this.img.pause(); }; ANSIImage.prototype.stop = function() { + if (!this.img) return; return this.img.stop(); }; ANSIImage.prototype.clearImage = function() { - if (this.img) { - this.stop(); - } + this.stop(); this.setContent(''); this.img = null; this.cellmap = null; @@ -148,7 +153,7 @@ ANSIImage.prototype.render = function() { var coords = this._render(); if (!coords) return; - if (this.img) { + if (this.img && this.cellmap) { this.img.renderElement(this.cellmap, this); } diff --git a/test/widget-png.js b/test/widget-png.js index bb6b6f5..2a5df3a 100644 --- a/test/widget-png.js +++ b/test/widget-png.js @@ -94,14 +94,41 @@ var png = blessed.image({ screen.render(); screen.key('q', function() { + clearInterval(timeout); screen.destroy(); }); var timeout = setInterval(function() { + if (png.right <= 0) { + clearInterval(timeout); + return; + } png.left++; screen.render(); }, 100); +if (timeout.unref) timeout.unref(); + +screen.key(['h', 'left'], function() { + png.left -= 2; +}); + +screen.key(['k', 'up'], function() { + png.top -= 2; +}); + +screen.key(['l', 'right'], function() { + png.left += 2; +}); + +screen.key(['j', 'down'], function() { + png.top += 2; +}); + +screen.on('keypress', function() { + clearInterval(timeout); +}); + png.on('mousedown', function() { clearInterval(timeout); });