split.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package layout
  2. import (
  3. "github.com/charmbracelet/bubbles/key"
  4. tea "github.com/charmbracelet/bubbletea"
  5. "github.com/charmbracelet/lipgloss"
  6. "github.com/opencode-ai/opencode/internal/tui/styles"
  7. )
  8. type SplitPaneLayout interface {
  9. tea.Model
  10. Sizeable
  11. Bindings
  12. SetLeftPanel(panel Container) tea.Cmd
  13. SetRightPanel(panel Container) tea.Cmd
  14. SetBottomPanel(panel Container) tea.Cmd
  15. ClearLeftPanel() tea.Cmd
  16. ClearRightPanel() tea.Cmd
  17. ClearBottomPanel() tea.Cmd
  18. }
  19. type splitPaneLayout struct {
  20. width int
  21. height int
  22. ratio float64
  23. verticalRatio float64
  24. rightPanel Container
  25. leftPanel Container
  26. bottomPanel Container
  27. backgroundColor lipgloss.TerminalColor
  28. }
  29. type SplitPaneOption func(*splitPaneLayout)
  30. func (s *splitPaneLayout) Init() tea.Cmd {
  31. var cmds []tea.Cmd
  32. if s.leftPanel != nil {
  33. cmds = append(cmds, s.leftPanel.Init())
  34. }
  35. if s.rightPanel != nil {
  36. cmds = append(cmds, s.rightPanel.Init())
  37. }
  38. if s.bottomPanel != nil {
  39. cmds = append(cmds, s.bottomPanel.Init())
  40. }
  41. return tea.Batch(cmds...)
  42. }
  43. func (s *splitPaneLayout) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  44. var cmds []tea.Cmd
  45. switch msg := msg.(type) {
  46. case tea.WindowSizeMsg:
  47. return s, s.SetSize(msg.Width, msg.Height)
  48. }
  49. if s.rightPanel != nil {
  50. u, cmd := s.rightPanel.Update(msg)
  51. s.rightPanel = u.(Container)
  52. if cmd != nil {
  53. cmds = append(cmds, cmd)
  54. }
  55. }
  56. if s.leftPanel != nil {
  57. u, cmd := s.leftPanel.Update(msg)
  58. s.leftPanel = u.(Container)
  59. if cmd != nil {
  60. cmds = append(cmds, cmd)
  61. }
  62. }
  63. if s.bottomPanel != nil {
  64. u, cmd := s.bottomPanel.Update(msg)
  65. s.bottomPanel = u.(Container)
  66. if cmd != nil {
  67. cmds = append(cmds, cmd)
  68. }
  69. }
  70. return s, tea.Batch(cmds...)
  71. }
  72. func (s *splitPaneLayout) View() string {
  73. var topSection string
  74. if s.leftPanel != nil && s.rightPanel != nil {
  75. leftView := s.leftPanel.View()
  76. rightView := s.rightPanel.View()
  77. topSection = lipgloss.JoinHorizontal(lipgloss.Top, leftView, rightView)
  78. } else if s.leftPanel != nil {
  79. topSection = s.leftPanel.View()
  80. } else if s.rightPanel != nil {
  81. topSection = s.rightPanel.View()
  82. } else {
  83. topSection = ""
  84. }
  85. var finalView string
  86. if s.bottomPanel != nil && topSection != "" {
  87. bottomView := s.bottomPanel.View()
  88. finalView = lipgloss.JoinVertical(lipgloss.Left, topSection, bottomView)
  89. } else if s.bottomPanel != nil {
  90. finalView = s.bottomPanel.View()
  91. } else {
  92. finalView = topSection
  93. }
  94. if s.backgroundColor != nil && finalView != "" {
  95. style := lipgloss.NewStyle().
  96. Width(s.width).
  97. Height(s.height).
  98. Background(s.backgroundColor)
  99. return style.Render(finalView)
  100. }
  101. return finalView
  102. }
  103. func (s *splitPaneLayout) SetSize(width, height int) tea.Cmd {
  104. s.width = width
  105. s.height = height
  106. var topHeight, bottomHeight int
  107. if s.bottomPanel != nil {
  108. topHeight = int(float64(height) * s.verticalRatio)
  109. bottomHeight = height - topHeight
  110. } else {
  111. topHeight = height
  112. bottomHeight = 0
  113. }
  114. var leftWidth, rightWidth int
  115. if s.leftPanel != nil && s.rightPanel != nil {
  116. leftWidth = int(float64(width) * s.ratio)
  117. rightWidth = width - leftWidth
  118. } else if s.leftPanel != nil {
  119. leftWidth = width
  120. rightWidth = 0
  121. } else if s.rightPanel != nil {
  122. leftWidth = 0
  123. rightWidth = width
  124. }
  125. var cmds []tea.Cmd
  126. if s.leftPanel != nil {
  127. cmd := s.leftPanel.SetSize(leftWidth, topHeight)
  128. cmds = append(cmds, cmd)
  129. }
  130. if s.rightPanel != nil {
  131. cmd := s.rightPanel.SetSize(rightWidth, topHeight)
  132. cmds = append(cmds, cmd)
  133. }
  134. if s.bottomPanel != nil {
  135. cmd := s.bottomPanel.SetSize(width, bottomHeight)
  136. cmds = append(cmds, cmd)
  137. }
  138. return tea.Batch(cmds...)
  139. }
  140. func (s *splitPaneLayout) GetSize() (int, int) {
  141. return s.width, s.height
  142. }
  143. func (s *splitPaneLayout) SetLeftPanel(panel Container) tea.Cmd {
  144. s.leftPanel = panel
  145. if s.width > 0 && s.height > 0 {
  146. return s.SetSize(s.width, s.height)
  147. }
  148. return nil
  149. }
  150. func (s *splitPaneLayout) SetRightPanel(panel Container) tea.Cmd {
  151. s.rightPanel = panel
  152. if s.width > 0 && s.height > 0 {
  153. return s.SetSize(s.width, s.height)
  154. }
  155. return nil
  156. }
  157. func (s *splitPaneLayout) SetBottomPanel(panel Container) tea.Cmd {
  158. s.bottomPanel = panel
  159. if s.width > 0 && s.height > 0 {
  160. return s.SetSize(s.width, s.height)
  161. }
  162. return nil
  163. }
  164. func (s *splitPaneLayout) ClearLeftPanel() tea.Cmd {
  165. s.leftPanel = nil
  166. if s.width > 0 && s.height > 0 {
  167. return s.SetSize(s.width, s.height)
  168. }
  169. return nil
  170. }
  171. func (s *splitPaneLayout) ClearRightPanel() tea.Cmd {
  172. s.rightPanel = nil
  173. if s.width > 0 && s.height > 0 {
  174. return s.SetSize(s.width, s.height)
  175. }
  176. return nil
  177. }
  178. func (s *splitPaneLayout) ClearBottomPanel() tea.Cmd {
  179. s.bottomPanel = nil
  180. if s.width > 0 && s.height > 0 {
  181. return s.SetSize(s.width, s.height)
  182. }
  183. return nil
  184. }
  185. func (s *splitPaneLayout) BindingKeys() []key.Binding {
  186. keys := []key.Binding{}
  187. if s.leftPanel != nil {
  188. if b, ok := s.leftPanel.(Bindings); ok {
  189. keys = append(keys, b.BindingKeys()...)
  190. }
  191. }
  192. if s.rightPanel != nil {
  193. if b, ok := s.rightPanel.(Bindings); ok {
  194. keys = append(keys, b.BindingKeys()...)
  195. }
  196. }
  197. if s.bottomPanel != nil {
  198. if b, ok := s.bottomPanel.(Bindings); ok {
  199. keys = append(keys, b.BindingKeys()...)
  200. }
  201. }
  202. return keys
  203. }
  204. func NewSplitPane(options ...SplitPaneOption) SplitPaneLayout {
  205. layout := &splitPaneLayout{
  206. ratio: 0.7,
  207. verticalRatio: 0.9, // Default 80% for top section, 20% for bottom
  208. backgroundColor: styles.Background,
  209. }
  210. for _, option := range options {
  211. option(layout)
  212. }
  213. return layout
  214. }
  215. func WithLeftPanel(panel Container) SplitPaneOption {
  216. return func(s *splitPaneLayout) {
  217. s.leftPanel = panel
  218. }
  219. }
  220. func WithRightPanel(panel Container) SplitPaneOption {
  221. return func(s *splitPaneLayout) {
  222. s.rightPanel = panel
  223. }
  224. }
  225. func WithRatio(ratio float64) SplitPaneOption {
  226. return func(s *splitPaneLayout) {
  227. s.ratio = ratio
  228. }
  229. }
  230. func WithSplitBackgroundColor(color lipgloss.TerminalColor) SplitPaneOption {
  231. return func(s *splitPaneLayout) {
  232. s.backgroundColor = color
  233. }
  234. }
  235. func WithBottomPanel(panel Container) SplitPaneOption {
  236. return func(s *splitPaneLayout) {
  237. s.bottomPanel = panel
  238. }
  239. }
  240. func WithVerticalRatio(ratio float64) SplitPaneOption {
  241. return func(s *splitPaneLayout) {
  242. s.verticalRatio = ratio
  243. }
  244. }