Skip to content

Commit 28829b6

Browse files
committed
feat: implement smart scroll for terminal output
Implement smart scrolling behavior that only auto-scrolls when user is already at the bottom of the terminal. This allows users to scroll up and view history without being forced back to the bottom when new output arrives. Changes: - Add isAtEndOfOutput() method to QTermWidget - Modify onQTermWidgetReceivedData() to check user position before scrolling - Improves experience with high-frequency output apps like kimi-cli - Enhanced debug logging for scroll behavior Fixes: Users can now scroll up to view history while output is ongoing
1 parent ca3cfe3 commit 28829b6

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

3rdparty/terminalwidget/lib/qtermwidget.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,17 @@ void QTermWidget::setTrackOutput(bool enable)
730730
m_impl->m_terminalDisplay->screenWindow()->setTrackOutput(enable);
731731
}
732732

733+
/*******************************************************************************
734+
1. @函数: isAtEndOfOutput
735+
2. @作者: Deepin Terminal Fix
736+
3. @日期: 2025-03-26
737+
4. @说明: 检查当前是否在输出末尾,用于智能滚动
738+
*******************************************************************************/
739+
bool QTermWidget::isAtEndOfOutput() const
740+
{
741+
return m_impl->m_terminalDisplay->screenWindow()->atEndOfOutput();
742+
}
743+
733744
void QTermWidget::sendText(const QString &text)
734745
{
735746
//标记当前命令是代码中通过sendText发给终端的(而不是用户手动输入的命令)

3rdparty/terminalwidget/lib/qtermwidget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ class TERMINALWIDGET_EXPORT QTermWidget : public QWidget
144144
void setTrackOutput(bool enable);
145145
/********************* Modify by n014361 wangpeili End ************************/
146146

147+
/******** Modify for smart scroll: 检查是否在输出末尾 ****************/
148+
bool isAtEndOfOutput() const;
149+
/********************* Modify for smart scroll End ************************/
150+
147151
// Send some text to terminal
148152
void sendText(const QString &text);
149153

src/views/termwidget.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,24 +279,27 @@ inline void TermWidget::onQTermWidgetReceivedData(QString value)
279279
{
280280
qCDebug(views) << "Enter TermWidget::onQTermWidgetReceivedData";
281281
Q_UNUSED(value)
282-
// 完善终端输出滚动相关功能,默认设置为"智能滚动"(即滚动条滑到最底下时自动滚动)
282+
283+
// 如果用户禁用了输出时滚动,重置标志后直接返回
283284
if (!Settings::instance()->OutputtingScroll()) {
284285
qCDebug(views) << "Branch: OutputtingScroll is false, setting isAllowScroll to true";
285286
setIsAllowScroll(true);
286287
return;
287288
}
288289

289-
// 获取是否允许输出时滚动
290-
if (getIsAllowScroll()) {
291-
qCDebug(views) << "Branch: getIsAllowScroll is true, setting trackOutput to OutputtingScroll";
292-
// 允许,则滚动到最新位置
293-
setTrackOutput(Settings::instance()->OutputtingScroll());
290+
// 智能滚动:只在用户在底部时才自动滚动
291+
// 这样用户向上滚动查看历史时不会被强制拉回
292+
qCDebug(views) << "Branch: Smart scroll - checking isAtEndOfOutput";
293+
if (isAtEndOfOutput()) {
294+
qCDebug(views) << "Branch: At end of output, setting trackOutput to true";
295+
setTrackOutput(true);
294296
} else {
295-
qCDebug(views) << "Branch: getIsAllowScroll is false, setting isAllowScroll to true";
296-
// 不允许,则不滚动
297-
// 将标志位置位
298-
setIsAllowScroll(true);
297+
qCDebug(views) << "Branch: Not at end of output, keeping current position";
299298
}
299+
// 如果用户不在底部(正在查看历史),不强制滚动,保持当前位置
300+
301+
// 重置 m_isAllowScroll 标志,确保下次能正常处理
302+
setIsAllowScroll(true);
300303
}
301304

302305
inline void TermWidget::onTermWidgetReceivedData(QString value)

0 commit comments

Comments
 (0)