Add ui toggle for image rendering

This commit is contained in:
Tulir Asokan 2018-06-02 00:43:56 +03:00
parent 7868bd90fa
commit 134604edce
12 changed files with 52 additions and 53 deletions

View File

@ -40,6 +40,7 @@ type UserPreferences struct {
HideUserList bool `yaml:"hide_user_list"` HideUserList bool `yaml:"hide_user_list"`
HideRoomList bool `yaml:"hide_room_list"` HideRoomList bool `yaml:"hide_room_list"`
BareMessageView bool `yaml:"bare_message_view"` BareMessageView bool `yaml:"bare_message_view"`
DisableImages bool `yaml:"disable_images"`
} }
// Config contains the main config of gomuks. // Config contains the main config of gomuks.

View File

@ -131,21 +131,20 @@ func cmdSetState(cmd *Command) {
func cmdUIToggle(cmd *Command) { func cmdUIToggle(cmd *Command) {
if len(cmd.Args) == 0 { if len(cmd.Args) == 0 {
cmd.Reply("Usage: /uitoggle <rooms/users/baremessages>") cmd.Reply("Usage: /uitoggle <rooms/users/baremessages/images>")
return return
} }
switch cmd.Args[0] { switch cmd.Args[0] {
case "rooms": case "rooms":
cmd.MainView.hideRoomList = !cmd.MainView.hideRoomList cmd.Config.Preferences.HideRoomList = !cmd.Config.Preferences.HideRoomList
cmd.Config.Preferences.HideRoomList = cmd.MainView.hideRoomList
case "users": case "users":
cmd.MainView.hideUserList = !cmd.MainView.hideUserList cmd.Config.Preferences.HideUserList = !cmd.Config.Preferences.HideUserList
cmd.Config.Preferences.HideUserList = cmd.MainView.hideUserList
case "baremessages": case "baremessages":
cmd.MainView.bareMessages = !cmd.MainView.bareMessages cmd.Config.Preferences.BareMessageView = !cmd.Config.Preferences.BareMessageView
cmd.Config.Preferences.BareMessageView = cmd.MainView.bareMessages case "images":
cmd.Config.Preferences.DisableImages = !cmd.Config.Preferences.DisableImages
default: default:
cmd.Reply("Usage: /uitoggle <rooms/users/baremessages>") cmd.Reply("Usage: /uitoggle <rooms/users/baremessages/images>")
return return
} }
cmd.UI.Render() cmd.UI.Render()

View File

@ -30,6 +30,7 @@ import (
"maunium.net/go/gomuks/ui/messages" "maunium.net/go/gomuks/ui/messages"
"maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/gomuks/ui/widget" "maunium.net/go/gomuks/ui/widget"
"maunium.net/go/gomuks/config"
"maunium.net/go/tcell" "maunium.net/go/tcell"
"maunium.net/go/tview" "maunium.net/go/tview"
) )
@ -38,6 +39,7 @@ type MessageView struct {
*tview.Box *tview.Box
parent *RoomView parent *RoomView
config *config.Config
ScrollOffset int ScrollOffset int
MaxSenderWidth int MaxSenderWidth int
@ -50,7 +52,7 @@ type MessageView struct {
prevWidth int prevWidth int
prevHeight int prevHeight int
prevMsgCount int prevMsgCount int
prevBareMode bool prevPrefs config.UserPreferences
messageIDs map[string]messages.UIMessage messageIDs map[string]messages.UIMessage
messages []messages.UIMessage messages []messages.UIMessage
@ -63,6 +65,7 @@ func NewMessageView(parent *RoomView) *MessageView {
return &MessageView{ return &MessageView{
Box: tview.NewBox(), Box: tview.NewBox(),
parent: parent, parent: parent,
config: parent.config,
MaxSenderWidth: 15, MaxSenderWidth: 15,
TimestampWidth: len(messages.TimeFormat), TimestampWidth: len(messages.TimeFormat),
@ -77,7 +80,6 @@ func NewMessageView(parent *RoomView) *MessageView {
prevWidth: -1, prevWidth: -1,
prevHeight: -1, prevHeight: -1,
prevMsgCount: -1, prevMsgCount: -1,
prevBareMode: false,
} }
} }
@ -171,11 +173,11 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction ifc.Messag
view.updateWidestSender(message.Sender()) view.updateWidestSender(message.Sender())
_, _, width, _ := view.GetRect() _, _, width, _ := view.GetRect()
bare := view.parent.parent.bareMessages bare := view.config.Preferences.BareMessageView
if !bare { if !bare {
width -= view.TimestampWidth + TimestampSenderGap + view.widestSender + SenderMessageGap width -= view.TimestampWidth + TimestampSenderGap + view.widestSender + SenderMessageGap
} }
message.CalculateBuffer(bare, width) message.CalculateBuffer(view.config.Preferences, width)
if direction == ifc.AppendMessage { if direction == ifc.AppendMessage {
if view.ScrollOffset > 0 { if view.ScrollOffset > 0 {
@ -264,11 +266,13 @@ func (view *MessageView) replaceBuffer(original messages.UIMessage, new messages
func (view *MessageView) recalculateBuffers() { func (view *MessageView) recalculateBuffers() {
_, _, width, height := view.GetRect() _, _, width, height := view.GetRect()
bareMode := view.parent.parent.bareMessages prefs := view.config.Preferences
if !bareMode { if !prefs.BareMessageView {
width -= view.TimestampWidth + TimestampSenderGap + view.widestSender + SenderMessageGap width -= view.TimestampWidth + TimestampSenderGap + view.widestSender + SenderMessageGap
} }
recalculateMessageBuffers := width != view.prevWidth || bareMode != view.prevBareMode recalculateMessageBuffers := width != view.prevWidth ||
view.prevPrefs.BareMessageView != prefs.BareMessageView ||
view.prevPrefs.DisableImages != prefs.DisableImages
if recalculateMessageBuffers || len(view.messages) != view.prevMsgCount { if recalculateMessageBuffers || len(view.messages) != view.prevMsgCount {
view.textBuffer = []tstring.TString{} view.textBuffer = []tstring.TString{}
view.metaBuffer = []ifc.MessageMeta{} view.metaBuffer = []ifc.MessageMeta{}
@ -279,14 +283,14 @@ func (view *MessageView) recalculateBuffers() {
break break
} }
if recalculateMessageBuffers { if recalculateMessageBuffers {
message.CalculateBuffer(bareMode, width) message.CalculateBuffer(prefs, width)
} }
view.appendBuffer(message) view.appendBuffer(message)
} }
} }
view.prevHeight = height view.prevHeight = height
view.prevWidth = width view.prevWidth = width
view.prevBareMode = bareMode view.prevPrefs = prefs
} }
func (view *MessageView) handleMessageClick(message ifc.MessageMeta) bool { func (view *MessageView) handleMessageClick(message ifc.MessageMeta) bool {
@ -495,7 +499,7 @@ func (view *MessageView) Draw(screen tcell.Screen) {
messageX := usernameX + view.widestSender + SenderMessageGap messageX := usernameX + view.widestSender + SenderMessageGap
separatorX := usernameX + view.widestSender + SenderSeparatorGap separatorX := usernameX + view.widestSender + SenderSeparatorGap
bareMode := view.parent.parent.bareMessages bareMode := view.config.Preferences.BareMessageView
if bareMode { if bareMode {
messageX = x messageX = x
} }

View File

@ -24,6 +24,7 @@ import (
"maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/gomuks/ui/widget" "maunium.net/go/gomuks/ui/widget"
"maunium.net/go/tcell" "maunium.net/go/tcell"
"maunium.net/go/gomuks/config"
) )
func init() { func init() {
@ -43,7 +44,7 @@ type BaseMessage struct {
buffer []tstring.TString buffer []tstring.TString
plainBuffer []tstring.TString plainBuffer []tstring.TString
prevBufferWidth int prevBufferWidth int
prevBareMode bool prevPrefs config.UserPreferences
} }
func newBaseMessage(id, sender, displayname, msgtype string, timestamp time.Time) BaseMessage { func newBaseMessage(id, sender, displayname, msgtype string, timestamp time.Time) BaseMessage {
@ -55,7 +56,6 @@ func newBaseMessage(id, sender, displayname, msgtype string, timestamp time.Time
MsgType: msgtype, MsgType: msgtype,
MsgID: id, MsgID: id,
prevBufferWidth: 0, prevBufferWidth: 0,
prevBareMode: false,
MsgState: ifc.MessageStateDefault, MsgState: ifc.MessageStateDefault,
MsgIsHighlight: false, MsgIsHighlight: false,
MsgIsService: false, MsgIsService: false,

View File

@ -21,6 +21,7 @@ import (
"time" "time"
"maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/gomuks/config"
) )
func init() { func init() {
@ -52,11 +53,11 @@ func (msg *ExpandedTextMessage) PlainText() string {
return msg.MsgText.String() return msg.MsgText.String()
} }
func (msg *ExpandedTextMessage) CalculateBuffer(bare bool, width int) { func (msg *ExpandedTextMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
msg.calculateBufferWithText(bare, msg.MsgText, width) msg.calculateBufferWithText(prefs, msg.MsgText, width)
} }
// RecalculateBuffer calculates the buffer again with the previously provided width. // RecalculateBuffer calculates the buffer again with the previously provided width.
func (msg *ExpandedTextMessage) RecalculateBuffer() { func (msg *ExpandedTextMessage) RecalculateBuffer() {
msg.CalculateBuffer(msg.prevBareMode, msg.prevBufferWidth) msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth)
} }

View File

@ -29,6 +29,7 @@ import (
"maunium.net/go/gomuks/lib/ansimage" "maunium.net/go/gomuks/lib/ansimage"
"maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/tcell" "maunium.net/go/tcell"
"maunium.net/go/gomuks/config"
) )
func init() { func init() {
@ -92,13 +93,13 @@ func (msg *ImageMessage) Path() string {
// CalculateBuffer generates the internal buffer for this message that consists // CalculateBuffer generates the internal buffer for this message that consists
// of the text of this message split into lines at most as wide as the width // of the text of this message split into lines at most as wide as the width
// parameter. // parameter.
func (msg *ImageMessage) CalculateBuffer(bare bool, width int) { func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
if width < 2 { if width < 2 {
return return
} }
if bare { if prefs.BareMessageView || prefs.DisableImages {
msg.calculateBufferWithText(bare, tstring.NewTString(msg.PlainText()), width) msg.calculateBufferWithText(prefs, tstring.NewTString(msg.PlainText()), width)
return return
} }
@ -111,10 +112,10 @@ func (msg *ImageMessage) CalculateBuffer(bare bool, width int) {
msg.buffer = image.Render() msg.buffer = image.Render()
msg.prevBufferWidth = width msg.prevBufferWidth = width
msg.prevBareMode = false msg.prevPrefs = prefs
} }
// RecalculateBuffer calculates the buffer again with the previously provided width. // RecalculateBuffer calculates the buffer again with the previously provided width.
func (msg *ImageMessage) RecalculateBuffer() { func (msg *ImageMessage) RecalculateBuffer() {
msg.CalculateBuffer(msg.prevBareMode, msg.prevBufferWidth) msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth)
} }

View File

@ -19,13 +19,14 @@ package messages
import ( import (
"maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/gomuks/config"
) )
// UIMessage is a wrapper for the content and metadata of a Matrix message intended to be displayed. // UIMessage is a wrapper for the content and metadata of a Matrix message intended to be displayed.
type UIMessage interface { type UIMessage interface {
ifc.Message ifc.Message
CalculateBuffer(bare bool, width int) CalculateBuffer(preferences config.UserPreferences, width int)
RecalculateBuffer() RecalculateBuffer()
Buffer() []tstring.TString Buffer() []tstring.TString
Height() int Height() int

View File

@ -20,6 +20,7 @@ import (
"regexp" "regexp"
"maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/messages/tstring"
"fmt" "fmt"
"maunium.net/go/gomuks/config"
) )
// Regular expressions used to split lines when calculating the buffer. // Regular expressions used to split lines when calculating the buffer.
@ -50,14 +51,14 @@ func matchBoundaryPattern(bare bool, extract tstring.TString) tstring.TString {
// CalculateBuffer generates the internal buffer for this message that consists // CalculateBuffer generates the internal buffer for this message that consists
// of the text of this message split into lines at most as wide as the width // of the text of this message split into lines at most as wide as the width
// parameter. // parameter.
func (msg *BaseMessage) calculateBufferWithText(bare bool, text tstring.TString, width int) { func (msg *BaseMessage) calculateBufferWithText(prefs config.UserPreferences, text tstring.TString, width int) {
if width < 2 { if width < 2 {
return return
} }
msg.buffer = []tstring.TString{} msg.buffer = []tstring.TString{}
if bare { if prefs.BareMessageView {
newText := tstring.NewTString(msg.FormatTime()) newText := tstring.NewTString(msg.FormatTime())
if len(msg.Sender()) > 0 { if len(msg.Sender()) > 0 {
newText = newText.AppendTString(tstring.NewColorTString(fmt.Sprintf(" <%s> ", msg.Sender()), msg.SenderColor())) newText = newText.AppendTString(tstring.NewColorTString(fmt.Sprintf(" <%s> ", msg.Sender()), msg.SenderColor()))
@ -84,12 +85,12 @@ func (msg *BaseMessage) calculateBufferWithText(bare bool, text tstring.TString,
if spaces := spacePattern.FindStringIndex(str[len(extract):].String()); spaces != nil && spaces[0] == 0 { if spaces := spacePattern.FindStringIndex(str[len(extract):].String()); spaces != nil && spaces[0] == 0 {
extract = str[:len(extract)+spaces[1]] extract = str[:len(extract)+spaces[1]]
} }
extract = matchBoundaryPattern(bare, extract) extract = matchBoundaryPattern(prefs.BareMessageView, extract)
} }
msg.buffer = append(msg.buffer, extract) msg.buffer = append(msg.buffer, extract)
str = str[len(extract):] str = str[len(extract):]
} }
} }
msg.prevBufferWidth = width msg.prevBufferWidth = width
msg.prevBareMode = bare msg.prevPrefs = prefs
} }

View File

@ -23,6 +23,7 @@ import (
"maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/gomuks/config"
) )
func init() { func init() {
@ -84,11 +85,11 @@ func (msg *TextMessage) PlainText() string {
return msg.MsgText return msg.MsgText
} }
func (msg *TextMessage) CalculateBuffer(bare bool, width int) { func (msg *TextMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
msg.calculateBufferWithText(bare, msg.getCache(), width) msg.calculateBufferWithText(prefs, msg.getCache(), width)
} }
// RecalculateBuffer calculates the buffer again with the previously provided width. // RecalculateBuffer calculates the buffer again with the previously provided width.
func (msg *TextMessage) RecalculateBuffer() { func (msg *TextMessage) RecalculateBuffer() {
msg.CalculateBuffer(msg.prevBareMode, msg.prevBufferWidth) msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth)
} }

View File

@ -32,6 +32,7 @@ import (
"maunium.net/go/gomuks/ui/widget" "maunium.net/go/gomuks/ui/widget"
"maunium.net/go/tcell" "maunium.net/go/tcell"
"maunium.net/go/tview" "maunium.net/go/tview"
"maunium.net/go/gomuks/config"
) )
type RoomView struct { type RoomView struct {
@ -46,6 +47,7 @@ type RoomView struct {
Room *rooms.Room Room *rooms.Room
parent *MainView parent *MainView
config *config.Config
typing []string typing []string
@ -66,6 +68,7 @@ func NewRoomView(parent *MainView, room *rooms.Room) *RoomView {
input: widget.NewAdvancedInputField(), input: widget.NewAdvancedInputField(),
Room: room, Room: room,
parent: parent, parent: parent,
config: parent.config,
} }
view.content = NewMessageView(view) view.content = NewMessageView(view)
@ -202,7 +205,7 @@ func (view *RoomView) Draw(screen tcell.Screen) {
statusRow = contentRow + contentHeight statusRow = contentRow + contentHeight
inputRow = statusRow + StatusBarHeight inputRow = statusRow + StatusBarHeight
) )
if view.parent.hideUserList { if view.config.Preferences.HideUserList {
contentWidth = width contentWidth = width
} }
@ -210,7 +213,7 @@ func (view *RoomView) Draw(screen tcell.Screen) {
view.topic.SetRect(x, topicRow, width, TopicBarHeight) view.topic.SetRect(x, topicRow, width, TopicBarHeight)
view.content.SetRect(x, contentRow, contentWidth, contentHeight) view.content.SetRect(x, contentRow, contentWidth, contentHeight)
view.status.SetRect(x, statusRow, width, StatusBarHeight) view.status.SetRect(x, statusRow, width, StatusBarHeight)
if !view.parent.hideUserList && userListColumn > x { if !view.config.Preferences.HideUserList && userListColumn > x {
view.userList.SetRect(userListColumn, contentRow, UserListWidth, contentHeight) view.userList.SetRect(userListColumn, contentRow, UserListWidth, contentHeight)
view.ulBorder.SetRect(userListBorderColumn, contentRow, UserListBorderWidth, contentHeight) view.ulBorder.SetRect(userListBorderColumn, contentRow, UserListBorderWidth, contentHeight)
} }
@ -223,7 +226,7 @@ func (view *RoomView) Draw(screen tcell.Screen) {
view.status.SetText(view.GetStatus()) view.status.SetText(view.GetStatus())
view.status.Draw(screen) view.status.Draw(screen)
view.input.Draw(screen) view.input.Draw(screen)
if !view.parent.hideUserList { if !view.config.Preferences.HideUserList {
view.ulBorder.Draw(screen) view.ulBorder.Draw(screen)
view.userList.Draw(screen) view.userList.Draw(screen)
} }

View File

@ -93,10 +93,6 @@ func (ui *GomuksUI) OnLogout() {
} }
func (ui *GomuksUI) HandleNewPreferences() { func (ui *GomuksUI) HandleNewPreferences() {
prefs := ui.gmx.Config().Preferences
ui.mainView.bareMessages = prefs.BareMessageView
ui.mainView.hideUserList = prefs.HideUserList
ui.mainView.hideRoomList = prefs.HideRoomList
ui.Render() ui.Render()
} }

View File

@ -50,14 +50,9 @@ type MainView struct {
gmx ifc.Gomuks gmx ifc.Gomuks
config *config.Config config *config.Config
parent *GomuksUI parent *GomuksUI
hideUserList bool
hideRoomList bool
bareMessages bool
} }
func (ui *GomuksUI) NewMainView() tview.Primitive { func (ui *GomuksUI) NewMainView() tview.Primitive {
prefs := ui.gmx.Config().Preferences
mainView := &MainView{ mainView := &MainView{
Flex: tview.NewFlex(), Flex: tview.NewFlex(),
roomList: NewRoomList(), roomList: NewRoomList(),
@ -68,10 +63,6 @@ func (ui *GomuksUI) NewMainView() tview.Primitive {
gmx: ui.gmx, gmx: ui.gmx,
config: ui.gmx.Config(), config: ui.gmx.Config(),
parent: ui, parent: ui,
hideUserList: prefs.HideUserList,
hideRoomList: prefs.HideRoomList,
bareMessages: prefs.BareMessageView,
} }
mainView.cmdProcessor = NewCommandProcessor(mainView) mainView.cmdProcessor = NewCommandProcessor(mainView)
@ -88,7 +79,7 @@ func (ui *GomuksUI) NewMainView() tview.Primitive {
} }
func (view *MainView) Draw(screen tcell.Screen) { func (view *MainView) Draw(screen tcell.Screen) {
if view.hideRoomList { if view.config.Preferences.HideRoomList {
view.roomView.SetRect(view.GetRect()) view.roomView.SetRect(view.GetRect())
view.roomView.Draw(screen) view.roomView.Draw(screen)
} else { } else {