Skip to content

Commit df8a5b0

Browse files
committed
Improve MarkdownLabel sizing and bubble layout
* Refine ChatMessage::resizeEvent to clamp the minimum width to 90 % of the widget width while preserving any pre‑existing minimum. * Add 4 px padding to the bubble backgrounds so text no longer touches the edges. * Adjust MarkdownLabel::heightForWidth to include an additional 8 px of vertical padding. * Implement sizeHint() in MarkdownLabel to honor the widget’s minimum width when queried by layouts. * Increase the horizontal padding used when computing the longest line width from 10 px to 20 px for a more comfortable visual appearance. These tweaks fix layout glitches that appeared when resizing the chat window and improve the overall readability of Markdown content.
1 parent 6008f90 commit df8a5b0

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

llamachatmessage.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ void ChatMessage::buildUI()
227227

228228
void ChatMessage::resizeEvent(QResizeEvent *ev)
229229
{
230-
m_markdownLabel->setMinimumWidth(qMin(m_markdownLabel->minimumWidth(), int(width() * 0.9)));
230+
const int minWidth = qMin(m_markdownLabel->minimumWidth(),
231+
qMax(m_markdownLabel->minimumWidth(), int(width() * 0.9)));
232+
m_markdownLabel->setMinimumWidth(m_msg.role == "user" ? minWidth : int(width() * 0.9));
231233
}
232234

233235
bool ChatMessage::eventFilter(QObject *obj, QEvent *event)
@@ -300,6 +302,7 @@ void ChatMessage::applyStyleSheet()
300302
QTextBrowser#BubbleUser {
301303
background: Token_Background_Muted;
302304
border-radius: 8px;
305+
padding: 4px 4px;
303306
}
304307
QTextBrowser#BubbleAssistant {
305308
background: Token_Background_Default;

llamamarkdownwidget.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,27 @@ int MarkdownLabel::heightForWidth(int w) const
270270
// (this does not change the widget’s real geometry)
271271
QTextDocument *doc = const_cast<QTextDocument *>(document());
272272
doc->setTextWidth(w);
273-
return qRound(doc->size().height());
273+
return qRound(doc->size().height() + 8);
274274
}
275275

276276
void MarkdownLabel::invalidate()
277277
{
278278
m_markdownConversionTimer.invalidate();
279279
}
280280

281+
QSize MarkdownLabel::sizeHint() const
282+
{
283+
QSize sh = QTextBrowser::sizeHint();
284+
sh.setWidth(minimumWidth());
285+
return sh;
286+
}
287+
281288
void MarkdownLabel::adjustMinimumWidth(const QString &markdown)
282289
{
283290
const QStringList lines = markdown.split('\n');
284291
const QString longestLine = *std::ranges::max_element(lines, std::less{}, &QString::length);
285292
QFontMetrics fm(font());
286-
const int longestLineWidth = fm.horizontalAdvance(longestLine) + 10;
293+
const int longestLineWidth = fm.horizontalAdvance(longestLine) + 20;
287294

288295
if (minimumWidth() == 0 || (lines.size() < 5 && minimumWidth() < longestLineWidth))
289296
setMinimumWidth(longestLineWidth);

llamamarkdownwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class MarkdownLabel : public QTextBrowser
2121
void resizeEvent(QResizeEvent *event) override;
2222
int heightForWidth(int w) const override;
2323
void invalidate();
24+
QSize sizeHint() const override;
2425

2526
signals:
2627
void copyToClipboard(const QString &verbatimText, const QString &highlightedText);

0 commit comments

Comments
 (0)