From b04f8d04b2f258052afe811d149b4e7ece86c8e8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 9 Jun 2013 15:02:12 -0500 Subject: [PATCH] fix textbox focusing. --- lib/widget.js | 35 ++++++++++++++++++++++------------- test/widget.js | 7 ++++--- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/widget.js b/lib/widget.js index 48d79d6..d529d94 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -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) diff --git a/test/widget.js b/test/widget.js index 12d07c1..b1f4061 100644 --- a/test/widget.js +++ b/test/widget.js @@ -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') {