basic-app.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Basic app demo:
  2. // - Structuring a basic element tree
  3. // - Creating a pane and text input
  4. // - Using content width/height to layout elements
  5. // - Subclassing a FocusElement and using its focused method
  6. // - Sending a quit-app request via Control-C
  7. //
  8. // This script cannot actually be used on its own; see the examples on
  9. // interfaces (command-line-interface.js and telnet-interface.js) for a
  10. // working demo.
  11. import {TextInput} from 'tui-lib/ui/controls'
  12. import {Pane} from 'tui-lib/ui/presentation'
  13. import {FocusElement} from 'tui-lib/ui/primitives'
  14. export default class AppElement extends FocusElement {
  15. constructor() {
  16. super()
  17. this.pane = new Pane()
  18. this.addChild(this.pane)
  19. this.textInput = new TextInput()
  20. this.pane.addChild(this.textInput)
  21. }
  22. fixLayout() {
  23. this.w = this.parent.contentW
  24. this.h = this.parent.contentH
  25. this.pane.w = this.contentW
  26. this.pane.h = this.contentH
  27. this.textInput.x = 4
  28. this.textInput.y = 2
  29. this.textInput.w = this.pane.contentW - 8
  30. }
  31. focused() {
  32. this.root.select(this.textInput)
  33. }
  34. keyPressed(keyBuf) {
  35. if (keyBuf[0] === 0x03) { // 0x03 is Control-C
  36. this.emit('quitRequested')
  37. return
  38. }
  39. super.keyPressed(keyBuf)
  40. }
  41. }