]> git.lizzy.rs Git - micro.git/blob - cmd/micro/action/tab.go
Implement split resizing
[micro.git] / cmd / micro / action / tab.go
1 package action
2
3 import (
4         "github.com/zyedidia/micro/cmd/micro/buffer"
5         "github.com/zyedidia/micro/cmd/micro/display"
6         "github.com/zyedidia/micro/cmd/micro/screen"
7         "github.com/zyedidia/micro/cmd/micro/views"
8         "github.com/zyedidia/tcell"
9 )
10
11 var MainTab *TabPane
12
13 type TabPane struct {
14         *views.Node
15         display.Window
16         Panes  []*EditPane
17         active int
18
19         resizing *views.Node // node currently being resized
20 }
21
22 func (t *TabPane) HandleEvent(event tcell.Event) {
23         switch e := event.(type) {
24         case *tcell.EventResize:
25                 w, h := screen.Screen.Size()
26                 InfoBar.Resize(w, h-1)
27                 t.Node.Resize(w, h-1)
28                 t.Resize()
29         case *tcell.EventMouse:
30                 switch e.Buttons() {
31                 case tcell.Button1:
32                         mx, my := e.Position()
33
34                         resizeID := t.GetMouseLoc(buffer.Loc{mx, my}).X
35                         if t.resizing != nil {
36                                 var size int
37                                 if t.resizing.Kind == views.STVert {
38                                         size = mx - t.resizing.X
39                                 } else {
40                                         size = my - t.resizing.Y + 1
41                                 }
42                                 t.resizing.ResizeSplit(size)
43                                 t.Resize()
44                                 return
45                         }
46
47                         if resizeID != -1 {
48                                 t.resizing = t.GetNode(uint64(resizeID))
49                                 return
50                         }
51
52                         for i, p := range t.Panes {
53                                 v := p.GetView()
54                                 inpane := mx >= v.X && mx < v.X+v.Width && my >= v.Y && my < v.Y+v.Height
55                                 if inpane {
56                                         t.active = i
57                                         p.SetActive(true)
58                                 } else {
59                                         p.SetActive(false)
60                                 }
61                         }
62                 case tcell.ButtonNone:
63                         t.resizing = nil
64                 }
65
66         }
67         t.Panes[t.active].HandleEvent(event)
68 }
69
70 func (t *TabPane) SetActive(i int) {
71         t.active = i
72         for j, p := range t.Panes {
73                 if j == i {
74                         p.SetActive(true)
75                 } else {
76                         p.SetActive(false)
77                 }
78         }
79 }
80
81 func (t *TabPane) GetPane(splitid uint64) int {
82         for i, p := range t.Panes {
83                 if p.splitID == splitid {
84                         return i
85                 }
86         }
87         return 0
88 }
89
90 func (t *TabPane) RemovePane(i int) {
91         copy(t.Panes[i:], t.Panes[i+1:])
92         t.Panes[len(t.Panes)-1] = nil // or the zero value of T
93         t.Panes = t.Panes[:len(t.Panes)-1]
94 }
95
96 func (t *TabPane) Resize() {
97         for i, p := range t.Panes {
98                 n := t.GetNode(p.splitID)
99                 pv := p.GetView()
100                 offset := 0
101                 if i != 0 {
102                         offset = 1
103                 }
104                 pv.X, pv.Y = n.X+offset, n.Y
105                 p.SetView(pv)
106                 p.Resize(n.W-offset, n.H)
107         }
108 }
109
110 func (t *TabPane) CurPane() *EditPane {
111         return t.Panes[t.active]
112 }