split.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/sst/opencode/internal/tui/theme"
  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. }
  28. type SplitPaneOption func(*splitPaneLayout)
  29. func (s *splitPaneLayout) Init() tea.Cmd {
  30. var cmds []tea.Cmd
  31. if s.leftPanel != nil {
  32. cmds = append(cmds, s.leftPanel.Init())
  33. }
  34. if s.rightPanel != nil {
  35. cmds = append(cmds, s.rightPanel.Init())
  36. }
  37. if s.bottomPanel != nil {
  38. cmds = append(cmds, s.bottomPanel.Init())
  39. }
  40. return tea.Batch(cmds...)
  41. }
  42. func (s *splitPaneLayout) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  43. var cmds []tea.Cmd
  44. switch msg := msg.(type) {
  45. case tea.WindowSizeMsg:
  46. return s, s.SetSize(msg.Width, msg.Height)
  47. }
  48. if s.rightPanel != nil {
  49. u, cmd := s.rightPanel.Update(msg)
  50. s.rightPanel = u.(Container)
  51. if cmd != nil {
  52. cmds = append(cmds, cmd)
  53. }
  54. }
  55. if s.leftPanel != nil {
  56. u, cmd := s.leftPanel.Update(msg)
  57. s.leftPanel = u.(Container)
  58. if cmd != nil {
  59. cmds = append(cmds, cmd)
  60. }
  61. }
  62. if s.bottomPanel != nil {
  63. u, cmd := s.bottomPanel.Update(msg)
  64. s.bottomPanel = u.(Container)
  65. if cmd != nil {
  66. cmds = append(cmds, cmd)
  67. }
  68. }
  69. return s, tea.Batch(cmds...)
  70. }
  71. func (s *splitPaneLayout) View() string {
  72. var topSection string
  73. if s.leftPanel != nil && s.rightPanel != nil {
  74. leftView := s.leftPanel.View()
  75. rightView := s.rightPanel.View()
  76. topSection = lipgloss.JoinHorizontal(lipgloss.Top, leftView, rightView)
  77. } else if s.leftPanel != nil {
  78. topSection = s.leftPanel.View()
  79. } else if s.rightPanel != nil {
  80. topSection = s.rightPanel.View()
  81. } else {
  82. topSection = ""
  83. }
  84. var finalView string
  85. if s.bottomPanel != nil && topSection != "" {
  86. bottomView := s.bottomPanel.View()
  87. finalView = lipgloss.JoinVertical(lipgloss.Left, topSection, bottomView)
  88. } else if s.bottomPanel != nil {
  89. finalView = s.bottomPanel.View()
  90. } else {
  91. finalView = topSection
  92. }
  93. if finalView != "" {
  94. t := theme.CurrentTheme()
  95. style := lipgloss.NewStyle().
  96. Width(s.width).
  97. Height(s.height).
  98. Background(t.Background())
  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 90% for top section, 10% for bottom
  208. }
  209. for _, option := range options {
  210. option(layout)
  211. }
  212. return layout
  213. }
  214. func WithLeftPanel(panel Container) SplitPaneOption {
  215. return func(s *splitPaneLayout) {
  216. s.leftPanel = panel
  217. }
  218. }
  219. func WithRightPanel(panel Container) SplitPaneOption {
  220. return func(s *splitPaneLayout) {
  221. s.rightPanel = panel
  222. }
  223. }
  224. func WithRatio(ratio float64) SplitPaneOption {
  225. return func(s *splitPaneLayout) {
  226. s.ratio = ratio
  227. }
  228. }
  229. func WithBottomPanel(panel Container) SplitPaneOption {
  230. return func(s *splitPaneLayout) {
  231. s.bottomPanel = panel
  232. }
  233. }
  234. func WithVerticalRatio(ratio float64) SplitPaneOption {
  235. return func(s *splitPaneLayout) {
  236. s.verticalRatio = ratio
  237. }
  238. }