Skip to content

Commit

Permalink
Simplify resize code
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Dec 10, 2024
1 parent 2bfca67 commit 0f9b315
Showing 1 changed file with 16 additions and 37 deletions.
53 changes: 16 additions & 37 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1692,13 +1692,13 @@ func (t *Terminal) resizeWindows(forcePreview bool) {

createPreviewWindow(marginInt[0], marginInt[3], pwidth, height)
} else {
t.window = t.tui.NewWindow(
marginInt[0], marginInt[3], width-pwidth, height, false, noBorder)
// NOTE: fzf --preview 'cat {}' --preview-window border-left --border
x := marginInt[3] + width - pwidth
if !previewOpts.border.HasRight() && t.borderShape.HasRight() {
pwidth++
width++
}
t.window = t.tui.NewWindow(
marginInt[0], marginInt[3], width-pwidth, height, false, noBorder)
x := marginInt[3] + width - pwidth
createPreviewWindow(marginInt[0], x, pwidth, height)
}
}
Expand Down Expand Up @@ -4741,49 +4741,28 @@ func (t *Terminal) Loop() error {
pborderDragging = mx == t.pborder.Left()
}
}
if pborderDragging {
previewWidth := t.pwindow.Width() + borderColumns(t.activePreviewOpts.border, t.borderWidth)
previewHeight := t.pwindow.Height() + borderLines(t.activePreviewOpts.border)
minPreviewWidth, minPreviewHeight := t.minPreviewSize(t.activePreviewOpts)

// Decrement, so the cursor drags the last column/row of the
// preview window (i.e. in most cases the border) and not
// the one after.
minPreviewWidth--
minPreviewHeight--

previewLeft := t.pwindow.Left()
previewTop := t.pwindow.Top()
// pwindow does not include it's border, so Left and Top have to be adjusted.
if t.activePreviewOpts.border.HasLeft() {
previewLeft -= 1 + t.borderWidth
}
if t.activePreviewOpts.border.HasTop() {
previewTop -= 1
}

if pborderDragging {
var newSize int
switch t.activePreviewOpts.position {
case posLeft:
diff := t.pborder.Width() - t.pwindow.Width()
newSize = mx - t.pborder.Left() - diff + 1
case posUp:
top := previewTop + minPreviewHeight
// +1 since index to size
newSize = my - top + 1
case posRight:
right := previewLeft + previewWidth - minPreviewWidth
newSize = right - mx
diff := t.pborder.Height() - t.pwindow.Height()
newSize = my - t.pborder.Top() - diff + 1
case posDown:
bottom := previewTop + previewHeight - minPreviewHeight
newSize = bottom - my
case posLeft:
left := previewLeft + minPreviewWidth
// +1 since index to size
newSize = mx - left + 1
offset := my - t.pborder.Top()
newSize = t.pwindow.Height() - offset
case posRight:
offset := mx - t.pborder.Left()
newSize = t.pwindow.Width() - offset
}
if newSize < 1 {
newSize = 1
}

// don't update if the size did not change (e.g. off-axis movement)
// Don't update if the size did not change (e.g. off-axis movement)
if !t.activePreviewOpts.size.percent && t.activePreviewOpts.size.size == float64(newSize) {
break
}
Expand Down

0 comments on commit 0f9b315

Please sign in to comment.