| 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 |
"bufio" |
| 21 |
"fmt" |
| 22 |
"os" |
| 23 |
"sync/atomic" |
| 24 |
"time" |
| 25 |
|
| 26 |
sync "github.com/sasha-s/go-deadlock" |
| 27 |
|
| 28 |
"go.mau.fi/mauview" |
| •29 |
"github.com/gdamore/tcell/v2" |
| 30 |
|
| 31 |
"maunium.net/go/mautrix/id" |
| 32 |
"maunium.net/go/mautrix/pushrules" |
| 33 |
|
| 34 |
"maunium.net/go/gomuks/config" |
| 35 |
"maunium.net/go/gomuks/debug" |
| 36 |
ifc "maunium.net/go/gomuks/interface" |
| 37 |
"maunium.net/go/gomuks/lib/notification" |
| 38 |
"maunium.net/go/gomuks/matrix/rooms" |
| 39 |
"maunium.net/go/gomuks/ui/messages" |
| 40 |
"maunium.net/go/gomuks/ui/widget" |
| 41 |
) |
| 42 |
|
| 43 |
type MainView struct { |
| 44 |
flex *mauview.Flex |
| 45 |
|
| 46 |
roomList *RoomList |
| 47 |
roomView *mauview.Box |
| 48 |
currentRoom *RoomView |
| 49 |
rooms map[id.RoomID]*RoomView |
| 50 |
roomsLock sync.RWMutex |
| 51 |
cmdProcessor *CommandProcessor |
| 52 |
focused mauview.Focusable |
| 53 |
|
| 54 |
modal mauview.Component |
| 55 |
|
| 56 |
lastFocusTime time.Time |
| 57 |
|
| 58 |
matrix ifc.MatrixContainer |
| 59 |
gmx ifc.Gomuks |
| 60 |
config *config.Config |
| 61 |
parent *GomuksUI |
| 62 |
} |
| 63 |
|
| 64 |
func (ui *GomuksUI) NewMainView() mauview.Component { |
| 65 |
mainView := &MainView{ |
| 66 |
flex: mauview.NewFlex().SetDirection(mauview.FlexColumn), |
| 67 |
roomView: mauview.NewBox(nil).SetBorder(false), |
| 68 |
rooms: make(map[id.RoomID]*RoomView), |
| 69 |
|
| 70 |
matrix: ui.gmx.Matrix(), |
| 71 |
gmx: ui.gmx, |
| 72 |
config: ui.gmx.Config(), |
| 73 |
parent: ui, |
| 74 |
} |
| 75 |
mainView.roomList = NewRoomList(mainView) |
| 76 |
mainView.cmdProcessor = NewCommandProcessor(mainView) |
| 77 |
|
| 78 |
mainView.flex. |
| 79 |
AddFixedComponent(mainView.roomList, 25). |
| 80 |
AddFixedComponent(widget.NewBorder(), 1). |
| 81 |
AddProportionalComponent(mainView.roomView, 1) |
| 82 |
mainView.BumpFocus(nil) |
| 83 |
|
| 84 |
ui.mainView = mainView |
| 85 |
|
| 86 |
return mainView |
| 87 |
} |
| 88 |
|
| 89 |
func (view *MainView) ShowModal(modal mauview.Component) { |
| 90 |
view.modal = modal |
| 91 |
var ok bool |
| 92 |
view.focused, ok = modal.(mauview.Focusable) |
| 93 |
if !ok { |
| 94 |
view.focused = nil |
| 95 |
} else { |
| 96 |
view.focused.Focus() |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func (view *MainView) HideModal() { |
| 101 |
view.modal = nil |
| 102 |
view.focused = view.roomView |
| 103 |
} |
| 104 |
|
| 105 |
func (view *MainView) Draw(screen mauview.Screen) { |
| 106 |
if view.config.Preferences.HideRoomList { |
| 107 |
view.roomView.Draw(screen) |
| 108 |
} else { |
| 109 |
view.flex.Draw(screen) |
| 110 |
} |
| 111 |
|
| 112 |
if view.modal != nil { |
| 113 |
view.modal.Draw(screen) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func (view *MainView) BumpFocus(roomView *RoomView) { |
| 118 |
if roomView != nil { |
| 119 |
view.lastFocusTime = time.Now() |
| 120 |
view.MarkRead(roomView) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
func (view *MainView) MarkRead(roomView *RoomView) { |
| 125 |
if roomView != nil && roomView.Room.HasNewMessages() && roomView.MessageView().ScrollOffset == 0 { |
| 126 |
msgList := roomView.MessageView().messages |
| 127 |
if len(msgList) > 0 { |
| 128 |
msg := msgList[len(msgList)-1] |
| 129 |
if roomView.Room.MarkRead(msg.ID()) { |
| 130 |
view.matrix.MarkRead(roomView.Room.ID, msg.ID()) |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func (view *MainView) InputChanged(roomView *RoomView, text string) { |
| 137 |
if !roomView.config.Preferences.DisableTypingNotifs { |
| 138 |
view.matrix.SendTyping(roomView.Room.ID, len(text) > 0 && text[0] != '/') |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func (view *MainView) ShowBare(roomView *RoomView) { |
| 143 |
if roomView == nil { |
| 144 |
return |
| 145 |
} |
| 146 |
_, height := view.parent.app.Screen().Size() |
| 147 |
view.parent.app.Suspend(func() { |
| 148 |
print("\033[2J\033[0;0H") |
| 149 |
// We don't know how much space there exactly is. Too few messages looks weird, |
| 150 |
// and too many messages shouldn't cause any problems, so we just show too many. |
| 151 |
height *= 2 |
| 152 |
fmt.Println(roomView.MessageView().CapturePlaintext(height)) |
| 153 |
fmt.Println("Press enter to return to normal mode.") |
| 154 |
reader := bufio.NewReader(os.Stdin) |
| 155 |
_, _, _ = reader.ReadRune() |
| 156 |
print("\033[2J\033[0;0H") |
| 157 |
}) |
| 158 |
} |
| 159 |
|
| 160 |
func (view *MainView) OpenSyncingModal() ifc.SyncingModal { |
| 161 |
component, modal := NewSyncingModal(view) |
| 162 |
view.ShowModal(component) |
| 163 |
return modal |
| 164 |
} |
| 165 |
|
| 166 |
func (view *MainView) OnKeyEvent(event mauview.KeyEvent) bool { |
| 167 |
view.BumpFocus(view.currentRoom) |
| 168 |
|
| 169 |
if view.modal != nil { |
| 170 |
return view.modal.OnKeyEvent(event) |
| 171 |
} |
| 172 |
|
| 173 |
kb := config.Keybind{ |
| 174 |
Key: event.Key(), |
| 175 |
Ch: event.Rune(), |
| 176 |
Mod: event.Modifiers(), |
| 177 |
} |
| 178 |
switch view.config.Keybindings.Main[kb] { |
| 179 |
case "next_room": |
| 180 |
view.SwitchRoom(view.roomList.Next()) |
| 181 |
case "prev_room": |
| 182 |
view.SwitchRoom(view.roomList.Previous()) |
| 183 |
case "search_rooms": |
| 184 |
view.ShowModal(NewFuzzySearchModal(view, 42, 12)) |
| 185 |
case "scroll_up": |
| 186 |
msgView := view.currentRoom.MessageView() |
| 187 |
msgView.AddScrollOffset(msgView.TotalHeight()) |
| 188 |
case "scroll_down": |
| 189 |
msgView := view.currentRoom.MessageView() |
| 190 |
msgView.AddScrollOffset(-msgView.TotalHeight()) |
| 191 |
case "add_newline": |
| 192 |
return view.flex.OnKeyEvent(tcell.NewEventKey(tcell.KeyEnter, '\n', event.Modifiers()|tcell.ModShift)) |
| 193 |
case "next_active_room": |
| 194 |
view.SwitchRoom(view.roomList.NextWithActivity()) |
| 195 |
case "show_bare": |
| 196 |
view.ShowBare(view.currentRoom) |
| 197 |
default: |
| 198 |
goto defaultHandler |
| 199 |
} |
| 200 |
return true |
| 201 |
defaultHandler: |
| 202 |
if view.config.Preferences.HideRoomList { |
| 203 |
return view.roomView.OnKeyEvent(event) |
| 204 |
} |
| 205 |
return view.flex.OnKeyEvent(event) |
| 206 |
} |
| 207 |
|
| 208 |
const WheelScrollOffsetDiff = 3 |
| 209 |
|
| 210 |
func (view *MainView) OnMouseEvent(event mauview.MouseEvent) bool { |
| 211 |
if view.modal != nil { |
| 212 |
return view.modal.OnMouseEvent(event) |
| 213 |
} |
| 214 |
if view.config.Preferences.HideRoomList { |
| 215 |
return view.roomView.OnMouseEvent(event) |
| 216 |
} |
| 217 |
return view.flex.OnMouseEvent(event) |
| 218 |
} |
| 219 |
|
| 220 |
func (view *MainView) OnPasteEvent(event mauview.PasteEvent) bool { |
| 221 |
if view.modal != nil { |
| 222 |
return view.modal.OnPasteEvent(event) |
| 223 |
} else if view.config.Preferences.HideRoomList { |
| 224 |
return view.roomView.OnPasteEvent(event) |
| 225 |
} |
| 226 |
return view.flex.OnPasteEvent(event) |
| 227 |
} |
| 228 |
|
| 229 |
func (view *MainView) Focus() { |
| 230 |
if view.focused != nil { |
| 231 |
view.focused.Focus() |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func (view *MainView) Blur() { |
| 236 |
if view.focused != nil { |
| 237 |
view.focused.Blur() |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func (view *MainView) SwitchRoom(tag string, room *rooms.Room) { |
| 242 |
view.switchRoom(tag, room, true) |
| 243 |
} |
| 244 |
|
| 245 |
func (view *MainView) switchRoom(tag string, room *rooms.Room, lock bool) { |
| 246 |
if room == nil { |
| 247 |
return |
| 248 |
} |
| 249 |
room.Load() |
| 250 |
|
| 251 |
roomView, ok := view.getRoomView(room.ID, lock) |
| 252 |
if !ok { |
| 253 |
debug.Print("Tried to switch to room with nonexistent roomView!") |
| 254 |
debug.Print(tag, room) |
| 255 |
return |
| 256 |
} |
| 257 |
roomView.Update() |
| 258 |
view.roomView.SetInnerComponent(roomView) |
| 259 |
view.currentRoom = roomView |
| 260 |
view.MarkRead(roomView) |
| 261 |
view.roomList.SetSelected(tag, room) |
| 262 |
view.flex.SetFocused(view.roomView) |
| 263 |
view.focused = view.roomView |
| 264 |
view.roomView.Focus() |
| 265 |
view.parent.Render() |
| 266 |
|
| 267 |
if msgView := roomView.MessageView(); len(msgView.messages) < 20 && !msgView.initialHistoryLoaded { |
| 268 |
msgView.initialHistoryLoaded = true |
| 269 |
go view.LoadHistory(room.ID) |
| 270 |
} |
| 271 |
if !room.MembersFetched { |
| 272 |
go func() { |
| 273 |
err := view.matrix.FetchMembers(room) |
| 274 |
if err != nil { |
| 275 |
debug.Print("Error fetching members:", err) |
| 276 |
return |
| 277 |
} |
| 278 |
roomView.UpdateUserList() |
| 279 |
view.parent.Render() |
| 280 |
}() |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
func (view *MainView) addRoomPage(room *rooms.Room) *RoomView { |
| 285 |
if _, ok := view.rooms[room.ID]; !ok { |
| 286 |
roomView := NewRoomView(view, room). |
| 287 |
SetInputChangedFunc(view.InputChanged) |
| 288 |
view.rooms[room.ID] = roomView |
| 289 |
return roomView |
| 290 |
} |
| 291 |
return nil |
| 292 |
} |
| 293 |
|
| 294 |
func (view *MainView) GetRoom(roomID id.RoomID) ifc.RoomView { |
| 295 |
room, ok := view.getRoomView(roomID, true) |
| 296 |
if !ok { |
| 297 |
return view.addRoom(view.matrix.GetOrCreateRoom(roomID)) |
| 298 |
} |
| 299 |
return room |
| 300 |
} |
| 301 |
|
| 302 |
func (view *MainView) getRoomView(roomID id.RoomID, lock bool) (room *RoomView, ok bool) { |
| 303 |
if lock { |
| 304 |
view.roomsLock.RLock() |
| 305 |
room, ok = view.rooms[roomID] |
| 306 |
view.roomsLock.RUnlock() |
| 307 |
} else { |
| 308 |
room, ok = view.rooms[roomID] |
| 309 |
} |
| 310 |
return room, ok |
| 311 |
} |
| 312 |
|
| 313 |
func (view *MainView) AddRoom(room *rooms.Room) { |
| 314 |
view.addRoom(room) |
| 315 |
} |
| 316 |
|
| 317 |
func (view *MainView) RemoveRoom(room *rooms.Room) { |
| 318 |
view.roomsLock.Lock() |
| 319 |
_, ok := view.getRoomView(room.ID, false) |
| 320 |
if !ok { |
| 321 |
view.roomsLock.Unlock() |
| 322 |
debug.Print("Remove aborted (not found)", room.ID, room.GetTitle()) |
| 323 |
return |
| 324 |
} |
| 325 |
debug.Print("Removing", room.ID, room.GetTitle()) |
| 326 |
|
| 327 |
view.roomList.Remove(room) |
| 328 |
t, r := view.roomList.Selected() |
| 329 |
view.switchRoom(t, r, false) |
| 330 |
delete(view.rooms, room.ID) |
| 331 |
view.roomsLock.Unlock() |
| 332 |
|
| 333 |
view.parent.Render() |
| 334 |
} |
| 335 |
|
| 336 |
func (view *MainView) addRoom(room *rooms.Room) *RoomView { |
| 337 |
if view.roomList.Contains(room.ID) { |
| 338 |
debug.Print("Add aborted (room exists)", room.ID, room.GetTitle()) |
| 339 |
return nil |
| 340 |
} |
| 341 |
debug.Print("Adding", room.ID, room.GetTitle()) |
| 342 |
view.roomList.Add(room) |
| 343 |
view.roomsLock.Lock() |
| 344 |
roomView := view.addRoomPage(room) |
| 345 |
if !view.roomList.HasSelected() { |
| 346 |
t, r := view.roomList.First() |
| 347 |
view.switchRoom(t, r, false) |
| 348 |
} |
| 349 |
view.roomsLock.Unlock() |
| 350 |
return roomView |
| 351 |
} |
| 352 |
|
| 353 |
func (view *MainView) SetRooms(rooms *rooms.RoomCache) { |
| 354 |
view.roomList.Clear() |
| 355 |
view.roomsLock.Lock() |
| 356 |
view.rooms = make(map[id.RoomID]*RoomView) |
| 357 |
for _, room := range rooms.Map { |
| 358 |
if room.HasLeft { |
| 359 |
continue |
| 360 |
} |
| 361 |
view.roomList.Add(room) |
| 362 |
view.addRoomPage(room) |
| 363 |
} |
| 364 |
t, r := view.roomList.First() |
| 365 |
view.switchRoom(t, r, false) |
| 366 |
view.roomsLock.Unlock() |
| 367 |
} |
| 368 |
|
| 369 |
func (view *MainView) UpdateTags(room *rooms.Room) { |
| 370 |
if !view.roomList.Contains(room.ID) { |
| 371 |
return |
| 372 |
} |
| 373 |
reselect := view.roomList.selected == room |
| 374 |
view.roomList.Remove(room) |
| 375 |
view.roomList.Add(room) |
| 376 |
if reselect { |
| 377 |
view.roomList.SetSelected(room.Tags()[0].Tag, room) |
| 378 |
} |
| 379 |
view.parent.Render() |
| 380 |
} |
| 381 |
|
| 382 |
func (view *MainView) SetTyping(roomID id.RoomID, users []id.UserID) { |
| 383 |
roomView, ok := view.getRoomView(roomID, true) |
| 384 |
if ok { |
| 385 |
roomView.SetTyping(users) |
| 386 |
view.parent.Render() |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
func sendNotification(room *rooms.Room, sender, text string, critical, sound bool) { |
| 391 |
if room.GetTitle() != sender { |
| 392 |
sender = fmt.Sprintf("%s (%s)", sender, room.GetTitle()) |
| 393 |
} |
| 394 |
debug.Printf("Sending notification with body \"%s\" from %s in room ID %s (critical=%v, sound=%v)", text, sender, room.ID, critical, sound) |
| 395 |
notification.Send(sender, text, critical, sound) |
| 396 |
} |
| 397 |
|
| 398 |
func (view *MainView) Bump(room *rooms.Room) { |
| 399 |
view.roomList.Bump(room) |
| 400 |
} |
| 401 |
|
| 402 |
func (view *MainView) NotifyMessage(room *rooms.Room, message ifc.Message, should pushrules.PushActionArrayShould) { |
| 403 |
view.Bump(room) |
| 404 |
uiMsg, ok := message.(*messages.UIMessage) |
| 405 |
if ok && uiMsg.SenderID == view.config.UserID { |
| 406 |
return |
| 407 |
} |
| 408 |
// Whether or not the room where the message came is the currently shown room. |
| 409 |
isCurrent := room == view.roomList.SelectedRoom() |
| 410 |
// Whether or not the terminal window is focused. |
| 411 |
recentlyFocused := time.Now().Add(-30 * time.Second).Before(view.lastFocusTime) |
| 412 |
isFocused := time.Now().Add(-5 * time.Second).Before(view.lastFocusTime) |
| 413 |
|
| 414 |
if !isCurrent || !isFocused { |
| 415 |
// The message is not in the current room, show new message status in room list. |
| 416 |
room.AddUnread(message.ID(), should.Notify, should.Highlight) |
| 417 |
} else { |
| 418 |
view.matrix.MarkRead(room.ID, message.ID()) |
| 419 |
} |
| 420 |
|
| 421 |
if should.Notify && !recentlyFocused && !view.config.Preferences.DisableNotifications { |
| 422 |
// Push rules say notify and the terminal is not focused, send desktop notification. |
| 423 |
shouldPlaySound := should.PlaySound && |
| 424 |
should.SoundName == "default" && |
| 425 |
view.config.NotifySound |
| 426 |
sendNotification(room, message.NotificationSenderName(), message.NotificationContent(), should.Highlight, shouldPlaySound) |
| 427 |
} |
| 428 |
|
| 429 |
// TODO this should probably happen somewhere else |
| 430 |
// (actually it's probably completely broken now) |
| 431 |
message.SetIsHighlight(should.Highlight) |
| 432 |
} |
| 433 |
|
| 434 |
func (view *MainView) LoadHistory(roomID id.RoomID) { |
| 435 |
defer debug.Recover() |
| 436 |
roomView, ok := view.getRoomView(roomID, true) |
| 437 |
if !ok { |
| 438 |
return |
| 439 |
} |
| 440 |
msgView := roomView.MessageView() |
| 441 |
|
| 442 |
if !atomic.CompareAndSwapInt32(&msgView.loadingMessages, 0, 1) { |
| 443 |
// Locked |
| 444 |
return |
| 445 |
} |
| 446 |
defer atomic.StoreInt32(&msgView.loadingMessages, 0) |
| 447 |
// Update the "Loading more messages..." text |
| 448 |
view.parent.Render() |
| 449 |
|
| 450 |
history, newLoadPtr, err := view.matrix.GetHistory(roomView.Room, 50, msgView.historyLoadPtr) |
| 451 |
if err != nil { |
| 452 |
roomView.AddServiceMessage("Failed to fetch history") |
| 453 |
debug.Print("Failed to fetch history for", roomView.Room.ID, err) |
| 454 |
view.parent.Render() |
| 455 |
return |
| 456 |
} |
| 457 |
//debug.Printf("Load pointer %d -> %d", msgView.historyLoadPtr, newLoadPtr) |
| 458 |
msgView.historyLoadPtr = newLoadPtr |
| 459 |
for _, evt := range history { |
| 460 |
roomView.AddHistoryEvent(evt) |
| 461 |
} |
| 462 |
view.parent.Render() |
| 463 |
} |