Skip to content

Conversation

@ut003640
Copy link
Contributor

@ut003640 ut003640 commented Jan 30, 2026

…rength changes

When the network signal strength changes, the taskbar icon should be updated accordingly.

Log: Fix the issue where the taskbar signal strength icon does not update in real-time
Influence: Taskbar signal strength display
PMS: BUG-331547

Summary by Sourcery

Ensure wireless network signal strength updates propagate to the UI in real time when the active access point changes.

Bug Fixes:

  • Fix stale taskbar/network icon signal strength by listening to active access point changes and emitting strength updates accordingly.

Enhancements:

  • Synchronize access point proxy strength with the currently active access point when networks are added or updated.
  • Trigger network icon refresh when the reported signal strength level changes.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 30, 2026

Reviewer's Guide

Updates wireless access point strength handling so the taskbar/network UI reacts in real time to changes in the active access point’s signal strength, and ensures icon refresh when strength level changes.

Sequence diagram for real-time taskbar signal strength updates

sequenceDiagram
    actor User
    participant NetworkManagerWirelessDevice as NetworkManager_WirelessDevice
    participant NetworkManagerAccessPoint as NetworkManager_AccessPoint
    participant WirelessDeviceManagerRealize
    participant AccessPointInfo
    participant AccessPointProxyNM
    participant NetStatus
    participant TaskbarIcon

    User->>NetworkManagerWirelessDevice: connectToNetwork
    NetworkManagerWirelessDevice->>WirelessDeviceManagerRealize: activeAccessPointChanged
    WirelessDeviceManagerRealize->>NetworkManagerWirelessDevice: activeAccessPoint()
    NetworkManagerWirelessDevice-->>WirelessDeviceManagerRealize: activeAp
    WirelessDeviceManagerRealize->>WirelessDeviceManagerRealize: changeActiveAp
    WirelessDeviceManagerRealize->>WirelessDeviceManagerRealize: updateActiveApStrength(activeAp)
    WirelessDeviceManagerRealize->>AccessPointInfo: findByUni(activeAp.uni())
    AccessPointInfo-->>WirelessDeviceManagerRealize: apInfo
    WirelessDeviceManagerRealize->>AccessPointProxyNM: updateStrengthFromActiveAp(activeAp.signalStrength())
    AccessPointProxyNM-->>NetStatus: strengthChanged(m_strength)
    NetStatus->>NetStatus: onStrengthLevelChanged
    NetStatus->>NetStatus: updateStatus
    NetStatus->>NetStatus: updateNetworkIcon
    NetStatus-->>TaskbarIcon: setSignalStrengthIcon

    loop While connected
        NetworkManagerAccessPoint-->>WirelessDeviceManagerRealize: signalStrengthChanged
        WirelessDeviceManagerRealize->>WirelessDeviceManagerRealize: updateActiveApStrength(activeAp)
        WirelessDeviceManagerRealize->>AccessPointProxyNM: updateStrengthFromActiveAp(activeAp.signalStrength())
        AccessPointProxyNM-->>NetStatus: strengthChanged(m_strength)
        NetStatus->>NetStatus: onStrengthLevelChanged
        NetStatus->>NetStatus: updateNetworkIcon
        NetStatus-->>TaskbarIcon: setSignalStrengthIcon
    end
Loading

Class diagram for updated network strength handling

classDiagram
    class WirelessDeviceManagerRealize {
        - NetworkManager_WirelessDevice_Ptr m_device
        - QList~AccessPointInfo_ptr~ m_accessPointInfos
        - bool m_hotspotEnabled
        - ProcesserInterface_ptr m_netProcesser
        - bool m_available
        - QMetaObject_Connection m_activeApStrengthConn
        + WirelessDeviceManagerRealize(NetworkManager_WirelessDevice_Ptr device)
        + ~WirelessDeviceManagerRealize()
        + void addNetwork(NetworkManager_WirelessNetwork_Ptr network)
    }

    class AccessPointInfo {
        + AccessPointProxyNM_ptr proxy()
        + QString accessPoint()
    }

    class AccessPointProxyNM {
        - int m_strength
        + bool isWlan6() const
        + void updateStrengthFromActiveAp(int strength)
        + void strengthChanged(int strength)
    }

    class NetStatus {
        + void onStrengthLevelChanged()
        + void updateStatus()
        + void updateNetworkIcon()
    }

    class NetworkManager_WirelessDevice {
        + NetworkManager_AccessPoint_Ptr activeAccessPoint() const
        + void activeAccessPointChanged()
    }

    class NetworkManager_AccessPoint {
        + QString uni() const
        + int signalStrength() const
        + void signalStrengthChanged(int strength)
    }

    WirelessDeviceManagerRealize --> NetworkManager_WirelessDevice : uses
    WirelessDeviceManagerRealize "1" --> "*" AccessPointInfo : manages
    AccessPointInfo --> AccessPointProxyNM : has
    WirelessDeviceManagerRealize ..> AccessPointProxyNM : calls updateStrengthFromActiveAp
    NetworkManager_WirelessDevice --> NetworkManager_AccessPoint : activeAccessPoint
    NetworkManager_AccessPoint ..> WirelessDeviceManagerRealize : signalStrengthChanged
    AccessPointProxyNM ..> NetStatus : strengthChanged
    NetStatus ..> NetStatus : updateNetworkIcon
Loading

File-Level Changes

Change Details Files
Track the active Wi-Fi access point’s signal strength and propagate updates to access point proxies so UI can update in real time.
  • Introduce a lambda in the wireless device constructor to locate the AccessPointInfo corresponding to the current active access point and push its signal strength into the proxy
  • Add a changeActiveAp helper lambda that reconnects to the current active access point’s signalStrengthChanged signal and immediately applies its strength, storing the QMetaObject::Connection for later disconnection
  • Connect WirelessDevice::activeAccessPointChanged to the changeActiveAp lambda and invoke it once at initialization so the current strength is applied on startup
  • Extend addNetwork to sync the proxy’s strength from the active access point both when creating a new AccessPointInfo and when updating an existing one
  • Add AccessPointProxyNM::updateStrengthFromActiveAp to update the cached strength and emit strengthChanged only on actual changes
  • Store the active access point strength signal connection in a new m_activeApStrengthConn member on WirelessDeviceManagerRealize
src/impl/networkmanager/devicemanagerrealize.cpp
src/impl/networkmanager/devicemanagerrealize.h
src/impl/networkmanager/accesspointproxynm.cpp
src/impl/networkmanager/accesspointproxynm.h
Ensure the taskbar network icon is refreshed whenever the strength level changes.
  • Call updateNetworkIcon from NetStatus::onStrengthLevelChanged after updating status so the tray icon reflects the new strength
net-view/window/netstatus.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 left some high level feedback:

  • The logic for syncing the active access point’s strength to AccessPointProxy is duplicated in multiple places (constructor, addNetwork); consider extracting this into a small helper method to avoid divergence and make future changes less error-prone.
  • In addNetwork, the new active-AP strength update paths assume apInfo->proxy() is non-null; for consistency with the constructor loop where you check apInfo && apInfo->proxy(), it may be safer to add the same null-checks here.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The logic for syncing the active access point’s strength to `AccessPointProxy` is duplicated in multiple places (constructor, `addNetwork`); consider extracting this into a small helper method to avoid divergence and make future changes less error-prone.
- In `addNetwork`, the new active-AP strength update paths assume `apInfo->proxy()` is non-null; for consistency with the constructor loop where you check `apInfo && apInfo->proxy()`, it may be safer to add the same null-checks here.

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.

…rength changes

When the network signal strength changes, the taskbar icon should be updated accordingly.

Log: Fix the issue where the taskbar signal strength icon does not update in real-time
Influence: Taskbar signal strength display
PMS: BUG-331547
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码主要解决了无线网络信号强度更新不及时的问题,通过监听 activeAccessPoint 的信号强度变化来更新 UI。以下是对代码的详细审查和改进建议:

1. 语法逻辑

  • AccessPointProxyNM::updateStrengthFromActiveAp:
    • 逻辑正确。通过比较新旧值来避免不必要的信号发射和 UI 刷新,这是一个好的实践。
  • WirelessDeviceManagerRealize 构造函数中的 Lambda:
    • changeActiveAp 逻辑基本正确:在切换热点时断开旧连接,建立新连接。
    • updateActiveApStrength 逻辑正确:遍历列表找到对应的 AP 并更新。
  • addNetwork 方法:
    • 逻辑正确:在添加新网络或更新现有网络时,立即同步一次当前活跃 AP 的信号强度,防止初始状态不同步。

2. 代码质量

  • Lambda 捕获与生命周期:

    • 在构造函数中,m_activeApStrengthConn 的 Lambda 捕获了 activeAp (NetworkManager::AccessPoint::Ptr)。这是一个强引用(共享指针),这通常是可以的,因为 activeAp 通常由 NetworkManagerQt 管理。但为了更安全,建议捕获弱引用或者确保 activeAp 的生命周期管理明确。
    • 建议: 在 changeActiveAp 中,connect 的 lambda 捕获了 activeAp。如果 activeAp 对象在 NetworkManager 内部被销毁(虽然作为 Ptr 不太可能),但信号发送者 activeAp.data() 如果被析构,Qt 会自动断开连接。这里主要风险在于 activeAp 指针本身的生命周期。由于使用了 Ptr,这里是安全的。
  • 代码重复:

    • WirelessDeviceManagerRealize::addNetwork 中,获取 activeAp 并判断是否需要更新信号强度的代码块出现了两次(新增和更新分支)。
    • 建议: 提取为私有辅助函数,例如 syncActiveApStrength(AccessPointInfo *apInfo)
  • 连接管理:

    • changeActiveAp 中,使用了 Qt::UniqueConnection。这通常用于防止重复连接。但在 Lambda 内部,由于每次 activeAccessPointChanged 都会重新 connect,且之前的连接已被 disconnectQt::UniqueConnection 在这里虽然无害但略显多余,因为逻辑上已经保证了旧连接被断开。
    • 建议: 可以移除 Qt::UniqueConnection,依靠逻辑控制连接的唯一性。
  • 命名与注释:

    • updateActiveApStrengthchangeActiveAp 作为局部 Lambda 变量命名清晰。
    • 注释 "referenceAccessPoint 信号强度变化不及时,需要监听 activeAccessPointChanged 信号" 很好地解释了修改原因。

3. 代码性能

  • 遍历查找:

    • updateActiveApStrength 中使用了 for 循环遍历 m_accessPointInfos 来查找匹配的 AP。如果 AP 数量非常多,这可能会有效率问题(O(N))。
    • 建议: 考虑使用 QHash<QString, AccessPointInfo*> 以 AP 的 uni 为 Key 来存储,这样查找可以变为 O(1)。但这需要较大改动 m_accessPointInfos 的数据结构。如果 AP 数量通常很少(几十个以内),目前的遍历是可以接受的。
  • 信号发射频率:

    • AccessPointProxyNM::updateStrengthFromActiveAp 中有 if (strength == m_strength) return;,这有效地防止了高频信号发射导致的 UI 重绘,性能优化做得很好。

4. 代码安全

  • 空指针检查:

    • updateActiveApStrength 中检查了 apInfoproxy() 是否为空,安全性较好。
    • addNetwork 中检查了 activeAp.isNull(),安全。
  • 连接对象管理:

    • m_activeApStrengthConn 是一个 QMetaObject::Connection 对象,用于管理信号槽连接的生命周期。
    • 在析构函数或设备移除时,虽然没有显式展示析构函数中对 m_activeApStrengthConn 的处理,但 QMetaObject::Connection 在对象销毁或 disconnect 调用时会自动失效。不过,显式地在析构函数或重置设备时 disconnect(m_activeApStrengthConn) 是更严谨的做法,防止在对象销毁过程中信号槽触发导致访问已释放内存(虽然 Qt 的信号槽机制在 receiver 销毁时会自动断开,但显式断开更清晰)。

5. 具体改进代码示例

针对代码重复问题,建议修改 devicemanagerrealize.cpp

// 辅助函数
void WirelessDeviceManagerRealize::syncActiveApStrength(AccessPointInfo *apInfo)
{
    if (!apInfo) return;
    
    NetworkManager::AccessPoint::Ptr activeAp = m_device->activeAccessPoint();
    if (!activeAp.isNull() && apInfo->proxy()->contains(activeAp->uni())) {
        apInfo->proxy()->updateStrengthFromActiveAp(activeAp->signalStrength());
    }
}

// 在 addNetwork 中使用
void WirelessDeviceManagerRealize::addNetwork(const NetworkManager::WirelessNetwork::Ptr &network)
{
    // ... 前面的代码 ...

    if (itApInfo == m_accessPointInfos.end()) {
        AccessPointInfo *apInfo = new AccessPointInfo(m_device, network);
        m_accessPointInfos << apInfo;
        
        // 使用辅助函数
        syncActiveApStrength(apInfo);
        
        Q_EMIT networkAdded({ apInfo->accessPoint() });
    } else {
        AccessPointInfo *apInfo = *itApInfo;
        apInfo->proxy()->updateNetwork(network);
        
        // 使用辅助函数
        syncActiveApStrength(apInfo);
    }
}

针对 Lambda 连接的建议修改:

// 在构造函数中
auto changeActiveAp = [this] {
    if (m_activeApStrengthConn) {
        disconnect(m_activeApStrengthConn);
        m_activeApStrengthConn = QMetaObject::Connection(); // 重置连接对象
    }
    
    NetworkManager::AccessPoint::Ptr activeAp = m_device->activeAccessPoint();
    if (activeAp.isNull())
        return;

    // 更新初始强度
    auto updateFunc = [this](const NetworkManager::AccessPoint::Ptr &ap) {
        if (ap.isNull()) return;
        for (AccessPointInfo *apInfo : m_accessPointInfos) {
            if (apInfo && apInfo->proxy() && apInfo->proxy()->contains(ap->uni())) {
                apInfo->proxy()->updateStrengthFromActiveAp(ap->signalStrength());
                break;
            }
        }
    };
    
    updateFunc(activeAp);

    // 建立新连接,移除 Qt::UniqueConnection,因为逻辑上已经唯一
    m_activeApStrengthConn = connect(activeAp.data(), &NetworkManager::AccessPoint::signalStrengthChanged, 
                                     this, [updateFunc, activeAp] {
        updateFunc(activeAp);
    });
};

总结

这段代码整体质量不错,逻辑清晰,有效地解决了信号强度同步问题。主要改进点在于消除代码重复(提取辅助函数)和微调 Lambda 连接管理的写法以提高可读性。安全性方面做得较好,有必要的空指针检查。性能方面对于常规场景(AP 数量不多)没有问题。

@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

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Jan 30, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 8e8e147 into linuxdeepin:master Jan 30, 2026
16 of 18 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