2018-03-25 13:21:59 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-04-09 22:45:54 +02:00
|
|
|
package ui
|
2018-03-25 13:21:59 +02:00
|
|
|
|
|
|
|
import (
|
2018-03-26 16:22:47 +02:00
|
|
|
"fmt"
|
2018-04-24 01:13:17 +02:00
|
|
|
"regexp"
|
2018-03-26 16:22:47 +02:00
|
|
|
"strconv"
|
2018-04-24 01:13:17 +02:00
|
|
|
"strings"
|
2018-03-26 16:22:47 +02:00
|
|
|
|
2018-04-24 21:46:16 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
2018-03-25 13:21:59 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
2018-04-09 22:45:54 +02:00
|
|
|
"maunium.net/go/gomuks/ui/widget"
|
2018-04-19 10:10:34 +02:00
|
|
|
"maunium.net/go/tcell"
|
2018-03-25 13:21:59 +02:00
|
|
|
"maunium.net/go/tview"
|
|
|
|
)
|
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
type orderedRoom struct {
|
|
|
|
*rooms.Room
|
|
|
|
order string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newOrderedRoom(order string, room *rooms.Room) *orderedRoom {
|
|
|
|
return &orderedRoom{
|
|
|
|
Room: room,
|
|
|
|
order: order,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertRoom(room *rooms.Room) *orderedRoom {
|
|
|
|
return newOrderedRoom("", room)
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertTaggedRoom(tag rooms.RoomTag, room *rooms.Room) *orderedRoom {
|
|
|
|
return newOrderedRoom(tag.Order, room)
|
|
|
|
}
|
|
|
|
|
|
|
|
type tagRoomList struct {
|
|
|
|
rooms []*orderedRoom
|
|
|
|
maxShown int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTagRoomList(rooms ...*orderedRoom) *tagRoomList {
|
|
|
|
return &tagRoomList{
|
|
|
|
maxShown: 10,
|
|
|
|
rooms: rooms,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) Visible() []*orderedRoom {
|
|
|
|
return trl.rooms[len(trl.rooms)-trl.Length():]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) FirstVisible() *rooms.Room {
|
|
|
|
visible := trl.Visible()
|
|
|
|
if len(visible) > 0 {
|
|
|
|
return visible[len(visible)-1].Room
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) LastVisible() *rooms.Room {
|
|
|
|
visible := trl.Visible()
|
|
|
|
if len(visible) > 0 {
|
|
|
|
return visible[0].Room
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) All() []*orderedRoom {
|
|
|
|
return trl.rooms
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) Length() int {
|
|
|
|
if len(trl.rooms) < trl.maxShown {
|
|
|
|
return len(trl.rooms)
|
|
|
|
}
|
|
|
|
return trl.maxShown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) TotalLength() int {
|
|
|
|
return len(trl.rooms)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) IsEmpty() bool {
|
|
|
|
return len(trl.rooms) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) IsCollapsed() bool {
|
|
|
|
return trl.maxShown == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) ToggleCollapse() {
|
|
|
|
if trl.IsCollapsed() {
|
|
|
|
trl.maxShown = 10
|
|
|
|
} else {
|
|
|
|
trl.maxShown = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) HasInvisibleRooms() bool {
|
|
|
|
return trl.maxShown < trl.TotalLength()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) HasVisibleRooms() bool {
|
|
|
|
return !trl.IsEmpty() && trl.maxShown > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) Insert(order string, room *rooms.Room) {
|
|
|
|
trl.rooms = append(trl.rooms, nil)
|
|
|
|
// The default insert index is the newly added slot.
|
|
|
|
// That index will be used if all other rooms in the list have the same LastReceivedMessage timestamp.
|
|
|
|
insertAt := len(trl.rooms) - 1
|
|
|
|
// Find the spot where the new room should be put according to the last received message timestamps.
|
|
|
|
for i := 0; i < len(trl.rooms)-1; i++ {
|
|
|
|
roomAtIndex := trl.rooms[i]
|
|
|
|
orderComp := strings.Compare(order, roomAtIndex.order)
|
|
|
|
if orderComp == 1 || (orderComp == 0 && roomAtIndex.LastReceivedMessage.After(room.LastReceivedMessage)) {
|
|
|
|
insertAt = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Move newer rooms forward in the array.
|
|
|
|
for i := len(trl.rooms) - 1; i > insertAt; i-- {
|
|
|
|
trl.rooms[i] = trl.rooms[i-1]
|
|
|
|
}
|
|
|
|
// Insert room.
|
|
|
|
trl.rooms[insertAt] = newOrderedRoom(order, room)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) String() string {
|
|
|
|
var str strings.Builder
|
|
|
|
fmt.Fprintln(&str, "&tagRoomList{")
|
|
|
|
fmt.Fprintf(&str, " maxShown: %d,\n", trl.maxShown)
|
|
|
|
fmt.Fprint(&str, " rooms: {")
|
|
|
|
for i, room := range trl.rooms {
|
|
|
|
if room == nil {
|
|
|
|
fmt.Fprintf(&str, "<<NIL>>")
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(&str, room.ID)
|
|
|
|
}
|
|
|
|
if i != len(trl.rooms)-1 {
|
|
|
|
fmt.Fprint(&str, ", ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintln(&str, "},")
|
|
|
|
fmt.Fprintln(&str, "}")
|
|
|
|
return str.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) Bump(room *rooms.Room) {
|
|
|
|
var found *orderedRoom
|
|
|
|
inserted := false
|
|
|
|
for i := 0; i < len(trl.rooms); i++ {
|
|
|
|
currentRoom := trl.rooms[i]
|
|
|
|
if found != nil {
|
|
|
|
if currentRoom.LastReceivedMessage.Before(room.LastReceivedMessage) {
|
|
|
|
trl.rooms[i-1] = currentRoom
|
|
|
|
} else {
|
|
|
|
trl.rooms[i-1] = found
|
|
|
|
inserted = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else if currentRoom.Room == room {
|
|
|
|
found = currentRoom
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !inserted {
|
|
|
|
trl.rooms[len(trl.rooms)-1] = found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) Remove(room *rooms.Room) {
|
|
|
|
trl.RemoveIndex(trl.Index(room))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) RemoveIndex(index int) {
|
|
|
|
if index < 0 || index > len(trl.rooms) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
trl.rooms = append(trl.rooms[0:index], trl.rooms[index+1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) Index(room *rooms.Room) int {
|
|
|
|
return trl.indexInList(trl.All(), room)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) IndexVisible(room *rooms.Room) int {
|
|
|
|
return trl.indexInList(trl.Visible(), room)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trl *tagRoomList) indexInList(list []*orderedRoom, room *rooms.Room) int {
|
|
|
|
for index, entry := range list {
|
|
|
|
if entry.Room == room {
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
|
2018-03-25 13:21:59 +02:00
|
|
|
type RoomList struct {
|
|
|
|
*tview.Box
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
// The list of tags in display order.
|
|
|
|
tags []string
|
2018-04-23 22:22:18 +02:00
|
|
|
// The list of rooms, in reverse order.
|
2018-05-03 23:55:28 +02:00
|
|
|
items map[string]*tagRoomList
|
2018-04-23 22:22:18 +02:00
|
|
|
// The selected room.
|
2018-04-24 01:13:17 +02:00
|
|
|
selected *rooms.Room
|
|
|
|
selectedTag string
|
2018-03-25 13:21:59 +02:00
|
|
|
|
2018-04-30 11:40:28 +02:00
|
|
|
scrollOffset int
|
|
|
|
|
2018-03-25 13:21:59 +02:00
|
|
|
// The item main text color.
|
|
|
|
mainTextColor tcell.Color
|
|
|
|
// The text color for selected items.
|
|
|
|
selectedTextColor tcell.Color
|
|
|
|
// The background color for selected items.
|
|
|
|
selectedBackgroundColor tcell.Color
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRoomList() *RoomList {
|
2018-05-03 23:55:28 +02:00
|
|
|
list := &RoomList{
|
2018-04-23 21:49:17 +02:00
|
|
|
Box: tview.NewBox(),
|
2018-05-03 23:55:28 +02:00
|
|
|
items: make(map[string]*tagRoomList),
|
2018-04-25 06:01:00 +02:00
|
|
|
tags: []string{"m.favourite", "net.maunium.gomuks.fake.direct", "", "m.lowpriority"},
|
2018-03-25 13:21:59 +02:00
|
|
|
|
2018-04-30 11:40:28 +02:00
|
|
|
scrollOffset: 0,
|
|
|
|
|
2018-03-25 13:21:59 +02:00
|
|
|
mainTextColor: tcell.ColorWhite,
|
|
|
|
selectedTextColor: tcell.ColorWhite,
|
|
|
|
selectedBackgroundColor: tcell.ColorDarkGreen,
|
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
for _, tag := range list.tags {
|
|
|
|
list.items[tag] = newTagRoomList()
|
|
|
|
}
|
|
|
|
return list
|
2018-03-25 13:21:59 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 21:49:17 +02:00
|
|
|
func (list *RoomList) Contains(roomID string) bool {
|
2018-05-03 23:55:28 +02:00
|
|
|
for _, tagRoomList := range list.items {
|
|
|
|
for _, room := range tagRoomList.All() {
|
2018-04-24 01:13:17 +02:00
|
|
|
if room.ID == roomID {
|
|
|
|
return true
|
|
|
|
}
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-25 13:21:59 +02:00
|
|
|
func (list *RoomList) Add(room *rooms.Room) {
|
2018-04-24 01:13:17 +02:00
|
|
|
for _, tag := range room.Tags() {
|
2018-05-03 23:55:28 +02:00
|
|
|
list.AddToTag(tag, room)
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) CheckTag(tag string) {
|
|
|
|
index := list.IndexTag(tag)
|
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList, ok := list.items[tag]
|
2018-04-24 01:13:17 +02:00
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
if ok && tagRoomList.IsEmpty() {
|
2018-04-24 01:13:17 +02:00
|
|
|
delete(list.items, tag)
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok && index == -1 {
|
|
|
|
list.tags = append(list.tags, tag)
|
2018-04-24 21:49:41 +02:00
|
|
|
} /* TODO this doesn't work properly
|
2018-04-24 21:46:16 +02:00
|
|
|
else if index != -1 {
|
2018-04-24 01:13:17 +02:00
|
|
|
list.tags = append(list.tags[0:index], list.tags[index+1:]...)
|
2018-04-24 21:46:16 +02:00
|
|
|
}*/
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
func (list *RoomList) AddToTag(tag rooms.RoomTag, room *rooms.Room) {
|
|
|
|
if tag.Tag == "" && len(room.GetMembers()) == 2 {
|
|
|
|
tag.Tag = "net.maunium.gomuks.fake.direct"
|
2018-04-25 06:01:00 +02:00
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
|
|
|
|
tagRoomList, ok := list.items[tag.Tag]
|
2018-04-24 01:13:17 +02:00
|
|
|
if !ok {
|
2018-05-03 23:55:28 +02:00
|
|
|
list.items[tag.Tag] = newTagRoomList(convertRoom(room))
|
2018-04-24 01:13:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList.Insert(tag.Order, room)
|
|
|
|
list.CheckTag(tag.Tag)
|
2018-03-25 13:21:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) Remove(room *rooms.Room) {
|
2018-04-24 01:13:17 +02:00
|
|
|
for _, tag := range room.Tags() {
|
|
|
|
list.RemoveFromTag(tag.Tag, room)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) RemoveFromTag(tag string, room *rooms.Room) {
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList, ok := list.items[tag]
|
2018-04-24 01:13:17 +02:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
index := tagRoomList.Index(room)
|
2018-04-24 01:13:17 +02:00
|
|
|
if index == -1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList.RemoveIndex(index)
|
2018-04-24 01:13:17 +02:00
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
if tagRoomList.IsEmpty() {
|
2018-04-24 01:13:17 +02:00
|
|
|
delete(list.items, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
if room == list.selected {
|
|
|
|
if index > 0 {
|
2018-05-03 23:55:28 +02:00
|
|
|
list.selected = tagRoomList.All()[index-1].Room
|
|
|
|
} else if tagRoomList.Length() > 0 {
|
|
|
|
list.selected = tagRoomList.Visible()[0].Room
|
2018-04-24 01:13:17 +02:00
|
|
|
} else if len(list.items) > 0 {
|
|
|
|
for _, tag := range list.tags {
|
|
|
|
moreItems := list.items[tag]
|
2018-05-03 23:55:28 +02:00
|
|
|
if moreItems.Length() > 0 {
|
|
|
|
list.selected = moreItems.Visible()[0].Room
|
2018-04-24 01:13:17 +02:00
|
|
|
list.selectedTag = tag
|
|
|
|
}
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
2018-04-24 01:13:17 +02:00
|
|
|
} else {
|
|
|
|
list.selected = nil
|
|
|
|
list.selectedTag = ""
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
2018-03-25 13:21:59 +02:00
|
|
|
}
|
2018-04-24 01:13:17 +02:00
|
|
|
list.CheckTag(tag)
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) Bump(room *rooms.Room) {
|
2018-04-24 01:13:17 +02:00
|
|
|
for _, tag := range room.Tags() {
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList, ok := list.items[tag.Tag]
|
|
|
|
if !ok {
|
|
|
|
return
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList.Bump(room)
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
2018-03-25 13:21:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) Clear() {
|
2018-05-03 23:55:28 +02:00
|
|
|
list.items = make(map[string]*tagRoomList)
|
|
|
|
list.tags = []string{"m.favourite", "net.maunium.gomuks.fake.direct", "", "m.lowpriority"}
|
|
|
|
for _, tag := range list.tags {
|
|
|
|
list.items[tag] = newTagRoomList()
|
|
|
|
}
|
2018-03-25 13:21:59 +02:00
|
|
|
list.selected = nil
|
2018-04-24 01:13:17 +02:00
|
|
|
list.selectedTag = ""
|
2018-03-25 13:21:59 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
func (list *RoomList) SetSelected(tag string, room *rooms.Room) {
|
2018-03-25 13:21:59 +02:00
|
|
|
list.selected = room
|
2018-04-24 15:51:40 +02:00
|
|
|
list.selectedTag = tag
|
2018-04-30 11:40:28 +02:00
|
|
|
pos := list.index(tag, room)
|
|
|
|
_, _, _, height := list.GetRect()
|
2018-04-30 11:50:12 +02:00
|
|
|
if pos <= list.scrollOffset {
|
2018-05-03 23:55:28 +02:00
|
|
|
list.scrollOffset = pos - 1
|
2018-04-30 11:50:12 +02:00
|
|
|
} else if pos >= list.scrollOffset+height {
|
2018-05-03 23:55:28 +02:00
|
|
|
list.scrollOffset = pos - height + 1
|
|
|
|
}
|
|
|
|
if list.scrollOffset < 0 {
|
|
|
|
list.scrollOffset = 0
|
2018-04-30 11:40:28 +02:00
|
|
|
}
|
2018-04-25 06:01:00 +02:00
|
|
|
debug.Print("Selecting", room.GetTitle(), "in", list.GetTagDisplayName(tag))
|
2018-03-25 13:21:59 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 21:49:17 +02:00
|
|
|
func (list *RoomList) HasSelected() bool {
|
|
|
|
return list.selected != nil
|
|
|
|
}
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
func (list *RoomList) Selected() (string, *rooms.Room) {
|
|
|
|
return list.selectedTag, list.selected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) SelectedRoom() *rooms.Room {
|
2018-04-23 21:49:17 +02:00
|
|
|
return list.selected
|
|
|
|
}
|
|
|
|
|
2018-04-30 11:40:28 +02:00
|
|
|
func (list *RoomList) AddScrollOffset(offset int) {
|
|
|
|
list.scrollOffset += offset
|
|
|
|
_, _, _, viewHeight := list.GetRect()
|
|
|
|
contentHeight := list.ContentHeight()
|
|
|
|
if list.scrollOffset > contentHeight-viewHeight {
|
|
|
|
list.scrollOffset = contentHeight - viewHeight
|
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
if list.scrollOffset < 0 {
|
|
|
|
list.scrollOffset = 0
|
|
|
|
}
|
2018-04-30 11:40:28 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
func (list *RoomList) First() (string, *rooms.Room) {
|
|
|
|
for _, tag := range list.tags {
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList := list.items[tag]
|
|
|
|
if tagRoomList.HasVisibleRooms() {
|
|
|
|
return tag, tagRoomList.FirstVisible()
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) Last() (string, *rooms.Room) {
|
|
|
|
for tagIndex := len(list.tags) - 1; tagIndex >= 0; tagIndex-- {
|
|
|
|
tag := list.tags[tagIndex]
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList := list.items[tag]
|
|
|
|
if tagRoomList.HasVisibleRooms() {
|
|
|
|
return tag, tagRoomList.LastVisible()
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) IndexTag(tag string) int {
|
|
|
|
for index, entry := range list.tags {
|
|
|
|
if tag == entry {
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) Previous() (string, *rooms.Room) {
|
2018-04-23 21:49:17 +02:00
|
|
|
if len(list.items) == 0 {
|
2018-04-24 01:13:17 +02:00
|
|
|
return "", nil
|
2018-04-23 21:49:17 +02:00
|
|
|
} else if list.selected == nil {
|
2018-04-24 01:13:17 +02:00
|
|
|
return list.First()
|
2018-04-21 20:53:52 +02:00
|
|
|
}
|
2018-04-23 21:49:17 +02:00
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList := list.items[list.selectedTag]
|
|
|
|
indexVisible := tagRoomList.IndexVisible(list.selected)
|
|
|
|
index := tagRoomList.Index(list.selected)
|
|
|
|
|
|
|
|
if indexVisible == tagRoomList.Length()-1 || (indexVisible == -1 && index == tagRoomList.TotalLength()-1) {
|
2018-04-24 01:13:17 +02:00
|
|
|
tagIndex := list.IndexTag(list.selectedTag)
|
2018-04-24 21:46:16 +02:00
|
|
|
tagIndex--
|
|
|
|
for ; tagIndex >= 0; tagIndex-- {
|
|
|
|
prevTag := list.tags[tagIndex]
|
2018-05-03 23:55:28 +02:00
|
|
|
prevTagRoomList := list.items[prevTag]
|
|
|
|
if prevTagRoomList.HasVisibleRooms() {
|
|
|
|
return prevTag, prevTagRoomList.LastVisible()
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-24 21:46:16 +02:00
|
|
|
return list.Last()
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
if indexVisible != -1 {
|
|
|
|
return list.selectedTag, tagRoomList.Visible()[indexVisible+1].Room
|
|
|
|
} else if index != -1 {
|
|
|
|
return list.selectedTag, tagRoomList.All()[index+1].Room
|
|
|
|
}
|
|
|
|
return list.First()
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
func (list *RoomList) Next() (string, *rooms.Room) {
|
2018-04-23 21:49:17 +02:00
|
|
|
if len(list.items) == 0 {
|
2018-04-24 01:13:17 +02:00
|
|
|
return "", nil
|
2018-04-23 21:49:17 +02:00
|
|
|
} else if list.selected == nil {
|
2018-04-24 01:13:17 +02:00
|
|
|
return list.First()
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList := list.items[list.selectedTag]
|
|
|
|
indexVisible := tagRoomList.IndexVisible(list.selected)
|
|
|
|
index := tagRoomList.Index(list.selected)
|
|
|
|
|
|
|
|
if indexVisible == 0 || (indexVisible == -1 && index == 0) {
|
2018-04-24 01:13:17 +02:00
|
|
|
tagIndex := list.IndexTag(list.selectedTag)
|
2018-04-24 21:46:16 +02:00
|
|
|
tagIndex++
|
|
|
|
for ; tagIndex < len(list.tags); tagIndex++ {
|
|
|
|
nextTag := list.tags[tagIndex]
|
2018-05-03 23:55:28 +02:00
|
|
|
nextTagRoomList := list.items[nextTag]
|
|
|
|
if nextTagRoomList.HasVisibleRooms() {
|
|
|
|
return nextTag, nextTagRoomList.FirstVisible()
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-24 21:46:16 +02:00
|
|
|
return list.First()
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
if indexVisible != -1 {
|
|
|
|
return list.selectedTag, tagRoomList.Visible()[indexVisible-1].Room
|
|
|
|
} else if index != -1 {
|
|
|
|
return list.selectedTag, tagRoomList.All()[index-1].Room
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
return list.Last()
|
2018-04-23 21:49:17 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 11:40:28 +02:00
|
|
|
func (list *RoomList) index(tag string, room *rooms.Room) int {
|
|
|
|
tagIndex := list.IndexTag(tag)
|
|
|
|
if tagIndex == -1 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList, ok := list.items[tag]
|
|
|
|
localIndex := -1
|
|
|
|
if ok {
|
|
|
|
localIndex = tagRoomList.IndexVisible(room)
|
|
|
|
}
|
2018-04-30 11:40:28 +02:00
|
|
|
if localIndex == -1 {
|
|
|
|
return -1
|
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
localIndex = tagRoomList.Length() - 1 - localIndex
|
2018-04-30 11:40:28 +02:00
|
|
|
|
|
|
|
// Tag header
|
|
|
|
localIndex += 1
|
|
|
|
|
|
|
|
if tagIndex > 0 {
|
|
|
|
for i := 0; i < tagIndex; i++ {
|
|
|
|
previousTag := list.tags[i]
|
2018-05-03 23:55:28 +02:00
|
|
|
previousTagRoomList := list.items[previousTag]
|
2018-04-30 11:40:28 +02:00
|
|
|
|
|
|
|
tagDisplayName := list.GetTagDisplayName(previousTag)
|
|
|
|
if len(tagDisplayName) > 0 {
|
2018-05-03 23:55:28 +02:00
|
|
|
if previousTagRoomList.IsCollapsed() {
|
|
|
|
localIndex++
|
|
|
|
continue
|
|
|
|
}
|
2018-04-30 11:40:28 +02:00
|
|
|
// Previous tag header + space
|
|
|
|
localIndex += 2
|
2018-05-03 23:55:28 +02:00
|
|
|
if previousTagRoomList.HasInvisibleRooms() {
|
|
|
|
// Previous tag "Show more" button
|
|
|
|
localIndex++
|
|
|
|
}
|
2018-04-30 11:40:28 +02:00
|
|
|
// Previous tag items
|
2018-05-03 23:55:28 +02:00
|
|
|
localIndex += previousTagRoomList.Length()
|
2018-04-30 11:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return localIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *RoomList) ContentHeight() (height int) {
|
|
|
|
for _, tag := range list.tags {
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList := list.items[tag]
|
2018-04-30 11:40:28 +02:00
|
|
|
tagDisplayName := list.GetTagDisplayName(tag)
|
|
|
|
if len(tagDisplayName) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
if tagRoomList.IsCollapsed() {
|
|
|
|
height++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
height += 2 + tagRoomList.Length()
|
|
|
|
if tagRoomList.HasInvisibleRooms() {
|
|
|
|
height++
|
|
|
|
}
|
2018-04-30 11:40:28 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
func (list *RoomList) HandleClick(column, line int, mod bool) (string, *rooms.Room) {
|
|
|
|
line += list.scrollOffset
|
|
|
|
if line < 0 {
|
2018-04-24 01:13:17 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
for _, tag := range list.tags {
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList := list.items[tag]
|
|
|
|
if line--; line == -1 {
|
|
|
|
tagRoomList.ToggleCollapse()
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if tagRoomList.IsCollapsed() {
|
|
|
|
continue
|
|
|
|
}
|
2018-04-24 01:13:17 +02:00
|
|
|
|
2018-05-03 23:55:28 +02:00
|
|
|
if line < 0 {
|
2018-04-24 01:13:17 +02:00
|
|
|
return "", nil
|
2018-05-03 23:55:28 +02:00
|
|
|
} else if line < tagRoomList.Length() {
|
|
|
|
return tag, tagRoomList.Visible()[tagRoomList.Length()-1-line].Room
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tag items
|
2018-05-03 23:55:28 +02:00
|
|
|
line -= tagRoomList.Length()
|
|
|
|
|
|
|
|
hasMore := tagRoomList.HasInvisibleRooms()
|
|
|
|
hasLess := tagRoomList.maxShown > 10
|
|
|
|
if hasMore || hasLess {
|
|
|
|
if line--; line == -1 {
|
|
|
|
diff := 10
|
|
|
|
if mod {
|
|
|
|
diff = 100
|
|
|
|
}
|
|
|
|
_, _, width, _ := list.GetRect()
|
|
|
|
if column <= 6 && hasLess {
|
|
|
|
tagRoomList.maxShown -= diff
|
|
|
|
} else if column >= width-6 && hasMore {
|
|
|
|
tagRoomList.maxShown += diff
|
|
|
|
}
|
|
|
|
if tagRoomList.maxShown < 10 {
|
|
|
|
tagRoomList.maxShown = 10
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 15:51:40 +02:00
|
|
|
// Tag footer
|
2018-05-03 23:55:28 +02:00
|
|
|
line--
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var nsRegex = regexp.MustCompile("^[a-z]\\.[a-z](?:\\.[a-z])*$")
|
|
|
|
|
|
|
|
func (list *RoomList) GetTagDisplayName(tag string) string {
|
|
|
|
switch {
|
|
|
|
case len(tag) == 0:
|
|
|
|
return "Rooms"
|
|
|
|
case tag == "m.favourite":
|
|
|
|
return "Favorites"
|
|
|
|
case tag == "m.lowpriority":
|
|
|
|
return "Low Priority"
|
2018-04-25 06:01:00 +02:00
|
|
|
case tag == "net.maunium.gomuks.fake.direct":
|
2018-04-24 21:46:16 +02:00
|
|
|
return "People"
|
2018-04-24 01:13:17 +02:00
|
|
|
case strings.HasPrefix(tag, "u."):
|
|
|
|
return tag[len("u."):]
|
|
|
|
case !nsRegex.MatchString(tag):
|
|
|
|
return tag
|
|
|
|
default:
|
|
|
|
return ""
|
2018-04-23 22:22:18 +02:00
|
|
|
}
|
2018-04-21 20:53:52 +02:00
|
|
|
}
|
|
|
|
|
2018-03-25 13:21:59 +02:00
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (list *RoomList) Draw(screen tcell.Screen) {
|
|
|
|
list.Box.Draw(screen)
|
|
|
|
|
|
|
|
x, y, width, height := list.GetInnerRect()
|
|
|
|
bottomLimit := y + height
|
|
|
|
|
2018-04-30 11:40:28 +02:00
|
|
|
handledOffset := 0
|
2018-03-25 13:21:59 +02:00
|
|
|
|
|
|
|
// Draw the list items.
|
2018-04-24 01:13:17 +02:00
|
|
|
for _, tag := range list.tags {
|
2018-05-03 23:55:28 +02:00
|
|
|
tagRoomList := list.items[tag]
|
2018-04-25 06:01:00 +02:00
|
|
|
tagDisplayName := list.GetTagDisplayName(tag)
|
|
|
|
if len(tagDisplayName) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-30 11:40:28 +02:00
|
|
|
localOffset := 0
|
|
|
|
|
|
|
|
if handledOffset < list.scrollOffset {
|
2018-05-03 23:55:28 +02:00
|
|
|
if handledOffset+tagRoomList.Length() < list.scrollOffset {
|
|
|
|
if tagRoomList.IsCollapsed() {
|
|
|
|
handledOffset++
|
|
|
|
} else {
|
|
|
|
handledOffset += tagRoomList.Length() + 2
|
|
|
|
if tagRoomList.HasInvisibleRooms() || tagRoomList.maxShown > 10 {
|
|
|
|
handledOffset++
|
|
|
|
}
|
|
|
|
}
|
2018-04-30 11:40:28 +02:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
localOffset = list.scrollOffset - handledOffset
|
|
|
|
handledOffset += localOffset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 06:01:00 +02:00
|
|
|
widget.WriteLine(screen, tview.AlignLeft, tagDisplayName, x, y, width, tcell.StyleDefault.Underline(true).Bold(true))
|
2018-05-03 23:55:28 +02:00
|
|
|
|
|
|
|
items := tagRoomList.Visible()
|
|
|
|
|
|
|
|
if tagRoomList.IsCollapsed() {
|
|
|
|
screen.SetCell(x+width-1, y, tcell.StyleDefault, '▶')
|
|
|
|
y++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
screen.SetCell(x+width-1, y, tcell.StyleDefault, '▼')
|
2018-04-24 01:13:17 +02:00
|
|
|
y++
|
2018-05-03 23:55:28 +02:00
|
|
|
|
|
|
|
for i := tagRoomList.Length() - 1; i >= 0; i-- {
|
2018-04-24 01:13:17 +02:00
|
|
|
item := items[i]
|
|
|
|
index := len(items) - 1 - i
|
2018-03-25 13:21:59 +02:00
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
if y >= bottomLimit {
|
|
|
|
break
|
|
|
|
}
|
2018-03-25 13:21:59 +02:00
|
|
|
|
2018-04-30 11:40:28 +02:00
|
|
|
if index < localOffset {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
text := item.GetTitle()
|
2018-03-25 13:21:59 +02:00
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
lineWidth := width
|
2018-03-26 16:22:47 +02:00
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
style := tcell.StyleDefault.Foreground(list.mainTextColor)
|
2018-05-03 23:55:28 +02:00
|
|
|
if tag == list.selectedTag && item.Room == list.selected {
|
2018-04-24 01:13:17 +02:00
|
|
|
style = style.Foreground(list.selectedTextColor).Background(list.selectedBackgroundColor)
|
2018-03-26 16:22:47 +02:00
|
|
|
}
|
2018-04-24 01:13:17 +02:00
|
|
|
if item.HasNewMessages {
|
|
|
|
style = style.Bold(true)
|
2018-03-25 13:21:59 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
if item.UnreadMessages > 0 {
|
|
|
|
unreadMessageCount := "99+"
|
|
|
|
if item.UnreadMessages < 100 {
|
|
|
|
unreadMessageCount = strconv.Itoa(item.UnreadMessages)
|
|
|
|
}
|
|
|
|
if item.Highlighted {
|
|
|
|
unreadMessageCount += "!"
|
|
|
|
}
|
|
|
|
unreadMessageCount = fmt.Sprintf("(%s)", unreadMessageCount)
|
|
|
|
widget.WriteLine(screen, tview.AlignRight, unreadMessageCount, x+lineWidth-6, y, 6, style)
|
|
|
|
lineWidth -= len(unreadMessageCount) + 1
|
|
|
|
}
|
2018-03-26 16:22:47 +02:00
|
|
|
|
2018-05-04 00:09:55 +02:00
|
|
|
widget.WriteLinePadded(screen, tview.AlignLeft, text, x, y, lineWidth, style)
|
2018-04-24 01:13:17 +02:00
|
|
|
y++
|
2018-05-03 23:55:28 +02:00
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
if y >= bottomLimit {
|
|
|
|
break
|
|
|
|
}
|
2018-03-25 13:21:59 +02:00
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
hasLess := tagRoomList.maxShown > 10
|
|
|
|
hasMore := tagRoomList.HasInvisibleRooms()
|
|
|
|
if hasLess || hasMore {
|
|
|
|
if hasMore {
|
|
|
|
widget.WriteLine(screen, tview.AlignRight, "More ↓", x, y, width, tcell.StyleDefault)
|
|
|
|
}
|
|
|
|
if hasLess {
|
|
|
|
widget.WriteLine(screen, tview.AlignLeft, "↑ Less", x, y, width, tcell.StyleDefault)
|
|
|
|
}
|
|
|
|
y++
|
|
|
|
}
|
|
|
|
|
2018-04-24 15:51:40 +02:00
|
|
|
y++
|
2018-03-25 13:21:59 +02:00
|
|
|
}
|
|
|
|
}
|