| 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 |
"encoding/json" |
| 21 |
"fmt" |
| 22 |
"math" |
| 23 |
"strconv" |
| 24 |
|
| 25 |
"go.mau.fi/mauview" |
| 26 |
"go.mau.fi/tcell" |
| 27 |
|
| 28 |
"maunium.net/go/gomuks/debug" |
| 29 |
|
| 30 |
"maunium.net/go/gomuks/matrix/rooms" |
| 31 |
"maunium.net/go/gomuks/ui/widget" |
| 32 |
) |
| 33 |
|
| 34 |
type OrderedRoom struct { |
| 35 |
*rooms.Room |
| 36 |
order float64 |
| 37 |
} |
| 38 |
|
| 39 |
func NewOrderedRoom(order json.Number, room *rooms.Room) *OrderedRoom { |
| 40 |
numOrder, err := order.Float64() |
| 41 |
if err != nil { |
| 42 |
numOrder = 0.5 |
| 43 |
} |
| 44 |
return &OrderedRoom{ |
| 45 |
Room: room, |
| 46 |
order: numOrder, |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func NewDefaultOrderedRoom(room *rooms.Room) *OrderedRoom { |
| 51 |
return NewOrderedRoom("0.5", room) |
| 52 |
} |
| 53 |
|
| 54 |
func (or *OrderedRoom) Draw(roomList *RoomList, screen mauview.Screen, x, y, lineWidth int, isSelected bool) { |
| 55 |
style := tcell.StyleDefault. |
| 56 |
Foreground(roomList.mainTextColor). |
| 57 |
Bold(or.HasNewMessages()) |
| 58 |
if isSelected { |
| 59 |
style = style. |
| 60 |
Foreground(roomList.selectedTextColor). |
| 61 |
Background(roomList.selectedBackgroundColor) |
| 62 |
} |
| 63 |
|
| 64 |
unreadCount := or.UnreadCount() |
| 65 |
|
| 66 |
widget.WriteLinePadded(screen, mauview.AlignLeft, or.GetTitle(), x, y, lineWidth, style) |
| 67 |
|
| 68 |
if unreadCount > 0 { |
| 69 |
unreadMessageCount := "99+" |
| 70 |
if unreadCount < 100 { |
| 71 |
unreadMessageCount = strconv.Itoa(unreadCount) |
| 72 |
} |
| 73 |
if or.Highlighted() { |
| 74 |
unreadMessageCount += "!" |
| 75 |
} |
| 76 |
unreadMessageCount = fmt.Sprintf("(%s)", unreadMessageCount) |
| 77 |
widget.WriteLine(screen, mauview.AlignRight, unreadMessageCount, x+lineWidth-7, y, 7, style) |
| 78 |
lineWidth -= len(unreadMessageCount) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
type TagRoomList struct { |
| 83 |
mauview.NoopEventHandler |
| 84 |
// The list of rooms in the list, in reverse order |
| 85 |
rooms []*OrderedRoom |
| 86 |
// Maximum number of rooms to show |
| 87 |
maxShown int |
| 88 |
// The internal name of this tag |
| 89 |
name string |
| 90 |
// The displayname of this tag |
| 91 |
displayname string |
| 92 |
// The parent RoomList instance |
| 93 |
parent *RoomList |
| 94 |
} |
| 95 |
|
| 96 |
func NewTagRoomList(parent *RoomList, name string, rooms ...*OrderedRoom) *TagRoomList { |
| 97 |
return &TagRoomList{ |
| 98 |
maxShown: 10, |
| 99 |
rooms: rooms, |
| 100 |
name: name, |
| 101 |
displayname: parent.GetTagDisplayName(name), |
| 102 |
parent: parent, |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func (trl *TagRoomList) Visible() []*OrderedRoom { |
| 107 |
return trl.rooms[len(trl.rooms)-trl.Length():] |
| 108 |
} |
| 109 |
|
| 110 |
func (trl *TagRoomList) FirstVisible() *rooms.Room { |
| 111 |
visible := trl.Visible() |
| 112 |
if len(visible) > 0 { |
| 113 |
return visible[len(visible)-1].Room |
| 114 |
} |
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
func (trl *TagRoomList) LastVisible() *rooms.Room { |
| 119 |
visible := trl.Visible() |
| 120 |
if len(visible) > 0 { |
| 121 |
return visible[0].Room |
| 122 |
} |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
func (trl *TagRoomList) All() []*OrderedRoom { |
| 127 |
return trl.rooms |
| 128 |
} |
| 129 |
|
| 130 |
func (trl *TagRoomList) Length() int { |
| 131 |
if len(trl.rooms) < trl.maxShown { |
| 132 |
return len(trl.rooms) |
| 133 |
} |
| 134 |
return trl.maxShown |
| 135 |
} |
| 136 |
|
| 137 |
func (trl *TagRoomList) TotalLength() int { |
| 138 |
return len(trl.rooms) |
| 139 |
} |
| 140 |
|
| 141 |
func (trl *TagRoomList) IsEmpty() bool { |
| 142 |
return len(trl.rooms) == 0 |
| 143 |
} |
| 144 |
|
| 145 |
func (trl *TagRoomList) IsCollapsed() bool { |
| 146 |
return trl.maxShown == 0 |
| 147 |
} |
| 148 |
|
| 149 |
func (trl *TagRoomList) ToggleCollapse() { |
| 150 |
if trl.IsCollapsed() { |
| 151 |
trl.maxShown = 10 |
| 152 |
} else { |
| 153 |
trl.maxShown = 0 |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func (trl *TagRoomList) HasInvisibleRooms() bool { |
| 158 |
return trl.maxShown < trl.TotalLength() |
| 159 |
} |
| 160 |
|
| 161 |
func (trl *TagRoomList) HasVisibleRooms() bool { |
| 162 |
return !trl.IsEmpty() && trl.maxShown > 0 |
| 163 |
} |
| 164 |
|
| 165 |
const equalityThreshold = 1e-6 |
| 166 |
|
| 167 |
func almostEqual(a, b float64) bool { |
| 168 |
return math.Abs(a-b) <= equalityThreshold |
| 169 |
} |
| 170 |
|
| 171 |
// ShouldBeAfter returns if the first room should be after the second room in the room list. |
| 172 |
// The manual order and last received message timestamp are considered. |
| 173 |
func (trl *TagRoomList) ShouldBeAfter(room1 *OrderedRoom, room2 *OrderedRoom) bool { |
| 174 |
// Lower order value = higher in list |
| 175 |
return room1.order > room2.order || |
| 176 |
// Equal order value and more recent message = higher in the list |
| 177 |
(almostEqual(room1.order, room2.order) && room2.LastReceivedMessage.After(room1.LastReceivedMessage)) |
| 178 |
} |
| 179 |
|
| 180 |
func (trl *TagRoomList) Insert(order json.Number, mxRoom *rooms.Room) { |
| 181 |
room := NewOrderedRoom(order, mxRoom) |
| 182 |
// The default insert index is the newly added slot. |
| 183 |
// That index will be used if all other rooms in the list have the same LastReceivedMessage timestamp. |
| 184 |
insertAt := len(trl.rooms) |
| 185 |
// Find the spot where the new room should be put according to the last received message timestamps. |
| 186 |
for i := 0; i < len(trl.rooms); i++ { |
| 187 |
if trl.rooms[i].Room == mxRoom { |
| 188 |
debug.Printf("Warning: tried to re-insert room %s into tag %s", mxRoom.ID, trl.name) |
| 189 |
return |
| 190 |
} else if trl.ShouldBeAfter(room, trl.rooms[i]) { |
| 191 |
insertAt = i |
| 192 |
break |
| 193 |
} |
| 194 |
} |
| 195 |
trl.rooms = append(trl.rooms, nil) |
| 196 |
copy(trl.rooms[insertAt+1:], trl.rooms[insertAt:len(trl.rooms)-1]) |
| 197 |
trl.rooms[insertAt] = room |
| 198 |
} |
| 199 |
|
| 200 |
func (trl *TagRoomList) Bump(mxRoom *rooms.Room) { |
| 201 |
var roomBeingBumped *OrderedRoom |
| 202 |
for i := 0; i < len(trl.rooms); i++ { |
| 203 |
currentIndexRoom := trl.rooms[i] |
| 204 |
if roomBeingBumped != nil { |
| 205 |
if trl.ShouldBeAfter(roomBeingBumped, currentIndexRoom) { |
| 206 |
// This room should be after the room being bumped, so insert the |
| 207 |
// room being bumped here and return |
| 208 |
trl.rooms[i-1] = roomBeingBumped |
| 209 |
return |
| 210 |
} |
| 211 |
// Move older rooms back in the array |
| 212 |
trl.rooms[i-1] = currentIndexRoom |
| 213 |
} else if currentIndexRoom.Room == mxRoom { |
| 214 |
roomBeingBumped = currentIndexRoom |
| 215 |
} |
| 216 |
} |
| 217 |
if roomBeingBumped == nil { |
| 218 |
debug.Print("Warning: couldn't find room", mxRoom.ID, mxRoom.NameCache, "to bump in tag", trl.name) |
| 219 |
return |
| 220 |
} |
| 221 |
// If the room being bumped should be first in the list, it won't be inserted during the loop. |
| 222 |
trl.rooms[len(trl.rooms)-1] = roomBeingBumped |
| 223 |
} |
| 224 |
|
| 225 |
func (trl *TagRoomList) Remove(room *rooms.Room) { |
| 226 |
trl.RemoveIndex(trl.Index(room)) |
| 227 |
} |
| 228 |
|
| 229 |
func (trl *TagRoomList) RemoveIndex(index int) { |
| 230 |
if index < 0 || index > len(trl.rooms) { |
| 231 |
return |
| 232 |
} |
| 233 |
last := len(trl.rooms) - 1 |
| 234 |
if index < last { |
| 235 |
copy(trl.rooms[index:], trl.rooms[index+1:]) |
| 236 |
} |
| 237 |
trl.rooms[last] = nil |
| 238 |
trl.rooms = trl.rooms[:last] |
| 239 |
} |
| 240 |
|
| 241 |
func (trl *TagRoomList) Index(room *rooms.Room) int { |
| 242 |
return trl.indexInList(trl.All(), room) |
| 243 |
} |
| 244 |
|
| 245 |
func (trl *TagRoomList) IndexVisible(room *rooms.Room) int { |
| 246 |
return trl.indexInList(trl.Visible(), room) |
| 247 |
} |
| 248 |
|
| 249 |
func (trl *TagRoomList) indexInList(list []*OrderedRoom, room *rooms.Room) int { |
| 250 |
for index, entry := range list { |
| 251 |
if entry.Room == room { |
| 252 |
return index |
| 253 |
} |
| 254 |
} |
| 255 |
return -1 |
| 256 |
} |
| 257 |
|
| 258 |
var TagDisplayNameStyle = tcell.StyleDefault.Underline(true).Bold(true) |
| 259 |
var TagRoomCountStyle = tcell.StyleDefault.Italic(true) |
| 260 |
|
| 261 |
func (trl *TagRoomList) RenderHeight() int { |
| 262 |
if len(trl.displayname) == 0 { |
| 263 |
return 0 |
| 264 |
} |
| 265 |
|
| 266 |
if trl.IsCollapsed() { |
| 267 |
return 1 |
| 268 |
} |
| 269 |
height := 2 + trl.Length() |
| 270 |
if trl.HasInvisibleRooms() || trl.maxShown > 10 { |
| 271 |
height++ |
| 272 |
} |
| 273 |
return height |
| 274 |
} |
| 275 |
|
| 276 |
func (trl *TagRoomList) DrawHeader(screen mauview.Screen) { |
| 277 |
width, _ := screen.Size() |
| 278 |
roomCount := strconv.Itoa(trl.TotalLength()) |
| 279 |
|
| 280 |
// Draw tag name |
| 281 |
displayNameWidth := width - 1 - len(roomCount) |
| 282 |
widget.WriteLine(screen, mauview.AlignLeft, trl.displayname, 0, 0, displayNameWidth, TagDisplayNameStyle) |
| 283 |
|
| 284 |
// Draw tag room count |
| 285 |
roomCountX := len(trl.displayname) + 1 |
| 286 |
roomCountWidth := width - 2 - len(trl.displayname) |
| 287 |
widget.WriteLine(screen, mauview.AlignLeft, roomCount, roomCountX, 0, roomCountWidth, TagRoomCountStyle) |
| 288 |
} |
| 289 |
|
| 290 |
func (trl *TagRoomList) Draw(screen mauview.Screen) { |
| 291 |
if len(trl.displayname) == 0 { |
| 292 |
return |
| 293 |
} |
| 294 |
|
| 295 |
trl.DrawHeader(screen) |
| 296 |
|
| 297 |
width, height := screen.Size() |
| 298 |
|
| 299 |
items := trl.Visible() |
| 300 |
|
| 301 |
if trl.IsCollapsed() { |
| 302 |
screen.SetCell(width-1, 0, tcell.StyleDefault, '▶') |
| 303 |
return |
| 304 |
} |
| 305 |
screen.SetCell(width-1, 0, tcell.StyleDefault, '▼') |
| 306 |
|
| 307 |
y := 1 |
| 308 |
for i := len(items) - 1; i >= 0; i-- { |
| 309 |
if y >= height { |
| 310 |
return |
| 311 |
} |
| 312 |
|
| 313 |
item := items[i] |
| 314 |
|
| 315 |
lineWidth := width |
| 316 |
isSelected := trl.name == trl.parent.selectedTag && item.Room == trl.parent.selected |
| 317 |
item.Draw(trl.parent, screen, 0, y, lineWidth, isSelected) |
| 318 |
y++ |
| 319 |
} |
| 320 |
hasLess := trl.maxShown > 10 |
| 321 |
hasMore := trl.HasInvisibleRooms() |
| 322 |
if (hasLess || hasMore) && y < height { |
| 323 |
if hasMore { |
| 324 |
widget.WriteLine(screen, mauview.AlignRight, "More ↓", 0, y, width, tcell.StyleDefault) |
| 325 |
} |
| 326 |
if hasLess { |
| 327 |
widget.WriteLine(screen, mauview.AlignLeft, "↑ Less", 0, y, width, tcell.StyleDefault) |
| 328 |
} |
| 329 |
y++ |
| 330 |
} |
| 331 |
} |