1
0

layout.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package layout
  2. import (
  3. "github.com/gdamore/tcell/v3"
  4. )
  5. type Rect struct {
  6. X int
  7. Y int
  8. Width int
  9. Height int
  10. }
  11. func RectDivide(rect Rect, split SplitType, num int) Rect {
  12. switch split {
  13. case SplitHorizontal: return Rect{
  14. X: rect.X,
  15. Y: rect.Y,
  16. Width: rect.Width / num,
  17. Height: rect.Height,
  18. }
  19. case SplitVertical: return Rect{
  20. X: rect.X,
  21. Y: rect.Y,
  22. Width: rect.Width,
  23. Height: rect.Height / num,
  24. }
  25. default: panic("[moose-error] impossible split type")
  26. }
  27. }
  28. func RectDisplace(rect Rect, split SplitType, idx int) Rect {
  29. switch split {
  30. case SplitHorizontal: return Rect{
  31. X: rect.X + (rect.Width * idx),
  32. Y: rect.Y,
  33. Width: rect.Width,
  34. Height: rect.Height,
  35. }
  36. case SplitVertical: return Rect{
  37. X: rect.X,
  38. Y: rect.Y + (rect.Height * idx),
  39. Width: rect.Width,
  40. Height: rect.Height,
  41. }
  42. default: panic("[moose-error] impossible split type")
  43. }
  44. }
  45. func RectFromScren(s tcell.Screen) Rect {
  46. sWidth, sHeight := s.Size()
  47. return Rect{
  48. X: 0,
  49. Y: 0,
  50. Width: sWidth,
  51. Height: sHeight,
  52. }
  53. }
  54. type LayoutManager struct {
  55. Workspaces []Workspace
  56. ActiveIdx int
  57. }
  58. type Workspace struct {
  59. RootContainer Container[ContainerBuffers]
  60. }
  61. type SplitType int
  62. const (
  63. SplitHorizontal SplitType = iota
  64. SplitVertical
  65. )
  66. type ContainerNode interface {
  67. ContainerBuffers | Container[ContainerBuffers]
  68. }
  69. type ContainerBuffers struct {
  70. Buffers []int
  71. ActiveIdx int
  72. }
  73. type Container[T ContainerNode] struct {
  74. Children [2]any
  75. Split SplitType
  76. ActiveChildIdx int
  77. }
  78. func NewLayoutManager() LayoutManager {
  79. return LayoutManager{
  80. Workspaces: []Workspace{NewWorkspace()},
  81. }
  82. }
  83. func NewWorkspace() Workspace {
  84. return Workspace{
  85. RootContainer: NewContainerEmpty(),
  86. }
  87. }
  88. func NewContainerEmpty() Container[ContainerBuffers] {
  89. return Container[ContainerBuffers]{
  90. Children: [2]any{
  91. ContainerBuffers{
  92. Buffers: []int{0},
  93. ActiveIdx: 0,
  94. },
  95. ContainerBuffers{
  96. Buffers: []int{1},
  97. ActiveIdx: 0,
  98. },
  99. },
  100. Split: SplitVertical,
  101. ActiveChildIdx: 0,
  102. }
  103. }