gomuks/view-main.go

241 lines
6.0 KiB
Go
Raw Normal View History

2018-03-13 20:58:43 +01:00
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"strings"
"time"
2018-03-13 20:58:43 +01:00
"github.com/gdamore/tcell"
2018-03-15 17:25:16 +01:00
"maunium.net/go/gomatrix"
2018-03-14 21:19:26 +01:00
"maunium.net/go/tview"
2018-03-13 20:58:43 +01:00
)
2018-03-15 19:53:04 +01:00
type MainView struct {
*tview.Grid
roomList *tview.List
roomView *tview.Pages
rooms map[string]*RoomView
input *tview.InputField
currentRoomIndex int
roomIDs []string
matrix *MatrixContainer
debug DebugPrinter
gmx Gomuks
config *Config
parent *GomuksUI
}
func (view *MainView) addItem(p tview.Primitive, x, y, w, h int) {
view.Grid.AddItem(p, x, y, w, h, 0, 0, false)
}
2018-03-15 20:28:21 +01:00
func (ui *GomuksUI) NewMainView() tview.Primitive {
mainView := &MainView{
2018-03-15 19:53:04 +01:00
Grid: tview.NewGrid(),
roomList: tview.NewList(),
roomView: tview.NewPages(),
rooms: make(map[string]*RoomView),
input: tview.NewInputField(),
matrix: ui.matrix,
debug: ui.debug,
gmx: ui.gmx,
config: ui.config,
parent: ui,
}
2018-03-15 20:28:21 +01:00
mainView.SetColumns(30, 1, 0).SetRows(0, 1)
2018-03-15 19:53:04 +01:00
2018-03-15 20:28:21 +01:00
mainView.roomList.
2018-03-15 19:53:04 +01:00
ShowSecondaryText(false).
SetBorderPadding(0, 0, 1, 0)
2018-03-13 20:58:43 +01:00
2018-03-15 20:28:21 +01:00
mainView.input.
SetDoneFunc(mainView.InputDone).
SetChangedFunc(mainView.InputChanged).
SetFieldBackgroundColor(tcell.ColorDefault).
SetInputCapture(mainView.InputCapture)
2018-03-13 20:58:43 +01:00
2018-03-15 20:28:21 +01:00
mainView.addItem(mainView.roomList, 0, 0, 2, 1)
mainView.addItem(NewBorder(), 0, 1, 2, 1)
mainView.addItem(mainView.roomView, 0, 2, 1, 1)
mainView.AddItem(mainView.input, 1, 2, 1, 1, 0, 0, true)
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
}
2018-03-15 19:53:04 +01:00
func (view *MainView) InputChanged(text string) {
2018-03-15 20:28:21 +01:00
if len(text) == 0 {
2018-03-16 15:24:11 +01:00
go view.matrix.SendTyping(view.CurrentRoomID(), false)
2018-03-15 20:28:21 +01:00
} else if text[0] != '/' {
2018-03-16 15:24:11 +01:00
go view.matrix.SendTyping(view.CurrentRoomID(), true)
2018-03-15 20:28:21 +01:00
}
2018-03-15 19:53:04 +01:00
}
func (view *MainView) InputDone(key tcell.Key) {
if key == tcell.KeyEnter {
room, text := view.CurrentRoomID(), view.input.GetText()
if len(text) == 0 {
return
} else if text[0] == '/' {
args := strings.SplitN(text, " ", 2)
command := strings.ToLower(args[0])
args = args[1:]
2018-03-16 15:24:11 +01:00
go view.HandleCommand(room, command, args)
2018-03-15 19:53:04 +01:00
} else {
2018-03-16 15:24:11 +01:00
go view.matrix.SendMessage(room, text)
2018-03-15 19:53:04 +01:00
}
view.input.SetText("")
}
}
func (view *MainView) HandleCommand(room, command string, args []string) {
2018-03-16 15:24:11 +01:00
view.gmx.Recover()
2018-03-15 19:53:04 +01:00
view.debug.Print("Handling command", command, args)
2018-03-13 20:58:43 +01:00
switch command {
2018-03-14 23:14:39 +01:00
case "/quit":
2018-03-15 19:53:04 +01:00
view.gmx.Stop()
2018-03-14 23:14:39 +01:00
case "/clearcache":
2018-03-15 19:53:04 +01:00
view.config.Session.Rooms = make(map[string]*gomatrix.Room)
view.config.Session.NextBatch = ""
view.config.Session.FilterID = ""
view.config.Session.Save()
view.gmx.Stop()
2018-03-14 23:14:39 +01:00
case "/part":
2018-03-16 15:24:11 +01:00
fallthrough
2018-03-14 23:14:39 +01:00
case "/leave":
2018-03-15 19:53:04 +01:00
view.matrix.client.LeaveRoom(room)
2018-03-14 23:14:39 +01:00
case "/join":
2018-03-13 20:58:43 +01:00
if len(args) == 0 {
view.AddMessage(room, "*", "Usage: /join <room>", time.Now())
2018-03-15 19:53:04 +01:00
break
2018-03-13 20:58:43 +01:00
}
2018-03-16 15:24:11 +01:00
view.debug.Print(view.matrix.JoinRoom(args[0]))
default:
view.AddMessage(room, "*", "Unknown command.", time.Now())
2018-03-13 20:58:43 +01:00
}
}
2018-03-15 19:53:04 +01:00
func (view *MainView) InputCapture(key *tcell.EventKey) *tcell.EventKey {
2018-03-13 20:58:43 +01:00
if key.Modifiers() == tcell.ModCtrl {
if key.Key() == tcell.KeyDown {
2018-03-15 19:53:04 +01:00
view.SwitchRoom(view.currentRoomIndex + 1)
view.roomList.SetCurrentItem(view.currentRoomIndex)
2018-03-13 20:58:43 +01:00
} else if key.Key() == tcell.KeyUp {
2018-03-15 19:53:04 +01:00
view.SwitchRoom(view.currentRoomIndex - 1)
view.roomList.SetCurrentItem(view.currentRoomIndex)
2018-03-14 21:19:26 +01:00
} else {
return key
2018-03-13 20:58:43 +01:00
}
} else if key.Key() == tcell.KeyPgUp || key.Key() == tcell.KeyPgDn {
2018-03-15 19:53:04 +01:00
view.rooms[view.CurrentRoomID()].InputHandler()(key, nil)
2018-03-13 20:58:43 +01:00
} else {
return key
}
return nil
}
2018-03-15 19:53:04 +01:00
func (view *MainView) CurrentRoomID() string {
if len(view.roomIDs) == 0 {
return ""
}
return view.roomIDs[view.currentRoomIndex]
}
func (view *MainView) SwitchRoom(roomIndex int) {
if roomIndex < 0 {
roomIndex = len(view.roomIDs) - 1
}
view.currentRoomIndex = roomIndex % len(view.roomIDs)
view.roomView.SwitchToPage(view.CurrentRoomID())
2018-03-16 15:24:11 +01:00
view.roomList.SetCurrentItem(roomIndex)
2018-03-15 19:53:04 +01:00
view.parent.Render()
}
2018-03-16 15:24:11 +01:00
func (view *MainView) addRoom(index int, room string) {
roomStore := view.matrix.GetRoom(room)
view.roomList.AddItem(roomStore.GetTitle(), "", 0, func() {
view.SwitchRoom(index)
})
if !view.roomView.HasPage(room) {
roomView := NewRoomView(roomStore)
view.rooms[room] = roomView
view.roomView.AddPage(room, roomView, true, false)
roomView.UpdateUserList()
}
}
func (view *MainView) HasRoom(room string) bool {
for _, existingRoom := range view.roomIDs {
if existingRoom == room {
return true
}
}
return false
}
func (view *MainView) AddRoom(room string) {
if view.HasRoom(room) {
return
}
view.roomIDs = append(view.roomIDs, room)
view.addRoom(len(view.roomIDs) - 1, room)
}
func (view *MainView) RemoveRoom(room string) {
if !view.HasRoom(room) {
return
}
view.roomList.RemoveItem(view.currentRoomIndex)
if view.CurrentRoomID() == room {
view.SwitchRoom(view.currentRoomIndex - 1)
}
view.roomView.RemovePage(room)
}
2018-03-15 19:53:04 +01:00
func (view *MainView) SetRoomList(rooms []string) {
view.roomIDs = rooms
view.roomList.Clear()
2018-03-16 15:24:11 +01:00
view.roomView.Clear()
2018-03-13 20:58:43 +01:00
for index, room := range rooms {
2018-03-16 15:24:11 +01:00
view.addRoom(index, room)
2018-03-13 20:58:43 +01:00
}
2018-03-15 19:53:04 +01:00
view.SwitchRoom(0)
2018-03-13 20:58:43 +01:00
}
2018-03-15 19:53:04 +01:00
func (view *MainView) SetTyping(room string, users []string) {
roomView, ok := view.rooms[room]
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
}
}
func (view *MainView) AddMessage(room, sender, message string, timestamp time.Time) {
2018-03-15 19:53:04 +01:00
roomView, ok := view.rooms[room]
2018-03-13 20:58:43 +01:00
if ok {
roomView.AddMessage(sender, message, timestamp)
2018-03-15 19:53:04 +01:00
view.parent.Render()
2018-03-13 20:58:43 +01:00
}
}