|
25 | 25 | #include <QRegularExpression> |
26 | 26 | #include <QResizeEvent> |
27 | 27 | #include <QScrollBar> |
| 28 | +#include <QTextBlock> |
28 | 29 | #include <QTextBrowser> |
| 30 | +#include <QTextCursor> |
29 | 31 | #include <QTextDocument> |
30 | 32 | #include <QToolButton> |
31 | 33 | #include <QVBoxLayout> |
@@ -53,6 +55,27 @@ void configureBubbleBrowser(QTextBrowser *b) |
53 | 55 | b->document()->setDocumentMargin(0); |
54 | 56 | } |
55 | 57 |
|
| 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 | + |
56 | 79 | } // namespace |
57 | 80 |
|
58 | 81 | AcpMessageWidget::AcpMessageWidget(QString role, QWidget *parent) |
@@ -83,12 +106,18 @@ AcpMessageWidget::AcpMessageWidget(QString role, QWidget *parent) |
83 | 106 | m_thoughtHeader->setChecked(true); // start expanded while streaming |
84 | 107 | m_thoughtHeader->setStyleSheet(QStringLiteral("QToolButton { border: none; padding: 0; margin: 0; font-style: italic; color: palette(placeholder-text); text-align: left; }")); |
85 | 108 | m_thoughtHeader->setToolButtonStyle(Qt::ToolButtonTextOnly); |
| 109 | + m_thoughtHeader->setFixedHeight(m_thoughtHeader->fontMetrics().height()); |
86 | 110 | m_layout->addWidget(m_thoughtHeader); |
87 | 111 |
|
88 | 112 | m_browser = new QTextBrowser(this); |
89 | 113 | m_browser->setStyleSheet(QStringLiteral("QTextBrowser { background: transparent; border: none; font-style: italic; padding-left: 4px; }")); |
90 | 114 | m_browser->setOpenExternalLinks(true); |
91 | 115 | 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; }")); |
92 | 121 | m_layout->addWidget(m_browser); |
93 | 122 |
|
94 | 123 | connect(m_thoughtHeader, &QToolButton::toggled, this, [this](bool checked) { |
@@ -232,8 +261,21 @@ void AcpMessageWidget::rerender() |
232 | 261 | html.replace(QRegularExpression(QStringLiteral("<table([^>]*)>")), |
233 | 262 | QStringLiteral("<table\\1 cellspacing=\"0\" cellpadding=\"8\">")); |
234 | 263 | m_browser->document()->setHtml(html); |
| 264 | + normalizeBlockMargins(m_browser->document()); |
235 | 265 | } 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()); |
237 | 279 | } |
238 | 280 | refitBrowserHeight(); |
239 | 281 | } |
@@ -263,7 +305,10 @@ void AcpMessageWidget::refitBrowserHeight() |
263 | 305 | // bubble. Computing the bubble height here directly is authoritative. |
264 | 306 | int bubbleH = marginT + marginB; |
265 | 307 | 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(); |
267 | 312 | if (m_browser->isVisible()) { |
268 | 313 | bubbleH += m_layout->spacing() + browserH; |
269 | 314 | } |
|
0 commit comments