image element: fix shrink.

This commit is contained in:
Christopher Jeffrey 2015-02-09 01:43:16 -08:00
parent a4e6856901
commit 1729244db3
2 changed files with 147 additions and 69 deletions

View File

@ -6569,8 +6569,8 @@ function Image(options) {
}
}
if (this.options.img || this.options.file) {
this.setImage(this.options.img || this.options.file);
if (this.options.file || this.options.img) {
this.setImage(this.options.file || this.options.img);
}
}
@ -6583,24 +6583,14 @@ Image.w3mdisplay = '/usr/lib/w3m/w3mimgdisplay';
Image.prototype.render = function() {
var ret = this._render();
if (!ret) return;
this.setImage(this.img);
if (!this._noImage) {
if (this._renderImage) this._renderImage();
else if (this.file) this.setImage(this.file);
}
return ret;
};
Image.prototype.spawn = function(file, args, opt, callback) {
var self = this;
if (Image.hasW3MDisplay == null) {
return this._waitForW3MDisplay(function() {
return self.spawn(file, args, opt, callback);
});
}
if (Image.hasW3MDisplay === false) {
if (!callback) return;
return callback(new Error('W3M Image Display not available.'));
}
var screen = this.screen
, opt = opt || {}
, spawn = require('child_process').spawn
@ -6625,7 +6615,18 @@ Image.prototype.spawn = function(file, args, opt, callback) {
Image.prototype.setImage = function(img, callback) {
var self = this;
this.img = img;
if (Image.hasW3MDisplay == null) {
return this._waitForW3MDisplay(function() {
return self.setImage(img, callback);
});
}
if (Image.hasW3MDisplay === false) {
if (!callback) return;
return callback(new Error('W3M Image Display not available.'));
}
this.file = img;
if (!img) {
if (!callback) return;
@ -6634,16 +6635,13 @@ Image.prototype.setImage = function(img, callback) {
// NOTE: Fall back to screen.displayImage() instead of setImage()?
this.termSize(function(err, dimensions) {
if (err) {
if (!callback) return;
return callback(err);
}
var tw, th;
var tw = dimensions.width / self.screen.width;
var th = dimensions.height / self.screen.height;
var file = Image.w3mdisplay;
function renderImage(callback) {
return self.clearImage(function() {
// XXX clearImage unsets this:
self.file = img;
self._renderImage = renderImage;
var opt = {
stdio: 'pipe',
@ -6651,26 +6649,13 @@ Image.prototype.setImage = function(img, callback) {
cwd: process.env.HOME
};
self.clearImage(function() {
var ps = self.spawn(file, [], opt, function(err, success) {
var ps = self.spawn(Image.w3mdisplay, [], opt, function(err, success) {
if (!callback) return;
return err
? callback(err)
: callback(null, success);
});
// XXX Move below code to callback.
if (self.shrink || self.options.autofit) {
delete self.shrink;
delete self.options.shrink;
self.options.autofit = true;
self.imageSize(function(err, size) {
if (err) return;
self.position.width = size.width / tw | 0;
self.position.height = size.height / th | 0;
});
}
var width = self.width * tw | 0
, height = self.height * th | 0
, left = self.left * tw | 0
@ -6694,38 +6679,104 @@ Image.prototype.setImage = function(img, callback) {
ps.stdin.write(input);
ps.stdin.end();
});
}
this._renderImage = renderImage;
return this.termSize(function(err, dimensions) {
if (err) {
if (!callback) return;
return callback(err);
}
tw = dimensions.width / self.screen.width;
th = dimensions.height / self.screen.height;
return renderImage(function(err, success) {
if (err) {
if (!callback) return;
return callback(err);
}
if (self.shrink || self.options.autofit) {
delete self.shrink;
delete self.options.shrink;
self.options.autofit = true;
return self.imageSize(function(err, size) {
if (err) return;
if (self._lastSize
&& tw === self._lastSize.tw
&& th === self._lastSize.th
&& size.width === self._lastSize.width
&& size.height === self._lastSize.height
&& self.left === self._lastSize.left
&& self.top === self._lastSize.top) {
if (!callback) return;
return callback(null, success);
}
self._lastSize = {
tw: tw,
th: th,
width: size.width,
height: size.height,
left: self.left,
top: self.top
};
self.position.width = size.width / tw | 0;
self.position.height = size.height / th | 0;
self._noImage = true;
self.screen.render();
self._noImage = false;
return renderImage(callback);
});
}
if (!callback) return;
return callback(null, success);
});
});
};
Image.prototype.clearImage = function(callback) {
var self = this;
if (Image.hasW3MDisplay == null) {
return this._waitForW3MDisplay(function() {
return self.clearImage(callback);
});
}
if (Image.hasW3MDisplay === false) {
if (!callback) return;
return callback(new Error('W3M Image Display not available.'));
}
if (!this._props) {
if (!callback) return;
return callback(null);
}
var file = Image.w3mdisplay;
var opt = {
stdio: 'pipe',
env: process.env,
cwd: process.env.HOME
};
var ps = this.spawn(file, [], opt, function(err, success) {
var ps = this.spawn(Image.w3mdisplay, [], opt, function(err, success) {
if (!callback) return;
return err
? callback(err)
: callback(null, success);
});
if (!ps) {
if (!callback) return;
return callback(new Error('Spawn Failed.'));
}
var width = this._props.width
, height = this._props.height
var width = this._props.width + 2
, height = this._props.height + 2
, left = this._props.left
, top = this._props.top;
@ -6737,7 +6788,9 @@ Image.prototype.clearImage = function(callback) {
+ '\n4;\n3;\n';
delete this._props;
delete this.img;
delete this.file;
delete this._renderImage;
delete this._lastSize;
ps.stdin.write(input);
ps.stdin.end();
@ -6745,8 +6798,18 @@ Image.prototype.clearImage = function(callback) {
Image.prototype.imageSize = function(callback) {
var self = this;
var img = this.img;
var file = Image.w3mdisplay;
var img = this.file;
if (Image.hasW3MDisplay == null) {
return this._waitForW3MDisplay(function() {
return self.imageSize(callback);
});
}
if (Image.hasW3MDisplay === false) {
if (!callback) return;
return callback(new Error('W3M Image Display not available.'));
}
if (!img) {
if (!callback) return;
@ -6759,11 +6822,7 @@ Image.prototype.imageSize = function(callback) {
cwd: process.env.HOME
};
var ps = this.spawn(file, [], opt);
if (!ps) {
if (!callback) return;
return callback(new Error('Spawn Failed.'));
}
var ps = this.spawn(Image.w3mdisplay, [], opt);
var buf = '';
@ -6796,7 +6855,17 @@ Image.prototype.imageSize = function(callback) {
Image.prototype.termSize = function(callback) {
var self = this;
var file = Image.w3mdisplay;
if (Image.hasW3MDisplay == null) {
return this._waitForW3MDisplay(function() {
return self.termSize(callback);
});
}
if (Image.hasW3MDisplay === false) {
if (!callback) return;
return callback(new Error('W3M Image Display not available.'));
}
var opt = {
stdio: 'pipe',
@ -6804,11 +6873,7 @@ Image.prototype.termSize = function(callback) {
cwd: process.env.HOME
};
var ps = this.spawn(file, ['-test'], opt);
if (!ps) {
if (!callback) return;
return callback(new Error('Spawn Failed.'));
}
var ps = this.spawn(Image.w3mdisplay, ['-test'], opt);
var buf = '';
@ -6837,7 +6902,7 @@ Image.prototype.termSize = function(callback) {
};
Image.prototype.displayImage = function(callback) {
return this.screen.displayImage(this.img, callback);
return this.screen.displayImage(this.file, callback);
};
Image.prototype._waitForW3MDisplay = function(callback) {

View File

@ -25,6 +25,19 @@ setTimeout(function() {
screen.render();
}, 1000);
//setTimeout(function() {
// image.setImage(file);
// image.rtop = 4;
// image.rleft = 10;
// screen.render();
// setTimeout(function() {
// image.setImage(file);
// image.rtop = 2;
// image.rleft = 7;
// screen.render();
// }, 1000);
//}, 5000);
image.focus();
screen.key('i', function() {