fix textbox focusing.

This commit is contained in:
Christopher Jeffrey 2013-06-09 15:02:12 -05:00
parent 87aef67274
commit b04f8d04b2
2 changed files with 26 additions and 16 deletions

View File

@ -145,6 +145,7 @@ function Screen(options) {
this.focused = null;
this.clickable = [];
this.input = [];
this.grabKeys = false;
this.alloc();
@ -173,8 +174,11 @@ Screen.prototype._listenMouse = function(el, hover) {
var self = this;
if (el) {
if (!hover) this.clickable.push(el);
else this.hover.push(el);
if (!hover) {
if (!~this.clickable.indexOf(el)) this.clickable.push(el);
} else {
if (!~this.hover.indexOf(el)) this.hover.push(el);
}
}
if (this._listenedMouse) return;
@ -213,7 +217,9 @@ Screen.prototype._listenKeys = function(el) {
var self = this;
if (el) {
this.input.push(el);
if (!~this.input.indexOf(el)) {
this.input.push(el);
}
//if (this.mouse)
//el.on('click', function() {
// el.focus();
@ -227,7 +233,7 @@ Screen.prototype._listenKeys = function(el) {
if (~self.input.indexOf(self.focused)) {
self.focused.emit('keypress', ch, key);
}
if (!self._grabKeys) {
if (!self.grabKeys) {
self.emit('keypress', ch, key);
}
});
@ -506,6 +512,15 @@ function Element(options) {
}
this.content = options.content || '';
if (options.label) {
this.append(new Text({
screen: this.screen,
content: options.label,
left: 2,
top: this.border ? 0 : -1
}));
}
}
Element.prototype.__proto__ = Node.prototype;
@ -1337,13 +1352,7 @@ function Textbox(options) {
Input.call(this, options);
this.value = options.value || '';
this.content = options.content || '';
if (options.label) {
// TODO: Make sure negative relative coords work.
this.append(new Text({
content: options.label,
top: -1 - (this.border ? 1 : 0)
}));
}
this.screen._listenKeys(this);
}
Textbox.prototype.__proto__ = Input.prototype;
@ -1351,7 +1360,7 @@ Textbox.prototype.__proto__ = Input.prototype;
Textbox.prototype.setInput = function(callback) {
var self = this;
this.screen._grabKeys = true;
this.screen.grabKeys = true;
this.focus();
// this.screen.program.saveCursor();
@ -1366,7 +1375,7 @@ Textbox.prototype.setInput = function(callback) {
self.screen.program.hideCursor();
// Wait for global keypress event to fire.
process.nextTick(function() {
self.screen._grabKeys = false;
self.screen.grabKeys = false;
});
return err
? callback(err)

View File

@ -177,6 +177,7 @@ screen.on('element focus', function(old, cur) {
var input = new blessed.Textbox({
mouse: true,
label: ' My Input ',
content: '',
fg: 4,
bg: -1,
@ -190,7 +191,7 @@ var input = new blessed.Textbox({
width: '30%',
height: 3,
right: 0,
top: 0
top: 2
});
screen.append(input);
@ -203,12 +204,12 @@ screen.on('keypress', function(ch, key) {
}
if (key.name === 'i') {
return input.setInput(function(err, value) {
screen.children[0].setContent(value);
if (value) screen.children[0].setContent(value);
});
}
if (key.name === 'e') {
return input.setEditor(function(err, value) {
screen.children[0].setContent(value);
if (value) screen.children[0].setContent(value);
});
}
if (key.name === 'escape' || key.name === 'q') {