aboutsummaryrefslogtreecommitdiff
path: root/ui/tag-room-list.go
blob: 3bea033608bac637da1bcbe4956f5a5548013ac0 (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
// 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 (
	"encoding/json"
	"fmt"
	"math"
	"strconv"

	"maunium.net/go/gomuks/debug"
	"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 float64
}

func NewOrderedRoom(order json.Number, room *rooms.Room) *OrderedRoom {
	numOrder, err := order.Float64()
	if err != nil {
		numOrder = 0.5
	}
	return &OrderedRoom{
		Room:  room,
		order: numOrder,
	}
}

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
	// The list of rooms in the list, in reverse order
	rooms []*OrderedRoom
	// Maximum number of rooms to show
	maxShown int
	// The internal name of this tag
	name string
	// The displayname of this tag
	displayname string
	// The parent RoomList instance
	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
}

const equalityThreshold = 1e-6

func almostEqual(a, b float64) bool {
	return math.Abs(a-b) <= equalityThreshold
}

// ShouldBeAfter 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 {
	// Lower order value = higher in list
	return room1.order > room2.order ||
		// Equal order value and more recent message = higher in the list
		(almostEqual(room1.order, room2.order) && room2.LastReceivedMessage.After(room1.LastReceivedMessage))
}

func (trl *TagRoomList) Insert(order json.Number, mxRoom *rooms.Room) {
	room := NewOrderedRoom(order, mxRoom)
	// 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)
	// Find the spot where the new room should be put according to the last received message timestamps.
	for i := 0; i < len(trl.rooms); i++ {
		if trl.rooms[i].Room == mxRoom {
			debug.Printf("Warning: tried to re-insert room %s into tag %s", mxRoom.ID, trl.name)
			return
		} else if trl.ShouldBeAfter(room, trl.rooms[i]) {
			insertAt = i
			break
		}
	}
	trl.rooms = append(trl.rooms, nil)
	copy(trl.rooms[insertAt+1:], trl.rooms[insertAt:len(trl.rooms)-1])
	trl.rooms[insertAt] = room
}

func (trl *TagRoomList) Bump(mxRoom *rooms.Room) {
	var roomBeingBumped *OrderedRoom
	for i := 0; i < len(trl.rooms); i++ {
		currentIndexRoom := trl.rooms[i]
		if roomBeingBumped != nil {
			if trl.ShouldBeAfter(roomBeingBumped, currentIndexRoom) {
				// This room should be after the room being bumped, so insert the
				// room being bumped here and return
				trl.rooms[i-1] = roomBeingBumped
				return
			}
			// Move older rooms back in the array
			trl.rooms[i-1] = currentIndexRoom
		} else if currentIndexRoom.Room == mxRoom {
			roomBeingBumped = currentIndexRoom
		}
	}
	if roomBeingBumped == nil {
		debug.Print("Warning: couldn't find room", mxRoom.ID, mxRoom.NameCache, "to bump in tag", trl.name)
		return
	}
	// 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] = roomBeingBumped
}

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
	}
	last := len(trl.rooms) - 1
	if index < last {
		copy(trl.rooms[index:], trl.rooms[index+1:])
	}
	trl.rooms[last] = nil
	trl.rooms = trl.rooms[:last]
}

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 := len(items) - 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++
	}
}