button.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package tview
  2. import (
  3. "github.com/gdamore/tcell"
  4. )
  5. // Button is labeled box that triggers an action when selected.
  6. //
  7. // See https://github.com/rivo/tview/wiki/Button for an example.
  8. type Button struct {
  9. *Box
  10. // The text to be displayed before the input area.
  11. label string
  12. // The label color.
  13. labelColor tcell.Color
  14. // The label color when the button is in focus.
  15. labelColorActivated tcell.Color
  16. // The background color when the button is in focus.
  17. backgroundColorActivated tcell.Color
  18. // An optional function which is called when the button was selected.
  19. selected func()
  20. // An optional function which is called when the user leaves the button. A
  21. // key is provided indicating which key was pressed to leave (tab or backtab).
  22. blur func(tcell.Key)
  23. }
  24. // NewButton returns a new input field.
  25. func NewButton(label string) *Button {
  26. box := NewBox().SetBackgroundColor(Styles.ContrastBackgroundColor)
  27. box.SetRect(0, 0, TaggedStringWidth(label)+4, 1)
  28. return &Button{
  29. Box: box,
  30. label: label,
  31. labelColor: Styles.PrimaryTextColor,
  32. labelColorActivated: Styles.InverseTextColor,
  33. backgroundColorActivated: Styles.PrimaryTextColor,
  34. }
  35. }
  36. // SetLabel sets the button text.
  37. func (b *Button) SetLabel(label string) *Button {
  38. b.label = label
  39. return b
  40. }
  41. // GetLabel returns the button text.
  42. func (b *Button) GetLabel() string {
  43. return b.label
  44. }
  45. // SetLabelColor sets the color of the button text.
  46. func (b *Button) SetLabelColor(color tcell.Color) *Button {
  47. b.labelColor = color
  48. return b
  49. }
  50. // SetLabelColorActivated sets the color of the button text when the button is
  51. // in focus.
  52. func (b *Button) SetLabelColorActivated(color tcell.Color) *Button {
  53. b.labelColorActivated = color
  54. return b
  55. }
  56. // SetBackgroundColorActivated sets the background color of the button text when
  57. // the button is in focus.
  58. func (b *Button) SetBackgroundColorActivated(color tcell.Color) *Button {
  59. b.backgroundColorActivated = color
  60. return b
  61. }
  62. // SetSelectedFunc sets a handler which is called when the button was selected.
  63. func (b *Button) SetSelectedFunc(handler func()) *Button {
  64. b.selected = handler
  65. return b
  66. }
  67. // SetBlurFunc sets a handler which is called when the user leaves the button.
  68. // The callback function is provided with the key that was pressed, which is one
  69. // of the following:
  70. //
  71. // - KeyEscape: Leaving the button with no specific direction.
  72. // - KeyTab: Move to the next field.
  73. // - KeyBacktab: Move to the previous field.
  74. func (b *Button) SetBlurFunc(handler func(key tcell.Key)) *Button {
  75. b.blur = handler
  76. return b
  77. }
  78. // Draw draws this primitive onto the screen.
  79. func (b *Button) Draw(screen tcell.Screen) {
  80. // Draw the box.
  81. borderColor := b.borderColor
  82. backgroundColor := b.backgroundColor
  83. if b.focus.HasFocus() {
  84. b.backgroundColor = b.backgroundColorActivated
  85. b.borderColor = b.labelColorActivated
  86. defer func() {
  87. b.borderColor = borderColor
  88. }()
  89. }
  90. b.Box.Draw(screen)
  91. b.backgroundColor = backgroundColor
  92. // Draw label.
  93. x, y, width, height := b.GetInnerRect()
  94. if width > 0 && height > 0 {
  95. y = y + height/2
  96. labelColor := b.labelColor
  97. if b.focus.HasFocus() {
  98. labelColor = b.labelColorActivated
  99. }
  100. Print(screen, b.label, x, y, width, AlignCenter, labelColor)
  101. }
  102. }
  103. // InputHandler returns the handler for this primitive.
  104. func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
  105. return b.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
  106. // Process key event.
  107. switch key := event.Key(); key {
  108. case tcell.KeyEnter: // Selected.
  109. if b.selected != nil {
  110. b.selected()
  111. }
  112. case tcell.KeyBacktab, tcell.KeyTab, tcell.KeyEscape: // Leave. No action.
  113. if b.blur != nil {
  114. b.blur(key)
  115. }
  116. }
  117. })
  118. }
  119. // MouseHandler returns the mouse handler for this primitive.
  120. func (b *Button) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  121. return b.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  122. if !b.InRect(event.Position()) {
  123. return false, nil
  124. }
  125. // Process mouse event.
  126. if action == MouseLeftClick {
  127. setFocus(b)
  128. if b.selected != nil {
  129. b.selected()
  130. }
  131. consumed = true
  132. }
  133. return
  134. })
  135. }