99 "github.com/zyedidia/micro/v2/internal/display"
1010 ulua "github.com/zyedidia/micro/v2/internal/lua"
1111 "github.com/zyedidia/micro/v2/internal/screen"
12+ "github.com/zyedidia/micro/v2/internal/util"
1213 "github.com/zyedidia/micro/v2/internal/views"
1314)
1415
@@ -17,6 +18,11 @@ import (
1718type TabList struct {
1819 * display.TabWindow
1920 List []* Tab
21+
22+ // captures whether the mouse is released
23+ release bool
24+ // captures whether the mouse is released
25+ dragging bool
2026}
2127
2228// NewTabList creates a TabList from a list of buffers by creating a Tab
@@ -35,6 +41,7 @@ func NewTabList(bufs []*buffer.Buffer) *TabList {
3541 }
3642 tl .TabWindow = display .NewTabWindow (w , 0 )
3743 tl .Names = make ([]string , len (bufs ))
44+ tl .release = true
3845
3946 return tl
4047}
@@ -107,20 +114,45 @@ func (t *TabList) HandleEvent(event tcell.Event) {
107114 mx , my := e .Position ()
108115 switch e .Buttons () {
109116 case tcell .Button1 :
117+ wasReleased := t .release
118+ t .release = false
119+ if ! wasReleased && ! t .dragging {
120+ //Non-tabbar dragging
121+ break
122+ }
110123 if my == t .Y && len (t .List ) > 1 {
111124 if mx == 0 {
112125 t .Scroll (- 4 )
113126 } else if mx == t .Width - 1 {
114127 t .Scroll (4 )
115128 } else {
116- ind := t .LocFromVisual (buffer.Loc {mx , my })
117- if ind != - 1 {
118- t .SetActive (ind )
129+ i := t .LocFromVisual (buffer.Loc {mx , my })
130+
131+ if i == - 1 {
132+ if t .dragging {
133+ i = len (t .List ) - 1
134+ } else {
135+ //Non-tab click/drag
136+ return
137+ }
138+ }
139+ if t .dragging {
140+ Tabs .MoveTab (MainTab (), i )
141+ Tabs .SetActive (i )
142+ } else {
143+ t .dragging = true
144+ t .SetActive (i )
119145 }
120146 }
121147 return
122148 }
149+ if wasReleased {
150+ t .dragging = false
151+ }
123152 case tcell .ButtonNone :
153+ t .release = true
154+ t .dragging = false
155+
124156 if t .List [t .Active ()].release {
125157 // Mouse release received, while already released
126158 t .ResetMouse ()
@@ -138,7 +170,9 @@ func (t *TabList) HandleEvent(event tcell.Event) {
138170 }
139171 }
140172 }
141- t .List [t .Active ()].HandleEvent (event )
173+ if ! t .dragging {
174+ t .List [t .Active ()].HandleEvent (event )
175+ }
142176}
143177
144178// Display updates the names and then displays the tab bar
@@ -150,6 +184,7 @@ func (t *TabList) Display() {
150184}
151185
152186func (t * TabList ) SetActive (a int ) {
187+ a = util .Clamp (a , 0 , len (t .List )- 1 )
153188 t .TabWindow .SetActive (a )
154189
155190 for i , p := range t .List {
@@ -399,3 +434,14 @@ func (t *Tab) CurPane() *BufPane {
399434 }
400435 return p
401436}
437+
438+ // MoveTab moves the specified tab to the given index
439+ func (tl * TabList ) MoveTab (t * Tab , i int ) {
440+ i = util .Clamp (i , 0 , len (tl .List )- 1 )
441+ tl .RemoveTab (t .Panes [0 ].ID ())
442+ tl .List = append (tl .List , nil )
443+ copy (tl .List [i + 1 :], tl .List [i :])
444+ tl .List [i ] = t
445+ tl .Resize ()
446+ tl .UpdateNames ()
447+ }
0 commit comments