1
0

layout.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package layout
  2. type LayoutManager struct {
  3. Workspaces []Workspace
  4. ActiveIdx int
  5. }
  6. type Workspace struct {
  7. RootContainer Container
  8. }
  9. type SplitType int
  10. const (
  11. SplitHorizontal SplitType = iota
  12. SplitVertical
  13. )
  14. type ContainerNode interface {
  15. isContainerNode()
  16. }
  17. type ContainerBuffers struct {
  18. Buffers []int
  19. ActiveIdx int
  20. }
  21. func (ContainerBuffers) isContainerNode() {}
  22. type Container struct {
  23. Children [2]ContainerNode
  24. Split SplitType
  25. ActiveChildIdx int
  26. }
  27. func (Container) isContainerNode() {}
  28. func NewLayoutManager() LayoutManager {
  29. return LayoutManager{
  30. Workspaces: []Workspace{NewWorkspace()},
  31. }
  32. }
  33. func NewWorkspace() Workspace {
  34. return Workspace{
  35. RootContainer: NewContainerEmpty(),
  36. }
  37. }
  38. func NewContainerEmpty() Container {
  39. return Container{
  40. Children: [2]ContainerNode{
  41. ContainerBuffers{
  42. Buffers: []int{0},
  43. ActiveIdx: 0,
  44. },
  45. ContainerBuffers{
  46. Buffers: []int{0},
  47. ActiveIdx: 0,
  48. },
  49. },
  50. Split: SplitHorizontal,
  51. ActiveChildIdx: 0,
  52. }
  53. }
  54. // NB: does not set the buffer manager's CurrentIdx, only the layouts active indecies!
  55. func (c *Container) InsertBufferInContainer(bufferIdx int) {
  56. for i := range c.Children {
  57. switch child := c.Children[i].(type) {
  58. case ContainerBuffers:
  59. child.Buffers = append(child.Buffers, bufferIdx)
  60. child.ActiveIdx = len(child.Buffers) - 1
  61. c.Children[i] = child
  62. c.ActiveChildIdx = i
  63. return
  64. case Container:
  65. child.InsertBufferInContainer(bufferIdx)
  66. c.Children[i] = child
  67. c.ActiveChildIdx = i
  68. return
  69. }
  70. }
  71. c.Children[0] = ContainerBuffers{
  72. Buffers: []int{bufferIdx},
  73. ActiveIdx: 0,
  74. }
  75. c.ActiveChildIdx = 0
  76. }
  77. // ny funksjon som setter in i aktiv
  78. func (lm *LayoutManager) InsertBuffer(bufferIdx int, newNode bool) {
  79. // follow active child trail to find the active buffer container, if not newNode insert in there using InsertBufferInContainer,
  80. // else: make new containerbuffers in that current active container, if it's full then split the containerbuffers into a new containernode
  81. // and then a new containerbuffers is made and the bufferidx is put there
  82. }