From c1256862160b2023b92701e1a2a5d56147603baf Mon Sep 17 00:00:00 2001 From: Artem Lavrenov Date: Thu, 29 Aug 2019 21:15:39 +0300 Subject: [PATCH] For mouse support in list widget need export TopRow for calculate SelectedRow under mouse cursor. --- _examples/list.go | 15 +++++++++++++-- v3/widgets/list.go | 24 ++++++++++++------------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/_examples/list.go b/_examples/list.go index 06571912..23090434 100644 --- a/_examples/list.go +++ b/_examples/list.go @@ -46,9 +46,20 @@ func main() { switch e.ID { case "q", "": return - case "j", "": + case "": + 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 { + l.SelectedRow = payload.Y - l.Rectangle.Min.Y - border + l.TopRow + } + case "j", "", "": l.ScrollDown() - case "k", "": + case "k", "", "": l.ScrollUp() case "": l.ScrollHalfPageDown() diff --git a/v3/widgets/list.go b/v3/widgets/list.go index a4a6cb1b..d833a170 100644 --- a/v3/widgets/list.go +++ b/v3/widgets/list.go @@ -18,7 +18,7 @@ type List struct { WrapText bool TextStyle Style SelectedRow int - topRow int + TopRow int SelectedRowStyle Style } @@ -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())) @@ -69,7 +69,7 @@ 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), @@ -77,7 +77,7 @@ func (self *List) Draw(buf *Buffer) { } // 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), @@ -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 @@ -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()) }