aboutsummaryrefslogtreecommitdiff
path: root/ui/room-list.go
blob: 3ef64c3918c3b41f3cba5ea4096ba3d33d8ea673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
// 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/>.

package ui

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"

	"maunium.net/go/gomuks/debug"
	"maunium.net/go/gomuks/matrix/rooms"
	"maunium.net/go/gomuks/ui/widget"
	"maunium.net/go/tcell"
	"maunium.net/go/tview"
)

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
}

type RoomList struct {
	*tview.Box

	// The list of tags in display order.
	tags []string
	// The list of rooms, in reverse order.
	items map[string]*tagRoomList
	// The selected room.
	selected    *rooms.Room
	selectedTag string

	scrollOffset int

	// 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 {
	list := &RoomList{
		Box:   tview.NewBox(),
		items: make(map[string]*tagRoomList),
		tags:  []string{"m.favourite", "net.maunium.gomuks.fake.direct", "", "m.lowpriority"},

		scrollOffset: 0,

		mainTextColor:           tcell.ColorWhite,
		selectedTextColor:       tcell.ColorWhite,
		selectedBackgroundColor: tcell.ColorDarkGreen,
	}
	for _, tag := range list.tags {
		list.items[tag] = newTagRoomList()
	}
	return list
}

func (list *RoomList) Contains(roomID string) bool {
	for _, tagRoomList := range list.items {
		for _, room := range tagRoomList.All() {
			if room.ID == roomID {
				return true
			}
		}
	}
	return false
}

func (list *RoomList) Add(room *rooms.Room) {
	for _, tag := range room.Tags() {
		list.AddToTag(tag, room)
	}
}

func (list *RoomList) CheckTag(tag string) {
	index := list.IndexTag(tag)

	tagRoomList, ok := list.items[tag]

	if ok && tagRoomList.IsEmpty() {
		delete(list.items, tag)
		ok = false
	}

	if ok && index == -1 {
		list.tags = append(list.tags, tag)
	} /* TODO this doesn't work properly
	else if index != -1 {
		list.tags = append(list.tags[0:index], list.tags[index+1:]...)
	}*/
}

func (list *RoomList) AddToTag(tag rooms.RoomTag, room *rooms.Room) {
	if tag.Tag == "" && len(room.GetMembers()) == 2 {
		tag.Tag = "net.maunium.gomuks.fake.direct"
	}

	tagRoomList, ok := list.items[tag.Tag]
	if !ok {
		list.items[tag.Tag] = newTagRoomList(convertRoom(room))
		return
	}

	tagRoomList.Insert(tag.Order, room)
	list.CheckTag(tag.Tag)
}

func (list *RoomList) Remove(room *rooms.Room) {
	for _, tag := range room.Tags() {
		list.RemoveFromTag(tag.Tag, room)
	}
}

func (list *RoomList) RemoveFromTag(tag string, room *rooms.Room) {
	tagRoomList, ok := list.items[tag]
	if !ok {
		return
	}

	index := tagRoomList.Index(room)
	if index == -1 {
		return
	}

	tagRoomList.RemoveIndex(index)

	if tagRoomList.IsEmpty() {
		delete(list.items, tag)
	}

	if room == list.selected {
		if index > 0 {
			list.selected = tagRoomList.All()[index-1].Room
		} else if tagRoomList.Length() > 0 {
			list.selected = tagRoomList.Visible()[0].Room
		} else if len(list.items) > 0 {
			for _, tag := range list.tags {
				moreItems := list.items[tag]
				if moreItems.Length() > 0 {
					list.selected = moreItems.Visible()[0].Room
					list.selectedTag = tag
				}
			}
		} else {
			list.selected = nil
			list.selectedTag = ""
		}
	}
	list.CheckTag(tag)
}

func (list *RoomList) Bump(room *rooms.Room) {
	for _, tag := range room.Tags() {
		tagRoomList, ok := list.items[tag.Tag]
		if !ok {
			return
		}
		tagRoomList.Bump(room)
	}
}

func (list *RoomList) Clear() {
	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()
	}
	list.selected = nil
	list.selectedTag = ""
}

func (list *RoomList) SetSelected(tag string, room *rooms.Room) {
	list.selected = room
	list.selectedTag = tag
	pos := list.index(tag, room)
	_, _, _, height := list.GetRect()
	if pos <= list.scrollOffset {
		list.scrollOffset = pos - 1
	} else if pos >= list.scrollOffset+height {
		list.scrollOffset = pos - height + 1
	}
	if list.scrollOffset < 0 {
		list.scrollOffset = 0
	}
	debug.Print("Selecting", room.GetTitle(), "in", list.GetTagDisplayName(tag))
}

func (list *RoomList) HasSelected() bool {
	return list.selected != nil
}

func (list *RoomList) Selected() (string, *rooms.Room) {
	return list.selectedTag, list.selected
}

func (list *RoomList) SelectedRoom() *rooms.Room {
	return list.selected
}

func (list *RoomList) AddScrollOffset(offset int) {
	list.scrollOffset += offset
	_, _, _, viewHeight := list.GetRect()
	contentHeight := list.ContentHeight()
	if list.scrollOffset > contentHeight-viewHeight {
		list.scrollOffset = contentHeight - viewHeight
	}
	if list.scrollOffset < 0 {
		list.scrollOffset = 0
	}
}

func (list *RoomList) First() (string, *rooms.Room) {
	for _, tag := range list.tags {
		tagRoomList := list.items[tag]
		if tagRoomList.HasVisibleRooms() {
			return tag, tagRoomList.FirstVisible()
		}
	}
	return "", nil
}

func (list *RoomList) Last() (string, *rooms.Room) {
	for tagIndex := len(list.tags) - 1; tagIndex >= 0; tagIndex-- {
		tag := list.tags[tagIndex]
		tagRoomList := list.items[tag]
		if tagRoomList.HasVisibleRooms() {
			return tag, tagRoomList.LastVisible()
		}
	}
	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) {
	if len(list.items) == 0 {
		return "", nil
	} else if list.selected == nil {
		return list.First()
	}

	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) {
		tagIndex := list.IndexTag(list.selectedTag)
		tagIndex--
		for ; tagIndex >= 0; tagIndex-- {
			prevTag := list.tags[tagIndex]
			prevTagRoomList := list.items[prevTag]
			if prevTagRoomList.HasVisibleRooms() {
				return prevTag, prevTagRoomList.LastVisible()
			}
		}
		return list.Last()
	}
	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()
}

func (list *RoomList) Next() (string, *rooms.Room) {
	if len(list.items) == 0 {
		return "", nil
	} else if list.selected == nil {
		return list.First()
	}

	tagRoomList := list.items[list.selectedTag]
	indexVisible := tagRoomList.IndexVisible(list.selected)
	index := tagRoomList.Index(list.selected)

	if indexVisible == 0 || (indexVisible == -1 && index == 0) {
		tagIndex := list.IndexTag(list.selectedTag)
		tagIndex++
		for ; tagIndex < len(list.tags); tagIndex++ {
			nextTag := list.tags[tagIndex]
			nextTagRoomList := list.items[nextTag]
			if nextTagRoomList.HasVisibleRooms() {
				return nextTag, nextTagRoomList.FirstVisible()
			}
		}
		return list.First()
	}
	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.Last()
}

func (list *RoomList) index(tag string, room *rooms.Room) int {
	tagIndex := list.IndexTag(tag)
	if tagIndex == -1 {
		return -1
	}

	tagRoomList, ok := list.items[tag]
	localIndex := -1
	if ok {
		localIndex = tagRoomList.IndexVisible(room)
	}
	if localIndex == -1 {
		return -1
	}
	localIndex = tagRoomList.Length() - 1 - localIndex

	// Tag header
	localIndex += 1

	if tagIndex > 0 {
		for i := 0; i < tagIndex; i++ {
			previousTag := list.tags[i]
			previousTagRoomList := list.items[previousTag]

			tagDisplayName := list.GetTagDisplayName(previousTag)
			if len(tagDisplayName) > 0 {
				if previousTagRoomList.IsCollapsed() {
					localIndex++
					continue
				}
				// Previous tag header + space
				localIndex += 2
				if previousTagRoomList.HasInvisibleRooms() {
					// Previous tag "Show more" button
					localIndex++
				}
				// Previous tag items
				localIndex += previousTagRoomList.Length()
			}
		}
	}

	return localIndex
}

func (list *RoomList) ContentHeight() (height int) {
	for _, tag := range list.tags {
		tagRoomList := list.items[tag]
		tagDisplayName := list.GetTagDisplayName(tag)
		if len(tagDisplayName) == 0 {
			continue
		}
		if tagRoomList.IsCollapsed() {
			height++
			continue
		}
		height += 2 + tagRoomList.Length()
		if tagRoomList.HasInvisibleRooms() {
			height++
		}
	}
	return
}

func (list *RoomList) HandleClick(column, line int, mod bool) (string, *rooms.Room) {
	line += list.scrollOffset
	if line < 0 {
		return "", nil
	}
	for _, tag := range list.tags {
		tagRoomList := list.items[tag]
		if line--; line == -1 {
			tagRoomList.ToggleCollapse()
			return "", nil
		}

		if tagRoomList.IsCollapsed() {
			continue
		}

		if line < 0 {
			return "", nil
		} else if line < tagRoomList.Length() {
			return tag, tagRoomList.Visible()[tagRoomList.Length()-1-line].Room
		}

		// Tag items
		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
			}
		}
		// Tag footer
		line--
	}
	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"
	case tag == "net.maunium.gomuks.fake.direct":
		return "People"
	case strings.HasPrefix(tag, "u."):
		return tag[len("u."):]
	case !nsRegex.MatchString(tag):
		return tag
	default:
		return ""
	}
}

// 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

	handledOffset := 0

	// Draw the list items.
	for _, tag := range list.tags {
		tagRoomList := list.items[tag]
		tagDisplayName := list.GetTagDisplayName(tag)
		if len(tagDisplayName) == 0 {
			continue
		}

		localOffset := 0

		if handledOffset < list.scrollOffset {
			if handledOffset+tagRoomList.Length() < list.scrollOffset {
				if tagRoomList.IsCollapsed() {
					handledOffset++
				} else {
					handledOffset += tagRoomList.Length() + 2
					if tagRoomList.HasInvisibleRooms() || tagRoomList.maxShown > 10 {
						handledOffset++
					}
				}
				continue
			} else {
				localOffset = list.scrollOffset - handledOffset
				handledOffset += localOffset
			}
		}

		widget.WriteLine(screen, tview.AlignLeft, tagDisplayName, x, y, width, tcell.StyleDefault.Underline(true).Bold(true))

		items := tagRoomList.Visible()

		if tagRoomList.IsCollapsed() {
			screen.SetCell(x+width-1, y, tcell.StyleDefault, '▶')
			y++
			continue
		}
		screen.SetCell(x+width-1, y, tcell.StyleDefault, '▼')
		y++

		for i := tagRoomList.Length() - 1; i >= 0; i-- {
			item := items[i]
			index := len(items) - 1 - i

			if y >= bottomLimit {
				break
			}

			if index < localOffset {
				continue
			}

			text := item.GetTitle()

			lineWidth := width

			style := tcell.StyleDefault.Foreground(list.mainTextColor)
			if tag == list.selectedTag && item.Room == list.selected {
				style = style.Foreground(list.selectedTextColor).Background(list.selectedBackgroundColor)
			}
			if item.HasNewMessages {
				style = style.Bold(true)
			}

			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
			}

			widget.WriteLinePadded(screen, tview.AlignLeft, text, x, y, lineWidth, style)
			y++

			if y >= bottomLimit {
				break
			}
		}
		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++
		}

		y++
	}
}