Replace BufWidth & BufHeight with BufView

BufView returns not only the buffer's width and height but also its
x,y position. It may be useful e.g. for checking if a mouse click was
on the actual buffer or ourside it, e.g. on the gutter.
This commit is contained in:
Dmitry Maluka 2021-04-08 23:32:00 +02:00
parent ab6ce444a7
commit aaac60a78d
4 changed files with 35 additions and 26 deletions

View File

@ -36,8 +36,8 @@ func (h *BufPane) ScrollDown(n int) {
func (h *BufPane) ScrollAdjust() { func (h *BufPane) ScrollAdjust() {
v := h.GetView() v := h.GetView()
end := h.SLocFromLoc(h.Buf.End()) end := h.SLocFromLoc(h.Buf.End())
if h.Diff(v.StartLine, end) < h.BufHeight()-1 { if h.Diff(v.StartLine, end) < h.BufView().Height-1 {
v.StartLine = h.Scroll(end, -h.BufHeight()+1) v.StartLine = h.Scroll(end, -h.BufView().Height+1)
} }
h.SetView(v) h.SetView(v)
} }
@ -117,7 +117,7 @@ func (h *BufPane) ScrollDownAction() bool {
// Center centers the view on the cursor // Center centers the view on the cursor
func (h *BufPane) Center() bool { func (h *BufPane) Center() bool {
v := h.GetView() v := h.GetView()
v.StartLine = h.Scroll(h.SLocFromLoc(h.Cursor.Loc), -h.BufHeight()/2) v.StartLine = h.Scroll(h.SLocFromLoc(h.Cursor.Loc), -h.BufView().Height/2)
h.SetView(v) h.SetView(v)
h.ScrollAdjust() h.ScrollAdjust()
return true return true
@ -1290,20 +1290,20 @@ func (h *BufPane) Start() bool {
// End moves the viewport to the end of the buffer // End moves the viewport to the end of the buffer
func (h *BufPane) End() bool { func (h *BufPane) End() bool {
v := h.GetView() v := h.GetView()
v.StartLine = h.Scroll(h.SLocFromLoc(h.Buf.End()), -h.BufHeight()+1) v.StartLine = h.Scroll(h.SLocFromLoc(h.Buf.End()), -h.BufView().Height+1)
h.SetView(v) h.SetView(v)
return true return true
} }
// PageUp scrolls the view up a page // PageUp scrolls the view up a page
func (h *BufPane) PageUp() bool { func (h *BufPane) PageUp() bool {
h.ScrollUp(h.BufHeight()) h.ScrollUp(h.BufView().Height)
return true return true
} }
// PageDown scrolls the view down a page // PageDown scrolls the view down a page
func (h *BufPane) PageDown() bool { func (h *BufPane) PageDown() bool {
h.ScrollDown(h.BufHeight()) h.ScrollDown(h.BufView().Height)
h.ScrollAdjust() h.ScrollAdjust()
return true return true
} }
@ -1313,7 +1313,7 @@ func (h *BufPane) SelectPageUp() bool {
if !h.Cursor.HasSelection() { if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc h.Cursor.OrigSelection[0] = h.Cursor.Loc
} }
h.MoveCursorUp(h.BufHeight()) h.MoveCursorUp(h.BufView().Height)
h.Cursor.SelectTo(h.Cursor.Loc) h.Cursor.SelectTo(h.Cursor.Loc)
h.Relocate() h.Relocate()
return true return true
@ -1324,7 +1324,7 @@ func (h *BufPane) SelectPageDown() bool {
if !h.Cursor.HasSelection() { if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc h.Cursor.OrigSelection[0] = h.Cursor.Loc
} }
h.MoveCursorDown(h.BufHeight()) h.MoveCursorDown(h.BufView().Height)
h.Cursor.SelectTo(h.Cursor.Loc) h.Cursor.SelectTo(h.Cursor.Loc)
h.Relocate() h.Relocate()
return true return true
@ -1339,7 +1339,7 @@ func (h *BufPane) CursorPageUp() bool {
h.Cursor.ResetSelection() h.Cursor.ResetSelection()
h.Cursor.StoreVisualX() h.Cursor.StoreVisualX()
} }
h.MoveCursorUp(h.BufHeight()) h.MoveCursorUp(h.BufView().Height)
h.Relocate() h.Relocate()
return true return true
} }
@ -1353,20 +1353,20 @@ func (h *BufPane) CursorPageDown() bool {
h.Cursor.ResetSelection() h.Cursor.ResetSelection()
h.Cursor.StoreVisualX() h.Cursor.StoreVisualX()
} }
h.MoveCursorDown(h.BufHeight()) h.MoveCursorDown(h.BufView().Height)
h.Relocate() h.Relocate()
return true return true
} }
// HalfPageUp scrolls the view up half a page // HalfPageUp scrolls the view up half a page
func (h *BufPane) HalfPageUp() bool { func (h *BufPane) HalfPageUp() bool {
h.ScrollUp(h.BufHeight() / 2) h.ScrollUp(h.BufView().Height / 2)
return true return true
} }
// HalfPageDown scrolls the view down half a page // HalfPageDown scrolls the view down half a page
func (h *BufPane) HalfPageDown() bool { func (h *BufPane) HalfPageDown() bool {
h.ScrollDown(h.BufHeight() / 2) h.ScrollDown(h.BufView().Height / 2)
h.ScrollAdjust() h.ScrollAdjust()
return true return true
} }

View File

@ -94,16 +94,18 @@ func (w *BufWindow) IsActive() bool {
return w.active return w.active
} }
// BufWidth returns the width of the actual buffer displayed in the window, // BufView returns the width, height and x,y location of the actual buffer.
// which is usually less than the window width due to the gutter, ruler or scrollbar // It is not exactly the same as the whole window which also contains gutter,
func (w *BufWindow) BufWidth() int { // ruler, scrollbar and statusline.
return w.bufWidth func (w *BufWindow) BufView() View {
return View{
X: w.gutterOffset,
Y: 0,
Width: w.bufWidth,
Height: w.bufHeight,
StartLine: w.StartLine,
StartCol: w.StartCol,
} }
// BufHeight returns the height of the actual buffer displayed in the window,
// which is usually less than the window height due to the statusline
func (w *BufWindow) BufHeight() int {
return w.bufHeight
} }
func (w *BufWindow) updateDisplayInfo() { func (w *BufWindow) updateDisplayInfo() {

View File

@ -72,8 +72,16 @@ func (i *InfoWindow) LocFromVisual(vloc buffer.Loc) buffer.Loc {
return buffer.Loc{c.GetCharPosInLine(l, vloc.X-n), 0} return buffer.Loc{c.GetCharPosInLine(l, vloc.X-n), 0}
} }
func (i *InfoWindow) BufWidth() int { return i.Width } func (i *InfoWindow) BufView() View {
func (i *InfoWindow) BufHeight() int { return 1 } return View{
X: 0,
Y: 0,
Width: i.Width,
Height: 1,
StartLine: SLoc{0, 0},
StartCol: 0,
}
}
func (i *InfoWindow) Scroll(s SLoc, n int) SLoc { return s } func (i *InfoWindow) Scroll(s SLoc, n int) SLoc { return s }
func (i *InfoWindow) Diff(s1, s2 SLoc) int { return 0 } func (i *InfoWindow) Diff(s1, s2 SLoc) int { return 0 }

View File

@ -33,6 +33,5 @@ type BWindow interface {
Window Window
SoftWrap SoftWrap
SetBuffer(b *buffer.Buffer) SetBuffer(b *buffer.Buffer)
BufWidth() int BufView() View
BufHeight() int
} }