flex.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package tview
  2. import (
  3. "github.com/gdamore/tcell"
  4. )
  5. // Configuration values.
  6. const (
  7. FlexRow = iota
  8. FlexColumn
  9. )
  10. // flexItem holds layout options for one item.
  11. type flexItem struct {
  12. Item Primitive // The item to be positioned. May be nil for an empty item.
  13. FixedSize int // The item's fixed size which may not be changed, 0 if it has no fixed size.
  14. Proportion int // The item's proportion.
  15. Focus bool // Whether or not this item attracts the layout's focus.
  16. }
  17. // Flex is a basic implementation of the Flexbox layout. The contained
  18. // primitives are arranged horizontally or vertically. The way they are
  19. // distributed along that dimension depends on their layout settings, which is
  20. // either a fixed length or a proportional length. See AddItem() for details.
  21. //
  22. // See https://github.com/rivo/tview/wiki/Flex for an example.
  23. type Flex struct {
  24. *Box
  25. // The items to be positioned.
  26. items []*flexItem
  27. // FlexRow or FlexColumn.
  28. direction int
  29. // If set to true, Flex will use the entire screen as its available space
  30. // instead its box dimensions.
  31. fullScreen bool
  32. }
  33. // NewFlex returns a new flexbox layout container with no primitives and its
  34. // direction set to FlexColumn. To add primitives to this layout, see AddItem().
  35. // To change the direction, see SetDirection().
  36. //
  37. // Note that Box, the superclass of Flex, will have its background color set to
  38. // transparent so that any nil flex items will leave their background unchanged.
  39. // To clear a Flex's background before any items are drawn, set it to the
  40. // desired color:
  41. //
  42. // flex.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor)
  43. func NewFlex() *Flex {
  44. f := &Flex{
  45. Box: NewBox().SetBackgroundColor(tcell.ColorDefault),
  46. direction: FlexColumn,
  47. }
  48. f.focus = f
  49. return f
  50. }
  51. // SetDirection sets the direction in which the contained primitives are
  52. // distributed. This can be either FlexColumn (default) or FlexRow.
  53. func (f *Flex) SetDirection(direction int) *Flex {
  54. f.direction = direction
  55. return f
  56. }
  57. // SetFullScreen sets the flag which, when true, causes the flex layout to use
  58. // the entire screen space instead of whatever size it is currently assigned to.
  59. func (f *Flex) SetFullScreen(fullScreen bool) *Flex {
  60. f.fullScreen = fullScreen
  61. return f
  62. }
  63. // AddItem adds a new item to the container. The "fixedSize" argument is a width
  64. // or height that may not be changed by the layout algorithm. A value of 0 means
  65. // that its size is flexible and may be changed. The "proportion" argument
  66. // defines the relative size of the item compared to other flexible-size items.
  67. // For example, items with a proportion of 2 will be twice as large as items
  68. // with a proportion of 1. The proportion must be at least 1 if fixedSize == 0
  69. // (ignored otherwise).
  70. //
  71. // If "focus" is set to true, the item will receive focus when the Flex
  72. // primitive receives focus. If multiple items have the "focus" flag set to
  73. // true, the first one will receive focus.
  74. //
  75. // You can provide a nil value for the primitive. This will still consume screen
  76. // space but nothing will be drawn.
  77. func (f *Flex) AddItem(item Primitive, fixedSize, proportion int, focus bool) *Flex {
  78. f.items = append(f.items, &flexItem{Item: item, FixedSize: fixedSize, Proportion: proportion, Focus: focus})
  79. return f
  80. }
  81. // RemoveItem removes all items for the given primitive from the container,
  82. // keeping the order of the remaining items intact.
  83. func (f *Flex) RemoveItem(p Primitive) *Flex {
  84. for index := len(f.items) - 1; index >= 0; index-- {
  85. if f.items[index].Item == p {
  86. f.items = append(f.items[:index], f.items[index+1:]...)
  87. }
  88. }
  89. return f
  90. }
  91. // Clear removes all items from the container.
  92. func (f *Flex) Clear() *Flex {
  93. f.items = nil
  94. return f
  95. }
  96. // ResizeItem sets a new size for the item(s) with the given primitive. If there
  97. // are multiple Flex items with the same primitive, they will all receive the
  98. // same size. For details regarding the size parameters, see AddItem().
  99. func (f *Flex) ResizeItem(p Primitive, fixedSize, proportion int) *Flex {
  100. for _, item := range f.items {
  101. if item.Item == p {
  102. item.FixedSize = fixedSize
  103. item.Proportion = proportion
  104. }
  105. }
  106. return f
  107. }
  108. // Draw draws this primitive onto the screen.
  109. func (f *Flex) Draw(screen tcell.Screen) {
  110. f.Box.Draw(screen)
  111. // Calculate size and position of the items.
  112. // Do we use the entire screen?
  113. if f.fullScreen {
  114. width, height := screen.Size()
  115. f.SetRect(0, 0, width, height)
  116. }
  117. // How much space can we distribute?
  118. x, y, width, height := f.GetInnerRect()
  119. var proportionSum int
  120. distSize := width
  121. if f.direction == FlexRow {
  122. distSize = height
  123. }
  124. for _, item := range f.items {
  125. if item.FixedSize > 0 {
  126. distSize -= item.FixedSize
  127. } else {
  128. proportionSum += item.Proportion
  129. }
  130. }
  131. // Calculate positions and draw items.
  132. pos := x
  133. if f.direction == FlexRow {
  134. pos = y
  135. }
  136. for _, item := range f.items {
  137. size := item.FixedSize
  138. if size <= 0 {
  139. if proportionSum > 0 {
  140. size = distSize * item.Proportion / proportionSum
  141. distSize -= size
  142. proportionSum -= item.Proportion
  143. } else {
  144. size = 0
  145. }
  146. }
  147. if item.Item != nil {
  148. if f.direction == FlexColumn {
  149. item.Item.SetRect(pos, y, size, height)
  150. } else {
  151. item.Item.SetRect(x, pos, width, size)
  152. }
  153. }
  154. pos += size
  155. if item.Item != nil {
  156. if item.Item.GetFocusable().HasFocus() {
  157. defer item.Item.Draw(screen)
  158. } else {
  159. item.Item.Draw(screen)
  160. }
  161. }
  162. }
  163. }
  164. // Focus is called when this primitive receives focus.
  165. func (f *Flex) Focus(delegate func(p Primitive)) {
  166. for _, item := range f.items {
  167. if item.Item != nil && item.Focus {
  168. delegate(item.Item)
  169. return
  170. }
  171. }
  172. }
  173. // HasFocus returns whether or not this primitive has focus.
  174. func (f *Flex) HasFocus() bool {
  175. for _, item := range f.items {
  176. if item.Item != nil && item.Item.GetFocusable().HasFocus() {
  177. return true
  178. }
  179. }
  180. return false
  181. }
  182. // MouseHandler returns the mouse handler for this primitive.
  183. func (f *Flex) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  184. return f.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  185. if !f.InRect(event.Position()) {
  186. return false, nil
  187. }
  188. // Pass mouse events along to the first child item that takes it.
  189. for _, item := range f.items {
  190. if item.Item == nil {
  191. continue
  192. }
  193. consumed, capture = item.Item.MouseHandler()(action, event, setFocus)
  194. if consumed {
  195. return
  196. }
  197. }
  198. return
  199. })
  200. }