2018-03-13 20:58:43 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-03-13 20:58:43 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-03-13 20:58:43 +01:00
|
|
|
// 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
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-03-13 20:58:43 +01:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// 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/>.
|
2018-03-13 20:58:43 +01:00
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
package ui
|
2018-03-13 20:58:43 +01:00
|
|
|
|
|
|
|
import (
|
2019-01-17 13:13:25 +01:00
|
|
|
"bufio"
|
2018-03-17 13:32:01 +01:00
|
|
|
"fmt"
|
2019-01-17 13:13:25 +01:00
|
|
|
"os"
|
2019-04-27 14:02:21 +02:00
|
|
|
"sync/atomic"
|
2018-03-15 20:38:43 +01:00
|
|
|
"time"
|
2018-03-17 13:32:01 +01:00
|
|
|
"unicode"
|
2018-03-13 20:58:43 +01:00
|
|
|
|
2019-04-27 14:02:52 +02:00
|
|
|
sync "github.com/sasha-s/go-deadlock"
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
"maunium.net/go/gomuks/ui/messages"
|
2020-04-16 18:27:35 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
2019-03-25 23:37:35 +01:00
|
|
|
"maunium.net/go/mauview"
|
2019-01-17 13:13:25 +01:00
|
|
|
"maunium.net/go/tcell"
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
"maunium.net/go/gomuks/config"
|
2018-04-09 22:45:54 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
2018-03-18 20:24:03 +01:00
|
|
|
"maunium.net/go/gomuks/interface"
|
2018-04-11 14:05:42 +02:00
|
|
|
"maunium.net/go/gomuks/lib/notification"
|
2018-03-26 16:22:47 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
2018-03-18 20:24:03 +01:00
|
|
|
"maunium.net/go/gomuks/ui/widget"
|
2020-04-16 18:27:35 +02:00
|
|
|
"maunium.net/go/mautrix/pushrules"
|
2018-03-13 20:58:43 +01:00
|
|
|
)
|
|
|
|
|
2018-03-15 19:53:04 +01:00
|
|
|
type MainView struct {
|
2019-03-25 23:37:35 +01:00
|
|
|
flex *mauview.Flex
|
2018-03-15 19:53:04 +01:00
|
|
|
|
2018-05-27 13:54:07 +02:00
|
|
|
roomList *RoomList
|
2019-03-25 23:37:35 +01:00
|
|
|
roomView *mauview.Box
|
|
|
|
currentRoom *RoomView
|
2020-04-16 18:27:35 +02:00
|
|
|
rooms map[id.RoomID]*RoomView
|
2019-04-27 14:02:21 +02:00
|
|
|
roomsLock sync.RWMutex
|
2018-05-27 13:54:07 +02:00
|
|
|
cmdProcessor *CommandProcessor
|
2019-03-25 23:37:35 +01:00
|
|
|
focused mauview.Focusable
|
|
|
|
|
|
|
|
modal mauview.Component
|
2018-03-15 19:53:04 +01:00
|
|
|
|
2018-03-24 21:14:17 +01:00
|
|
|
lastFocusTime time.Time
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
matrix ifc.MatrixContainer
|
|
|
|
gmx ifc.Gomuks
|
|
|
|
config *config.Config
|
2018-03-15 19:53:04 +01:00
|
|
|
parent *GomuksUI
|
|
|
|
}
|
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
func (ui *GomuksUI) NewMainView() mauview.Component {
|
2018-03-15 20:28:21 +01:00
|
|
|
mainView := &MainView{
|
2019-03-25 23:37:35 +01:00
|
|
|
flex: mauview.NewFlex().SetDirection(mauview.FlexColumn),
|
|
|
|
roomView: mauview.NewBox(nil).SetBorder(false),
|
2020-04-16 18:27:35 +02:00
|
|
|
rooms: make(map[id.RoomID]*RoomView),
|
2018-03-15 19:53:04 +01:00
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
matrix: ui.gmx.Matrix(),
|
2018-03-15 19:53:04 +01:00
|
|
|
gmx: ui.gmx,
|
2018-03-18 20:24:03 +01:00
|
|
|
config: ui.gmx.Config(),
|
2018-03-15 19:53:04 +01:00
|
|
|
parent: ui,
|
|
|
|
}
|
2019-03-25 23:37:35 +01:00
|
|
|
mainView.roomList = NewRoomList(mainView)
|
2018-05-27 13:54:07 +02:00
|
|
|
mainView.cmdProcessor = NewCommandProcessor(mainView)
|
2018-03-15 19:53:04 +01:00
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
mainView.flex.
|
|
|
|
AddFixedComponent(mainView.roomList, 25).
|
|
|
|
AddFixedComponent(widget.NewBorder(), 1).
|
|
|
|
AddProportionalComponent(mainView.roomView, 1)
|
2018-05-16 19:09:09 +02:00
|
|
|
mainView.BumpFocus(nil)
|
2018-03-13 20:58:43 +01:00
|
|
|
|
2018-03-15 20:28:21 +01:00
|
|
|
ui.mainView = mainView
|
|
|
|
|
|
|
|
return mainView
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
func (view *MainView) ShowModal(modal mauview.Component) {
|
|
|
|
view.modal = modal
|
|
|
|
var ok bool
|
|
|
|
view.focused, ok = modal.(mauview.Focusable)
|
|
|
|
if !ok {
|
|
|
|
view.focused = nil
|
2019-03-28 22:28:27 +01:00
|
|
|
} else {
|
|
|
|
view.focused.Focus()
|
2019-03-25 23:37:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) HideModal() {
|
|
|
|
view.modal = nil
|
|
|
|
view.focused = view.roomView
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) Draw(screen mauview.Screen) {
|
2018-06-01 23:43:56 +02:00
|
|
|
if view.config.Preferences.HideRoomList {
|
2018-05-22 21:06:48 +02:00
|
|
|
view.roomView.Draw(screen)
|
|
|
|
} else {
|
2019-03-25 23:37:35 +01:00
|
|
|
view.flex.Draw(screen)
|
|
|
|
}
|
|
|
|
|
|
|
|
if view.modal != nil {
|
|
|
|
view.modal.Draw(screen)
|
2018-05-22 21:06:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 19:09:09 +02:00
|
|
|
func (view *MainView) BumpFocus(roomView *RoomView) {
|
2019-03-25 23:37:35 +01:00
|
|
|
if roomView != nil {
|
|
|
|
view.lastFocusTime = time.Now()
|
|
|
|
view.MarkRead(roomView)
|
|
|
|
}
|
2018-05-16 19:09:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) MarkRead(roomView *RoomView) {
|
|
|
|
if roomView != nil && roomView.Room.HasNewMessages() && roomView.MessageView().ScrollOffset == 0 {
|
|
|
|
msgList := roomView.MessageView().messages
|
2019-04-05 22:44:17 +02:00
|
|
|
if len(msgList) > 0 {
|
|
|
|
msg := msgList[len(msgList)-1]
|
|
|
|
if roomView.Room.MarkRead(msg.ID()) {
|
|
|
|
view.matrix.MarkRead(roomView.Room.ID, msg.ID())
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 19:09:09 +02:00
|
|
|
}
|
2018-03-24 21:14:17 +01:00
|
|
|
}
|
|
|
|
|
2018-04-09 22:45:54 +02:00
|
|
|
func (view *MainView) InputChanged(roomView *RoomView, text string) {
|
2018-06-30 22:26:40 +02:00
|
|
|
if !roomView.config.Preferences.DisableTypingNotifs {
|
2020-03-03 20:15:25 +01:00
|
|
|
view.matrix.SendTyping(roomView.Room.ID, len(text) > 0 && text[0] != '/')
|
2018-03-15 20:28:21 +01:00
|
|
|
}
|
2018-03-15 19:53:04 +01:00
|
|
|
}
|
|
|
|
|
2018-03-17 13:32:01 +01:00
|
|
|
func findWordToTabComplete(text string) string {
|
|
|
|
output := ""
|
|
|
|
runes := []rune(text)
|
|
|
|
for i := len(runes) - 1; i >= 0; i-- {
|
|
|
|
if unicode.IsSpace(runes[i]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
output = string(runes[i]) + output
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2018-05-22 23:44:29 +02:00
|
|
|
func (view *MainView) ShowBare(roomView *RoomView) {
|
2019-03-25 23:37:35 +01:00
|
|
|
if roomView == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, height := view.parent.app.Screen().Size()
|
2018-05-22 23:44:29 +02:00
|
|
|
view.parent.app.Suspend(func() {
|
|
|
|
print("\033[2J\033[0;0H")
|
|
|
|
// We don't know how much space there exactly is. Too few messages looks weird,
|
|
|
|
// and too many messages shouldn't cause any problems, so we just show too many.
|
|
|
|
height *= 2
|
|
|
|
fmt.Println(roomView.MessageView().CapturePlaintext(height))
|
|
|
|
fmt.Println("Press enter to return to normal mode.")
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
2019-04-10 00:04:39 +02:00
|
|
|
_, _, _ = reader.ReadRune()
|
2018-05-22 23:44:29 +02:00
|
|
|
print("\033[2J\033[0;0H")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-19 17:06:45 +02:00
|
|
|
func (view *MainView) OpenSyncingModal() ifc.SyncingModal {
|
|
|
|
component, modal := NewSyncingModal(view)
|
|
|
|
view.ShowModal(component)
|
|
|
|
return modal
|
|
|
|
}
|
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
func (view *MainView) OnKeyEvent(event mauview.KeyEvent) bool {
|
|
|
|
view.BumpFocus(view.currentRoom)
|
|
|
|
|
|
|
|
if view.modal != nil {
|
|
|
|
return view.modal.OnKeyEvent(event)
|
|
|
|
}
|
2018-03-24 21:14:17 +01:00
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
k := event.Key()
|
|
|
|
c := event.Rune()
|
|
|
|
if event.Modifiers() == tcell.ModCtrl || event.Modifiers() == tcell.ModAlt {
|
2018-05-22 21:06:48 +02:00
|
|
|
switch {
|
|
|
|
case k == tcell.KeyDown:
|
2018-04-23 21:49:17 +02:00
|
|
|
view.SwitchRoom(view.roomList.Next())
|
2018-05-22 21:06:48 +02:00
|
|
|
case k == tcell.KeyUp:
|
2018-04-23 21:49:17 +02:00
|
|
|
view.SwitchRoom(view.roomList.Previous())
|
2020-04-03 22:46:50 +02:00
|
|
|
case c == 'k' || k == tcell.KeyCtrlK:
|
2019-03-25 23:37:35 +01:00
|
|
|
view.ShowModal(NewFuzzySearchModal(view, 42, 12))
|
|
|
|
case k == tcell.KeyHome:
|
|
|
|
msgView := view.currentRoom.MessageView()
|
|
|
|
msgView.AddScrollOffset(msgView.TotalHeight())
|
|
|
|
case k == tcell.KeyEnd:
|
|
|
|
msgView := view.currentRoom.MessageView()
|
|
|
|
msgView.AddScrollOffset(-msgView.TotalHeight())
|
2020-04-03 22:46:50 +02:00
|
|
|
case k == tcell.KeyEnter:
|
2020-02-18 19:38:35 +01:00
|
|
|
return view.flex.OnKeyEvent(tcell.NewEventKey(tcell.KeyEnter, '\n', event.Modifiers()|tcell.ModShift, ""))
|
2018-10-18 16:02:38 +02:00
|
|
|
case c == 'a':
|
|
|
|
view.SwitchRoom(view.roomList.NextWithActivity())
|
2019-03-30 17:51:26 +01:00
|
|
|
case c == 'l' || k == tcell.KeyCtrlL:
|
2019-03-25 23:37:35 +01:00
|
|
|
view.ShowBare(view.currentRoom)
|
2018-03-25 13:21:59 +02:00
|
|
|
default:
|
2019-03-25 23:37:35 +01:00
|
|
|
goto defaultHandler
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2019-03-25 23:37:35 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
defaultHandler:
|
|
|
|
if view.config.Preferences.HideRoomList {
|
|
|
|
return view.roomView.OnKeyEvent(event)
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2019-03-25 23:37:35 +01:00
|
|
|
return view.flex.OnKeyEvent(event)
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-25 11:35:50 +02:00
|
|
|
const WheelScrollOffsetDiff = 3
|
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
func (view *MainView) OnMouseEvent(event mauview.MouseEvent) bool {
|
2019-03-28 22:28:27 +01:00
|
|
|
if view.modal != nil {
|
|
|
|
return view.modal.OnMouseEvent(event)
|
|
|
|
}
|
2019-03-25 23:37:35 +01:00
|
|
|
if view.config.Preferences.HideRoomList {
|
|
|
|
return view.roomView.OnMouseEvent(event)
|
2018-03-25 11:35:50 +02:00
|
|
|
}
|
2019-03-25 23:37:35 +01:00
|
|
|
return view.flex.OnMouseEvent(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) OnPasteEvent(event mauview.PasteEvent) bool {
|
|
|
|
if view.modal != nil {
|
|
|
|
return view.modal.OnPasteEvent(event)
|
|
|
|
} else if view.config.Preferences.HideRoomList {
|
|
|
|
return view.roomView.OnPasteEvent(event)
|
|
|
|
}
|
|
|
|
return view.flex.OnPasteEvent(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) Focus() {
|
|
|
|
if view.focused != nil {
|
|
|
|
view.focused.Focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) Blur() {
|
|
|
|
if view.focused != nil {
|
|
|
|
view.focused.Blur()
|
|
|
|
}
|
2018-03-24 21:14:17 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
func (view *MainView) SwitchRoom(tag string, room *rooms.Room) {
|
2019-04-27 14:02:21 +02:00
|
|
|
view.switchRoom(tag, room, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) switchRoom(tag string, room *rooms.Room, lock bool) {
|
2018-04-23 22:22:18 +02:00
|
|
|
if room == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
room.Load()
|
2018-04-23 22:22:18 +02:00
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
roomView, ok := view.getRoomView(room.ID, lock)
|
|
|
|
if !ok {
|
|
|
|
debug.Print("Tried to switch to room with nonexistent roomView!")
|
2018-05-16 19:09:09 +02:00
|
|
|
debug.Print(tag, room)
|
|
|
|
return
|
2018-03-26 16:22:47 +02:00
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
roomView.Update()
|
2019-03-25 23:37:35 +01:00
|
|
|
view.roomView.SetInnerComponent(roomView)
|
|
|
|
view.currentRoom = roomView
|
2018-05-16 19:09:09 +02:00
|
|
|
view.MarkRead(roomView)
|
2018-04-24 01:13:17 +02:00
|
|
|
view.roomList.SetSelected(tag, room)
|
2019-06-16 15:52:19 +02:00
|
|
|
view.flex.SetFocused(view.roomView)
|
|
|
|
view.focused = view.roomView
|
|
|
|
view.roomView.Focus()
|
2018-03-15 19:53:04 +01:00
|
|
|
view.parent.Render()
|
2019-06-15 00:11:51 +02:00
|
|
|
|
|
|
|
if msgView := roomView.MessageView(); len(msgView.messages) < 20 && !msgView.initialHistoryLoaded {
|
|
|
|
msgView.initialHistoryLoaded = true
|
2019-04-05 22:44:17 +02:00
|
|
|
go view.LoadHistory(room.ID)
|
2018-03-22 18:51:20 +01:00
|
|
|
}
|
2020-02-21 23:03:57 +01:00
|
|
|
if !room.MembersFetched {
|
|
|
|
go func() {
|
|
|
|
err := view.matrix.FetchMembers(room)
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Error fetching members:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
roomView.UpdateUserList()
|
|
|
|
view.parent.Render()
|
|
|
|
}()
|
|
|
|
}
|
2018-03-22 18:51:20 +01:00
|
|
|
}
|
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
func (view *MainView) addRoomPage(room *rooms.Room) *RoomView {
|
2019-03-25 23:37:35 +01:00
|
|
|
if _, ok := view.rooms[room.ID]; !ok {
|
2018-04-23 21:49:17 +02:00
|
|
|
roomView := NewRoomView(view, room).
|
2019-03-25 23:37:35 +01:00
|
|
|
SetInputChangedFunc(view.InputChanged)
|
2018-04-23 21:49:17 +02:00
|
|
|
view.rooms[room.ID] = roomView
|
2019-04-27 14:02:21 +02:00
|
|
|
return roomView
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
return nil
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (view *MainView) GetRoom(roomID id.RoomID) ifc.RoomView {
|
2019-04-27 14:02:21 +02:00
|
|
|
room, ok := view.getRoomView(roomID, true)
|
2018-04-24 01:13:17 +02:00
|
|
|
if !ok {
|
2019-06-15 00:11:51 +02:00
|
|
|
return view.addRoom(view.matrix.GetOrCreateRoom(roomID))
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
return room
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (view *MainView) getRoomView(roomID id.RoomID, lock bool) (room *RoomView, ok bool) {
|
2019-04-27 14:02:21 +02:00
|
|
|
if lock {
|
|
|
|
view.roomsLock.RLock()
|
|
|
|
room, ok = view.rooms[roomID]
|
|
|
|
view.roomsLock.RUnlock()
|
|
|
|
} else {
|
|
|
|
room, ok = view.rooms[roomID]
|
2018-04-24 21:46:16 +02:00
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
return room, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) AddRoom(room *rooms.Room) {
|
|
|
|
view.addRoom(room)
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 22:12:43 +02:00
|
|
|
func (view *MainView) RemoveRoom(room *rooms.Room) {
|
2019-04-27 14:02:21 +02:00
|
|
|
view.roomsLock.Lock()
|
|
|
|
_, ok := view.getRoomView(room.ID, false)
|
|
|
|
if !ok {
|
|
|
|
view.roomsLock.Unlock()
|
2018-05-16 19:09:09 +02:00
|
|
|
debug.Print("Remove aborted (not found)", room.ID, room.GetTitle())
|
2018-03-16 15:24:11 +01:00
|
|
|
return
|
|
|
|
}
|
2018-05-16 19:09:09 +02:00
|
|
|
debug.Print("Removing", room.ID, room.GetTitle())
|
2018-04-23 21:49:17 +02:00
|
|
|
|
2018-04-30 22:12:43 +02:00
|
|
|
view.roomList.Remove(room)
|
2019-04-27 14:02:21 +02:00
|
|
|
t, r := view.roomList.Selected()
|
|
|
|
view.switchRoom(t, r, false)
|
2018-04-30 22:12:43 +02:00
|
|
|
delete(view.rooms, room.ID)
|
2019-04-27 14:02:21 +02:00
|
|
|
view.roomsLock.Unlock()
|
2018-04-23 21:49:17 +02:00
|
|
|
|
2018-03-20 11:16:32 +01:00
|
|
|
view.parent.Render()
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
func (view *MainView) addRoom(room *rooms.Room) *RoomView {
|
|
|
|
if view.roomList.Contains(room.ID) {
|
|
|
|
debug.Print("Add aborted (room exists)", room.ID, room.GetTitle())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
debug.Print("Adding", room.ID, room.GetTitle())
|
|
|
|
view.roomList.Add(room)
|
|
|
|
view.roomsLock.Lock()
|
|
|
|
roomView := view.addRoomPage(room)
|
|
|
|
if !view.roomList.HasSelected() {
|
|
|
|
t, r := view.roomList.First()
|
|
|
|
view.switchRoom(t, r, false)
|
|
|
|
}
|
|
|
|
view.roomsLock.Unlock()
|
|
|
|
return roomView
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (view *MainView) SetRooms(rooms *rooms.RoomCache) {
|
2018-03-15 19:53:04 +01:00
|
|
|
view.roomList.Clear()
|
2019-04-27 14:02:21 +02:00
|
|
|
view.roomsLock.Lock()
|
2020-04-16 18:27:35 +02:00
|
|
|
view.rooms = make(map[id.RoomID]*RoomView)
|
2019-06-15 00:11:51 +02:00
|
|
|
for _, room := range rooms.Map {
|
2018-04-30 21:28:29 +02:00
|
|
|
if room.HasLeft {
|
|
|
|
continue
|
|
|
|
}
|
2018-04-23 21:49:17 +02:00
|
|
|
view.roomList.Add(room)
|
|
|
|
view.addRoomPage(room)
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
t, r := view.roomList.First()
|
|
|
|
view.switchRoom(t, r, false)
|
|
|
|
view.roomsLock.Unlock()
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 19:09:09 +02:00
|
|
|
func (view *MainView) UpdateTags(room *rooms.Room) {
|
|
|
|
if !view.roomList.Contains(room.ID) {
|
|
|
|
return
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2019-06-16 18:32:57 +02:00
|
|
|
reselect := view.roomList.selected == room
|
2018-05-16 19:09:09 +02:00
|
|
|
view.roomList.Remove(room)
|
|
|
|
view.roomList.Add(room)
|
2019-06-16 18:32:57 +02:00
|
|
|
if reselect {
|
|
|
|
view.roomList.SetSelected(room.Tags()[0].Tag, room)
|
|
|
|
}
|
|
|
|
view.parent.Render()
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (view *MainView) SetTyping(roomID id.RoomID, users []id.UserID) {
|
2019-04-27 14:02:21 +02:00
|
|
|
roomView, ok := view.getRoomView(roomID, true)
|
2018-03-14 23:14:39 +01:00
|
|
|
if ok {
|
2018-03-15 18:45:52 +01:00
|
|
|
roomView.SetTyping(users)
|
2018-03-15 19:53:04 +01:00
|
|
|
view.parent.Render()
|
2018-03-14 23:14:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:04:10 +02:00
|
|
|
func sendNotification(room *rooms.Room, sender, text string, critical, sound bool) {
|
2018-03-26 16:22:47 +02:00
|
|
|
if room.GetTitle() != sender {
|
|
|
|
sender = fmt.Sprintf("%s (%s)", sender, room.GetTitle())
|
|
|
|
}
|
2018-04-25 06:01:00 +02:00
|
|
|
debug.Printf("Sending notification with body \"%s\" from %s in room ID %s (critical=%v, sound=%v)", text, sender, room.ID, critical, sound)
|
2018-03-26 17:04:10 +02:00
|
|
|
notification.Send(sender, text, critical, sound)
|
2018-03-26 16:22:47 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 16:51:36 +02:00
|
|
|
func (view *MainView) Bump(room *rooms.Room) {
|
2018-05-16 20:42:07 +02:00
|
|
|
view.roomList.Bump(room)
|
2019-06-15 16:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) NotifyMessage(room *rooms.Room, message ifc.Message, should pushrules.PushActionArrayShould) {
|
|
|
|
view.Bump(room)
|
2019-06-15 00:11:51 +02:00
|
|
|
uiMsg, ok := message.(*messages.UIMessage)
|
|
|
|
if ok && uiMsg.SenderID == view.config.UserID {
|
2018-05-16 20:42:07 +02:00
|
|
|
return
|
|
|
|
}
|
2018-03-26 19:55:52 +02:00
|
|
|
// Whether or not the room where the message came is the currently shown room.
|
2018-04-24 01:13:17 +02:00
|
|
|
isCurrent := room == view.roomList.SelectedRoom()
|
2018-03-26 19:55:52 +02:00
|
|
|
// Whether or not the terminal window is focused.
|
2018-05-16 19:09:09 +02:00
|
|
|
recentlyFocused := time.Now().Add(-30 * time.Second).Before(view.lastFocusTime)
|
|
|
|
isFocused := time.Now().Add(-5 * time.Second).Before(view.lastFocusTime)
|
2018-03-26 19:55:52 +02:00
|
|
|
|
|
|
|
// Whether or not the push rules say this message should be notified about.
|
2018-05-16 20:42:07 +02:00
|
|
|
shouldNotify := should.Notify || !should.NotifySpecified
|
2018-03-26 19:55:52 +02:00
|
|
|
|
2018-05-16 19:09:09 +02:00
|
|
|
if !isCurrent || !isFocused {
|
2018-03-26 19:55:52 +02:00
|
|
|
// The message is not in the current room, show new message status in room list.
|
2018-05-16 19:09:09 +02:00
|
|
|
room.AddUnread(message.ID(), shouldNotify, should.Highlight)
|
|
|
|
} else {
|
|
|
|
view.matrix.MarkRead(room.ID, message.ID())
|
2018-03-26 16:22:47 +02:00
|
|
|
}
|
2018-03-26 19:55:52 +02:00
|
|
|
|
2020-04-16 18:42:55 +02:00
|
|
|
if shouldNotify && !recentlyFocused && !view.config.Preferences.DisableNotifications {
|
2018-03-26 19:55:52 +02:00
|
|
|
// Push rules say notify and the terminal is not focused, send desktop notification.
|
2020-03-01 11:49:32 +01:00
|
|
|
shouldPlaySound := should.PlaySound &&
|
|
|
|
should.SoundName == "default" &&
|
|
|
|
view.config.NotifySound
|
2019-04-09 17:45:41 +02:00
|
|
|
sendNotification(room, message.NotificationSenderName(), message.NotificationContent(), should.Highlight, shouldPlaySound)
|
2018-03-26 16:22:47 +02:00
|
|
|
}
|
2018-03-26 19:55:52 +02:00
|
|
|
|
2019-04-10 00:04:39 +02:00
|
|
|
// TODO this should probably happen somewhere else
|
2019-04-10 01:28:24 +02:00
|
|
|
// (actually it's probably completely broken now)
|
2019-04-10 00:04:39 +02:00
|
|
|
message.SetIsHighlight(should.Highlight)
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2018-03-17 14:48:31 +01:00
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (view *MainView) LoadHistory(roomID id.RoomID) {
|
2018-04-18 17:35:24 +02:00
|
|
|
defer debug.Recover()
|
2019-04-27 14:02:21 +02:00
|
|
|
roomView, ok := view.getRoomView(roomID, true)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-04-10 00:04:39 +02:00
|
|
|
msgView := roomView.MessageView()
|
2018-03-20 11:16:32 +01:00
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
if !atomic.CompareAndSwapInt32(&msgView.loadingMessages, 0, 1) {
|
|
|
|
// Locked
|
2018-03-20 11:16:32 +01:00
|
|
|
return
|
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
defer atomic.StoreInt32(&msgView.loadingMessages, 0)
|
2020-02-20 22:08:37 +01:00
|
|
|
// Update the "Loading more messages..." text
|
|
|
|
view.parent.Render()
|
2018-03-20 11:16:32 +01:00
|
|
|
|
2020-05-10 01:28:32 +02:00
|
|
|
history, newLoadPtr, err := view.matrix.GetHistory(roomView.Room, 50, msgView.historyLoadPtr)
|
2018-03-18 16:34:42 +01:00
|
|
|
if err != nil {
|
2018-04-09 22:45:54 +02:00
|
|
|
roomView.AddServiceMessage("Failed to fetch history")
|
2018-03-18 20:24:03 +01:00
|
|
|
debug.Print("Failed to fetch history for", roomView.Room.ID, err)
|
2020-02-20 22:08:37 +01:00
|
|
|
view.parent.Render()
|
2018-03-18 16:34:42 +01:00
|
|
|
return
|
|
|
|
}
|
2020-05-10 01:28:32 +02:00
|
|
|
//debug.Printf("Load pointer %d -> %d", msgView.historyLoadPtr, newLoadPtr)
|
|
|
|
msgView.historyLoadPtr = newLoadPtr
|
2018-03-18 16:34:42 +01:00
|
|
|
for _, evt := range history {
|
2019-06-17 12:46:02 +02:00
|
|
|
roomView.AddHistoryEvent(evt)
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2018-03-20 11:16:32 +01:00
|
|
|
view.parent.Render()
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|