flex.go 7.5 KB

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