Browse Source

layout work, not finish

Johan Rong 1 month ago
parent
commit
86da54dc36

+ 2 - 1
cmd/moose/main.go

@@ -43,7 +43,8 @@ func main() {
 			panic(maybePanic)
 		}
 
-		spew.Dump(m.LM.Workspaces[0].RootContainer)
+		spew.Dump(m.LM.Workspaces)
+		spew.Dump(m.BM)
 		fmt.Println(m.DebugLog)
 	}
 	defer quit()

+ 18 - 1
internal/editor/action.go

@@ -52,7 +52,7 @@ func NewChordManager() ChordManager {
 }
 
 func DefaultActionManager() ActionManager {
-	return ActionManager{
+	am := ActionManager{
 		CM:	     NewChordManager(),
 		Common:  []Action{
 			Action{
@@ -530,4 +530,21 @@ func DefaultActionManager() ActionManager {
 			},
 		},
 	}
+
+	for i := range 9 {
+		am.Common = append(am.Common, Action{
+			Bindings: []Binding{
+				Binding{
+					Type:   BindingSingle,
+					Single: "ctrl+" + strconv.Itoa(i + 1),
+				},
+			},
+			Callback: func(m *Model, args []string) {
+				m.Mode = ModeNormal
+				m.LM.ActiveIdx = i
+			},
+		})
+	}
+
+	return am
 }

+ 6 - 1
internal/editor/draw.go

@@ -25,7 +25,8 @@ func (m *Model) Draw() {
 		Height: 2,
 	}
 
-	m.DrawWorkspace(&m.LM.Workspaces[m.LM.ActiveIdx], main)
+	copy := m.LM.Workspaces[m.LM.ActiveIdx]
+	m.DrawWorkspace(&copy, main)
 	m.DrawPalette(palette)	
 }
 
@@ -77,6 +78,10 @@ func (m *Model) DrawWorkspace(w *layout.Workspace, rect layout.Rect) {
 
 func (m *Model) DrawContainer(c *layout.Container, rect layout.Rect) {
 	length := util.NonNilLen(c.Children[:])
+	if length == 0 {
+		return
+	}
+	
 	rect = layout.Rect{
 		X: rect.X,
 		Y: rect.Y,

+ 3 - 2
internal/editor/layout.go

@@ -10,7 +10,8 @@ func (m *Model) AddBuffer(newNode bool) {
 	m.BM.Buffers = append(m.BM.Buffers, b)
 	m.BM.CurrentIdx = idx
 
-	m.LM.Workspaces[m.LM.ActiveIdx].RootContainer.InsertBufferInContainer(idx)
+	//m.LM.Workspaces[m.LM.ActiveIdx].RootContainer.InsertBufferInContainer(idx)
+	m.LM.InsertBuffer(idx, newNode)
 }
 
 func (m *Model) AddBufferFromPath(path string, newNode bool) {
@@ -19,5 +20,5 @@ func (m *Model) AddBufferFromPath(path string, newNode bool) {
 	m.BM.Buffers = append(m.BM.Buffers, b)
 	m.BM.CurrentIdx = idx
 
-	m.LM.Workspaces[m.LM.ActiveIdx].RootContainer.InsertBufferInContainer(idx)
+	m.LM.InsertBuffer(idx, newNode)
 }

+ 1 - 1
internal/editor/model.go

@@ -23,7 +23,7 @@ func NewModel(screen tcell.Screen, style tcell.Style) Model {
 		Style:		   style,
 		Mode:		   ModeNormal,
 		BM:            buffer.BufferManager{
-			Buffers:       []buffer.Buffer{buffer.NewBuffer()},
+			Buffers:       []buffer.Buffer{},
 			CurrentIdx:    0,
 			PaletteBuffer: buffer.NewBuffer(),
 		},

+ 36 - 41
internal/layout/layout.go

@@ -1,7 +1,7 @@
 package layout
 
 type LayoutManager struct {
-	Workspaces []Workspace
+	Workspaces map[int]Workspace
 	ActiveIdx  int
 }
 
@@ -36,7 +36,7 @@ func (Container) isContainerNode() {}
 
 func NewLayoutManager() LayoutManager {
 	return LayoutManager{
-		Workspaces: []Workspace{NewWorkspace()},
+		Workspaces: map[int]Workspace{0: NewWorkspace()},
 	}
 }
 
@@ -48,51 +48,46 @@ func NewWorkspace() Workspace {
 
 func NewContainerEmpty() Container {
 	return Container{
-		Children: 	    [2]ContainerNode{
-			ContainerBuffers{
-				Buffers: []int{0},
-				ActiveIdx: 0,
-			},
-			ContainerBuffers{
-				Buffers: []int{0},
-				ActiveIdx: 0,
-			},
-		},
-		Split:	  	    SplitHorizontal,
+		Children: 	    [2]ContainerNode{},
+		Split:	  	    SplitVertical,
 		ActiveChildIdx: 0,
 	}
 }
 
-// NB: does not set the buffer manager's CurrentIdx, only the layouts active indecies!
-func (c *Container) InsertBufferInContainer(bufferIdx int) {
-	for i := range c.Children {
-		switch child := c.Children[i].(type) {
-		case ContainerBuffers:
-			child.Buffers = append(child.Buffers, bufferIdx)
-			child.ActiveIdx = len(child.Buffers) - 1
-
-			c.Children[i] = child
-			c.ActiveChildIdx = i
-			return
-
-		case Container:
-			child.InsertBufferInContainer(bufferIdx)
-			c.Children[i] = child
-			c.ActiveChildIdx = i
-			return
-		}
-	}
-
-	c.Children[0] = ContainerBuffers{
-		Buffers:   []int{bufferIdx},
-		ActiveIdx: 0,
+func (c *Container) WalkAndMutateActive(fn func(cb *ContainerBuffers) ContainerNode) {
+	switch child := c.Children[c.ActiveChildIdx].(type) {
+	case ContainerBuffers:
+		c.Children[c.ActiveChildIdx] = fn(&child)
+	case Container:
+		child.WalkAndMutateActive(fn)
+		c.Children[c.ActiveChildIdx] = child
+	default:
+		cb := ContainerBuffers{Buffers: []int{}, ActiveIdx: 0}
+		c.Children[c.ActiveChildIdx] = fn(&cb)
 	}
-	c.ActiveChildIdx = 0
 }
 
-// ny funksjon som setter in i aktiv
 func (lm *LayoutManager) InsertBuffer(bufferIdx int, newNode bool) {
-	// follow active child trail to find the active buffer container, if not newNode insert in there using InsertBufferInContainer,
-	// else: make new containerbuffers in that current active container, if it's full then split the containerbuffers into a new containernode
-	// and then a new containerbuffers is made and the bufferidx is put there
+	workspace := lm.Workspaces[lm.ActiveIdx]
+
+	workspace.RootContainer.WalkAndMutateActive(func(cb *ContainerBuffers) ContainerNode {
+		if !newNode || len(cb.Buffers) == 0 {
+			cb.Buffers = append(cb.Buffers, bufferIdx)
+			cb.ActiveIdx = len(cb.Buffers) - 1
+			return *cb
+		}
+
+		cbNew := ContainerBuffers{
+			Buffers:   []int{bufferIdx},
+			ActiveIdx: 0,
+		}
+
+		return Container{
+			Children:       [2]ContainerNode{cbNew, *cb},
+			Split:          SplitHorizontal,
+			ActiveChildIdx: 0,
+		}
+	})
+
+	lm.Workspaces[lm.ActiveIdx] = workspace
 }