Skip to content

fix: prevent premature dataChanged emission causing dock state desync#1462

Merged
yixinshark merged 1 commit intolinuxdeepin:masterfrom
yixinshark:fix-taskmanager
Mar 2, 2026
Merged

fix: prevent premature dataChanged emission causing dock state desync#1462
yixinshark merged 1 commit intolinuxdeepin:masterfrom
yixinshark:fix-taskmanager

Conversation

@yixinshark
Copy link
Contributor

@yixinshark yixinshark commented Mar 2, 2026

When a window is closed and its application is converted to a docked state (rather than fully removed), the dataChanged signal was previously emitted before the internal m_data index-shifting calculations were finished. This led downstream proxy models to fetch outdated mappings from m_data, resulting in empty window queries and incorrectly disappearing status indicators for other applications like the file manager. This commit defers the dataChanged emission until after all row shifts are fully processed.

修复应用窗口关闭时,任务栏其他应用指示器异常消失的问题。
之前当应用的窗口被关闭并且应用转为“未运行但驻留任务栏”的状态时,
dataChanged 信号会在内部 m_data 映射表完成下标上移更新之前被提前分发。
这导致下游的代理模型在响应该信号并刷新视图状态时,拿到了错误的偏移地址,从而读到了空的窗口记录列表, 使得原本正常的(如文件管理器等)其他应用指示点异常消失。本次提交将 dataChanged 信号的发射推迟到 内部数据表的循环移数逻辑全部执行完毕之后,确保读写同步。

Log: fix(taskmanager): prevent premature dataChanged emission causing dock state desync
Pms: BUG-350947

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.

Sorry @yixinshark, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@yixinshark yixinshark force-pushed the fix-taskmanager branch 2 times, most recently from 551ba49 to 199913e Compare March 2, 2026 08:13
@yixinshark yixinshark requested a review from BLumia March 2, 2026 08:13
BLumia
BLumia previously approved these changes Mar 2, 2026
When a window is closed and its application is converted to a docked state (rather than fully removed), the
`dataChanged` signal was previously emitted before the internal `m_data` index-shifting calculations were finished.
This led downstream proxy models to fetch outdated mappings from `m_data`,
resulting in empty window queries and incorrectly disappearing status indicators for other applications like the file manager.
This commit defers the `dataChanged` emission until after all row shifts are fully processed.

修复应用窗口关闭时,任务栏其他应用指示器异常消失的问题。
之前当应用的窗口被关闭并且应用转为“未运行但驻留任务栏”的状态时,
`dataChanged` 信号会在内部 `m_data` 映射表完成下标上移更新之前被提前分发。
这导致下游的代理模型在响应该信号并刷新视图状态时,拿到了错误的偏移地址,从而读到了空的窗口记录列表,
使得原本正常的(如文件管理器等)其他应用指示点异常消失。本次提交将 `dataChanged` 信号的发射推迟到
内部数据表的循环移数逻辑全部执行完毕之后,确保读写同步。

Log: fix(taskmanager): prevent premature dataChanged emission causing dock state desync
Pms: BUG-350947
@deepin-ci-robot
Copy link

deepin pr auto review

Git Diff 代码审查

整体评价

这段代码修改主要针对 DockGlobalElementModel 类中处理模型数据变更的逻辑进行了优化。主要改进是将数据变更通知(dataChanged信号)从在循环中立即触发改为在所有内部模型调整完成后再统一触发,这是一个很好的优化方向。

详细分析

语法逻辑

  1. 版权年份更新

    • 将版权年份从 2024 更新为 2024 - 2026,这是合理的更新,表示版权延续到2026年。
  2. 延迟信号发射

    • 引入了 pendingDataChangedRows 列表来收集需要发射 dataChanged 信号的行号
    • 将原本在循环中立即发射的信号改为在所有内部模型调整完成后统一发射
    • 添加了注释 // DEFER emitter until internal model shift is done! 解释了这一改动的原因
  3. 行索引调整

    • 保留了原有的行索引调整逻辑,确保在删除行后正确更新剩余行的索引

代码质量

  1. 优点

    • 将信号发射从循环中移出,避免了在模型数据不一致状态下触发信号
    • 添加了清晰的注释说明延迟发射的原因
    • 使用 QList<int> 收集需要更新的行号,逻辑清晰
  2. 可改进点

    • pendingDataChangedRows 可以使用 QVector<int> 替代 QList<int>,在Qt中 QVector 通常有更好的性能
    • 可以考虑使用 std::vector<int> 替代 Qt 容器,减少对 Qt 容器的依赖

代码性能

  1. 优点

    • 将信号发射从循环中移出,减少了信号发射次数,提高了性能
    • 避免了在模型数据不一致状态下触发信号导致的潜在性能问题
  2. 可改进点

    • 如果 pendingDataChangedRows 可能包含大量元素,可以考虑使用 QSet<int> 来避免重复行
    • 如果行号是连续的,可以考虑合并为单个 dataChanged 信号

代码安全

  1. 优点

    • 确保了在模型数据一致状态下才触发 dataChanged 信号
    • 避免了在模型调整过程中外部访问不一致的数据
  2. 可改进点

    • 添加对 pendingDataChangedRows 边界检查,确保行号有效
    • 考虑添加断言确保 pos 在有效范围内

改进建议

  1. 使用 QVector<int> 替代 QList<int>
QVector<int> pendingDataChangedRows;
  1. 添加边界检查:
for (int pos : pendingDataChangedRows) {
    if (pos >= 0 && pos < rowCount()) {
        auto pIndex = this->index(pos, 0);
        Q_EMIT dataChanged(pIndex, pIndex,
                          {TaskManager::ActiveRole, TaskManager::AttentionRole, 
                           TaskManager::WindowsRole, TaskManager::MenusRole, 
                           TaskManager::WinTitleRole});
    }
}
  1. 考虑合并连续的行变更:
// 合并连续的行变更
std::sort(pendingDataChangedRows.begin(), pendingDataChangedRows.end());
for (int i = 0; i < pendingDataChangedRows.size(); ) {
    int startRow = pendingDataChangedRows[i];
    int endRow = startRow;
    while (i + 1 < pendingDataChangedRows.size() && 
           pendingDataChangedRows[i + 1] == endRow + 1) {
        endRow = pendingDataChangedRows[++i];
    }
    auto startIndex = this->index(startRow, 0);
    auto endIndex = this->index(endRow, 0);
    Q_EMIT dataChanged(startIndex, endIndex,
                      {TaskManager::ActiveRole, TaskManager::AttentionRole, 
                       TaskManager::WindowsRole, TaskManager::MenusRole, 
                       TaskManager::WinTitleRole});
    i++;
}

总结

这次修改是一个很好的优化,解决了在模型数据不一致状态下触发信号的问题。通过延迟信号发射,确保了模型数据的一致性,提高了代码的健壮性。建议考虑上述改进点,进一步提高代码质量和性能。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: BLumia, yixinshark

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

@yixinshark yixinshark merged commit e3b0e78 into linuxdeepin:master Mar 2, 2026
14 of 15 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