Make scrollAmount public and refactor previous scrolling methods

This commit is contained in:
Caleb Bassi 2019-02-01 21:07:23 -08:00
parent 013ef15209
commit a77ad90f64

View File

@ -78,30 +78,12 @@ func (self *List) Draw(buf *Buffer) {
} }
} }
func (self *List) ScrollUp() { // ScrollAmount scrolls by amount given. If amount is < 0, then scroll up.
if self.SelectedRow > 0 {
self.SelectedRow--
if self.SelectedRow < self.topRow {
self.topRow--
}
}
}
func (self *List) ScrollDown() {
if self.SelectedRow < uint(len(self.Rows))-1 {
self.SelectedRow++
if self.SelectedRow-self.topRow > uint(self.Inner.Dy()-1) {
self.topRow++
}
}
}
// Scrolls by amount given. If amount is < 0, then scroll up.
// There is no need to set self.topRow, as this will be set automatically when drawn, // There is no need to set self.topRow, as this will be set automatically when drawn,
// since if the selected item is off screen then the topRow variable will change accordingly. // since if the selected item is off screen then the topRow variable will change accordingly.
func (self *List) scrollAmount(amount int) { func (self *List) ScrollAmount(amount int) {
if len(self.Rows)-int(self.SelectedRow) <= amount { if len(self.Rows)-int(self.SelectedRow) <= amount {
self.SelectedRow = uint(len(self.Rows)-1) self.SelectedRow = uint(len(self.Rows) - 1)
} else if int(self.SelectedRow)+amount < 0 { } else if int(self.SelectedRow)+amount < 0 {
self.SelectedRow = 0 self.SelectedRow = 0
} else { } else {
@ -109,27 +91,35 @@ func (self *List) scrollAmount(amount int) {
} }
} }
func (self *List) ScrollUp() {
self.ScrollAmount(-1)
}
func (self *List) ScrollDown() {
self.ScrollAmount(1)
}
// PageUp scrolls up one whole page. // PageUp scrolls up one whole page.
func (self *List) PageUp() { func (self *List) PageUp() {
// If an item is selected below top row, then go to the top row. // If an item is selected below top row, then go to the top row.
if self.SelectedRow > self.topRow { if self.SelectedRow > self.topRow {
self.SelectedRow = self.topRow self.SelectedRow = self.topRow
} else { } else {
self.scrollAmount(-self.Inner.Dy()) self.ScrollAmount(-self.Inner.Dy())
} }
} }
// PageDown scolls down one whole page. // PageDown scolls down one whole page.
func (self *List) PageDown() { func (self *List) PageDown() {
self.scrollAmount(self.Inner.Dy()) self.ScrollAmount(self.Inner.Dy())
} }
func (self *List) HalfPageUp() { func (self *List) HalfPageUp() {
self.scrollAmount(-int(FloorFloat64(float64(self.Inner.Dy())/2))) self.ScrollAmount(-int(FloorFloat64(float64(self.Inner.Dy()) / 2)))
} }
func (self *List) HalfPageDown() { func (self *List) HalfPageDown() {
self.scrollAmount(int(FloorFloat64(float64(self.Inner.Dy())/2))) self.ScrollAmount(int(FloorFloat64(float64(self.Inner.Dy()) / 2)))
} }
func (self *List) ScrollTop() { func (self *List) ScrollTop() {
@ -137,5 +127,5 @@ func (self *List) ScrollTop() {
} }
func (self *List) ScrollBottom() { func (self *List) ScrollBottom() {
self.SelectedRow = uint(len(self.Rows)-1) self.SelectedRow = uint(len(self.Rows) - 1)
} }