Skip to content

Conversation

@BLumia
Copy link
Member

@BLumia BLumia commented Nov 4, 2025

为 ItemModel 类添加了一些兼容目前接口行为的接口,以供临时切换任务栏模型到 ItemModel 所需. (此 PR 中未进行实际的切换)

Summary by Sourcery

Introduce a compatibility layer in ItemModel for temporarily switching back to the legacy taskbar model by exposing request* action methods, update role definitions to use shared constants, and refactor TaskManager to forward item requests through its generic dataModel interface.

New Features:

  • Add requestActivate, requestNewInstance, requestClose, requestOpenUrls, requestWindowsView, and requestUpdateWindowIconGeometry methods to ItemModel for legacy model compatibility

Enhancements:

  • Replace hard-coded role names and enums in ItemModel with shared MODEL_* constants
  • Adjust ItemModel Roles enum to align with the new constants
  • Refactor TaskManager to call request methods via dataModel() instead of m_itemModel

为 ItemModel 类添加了一些兼容目前接口行为的接口,以供临时切换任务栏
模型到 ItemModel 所需. 此 PR 中未进行实际的切换.

Log:
@BLumia BLumia requested review from 18202781743 and wjyrich November 4, 2025 06:29
@sourcery-ai
Copy link

sourcery-ai bot commented Nov 4, 2025

Reviewer's Guide

Enhances ItemModel with a set of request* methods for legacy taskbar model compatibility, standardizes role names using TaskManager constants, refactors the Roles enum, and updates TaskManager to delegate interactions through the new ItemModel interface—preparing for a temporary switch back to the legacy model without performing it.

Sequence diagram for TaskManager delegating requests to ItemModel

sequenceDiagram
    participant TM as TaskManager
    participant IM as ItemModel
    TM->>IM: requestActivate(index)
    TM->>IM: requestOpenUrls(index, urls)
    TM->>IM: requestNewInstance(index, action)
    TM->>IM: requestClose(index, force)
    TM->>IM: requestUpdateWindowIconGeometry(index, geometry, delegate)
    TM->>IM: requestWindowsView(indexes)
    TM->>IM: requestOpenUrls(index, urls) (from dropFilesOnItem)
Loading

Class diagram for updated ItemModel and TaskManager interfaces

classDiagram
    class ItemModel {
        +QHash<int, QByteArray> roleNames() const
        +int rowCount(const QModelIndex &parent) const
        +QVariant data(const QModelIndex &index, int role) const
        +QJsonArray dumpDockedItems() const
        +void requestActivate(const QModelIndex &index) const
        +void requestNewInstance(const QModelIndex &index, const QString &action) const
        +void requestClose(const QModelIndex &index, bool force = false) const
        +void requestOpenUrls(const QModelIndex &index, const QList<QUrl> &urls) const
        +void requestWindowsView(const QModelIndexList &indexes) const
        +void requestUpdateWindowIconGeometry(const QModelIndex &index, const QRect &geometry, QObject *delegate = nullptr) const
        +QPointer<AbstractItem> getItemById(const QString& id) const
        -void onItemDestroyed()
        -void onItemChanged()
    }
    class TaskManager {
        +void requestActivate(const QModelIndex &index) const
        +void requestOpenUrls(const QModelIndex &index, const QList<QUrl> &urls) const
        +void requestNewInstance(const QModelIndex &index, const QString &action) const
        +void requestClose(const QModelIndex &index, bool force) const
        +void requestUpdateWindowIconGeometry(const QModelIndex &index, const QRect &geometry, QObject *delegate) const
        +void requestWindowsView(const QModelIndexList &indexes) const
        +void dropFilesOnItem(const QString& itemId, const QStringList& urls)
    }
    TaskManager --> ItemModel : delegates request* methods
Loading

Class diagram for updated ItemModel::Roles enum

classDiagram
    class ItemModel {
        ItemIdRole : int
        DockedDirRole : int
    }
Loading

File-Level Changes

Change Details Files
Add legacy-compatible request interfaces to ItemModel
  • introduce requestActivate, requestNewInstance, requestClose, requestOpenUrls, requestWindowsView, requestUpdateWindowIconGeometry methods
  • route these methods in ItemModel to underlying AbstractItem/AppItem actions
  • include matching declarations in ItemModel header
panels/dock/taskmanager/itemmodel.cpp
panels/dock/taskmanager/itemmodel.h
Unify role names in ItemModel to use TaskManager constants
  • replace hardcoded role name strings with MODEL_* macros
  • use TaskManager::NameRole, IconNameRole, etc. in both roleNames() and data() switch
  • add clang-format directives around modified sections
panels/dock/taskmanager/itemmodel.cpp
Refactor ItemModel Roles enum to align with legacy model
  • shift ItemIdRole to Qt::UserRole + 64 and remove deprecated role entries
  • retain only ItemIdRole and DockedDirRole in Roles enum
panels/dock/taskmanager/itemmodel.h
Update TaskManager to delegate requests through dataModel()
  • replace m_itemModel->requests with dataModel()->requests in TaskManager methods
  • ensure dropFilesOnItem uses dataModel() as well
panels/dock/taskmanager/taskmanager.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

@deepin-ci-robot
Copy link

deepin pr auto review

我来对这个代码变更进行审查:

  1. 代码结构和组织:
  • 优点:将角色定义从 ItemModel 移到 TaskManager,统一管理角色定义,符合单一职责原则。
  • 改进建议:建议将所有角色常量(如 MODEL_ITEMID 等)集中在一个头文件中定义,便于统一管理和维护。
  1. 代码安全性:
  • 潜在问题:在 requestUpdateWindowIconGeometry 函数中,使用了 QPointer 进行类型转换,但没有验证转换的安全性。
  • 改进建议:应该先检查原始指针是否有效,然后再进行类型转换:
auto baseItem = ItemModel::instance()->getItemById(itemId);
if (!baseItem) return;
auto appItem = qobject_cast<AppItem*>(baseItem.get());
if (!appItem) return;
  1. 性能优化:
  • 在 requestOpenUrls 函数中,使用 std::as_const(urls) 是好的实践,避免了不必要的拷贝。
  • 建议:在 getItemById 中使用 std::find_if 的查找操作可以优化,考虑使用 QHash 来存储 m_items,将时间复杂度从 O(n) 降到 O(1)。
  1. 代码质量:
  • 优点:使用了 clang-format 注释来保持代码格式统一。
  • 改进建议:
    • requestWindowsView 函数目前是空实现,应该添加注释说明原因或实现具体功能。
    • 建议为所有新增的公共方法添加文档注释,说明其用途、参数和返回值。
  1. 逻辑一致性:
  • 优点:在 TaskManager 类中统一使用 dataModel() 替代直接访问 m_itemModel,提高了代码的一致性和可维护性。
  • 改进建议:建议检查所有使用 ItemModel::instance() 的地方,确保与 dataModel() 的使用保持一致。
  1. 错误处理:
  • 在 requestActivate、requestNewInstance 等函数中都有基本的空指针检查,这是好的实践。
  • 建议:可以考虑添加更多的错误处理,比如在 requestOpenUrls 中检查 URL 列表是否为空。
  1. 代码复用:
  • 注意到多个 request* 函数都有相似的获取 item 的逻辑,建议提取一个公共函数:
QPointer<AbstractItem> ItemModel::getValidItem(const QModelIndex &index) const {
    QString itemId = data(index).toString();
    return getItemById(itemId);
}
  1. 类型安全:
  • 在 roleNames() 和 data() 函数中使用了 TaskManager 的角色定义,但 ItemModel 中仍然保留了一些自己的角色定义,建议完全统一角色定义。

总的来说,这次改动提高了代码的组织性和可维护性,但还有一些细节可以进一步优化。建议在合并前进行充分的测试,特别是涉及到类型转换和空指针检查的部分。

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 and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `panels/dock/taskmanager/itemmodel.cpp:101` </location>
<code_context>

+void ItemModel::requestActivate(const QModelIndex &index) const
+{
+    QString itemId = data(index).toString();
+
+    auto item = ItemModel::instance()->getItemById(itemId);
</code_context>

<issue_to_address>
**issue (bug_risk):** Potential mismatch between index data and itemId retrieval.

Explicitly request ItemIdRole when retrieving data to ensure correct itemId lookup and prevent potential mismatches.
</issue_to_address>

### Comment 2
<location> `panels/dock/taskmanager/itemmodel.cpp:120` </location>
<code_context>
+        return;
+    }
+
+    item->handleClick(DOCK_ACTIN_LAUNCH);
+}
+
</code_context>

<issue_to_address>
**issue (typo):** Typo in constant name: DOCK_ACTIN_LAUNCH.

Please correct the constant name to DOCK_ACTION_LAUNCH to avoid potential runtime errors.

```suggestion
    item->handleClick(DOCK_ACTION_LAUNCH);
```
</issue_to_address>

### Comment 3
<location> `panels/dock/taskmanager/itemmodel.cpp:162` </location>
<code_context>
+{
+    QString itemId = data(index).toString();
+
+    QPointer<AppItem> item = static_cast<AppItem *>(ItemModel::instance()->getItemById(itemId).get());
+    if (item.isNull())
+        return;
</code_context>

<issue_to_address>
**issue (bug_risk):** Unsafe static_cast from AbstractItem to AppItem.

static_cast assumes getItemById always returns an AppItem, which may not be guaranteed. Use dynamic_cast and handle failed casts to avoid undefined behavior.
</issue_to_address>

### Comment 4
<location> `panels/dock/taskmanager/itemmodel.cpp:99` </location>
<code_context>
     return result;
 }

+void ItemModel::requestActivate(const QModelIndex &index) const
+{
+    QString itemId = data(index).toString();
</code_context>

<issue_to_address>
**issue (complexity):** Consider refactoring repeated item lookup and null-check logic into a single helper template that invokes a lambda for each request method.

You can collapse all the boiler-plate into a single helper that does “id → lookup → null-check” and then invokes a lambda. For example, add this private template in `ItemModel`:

```cpp
template<typename Func>
void withItem(const QModelIndex &idx, Func&& func) const {
    // directly pull itemId from the index
    const QString id = idx.data(ItemModel::ItemIdRole).toString();
    if (auto item = ItemModel::instance()->getItemById(id))
        func(item);
}
```

Then each request method becomes just a call into `withItem()`:

```cpp
void ItemModel::requestActivate(const QModelIndex &index) const {
    withItem(index, [](auto item){
        item->handleClick(QString{});
    });
}

void ItemModel::requestClose(const QModelIndex &index, bool force) const {
    withItem(index, [force](auto item){
        item->handleClick(force ? DOCK_ACTION_FORCEQUIT : DOCK_ACTION_CLOSEALL);
    });
}
```

For the URL drop you can still do conversion inside the lambda:

```cpp
void ItemModel::requestOpenUrls(const QModelIndex &index, const QList<QUrl> &urls) const {
    withItem(index, [&](auto item){
        QStringList list;
        for (auto &u : urls) list << u.toString();
        item->handleFileDrop(list);
    });
}
```

This preserves every behavior but removes the six-times repeated “get id → find → if (!item) return” logic.
</issue_to_address>

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.

return;
}

item->handleClick(DOCK_ACTIN_LAUNCH);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (typo): Typo in constant name: DOCK_ACTIN_LAUNCH.

Please correct the constant name to DOCK_ACTION_LAUNCH to avoid potential runtime errors.

Suggested change
item->handleClick(DOCK_ACTIN_LAUNCH);
item->handleClick(DOCK_ACTION_LAUNCH);

@deepin-bot
Copy link

deepin-bot bot commented Nov 6, 2025

TAG Bot

New tag: 2.0.17
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1333

@deepin-bot
Copy link

deepin-bot bot commented Nov 13, 2025

TAG Bot

New tag: 2.0.18
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1349

@deepin-bot
Copy link

deepin-bot bot commented Nov 14, 2025

TAG Bot

New tag: 2.0.19
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1350

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

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