fix: prevent excessively long sliding distances.#432
fix: prevent excessively long sliding distances.#432deepin-bot[bot] merged 1 commit intolinuxdeepin:release/eaglefrom
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideConstrains 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 movesequenceDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
e0b3cff to
4fdbae7
Compare
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
4fdbae7 to
6b7180a
Compare
deepin pr auto review这段代码是在处理文本编辑器中的鼠标移动事件,主要涉及惯性滑动的速度计算。以下是对代码变更的详细审查意见: 1. 语法逻辑审查代码语法正确,逻辑清晰。
2. 代码质量审查代码质量总体良好,但有以下几点可以改进: 优点:
改进建议:
3. 代码性能审查代码性能良好:
4. 代码安全审查代码安全性良好:
改进后的代码建议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为调优数值*/
// ... 其他代码 ...
}
}总结这段代码变更主要是添加了速度限制,以防止惯性滑动过快,这是一个很好的改进。建议进一步优化常量命名和消除魔法数字,以提高代码的可读性和可维护性。 |
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
|
This pr force merged! (status: unstable) |
32ab10b
into
linuxdeepin:release/eagle
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: