container.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 ModelWithView interface {
  8. tea.Model
  9. tea.ViewModel
  10. }
  11. type Container interface {
  12. ModelWithView
  13. Sizeable
  14. Focus()
  15. Blur()
  16. MaxWidth() int
  17. Alignment() lipgloss.Position
  18. GetPosition() (x, y int)
  19. }
  20. type container struct {
  21. width int
  22. height int
  23. x int
  24. y int
  25. content ModelWithView
  26. paddingTop int
  27. paddingRight int
  28. paddingBottom int
  29. paddingLeft int
  30. borderTop bool
  31. borderRight bool
  32. borderBottom bool
  33. borderLeft bool
  34. borderStyle lipgloss.Border
  35. maxWidth int
  36. align lipgloss.Position
  37. focused bool
  38. }
  39. func (c *container) Init() tea.Cmd {
  40. return c.content.Init()
  41. }
  42. func (c *container) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  43. u, cmd := c.content.Update(msg)
  44. c.content = u.(ModelWithView)
  45. return c, cmd
  46. }
  47. func (c *container) View() string {
  48. t := theme.CurrentTheme()
  49. style := lipgloss.NewStyle()
  50. width := c.width
  51. height := c.height
  52. // Apply max width constraint if set
  53. if c.maxWidth > 0 && width > c.maxWidth {
  54. width = c.maxWidth
  55. }
  56. style = style.Background(t.Background())
  57. // Apply border if any side is enabled
  58. if c.borderTop || c.borderRight || c.borderBottom || c.borderLeft {
  59. // Adjust width and height for borders
  60. if c.borderTop {
  61. height--
  62. }
  63. if c.borderBottom {
  64. height--
  65. }
  66. if c.borderLeft {
  67. width--
  68. }
  69. if c.borderRight {
  70. width--
  71. }
  72. style = style.Border(c.borderStyle, c.borderTop, c.borderRight, c.borderBottom, c.borderLeft)
  73. // Use primary color for border if focused
  74. if c.focused {
  75. style = style.BorderBackground(t.Background()).BorderForeground(t.Primary())
  76. } else {
  77. style = style.BorderBackground(t.Background()).BorderForeground(t.Border())
  78. }
  79. }
  80. style = style.
  81. Width(width).
  82. Height(height).
  83. PaddingTop(c.paddingTop).
  84. PaddingRight(c.paddingRight).
  85. PaddingBottom(c.paddingBottom).
  86. PaddingLeft(c.paddingLeft)
  87. return style.Render(c.content.View())
  88. }
  89. func (c *container) SetSize(width, height int) tea.Cmd {
  90. c.width = width
  91. c.height = height
  92. // Apply max width constraint if set
  93. effectiveWidth := width
  94. if c.maxWidth > 0 && width > c.maxWidth {
  95. effectiveWidth = c.maxWidth
  96. }
  97. // If the content implements Sizeable, adjust its size to account for padding and borders
  98. if sizeable, ok := c.content.(Sizeable); ok {
  99. // Calculate horizontal space taken by padding and borders
  100. horizontalSpace := c.paddingLeft + c.paddingRight
  101. if c.borderLeft {
  102. horizontalSpace++
  103. }
  104. if c.borderRight {
  105. horizontalSpace++
  106. }
  107. // Calculate vertical space taken by padding and borders
  108. verticalSpace := c.paddingTop + c.paddingBottom
  109. if c.borderTop {
  110. verticalSpace++
  111. }
  112. if c.borderBottom {
  113. verticalSpace++
  114. }
  115. // Set content size with adjusted dimensions
  116. contentWidth := max(0, effectiveWidth-horizontalSpace)
  117. contentHeight := max(0, height-verticalSpace)
  118. return sizeable.SetSize(contentWidth, contentHeight)
  119. }
  120. return nil
  121. }
  122. func (c *container) GetSize() (int, int) {
  123. return min(c.width, c.maxWidth), c.height
  124. }
  125. func (c *container) MaxWidth() int {
  126. return c.maxWidth
  127. }
  128. func (c *container) Alignment() lipgloss.Position {
  129. return c.align
  130. }
  131. // Focus sets the container as focused
  132. func (c *container) Focus() {
  133. c.focused = true
  134. // Pass focus to content if it supports it
  135. if focusable, ok := c.content.(interface{ Focus() }); ok {
  136. focusable.Focus()
  137. }
  138. }
  139. // Blur removes focus from the container
  140. func (c *container) Blur() {
  141. c.focused = false
  142. // Remove focus from content if it supports it
  143. if blurable, ok := c.content.(interface{ Blur() }); ok {
  144. blurable.Blur()
  145. }
  146. }
  147. // GetPosition returns the x, y coordinates of the container
  148. func (c *container) GetPosition() (x, y int) {
  149. return c.x, c.y
  150. }
  151. type ContainerOption func(*container)
  152. func NewContainer(content ModelWithView, options ...ContainerOption) Container {
  153. c := &container{
  154. content: content,
  155. borderStyle: lipgloss.NormalBorder(),
  156. }
  157. for _, option := range options {
  158. option(c)
  159. }
  160. return c
  161. }
  162. // Padding options
  163. func WithPadding(top, right, bottom, left int) ContainerOption {
  164. return func(c *container) {
  165. c.paddingTop = top
  166. c.paddingRight = right
  167. c.paddingBottom = bottom
  168. c.paddingLeft = left
  169. }
  170. }
  171. func WithPaddingAll(padding int) ContainerOption {
  172. return WithPadding(padding, padding, padding, padding)
  173. }
  174. func WithPaddingHorizontal(padding int) ContainerOption {
  175. return func(c *container) {
  176. c.paddingLeft = padding
  177. c.paddingRight = padding
  178. }
  179. }
  180. func WithPaddingVertical(padding int) ContainerOption {
  181. return func(c *container) {
  182. c.paddingTop = padding
  183. c.paddingBottom = padding
  184. }
  185. }
  186. func WithBorder(top, right, bottom, left bool) ContainerOption {
  187. return func(c *container) {
  188. c.borderTop = top
  189. c.borderRight = right
  190. c.borderBottom = bottom
  191. c.borderLeft = left
  192. }
  193. }
  194. func WithBorderAll() ContainerOption {
  195. return WithBorder(true, true, true, true)
  196. }
  197. func WithBorderHorizontal() ContainerOption {
  198. return WithBorder(true, false, true, false)
  199. }
  200. func WithBorderVertical() ContainerOption {
  201. return WithBorder(false, true, false, true)
  202. }
  203. func WithBorderStyle(style lipgloss.Border) ContainerOption {
  204. return func(c *container) {
  205. c.borderStyle = style
  206. }
  207. }
  208. func WithRoundedBorder() ContainerOption {
  209. return WithBorderStyle(lipgloss.RoundedBorder())
  210. }
  211. func WithThickBorder() ContainerOption {
  212. return WithBorderStyle(lipgloss.ThickBorder())
  213. }
  214. func WithDoubleBorder() ContainerOption {
  215. return WithBorderStyle(lipgloss.DoubleBorder())
  216. }
  217. func WithMaxWidth(maxWidth int) ContainerOption {
  218. return func(c *container) {
  219. c.maxWidth = maxWidth
  220. }
  221. }
  222. func WithAlign(align lipgloss.Position) ContainerOption {
  223. return func(c *container) {
  224. c.align = align
  225. }
  226. }
  227. func WithAlignLeft() ContainerOption {
  228. return WithAlign(lipgloss.Left)
  229. }
  230. func WithAlignCenter() ContainerOption {
  231. return WithAlign(lipgloss.Center)
  232. }
  233. func WithAlignRight() ContainerOption {
  234. return WithAlign(lipgloss.Right)
  235. }