Skip to content

fix: prevent excessively long sliding distances.#432

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:release/eaglefrom
lichaofan2008:release/eagle
Mar 20, 2026
Merged

fix: prevent excessively long sliding distances.#432
deepin-bot[bot] merged 1 commit intolinuxdeepin:release/eaglefrom
lichaofan2008:release/eagle

Conversation

@lichaofan2008
Copy link
Copy Markdown
Contributor

@lichaofan2008 lichaofan2008 commented Mar 19, 2026

Limit the speed within the range of -10 to 10 to prevent excessively long sliding distances caused by high speed.
限制速度在-10到10之间,避免速度过快导致滑动距离过长。

Bug: https://pms.uniontech.com/bug-view-353489.html

Summary by Sourcery

Bug Fixes:

  • Limit inertial scrolling speed between -10 and 10 to prevent overly long scroll distances at high swipe speeds.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Mar 19, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Constrains the computed inertial scroll velocities in the text editor to a safe range to prevent excessively long sliding distances at very high speeds.

Sequence diagram for clamped inertial scrolling on mouse move

sequenceDiagram
    actor User
    participant TextEdit
    participant InertialScroller

    User->>TextEdit: mouse drag move (QMouseEvent)
    activate TextEdit
    TextEdit->>TextEdit: calculate diffXpos, diffYpos
    TextEdit->>TextEdit: calculate diffTimeX, diffTimeY

    TextEdit->>InertialScroller: requestInertialScrollParameters(diffXpos, diffYpos, diffTimeX, diffTimeY)
    activate InertialScroller
    InertialScroller->>InertialScroller: m_stepSpeedY = diffYpos / diffTimeY
    InertialScroller->>InertialScroller: m_stepSpeedY = clamp(m_stepSpeedY, -10.0, 10.0)
    InertialScroller->>InertialScroller: durationY = sqrt(abs(m_stepSpeedY)) * 1000
    InertialScroller->>InertialScroller: m_stepSpeedX = diffXpos / diffTimeX
    InertialScroller->>InertialScroller: m_stepSpeedX = clamp(m_stepSpeedX, -10.0, 10.0)
    InertialScroller->>InertialScroller: durationX = sqrt(abs(m_stepSpeedX)) * 1000
    InertialScroller-->>TextEdit: m_stepSpeedX, m_stepSpeedY, durationX, durationY
    deactivate InertialScroller

    TextEdit->>TextEdit: start inertial scroll animation
    deactivate TextEdit

    TextEdit-->>User: smoothed scroll with limited slide distance
Loading

File-Level Changes

Change Details Files
Clamp computed inertial scroll speeds on both axes to a bounded range before using them to derive scroll duration and distance.
  • After computing vertical inertial speed from pointer delta and elapsed time, clamp it between -10.0 and 10.0 before using it to calculate the scroll duration.
  • After computing horizontal inertial speed from pointer delta and elapsed time, clamp it between -10.0 and 10.0 before using it to calculate the scroll duration and distance.
src/editor/dtextedit.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The hard-coded speed limits (-10.0 and 10.0) appear twice; consider extracting them into a named constant (e.g., kMaxInertialSpeed) to make the intent clearer and avoid duplication.
  • Instead of nesting qMin and qMax, consider using qBound (or std::clamp if available) to express the clamping more clearly and reduce the chance of ordering mistakes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The hard-coded speed limits (-10.0 and 10.0) appear twice; consider extracting them into a named constant (e.g., kMaxInertialSpeed) to make the intent clearer and avoid duplication.
- Instead of nesting qMin and qMax, consider using qBound (or std::clamp if available) to express the clamping more clearly and reduce the chance of ordering mistakes.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Limit the speed within the range of -10 to 10 to prevent excessively long sliding distances caused by high speed.
限制速度在-10到10之间,避免速度过快导致滑动距离过长。

Bug: https://pms.uniontech.com/bug-view-353489.html
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这段代码是在处理文本编辑器中的鼠标移动事件,主要涉及惯性滑动的速度计算。以下是对代码变更的详细审查意见:

1. 语法逻辑审查

代码语法正确,逻辑清晰。

  • 使用了 static const 定义常量,这是良好的实践,避免了魔法数字(Magic Number)。
  • 使用 qBound 对速度进行限制,逻辑正确。
  • 注释清晰说明了代码意图。

2. 代码质量审查

代码质量总体良好,但有以下几点可以改进:

优点:

  • 使用了 static const 定义常量,提高了代码可读性和可维护性。
  • 使用 qBound 进行边界检查,确保速度在合理范围内。
  • 添加了清晰的注释,解释了代码的目的。

改进建议:

  1. 常量命名MAX_INERTIAL_SPEED 命名较为通用,建议改为更具描述性的名称,如 MAX_INERTIAL_SCROLL_SPEED,以明确这是惯性滑动的最大速度。
  2. 魔法数字:虽然添加了常量,但代码中仍有其他魔法数字,如 10004.0(在原始代码中)。建议将这些也定义为常量,并添加注释说明其含义。
  3. 注释位置:注释可以放在代码块之前,而不是行尾,这样更符合常见的代码风格。

3. 代码性能审查

代码性能良好:

  • qBound 是一个简单的模板函数,开销很小。
  • 数学运算(除法、平方根)是必要的,没有明显的性能问题。
  • 使用 static const 避免了每次函数调用时重新创建常量。

4. 代码安全审查

代码安全性良好:

  • 使用 qBound 限制了速度范围,防止了因速度过大导致的滑动距离过长问题。
  • 除数 diffTimeY + 0.000001diffTimeX + 0.000001 已经添加了极小值,避免了除以零的风险。

改进后的代码建议

void TextEdit::mouseMoveEvent(QMouseEvent *e)
{
    // 定义惯性滑动的最大速度和计算常量
    static const qreal MAX_INERTIAL_SCROLL_SPEED = 10.0;
    static const qreal DURATION_FACTOR = 1000.0;
    static const qreal TIME_EPSILON = 0.000001;

    if (Qt::MouseEventSynthesizedByQt == e->source())
    {
        m_endY = e->y();
        // ... 其他代码 ...
    }
    else
    {
        // ... 其他代码 ...
        
        /*预算惯性滑动时间*/
        m_stepSpeedY = static_cast<qreal>(diffYpos) / static_cast<qreal>(diffTimeY + TIME_EPSILON);
        m_stepSpeedY = qBound(-MAX_INERTIAL_SCROLL_SPEED, m_stepSpeedY, MAX_INERTIAL_SCROLL_SPEED);
        durationY = sqrt(abs(m_stepSpeedY)) * DURATION_FACTOR;
        
        m_stepSpeedX = static_cast<qreal>(diffXpos) / static_cast<qreal>(diffTimeX + TIME_EPSILON);
        m_stepSpeedX = qBound(-MAX_INERTIAL_SCROLL_SPEED, m_stepSpeedX, MAX_INERTIAL_SCROLL_SPEED);
        durationX = sqrt(abs(m_stepSpeedX)) * DURATION_FACTOR;

        /*预算惯性滑动距离,4.0为调优数值*/
        // ... 其他代码 ...
    }
}

总结

这段代码变更主要是添加了速度限制,以防止惯性滑动过快,这是一个很好的改进。建议进一步优化常量命名和消除魔法数字,以提高代码的可读性和可维护性。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lichaofan2008, max-lvs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@lichaofan2008
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot bot commented Mar 20, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 32ab10b into linuxdeepin:release/eagle Mar 20, 2026
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants