Skip to content

fix(quickpanel): delay DBus call to wait for panel close animation#831

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:bug-fix-4-16
Apr 23, 2026
Merged

fix(quickpanel): delay DBus call to wait for panel close animation#831
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:bug-fix-4-16

Conversation

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member

Delay StartScreenshot DBus call by 300ms after clicking the quick panel screenshot button, allowing the quick panel close animation to complete before the screenshot window appears.

bug: https://pms.uniontech.com/bug-view-358533.html

Delay StartScreenshot DBus call by 300ms after clicking the quick panel
screenshot button, allowing the quick panel close animation to complete
before the screenshot window appears.

bug: https://pms.uniontech.com/bug-view-358533.html
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, lzwind

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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

我已经仔细审查了您提供的 git diff,主要涉及 .gitignore 文件的修改和 shotstartplugin.cpponClickQuickPanel 函数的变更。以下是我的详细审查意见:

1. 总体评价

这次修改主要目的是在触发截图功能前增加了一个 400 毫秒的延时,这通常是为了解决界面状态切换(如隐藏 Dock 小窗口)与截图功能启动之间的竞态条件或视觉冲突问题。.gitignore 的修改是为了排除新增加的 .trellis/ 目录。

2. 代码审查与改进意见

A. .gitignore 修改

  • 逻辑与功能:增加了 .trellis/ 到忽略列表。如果这是项目生成的临时文件或 IDE 配置,修改是合理的。
  • 建议:无重大问题。

B. shotstartplugin.cpp 修改 (onClickQuickPanel 函数)

1. 语法逻辑

  • 当前逻辑:在 !m_isRecording 分支中,代码先隐藏插件界面,然后通过 QTimer::singleShot 延迟 400ms 后调用 DBus 接口启动截图。
  • 潜在风险
    • 状态一致性:在 QTimer::singleShot 的 lambda 回调执行时(即 400ms 后),m_isRecording 的状态是否仍然有效?如果用户在 400ms 内快速点击或进行了其他操作,可能会导致逻辑判断过时。
    • 对象生命周期this 指针被捕获在 lambda 中。虽然 QTimer::singleShot 通常是关联到对象生命周期的,但需确保 ShotStartPlugin 对象在这 400ms 内不会被销毁,否则会导致崩溃。Qt 的上下文对象机制(如果 this 继承自 QObject)通常能处理这种情况,但需注意。

2. 代码质量

  • 硬编码延时400 这个毫秒数是硬编码的。这种"魔法数字"(Magic Number)降低了代码的可维护性。如果未来需要调整延时,或者根据不同机器性能动态调整,修改起来不方便。
  • 日志冗余:在 lambda 内部有两行 qCDebug,其中 qCDebug(dsrApp) << "Shot and Recorder plugin start run!"; 和下一行日志信息略有重复,建议精简。

3. 代码性能

  • DBus 接口重复创建QDBusInterface shotDBusInterface(...) 在 lambda 内部被创建。虽然 Qt 的 DBus 连接有缓存机制,但频繁创建局部 QDBusInterface 对象涉及少量的内存分配和字符串查找。
  • 建议:如果 onClickQuickPanel 调用频率不高,当前性能开销可以忽略不计。如果是高频调用场景,建议将 QDBusInterface 提升为类成员变量或使用静态局部变量进行复用。

4. 代码安全

  • DBus 调用错误处理:代码调用了 asyncCall("StartScreenshot") 但没有检查调用是否成功(例如服务是否存在)。虽然 asyncCall 不会阻塞,但忽略错误可能导致用户点击后无反应且无提示。
  • 建议:虽然异步调用处理回调比较繁琐,但至少应确保 DBus 服务是可用的,或者在日志中记录 DBus 连接失败的情况。

3. 改进后的代码示例

基于以上分析,建议对代码进行如下优化:

// 在类定义中添加常量或静态变量
// class ShotStartPlugin { ... private: static constexpr int SCREENSHOT_DELAY_MS = 400; ... };

void ShotStartPlugin::onClickQuickPanel()
{
    if (!m_isRecording) {
        qCDebug(dsrApp) << "Get Shot DBus Interface";
        m_proxyInter->requestSetAppletVisible(this, pluginName(), false);
        
        // 使用类成员常量代替魔法数字,并明确捕获上下文
        QTimer::singleShot(SCREENSHOT_DELAY_MS, this, [this]() {
            // 检查对象是否仍然有效(虽然 QTimer::singleShot 使用 context object 会自动处理,但显式检查更安全)
            if (!m_isRecording) { 
                // 可选:将 DBusInterface 设为成员变量 m_shotDBusInterface 以提升性能
                QDBusInterface shotDBusInterface(
                    "com.deepin.Screenshot", 
                    "/com/deepin/Screenshot", 
                    "com.deepin.Screenshot", 
                    QDBusConnection::sessionBus()
                );

                if (shotDBusInterface.isValid()) {
                    shotDBusInterface.asyncCall("StartScreenshot");
                    qCDebug(dsrApp) << "Initiating StartScreenshot DBus call.";
                } else {
                    qCWarning(dsrApp) << "Failed to connect to Screenshot DBus service.";
                }
            }
        });
    } else {
        qCDebug(dsrApp) << "Recording in progress, ignoring quick panel click.";
    }
}

4. 总结

  • 主要改进点
    1. 消除魔法数字,使用命名常量。
    2. 在异步回调中增加状态检查,防止状态过期。
    3. 增加 DBus 服务有效性检查,提高健壮性。
    4. 精简日志输出。

这些修改能显著提高代码的可读性、健壮性和可维护性。

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot deepin-bot Bot merged commit 27ef987 into linuxdeepin:master Apr 23, 2026
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