flex.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package layout
  2. import (
  3. tea "github.com/charmbracelet/bubbletea/v2"
  4. "github.com/charmbracelet/lipgloss/v2"
  5. "github.com/sst/opencode/internal/theme"
  6. )
  7. type FlexDirection int
  8. const (
  9. FlexDirectionHorizontal FlexDirection = iota
  10. FlexDirectionVertical
  11. )
  12. type FlexChildSize struct {
  13. Fixed bool
  14. Size int
  15. }
  16. var FlexChildSizeGrow = FlexChildSize{Fixed: false}
  17. func FlexChildSizeFixed(size int) FlexChildSize {
  18. return FlexChildSize{Fixed: true, Size: size}
  19. }
  20. type FlexLayout interface {
  21. tea.ViewModel
  22. Sizeable
  23. SetChildren(panes []tea.ViewModel) tea.Cmd
  24. SetSizes(sizes []FlexChildSize) tea.Cmd
  25. SetDirection(direction FlexDirection) tea.Cmd
  26. }
  27. type flexLayout struct {
  28. width int
  29. height int
  30. direction FlexDirection
  31. children []tea.ViewModel
  32. sizes []FlexChildSize
  33. }
  34. type FlexLayoutOption func(*flexLayout)
  35. func (f *flexLayout) View() string {
  36. if len(f.children) == 0 {
  37. return ""
  38. }
  39. t := theme.CurrentTheme()
  40. views := make([]string, 0, len(f.children))
  41. for i, child := range f.children {
  42. if child == nil {
  43. continue
  44. }
  45. alignment := lipgloss.Center
  46. if alignable, ok := child.(Alignable); ok {
  47. alignment = alignable.Alignment()
  48. }
  49. var childWidth, childHeight int
  50. if f.direction == FlexDirectionHorizontal {
  51. childWidth, childHeight = f.calculateChildSize(i)
  52. view := lipgloss.PlaceHorizontal(
  53. childWidth,
  54. alignment,
  55. child.View(),
  56. // TODO: make configurable WithBackgroundStyle
  57. lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Background(t.Background())),
  58. )
  59. views = append(views, view)
  60. } else {
  61. childWidth, childHeight = f.calculateChildSize(i)
  62. view := lipgloss.Place(
  63. f.width,
  64. childHeight,
  65. lipgloss.Center,
  66. alignment,
  67. child.View(),
  68. // TODO: make configurable WithBackgroundStyle
  69. lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Background(t.Background())),
  70. )
  71. views = append(views, view)
  72. }
  73. }
  74. if f.direction == FlexDirectionHorizontal {
  75. return lipgloss.JoinHorizontal(lipgloss.Center, views...)
  76. }
  77. return lipgloss.JoinVertical(lipgloss.Center, views...)
  78. }
  79. func (f *flexLayout) calculateChildSize(index int) (width, height int) {
  80. if index >= len(f.children) {
  81. return 0, 0
  82. }
  83. totalFixed := 0
  84. flexCount := 0
  85. for i, child := range f.children {
  86. if child == nil {
  87. continue
  88. }
  89. if i < len(f.sizes) && f.sizes[i].Fixed {
  90. if f.direction == FlexDirectionHorizontal {
  91. totalFixed += f.sizes[i].Size
  92. } else {
  93. totalFixed += f.sizes[i].Size
  94. }
  95. } else {
  96. flexCount++
  97. }
  98. }
  99. if f.direction == FlexDirectionHorizontal {
  100. height = f.height
  101. if index < len(f.sizes) && f.sizes[index].Fixed {
  102. width = f.sizes[index].Size
  103. } else if flexCount > 0 {
  104. remainingSpace := f.width - totalFixed
  105. width = remainingSpace / flexCount
  106. }
  107. } else {
  108. width = f.width
  109. if index < len(f.sizes) && f.sizes[index].Fixed {
  110. height = f.sizes[index].Size
  111. } else if flexCount > 0 {
  112. remainingSpace := f.height - totalFixed
  113. height = remainingSpace / flexCount
  114. }
  115. }
  116. return width, height
  117. }
  118. func (f *flexLayout) SetSize(width, height int) tea.Cmd {
  119. f.width = width
  120. f.height = height
  121. var cmds []tea.Cmd
  122. currentX, currentY := 0, 0
  123. for i, child := range f.children {
  124. if child != nil {
  125. paneWidth, paneHeight := f.calculateChildSize(i)
  126. alignment := lipgloss.Center
  127. if alignable, ok := child.(Alignable); ok {
  128. alignment = alignable.Alignment()
  129. }
  130. // Calculate actual position based on alignment
  131. actualX, actualY := currentX, currentY
  132. if f.direction == FlexDirectionHorizontal {
  133. // In horizontal layout, vertical alignment affects Y position
  134. // (lipgloss.Center is used for vertical alignment in JoinHorizontal)
  135. actualY = (f.height - paneHeight) / 2
  136. } else {
  137. // In vertical layout, horizontal alignment affects X position
  138. contentWidth := paneWidth
  139. if alignable, ok := child.(Alignable); ok {
  140. if alignable.MaxWidth() > 0 && contentWidth > alignable.MaxWidth() {
  141. contentWidth = alignable.MaxWidth()
  142. }
  143. }
  144. switch alignment {
  145. case lipgloss.Center:
  146. actualX = (f.width - contentWidth) / 2
  147. case lipgloss.Right:
  148. actualX = f.width - contentWidth
  149. case lipgloss.Left:
  150. actualX = 0
  151. }
  152. }
  153. // Set position if the pane is Alignable
  154. if c, ok := child.(Alignable); ok {
  155. c.SetPosition(actualX, actualY)
  156. }
  157. if sizeable, ok := child.(Sizeable); ok {
  158. cmd := sizeable.SetSize(paneWidth, paneHeight)
  159. cmds = append(cmds, cmd)
  160. }
  161. // Update position for next pane
  162. if f.direction == FlexDirectionHorizontal {
  163. currentX += paneWidth
  164. } else {
  165. currentY += paneHeight
  166. }
  167. }
  168. }
  169. return tea.Batch(cmds...)
  170. }
  171. func (f *flexLayout) GetSize() (int, int) {
  172. return f.width, f.height
  173. }
  174. func (f *flexLayout) SetChildren(children []tea.ViewModel) tea.Cmd {
  175. f.children = children
  176. if f.width > 0 && f.height > 0 {
  177. return f.SetSize(f.width, f.height)
  178. }
  179. return nil
  180. }
  181. func (f *flexLayout) SetSizes(sizes []FlexChildSize) tea.Cmd {
  182. f.sizes = sizes
  183. if f.width > 0 && f.height > 0 {
  184. return f.SetSize(f.width, f.height)
  185. }
  186. return nil
  187. }
  188. func (f *flexLayout) SetDirection(direction FlexDirection) tea.Cmd {
  189. f.direction = direction
  190. if f.width > 0 && f.height > 0 {
  191. return f.SetSize(f.width, f.height)
  192. }
  193. return nil
  194. }
  195. func NewFlexLayout(children []tea.ViewModel, options ...FlexLayoutOption) FlexLayout {
  196. layout := &flexLayout{
  197. children: children,
  198. direction: FlexDirectionHorizontal,
  199. sizes: []FlexChildSize{},
  200. }
  201. for _, option := range options {
  202. option(layout)
  203. }
  204. return layout
  205. }
  206. func WithDirection(direction FlexDirection) FlexLayoutOption {
  207. return func(f *flexLayout) {
  208. f.direction = direction
  209. }
  210. }
  211. func WithChildren(children ...tea.ViewModel) FlexLayoutOption {
  212. return func(f *flexLayout) {
  213. f.children = children
  214. }
  215. }
  216. func WithSizes(sizes ...FlexChildSize) FlexLayoutOption {
  217. return func(f *flexLayout) {
  218. f.sizes = sizes
  219. }
  220. }