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"
16 func NewTabList(bufs []*buffer.Buffer) *TabList {
17 w, h := screen.Screen.Size()
19 tl.List = make([]*Tab, len(bufs))
21 for i, b := range bufs {
22 tl.List[i] = NewTabFromBuffer(0, 1, w, h-2, b)
25 tl.List[0] = NewTabFromBuffer(0, 0, w, h-1, bufs[0])
27 tl.TabWindow = display.NewTabWindow(w, 0)
28 tl.Names = make([]string, len(bufs))
33 func (t *TabList) UpdateNames() {
35 for _, p := range t.List {
36 t.Names = append(t.Names, p.Panes[p.active].Name())
40 func (t *TabList) AddTab(p *Tab) {
41 t.List = append(t.List, p)
46 func (t *TabList) RemoveTab(id uint64) {
47 for i, p := range t.List {
48 if len(p.Panes) == 0 {
51 if p.Panes[0].ID() == id {
52 copy(t.List[i:], t.List[i+1:])
53 t.List[len(t.List)-1] = nil
54 t.List = t.List[:len(t.List)-1]
55 if t.Active() >= len(t.List) {
56 t.SetActive(len(t.List) - 1)
65 func (t *TabList) Resize() {
66 w, h := screen.Screen.Size()
67 InfoBar.Resize(w, h-1)
69 for _, p := range t.List {
74 } else if len(t.List) == 1 {
76 t.List[0].Node.Resize(w, h-1)
81 func (t *TabList) HandleEvent(event tcell.Event) {
82 switch e := event.(type) {
83 case *tcell.EventResize:
85 case *tcell.EventMouse:
86 mx, my := e.Position()
89 ind := t.GetMouseLoc(buffer.Loc{mx, my})
105 t.List[t.Active()].HandleEvent(event)
108 func (t *TabList) Display() {
111 t.TabWindow.Display()
117 func InitTabs(bufs []*buffer.Buffer) {
118 Tabs = NewTabList(bufs)
121 func MainTab() *Tab {
122 return Tabs.List[Tabs.Active()]
125 // A Tab represents a single tab
126 // It consists of a list of edit panes (the open buffers),
127 // a split tree (stored as just the root node), and a uiwindow
128 // to display the UI elements like the borders between splits
135 resizing *views.Node // node currently being resized
138 func NewTabFromBuffer(x, y, width, height int, b *buffer.Buffer) *Tab {
140 t.Node = views.NewRoot(x, y, width, height)
141 t.UIWindow = display.NewUIWindow(t.Node)
143 e := NewBufEditPane(x, y, width, height, b)
146 t.Panes = append(t.Panes, e)
150 // HandleEvent takes a tcell event and usually dispatches it to the current
151 // active pane. However if the event is a resize or a mouse event where the user
152 // is interacting with the UI (resizing splits) then the event is consumed here
153 // If the event is a mouse event in a pane, that pane will become active and get
155 func (t *Tab) HandleEvent(event tcell.Event) {
156 switch e := event.(type) {
157 case *tcell.EventMouse:
158 mx, my := e.Position()
161 resizeID := t.GetMouseSplitID(buffer.Loc{mx, my})
162 if t.resizing != nil {
164 if t.resizing.Kind == views.STVert {
165 size = mx - t.resizing.X
167 size = my - t.resizing.Y + 1
169 t.resizing.ResizeSplit(size)
175 t.resizing = t.GetNode(uint64(resizeID))
179 for i, p := range t.Panes {
181 inpane := mx >= v.X && mx < v.X+v.Width && my >= v.Y && my < v.Y+v.Height
189 case tcell.ButtonNone:
192 for _, p := range t.Panes {
194 inpane := mx >= v.X && mx < v.X+v.Width && my >= v.Y && my < v.Y+v.Height
203 t.Panes[t.active].HandleEvent(event)
206 // SetActive changes the currently active pane to the specified index
207 func (t *Tab) SetActive(i int) {
209 for j, p := range t.Panes {
218 // GetPane returns the pane with the given split index
219 func (t *Tab) GetPane(splitid uint64) int {
220 for i, p := range t.Panes {
221 if p.ID() == splitid {
228 // Remove pane removes the pane with the given index
229 func (t *Tab) RemovePane(i int) {
230 copy(t.Panes[i:], t.Panes[i+1:])
231 t.Panes[len(t.Panes)-1] = nil
232 t.Panes = t.Panes[:len(t.Panes)-1]
235 // Resize resizes all panes according to their corresponding split nodes
236 func (t *Tab) Resize() {
237 for _, p := range t.Panes {
238 n := t.GetNode(p.ID())
244 pv.X, pv.Y = n.X+offset, n.Y
246 p.Resize(n.W-offset, n.H)
250 // CurPane returns the currently active pane
251 func (t *Tab) CurPane() Pane {
252 return t.Panes[t.active]