aboutsummaryrefslogtreecommitdiff
path: root/ui/tag-room-list.go
blob: c965f100f26fe55bbc5789dae4e92e1322b2a9e3 (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
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package ui

import (
	"fmt"
	"strconv"
	"strings"

	"maunium.net/go/mauview"
	"maunium.net/go/tcell"

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

type OrderedRoom struct {
	*rooms.Room
	order string
}

func NewOrderedRoom(order string, room *rooms.Room) *OrderedRoom {
	return &OrderedRoom{
		Room:  room,
		order: order,
	}
}

func NewDefaultOrderedRoom(room *rooms.Room) *OrderedRoom {
	return NewOrderedRoom("0.5", room)
}

func (or *OrderedRoom) Draw(roomList *RoomList, screen mauview.Screen, x, y, lineWidth int, isSelected bool) {
	style := tcell.StyleDefault.
		Foreground(roomList.mainTextColor).
		Bold(or.HasNewMessages())
	if isSelected {
		style = style.
			Foreground(roomList.selectedTextColor).
			Background(roomList.selectedBackgroundColor)
	}

	unreadCount := or.UnreadCount()

	widget.WriteLinePadded(screen, mauview.AlignLeft, or.GetTitle(), x, y, lineWidth, style)

	if unreadCount > 0 {
		unreadMessageCount := "99+"
		if unreadCount < 100 {
			unreadMessageCount = strconv.Itoa(unreadCount)
		}
		if or.Highlighted() {
			unreadMessageCount += "!"
		}
		unreadMessageCount = fmt.Sprintf("(%s)", unreadMessageCount)
		widget.WriteLine(screen, mauview.AlignRight, unreadMessageCount, x+lineWidth-7, y, 7, style)
		lineWidth -= len(unreadMessageCount)
	}
}

type TagRoomList struct {
	mauview.NoopEventHandler
	rooms       []*OrderedRoom
	maxShown    int
	name        string
	displayname string
	parent      *RoomList
}

func NewTagRoomList(parent *RoomList, name string, rooms ...*OrderedRoom) *TagRoomList {
	return &TagRoomList{
		maxShown:    10,
		rooms:       rooms,
		name:        name,
		displayname: parent.GetTagDisplayName(name),
		parent:      parent,
	}
}

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
}

// ShouldBeBefore returns if the first room should be after the second room in the room list.
// The manual order and last received message timestamp are considered.
func (trl *TagRoomList) ShouldBeAfter(room1 *OrderedRoom, room2 *OrderedRoom) bool {
	orderComp := strings.Compare(room1.order, room2.order)
	return orderComp == 1 || (orderComp == 0 && room2.LastReceivedMessage.After(room1.LastReceivedMessage))
}

func (trl *TagRoomList) Insert(order string, mxRoom *rooms.Room) {
	room := NewOrderedRoom(order, mxRoom)
	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++ {
		if trl.ShouldBeAfter(room, trl.rooms[i]) {
			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] = room
}

func (trl *TagRoomList) Bump(mxRoom *rooms.Room) {
	var found *OrderedRoom
	for i := 0; i < len(trl.rooms); i++ {
		currentRoom := trl.rooms[i]
		if found != nil {
			if trl.ShouldBeAfter(found, trl.rooms[i]) {
				// This room should be after the room being bumped, so insert the
				// room being bumped here and return
				trl.rooms[i-1] = found
				return
			}
			// Move older rooms back in the array
			trl.rooms[i-1] = currentRoom
		} else if currentRoom.Room == mxRoom {
			found = currentRoom
		}
	}
	// If the room being bumped should be first in the list, it won't be inserted during the loop.
	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
}

var TagDisplayNameStyle = tcell.StyleDefault.Underline(true).Bold(true)
var TagRoomCountStyle = tcell.StyleDefault.Italic(true)

func (trl *TagRoomList) RenderHeight() int {
	if len(trl.displayname) == 0 {
		return 0
	}

	if trl.IsCollapsed() {
		return 1
	}
	height := 2 + trl.Length()
	if trl.HasInvisibleRooms() || trl.maxShown > 10 {
		height++
	}
	return height
}

func (trl *TagRoomList) DrawHeader(screen mauview.Screen) {
	width, _ := screen.Size()
	roomCount := strconv.Itoa(trl.TotalLength())

	// Draw tag name
	displayNameWidth := width - 1 - len(roomCount)
	widget.WriteLine(screen, mauview.AlignLeft, trl.displayname, 0, 0, displayNameWidth, TagDisplayNameStyle)

	// Draw tag room count
	roomCountX := len(trl.displayname) + 1
	roomCountWidth := width - 2 - len(trl.displayname)
	widget.WriteLine(screen, mauview.AlignLeft, roomCount, roomCountX, 0, roomCountWidth, TagRoomCountStyle)
}

func (trl *TagRoomList) Draw(screen mauview.Screen) {
	if len(trl.displayname) == 0 {
		return
	}

	trl.DrawHeader(screen)

	width, height := screen.Size()

	items := trl.Visible()

	if trl.IsCollapsed() {
		screen.SetCell(width-1, 0, tcell.StyleDefault, '▶')
		return
	}
	screen.SetCell(width-1, 0, tcell.StyleDefault, '▼')

	y := 1
	for i := trl.Length() - 1; i >= 0; i-- {
		if y >= height {
			return
		}

		item := items[i]

		lineWidth := width
		isSelected := trl.name == trl.parent.selectedTag && item.Room == trl.parent.selected
		item.Draw(trl.parent, screen, 0, y, lineWidth, isSelected)
		y++
	}
	hasLess := trl.maxShown > 10
	hasMore := trl.HasInvisibleRooms()
	if (hasLess || hasMore) && y < height {
		if hasMore {
			widget.WriteLine(screen, mauview.AlignRight, "More ↓", 0, y, width, tcell.StyleDefault)
		}
		if hasLess {
			widget.WriteLine(screen, mauview.AlignLeft, "↑ Less", 0, y, width, tcell.StyleDefault)
		}
		y++
	}
}