Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TopRow must be public in order to calculate which line of the list is under the cursor. #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions _examples/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,20 @@ func main() {
switch e.ID {
case "q", "<C-c>":
return
case "j", "<Down>":
case "<MouseLeft>":
payload := e.Payload.(ui.Mouse)
border := 0
if l.BorderTop {
border = 1
}
x0, y0 := l.Inner.Min.X, l.Inner.Min.Y
x1, y1 := l.Inner.Max.X, l.Inner.Max.Y
if x0 < payload.X && payload.X < x1 && y0 < payload.Y && payload.Y < y1 {
artyl marked this conversation as resolved.
Show resolved Hide resolved
l.SelectedRow = payload.Y - l.Rectangle.Min.Y - border + l.TopRow
}
case "j", "<Down>", "<MouseWheelDown>":
l.ScrollDown()
case "k", "<Up>":
case "k", "<Up>", "<MouseWheelUp>":
l.ScrollUp()
case "<C-d>":
l.ScrollHalfPageDown()
Expand Down
24 changes: 12 additions & 12 deletions v3/widgets/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type List struct {
WrapText bool
TextStyle Style
SelectedRow int
topRow int
TopRow int
SelectedRowStyle Style
}

Expand All @@ -36,14 +36,14 @@ func (self *List) Draw(buf *Buffer) {
point := self.Inner.Min

// adjusts view into widget
if self.SelectedRow >= self.Inner.Dy()+self.topRow {
self.topRow = self.SelectedRow - self.Inner.Dy() + 1
} else if self.SelectedRow < self.topRow {
self.topRow = self.SelectedRow
if self.SelectedRow >= self.Inner.Dy()+self.TopRow {
self.TopRow = self.SelectedRow - self.Inner.Dy() + 1
} else if self.SelectedRow < self.TopRow {
self.TopRow = self.SelectedRow
}

// draw rows
for row := self.topRow; row < len(self.Rows) && point.Y < self.Inner.Max.Y; row++ {
for row := self.TopRow; row < len(self.Rows) && point.Y < self.Inner.Max.Y; row++ {
cells := ParseStyles(self.Rows[row], self.TextStyle)
if self.WrapText {
cells = WrapCells(cells, uint(self.Inner.Dx()))
Expand All @@ -69,15 +69,15 @@ func (self *List) Draw(buf *Buffer) {
}

// draw UP_ARROW if needed
if self.topRow > 0 {
if self.TopRow > 0 {
buf.SetCell(
NewCell(UP_ARROW, NewStyle(ColorWhite)),
image.Pt(self.Inner.Max.X-1, self.Inner.Min.Y),
)
}

// draw DOWN_ARROW if needed
if len(self.Rows) > int(self.topRow)+self.Inner.Dy() {
if len(self.Rows) > int(self.TopRow)+self.Inner.Dy() {
buf.SetCell(
NewCell(DOWN_ARROW, NewStyle(ColorWhite)),
image.Pt(self.Inner.Max.X-1, self.Inner.Max.Y-1),
Expand All @@ -86,8 +86,8 @@ func (self *List) Draw(buf *Buffer) {
}

// ScrollAmount 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,
// since if the selected item is off screen then the topRow variable will change accordingly.
// 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.
func (self *List) ScrollAmount(amount int) {
if len(self.Rows)-int(self.SelectedRow) <= amount {
self.SelectedRow = len(self.Rows) - 1
Expand All @@ -108,8 +108,8 @@ func (self *List) ScrollDown() {

func (self *List) ScrollPageUp() {
// If an item is selected below top row, then go to the top row.
if self.SelectedRow > self.topRow {
self.SelectedRow = self.topRow
if self.SelectedRow > self.TopRow {
self.SelectedRow = self.TopRow
} else {
self.ScrollAmount(-self.Inner.Dy())
}
Expand Down