bento.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package layout
  2. import (
  3. "github.com/charmbracelet/bubbles/key"
  4. tea "github.com/charmbracelet/bubbletea"
  5. "github.com/charmbracelet/lipgloss"
  6. )
  7. type paneID string
  8. const (
  9. BentoLeftPane paneID = "left"
  10. BentoRightTopPane paneID = "right-top"
  11. BentoRightBottomPane paneID = "right-bottom"
  12. )
  13. type BentoPanes map[paneID]tea.Model
  14. const (
  15. defaultLeftWidthRatio = 0.2
  16. defaultRightTopHeightRatio = 0.85
  17. minLeftWidth = 10
  18. minRightBottomHeight = 10
  19. )
  20. type BentoLayout interface {
  21. tea.Model
  22. Sizeable
  23. Bindings
  24. }
  25. type BentoKeyBindings struct {
  26. SwitchPane key.Binding
  27. SwitchPaneBack key.Binding
  28. HideCurrentPane key.Binding
  29. ShowAllPanes key.Binding
  30. }
  31. var defaultBentoKeyBindings = BentoKeyBindings{
  32. SwitchPane: key.NewBinding(
  33. key.WithKeys("tab"),
  34. key.WithHelp("tab", "switch pane"),
  35. ),
  36. SwitchPaneBack: key.NewBinding(
  37. key.WithKeys("shift+tab"),
  38. key.WithHelp("shift+tab", "switch pane back"),
  39. ),
  40. HideCurrentPane: key.NewBinding(
  41. key.WithKeys("X"),
  42. key.WithHelp("X", "hide current pane"),
  43. ),
  44. ShowAllPanes: key.NewBinding(
  45. key.WithKeys("R"),
  46. key.WithHelp("R", "show all panes"),
  47. ),
  48. }
  49. type bentoLayout struct {
  50. width int
  51. height int
  52. leftWidthRatio float64
  53. rightTopHeightRatio float64
  54. currentPane paneID
  55. panes map[paneID]SinglePaneLayout
  56. hiddenPanes map[paneID]bool
  57. }
  58. func (b *bentoLayout) GetSize() (int, int) {
  59. return b.width, b.height
  60. }
  61. func (b *bentoLayout) Init() tea.Cmd {
  62. var cmds []tea.Cmd
  63. for _, pane := range b.panes {
  64. cmd := pane.Init()
  65. if cmd != nil {
  66. cmds = append(cmds, cmd)
  67. }
  68. }
  69. return tea.Batch(cmds...)
  70. }
  71. func (b *bentoLayout) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  72. switch msg := msg.(type) {
  73. case tea.WindowSizeMsg:
  74. b.SetSize(msg.Width, msg.Height)
  75. return b, nil
  76. case tea.KeyMsg:
  77. switch {
  78. case key.Matches(msg, defaultBentoKeyBindings.SwitchPane):
  79. return b, b.SwitchPane(false)
  80. case key.Matches(msg, defaultBentoKeyBindings.SwitchPaneBack):
  81. return b, b.SwitchPane(true)
  82. case key.Matches(msg, defaultBentoKeyBindings.HideCurrentPane):
  83. return b, b.HidePane(b.currentPane)
  84. case key.Matches(msg, defaultBentoKeyBindings.ShowAllPanes):
  85. for id := range b.hiddenPanes {
  86. delete(b.hiddenPanes, id)
  87. }
  88. b.SetSize(b.width, b.height)
  89. return b, nil
  90. }
  91. }
  92. var cmds []tea.Cmd
  93. for id, pane := range b.panes {
  94. u, cmd := pane.Update(msg)
  95. b.panes[id] = u.(SinglePaneLayout)
  96. if cmd != nil {
  97. cmds = append(cmds, cmd)
  98. }
  99. }
  100. return b, tea.Batch(cmds...)
  101. }
  102. func (b *bentoLayout) View() string {
  103. if b.width <= 0 || b.height <= 0 {
  104. return ""
  105. }
  106. for id, pane := range b.panes {
  107. if b.currentPane == id {
  108. pane.Focus()
  109. } else {
  110. pane.Blur()
  111. }
  112. }
  113. leftVisible := false
  114. rightTopVisible := false
  115. rightBottomVisible := false
  116. var leftPane, rightTopPane, rightBottomPane string
  117. if pane, ok := b.panes[BentoLeftPane]; ok && !b.hiddenPanes[BentoLeftPane] {
  118. leftPane = pane.View()
  119. leftVisible = true
  120. }
  121. if pane, ok := b.panes[BentoRightTopPane]; ok && !b.hiddenPanes[BentoRightTopPane] {
  122. rightTopPane = pane.View()
  123. rightTopVisible = true
  124. }
  125. if pane, ok := b.panes[BentoRightBottomPane]; ok && !b.hiddenPanes[BentoRightBottomPane] {
  126. rightBottomPane = pane.View()
  127. rightBottomVisible = true
  128. }
  129. if leftVisible {
  130. if rightTopVisible || rightBottomVisible {
  131. rightSection := ""
  132. if rightTopVisible && rightBottomVisible {
  133. rightSection = lipgloss.JoinVertical(lipgloss.Top, rightTopPane, rightBottomPane)
  134. } else if rightTopVisible {
  135. rightSection = rightTopPane
  136. } else {
  137. rightSection = rightBottomPane
  138. }
  139. return lipgloss.NewStyle().Width(b.width).Height(b.height).Render(
  140. lipgloss.JoinHorizontal(lipgloss.Left, leftPane, rightSection),
  141. )
  142. } else {
  143. return lipgloss.NewStyle().Width(b.width).Height(b.height).Render(leftPane)
  144. }
  145. } else if rightTopVisible || rightBottomVisible {
  146. if rightTopVisible && rightBottomVisible {
  147. return lipgloss.NewStyle().Width(b.width).Height(b.height).Render(
  148. lipgloss.JoinVertical(lipgloss.Top, rightTopPane, rightBottomPane),
  149. )
  150. } else if rightTopVisible {
  151. return lipgloss.NewStyle().Width(b.width).Height(b.height).Render(rightTopPane)
  152. } else {
  153. return lipgloss.NewStyle().Width(b.width).Height(b.height).Render(rightBottomPane)
  154. }
  155. }
  156. return ""
  157. }
  158. func (b *bentoLayout) SetSize(width int, height int) {
  159. if width < 0 || height < 0 {
  160. return
  161. }
  162. b.width = width
  163. b.height = height
  164. // Check which panes are available
  165. leftExists := false
  166. rightTopExists := false
  167. rightBottomExists := false
  168. if _, ok := b.panes[BentoLeftPane]; ok && !b.hiddenPanes[BentoLeftPane] {
  169. leftExists = true
  170. }
  171. if _, ok := b.panes[BentoRightTopPane]; ok && !b.hiddenPanes[BentoRightTopPane] {
  172. rightTopExists = true
  173. }
  174. if _, ok := b.panes[BentoRightBottomPane]; ok && !b.hiddenPanes[BentoRightBottomPane] {
  175. rightBottomExists = true
  176. }
  177. leftWidth := 0
  178. rightWidth := 0
  179. rightTopHeight := 0
  180. rightBottomHeight := 0
  181. if leftExists && (rightTopExists || rightBottomExists) {
  182. leftWidth = int(float64(width) * b.leftWidthRatio)
  183. if leftWidth < minLeftWidth && width >= minLeftWidth {
  184. leftWidth = minLeftWidth
  185. }
  186. rightWidth = width - leftWidth
  187. if rightTopExists && rightBottomExists {
  188. rightTopHeight = int(float64(height) * b.rightTopHeightRatio)
  189. rightBottomHeight = height - rightTopHeight
  190. // Ensure minimum height for bottom pane
  191. if rightBottomHeight < minRightBottomHeight && height >= minRightBottomHeight {
  192. rightBottomHeight = minRightBottomHeight
  193. rightTopHeight = height - rightBottomHeight
  194. }
  195. } else if rightTopExists {
  196. rightTopHeight = height
  197. } else if rightBottomExists {
  198. rightBottomHeight = height
  199. }
  200. } else if leftExists {
  201. leftWidth = width
  202. } else if rightTopExists || rightBottomExists {
  203. rightWidth = width
  204. if rightTopExists && rightBottomExists {
  205. rightTopHeight = int(float64(height) * b.rightTopHeightRatio)
  206. rightBottomHeight = height - rightTopHeight
  207. if rightBottomHeight < minRightBottomHeight && height >= minRightBottomHeight {
  208. rightBottomHeight = minRightBottomHeight
  209. rightTopHeight = height - rightBottomHeight
  210. }
  211. } else if rightTopExists {
  212. rightTopHeight = height
  213. } else if rightBottomExists {
  214. rightBottomHeight = height
  215. }
  216. }
  217. if pane, ok := b.panes[BentoLeftPane]; ok && !b.hiddenPanes[BentoLeftPane] {
  218. pane.SetSize(leftWidth, height)
  219. }
  220. if pane, ok := b.panes[BentoRightTopPane]; ok && !b.hiddenPanes[BentoRightTopPane] {
  221. pane.SetSize(rightWidth, rightTopHeight)
  222. }
  223. if pane, ok := b.panes[BentoRightBottomPane]; ok && !b.hiddenPanes[BentoRightBottomPane] {
  224. pane.SetSize(rightWidth, rightBottomHeight)
  225. }
  226. }
  227. func (b *bentoLayout) HidePane(pane paneID) tea.Cmd {
  228. if len(b.panes)-len(b.hiddenPanes) == 1 {
  229. return nil
  230. }
  231. if _, ok := b.panes[pane]; ok {
  232. b.hiddenPanes[pane] = true
  233. }
  234. b.SetSize(b.width, b.height)
  235. return b.SwitchPane(false)
  236. }
  237. func (b *bentoLayout) SwitchPane(back bool) tea.Cmd {
  238. if back {
  239. switch b.currentPane {
  240. case BentoLeftPane:
  241. b.currentPane = BentoRightBottomPane
  242. case BentoRightTopPane:
  243. b.currentPane = BentoLeftPane
  244. case BentoRightBottomPane:
  245. b.currentPane = BentoRightTopPane
  246. }
  247. } else {
  248. switch b.currentPane {
  249. case BentoLeftPane:
  250. b.currentPane = BentoRightTopPane
  251. case BentoRightTopPane:
  252. b.currentPane = BentoRightBottomPane
  253. case BentoRightBottomPane:
  254. b.currentPane = BentoLeftPane
  255. }
  256. }
  257. var cmds []tea.Cmd
  258. for id, pane := range b.panes {
  259. if _, ok := b.hiddenPanes[id]; ok {
  260. continue
  261. }
  262. if id == b.currentPane {
  263. cmds = append(cmds, pane.Focus())
  264. } else {
  265. cmds = append(cmds, pane.Blur())
  266. }
  267. }
  268. return tea.Batch(cmds...)
  269. }
  270. func (s *bentoLayout) BindingKeys() []key.Binding {
  271. bindings := KeyMapToSlice(defaultBentoKeyBindings)
  272. if b, ok := s.panes[s.currentPane].(Bindings); ok {
  273. bindings = append(bindings, b.BindingKeys()...)
  274. }
  275. return bindings
  276. }
  277. type BentoLayoutOption func(*bentoLayout)
  278. func NewBentoLayout(panes BentoPanes, opts ...BentoLayoutOption) BentoLayout {
  279. p := make(map[paneID]SinglePaneLayout, len(panes))
  280. for id, pane := range panes {
  281. // Wrap any pane that is not a SinglePaneLayout in a SinglePaneLayout
  282. if sp, ok := pane.(SinglePaneLayout); !ok {
  283. p[id] = NewSinglePane(
  284. pane,
  285. WithSinglePaneFocusable(true),
  286. WithSinglePaneBordered(true),
  287. )
  288. } else {
  289. p[id] = sp
  290. }
  291. }
  292. if len(p) == 0 {
  293. panic("no panes provided for BentoLayout")
  294. }
  295. layout := &bentoLayout{
  296. panes: p,
  297. hiddenPanes: make(map[paneID]bool),
  298. currentPane: BentoLeftPane,
  299. leftWidthRatio: defaultLeftWidthRatio,
  300. rightTopHeightRatio: defaultRightTopHeightRatio,
  301. }
  302. for _, opt := range opts {
  303. opt(layout)
  304. }
  305. return layout
  306. }
  307. func WithBentoLayoutLeftWidthRatio(ratio float64) BentoLayoutOption {
  308. return func(b *bentoLayout) {
  309. if ratio > 0 && ratio < 1 {
  310. b.leftWidthRatio = ratio
  311. }
  312. }
  313. }
  314. func WithBentoLayoutRightTopHeightRatio(ratio float64) BentoLayoutOption {
  315. return func(b *bentoLayout) {
  316. if ratio > 0 && ratio < 1 {
  317. b.rightTopHeightRatio = ratio
  318. }
  319. }
  320. }
  321. func WithBentoLayoutCurrentPane(pane paneID) BentoLayoutOption {
  322. return func(b *bentoLayout) {
  323. b.currentPane = pane
  324. }
  325. }