Skip to content

fix: support scroll on touch screen#511

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
ut003640:master
Mar 3, 2026
Merged

fix: support scroll on touch screen#511
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
ut003640:master

Conversation

@ut003640
Copy link
Contributor

@ut003640 ut003640 commented Mar 3, 2026

support scroll on touch screen

PMS: BUG-351259

Summary by Sourcery

Bug Fixes:

  • Allow NetView content to be scrolled on touch-screen devices by using a touch gesture scroller and disabling overscroll.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 3, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Configures QScroller-based touch scrolling for NetView’s viewport, enabling touch gesture scrolling and customizing scroller properties to remove overshoot and tune scrolling dynamics on touch screens.

Sequence diagram for NetView constructor touch scrolling setup

sequenceDiagram
    participant NetManager
    participant NetView
    participant QAbstractItemView as ViewportOwner
    participant QWidget as Viewport
    participant QScroller
    participant QScrollerProperties as Props

    NetManager ->> NetView: create NetView(manager)
    NetView ->> ViewportOwner: viewport()
    ViewportOwner -->> NetView: Viewport

    NetView ->> QScroller: grabGesture(Viewport, TouchGesture)

    NetView ->> QScroller: scroller(Viewport)
    QScroller -->> NetView: scrollerInstance

    NetView ->> QScroller: scrollerProperties()
    QScroller -->> NetView: Props

    NetView ->> Props: setScrollMetric(VerticalOvershootPolicy, OvershootAlwaysOff)
    NetView ->> Props: setScrollMetric(HorizontalOvershootPolicy, OvershootAlwaysOff)
    NetView ->> Props: setScrollMetric(DecelerationFactor, 0.5)
    NetView ->> Props: setScrollMetric(MaximumVelocity, 0.5)

    NetView ->> QScroller: setScrollerProperties(Props)
Loading

Updated class diagram for NetView touch scrolling configuration

classDiagram
    class NetView {
        +NetView(manager : NetManager*)
        +~NetView()
    }

    class NetManager

    class QAbstractItemView {
        +viewport() : QWidget*
    }

    class QWidget

    class QScroller {
        +static grabGesture(target : QObject*, gestureType : GestureType) : void
        +static scroller(target : QObject*) : QScroller*
        +scrollerProperties() : QScrollerProperties
        +setScrollerProperties(properties : QScrollerProperties) : void
    }

    class QScrollerProperties {
        +setScrollMetric(metric : ScrollMetric, value : double) : void
    }

    NetView --|> QAbstractItemView
    NetView --> NetManager
    NetView ..> QScroller : configures_scrolling
    NetView ..> QScrollerProperties : updates_properties
    QScroller o--> QScrollerProperties
    QAbstractItemView o--> QWidget : owns_viewport
Loading

File-Level Changes

Change Details Files
Enable QScroller touch gesture scrolling on the NetView viewport and configure its behavior to work better on touch screens.
  • Include QScrollerProperties to allow configuration of scrolling metrics.
  • Change QScroller gesture from the previously commented-out LeftMouseButtonGesture to an active TouchGesture on the viewport.
  • Obtain the viewport’s QScroller instance and read its current scroller properties before modification.
  • Disable vertical and horizontal overshoot via QScrollerProperties metrics.
  • Adjust deceleration factor and maximum velocity to 0.5 for smoother, slower scrolling on touch input.
  • Apply the updated QScrollerProperties back to the viewport’s scroller.
net-view/window/netview.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

@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 found 1 issue, and left some high level feedback:

  • Consider avoiding hard-coded magic values for DecelerationFactor and MaximumVelocity (e.g., define named constants or add a brief comment) so the chosen tuning parameters are easier to understand and adjust later.
  • You call QScroller::scroller(viewport()) multiple times; caching the pointer in a local variable would slightly simplify the code and avoid duplicate lookups.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider avoiding hard-coded magic values for DecelerationFactor and MaximumVelocity (e.g., define named constants or add a brief comment) so the chosen tuning parameters are easier to understand and adjust later.
- You call QScroller::scroller(viewport()) multiple times; caching the pointer in a local variable would slightly simplify the code and avoid duplicate lookups.

## Individual Comments

### Comment 1
<location path="net-view/window/netview.cpp" line_range="85-93" />
<code_context>
+    QScrollerProperties sp = QScroller::scroller(viewport())->scrollerProperties();
+    sp.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
+    sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
+    sp.setScrollMetric(QScrollerProperties::DecelerationFactor, 0.5);
+    sp.setScrollMetric(QScrollerProperties::MaximumVelocity, 0.5);
+    QScroller::scroller(viewport())->setScrollerProperties(sp);
 }
</code_context>
<issue_to_address>
**suggestion:** Consider extracting scroll tuning constants or referencing expected ranges.

These hard-coded `0.5` values effectively lock in your touch scroll UX and may need tuning later. Consider naming these as shared constants or centralizing them in a config so they’re easier to adjust, reason about, and keep aligned with Qt’s recommended or platform-specific ranges.

```suggestion
    // 支持在触摸屏上滚动
    constexpr qreal kTouchScrollDecelerationFactor = 0.5;  // Tunable: see Qt docs for recommended ranges
    constexpr qreal kTouchScrollMaximumVelocity    = 0.5;  // Tunable: see Qt docs for recommended ranges

    QScroller::grabGesture(viewport(), QScroller::TouchGesture);
    QScroller *scroller = QScroller::scroller(viewport());
    QScrollerProperties sp = scroller->scrollerProperties();
    sp.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
    sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
    sp.setScrollMetric(QScrollerProperties::DecelerationFactor, kTouchScrollDecelerationFactor);
    sp.setScrollMetric(QScrollerProperties::MaximumVelocity, kTouchScrollMaximumVelocity);
    scroller->setScrollerProperties(sp);
}
```
</issue_to_address>

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.

Comment on lines 85 to 93
// 支持在触摸屏上滚动
// QScroller::grabGesture(viewport(), QScroller::LeftMouseButtonGesture);
// QScrollerProperties sp;
// sp.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
QScroller::grabGesture(viewport(), QScroller::TouchGesture);
QScrollerProperties sp = QScroller::scroller(viewport())->scrollerProperties();
sp.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
sp.setScrollMetric(QScrollerProperties::DecelerationFactor, 0.5);
sp.setScrollMetric(QScrollerProperties::MaximumVelocity, 0.5);
QScroller::scroller(viewport())->setScrollerProperties(sp);
}
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider extracting scroll tuning constants or referencing expected ranges.

These hard-coded 0.5 values effectively lock in your touch scroll UX and may need tuning later. Consider naming these as shared constants or centralizing them in a config so they’re easier to adjust, reason about, and keep aligned with Qt’s recommended or platform-specific ranges.

Suggested change
// 支持在触摸屏上滚动
// QScroller::grabGesture(viewport(), QScroller::LeftMouseButtonGesture);
// QScrollerProperties sp;
// sp.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
QScroller::grabGesture(viewport(), QScroller::TouchGesture);
QScrollerProperties sp = QScroller::scroller(viewport())->scrollerProperties();
sp.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
sp.setScrollMetric(QScrollerProperties::DecelerationFactor, 0.5);
sp.setScrollMetric(QScrollerProperties::MaximumVelocity, 0.5);
QScroller::scroller(viewport())->setScrollerProperties(sp);
}
// 支持在触摸屏上滚动
constexpr qreal kTouchScrollDecelerationFactor = 0.5; // Tunable: see Qt docs for recommended ranges
constexpr qreal kTouchScrollMaximumVelocity = 0.5; // Tunable: see Qt docs for recommended ranges
QScroller::grabGesture(viewport(), QScroller::TouchGesture);
QScroller *scroller = QScroller::scroller(viewport());
QScrollerProperties sp = scroller->scrollerProperties();
sp.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
sp.setScrollMetric(QScrollerProperties::DecelerationFactor, kTouchScrollDecelerationFactor);
sp.setScrollMetric(QScrollerProperties::MaximumVelocity, kTouchScrollMaximumVelocity);
scroller->setScrollerProperties(sp);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

时间改下

support scroll on touch screen

PMS: BUG-351259
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码的修改主要是为了在 NetView 类中启用触摸屏滚动功能,并配置相关的滚动参数。以下是对这段代码的审查意见,包括语法逻辑、代码质量、代码性能和代码安全方面的改进建议:

1. 语法逻辑

  • 逻辑正确性:代码逻辑基本正确。它启用了触摸手势(QScroller::TouchGesture),并配置了滚动属性,如禁用垂直和水平过冲(OvershootAlwaysOff),并设置了减速度因子和最大速度。
  • 参数合理性DecelerationFactorMaximumVelocity 被设置为 0.5。这些值是否合理取决于具体的使用场景。建议根据实际测试效果调整这些值,可能需要更细致的调优。

2. 代码质量

  • 代码可读性:代码结构清晰,注释得当。但 sp 变量名可以更具描述性,例如改为 scrollerProperties
  • 代码复用性:如果滚动参数需要在多个地方使用,建议将其提取为常量或配置函数,以提高复用性。
  • 注释:原代码中有一段被注释掉的代码(QScroller::LeftMouseButtonGesture),建议删除或说明为何保留。

3. 代码性能

  • 性能影响:启用 QScroller 会对滚动性能产生一定影响,尤其是在低端设备上。建议在实际设备上测试滚动流畅度,必要时调整 DecelerationFactorMaximumVelocity 以优化性能。
  • 资源管理QScroller::scroller(viewport()) 被调用两次,可以优化为一次调用并缓存结果,以减少不必要的开销。

4. 代码安全

  • 空指针检查QScroller::scroller(viewport()) 返回的指针可能为空(尽管在 viewport() 有效时不太可能),建议增加空指针检查以提高鲁棒性。
  • 异常处理:Qt 的 QScroller 相关操作通常不会抛出异常,但建议在关键操作后检查返回值或状态。

改进后的代码示例

// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "netview.h"

// ... 其他头文件 ...

NetView::NetView(NetManager *manager)
    : DListView(manager)
    , m_manager(manager)
{
    // ... 其他初始化代码 ...

    connect(this, &NetView::activated, this, &NetView::onActivated);

    // 配置触摸屏滚动参数
    configureTouchScrolling();
}

void NetView::configureTouchScrolling()
{
    // 启用触摸手势
    QScroller::grabGesture(viewport(), QScroller::TouchGesture);

    // 获取并配置滚动属性
    QScroller *scroller = QScroller::scroller(viewport());
    if (!scroller) {
        qWarning() << "Failed to get QScroller for viewport";
        return;
    }

    QScrollerProperties scrollerProperties = scroller->scrollerProperties();
    scrollerProperties.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
    scrollerProperties.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
    scrollerProperties.setScrollMetric(QScrollerProperties::DecelerationFactor, 0.5);
    scrollerProperties.setScrollMetric(QScrollerProperties::MaximumVelocity, 0.5);
    scroller->setScrollerProperties(scrollerProperties);
}

总结

  • 优点:代码逻辑清晰,功能明确,启用了触摸滚动并配置了合理的参数。
  • 改进点
    1. 增加空指针检查,提高鲁棒性。
    2. 优化变量命名,提高可读性。
    3. 提取滚动配置为单独函数,提高复用性。
    4. 删除无用的注释代码。
    5. 根据实际测试调整滚动参数。

这些改进可以提升代码的可维护性、安全性和性能。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: caixr23, ut003640

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

@ut003640
Copy link
Contributor Author

ut003640 commented Mar 3, 2026

merge

@ut003640
Copy link
Contributor Author

ut003640 commented Mar 3, 2026

/merge

@deepin-bot deepin-bot bot merged commit 075a564 into linuxdeepin:master Mar 3, 2026
16 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