Skip to content

Commit bf02c50

Browse files
fix(AcpMessageWidget): eliminate phantom whitespace in thought bubbles
QTextDocument reports a larger height than the visible content due to implicit paragraph margins and trailing empty blocks. This caused thought bubbles to grow extra blank space below the last line of text. Zero block margins via normalizeBlockMargins, strip trailing whitespace before setPlainText, apply a CSS reset for HTML content, and use font metrics instead of sizeHint for the header height to avoid style-derived button padding inflating the bubble.
1 parent e29a717 commit bf02c50

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

‎src/widgets/AcpMessageWidget.cpp‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
#include <QRegularExpression>
2626
#include <QResizeEvent>
2727
#include <QScrollBar>
28+
#include <QTextBlock>
2829
#include <QTextBrowser>
30+
#include <QTextCursor>
2931
#include <QTextDocument>
3032
#include <QToolButton>
3133
#include <QVBoxLayout>
@@ -53,6 +55,27 @@ void configureBubbleBrowser(QTextBrowser *b)
5355
b->document()->setDocumentMargin(0);
5456
}
5557

58+
// QTextBlock carries an implicit top/bottom margin (paragraph leading) that
59+
// `documentMargin(0)` does NOT zero. The result is that `doc->size().height()`
60+
// over-reports the rendered text height by ~one line's worth of leading,
61+
// inflating bubbles with empty space below the last visible line. Walk every
62+
// block and merge a zero-margin block format so the document's size matches
63+
// what the user actually sees.
64+
void normalizeBlockMargins(QTextDocument *doc)
65+
{
66+
if (!doc) return;
67+
QTextBlockFormat zero;
68+
zero.setTopMargin(0);
69+
zero.setBottomMargin(0);
70+
QTextCursor cur(doc);
71+
cur.movePosition(QTextCursor::Start);
72+
do {
73+
QTextCursor blockCur = cur;
74+
blockCur.select(QTextCursor::BlockUnderCursor);
75+
blockCur.mergeBlockFormat(zero);
76+
} while (cur.movePosition(QTextCursor::NextBlock));
77+
}
78+
5679
} // namespace
5780

5881
AcpMessageWidget::AcpMessageWidget(QString role, QWidget *parent)
@@ -83,12 +106,18 @@ AcpMessageWidget::AcpMessageWidget(QString role, QWidget *parent)
83106
m_thoughtHeader->setChecked(true); // start expanded while streaming
84107
m_thoughtHeader->setStyleSheet(QStringLiteral("QToolButton { border: none; padding: 0; margin: 0; font-style: italic; color: palette(placeholder-text); text-align: left; }"));
85108
m_thoughtHeader->setToolButtonStyle(Qt::ToolButtonTextOnly);
109+
m_thoughtHeader->setFixedHeight(m_thoughtHeader->fontMetrics().height());
86110
m_layout->addWidget(m_thoughtHeader);
87111

88112
m_browser = new QTextBrowser(this);
89113
m_browser->setStyleSheet(QStringLiteral("QTextBrowser { background: transparent; border: none; font-style: italic; padding-left: 4px; }"));
90114
m_browser->setOpenExternalLinks(true);
91115
configureBubbleBrowser(m_browser);
116+
// QTextDocument paragraphs carry an implicit ~12px bottom margin even
117+
// with documentMargin=0; zero it out so the bubble doesn't sprout
118+
// phantom whitespace below the last line.
119+
m_browser->document()->setDefaultStyleSheet(
120+
QStringLiteral("p, body { margin: 0; padding: 0; }"));
92121
m_layout->addWidget(m_browser);
93122

94123
connect(m_thoughtHeader, &QToolButton::toggled, this, [this](bool checked) {
@@ -232,8 +261,21 @@ void AcpMessageWidget::rerender()
232261
html.replace(QRegularExpression(QStringLiteral("<table([^>]*)>")),
233262
QStringLiteral("<table\\1 cellspacing=\"0\" cellpadding=\"8\">"));
234263
m_browser->document()->setHtml(html);
264+
normalizeBlockMargins(m_browser->document());
235265
} else {
236-
m_browser->document()->setPlainText(m_text);
266+
// Streamed chunks often end with "\n", which QTextDocument turns into
267+
// an empty trailing block that adds a full line-height of phantom
268+
// whitespace below the visible text. Strip trailing whitespace so the
269+
// document size matches what the user actually reads.
270+
QString text = m_text;
271+
while (!text.isEmpty() && (text.endsWith(QLatin1Char('\n'))
272+
|| text.endsWith(QLatin1Char('\r'))
273+
|| text.endsWith(QLatin1Char(' '))
274+
|| text.endsWith(QLatin1Char('\t')))) {
275+
text.chop(1);
276+
}
277+
m_browser->document()->setPlainText(text);
278+
normalizeBlockMargins(m_browser->document());
237279
}
238280
refitBrowserHeight();
239281
}
@@ -263,7 +305,10 @@ void AcpMessageWidget::refitBrowserHeight()
263305
// bubble. Computing the bubble height here directly is authoritative.
264306
int bubbleH = marginT + marginB;
265307
if (m_thoughtHeader) {
266-
bubbleH += m_thoughtHeader->sizeHint().height();
308+
// Use font metrics for the header height; QToolButton::sizeHint()
309+
// adds style-derived button margins even with stylesheet padding:0,
310+
// which adds phantom vertical space inside the bubble.
311+
bubbleH += m_thoughtHeader->fontMetrics().height();
267312
if (m_browser->isVisible()) {
268313
bubbleH += m_layout->spacing() + browserH;
269314
}

0 commit comments

Comments
 (0)