Skip to content

refactor(notification): move wayland guard into initScreenLockedState#1612

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
mhduiy:test
May 29, 2026
Merged

refactor(notification): move wayland guard into initScreenLockedState#1612
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
mhduiy:test

Conversation

@mhduiy
Copy link
Copy Markdown
Contributor

@mhduiy mhduiy commented May 29, 2026

  1. Moved the Wayland platform check from the constructor into initScreenLockedState()
  2. Replaced QGuiApplication::platformName() string comparison with DGuiApplicationHelper::testAttribute(IsWaylandPlatform)
  3. Added TODO note for future Wayland session support in screen lock detection

Log: Refactor Wayland session check into initScreenLockedState using Dtk API

refactor(notification): 将 Wayland 检测移入 initScreenLockedState

  1. 将 Wayland 平台检测从构造函数移入 initScreenLockedState() 方法
  2. 使用 DGuiApplicationHelper::testAttribute(IsWaylandPlatform) 替代 QGuiApplication::platformName() 字符串比较
  3. 添加 TODO 注释记录待 Wayland 支持屏幕锁检测

Log: 用 Dtk API 重构 Wayland 检测逻辑,移入 initScreenLockedState

Summary by Sourcery

Refactor notification screen lock initialization to centralize and modernize Wayland platform detection.

Enhancements:

  • Move the Wayland platform guard from the NotificationManager constructor into initScreenLockedState to centralize screen lock initialization logic.
  • Replace string-based platform name checks with DGuiApplicationHelper::testAttribute(IsWaylandPlatform) for Wayland detection.
  • Add a TODO marker to document pending Wayland session support for screen lock detection.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 29, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors Wayland platform detection for notification screen lock handling by moving the guard into initScreenLockedState and switching to DGuiApplicationHelper’s Wayland attribute check, while adding a TODO for future Wayland lock support.

Flow diagram for initScreenLockedState Wayland guard refactor

flowchart TD
    A[NotificationManager constructor] --> B[initScreenLockedState]
    B --> C{"DGuiApplicationHelper::testAttribute(IsWaylandPlatform)"}
    C -- true --> D[return]
    C -- false --> E[Connect to org.deepin.dde.LockFront1<br/>and initialize screen lock state]
Loading

File-Level Changes

Change Details Files
Refactor Wayland platform guard around screen lock state initialization.
  • Move the platform guard from the NotificationManager constructor into initScreenLockedState so the constructor always calls the initializer
  • Introduce an early return in initScreenLockedState when running on Wayland to skip DBus-based lock handling
  • Add a TODO comment indicating that Wayland session screen lock detection still needs implementation
panels/notification/server/notificationmanager.cpp
Update Wayland detection to use Dtk API instead of string-based platformName comparison.
  • Include DGuiApplicationHelper header to access Dtk GUI attributes
  • Replace QGuiApplication::platformName() string comparison with DGuiApplicationHelper::testAttribute(IsWaylandPlatform) for platform detection
panels/notification/server/notificationmanager.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:

  • Consider updating the TODO comment text to clearly state the intention (e.g. "TODO: implement screen lock detection in Wayland sessions") to avoid confusion about the current behavior.
  • When early-returning for Wayland in initScreenLockedState, you might want to add a debug log or comment explaining that screen lock state is intentionally not initialized on Wayland yet, so future readers understand why this path is skipped.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider updating the TODO comment text to clearly state the intention (e.g. "TODO: implement screen lock detection in Wayland sessions") to avoid confusion about the current behavior.
- When early-returning for Wayland in initScreenLockedState, you might want to add a debug log or comment explaining that screen lock state is intentionally not initialized on Wayland yet, so future readers understand why this path is skipped.

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.

18202781743
18202781743 previously approved these changes May 29, 2026
@mhduiy mhduiy force-pushed the test branch 5 times, most recently from 8059512 to 1c7a444 Compare May 29, 2026 05:07
@mhduiy mhduiy requested a review from 18202781743 May 29, 2026 05:10
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

1. Added fallback condition to call initScreenLockedState() when platform
   name is non-empty, ensuring the function runs on Wayland as well
2. Previously skipped on Wayland only; now also handles empty platform
   name case that occurs in unit test environments
3. Marks intention for subsequent migration to the login1 interface

Log: Expand screen lock initialization condition to work in unit test and Wayland environments

fix(notification): 扩展屏幕锁定初始化条件以支持单元测试

1. 新增 platformName 非空时的回退条件,确保在 Wayland 上也能调用
   initScreenLockedState()
2. 原先仅在 Wayland 时跳过;现在同时处理单元测试环境中
   platformName 为空的情况
3. 标注了后续迁移至 login1 接口的意图

Log: 扩展屏幕锁定初始化条件,兼容单元测试和 Wayland 环境
@mhduiy
Copy link
Copy Markdown
Contributor Author

mhduiy commented May 29, 2026

/forcemerge

@deepin-bot
Copy link
Copy Markdown

deepin-bot Bot commented May 29, 2026

This pr force merged! (status: blocked)

@deepin-bot deepin-bot Bot merged commit 1fe247f into linuxdeepin:master May 29, 2026
8 of 12 checks passed
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeeX,你的智能编程助手。我已仔细审查了你提供的Git Diff代码。下面我将从语法逻辑、代码质量、代码性能和代码安全四个方面为你提供详细的审查意见和改进建议。

总体评价

这次修改的主要目的是在非Wayland平台且平台名称不为空的情况下初始化屏幕锁定状态,以避免在单元测试等没有图形平台的环境下引发错误。修改方向是正确的,但实现细节上存在一些可以优化的空间。


一、 语法逻辑

  1. 多次调用 QGuiApplication::platformName()

    • if 条件判断中,QGuiApplication::platformName() 被调用了两次。虽然这在逻辑上不会出错,但如果平台名在两次调用之间发生变化(尽管在常规应用中极不可能),可能会导致逻辑不一致。
    • 建议:将平台名提取到一个局部变量中,既保证逻辑的原子性,也提高可读性。
  2. 条件判断的顺序

    • 当前逻辑是:不是wayland && 平台名不为空
    • 从防御性编程的角度来看,应该先检查平台名是否为空(前置条件),再检查其具体值。如果平台名为空,后面的相等性比较也就没有意义了。
    • 建议:调整条件顺序为 !platformName.isEmpty() && platformName != QStringLiteral("wayland")
  3. 注释说明

    • 注释写着 // for unit test, Subsequent migration to the login1 interface。这对理解代码意图很有帮助,但“Subsequent migration to the login1 interface”属于 TODO 性质,建议使用更规范的 TODO 标记,方便后续追踪。

二、 代码质量

  1. 代码可读性
    • QGuiApplication::platformName() 赋值给局部 const 变量,可以使得 if 语句更加简洁明了,降低认知负担。
  2. 魔法值/硬编码
    • QStringLiteral("wayland") 在代码中可能是多次出现的硬编码字符串。如果在同一个类或项目中频繁使用,建议提取为静态常量。

三、 代码性能

  1. 函数调用开销
    • QGuiApplication::platformName() 返回的是 QString,连续调用两次会触发两次 QString 的构造、拷贝(或隐式共享引用计数的增减)以及底层虚函数的调用。虽然性能影响微乎其微,但将其缓存到局部变量中是一个零成本提升性能的好习惯。

四、 代码安全

  1. 空指针/崩溃风险
    • 在单元测试或无界面环境中,如果在 NotificationManager 构造函数执行前 QGuiApplication 未实例化,调用 QGuiApplication::platformName() 可能会导致未定义行为或崩溃。
    • 建议:增加对 QGuiApplication 实例是否存在的检查(qApp 宏)。

改进后的代码建议

结合以上分析,我为你提供一份优化后的代码参考:

diff --git a/panels/notification/server/notificationmanager.cpp b/panels/notification/server/notificationmanager.cpp
index a02efa10a..c1d2e3f4g 100644
--- a/panels/notification/server/notificationmanager.cpp
+++ b/panels/notification/server/notificationmanager.cpp
@@ -81,7 +81,11 @@ NotificationManager::NotificationManager(QObject *parent)
     if(!config->value("notificationCleanupDays").isNull()) {
         m_cleanupDays = config->value("notificationCleanupDays").toInt();
     } 
-    if (QStringLiteral("wayland") != QGuiApplication::platformName()) {
+    // TODO: Subsequent migration to the login1 interface
+    const QString platformName = QGuiApplication::platformName(); 
+    if (!platformName.isEmpty() 
+        && platformName != QStringLiteral("wayland")) {
         initScreenLockedState();
     }
 }

更严格的安全版本(如果存在 QGuiApplication 未初始化的可能)

如果该类有可能在 QGuiApplication 构造之前被实例化(在某些测试框架或插件加载时可能发生),建议加上 qApp 的检查:

    // TODO: Subsequent migration to the login1 interface
    if (qApp) {
        const QString platformName = QGuiApplication::platformName(); 
        if (!platformName.isEmpty() 
            && platformName != QStringLiteral("wayland")) {
            initScreenLockedState();
        }
    }

修改点总结:

  1. 提取了 platformNameconst 局部变量,避免重复调用,提升性能和逻辑严密性。
  2. 调整了条件判断顺序,优先检查 !isEmpty(),更符合防御性编程思维。
  3. 规范了 TODO 注释格式,便于代码维护。
  4. (可选)增加了 qApp 的前置检查,彻底杜绝空指针或未初始化导致的崩溃风险。

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