forked from Mirrors/gomuks
Add continuous scroll to rooms in roster view
This commit is contained in:
parent
b9b363e686
commit
6bb265cc66
1 changed files with 34 additions and 5 deletions
|
@ -36,6 +36,7 @@ type RosterView struct {
|
||||||
|
|
||||||
selected *rooms.Room
|
selected *rooms.Room
|
||||||
rooms []*rooms.Room
|
rooms []*rooms.Room
|
||||||
|
scrollOffset int
|
||||||
|
|
||||||
height, width int
|
height, width int
|
||||||
focused bool
|
focused bool
|
||||||
|
@ -142,22 +143,50 @@ func (rstr *RosterView) Last() *rooms.Room {
|
||||||
func (rstr *RosterView) ScrollNext() {
|
func (rstr *RosterView) ScrollNext() {
|
||||||
if index := rstr.index(rstr.selected); index == -1 || index == len(rstr.rooms)-1 {
|
if index := rstr.index(rstr.selected); index == -1 || index == len(rstr.rooms)-1 {
|
||||||
rstr.selected = rstr.First()
|
rstr.selected = rstr.First()
|
||||||
|
rstr.scrollOffset = 0
|
||||||
} else {
|
} else {
|
||||||
rstr.Lock()
|
rstr.Lock()
|
||||||
defer rstr.Unlock()
|
defer rstr.Unlock()
|
||||||
rstr.selected = rstr.rooms[index+1]
|
rstr.selected = rstr.rooms[index+1]
|
||||||
|
if rstr.VisualScrollHeight(rstr.scrollOffset, index+1) >= rstr.height {
|
||||||
|
rstr.scrollOffset++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rstr *RosterView) ScrollPrev() {
|
func (rstr *RosterView) ScrollPrev() {
|
||||||
if index := rstr.index(rstr.selected); index < 1 {
|
if index := rstr.index(rstr.selected); index < 1 {
|
||||||
rstr.selected = rstr.Last()
|
rstr.selected = rstr.Last()
|
||||||
|
|
||||||
|
if i := len(rstr.rooms) - rstr.RoomsOnScreen(); i < 0 {
|
||||||
|
rstr.scrollOffset = 0
|
||||||
|
} else {
|
||||||
|
rstr.scrollOffset = i
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
rstr.Lock()
|
rstr.Lock()
|
||||||
defer rstr.Unlock()
|
defer rstr.Unlock()
|
||||||
rstr.selected = rstr.rooms[index-1]
|
rstr.selected = rstr.rooms[index-1]
|
||||||
|
if index == rstr.scrollOffset {
|
||||||
|
rstr.scrollOffset--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rstr *RosterView) VisualScrollHeight(start, end int) int {
|
||||||
|
if start < 0 || start > end {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return 3 + (2 * (end - start))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rstr *RosterView) RoomsOnScreen() int {
|
||||||
|
return (rstr.height - 3) / 2
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rstr *RosterView) IndexOfLastVisibleRoom() int {
|
||||||
|
return rstr.scrollOffset + rstr.RoomsOnScreen()
|
||||||
|
}
|
||||||
|
|
||||||
func (rstr *RosterView) Draw(screen mauview.Screen) {
|
func (rstr *RosterView) Draw(screen mauview.Screen) {
|
||||||
if rstr.focused {
|
if rstr.focused {
|
||||||
|
@ -186,7 +215,7 @@ func (rstr *RosterView) Draw(screen mauview.Screen) {
|
||||||
widget.NewBorder().Draw(mauview.NewProxyScreen(screen, 2, 3, rstr.width-5, 1))
|
widget.NewBorder().Draw(mauview.NewProxyScreen(screen, 2, 3, rstr.width-5, 1))
|
||||||
|
|
||||||
y := 4
|
y := 4
|
||||||
for _, room := range rstr.rooms {
|
for _, room := range rstr.rooms[rstr.scrollOffset:] {
|
||||||
if room.IsReplaced() {
|
if room.IsReplaced() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -292,10 +321,10 @@ func (rstr *RosterView) OnMouseEvent(event mauview.MouseEvent) bool {
|
||||||
return true
|
return true
|
||||||
case tcell.Button1:
|
case tcell.Button1:
|
||||||
_, y := event.Position()
|
_, y := event.Position()
|
||||||
if y <= 3 || y > 3+(2*len(rstr.rooms)) {
|
if y <= 3 || y > rstr.VisualScrollHeight(rstr.scrollOffset, rstr.IndexOfLastVisibleRoom()) {
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
index := y/2 - 2
|
index := rstr.scrollOffset + y/2 - 2
|
||||||
if index > len(rstr.rooms)-1 {
|
if index > len(rstr.rooms)-1 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue