bento.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. leftExists := false
  165. rightTopExists := false
  166. rightBottomExists := false
  167. if _, ok := b.panes[BentoLeftPane]; ok && !b.hiddenPanes[BentoLeftPane] {
  168. leftExists = true
  169. }
  170. if _, ok := b.panes[BentoRightTopPane]; ok && !b.hiddenPanes[BentoRightTopPane] {
  171. rightTopExists = true
  172. }
  173. if _, ok := b.panes[BentoRightBottomPane]; ok && !b.hiddenPanes[BentoRightBottomPane] {
  174. rightBottomExists = true
  175. }
  176. leftWidth := 0
  177. rightWidth := 0
  178. rightTopHeight := 0
  179. rightBottomHeight := 0
  180. if leftExists && (rightTopExists || rightBottomExists) {
  181. leftWidth = int(float64(width) * b.leftWidthRatio)
  182. if leftWidth < minLeftWidth && width >= minLeftWidth {
  183. leftWidth = minLeftWidth
  184. }
  185. rightWidth = width - leftWidth
  186. if rightTopExists && rightBottomExists {
  187. rightTopHeight = int(float64(height) * b.rightTopHeightRatio)
  188. rightBottomHeight = height - rightTopHeight
  189. if rightBottomHeight < minRightBottomHeight && height >= minRightBottomHeight {
  190. rightBottomHeight = minRightBottomHeight
  191. rightTopHeight = height - rightBottomHeight
  192. }
  193. } else if rightTopExists {
  194. rightTopHeight = height
  195. } else if rightBottomExists {
  196. rightBottomHeight = height
  197. }
  198. } else if leftExists {
  199. leftWidth = width
  200. } else if rightTopExists || rightBottomExists {
  201. rightWidth = width
  202. if rightTopExists && rightBottomExists {
  203. rightTopHeight = int(float64(height) * b.rightTopHeightRatio)
  204. rightBottomHeight = height - rightTopHeight
  205. if rightBottomHeight < minRightBottomHeight && height >= minRightBottomHeight {
  206. rightBottomHeight = minRightBottomHeight
  207. rightTopHeight = height - rightBottomHeight
  208. }
  209. } else if rightTopExists {
  210. rightTopHeight = height
  211. } else if rightBottomExists {
  212. rightBottomHeight = height
  213. }
  214. }
  215. if pane, ok := b.panes[BentoLeftPane]; ok && !b.hiddenPanes[BentoLeftPane] {
  216. pane.SetSize(leftWidth, height)
  217. }
  218. if pane, ok := b.panes[BentoRightTopPane]; ok && !b.hiddenPanes[BentoRightTopPane] {
  219. pane.SetSize(rightWidth, rightTopHeight)
  220. }
  221. if pane, ok := b.panes[BentoRightBottomPane]; ok && !b.hiddenPanes[BentoRightBottomPane] {
  222. pane.SetSize(rightWidth, rightBottomHeight)
  223. }
  224. }
  225. func (b *bentoLayout) HidePane(pane paneID) tea.Cmd {
  226. if len(b.panes)-len(b.hiddenPanes) == 1 {
  227. return nil
  228. }
  229. if _, ok := b.panes[pane]; ok {
  230. b.hiddenPanes[pane] = true
  231. }
  232. b.SetSize(b.width, b.height)
  233. return b.SwitchPane(false)
  234. }
  235. func (b *bentoLayout) SwitchPane(back bool) tea.Cmd {
  236. orderForward := []paneID{BentoLeftPane, BentoRightTopPane, BentoRightBottomPane}
  237. orderBackward := []paneID{BentoLeftPane, BentoRightBottomPane, BentoRightTopPane}
  238. order := orderForward
  239. if back {
  240. order = orderBackward
  241. }
  242. currentIdx := -1
  243. for i, id := range order {
  244. if id == b.currentPane {
  245. currentIdx = i
  246. break
  247. }
  248. }
  249. if currentIdx == -1 {
  250. for _, id := range order {
  251. if _, exists := b.panes[id]; exists {
  252. if _, hidden := b.hiddenPanes[id]; !hidden {
  253. b.currentPane = id
  254. break
  255. }
  256. }
  257. }
  258. } else {
  259. startIdx := currentIdx
  260. for {
  261. currentIdx = (currentIdx + 1) % len(order)
  262. nextID := order[currentIdx]
  263. if _, exists := b.panes[nextID]; exists {
  264. if _, hidden := b.hiddenPanes[nextID]; !hidden {
  265. b.currentPane = nextID
  266. break
  267. }
  268. }
  269. if currentIdx == startIdx {
  270. break
  271. }
  272. }
  273. }
  274. var cmds []tea.Cmd
  275. for id, pane := range b.panes {
  276. if _, ok := b.hiddenPanes[id]; ok {
  277. continue
  278. }
  279. if id == b.currentPane {
  280. cmds = append(cmds, pane.Focus())
  281. } else {
  282. cmds = append(cmds, pane.Blur())
  283. }
  284. }
  285. return tea.Batch(cmds...)
  286. }
  287. func (s *bentoLayout) BindingKeys() []key.Binding {
  288. bindings := KeyMapToSlice(defaultBentoKeyBindings)
  289. if b, ok := s.panes[s.currentPane].(Bindings); ok {
  290. bindings = append(bindings, b.BindingKeys()...)
  291. }
  292. return bindings
  293. }
  294. type BentoLayoutOption func(*bentoLayout)
  295. func NewBentoLayout(panes BentoPanes, opts ...BentoLayoutOption) BentoLayout {
  296. p := make(map[paneID]SinglePaneLayout, len(panes))
  297. for id, pane := range panes {
  298. if sp, ok := pane.(SinglePaneLayout); !ok {
  299. p[id] = NewSinglePane(
  300. pane,
  301. WithSinglePaneFocusable(true),
  302. WithSinglePaneBordered(true),
  303. )
  304. } else {
  305. p[id] = sp
  306. }
  307. }
  308. if len(p) == 0 {
  309. panic("no panes provided for BentoLayout")
  310. }
  311. layout := &bentoLayout{
  312. panes: p,
  313. hiddenPanes: make(map[paneID]bool),
  314. currentPane: BentoLeftPane,
  315. leftWidthRatio: defaultLeftWidthRatio,
  316. rightTopHeightRatio: defaultRightTopHeightRatio,
  317. }
  318. for _, opt := range opts {
  319. opt(layout)
  320. }
  321. return layout
  322. }
  323. func WithBentoLayoutLeftWidthRatio(ratio float64) BentoLayoutOption {
  324. return func(b *bentoLayout) {
  325. if ratio > 0 && ratio < 1 {
  326. b.leftWidthRatio = ratio
  327. }
  328. }
  329. }
  330. func WithBentoLayoutRightTopHeightRatio(ratio float64) BentoLayoutOption {
  331. return func(b *bentoLayout) {
  332. if ratio > 0 && ratio < 1 {
  333. b.rightTopHeightRatio = ratio
  334. }
  335. }
  336. }
  337. func WithBentoLayoutCurrentPane(pane paneID) BentoLayoutOption {
  338. return func(b *bentoLayout) {
  339. b.currentPane = pane
  340. }
  341. }