layout.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package layout
  2. import (
  3. "reflect"
  4. "github.com/charmbracelet/bubbles/v2/key"
  5. tea "github.com/charmbracelet/bubbletea/v2"
  6. "github.com/charmbracelet/lipgloss/v2"
  7. )
  8. var Current *LayoutInfo
  9. func init() {
  10. Current = &LayoutInfo{
  11. Viewport: Dimensions{Width: 80, Height: 25},
  12. Container: Dimensions{Width: 80, Height: 25},
  13. }
  14. }
  15. type LayoutSize string
  16. type Dimensions struct {
  17. Width int
  18. Height int
  19. }
  20. type LayoutInfo struct {
  21. Viewport Dimensions
  22. Container Dimensions
  23. }
  24. type Modal interface {
  25. tea.Model
  26. Render(background string) string
  27. Close() tea.Cmd
  28. }
  29. type Focusable interface {
  30. Focus() tea.Cmd
  31. Blur() tea.Cmd
  32. IsFocused() bool
  33. }
  34. type Sizeable interface {
  35. SetSize(width, height int) tea.Cmd
  36. GetSize() (int, int)
  37. }
  38. type Alignable interface {
  39. MaxWidth() int
  40. Alignment() lipgloss.Position
  41. SetPosition(x, y int)
  42. GetPosition() (x, y int)
  43. }
  44. func KeyMapToSlice(t any) (bindings []key.Binding) {
  45. typ := reflect.TypeOf(t)
  46. if typ.Kind() != reflect.Struct {
  47. return nil
  48. }
  49. for i := range typ.NumField() {
  50. v := reflect.ValueOf(t).Field(i)
  51. bindings = append(bindings, v.Interface().(key.Binding))
  52. }
  53. return
  54. }