Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Mactrix/Views/ChatView/ChatMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,9 @@ struct ChatMessageView: View, UI.MessageEventActions {
case let .gallery(content: content):
Text("Gallery: \(content.body)").textSelection(.enabled)
case let .notice(content: content):
Text(content.body.formatAsMarkdown)
.textSelection(.enabled)
.foregroundColor(.secondary)
.fixedSize(horizontal: false, vertical: true)
FormattedBodyView(messageContent: content, color: .secondaryLabelColor)
case let .text(content: content):
FormattedBodyView(messageContent: content)
//Text(content.body.formatAsMarkdown)
case let .location(content: content):
Text("Location: \(content.body) \(content.geoUri)").textSelection(.enabled)
case let .other(msgtype: msgtype, body: body):
Expand Down
39 changes: 29 additions & 10 deletions Mactrix/Views/ChatView/FormattedBodyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,45 @@ struct FormattedBodyView: View {
@AppStorage("fontSize") private var fontSize = 13

let rawBody: String
let htmlBody: String?
var formattedBody: NSAttributedString? = nil
var color: NSColor? = nil

init(messageContent: some MessageContent) {
init(messageContent: some MessageContent) {
self.rawBody = messageContent.body

if let formatted = messageContent.formatted, formatted.format == .html {
self.formattedBody = parseFormattedBody(formatted.body, baseFontSize: CGFloat(fontSize))
}
}

// accepts a custom foreground color (for use in grayed out messages)
init(messageContent: some MessageContent, color: NSColor) {
self.rawBody = messageContent.body
self.color = color

if let formatted = messageContent.formatted, formatted.format == .html {
self.htmlBody = formatted.body
} else {
self.htmlBody = nil
if let formatted = messageContent.formatted, formatted.format == .html {
let parsed = parseFormattedBody(formatted.body, baseFontSize: CGFloat(fontSize))

let mutable = NSMutableAttributedString(attributedString: parsed)
mutable.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: mutable.length))

self.formattedBody = mutable
}
}

var body: some View {
if let htmlBody {
AttributedTextView(attributedString: parseFormattedBody(htmlBody, baseFontSize: CGFloat(fontSize)))
if let formattedBody {
AttributedTextView(attributedString: formattedBody)
.fixedSize(horizontal: false, vertical: true)
} else {
} else if let color {
Text(rawBody)
.textSelection(.enabled)
.fixedSize(horizontal: false, vertical: true)
}
.foregroundStyle(Color(nsColor: color))
} else {
Text(rawBody)
.textSelection(.enabled)
.fixedSize(horizontal: false, vertical: true)
}
}
}
Loading