| 1 |
// gomuks - A terminal Matrix client written in Go. |
| 2 |
// Copyright (C) 2020 Tulir Asokan |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU Affero General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU Affero General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Affero General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package ui |
| 18 |
|
| 19 |
import ( |
| 20 |
"math" |
| 21 |
"regexp" |
| 22 |
"sort" |
| 23 |
"strings" |
| 24 |
|
| 25 |
sync "github.com/sasha-s/go-deadlock" |
| 26 |
|
| 27 |
"go.mau.fi/mauview" |
| 28 |
"go.mau.fi/tcell" |
| 29 |
|
| 30 |
"maunium.net/go/mautrix/id" |
| 31 |
|
| 32 |
"maunium.net/go/gomuks/debug" |
| 33 |
"maunium.net/go/gomuks/matrix/rooms" |
| 34 |
) |
| 35 |
|
| 36 |
var tagOrder = map[string]int{ |
| 37 |
"net.maunium.gomuks.fake.invite": 4, |
| 38 |
"m.favourite": 3, |
| 39 |
"net.maunium.gomuks.fake.direct": 2, |
| 40 |
"": 1, |
| 41 |
"m.lowpriority": -1, |
| 42 |
"m.server_notice": -2, |
| 43 |
"net.maunium.gomuks.fake.leave": -3, |
| 44 |
} |
| 45 |
|
| 46 |
// TagNameList is a list of Matrix tag names where default names are sorted in a hardcoded way. |
| 47 |
type TagNameList []string |
| 48 |
|
| 49 |
func (tnl TagNameList) Len() int { |
| 50 |
return len(tnl) |
| 51 |
} |
| 52 |
|
| 53 |
func (tnl TagNameList) Less(i, j int) bool { |
| 54 |
orderI, _ := tagOrder[tnl[i]] |
| 55 |
orderJ, _ := tagOrder[tnl[j]] |
| 56 |
if orderI != orderJ { |
| 57 |
return orderI > orderJ |
| 58 |
} |
| 59 |
return strings.Compare(tnl[i], tnl[j]) > 0 |
| 60 |
} |
| 61 |
|
| 62 |
func (tnl TagNameList) Swap(i, j int) { |
| 63 |
tnl[i], tnl[j] = tnl[j], tnl[i] |
| 64 |
} |
| 65 |
|
| 66 |
type RoomList struct { |
| 67 |
sync.RWMutex |
| 68 |
|
| 69 |
parent *MainView |
| 70 |
|
| 71 |
// The list of tags in display order. |
| 72 |
tags TagNameList |
| 73 |
// The list of rooms, in reverse order. |
| 74 |
items map[string]*TagRoomList |
| 75 |
// The selected room. |
| 76 |
selected *rooms.Room |
| 77 |
selectedTag string |
| 78 |
|
| 79 |
scrollOffset int |
| 80 |
height int |
| 81 |
width int |
| 82 |
|
| 83 |
// The item main text color. |
| 84 |
mainTextColor tcell.Color |
| 85 |
// The text color for selected items. |
| 86 |
selectedTextColor tcell.Color |
| 87 |
// The background color for selected items. |
| 88 |
selectedBackgroundColor tcell.Color |
| 89 |
} |
| 90 |
|
| 91 |
func NewRoomList(parent *MainView) *RoomList { |
| 92 |
list := &RoomList{ |
| 93 |
parent: parent, |
| 94 |
|
| 95 |
items: make(map[string]*TagRoomList), |
| 96 |
tags: []string{}, |
| 97 |
|
| 98 |
scrollOffset: 0, |
| 99 |
|
| 100 |
mainTextColor: tcell.ColorDefault, |
| 101 |
selectedTextColor: tcell.ColorWhite, |
| 102 |
selectedBackgroundColor: tcell.ColorDarkGreen, |
| 103 |
} |
| 104 |
for _, tag := range list.tags { |
| 105 |
list.items[tag] = NewTagRoomList(list, tag) |
| 106 |
} |
| 107 |
return list |
| 108 |
} |
| 109 |
|
| 110 |
func (list *RoomList) Contains(roomID id.RoomID) bool { |
| 111 |
list.RLock() |
| 112 |
defer list.RUnlock() |
| 113 |
for _, trl := range list.items { |
| 114 |
for _, room := range trl.All() { |
| 115 |
if room.ID == roomID { |
| 116 |
return true |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
return false |
| 121 |
} |
| 122 |
|
| 123 |
func (list *RoomList) Add(room *rooms.Room) { |
| 124 |
if room.IsReplaced() { |
| 125 |
debug.Print(room.ID, "is replaced by", room.ReplacedBy(), "-> not adding to room list") |
| 126 |
return |
| 127 |
} |
| 128 |
debug.Print("Adding room to list", room.ID, room.GetTitle(), room.IsDirect, room.ReplacedBy(), room.Tags()) |
| 129 |
for _, tag := range room.Tags() { |
| 130 |
list.AddToTag(tag, room) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func (list *RoomList) checkTag(tag string) { |
| 135 |
index := list.indexTag(tag) |
| 136 |
|
| 137 |
trl, ok := list.items[tag] |
| 138 |
|
| 139 |
if ok && trl.IsEmpty() { |
| 140 |
delete(list.items, tag) |
| 141 |
ok = false |
| 142 |
} |
| 143 |
|
| 144 |
if ok && index == -1 { |
| 145 |
list.tags = append(list.tags, tag) |
| 146 |
sort.Sort(list.tags) |
| 147 |
} else if !ok && index != -1 { |
| 148 |
list.tags = append(list.tags[0:index], list.tags[index+1:]...) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func (list *RoomList) AddToTag(tag rooms.RoomTag, room *rooms.Room) { |
| 153 |
list.Lock() |
| 154 |
defer list.Unlock() |
| 155 |
trl, ok := list.items[tag.Tag] |
| 156 |
if !ok { |
| 157 |
list.items[tag.Tag] = NewTagRoomList(list, tag.Tag, NewOrderedRoom(tag.Order, room)) |
| 158 |
} else { |
| 159 |
trl.Insert(tag.Order, room) |
| 160 |
} |
| 161 |
list.checkTag(tag.Tag) |
| 162 |
} |
| 163 |
|
| 164 |
func (list *RoomList) Remove(room *rooms.Room) { |
| 165 |
for _, tag := range list.tags { |
| 166 |
list.RemoveFromTag(tag, room) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func (list *RoomList) RemoveFromTag(tag string, room *rooms.Room) { |
| 171 |
list.Lock() |
| 172 |
defer list.Unlock() |
| 173 |
trl, ok := list.items[tag] |
| 174 |
if !ok { |
| 175 |
return |
| 176 |
} |
| 177 |
|
| 178 |
index := trl.Index(room) |
| 179 |
if index == -1 { |
| 180 |
return |
| 181 |
} |
| 182 |
|
| 183 |
trl.RemoveIndex(index) |
| 184 |
|
| 185 |
if trl.IsEmpty() { |
| 186 |
// delete(list.items, tag) |
| 187 |
} |
| 188 |
|
| 189 |
if room == list.selected { |
| 190 |
if index > 0 { |
| 191 |
list.selected = trl.All()[index-1].Room |
| 192 |
} else if trl.Length() > 0 { |
| 193 |
list.selected = trl.Visible()[0].Room |
| 194 |
} else if len(list.items) > 0 { |
| 195 |
for _, tag := range list.tags { |
| 196 |
moreItems := list.items[tag] |
| 197 |
if moreItems.Length() > 0 { |
| 198 |
list.selected = moreItems.Visible()[0].Room |
| 199 |
list.selectedTag = tag |
| 200 |
} |
| 201 |
} |
| 202 |
} else { |
| 203 |
list.selected = nil |
| 204 |
list.selectedTag = "" |
| 205 |
} |
| 206 |
} |
| 207 |
list.checkTag(tag) |
| 208 |
} |
| 209 |
|
| 210 |
func (list *RoomList) Bump(room *rooms.Room) { |
| 211 |
list.RLock() |
| 212 |
defer list.RUnlock() |
| 213 |
for _, tag := range room.Tags() { |
| 214 |
trl, ok := list.items[tag.Tag] |
| 215 |
if !ok { |
| 216 |
return |
| 217 |
} |
| 218 |
trl.Bump(room) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
func (list *RoomList) Clear() { |
| 223 |
list.Lock() |
| 224 |
defer list.Unlock() |
| 225 |
list.items = make(map[string]*TagRoomList) |
| 226 |
list.tags = []string{} |
| 227 |
for _, tag := range list.tags { |
| 228 |
list.items[tag] = NewTagRoomList(list, tag) |
| 229 |
} |
| 230 |
list.selected = nil |
| 231 |
list.selectedTag = "" |
| 232 |
} |
| 233 |
|
| 234 |
func (list *RoomList) SetSelected(tag string, room *rooms.Room) { |
| 235 |
list.selected = room |
| 236 |
list.selectedTag = tag |
| 237 |
pos := list.index(tag, room) |
| 238 |
if pos <= list.scrollOffset { |
| 239 |
list.scrollOffset = pos - 1 |
| 240 |
} else if pos >= list.scrollOffset+list.height { |
| 241 |
list.scrollOffset = pos - list.height + 1 |
| 242 |
} |
| 243 |
if list.scrollOffset < 0 { |
| 244 |
list.scrollOffset = 0 |
| 245 |
} |
| 246 |
debug.Print("Selecting", room.GetTitle(), "in", list.GetTagDisplayName(tag)) |
| 247 |
} |
| 248 |
|
| 249 |
func (list *RoomList) HasSelected() bool { |
| 250 |
return list.selected != nil |
| 251 |
} |
| 252 |
|
| 253 |
func (list *RoomList) Selected() (string, *rooms.Room) { |
| 254 |
return list.selectedTag, list.selected |
| 255 |
} |
| 256 |
|
| 257 |
func (list *RoomList) SelectedRoom() *rooms.Room { |
| 258 |
return list.selected |
| 259 |
} |
| 260 |
|
| 261 |
func (list *RoomList) AddScrollOffset(offset int) { |
| 262 |
list.scrollOffset += offset |
| 263 |
contentHeight := list.ContentHeight() |
| 264 |
if list.scrollOffset > contentHeight-list.height { |
| 265 |
list.scrollOffset = contentHeight - list.height |
| 266 |
} |
| 267 |
if list.scrollOffset < 0 { |
| 268 |
list.scrollOffset = 0 |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
func (list *RoomList) First() (string, *rooms.Room) { |
| 273 |
list.RLock() |
| 274 |
defer list.RUnlock() |
| 275 |
return list.first() |
| 276 |
} |
| 277 |
|
| 278 |
func (list *RoomList) first() (string, *rooms.Room) { |
| 279 |
for _, tag := range list.tags { |
| 280 |
trl := list.items[tag] |
| 281 |
if trl.HasVisibleRooms() { |
| 282 |
return tag, trl.FirstVisible() |
| 283 |
} |
| 284 |
} |
| 285 |
return "", nil |
| 286 |
} |
| 287 |
|
| 288 |
func (list *RoomList) Last() (string, *rooms.Room) { |
| 289 |
list.RLock() |
| 290 |
defer list.RUnlock() |
| 291 |
return list.last() |
| 292 |
} |
| 293 |
|
| 294 |
func (list *RoomList) last() (string, *rooms.Room) { |
| 295 |
for tagIndex := len(list.tags) - 1; tagIndex >= 0; tagIndex-- { |
| 296 |
tag := list.tags[tagIndex] |
| 297 |
trl := list.items[tag] |
| 298 |
if trl.HasVisibleRooms() { |
| 299 |
return tag, trl.LastVisible() |
| 300 |
} |
| 301 |
} |
| 302 |
return "", nil |
| 303 |
} |
| 304 |
|
| 305 |
func (list *RoomList) indexTag(tag string) int { |
| 306 |
for index, entry := range list.tags { |
| 307 |
if tag == entry { |
| 308 |
return index |
| 309 |
} |
| 310 |
} |
| 311 |
return -1 |
| 312 |
} |
| 313 |
|
| 314 |
func (list *RoomList) Previous() (string, *rooms.Room) { |
| 315 |
list.RLock() |
| 316 |
defer list.RUnlock() |
| 317 |
if len(list.items) == 0 { |
| 318 |
return "", nil |
| 319 |
} else if list.selected == nil { |
| 320 |
return list.first() |
| 321 |
} |
| 322 |
|
| 323 |
trl := list.items[list.selectedTag] |
| 324 |
index := trl.IndexVisible(list.selected) |
| 325 |
indexInvisible := trl.Index(list.selected) |
| 326 |
if index == -1 && indexInvisible >= 0 { |
| 327 |
num := trl.TotalLength() - indexInvisible |
| 328 |
trl.maxShown = int(math.Ceil(float64(num)/10.0) * 10.0) |
| 329 |
index = trl.IndexVisible(list.selected) |
| 330 |
} |
| 331 |
|
| 332 |
if index == trl.Length()-1 { |
| 333 |
tagIndex := list.indexTag(list.selectedTag) |
| 334 |
tagIndex-- |
| 335 |
for ; tagIndex >= 0; tagIndex-- { |
| 336 |
prevTag := list.tags[tagIndex] |
| 337 |
prevTRL := list.items[prevTag] |
| 338 |
if prevTRL.HasVisibleRooms() { |
| 339 |
return prevTag, prevTRL.LastVisible() |
| 340 |
} |
| 341 |
} |
| 342 |
return list.last() |
| 343 |
} else if index >= 0 { |
| 344 |
return list.selectedTag, trl.Visible()[index+1].Room |
| 345 |
} |
| 346 |
return list.first() |
| 347 |
} |
| 348 |
|
| 349 |
func (list *RoomList) Next() (string, *rooms.Room) { |
| 350 |
list.RLock() |
| 351 |
defer list.RUnlock() |
| 352 |
if len(list.items) == 0 { |
| 353 |
return "", nil |
| 354 |
} else if list.selected == nil { |
| 355 |
return list.first() |
| 356 |
} |
| 357 |
|
| 358 |
trl := list.items[list.selectedTag] |
| 359 |
index := trl.IndexVisible(list.selected) |
| 360 |
indexInvisible := trl.Index(list.selected) |
| 361 |
if index == -1 && indexInvisible >= 0 { |
| 362 |
num := trl.TotalLength() - indexInvisible + 1 |
| 363 |
trl.maxShown = int(math.Ceil(float64(num)/10.0) * 10.0) |
| 364 |
index = trl.IndexVisible(list.selected) |
| 365 |
} |
| 366 |
|
| 367 |
if index == 0 { |
| 368 |
tagIndex := list.indexTag(list.selectedTag) |
| 369 |
tagIndex++ |
| 370 |
for ; tagIndex < len(list.tags); tagIndex++ { |
| 371 |
nextTag := list.tags[tagIndex] |
| 372 |
nextTRL := list.items[nextTag] |
| 373 |
if nextTRL.HasVisibleRooms() { |
| 374 |
return nextTag, nextTRL.FirstVisible() |
| 375 |
} |
| 376 |
} |
| 377 |
return list.first() |
| 378 |
} else if index > 0 { |
| 379 |
return list.selectedTag, trl.Visible()[index-1].Room |
| 380 |
} |
| 381 |
return list.last() |
| 382 |
} |
| 383 |
|
| 384 |
// NextWithActivity Returns next room with activity. |
| 385 |
// |
| 386 |
// Sorted by (in priority): |
| 387 |
// |
| 388 |
// - Highlights |
| 389 |
// - Messages |
| 390 |
// - Other traffic (joins, parts, etc) |
| 391 |
// |
| 392 |
// TODO: Sorting. Now just finds first room with new messages. |
| 393 |
func (list *RoomList) NextWithActivity() (string, *rooms.Room) { |
| 394 |
list.RLock() |
| 395 |
defer list.RUnlock() |
| 396 |
for tag, trl := range list.items { |
| 397 |
for _, room := range trl.All() { |
| 398 |
if room.HasNewMessages() { |
| 399 |
return tag, room.Room |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
// No room with activity found |
| 404 |
return "", nil |
| 405 |
} |
| 406 |
|
| 407 |
func (list *RoomList) index(tag string, room *rooms.Room) int { |
| 408 |
tagIndex := list.indexTag(tag) |
| 409 |
if tagIndex == -1 { |
| 410 |
return -1 |
| 411 |
} |
| 412 |
|
| 413 |
trl, ok := list.items[tag] |
| 414 |
localIndex := -1 |
| 415 |
if ok { |
| 416 |
localIndex = trl.IndexVisible(room) |
| 417 |
} |
| 418 |
if localIndex == -1 { |
| 419 |
return -1 |
| 420 |
} |
| 421 |
localIndex = trl.Length() - 1 - localIndex |
| 422 |
|
| 423 |
// Tag header |
| 424 |
localIndex++ |
| 425 |
|
| 426 |
if tagIndex > 0 { |
| 427 |
for i := 0; i < tagIndex; i++ { |
| 428 |
prevTag := list.tags[i] |
| 429 |
|
| 430 |
prevTRL := list.items[prevTag] |
| 431 |
localIndex += prevTRL.RenderHeight() |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
return localIndex |
| 436 |
} |
| 437 |
|
| 438 |
func (list *RoomList) ContentHeight() (height int) { |
| 439 |
list.RLock() |
| 440 |
for _, tag := range list.tags { |
| 441 |
height += list.items[tag].RenderHeight() |
| 442 |
} |
| 443 |
list.RUnlock() |
| 444 |
return |
| 445 |
} |
| 446 |
|
| 447 |
func (list *RoomList) OnKeyEvent(_ mauview.KeyEvent) bool { |
| 448 |
return false |
| 449 |
} |
| 450 |
|
| 451 |
func (list *RoomList) OnPasteEvent(_ mauview.PasteEvent) bool { |
| 452 |
return false |
| 453 |
} |
| 454 |
|
| 455 |
func (list *RoomList) OnMouseEvent(event mauview.MouseEvent) bool { |
| 456 |
if event.HasMotion() { |
| 457 |
return false |
| 458 |
} |
| 459 |
switch event.Buttons() { |
| 460 |
case tcell.WheelUp: |
| 461 |
list.AddScrollOffset(-WheelScrollOffsetDiff) |
| 462 |
return true |
| 463 |
case tcell.WheelDown: |
| 464 |
list.AddScrollOffset(WheelScrollOffsetDiff) |
| 465 |
return true |
| 466 |
case tcell.Button1: |
| 467 |
x, y := event.Position() |
| 468 |
return list.clickRoom(y, x, event.Modifiers() == tcell.ModCtrl) |
| 469 |
} |
| 470 |
return false |
| 471 |
} |
| 472 |
|
| 473 |
func (list *RoomList) Focus() { |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
func (list *RoomList) Blur() { |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
func (list *RoomList) clickRoom(line, column int, mod bool) bool { |
| 482 |
line += list.scrollOffset |
| 483 |
if line < 0 { |
| 484 |
return false |
| 485 |
} |
| 486 |
list.RLock() |
| 487 |
for _, tag := range list.tags { |
| 488 |
trl := list.items[tag] |
| 489 |
if line--; line == -1 { |
| 490 |
trl.ToggleCollapse() |
| 491 |
list.RUnlock() |
| 492 |
return true |
| 493 |
} |
| 494 |
|
| 495 |
if trl.IsCollapsed() { |
| 496 |
continue |
| 497 |
} |
| 498 |
|
| 499 |
if line < 0 { |
| 500 |
break |
| 501 |
} else if line < trl.Length() { |
| 502 |
switchToRoom := trl.Visible()[trl.Length()-1-line].Room |
| 503 |
list.RUnlock() |
| 504 |
list.parent.SwitchRoom(tag, switchToRoom) |
| 505 |
return true |
| 506 |
} |
| 507 |
|
| 508 |
// Tag items |
| 509 |
line -= trl.Length() |
| 510 |
|
| 511 |
hasMore := trl.HasInvisibleRooms() |
| 512 |
hasLess := trl.maxShown > 10 |
| 513 |
if hasMore || hasLess { |
| 514 |
if line--; line == -1 { |
| 515 |
diff := 10 |
| 516 |
if mod { |
| 517 |
diff = 100 |
| 518 |
} |
| 519 |
if column <= 6 && hasLess { |
| 520 |
trl.maxShown -= diff |
| 521 |
} else if column >= list.width-6 && hasMore { |
| 522 |
trl.maxShown += diff |
| 523 |
} |
| 524 |
if trl.maxShown < 10 { |
| 525 |
trl.maxShown = 10 |
| 526 |
} |
| 527 |
list.RUnlock() |
| 528 |
return true |
| 529 |
} |
| 530 |
} |
| 531 |
// Tag footer |
| 532 |
line-- |
| 533 |
} |
| 534 |
list.RUnlock() |
| 535 |
return false |
| 536 |
} |
| 537 |
|
| 538 |
var nsRegex = regexp.MustCompile("^[a-z]+\\.[a-z]+(?:\\.[a-z]+)*$") |
| 539 |
|
| 540 |
func (list *RoomList) GetTagDisplayName(tag string) string { |
| 541 |
switch { |
| 542 |
case len(tag) == 0: |
| 543 |
return "Rooms" |
| 544 |
case tag == "m.favourite": |
| 545 |
return "Favorites" |
| 546 |
case tag == "m.lowpriority": |
| 547 |
return "Low Priority" |
| 548 |
case tag == "m.server_notice": |
| 549 |
return "System Alerts" |
| 550 |
case tag == "net.maunium.gomuks.fake.direct": |
| 551 |
return "People" |
| 552 |
case tag == "net.maunium.gomuks.fake.invite": |
| 553 |
return "Invites" |
| 554 |
case tag == "net.maunium.gomuks.fake.leave": |
| 555 |
return "Historical" |
| 556 |
case strings.HasPrefix(tag, "u."): |
| 557 |
return tag[len("u."):] |
| 558 |
case !nsRegex.MatchString(tag): |
| 559 |
return tag |
| 560 |
default: |
| 561 |
return "" |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
// Draw draws this primitive onto the screen. |
| 566 |
func (list *RoomList) Draw(screen mauview.Screen) { |
| 567 |
list.width, list.height = screen.Size() |
| 568 |
y := 0 |
| 569 |
yLimit := y + list.height |
| 570 |
y -= list.scrollOffset |
| 571 |
|
| 572 |
// Draw the list items. |
| 573 |
list.RLock() |
| 574 |
for _, tag := range list.tags { |
| 575 |
trl := list.items[tag] |
| 576 |
tagDisplayName := list.GetTagDisplayName(tag) |
| 577 |
if trl == nil || len(tagDisplayName) == 0 { |
| 578 |
continue |
| 579 |
} |
| 580 |
|
| 581 |
renderHeight := trl.RenderHeight() |
| 582 |
if y+renderHeight >= yLimit { |
| 583 |
renderHeight = yLimit - y |
| 584 |
} |
| 585 |
trl.Draw(mauview.NewProxyScreen(screen, 0, y, list.width, renderHeight)) |
| 586 |
y += renderHeight |
| 587 |
if y >= yLimit { |
| 588 |
break |
| 589 |
} |
| 590 |
} |
| 591 |
list.RUnlock() |
| 592 |
} |