diff --git a/Mactrix/Views/ChatView/ChatMessageView.swift b/Mactrix/Views/ChatView/ChatMessageView.swift index 1cb56ed..b107d13 100644 --- a/Mactrix/Views/ChatView/ChatMessageView.swift +++ b/Mactrix/Views/ChatView/ChatMessageView.swift @@ -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): diff --git a/Mactrix/Views/ChatView/FormattedBodyView.swift b/Mactrix/Views/ChatView/FormattedBodyView.swift index d1c0f17..3649190 100644 --- a/Mactrix/Views/ChatView/FormattedBodyView.swift +++ b/Mactrix/Views/ChatView/FormattedBodyView.swift @@ -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) + } } }