Allow dragging of the preview window

This commit is contained in:
Junegunn Choi
2023-01-03 01:46:37 +09:00
parent 1fc1f47d80
commit c1cd0c09a2

View File

@@ -2588,7 +2588,9 @@ func (t *Terminal) Loop() {
t.eventChan <- t.tui.GetChar() t.eventChan <- t.tui.GetChar()
} }
}() }()
dragging := false previewDraggingPos := -1
barDragging := false
wasDown := false
for looping { for looping {
var newCommand *string var newCommand *string
var reloadSync bool var reloadSync bool
@@ -3032,8 +3034,15 @@ func (t *Terminal) Loop() {
case actMouse: case actMouse:
me := event.MouseEvent me := event.MouseEvent
mx, my := me.X, me.Y mx, my := me.X, me.Y
clicked := !wasDown && me.Down
wasDown = me.Down
if !me.Down {
barDragging = false
previewDraggingPos = -1
}
// Scrolling
if me.S != 0 { if me.S != 0 {
// Scroll
if t.window.Enclose(my, mx) && t.merger.Length() > 0 { if t.window.Enclose(my, mx) && t.merger.Length() > 0 {
if t.multi > 0 && me.Mod { if t.multi > 0 && me.Mod {
toggle() toggle()
@@ -3043,61 +3052,86 @@ func (t *Terminal) Loop() {
} else if t.hasPreviewWindow() && t.pwindow.Enclose(my, mx) { } else if t.hasPreviewWindow() && t.pwindow.Enclose(my, mx) {
scrollPreviewBy(-me.S) scrollPreviewBy(-me.S)
} }
} else if t.window.Enclose(my, mx) { break
mx -= t.window.Left() }
my -= t.window.Top()
min := 2 + len(t.header) // Preview dragging
if t.noInfoLine() { if me.Down && (previewDraggingPos > 0 || clicked && t.hasPreviewWindow() && t.pwindow.Enclose(my, mx)) {
min-- if previewDraggingPos > 0 {
scrollPreviewBy(previewDraggingPos - my)
} }
h := t.window.Height() previewDraggingPos = my
switch t.layout { break
case layoutDefault: }
// Ignored
if !t.window.Enclose(my, mx) && !barDragging {
break
}
// Translate coordinates
mx -= t.window.Left()
my -= t.window.Top()
min := 2 + len(t.header)
if t.noInfoLine() {
min--
}
h := t.window.Height()
switch t.layout {
case layoutDefault:
my = h - my - 1
case layoutReverseList:
if my < h-min {
my += min
} else {
my = h - my - 1 my = h - my - 1
case layoutReverseList: }
if my < h-min { }
my += min
} else { // Scrollbar dragging
my = h - my - 1 barDragging = me.Down && (barDragging || clicked && my >= min && mx == t.window.Width()-1)
if barDragging {
barLength, barStart := t.getScrollbar()
if barLength > 0 {
maxItems := t.maxItems()
if newBarStart := util.Constrain(my-min-barLength/2, 0, maxItems-barLength); newBarStart != barStart {
total := t.merger.Length()
prevOffset := t.offset
// barStart = (maxItems - barLength) * t.offset / (total - maxItems)
t.offset = int(math.Ceil(float64(newBarStart) * float64(total-maxItems) / float64(maxItems-barLength)))
t.cy = t.offset + t.cy - prevOffset
req(reqList)
} }
} }
dragging = me.Down && (dragging || my >= min && mx == t.window.Width()-1) break
if me.Double { }
// Double-click
if my >= min { // Double-click on an item
if t.vset(t.offset+my-min) && t.cy < t.merger.Length() { if me.Double && mx < t.window.Width()-1 {
return doActions(actionsFor(tui.DoubleClick)) // Double-click
} if my >= min {
if t.vset(t.offset+my-min) && t.cy < t.merger.Length() {
return doActions(actionsFor(tui.DoubleClick))
} }
} else if dragging && my >= min { }
barLength, barStart := t.getScrollbar() break
if barLength > 0 { }
maxItems := t.maxItems()
if newBarStart := util.Constrain(my-min-barLength/2, 0, maxItems-barLength); newBarStart != barStart { if me.Down {
total := t.merger.Length() mx = util.Constrain(mx-t.promptLen, 0, len(t.input))
prevOffset := t.offset if my == t.promptLine() && mx >= 0 {
// barStart = (maxItems - barLength) * t.offset / (total - maxItems) // Prompt
t.offset = int(math.Ceil(float64(newBarStart) * float64(total-maxItems) / float64(maxItems-barLength))) t.cx = mx + t.xoffset
t.cy = t.offset + t.cy - prevOffset } else if my >= min {
req(reqList) // List
} if t.vset(t.offset+my-min) && t.multi > 0 && me.Mod {
toggle()
} }
} else if me.Down { req(reqList)
mx = util.Constrain(mx-t.promptLen, 0, len(t.input)) if me.Left {
if my == t.promptLine() && mx >= 0 { return doActions(actionsFor(tui.LeftClick))
// Prompt
t.cx = mx + t.xoffset
} else if my >= min {
// List
if t.vset(t.offset+my-min) && t.multi > 0 && me.Mod {
toggle()
}
req(reqList)
if me.Left {
return doActions(actionsFor(tui.LeftClick))
}
return doActions(actionsFor(tui.RightClick))
} }
return doActions(actionsFor(tui.RightClick))
} }
} }
case actReload, actReloadSync: case actReload, actReloadSync: