« get me outta code hell

tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/ui
diff options
context:
space:
mode:
Diffstat (limited to 'ui')
-rw-r--r--ui/form/Button.js2
-rw-r--r--ui/form/FocusBox.js4
-rw-r--r--ui/form/FocusElement.js11
3 files changed, 8 insertions, 9 deletions
diff --git a/ui/form/Button.js b/ui/form/Button.js
index cf7262f..1f1c537 100644
--- a/ui/form/Button.js
+++ b/ui/form/Button.js
@@ -21,7 +21,7 @@ module.exports = class Button extends FocusElement {
   }
 
   drawTo(writable) {
-    if (this.isFocused) {
+    if (this.isSelected) {
       writable.write(ansi.invert())
     }
 
diff --git a/ui/form/FocusBox.js b/ui/form/FocusBox.js
index 51e961b..69b5bf5 100644
--- a/ui/form/FocusBox.js
+++ b/ui/form/FocusBox.js
@@ -19,13 +19,13 @@ module.exports = class FocusBox extends FocusElement {
   }
 
   drawTo(writable) {
-    if (this.isFocused) {
+    if (this.isSelected) {
       writable.write(ansi.invert())
     }
   }
 
   didRenderTo(writable) {
-    if (this.isFocused) {
+    if (this.isSelected) {
       writable.write(ansi.resetAttributes())
     }
   }
diff --git a/ui/form/FocusElement.js b/ui/form/FocusElement.js
index 79a8afc..9061838 100644
--- a/ui/form/FocusElement.js
+++ b/ui/form/FocusElement.js
@@ -9,20 +9,14 @@ module.exports = class FocusElement extends DisplayElement {
     this.cursorVisible = false
     this.cursorX = 0
     this.cursorY = 0
-
-    this.isFocused = false
   }
 
   focused() {
     // Should be overridden in subclasses.
-
-    this.isFocused = true
   }
 
   unfocused() {
     // Should be overridden in subclasses.
-
-    this.isFocused = false
   }
 
   get selectable() {
@@ -41,6 +35,11 @@ module.exports = class FocusElement extends DisplayElement {
     // http://stackoverflow.com/a/11432632/4633828
   }
 
+  get isSelected() {
+    const selected = this.root.selected
+    return selected && [selected, ...selected.directAncestors].includes(this)
+  }
+
   get absCursorX() { return this.absX + this.cursorX }
   get absCursorY() { return this.absY + this.cursorY }
 }