Skip to content

Conversation

wjyrich
Copy link
Contributor

@wjyrich wjyrich commented Oct 9, 2025

  1. Added singleShot method to DQmlGlobal for delayed JavaScript callback execution
  2. Implemented QTimer-based delayed callback with proper error handling
  3. Modified PanelPopupWindow to use delayed reset instead of immediate cleanup
  4. Added necessary includes for QTimer and QQmlEngine in implementation
  5. Fixed potential timing issues by delaying window state reset by 200ms

feat: 添加延迟窗口状态重置功能

  1. 在 DQmlGlobal 中添加 singleShot 方法用于延迟执行 JavaScript 回调
  2. 实现基于 QTimer 的延迟回调并包含错误处理
  3. 修改 PanelPopupWindow 使用延迟重置替代立即清理
  4. 在实现中添加必要的 QTimer 和 QQmlEngine 头文件包含
  5. 通过延迟 200ms 重置窗口状态修复潜在时序问题

Summary by Sourcery

Implement a QTimer-based singleShot API in DQmlGlobal for delayed JS callbacks and update PanelPopupWindow to use this delay for state reset, fixing timing glitches

New Features:

  • Add DQmlGlobal.singleShot method for delayed JavaScript callback execution

Bug Fixes:

  • Delay PanelPopupWindow state reset by 200ms to address potential timing issues

Enhancements:

  • Refactor PanelPopupWindow to use delayed resetWindowState callback instead of immediate cleanup

Build:

  • Include QTimer and QQmlEngine headers in DQmlGlobal implementation and add QJSValue and Qt meta macros in private header

Copy link

sourcery-ai bot commented Oct 9, 2025

Reviewer's Guide

This PR introduces a QTimer-based singleShot mechanism in DQmlGlobal to defer JavaScript callbacks, updates PanelPopupWindow to leverage delayed resets, and adds necessary includes to support the new functionality, addressing timing issues by delaying window state cleanup by 200ms.

Class diagram for updated DQmlGlobal with singleShot method

classDiagram
    class DQmlGlobal {
        +void singleShot(int msec, const QJSValue &callback)
        +static void closeChildrenWindows(QWindow *target)
        +bool grabKeyboard(QWindow *target, bool grab = true)
        +bool grabMouse(QWindow *target, bool grab = true)
        +DApplet *rootApplet() const
        +static DQmlGlobal *instance()
    }
    DQmlGlobal --|> QObject
    DQmlGlobal --|> DObject
Loading

File-Level Changes

Change Details Files
Implement singleShot method in DQmlGlobal for delayed JS callbacks
  • Validate callback.isCallable before scheduling
  • Use QTimer::singleShot to invoke callback after specified msec
  • Wrap callback.call() with error logging if result.isError
frame/dsqmlglobal.cpp
frame/private/dsqmlglobal_p.h
Add QTimer, QQmlEngine and QJSValue includes and expose singleShot in header
  • Include QTimer and QQmlEngine in implementation
  • Include qjsvalue_p.h and QT metamacros in private header
  • Declare singleShot as Q_INVOKABLE
frame/dsqmlglobal.cpp
frame/private/dsqmlglobal_p.h
Refactor PanelPopupWindow to use delayed resetWindowState
  • Extract immediate cleanup into resetWindowState function
  • Replace onVisibleChanged inline logic with DS.singleShot(200, resetWindowState)
  • Ensure no-op if window remains visible
frame/qml/PanelPopupWindow.qml

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 there - I've reviewed your changes - here's some feedback:

  • Use the public QJSValue header () instead of the private qjsvalue_p.h in dsqmlglobal_p.h to avoid relying on Qt private APIs.
  • Protect the delayed singleShot call against DQmlGlobal being destroyed (for example by tying it to a QPointer or context) to avoid invoking callbacks on deleted objects.
  • In PanelPopupWindow.qml, schedule only one pending reset (debounce) or cancel previous timers when visibility toggles rapidly to ensure the window state stays consistent.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Use the public QJSValue header (<QJSValue>) instead of the private qjsvalue_p.h in dsqmlglobal_p.h to avoid relying on Qt private APIs.
- Protect the delayed singleShot call against DQmlGlobal being destroyed (for example by tying it to a QPointer or context) to avoid invoking callbacks on deleted objects.
- In PanelPopupWindow.qml, schedule only one pending reset (debounce) or cancel previous timers when visibility toggles rapidly to ensure the window state stays consistent.

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.

@wjyrich wjyrich force-pushed the fix-popupAnimation branch from 2889f4a to 747e1ca Compare October 10, 2025 03:01
1. Added singleShot method to DQmlGlobal for delayed JavaScript callback
execution
2. Implemented QTimer-based delayed callback with proper error handling
3. Modified PanelPopupWindow to use delayed reset instead of immediate
cleanup
4. Added necessary includes for QTimer and QQmlEngine in implementation
5. Fixed potential timing issues by delaying window state reset by 200ms

feat: 添加延迟窗口状态重置功能

1. 在 DQmlGlobal 中添加 singleShot 方法用于延迟执行 JavaScript 回调
2. 实现基于 QTimer 的延迟回调并包含错误处理
3. 修改 PanelPopupWindow 使用延迟重置替代立即清理
4. 在实现中添加必要的 QTimer 和 QQmlEngine 头文件包含
5. 通过延迟 200ms 重置窗口状态修复潜在时序问题
@wjyrich wjyrich force-pushed the fix-popupAnimation branch from 747e1ca to 3aea476 Compare October 10, 2025 07:35
@deepin-ci-robot
Copy link

deepin pr auto review

我来对这段代码进行审查,从语法逻辑、代码质量、性能和安全性几个方面分析:

语法逻辑分析

  1. singleShot 函数实现正确,使用了 Qt 的定时器机制,并通过 lambda 表达式处理回调。
  2. PanelPopupWindow.qml 中,使用 DS.singleShot 替换了原来的 onVisibleChanged 处理逻辑,逻辑更清晰。
  3. 回调函数中对 root.visible 的检查是必要的,可以避免不必要的操作。

代码质量分析

  1. 优点:

    • 使用了 Q_INVOKABLE 宏使函数可以在 QML 中调用
    • 添加了适当的错误检查和日志记录
    • 使用了现代 C++ 的 lambda 表达式
  2. 改进建议:

    • singleShot 函数可以添加对 msec 参数的验证,确保它是非负数
    • PanelPopupWindow.qml 中,200ms 的延迟时间应该作为常量或配置项,而不是硬编码

性能分析

  1. 优点:

    • 使用 QTimer::singleShot 是轻量级的,不会造成性能问题
    • 延迟执行可以避免频繁的资源释放和窗口操作
  2. 潜在问题:

    • 在 lambda 中捕获了 callback,如果回调函数很大,可能会占用较多内存
    • 没有限制同时运行的 singleShot 实例数量,可能在某些情况下造成资源压力

安全性分析

  1. 优点:

    • 检查了回调是否可调用
    • 捕获并记录了回调执行中的错误
  2. 安全隐患:

    • 没有检查 msec 参数是否为负数,可能导致意外行为
    • 在 lambda 中直接使用了 root 变量,如果 root 在回调执行前被销毁,可能导致访问已释放的内存
    • 没有对回调执行的超时进行控制,如果回调函数执行时间过长,可能会阻塞主线程

具体改进建议

  1. singleShot 函数的改进:
void DQmlGlobal::singleShot(int msec, QJSValue callback)
{
    // 添加参数验证
    if (msec < 0) {
        qCWarning(dsLog) << "singleShot: invalid timeout value:" << msec;
        return;
    }
    
    if (!callback.isCallable()) {
        qCWarning(dsLog) << "singleShot: callback is not callable";
        return;
    }

    // 使用 QWeakPointer 避免悬空指针
    QWeakPointer<QObject> weakThis(this);
    
    QTimer::singleShot(msec, this, [callback, weakThis]() {
        if (weakThis.isNull()) {
            return;
        }
        
        if (callback.isCallable()) {
            // 设置执行超时
            QFuture<void> future = QtConcurrent::run([callback]() {
                QElapsedTimer timer;
                timer.start();
                callback.call();
                if (timer.elapsed() > 1000) { // 假设最大执行时间为1秒
                    qCWarning(dsLog) << "singleShot callback execution time exceeded limit";
                }
            });
            
            // 等待任务完成,但设置超时
            if (!future.waitForFinished(1000)) {
                qCWarning(dsLog) << "singleShot callback execution timeout";
            }
        }
    });
}
  1. PanelPopupWindow.qml 的改进:
// 定义常量
readonly property int HIDE_DELAY: 200

onVisibleChanged: DS.singleShot(HIDE_DELAY, (function () {
    if(!root.visible)
        return
    currentItem = null
    root.width = 10
    root.height = 10
    DS.closeChildrenWindows(root)
}))

这些改进可以提高代码的健壮性、安全性和可维护性。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, wjyrich

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

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

@wjyrich
Copy link
Contributor Author

wjyrich commented Oct 10, 2025

/forcemerge

@deepin-bot
Copy link

deepin-bot bot commented Oct 10, 2025

This pr force merged! (status: behind)

@deepin-bot deepin-bot bot merged commit 0f41dad into linuxdeepin:master Oct 10, 2025
9 of 10 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