MY HANDS ARE TYPING WORDS

This commit is contained in:
Tulir Asokan 2018-03-15 20:53:04 +02:00
parent bffd4d3d17
commit 2bf057d27c
3 changed files with 142 additions and 108 deletions

View File

@ -113,7 +113,7 @@ func (c *MatrixContainer) UpdateRoomList() {
c.debug.Print(err) c.debug.Print(err)
} }
c.ui.SetRoomList(rooms.JoinedRooms) c.ui.MainView().SetRoomList(rooms.JoinedRooms)
} }
func (c *MatrixContainer) Start() { func (c *MatrixContainer) Start() {
@ -147,7 +147,7 @@ func (c *MatrixContainer) Start() {
func (c *MatrixContainer) HandleMessage(evt *gomatrix.Event) { func (c *MatrixContainer) HandleMessage(evt *gomatrix.Event) {
message, _ := evt.Content["body"].(string) message, _ := evt.Content["body"].(string)
c.ui.Append(evt.RoomID, evt.Sender, message) c.ui.MainView().Append(evt.RoomID, evt.Sender, message)
} }
func (c *MatrixContainer) HandleTyping(evt *gomatrix.Event) { func (c *MatrixContainer) HandleTyping(evt *gomatrix.Event) {
@ -158,7 +158,7 @@ func (c *MatrixContainer) HandleTyping(evt *gomatrix.Event) {
for i, user := range users { for i, user := range users {
strUsers[i] = user.(string) strUsers[i] = user.(string)
} }
c.ui.SetTyping(evt.RoomID, strUsers...) c.ui.MainView().SetTyping(evt.RoomID, strUsers)
} }
func (c *MatrixContainer) SendMessage(roomID, message string) { func (c *MatrixContainer) SendMessage(roomID, message string) {

15
ui.go
View File

@ -35,13 +35,7 @@ type GomuksUI struct {
config *Config config *Config
views *tview.Pages views *tview.Pages
mainView *tview.Grid mainView *MainView
mainViewRoomList *tview.List
mainViewRoomView *tview.Pages
mainViewInput *tview.InputField
mainViewRooms map[string]*RoomView
currentRoomIndex int
roomList []string
} }
func init() { func init() {
@ -71,7 +65,12 @@ func (ui *GomuksUI) SetView(name string) {
} }
func (ui *GomuksUI) InitViews() tview.Primitive { func (ui *GomuksUI) InitViews() tview.Primitive {
ui.mainView = ui.NewMainView()
ui.views.AddPage(ViewLogin, ui.MakeLoginUI(), true, true) ui.views.AddPage(ViewLogin, ui.MakeLoginUI(), true, true)
ui.views.AddPage(ViewMain, ui.MakeMainUI(), true, false) ui.views.AddPage(ViewMain, ui.mainView, true, false)
return ui.views return ui.views
} }
func (ui *GomuksUI) MainView() *MainView {
return ui.mainView
}

View File

@ -24,100 +24,150 @@ import (
"maunium.net/go/tview" "maunium.net/go/tview"
) )
func (ui *GomuksUI) MakeMainUI() tview.Primitive { type MainView struct {
ui.mainView = tview.NewGrid() *tview.Grid
ui.mainView.SetColumns(30, 1, 0).SetRows(0, 1)
ui.mainViewRoomList = tview.NewList().ShowSecondaryText(false) roomList *tview.List
ui.mainViewRoomList.SetBorderPadding(0, 0, 1, 0) roomView *tview.Pages
ui.mainView.AddItem(ui.mainViewRoomList, 0, 0, 2, 1, 0, 0, false) rooms map[string]*RoomView
input *tview.InputField
currentRoomIndex int
roomIDs []string
ui.mainView.AddItem(NewBorder(), 0, 1, 2, 1, 0, 0, false) matrix *MatrixContainer
debug DebugPrinter
gmx Gomuks
config *Config
parent *GomuksUI
}
ui.mainViewRoomView = tview.NewPages() func (view *MainView) addItem(p tview.Primitive, x, y, w, h int) {
ui.mainViewRoomView.SetChangedFunc(ui.Render) view.Grid.AddItem(p, x, y, w, h, 0, 0, false)
ui.mainView.AddItem(ui.mainViewRoomView, 0, 2, 1, 1, 0, 0, false) }
ui.mainViewInput = tview.NewInputField() func (ui *GomuksUI) NewMainView() *MainView {
ui.mainViewInput.SetChangedFunc(func(text string) { mainUI := &MainView{
ui.matrix.SendTyping(ui.currentRoom(), len(text) > 0) Grid: tview.NewGrid(),
}) roomList: tview.NewList(),
ui.mainViewInput.SetDoneFunc(func(key tcell.Key) { 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,
}
mainUI.SetColumns(30, 1, 0).SetRows(0, 1)
mainUI.roomList.
ShowSecondaryText(false).
SetBorderPadding(0, 0, 1, 0)
mainUI.input.
SetDoneFunc(mainUI.InputDone).
SetChangedFunc(mainUI.InputChanged).
SetInputCapture(mainUI.InputCapture)
mainUI.addItem(mainUI.roomList, 0, 0, 2, 1)
mainUI.addItem(NewBorder(), 0, 1, 2, 1)
mainUI.addItem(mainUI.roomView, 0, 2, 1, 1)
mainUI.AddItem(mainUI.input, 1, 2, 1, 1, 0, 0, true)
return mainUI
}
func (view *MainView) InputChanged(text string) {
view.matrix.SendTyping(view.CurrentRoomID(), len(text) > 0)
}
func (view *MainView) InputDone(key tcell.Key) {
if key == tcell.KeyEnter { if key == tcell.KeyEnter {
room, text := ui.currentRoom(), ui.mainViewInput.GetText() room, text := view.CurrentRoomID(), view.input.GetText()
if len(text) == 0 { if len(text) == 0 {
return return
} else if text[0] == '/' { } else if text[0] == '/' {
args := strings.SplitN(text, " ", 2) args := strings.SplitN(text, " ", 2)
command := strings.ToLower(args[0]) command := strings.ToLower(args[0])
args = args[1:] args = args[1:]
ui.HandleCommand(room, command, args) view.HandleCommand(room, command, args)
} else { } else {
ui.matrix.SendMessage(room, text) view.matrix.SendMessage(room, text)
} }
ui.mainViewInput.SetText("") view.input.SetText("")
} }
})
ui.mainView.AddItem(ui.mainViewInput, 1, 2, 1, 1, 0, 0, true)
ui.debug.Print(ui.mainViewInput.SetInputCapture(ui.MainUIKeyHandler))
ui.mainViewRooms = make(map[string]*RoomView)
return ui.mainView
} }
func (ui *GomuksUI) HandleCommand(room, command string, args []string) { func (view *MainView) HandleCommand(room, command string, args []string) {
ui.debug.Print("Handling command", command, args) view.debug.Print("Handling command", command, args)
switch command { switch command {
case "/quit": case "/quit":
ui.gmx.Stop() view.gmx.Stop()
case "/clearcache": case "/clearcache":
ui.config.Session.Rooms = make(map[string]*gomatrix.Room) view.config.Session.Rooms = make(map[string]*gomatrix.Room)
ui.config.Session.NextBatch = "" view.config.Session.NextBatch = ""
ui.config.Session.FilterID = "" view.config.Session.FilterID = ""
ui.config.Session.Save() view.config.Session.Save()
ui.gmx.Stop() view.gmx.Stop()
case "/part": case "/part":
case "/leave": case "/leave":
ui.matrix.client.LeaveRoom(room) view.matrix.client.LeaveRoom(room)
case "/join": case "/join":
if len(args) == 0 { if len(args) == 0 {
ui.Append(room, "*", "Usage: /join <room>") view.Append(room, "*", "Usage: /join <room>")
break
} }
mxid := args[0] mxid := args[0]
server := mxid[strings.Index(mxid, ":")+1:] server := mxid[strings.Index(mxid, ":")+1:]
ui.matrix.client.JoinRoom(mxid, server, nil) view.matrix.client.JoinRoom(mxid, server, nil)
} }
} }
func (ui *GomuksUI) MainUIKeyHandler(key *tcell.EventKey) *tcell.EventKey { func (view *MainView) InputCapture(key *tcell.EventKey) *tcell.EventKey {
if key.Modifiers() == tcell.ModCtrl { if key.Modifiers() == tcell.ModCtrl {
if key.Key() == tcell.KeyDown { if key.Key() == tcell.KeyDown {
ui.SwitchRoom(ui.currentRoomIndex + 1) view.SwitchRoom(view.currentRoomIndex + 1)
ui.mainViewRoomList.SetCurrentItem(ui.currentRoomIndex) view.roomList.SetCurrentItem(view.currentRoomIndex)
} else if key.Key() == tcell.KeyUp { } else if key.Key() == tcell.KeyUp {
ui.SwitchRoom(ui.currentRoomIndex - 1) view.SwitchRoom(view.currentRoomIndex - 1)
ui.mainViewRoomList.SetCurrentItem(ui.currentRoomIndex) view.roomList.SetCurrentItem(view.currentRoomIndex)
} else { } else {
return key return key
} }
} else if key.Key() == tcell.KeyPgUp || key.Key() == tcell.KeyPgDn { } else if key.Key() == tcell.KeyPgUp || key.Key() == tcell.KeyPgDn {
ui.mainViewRooms[ui.currentRoom()].InputHandler()(key, nil) view.rooms[view.CurrentRoomID()].InputHandler()(key, nil)
} else { } else {
return key return key
} }
return nil return nil
} }
func (ui *GomuksUI) SetRoomList(rooms []string) { func (view *MainView) CurrentRoomID() string {
ui.roomList = rooms if len(view.roomIDs) == 0 {
ui.mainViewRoomList.Clear() 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())
view.parent.Render()
}
func (view *MainView) SetRoomList(rooms []string) {
view.roomIDs = rooms
view.roomList.Clear()
for index, room := range rooms { for index, room := range rooms {
localRoomIndex := index localRoomIndex := index
ui.matrix.UpdateState(room) view.matrix.UpdateState(room)
roomStore := ui.matrix.config.Session.LoadRoom(room) roomStore := view.matrix.config.Session.LoadRoom(room)
name := room name := room
topic := "" topic := ""
@ -128,46 +178,31 @@ func (ui *GomuksUI) SetRoomList(rooms []string) {
users = roomStore.GetMembers() users = roomStore.GetMembers()
} }
ui.mainViewRoomList.AddItem(name, "", 0, func() { view.roomList.AddItem(name, "", 0, func() {
ui.SwitchRoom(localRoomIndex) view.SwitchRoom(localRoomIndex)
}) })
if !ui.mainViewRoomView.HasPage(room) { if !view.roomView.HasPage(room) {
roomView := NewRoomView(topic) roomView := NewRoomView(topic)
roomView.SetUsers(users) roomView.SetUsers(users)
ui.mainViewRooms[room] = roomView view.rooms[room] = roomView
ui.mainViewRoomView.AddPage(room, roomView, true, false) view.roomView.AddPage(room, roomView, true, false)
} }
} }
ui.SwitchRoom(0) view.SwitchRoom(0)
} }
func (ui *GomuksUI) currentRoom() string { func (view *MainView) SetTyping(room string, users []string) {
if len(ui.roomList) == 0 { roomView, ok := view.rooms[room]
return ""
}
return ui.roomList[ui.currentRoomIndex]
}
func (ui *GomuksUI) SwitchRoom(roomIndex int) {
if roomIndex < 0 {
roomIndex = len(ui.roomList) - 1
}
ui.currentRoomIndex = roomIndex % len(ui.roomList)
ui.mainViewRoomView.SwitchToPage(ui.currentRoom())
}
func (ui *GomuksUI) SetTyping(room string, users ...string) {
roomView, ok := ui.mainViewRooms[room]
if ok { if ok {
roomView.SetTyping(users) roomView.SetTyping(users)
ui.Render() view.parent.Render()
} }
} }
func (ui *GomuksUI) Append(room, sender, message string) { func (view *MainView) Append(room, sender, message string) {
roomView, ok := ui.mainViewRooms[room] roomView, ok := view.rooms[room]
if ok { if ok {
roomView.AddMessage(sender, message) roomView.AddMessage(sender, message)
ui.Render() view.parent.Render()
} }
} }