aboutsummaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-04-18 18:35:24 +0300
committerTulir Asokan <tulir@maunium.net>2018-04-18 18:35:54 +0300
commitb17ff318c26aecdcf2c6719ed5b0872be5254c44 (patch)
tree43f5f9aa3ce300c15376860727ff13d3716f3751 /ui
parent3b2d5fa0349bf01217f6954813c669680cdd16d4 (diff)
Disconnect UI from main Gomuks struct. Fixes #21
Diffstat (limited to 'ui')
-rw-r--r--ui/message-view.go4
-rw-r--r--ui/messages/base.go2
-rw-r--r--ui/messages/imagemessage.go18
-rw-r--r--ui/messages/message.go2
-rw-r--r--ui/messages/parser/parser.go10
-rw-r--r--ui/room-view.go4
-rw-r--r--ui/ui.go26
-rw-r--r--ui/view-main.go12
-rw-r--r--ui/widget/color.go2
9 files changed, 47 insertions, 33 deletions
diff --git a/ui/message-view.go b/ui/message-view.go
index 4b67c6a..71d1cba 100644
--- a/ui/message-view.go
+++ b/ui/message-view.go
@@ -95,7 +95,7 @@ func (view *MessageView) SaveHistory(path string) error {
return nil
}
-func (view *MessageView) LoadHistory(gmx ifc.Gomuks, path string) (int, error) {
+func (view *MessageView) LoadHistory(matrix ifc.MatrixContainer, path string) (int, error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0600)
if err != nil {
if os.IsNotExist(err) {
@@ -119,7 +119,7 @@ func (view *MessageView) LoadHistory(gmx ifc.Gomuks, path string) (int, error) {
if message != nil {
view.messages[index-indexOffset] = message
view.updateWidestSender(message.Sender())
- message.RegisterGomuks(gmx)
+ message.RegisterMatrix(matrix)
} else {
indexOffset++
}
diff --git a/ui/messages/base.go b/ui/messages/base.go
index 5322b34..cf698db 100644
--- a/ui/messages/base.go
+++ b/ui/messages/base.go
@@ -59,7 +59,7 @@ func newBaseMessage(id, sender, displayname, msgtype string, timestamp time.Time
}
}
-func (msg *BaseMessage) RegisterGomuks(gmx ifc.Gomuks) {}
+func (msg *BaseMessage) RegisterMatrix(matrix ifc.MatrixContainer) {}
// Sender gets the string that should be displayed as the sender of this message.
//
diff --git a/ui/messages/imagemessage.go b/ui/messages/imagemessage.go
index cd8c2fe..c9301c5 100644
--- a/ui/messages/imagemessage.go
+++ b/ui/messages/imagemessage.go
@@ -41,27 +41,26 @@ type ImageMessage struct {
FileID string
data []byte
- gmx ifc.Gomuks
+ matrix ifc.MatrixContainer
}
// NewImageMessage creates a new ImageMessage object with the provided values and the default state.
-func NewImageMessage(gmx ifc.Gomuks, id, sender, displayname, msgtype, homeserver, fileID string, data []byte, timestamp time.Time) UIMessage {
+func NewImageMessage(matrix ifc.MatrixContainer, id, sender, displayname, msgtype, homeserver, fileID string, data []byte, timestamp time.Time) UIMessage {
return &ImageMessage{
newBaseMessage(id, sender, displayname, msgtype, timestamp),
homeserver,
fileID,
data,
- gmx,
+ matrix,
}
}
-func (msg *ImageMessage) RegisterGomuks(gmx ifc.Gomuks) {
- msg.gmx = gmx
+func (msg *ImageMessage) RegisterMatrix(matrix ifc.MatrixContainer) {
+ msg.matrix = matrix
- debug.Print(len(msg.data), msg.data)
if len(msg.data) == 0 {
go func() {
- defer gmx.Recover()
+ defer debug.Recover()
msg.updateData()
}()
}
@@ -73,7 +72,7 @@ func (msg *ImageMessage) NotificationContent() string {
func (msg *ImageMessage) updateData() {
debug.Print("Loading image:", msg.Homeserver, msg.FileID)
- data, _, _, err := msg.gmx.Matrix().Download(fmt.Sprintf("mxc://%s/%s", msg.Homeserver, msg.FileID))
+ data, _, _, err := msg.matrix.Download(fmt.Sprintf("mxc://%s/%s", msg.Homeserver, msg.FileID))
if err != nil {
debug.Print("Failed to download image %s/%s: %v", msg.Homeserver, msg.FileID, err)
return
@@ -82,7 +81,7 @@ func (msg *ImageMessage) updateData() {
}
func (msg *ImageMessage) Path() string {
- return msg.gmx.Matrix().GetCachePath(msg.Homeserver, msg.FileID)
+ return msg.matrix.GetCachePath(msg.Homeserver, msg.FileID)
}
// CalculateBuffer generates the internal buffer for this message that consists
@@ -108,4 +107,3 @@ func (msg *ImageMessage) CalculateBuffer(width int) {
func (msg *ImageMessage) RecalculateBuffer() {
msg.CalculateBuffer(msg.prevBufferWidth)
}
-
diff --git a/ui/messages/message.go b/ui/messages/message.go
index 0d5c9e4..d3f2db4 100644
--- a/ui/messages/message.go
+++ b/ui/messages/message.go
@@ -32,7 +32,7 @@ type UIMessage interface {
SenderID() string
RealSender() string
- RegisterGomuks(gmx ifc.Gomuks)
+ RegisterMatrix(matrix ifc.MatrixContainer)
}
const DateFormat = "January _2, 2006"
diff --git a/ui/messages/parser/parser.go b/ui/messages/parser/parser.go
index fbb2bf5..9c833f5 100644
--- a/ui/messages/parser/parser.go
+++ b/ui/messages/parser/parser.go
@@ -30,10 +30,10 @@ import (
"maunium.net/go/tcell"
)
-func ParseEvent(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
+func ParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
switch evt.Type {
case "m.room.message":
- return ParseMessage(gmx, room, evt)
+ return ParseMessage(matrix, room, evt)
case "m.room.member":
return ParseMembershipEvent(room, evt)
}
@@ -48,7 +48,7 @@ func unixToTime(unix int64) time.Time {
return timestamp
}
-func ParseMessage(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
+func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
displayname := evt.Sender
member := room.GetMember(evt.Sender)
if member != nil {
@@ -68,11 +68,11 @@ func ParseMessage(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) message
}
case "m.image":
url, _ := evt.Content["url"].(string)
- data, hs, id, err := gmx.Matrix().Download(url)
+ data, hs, id, err := matrix.Download(url)
if err != nil {
debug.Printf("Failed to download %s: %v", url, err)
}
- return messages.NewImageMessage(gmx, evt.ID, evt.Sender, displayname, msgtype, hs, id, data, ts)
+ return messages.NewImageMessage(matrix, evt.ID, evt.Sender, displayname, msgtype, hs, id, data, ts)
}
return nil
}
diff --git a/ui/room-view.go b/ui/room-view.go
index d1e5a16..5268682 100644
--- a/ui/room-view.go
+++ b/ui/room-view.go
@@ -81,8 +81,8 @@ func (view *RoomView) SaveHistory(dir string) error {
return view.MessageView().SaveHistory(view.logPath(dir))
}
-func (view *RoomView) LoadHistory(gmx ifc.Gomuks, dir string) (int, error) {
- return view.MessageView().LoadHistory(gmx, view.logPath(dir))
+func (view *RoomView) LoadHistory(matrix ifc.MatrixContainer, dir string) (int, error) {
+ return view.MessageView().LoadHistory(matrix, view.logPath(dir))
}
func (view *RoomView) SetTabCompleteFunc(fn func(room *RoomView, text string, cursorOffset int) string) *RoomView {
diff --git a/ui/ui.go b/ui/ui.go
index b5f847d..75fd8bf 100644
--- a/ui/ui.go
+++ b/ui/ui.go
@@ -36,14 +36,32 @@ func init() {
tview.Styles.ContrastBackgroundColor = tcell.ColorDarkGreen
}
-func NewGomuksUI(gmx ifc.Gomuks) (ui *GomuksUI) {
- ui = &GomuksUI{
+func NewGomuksUI(gmx ifc.Gomuks) ifc.GomuksUI {
+ ui := &GomuksUI{
gmx: gmx,
- app: gmx.App(),
+ app: tview.NewApplication(),
views: tview.NewPages(),
}
ui.views.SetChangedFunc(ui.Render)
- return
+ return ui
+}
+
+func (ui *GomuksUI) Init() {
+ ui.app.SetRoot(ui.InitViews(), true)
+}
+
+func (ui *GomuksUI) Start() error {
+ return ui.app.Run()
+}
+
+func (ui *GomuksUI) Stop() {
+ ui.app.Stop()
+}
+
+func (ui *GomuksUI) Finish() {
+ if ui.app.GetScreen() != nil {
+ ui.app.GetScreen().Fini()
+ }
}
func (ui *GomuksUI) Render() {
diff --git a/ui/view-main.go b/ui/view-main.go
index f54654a..c3e94a5 100644
--- a/ui/view-main.go
+++ b/ui/view-main.go
@@ -143,7 +143,7 @@ func (view *MainView) SendMessage(roomView *RoomView, text string) {
}
func (view *MainView) sendTempMessage(roomView *RoomView, tempMessage ifc.Message, text string) {
- defer view.gmx.Recover()
+ defer debug.Recover()
eventID, err := view.matrix.SendMarkdownMessage(roomView.Room.ID, tempMessage.Type(), text)
if err != nil {
tempMessage.SetState(ifc.MessageStateFailed)
@@ -154,7 +154,7 @@ func (view *MainView) sendTempMessage(roomView *RoomView, tempMessage ifc.Messag
}
func (view *MainView) HandleCommand(roomView *RoomView, command string, args []string) {
- defer view.gmx.Recover()
+ defer debug.Recover()
debug.Print("Handling command", command, args)
switch command {
case "/me":
@@ -286,7 +286,7 @@ func (view *MainView) SwitchRoom(roomIndex int) {
roomView.Room.MarkRead()
}
view.roomList.SetSelected(roomView.Room)
- view.gmx.App().SetFocus(view)
+ view.parent.app.SetFocus(view)
view.parent.Render()
}
@@ -321,7 +321,7 @@ func (view *MainView) addRoom(index int, room string) {
view.roomView.AddPage(room, roomView, true, false)
roomView.UpdateUserList()
- count, err := roomView.LoadHistory(view.gmx, view.config.HistoryDir)
+ count, err := roomView.LoadHistory(view.matrix, view.config.HistoryDir)
if err != nil {
debug.Printf("Failed to load history of %s: %v", roomView.Room.GetTitle(), err)
} else if count <= 0 {
@@ -424,7 +424,7 @@ func (view *MainView) NotifyMessage(room *rooms.Room, message ifc.Message, shoul
}
func (view *MainView) LoadHistory(room string, initial bool) {
- defer view.gmx.Recover()
+ defer debug.Recover()
roomView := view.rooms[room]
batch := roomView.Room.PrevBatch
@@ -472,5 +472,5 @@ func (view *MainView) LoadHistory(room string, initial bool) {
}
func (view *MainView) ParseEvent(roomView ifc.RoomView, evt *gomatrix.Event) ifc.Message {
- return parser.ParseEvent(view.gmx, roomView.MxRoom(), evt)
+ return parser.ParseEvent(view.matrix, roomView.MxRoom(), evt)
}
diff --git a/ui/widget/color.go b/ui/widget/color.go
index c46377a..e2c2b68 100644
--- a/ui/widget/color.go
+++ b/ui/widget/color.go
@@ -21,7 +21,6 @@ import (
"hash/fnv"
"sort"
- "maunium.net/go/gomuks/debug"
"maunium.net/go/tcell"
)
@@ -52,7 +51,6 @@ func init() {
// <-- = red
// --- = yellow
func GetHashColorName(s string) string {
- debug.Print("Getting color for", s)
switch s {
case "-->":
return "green"