Skip to content

Commit 68c437b

Browse files
authored
Merge pull request #66 from jesseduffield/saner-view-geometry
Make view geometry a little more sane
2 parents 393cf89 + f2963ec commit 68c437b

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

gui.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ func calcScrollbarRune(
912912
}
913913

914914
func calcRealScrollbarStartEnd(v *View) (bool, int, int) {
915-
height := v.InnerHeight() + 1
915+
height := v.InnerHeight()
916916
fullHeight := v.ViewLinesHeight() - v.scrollMargin()
917917

918918
if v.CanScrollPastBottom {

view.go

+29-32
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,9 @@ func (v *View) FocusPoint(cx int, cy int) {
322322
if cy < 0 || cy > lineCount {
323323
return
324324
}
325-
_, height := v.Size()
325+
height := v.InnerHeight()
326326

327-
ly := height - 1
328-
if ly < 0 {
329-
ly = 0
330-
}
331-
332-
v.oy = calculateNewOrigin(cy, v.oy, lineCount, ly)
327+
v.oy = calculateNewOrigin(cy, v.oy, lineCount, height)
333328
v.cx = cx
334329
v.cy = cy - v.oy
335330
}
@@ -343,16 +338,16 @@ func (v *View) CancelRangeSelect() {
343338
}
344339

345340
func calculateNewOrigin(selectedLine int, oldOrigin int, lineCount int, viewHeight int) int {
346-
if viewHeight > lineCount {
341+
if viewHeight >= lineCount {
347342
return 0
348-
} else if selectedLine < oldOrigin || selectedLine > oldOrigin+viewHeight {
343+
} else if selectedLine < oldOrigin || selectedLine >= oldOrigin+viewHeight {
349344
// If the selected line is outside the visible area, scroll the view so
350345
// that the selected line is in the middle.
351346
newOrigin := selectedLine - viewHeight/2
352347

353348
// However, take care not to overflow if the total line count is less
354349
// than the view height.
355-
maxOrigin := lineCount - viewHeight - 1
350+
maxOrigin := lineCount - viewHeight
356351
if newOrigin > maxOrigin {
357352
newOrigin = maxOrigin
358353
}
@@ -438,22 +433,32 @@ func (v *View) Dimensions() (int, int, int, int) {
438433
return v.x0, v.y0, v.x1, v.y1
439434
}
440435

441-
// Size returns the number of visible columns and rows in the View.
436+
// Size returns the number of visible columns and rows in the View, including
437+
// the frame if any
442438
func (v *View) Size() (x, y int) {
443439
return v.Width(), v.Height()
444440
}
445441

442+
// InnerSize returns the number of usable columns and rows in the View, excluding
443+
// the frame if any
444+
func (v *View) InnerSize() (x, y int) {
445+
return v.InnerWidth(), v.InnerHeight()
446+
}
447+
446448
func (v *View) Width() int {
447-
return v.x1 - v.x0 - 1
449+
return v.x1 - v.x0 + 1
448450
}
449451

450452
func (v *View) Height() int {
451-
return v.y1 - v.y0 - 1
453+
return v.y1 - v.y0 + 1
452454
}
453455

454-
// if a view has a frame, that leaves less space for its writeable area
456+
// The writeable area of the view is always two less then the view's size,
457+
// because if it has a frame, we need to subtract that, but if it doesn't, the
458+
// view is made 1 larger on all sides. I'd like to clean this up at some point,
459+
// but for now we live with this weirdness.
455460
func (v *View) InnerWidth() int {
456-
innerWidth := v.Width() - v.frameOffset()
461+
innerWidth := v.Width() - 2
457462
if innerWidth < 0 {
458463
return 0
459464
}
@@ -462,22 +467,14 @@ func (v *View) InnerWidth() int {
462467
}
463468

464469
func (v *View) InnerHeight() int {
465-
innerHeight := v.Height() - v.frameOffset()
470+
innerHeight := v.Height() - 2
466471
if innerHeight < 0 {
467472
return 0
468473
}
469474

470475
return innerHeight
471476
}
472477

473-
func (v *View) frameOffset() int {
474-
if v.Frame {
475-
return 1
476-
} else {
477-
return 0
478-
}
479-
}
480-
481478
// Name returns the name of the view.
482479
func (v *View) Name() string {
483480
return v.name
@@ -573,7 +570,7 @@ func max(a, b int) int {
573570
// SetCursor sets the cursor position of the view at the given point,
574571
// relative to the view. It checks if the position is valid.
575572
func (v *View) SetCursor(x, y int) {
576-
maxX, maxY := v.Size()
573+
maxX, maxY := v.InnerSize()
577574
if x < 0 || x >= maxX || y < 0 || y >= maxY {
578575
return
579576
}
@@ -582,15 +579,15 @@ func (v *View) SetCursor(x, y int) {
582579
}
583580

584581
func (v *View) SetCursorX(x int) {
585-
maxX, _ := v.Size()
582+
maxX := v.InnerWidth()
586583
if x < 0 || x >= maxX {
587584
return
588585
}
589586
v.cx = x
590587
}
591588

592589
func (v *View) SetCursorY(y int) {
593-
_, maxY := v.Size()
590+
maxY := v.InnerHeight()
594591
if y < 0 || y >= maxY {
595592
return
596593
}
@@ -917,7 +914,7 @@ func (v *View) parseInput(ch rune, x int, _ int) (bool, []cell) {
917914
for _, cell := range v.lines[v.wy][0:v.wx] {
918915
cx += runewidth.RuneWidth(cell.chr)
919916
}
920-
repeatCount = v.InnerWidth() - cx + 1
917+
repeatCount = v.InnerWidth() - cx
921918
ch = ' '
922919
truncateLine = true
923920
} else if isEscape {
@@ -1157,7 +1154,7 @@ func (v *View) draw() {
11571154

11581155
v.clearRunes()
11591156

1160-
maxX, maxY := v.Size()
1157+
maxX, maxY := v.InnerSize()
11611158

11621159
if v.Wrap {
11631160
if maxX == 0 {
@@ -1250,7 +1247,7 @@ func (v *View) draw() {
12501247

12511248
func (v *View) refreshViewLinesIfNeeded() {
12521249
if v.tainted {
1253-
maxX := v.Width()
1250+
maxX := v.InnerWidth()
12541251
lineIdx := 0
12551252
lines := v.lines
12561253
if v.HasLoader {
@@ -1341,7 +1338,7 @@ func (v *View) realPosition(vx, vy int) (x, y int, ok bool) {
13411338

13421339
// clearRunes erases all the cells in the view.
13431340
func (v *View) clearRunes() {
1344-
maxX, maxY := v.Size()
1341+
maxX, maxY := v.InnerSize()
13451342
for x := 0; x < maxX; x++ {
13461343
for y := 0; y < maxY; y++ {
13471344
tcellSetCell(v.x0+x+1, v.y0+y+1, ' ', v.FgColor, v.BgColor, v.outMode)
@@ -1789,7 +1786,7 @@ func (v *View) adjustDownwardScrollAmount(scrollHeight int) int {
17891786
_, oy := v.Origin()
17901787
y := oy
17911788
if !v.CanScrollPastBottom {
1792-
_, sy := v.Size()
1789+
sy := v.InnerHeight()
17931790
y += sy
17941791
}
17951792
scrollableLines := v.ViewLinesHeight() - y

0 commit comments

Comments
 (0)