Add ui toggle for image rendering
This commit is contained in:
		| @@ -40,6 +40,7 @@ type UserPreferences struct { | ||||
| 	HideUserList    bool `yaml:"hide_user_list"` | ||||
| 	HideRoomList    bool `yaml:"hide_room_list"` | ||||
| 	BareMessageView bool `yaml:"bare_message_view"` | ||||
| 	DisableImages   bool `yaml:"disable_images"` | ||||
| } | ||||
|  | ||||
| // Config contains the main config of gomuks. | ||||
|   | ||||
| @@ -131,21 +131,20 @@ func cmdSetState(cmd *Command) { | ||||
|  | ||||
| func cmdUIToggle(cmd *Command) { | ||||
| 	if len(cmd.Args) == 0 { | ||||
| 		cmd.Reply("Usage: /uitoggle <rooms/users/baremessages>") | ||||
| 		cmd.Reply("Usage: /uitoggle <rooms/users/baremessages/images>") | ||||
| 		return | ||||
| 	} | ||||
| 	switch cmd.Args[0] { | ||||
| 	case "rooms": | ||||
| 		cmd.MainView.hideRoomList = !cmd.MainView.hideRoomList | ||||
| 		cmd.Config.Preferences.HideRoomList = cmd.MainView.hideRoomList | ||||
| 		cmd.Config.Preferences.HideRoomList = !cmd.Config.Preferences.HideRoomList | ||||
| 	case "users": | ||||
| 		cmd.MainView.hideUserList = !cmd.MainView.hideUserList | ||||
| 		cmd.Config.Preferences.HideUserList = cmd.MainView.hideUserList | ||||
| 		cmd.Config.Preferences.HideUserList = !cmd.Config.Preferences.HideUserList | ||||
| 	case "baremessages": | ||||
| 		cmd.MainView.bareMessages = !cmd.MainView.bareMessages | ||||
| 		cmd.Config.Preferences.BareMessageView = cmd.MainView.bareMessages | ||||
| 		cmd.Config.Preferences.BareMessageView = !cmd.Config.Preferences.BareMessageView | ||||
| 	case "images": | ||||
| 		cmd.Config.Preferences.DisableImages = !cmd.Config.Preferences.DisableImages | ||||
| 	default: | ||||
| 		cmd.Reply("Usage: /uitoggle <rooms/users/baremessages>") | ||||
| 		cmd.Reply("Usage: /uitoggle <rooms/users/baremessages/images>") | ||||
| 		return | ||||
| 	} | ||||
| 	cmd.UI.Render() | ||||
|   | ||||
| @@ -30,6 +30,7 @@ import ( | ||||
| 	"maunium.net/go/gomuks/ui/messages" | ||||
| 	"maunium.net/go/gomuks/ui/messages/tstring" | ||||
| 	"maunium.net/go/gomuks/ui/widget" | ||||
| 	"maunium.net/go/gomuks/config" | ||||
| 	"maunium.net/go/tcell" | ||||
| 	"maunium.net/go/tview" | ||||
| ) | ||||
| @@ -38,6 +39,7 @@ type MessageView struct { | ||||
| 	*tview.Box | ||||
|  | ||||
| 	parent *RoomView | ||||
| 	config *config.Config | ||||
|  | ||||
| 	ScrollOffset    int | ||||
| 	MaxSenderWidth  int | ||||
| @@ -50,7 +52,7 @@ type MessageView struct { | ||||
| 	prevWidth    int | ||||
| 	prevHeight   int | ||||
| 	prevMsgCount int | ||||
| 	prevBareMode bool | ||||
| 	prevPrefs    config.UserPreferences | ||||
|  | ||||
| 	messageIDs map[string]messages.UIMessage | ||||
| 	messages   []messages.UIMessage | ||||
| @@ -63,6 +65,7 @@ func NewMessageView(parent *RoomView) *MessageView { | ||||
| 	return &MessageView{ | ||||
| 		Box:    tview.NewBox(), | ||||
| 		parent: parent, | ||||
| 		config: parent.config, | ||||
|  | ||||
| 		MaxSenderWidth: 15, | ||||
| 		TimestampWidth: len(messages.TimeFormat), | ||||
| @@ -77,7 +80,6 @@ func NewMessageView(parent *RoomView) *MessageView { | ||||
| 		prevWidth:    -1, | ||||
| 		prevHeight:   -1, | ||||
| 		prevMsgCount: -1, | ||||
| 		prevBareMode: false, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -171,11 +173,11 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction ifc.Messag | ||||
| 	view.updateWidestSender(message.Sender()) | ||||
|  | ||||
| 	_, _, width, _ := view.GetRect() | ||||
| 	bare := view.parent.parent.bareMessages | ||||
| 	bare := view.config.Preferences.BareMessageView | ||||
| 	if !bare { | ||||
| 		width -= view.TimestampWidth + TimestampSenderGap + view.widestSender + SenderMessageGap | ||||
| 	} | ||||
| 	message.CalculateBuffer(bare, width) | ||||
| 	message.CalculateBuffer(view.config.Preferences, width) | ||||
|  | ||||
| 	if direction == ifc.AppendMessage { | ||||
| 		if view.ScrollOffset > 0 { | ||||
| @@ -264,11 +266,13 @@ func (view *MessageView) replaceBuffer(original messages.UIMessage, new messages | ||||
|  | ||||
| func (view *MessageView) recalculateBuffers() { | ||||
| 	_, _, width, height := view.GetRect() | ||||
| 	bareMode := view.parent.parent.bareMessages | ||||
| 	if !bareMode { | ||||
| 	prefs := view.config.Preferences | ||||
| 	if !prefs.BareMessageView { | ||||
| 		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 { | ||||
| 		view.textBuffer = []tstring.TString{} | ||||
| 		view.metaBuffer = []ifc.MessageMeta{} | ||||
| @@ -279,14 +283,14 @@ func (view *MessageView) recalculateBuffers() { | ||||
| 				break | ||||
| 			} | ||||
| 			if recalculateMessageBuffers { | ||||
| 				message.CalculateBuffer(bareMode, width) | ||||
| 				message.CalculateBuffer(prefs, width) | ||||
| 			} | ||||
| 			view.appendBuffer(message) | ||||
| 		} | ||||
| 	} | ||||
| 	view.prevHeight = height | ||||
| 	view.prevWidth = width | ||||
| 	view.prevBareMode = bareMode | ||||
| 	view.prevPrefs = prefs | ||||
| } | ||||
|  | ||||
| func (view *MessageView) handleMessageClick(message ifc.MessageMeta) bool { | ||||
| @@ -495,7 +499,7 @@ func (view *MessageView) Draw(screen tcell.Screen) { | ||||
| 	messageX := usernameX + view.widestSender + SenderMessageGap | ||||
| 	separatorX := usernameX + view.widestSender + SenderSeparatorGap | ||||
|  | ||||
| 	bareMode := view.parent.parent.bareMessages | ||||
| 	bareMode := view.config.Preferences.BareMessageView | ||||
| 	if bareMode { | ||||
| 		messageX = x | ||||
| 	} | ||||
|   | ||||
| @@ -24,6 +24,7 @@ import ( | ||||
| 	"maunium.net/go/gomuks/ui/messages/tstring" | ||||
| 	"maunium.net/go/gomuks/ui/widget" | ||||
| 	"maunium.net/go/tcell" | ||||
| 	"maunium.net/go/gomuks/config" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| @@ -43,7 +44,7 @@ type BaseMessage struct { | ||||
| 	buffer          []tstring.TString | ||||
| 	plainBuffer     []tstring.TString | ||||
| 	prevBufferWidth int | ||||
| 	prevBareMode    bool | ||||
| 	prevPrefs    config.UserPreferences | ||||
| } | ||||
|  | ||||
| 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, | ||||
| 		MsgID:           id, | ||||
| 		prevBufferWidth: 0, | ||||
| 		prevBareMode:    false, | ||||
| 		MsgState:        ifc.MessageStateDefault, | ||||
| 		MsgIsHighlight:  false, | ||||
| 		MsgIsService:    false, | ||||
|   | ||||
| @@ -21,6 +21,7 @@ import ( | ||||
| 	"time" | ||||
|  | ||||
| 	"maunium.net/go/gomuks/ui/messages/tstring" | ||||
| 	"maunium.net/go/gomuks/config" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| @@ -52,11 +53,11 @@ func (msg *ExpandedTextMessage) PlainText() string { | ||||
| 	return msg.MsgText.String() | ||||
| } | ||||
|  | ||||
| func (msg *ExpandedTextMessage) CalculateBuffer(bare bool, width int) { | ||||
| 	msg.calculateBufferWithText(bare, msg.MsgText, width) | ||||
| func (msg *ExpandedTextMessage) CalculateBuffer(prefs config.UserPreferences, width int) { | ||||
| 	msg.calculateBufferWithText(prefs, msg.MsgText, width) | ||||
| } | ||||
|  | ||||
| // RecalculateBuffer calculates the buffer again with the previously provided width. | ||||
| func (msg *ExpandedTextMessage) RecalculateBuffer() { | ||||
| 	msg.CalculateBuffer(msg.prevBareMode, msg.prevBufferWidth) | ||||
| 	msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth) | ||||
| } | ||||
|   | ||||
| @@ -29,6 +29,7 @@ import ( | ||||
| 	"maunium.net/go/gomuks/lib/ansimage" | ||||
| 	"maunium.net/go/gomuks/ui/messages/tstring" | ||||
| 	"maunium.net/go/tcell" | ||||
| 	"maunium.net/go/gomuks/config" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| @@ -92,13 +93,13 @@ func (msg *ImageMessage) Path() string { | ||||
| // 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 | ||||
| // parameter. | ||||
| func (msg *ImageMessage) CalculateBuffer(bare bool, width int) { | ||||
| func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int) { | ||||
| 	if width < 2 { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	if bare { | ||||
| 		msg.calculateBufferWithText(bare, tstring.NewTString(msg.PlainText()), width) | ||||
| 	if prefs.BareMessageView || prefs.DisableImages { | ||||
| 		msg.calculateBufferWithText(prefs, tstring.NewTString(msg.PlainText()), width) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| @@ -111,10 +112,10 @@ func (msg *ImageMessage) CalculateBuffer(bare bool, width int) { | ||||
|  | ||||
| 	msg.buffer = image.Render() | ||||
| 	msg.prevBufferWidth = width | ||||
| 	msg.prevBareMode = false | ||||
| 	msg.prevPrefs = prefs | ||||
| } | ||||
|  | ||||
| // RecalculateBuffer calculates the buffer again with the previously provided width. | ||||
| func (msg *ImageMessage) RecalculateBuffer() { | ||||
| 	msg.CalculateBuffer(msg.prevBareMode, msg.prevBufferWidth) | ||||
| 	msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth) | ||||
| } | ||||
|   | ||||
| @@ -19,13 +19,14 @@ package messages | ||||
| import ( | ||||
| 	"maunium.net/go/gomuks/interface" | ||||
| 	"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. | ||||
| type UIMessage interface { | ||||
| 	ifc.Message | ||||
|  | ||||
| 	CalculateBuffer(bare bool, width int) | ||||
| 	CalculateBuffer(preferences config.UserPreferences, width int) | ||||
| 	RecalculateBuffer() | ||||
| 	Buffer() []tstring.TString | ||||
| 	Height() int | ||||
|   | ||||
| @@ -20,6 +20,7 @@ import ( | ||||
| 	"regexp" | ||||
| 	"maunium.net/go/gomuks/ui/messages/tstring" | ||||
| 	"fmt" | ||||
| 	"maunium.net/go/gomuks/config" | ||||
| ) | ||||
|  | ||||
| // 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 | ||||
| // of the text of this message split into lines at most as wide as the width | ||||
| // 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 { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	msg.buffer = []tstring.TString{} | ||||
|  | ||||
| 	if bare { | ||||
| 	if prefs.BareMessageView { | ||||
| 		newText := tstring.NewTString(msg.FormatTime()) | ||||
| 		if len(msg.Sender()) > 0 { | ||||
| 			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 { | ||||
| 					extract = str[:len(extract)+spaces[1]] | ||||
| 				} | ||||
| 				extract = matchBoundaryPattern(bare, extract) | ||||
| 				extract = matchBoundaryPattern(prefs.BareMessageView, extract) | ||||
| 			} | ||||
| 			msg.buffer = append(msg.buffer, extract) | ||||
| 			str = str[len(extract):] | ||||
| 		} | ||||
| 	} | ||||
| 	msg.prevBufferWidth = width | ||||
| 	msg.prevBareMode = bare | ||||
| 	msg.prevPrefs = prefs | ||||
| } | ||||
|   | ||||
| @@ -23,6 +23,7 @@ import ( | ||||
|  | ||||
| 	"maunium.net/go/gomuks/interface" | ||||
| 	"maunium.net/go/gomuks/ui/messages/tstring" | ||||
| 	"maunium.net/go/gomuks/config" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| @@ -84,11 +85,11 @@ func (msg *TextMessage) PlainText() string { | ||||
| 	return msg.MsgText | ||||
| } | ||||
|  | ||||
| func (msg *TextMessage) CalculateBuffer(bare bool, width int) { | ||||
| 	msg.calculateBufferWithText(bare, msg.getCache(), width) | ||||
| func (msg *TextMessage) CalculateBuffer(prefs config.UserPreferences, width int) { | ||||
| 	msg.calculateBufferWithText(prefs, msg.getCache(), width) | ||||
| } | ||||
|  | ||||
| // RecalculateBuffer calculates the buffer again with the previously provided width. | ||||
| func (msg *TextMessage) RecalculateBuffer() { | ||||
| 	msg.CalculateBuffer(msg.prevBareMode, msg.prevBufferWidth) | ||||
| 	msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth) | ||||
| } | ||||
|   | ||||
| @@ -32,6 +32,7 @@ import ( | ||||
| 	"maunium.net/go/gomuks/ui/widget" | ||||
| 	"maunium.net/go/tcell" | ||||
| 	"maunium.net/go/tview" | ||||
| 	"maunium.net/go/gomuks/config" | ||||
| ) | ||||
|  | ||||
| type RoomView struct { | ||||
| @@ -46,6 +47,7 @@ type RoomView struct { | ||||
| 	Room     *rooms.Room | ||||
|  | ||||
| 	parent *MainView | ||||
| 	config *config.Config | ||||
|  | ||||
| 	typing []string | ||||
|  | ||||
| @@ -66,6 +68,7 @@ func NewRoomView(parent *MainView, room *rooms.Room) *RoomView { | ||||
| 		input:    widget.NewAdvancedInputField(), | ||||
| 		Room:     room, | ||||
| 		parent:   parent, | ||||
| 		config:   parent.config, | ||||
| 	} | ||||
| 	view.content = NewMessageView(view) | ||||
|  | ||||
| @@ -202,7 +205,7 @@ func (view *RoomView) Draw(screen tcell.Screen) { | ||||
| 		statusRow  = contentRow + contentHeight | ||||
| 		inputRow   = statusRow + StatusBarHeight | ||||
| 	) | ||||
| 	if view.parent.hideUserList { | ||||
| 	if view.config.Preferences.HideUserList { | ||||
| 		contentWidth = width | ||||
| 	} | ||||
|  | ||||
| @@ -210,7 +213,7 @@ func (view *RoomView) Draw(screen tcell.Screen) { | ||||
| 	view.topic.SetRect(x, topicRow, width, TopicBarHeight) | ||||
| 	view.content.SetRect(x, contentRow, contentWidth, contentHeight) | ||||
| 	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.ulBorder.SetRect(userListBorderColumn, contentRow, UserListBorderWidth, contentHeight) | ||||
| 	} | ||||
| @@ -223,7 +226,7 @@ func (view *RoomView) Draw(screen tcell.Screen) { | ||||
| 	view.status.SetText(view.GetStatus()) | ||||
| 	view.status.Draw(screen) | ||||
| 	view.input.Draw(screen) | ||||
| 	if !view.parent.hideUserList { | ||||
| 	if !view.config.Preferences.HideUserList { | ||||
| 		view.ulBorder.Draw(screen) | ||||
| 		view.userList.Draw(screen) | ||||
| 	} | ||||
|   | ||||
							
								
								
									
										4
									
								
								ui/ui.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								ui/ui.go
									
									
									
									
									
								
							| @@ -93,10 +93,6 @@ func (ui *GomuksUI) OnLogout() { | ||||
| } | ||||
|  | ||||
| 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() | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -50,14 +50,9 @@ type MainView struct { | ||||
| 	gmx    ifc.Gomuks | ||||
| 	config *config.Config | ||||
| 	parent *GomuksUI | ||||
|  | ||||
| 	hideUserList bool | ||||
| 	hideRoomList bool | ||||
| 	bareMessages bool | ||||
| } | ||||
|  | ||||
| func (ui *GomuksUI) NewMainView() tview.Primitive { | ||||
| 	prefs := ui.gmx.Config().Preferences | ||||
| 	mainView := &MainView{ | ||||
| 		Flex:     tview.NewFlex(), | ||||
| 		roomList: NewRoomList(), | ||||
| @@ -68,10 +63,6 @@ func (ui *GomuksUI) NewMainView() tview.Primitive { | ||||
| 		gmx:    ui.gmx, | ||||
| 		config: ui.gmx.Config(), | ||||
| 		parent: ui, | ||||
|  | ||||
| 		hideUserList: prefs.HideUserList, | ||||
| 		hideRoomList: prefs.HideRoomList, | ||||
| 		bareMessages: prefs.BareMessageView, | ||||
| 	} | ||||
| 	mainView.cmdProcessor = NewCommandProcessor(mainView) | ||||
|  | ||||
| @@ -88,7 +79,7 @@ func (ui *GomuksUI) NewMainView() tview.Primitive { | ||||
| } | ||||
|  | ||||
| func (view *MainView) Draw(screen tcell.Screen) { | ||||
| 	if view.hideRoomList { | ||||
| 	if view.config.Preferences.HideRoomList { | ||||
| 		view.roomView.SetRect(view.GetRect()) | ||||
| 		view.roomView.Draw(screen) | ||||
| 	} else { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user