Debian Codegraph @

internal/action/tab.go

raw
1 package action
2
3 import (
4 luar "layeh.com/gopher-luar"
5
6 "github.com/micro-editor/tcell/v2"
7 "github.com/zyedidia/micro/v2/internal/buffer"
8 "github.com/zyedidia/micro/v2/internal/config"
9 "github.com/zyedidia/micro/v2/internal/display"
10 ulua "github.com/zyedidia/micro/v2/internal/lua"
11 "github.com/zyedidia/micro/v2/internal/screen"
12 "github.com/zyedidia/micro/v2/internal/views"
13 )
14
15 // The TabList is a list of tabs and a window to display the tab bar
16 // at the top of the screen
17 type TabList struct {
18 *display.TabWindow
19 List []*Tab
20 }
21
22 // NewTabList creates a TabList from a list of buffers by creating a Tab
23 // for each buffer
24 func NewTabList(bufs []*buffer.Buffer) *TabList {
25 w, h := screen.Screen.Size()
26 iOffset := config.GetInfoBarOffset()
27 tl := new(TabList)
28 tl.List = make([]*Tab, len(bufs))
29 if len(bufs) > 1 {
30 for i, b := range bufs {
31 tl.List[i] = NewTabFromBuffer(0, 1, w, h-1-iOffset, b)
32 }
33 } else {
34 tl.List[0] = NewTabFromBuffer(0, 0, w, h-iOffset, bufs[0])
35 }
36 tl.TabWindow = display.NewTabWindow(w, 0)
37 tl.Names = make([]string, len(bufs))
38
39 return tl
40 }
41
42 // UpdateNames makes sure that the list of names the tab window has access to is
43 // correct
44 func (t *TabList) UpdateNames() {
45 t.Names = t.Names[:0]
46 for _, p := range t.List {
47 t.Names = append(t.Names, p.Panes[p.active].Name())
48 }
49 }
50
51 // AddTab adds a new tab to this TabList
52 func (t *TabList) AddTab(p *Tab) {
53 t.List = append(t.List, p)
54 t.Resize()
55 t.UpdateNames()
56 }
57
58 // RemoveTab removes a tab with the given id from the TabList
59 func (t *TabList) RemoveTab(id uint64) {
60 for i, p := range t.List {
61 if len(p.Panes) == 0 {
62 continue
63 }
64 if p.Panes[0].ID() == id {
65 copy(t.List[i:], t.List[i+1:])
66 t.List[len(t.List)-1] = nil
67 t.List = t.List[:len(t.List)-1]
68 if t.Active() >= len(t.List) {
69 t.SetActive(len(t.List) - 1)
70 }
71 t.Resize()
72 t.UpdateNames()
73 return
74 }
75 }
76 }
77
78 // Resize resizes all elements within the tab list
79 // One thing to note is that when there is only 1 tab
80 // the tab bar should not be drawn so resizing must take
81 // that into account
82 func (t *TabList) Resize() {
83 w, h := screen.Screen.Size()
84 iOffset := config.GetInfoBarOffset()
85 InfoBar.Resize(w, h-1)
86 if len(t.List) > 1 {
87 for _, p := range t.List {
88 p.Y = 1
89 p.Node.Resize(w, h-1-iOffset)
90 p.Resize()
91 }
92 } else if len(t.List) == 1 {
93 t.List[0].Y = 0
94 t.List[0].Node.Resize(w, h-iOffset)
95 t.List[0].Resize()
96 }
97 t.TabWindow.Resize(w, h)
98 }
99
100 // HandleEvent checks for a resize event or a mouse event on the tab bar
101 // otherwise it will forward the event to the currently active tab
102 func (t *TabList) HandleEvent(event tcell.Event) {
103 switch e := event.(type) {
104 case *tcell.EventResize:
105 t.Resize()
106 case *tcell.EventMouse:
107 mx, my := e.Position()
108 switch e.Buttons() {
109 case tcell.Button1:
110 if my == t.Y && len(t.List) > 1 {
111 if mx == 0 {
112 t.Scroll(-4)
113 } else if mx == t.Width-1 {
114 t.Scroll(4)
115 } else {
116 ind := t.LocFromVisual(buffer.Loc{mx, my})
117 if ind != -1 {
118 t.SetActive(ind)
119 }
120 }
121 return
122 }
123 case tcell.ButtonNone:
124 if t.List[t.Active()].release {
125 // Mouse release received, while already released
126 t.ResetMouse()
127 return
128 }
129 case tcell.WheelUp:
130 if my == t.Y && len(t.List) > 1 {
131 t.Scroll(4)
132 return
133 }
134 case tcell.WheelDown:
135 if my == t.Y && len(t.List) > 1 {
136 t.Scroll(-4)
137 return
138 }
139 }
140 }
141 t.List[t.Active()].HandleEvent(event)
142 }
143
144 // Display updates the names and then displays the tab bar
145 func (t *TabList) Display() {
146 t.UpdateNames()
147 if len(t.List) > 1 {
148 t.TabWindow.Display()
149 }
150 }
151
152 func (t *TabList) SetActive(a int) {
153 t.TabWindow.SetActive(a)
154
155 for i, p := range t.List {
156 if i == a {
157 if !p.isActive {
158 p.isActive = true
159
160 err := config.RunPluginFn("onSetActive", luar.New(ulua.L, p.CurPane()))
161 if err != nil {
162 screen.TermMessage(err)
163 }
164 }
165 } else {
166 p.isActive = false
167 }
168 }
169 }
170
171 // ResetMouse resets the mouse release state after the screen was stopped
172 // or the pane changed.
173 // This prevents situations in which mouse releases are received at the wrong place
174 // and the mouse state is still pressed.
175 func (t *TabList) ResetMouse() {
176 for _, tab := range t.List {
177 if !tab.release && tab.resizing != nil {
178 tab.resizing = nil
179 }
180
181 tab.release = true
182
183 for _, p := range tab.Panes {
184 if bp, ok := p.(*BufPane); ok {
185 bp.resetMouse()
186 }
187 }
188 }
189 }
190
191 // CloseTerms notifies term panes that a terminal job has finished.
192 func (t *TabList) CloseTerms() {
193 for _, tab := range t.List {
194 for _, p := range tab.Panes {
195 if tp, ok := p.(*TermPane); ok {
196 tp.HandleTermClose()
197 }
198 }
199 }
200 }
201
202 // Tabs is the global tab list
203 var Tabs *TabList
204
205 func InitTabs(bufs []*buffer.Buffer) {
206 multiopen := config.GetGlobalOption("multiopen").(string)
207 if multiopen == "tab" {
208 Tabs = NewTabList(bufs)
209 } else {
210 Tabs = NewTabList(bufs[:1])
211 for _, b := range bufs[1:] {
212 if multiopen == "vsplit" {
213 MainTab().CurPane().VSplitBuf(b)
214 } else { // default hsplit
215 MainTab().CurPane().HSplitBuf(b)
216 }
217 }
218 }
219
220 screen.RestartCallback = Tabs.ResetMouse
221 }
222
223 func MainTab() *Tab {
224 return Tabs.List[Tabs.Active()]
225 }
226
227 // A Tab represents a single tab
228 // It consists of a list of edit panes (the open buffers),
229 // a split tree (stored as just the root node), and a uiwindow
230 // to display the UI elements like the borders between splits
231 type Tab struct {
232 *views.Node
233 *display.UIWindow
234
235 isActive bool
236
237 Panes []Pane
238 active int
239
240 resizing *views.Node // node currently being resized
241 // captures whether the mouse is released
242 release bool
243 }
244
245 // NewTabFromBuffer creates a new tab from the given buffer
246 func NewTabFromBuffer(x, y, width, height int, b *buffer.Buffer) *Tab {
247 t := new(Tab)
248 t.Node = views.NewRoot(x, y, width, height)
249 t.UIWindow = display.NewUIWindow(t.Node)
250 t.release = true
251
252 e := NewBufPaneFromBuf(b, t)
253 e.SetID(t.ID())
254
255 t.Panes = append(t.Panes, e)
256 return t
257 }
258
259 func NewTabFromPane(x, y, width, height int, pane Pane) *Tab {
260 t := new(Tab)
261 t.Node = views.NewRoot(x, y, width, height)
262 t.UIWindow = display.NewUIWindow(t.Node)
263 t.release = true
264 pane.SetTab(t)
265 pane.SetID(t.ID())
266
267 t.Panes = append(t.Panes, pane)
268 return t
269 }
270
271 // HandleEvent takes a tcell event and usually dispatches it to the current
272 // active pane. However if the event is a resize or a mouse event where the user
273 // is interacting with the UI (resizing splits) then the event is consumed here
274 // If the event is a mouse press event in a pane, that pane will become active
275 // and get the event
276 func (t *Tab) HandleEvent(event tcell.Event) {
277 switch e := event.(type) {
278 case *tcell.EventMouse:
279 mx, my := e.Position()
280 btn := e.Buttons()
281 switch {
282 case btn & ^(tcell.WheelUp|tcell.WheelDown|tcell.WheelLeft|tcell.WheelRight) != tcell.ButtonNone:
283 // button press or drag
284 wasReleased := t.release
285 t.release = false
286
287 if btn == tcell.Button1 {
288 if t.resizing != nil {
289 var size int
290 if t.resizing.Kind == views.STVert {
291 size = mx - t.resizing.X
292 } else {
293 size = my - t.resizing.Y + 1
294 }
295 t.resizing.ResizeSplit(size)
296 t.Resize()
297 return
298 }
299 if wasReleased {
300 t.resizing = t.GetMouseSplitNode(buffer.Loc{mx, my})
301 if t.resizing != nil {
302 return
303 }
304 }
305 }
306
307 if wasReleased {
308 for i, p := range t.Panes {
309 v := p.GetView()
310 inpane := mx >= v.X && mx < v.X+v.Width && my >= v.Y && my < v.Y+v.Height
311 if inpane {
312 t.SetActive(i)
313 break
314 }
315 }
316 }
317 case btn == tcell.ButtonNone:
318 // button release
319 t.release = true
320 if t.resizing != nil {
321 t.resizing = nil
322 return
323 }
324 default:
325 // wheel move
326 for _, p := range t.Panes {
327 v := p.GetView()
328 inpane := mx >= v.X && mx < v.X+v.Width && my >= v.Y && my < v.Y+v.Height
329 if inpane {
330 p.HandleEvent(event)
331 return
332 }
333 }
334 }
335
336 }
337 t.Panes[t.active].HandleEvent(event)
338 }
339
340 // SetActive changes the currently active pane to the specified index
341 func (t *Tab) SetActive(i int) {
342 t.active = i
343 for j, p := range t.Panes {
344 if j == i {
345 p.SetActive(true)
346 } else {
347 p.SetActive(false)
348 }
349 }
350 }
351
352 // AddPane adds a pane at a given index
353 func (t *Tab) AddPane(pane Pane, i int) {
354 if len(t.Panes) == i {
355 t.Panes = append(t.Panes, pane)
356 return
357 }
358 t.Panes = append(t.Panes[:i+1], t.Panes[i:]...)
359 t.Panes[i] = pane
360 }
361
362 // GetPane returns the pane with the given split index
363 func (t *Tab) GetPane(splitid uint64) int {
364 for i, p := range t.Panes {
365 if p.ID() == splitid {
366 return i
367 }
368 }
369 return 0
370 }
371
372 // Remove pane removes the pane with the given index
373 func (t *Tab) RemovePane(i int) {
374 copy(t.Panes[i:], t.Panes[i+1:])
375 t.Panes[len(t.Panes)-1] = nil
376 t.Panes = t.Panes[:len(t.Panes)-1]
377 }
378
379 // Resize resizes all panes according to their corresponding split nodes
380 func (t *Tab) Resize() {
381 for _, p := range t.Panes {
382 n := t.GetNode(p.ID())
383 pv := p.GetView()
384 offset := 0
385 if n.X != 0 {
386 offset = 1
387 }
388 pv.X, pv.Y = n.X+offset, n.Y
389 p.SetView(pv)
390 p.Resize(n.W-offset, n.H)
391 }
392 }
393
394 // CurPane returns the currently active pane
395 func (t *Tab) CurPane() *BufPane {
396 p, ok := t.Panes[t.active].(*BufPane)
397 if !ok {
398 return nil
399 }
400 return p
401 }