layout.go 1.1 KB

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