checkbox.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package tview
  2. import (
  3. "github.com/gdamore/tcell"
  4. )
  5. // Checkbox implements a simple box for boolean values which can be checked and
  6. // unchecked.
  7. //
  8. // See https://github.com/rivo/tview/wiki/Checkbox for an example.
  9. type Checkbox struct {
  10. *Box
  11. // Whether or not this box is checked.
  12. checked bool
  13. // The text to be displayed before the input area.
  14. label string
  15. // The screen width of the label area. A value of 0 means use the width of
  16. // the label text.
  17. labelWidth int
  18. // The label color.
  19. labelColor tcell.Color
  20. // The background color of the input area.
  21. fieldBackgroundColor tcell.Color
  22. // The text color of the input area.
  23. fieldTextColor tcell.Color
  24. // An optional function which is called when the user changes the checked
  25. // state of this checkbox.
  26. changed func(checked bool)
  27. // An optional function which is called when the user indicated that they
  28. // are done entering text. The key which was pressed is provided (tab,
  29. // shift-tab, or escape).
  30. done func(tcell.Key)
  31. // A callback function set by the Form class and called when the user leaves
  32. // this form item.
  33. finished func(tcell.Key)
  34. }
  35. // NewCheckbox returns a new input field.
  36. func NewCheckbox() *Checkbox {
  37. return &Checkbox{
  38. Box: NewBox(),
  39. labelColor: Styles.SecondaryTextColor,
  40. fieldBackgroundColor: Styles.ContrastBackgroundColor,
  41. fieldTextColor: Styles.PrimaryTextColor,
  42. }
  43. }
  44. // SetChecked sets the state of the checkbox.
  45. func (c *Checkbox) SetChecked(checked bool) *Checkbox {
  46. c.checked = checked
  47. return c
  48. }
  49. // IsChecked returns whether or not the box is checked.
  50. func (c *Checkbox) IsChecked() bool {
  51. return c.checked
  52. }
  53. // SetLabel sets the text to be displayed before the input area.
  54. func (c *Checkbox) SetLabel(label string) *Checkbox {
  55. c.label = label
  56. return c
  57. }
  58. // GetLabel returns the text to be displayed before the input area.
  59. func (c *Checkbox) GetLabel() string {
  60. return c.label
  61. }
  62. // SetLabelWidth sets the screen width of the label. A value of 0 will cause the
  63. // primitive to use the width of the label string.
  64. func (c *Checkbox) SetLabelWidth(width int) *Checkbox {
  65. c.labelWidth = width
  66. return c
  67. }
  68. // SetLabelColor sets the color of the label.
  69. func (c *Checkbox) SetLabelColor(color tcell.Color) *Checkbox {
  70. c.labelColor = color
  71. return c
  72. }
  73. // SetFieldBackgroundColor sets the background color of the input area.
  74. func (c *Checkbox) SetFieldBackgroundColor(color tcell.Color) *Checkbox {
  75. c.fieldBackgroundColor = color
  76. return c
  77. }
  78. // SetFieldTextColor sets the text color of the input area.
  79. func (c *Checkbox) SetFieldTextColor(color tcell.Color) *Checkbox {
  80. c.fieldTextColor = color
  81. return c
  82. }
  83. // SetFormAttributes sets attributes shared by all form items.
  84. func (c *Checkbox) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
  85. c.labelWidth = labelWidth
  86. c.labelColor = labelColor
  87. c.backgroundColor = bgColor
  88. c.fieldTextColor = fieldTextColor
  89. c.fieldBackgroundColor = fieldBgColor
  90. return c
  91. }
  92. // GetFieldWidth returns this primitive's field width.
  93. func (c *Checkbox) GetFieldWidth() int {
  94. return 1
  95. }
  96. // SetChangedFunc sets a handler which is called when the checked state of this
  97. // checkbox was changed by the user. The handler function receives the new
  98. // state.
  99. func (c *Checkbox) SetChangedFunc(handler func(checked bool)) *Checkbox {
  100. c.changed = handler
  101. return c
  102. }
  103. // SetDoneFunc sets a handler which is called when the user is done using the
  104. // checkbox. The callback function is provided with the key that was pressed,
  105. // which is one of the following:
  106. //
  107. // - KeyEscape: Abort text input.
  108. // - KeyTab: Move to the next field.
  109. // - KeyBacktab: Move to the previous field.
  110. func (c *Checkbox) SetDoneFunc(handler func(key tcell.Key)) *Checkbox {
  111. c.done = handler
  112. return c
  113. }
  114. // SetFinishedFunc sets a callback invoked when the user leaves this form item.
  115. func (c *Checkbox) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
  116. c.finished = handler
  117. return c
  118. }
  119. // Draw draws this primitive onto the screen.
  120. func (c *Checkbox) Draw(screen tcell.Screen) {
  121. c.Box.Draw(screen)
  122. // Prepare
  123. x, y, width, height := c.GetInnerRect()
  124. rightLimit := x + width
  125. if height < 1 || rightLimit <= x {
  126. return
  127. }
  128. // Draw label.
  129. if c.labelWidth > 0 {
  130. labelWidth := c.labelWidth
  131. if labelWidth > rightLimit-x {
  132. labelWidth = rightLimit - x
  133. }
  134. Print(screen, c.label, x, y, labelWidth, AlignLeft, c.labelColor)
  135. x += labelWidth
  136. } else {
  137. _, drawnWidth := Print(screen, c.label, x, y, rightLimit-x, AlignLeft, c.labelColor)
  138. x += drawnWidth
  139. }
  140. // Draw checkbox.
  141. fieldStyle := tcell.StyleDefault.Background(c.fieldBackgroundColor).Foreground(c.fieldTextColor)
  142. if c.focus.HasFocus() {
  143. fieldStyle = fieldStyle.Background(c.fieldTextColor).Foreground(c.fieldBackgroundColor)
  144. }
  145. checkedRune := 'X'
  146. if !c.checked {
  147. checkedRune = ' '
  148. }
  149. screen.SetContent(x, y, checkedRune, nil, fieldStyle)
  150. }
  151. // InputHandler returns the handler for this primitive.
  152. func (c *Checkbox) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
  153. return c.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
  154. // Process key event.
  155. switch key := event.Key(); key {
  156. case tcell.KeyRune, tcell.KeyEnter: // Check.
  157. if key == tcell.KeyRune && event.Rune() != ' ' {
  158. break
  159. }
  160. c.checked = !c.checked
  161. if c.changed != nil {
  162. c.changed(c.checked)
  163. }
  164. case tcell.KeyTab, tcell.KeyBacktab, tcell.KeyEscape: // We're done.
  165. if c.done != nil {
  166. c.done(key)
  167. }
  168. if c.finished != nil {
  169. c.finished(key)
  170. }
  171. }
  172. })
  173. }
  174. // MouseHandler returns the mouse handler for this primitive.
  175. func (c *Checkbox) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  176. return c.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  177. x, y := event.Position()
  178. _, rectY, _, _ := c.GetInnerRect()
  179. if !c.InRect(x, y) {
  180. return false, nil
  181. }
  182. // Process mouse event.
  183. if action == MouseLeftClick && y == rectY {
  184. setFocus(c)
  185. c.checked = !c.checked
  186. if c.changed != nil {
  187. c.changed(c.checked)
  188. }
  189. consumed = true
  190. }
  191. return
  192. })
  193. }