Skip to content

Commit 1f51d0b

Browse files
authored
Merge pull request zyedidia#3271 from JoeKar/fix/inactive-mouse-release
Fix lost mouse release events in case the pane becomes inactive
2 parents 385437d + 0a1447b commit 1f51d0b

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

internal/action/bufpane.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,17 @@ func (h *BufPane) HandleEvent(event tcell.Event) {
500500
// Mouse event with no click - mouse was just released.
501501
// If there were multiple mouse buttons pressed, we don't know which one
502502
// was actually released, so we assume they all were released.
503+
pressed := len(h.mousePressed) > 0
503504
for me := range h.mousePressed {
504505
delete(h.mousePressed, me)
505506

506507
me.state = MouseRelease
507508
h.DoMouseEvent(me, e)
508509
}
510+
if !pressed {
511+
// Propagate the mouse release in case the press wasn't for this BufPane
512+
Tabs.ResetMouse()
513+
}
509514
}
510515
}
511516
h.Buf.MergeCursors()

internal/action/tab.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ func (t *TabList) HandleEvent(event tcell.Event) {
124124
return
125125
}
126126
}
127+
case tcell.ButtonNone:
128+
if t.List[t.Active()].release {
129+
// Mouse release received, while already released
130+
t.ResetMouse()
131+
return
132+
}
127133
case tcell.WheelUp:
128134
if my == t.Y {
129135
t.Scroll(4)
@@ -166,6 +172,26 @@ func (t *TabList) SetActive(a int) {
166172
}
167173
}
168174

175+
// ResetMouse resets the mouse release state after the screen was stopped
176+
// or the pane changed.
177+
// This prevents situations in which mouse releases are received at the wrong place
178+
// and the mouse state is still pressed.
179+
func (t *TabList) ResetMouse() {
180+
for _, tab := range t.List {
181+
if !tab.release && tab.resizing != nil {
182+
tab.resizing = nil
183+
}
184+
185+
tab.release = true
186+
187+
for _, p := range tab.Panes {
188+
if bp, ok := p.(*BufPane); ok {
189+
bp.resetMouse()
190+
}
191+
}
192+
}
193+
}
194+
169195
// Tabs is the global tab list
170196
var Tabs *TabList
171197

@@ -184,20 +210,7 @@ func InitTabs(bufs []*buffer.Buffer) {
184210
}
185211
}
186212

187-
screen.RestartCallback = func() {
188-
// The mouse could be released after the screen was stopped, so that
189-
// we couldn't catch the mouse release event and would erroneously think
190-
// that it is still pressed. So need to reset the mouse release state
191-
// after the screen is restarted.
192-
for _, t := range Tabs.List {
193-
t.release = true
194-
for _, p := range t.Panes {
195-
if bp, ok := p.(*BufPane); ok {
196-
bp.resetMouse()
197-
}
198-
}
199-
}
200-
}
213+
screen.RestartCallback = Tabs.ResetMouse
201214
}
202215

203216
func MainTab() *Tab {

0 commit comments

Comments
 (0)