Skip to content

Conversation

@dengzhongyuan365-dev
Copy link
Contributor

@dengzhongyuan365-dev dengzhongyuan365-dev commented Feb 5, 2026

  • Adjusted the drop area anchoring to fill the parent regardless of control type.
  • Enhanced the import logic for Library type to default to importing to all music if the target index is invalid.
  • Added bounds checking for the drag flag property to prevent errors during drag-and-drop operations.

log: improve drag-and-drop functionality in SideBarItem

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

Summary by Sourcery

Improve sidebar drag-and-drop handling to be more robust and consistent across item types.

Bug Fixes:

  • Ensure the sidebar drop area always fills its parent so drag-and-drop works consistently for all control types.
  • Default library imports to the all-music target when the drop index is invalid to avoid incorrect or failed imports.
  • Guard drag flag updates with bounds checking on the target index to prevent errors during drag-and-drop exit handling.

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 5, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

SideBarItem drag-and-drop behavior is hardened by making the drop area always cover its parent, safely handling invalid target indices for library imports, and guarding dragFlag updates with bounds checks.

Sequence diagram for drag-and-drop import with Library default target

sequenceDiagram
    actor User
    participant SideBarItemDropArea as SideBarItemDropArea
    participant SideModel as SideModel
    participant Presenter as Presenter

    User->>SideBarItemDropArea: Drop files (drop.urls)
    SideBarItemDropArea->>SideBarItemDropArea: Build urlList from drop.urls
    SideBarItemDropArea->>SideBarItemDropArea: Determine toIndex
    alt Valid toIndex
        SideBarItemDropArea->>SideModel: get(toIndex)
        SideModel-->>SideBarItemDropArea: item with uuid
        SideBarItemDropArea->>Presenter: importMetas(urlList, uuid)
    else Invalid toIndex
        SideBarItemDropArea->>SideBarItemDropArea: targetUuid = all
        SideBarItemDropArea->>Presenter: importMetas(urlList, all)
    end
    Presenter-->>SideBarItemDropArea: Import completed
Loading

Sequence diagram for dragFlag reset with bounds checking

sequenceDiagram
    actor User
    participant SideBarItemDropArea as SideBarItemDropArea
    participant SideModel as SideModel

    User->>SideBarItemDropArea: Drag leaves drop area
    SideBarItemDropArea->>SideBarItemDropArea: onExited triggered
    alt Valid toIndex
        SideBarItemDropArea->>SideModel: setProperty(toIndex, dragFlag, false)
        SideModel-->>SideBarItemDropArea: dragFlag updated
    else Invalid toIndex
        SideBarItemDropArea->>SideBarItemDropArea: Skip dragFlag update
    end
    SideBarItemDropArea->>SideBarItemDropArea: scrollDownTimer.stop()
    SideBarItemDropArea->>SideBarItemDropArea: scrollUpTimer.stop()
Loading

File-Level Changes

Change Details Files
Make the drop area responsive for all sidebar item types by always filling the parent.
  • Removed the conditional anchoring that only filled the parent for playlist-type controls
  • Set the drop area anchors to always fill the parent layout element
src/music-player/musicbaseandsonglist/SideBarItem.qml
Harden import behavior when dropping onto Library items by defaulting invalid indices to the "all" library target.
  • Introduce a targetUuid variable defaulting to the "all" library UUID when handling drops
  • Add bounds checking on toIndex before reading sideModel entries
  • Pass the resolved targetUuid to Presenter.importMetas instead of always using sideModel.get(toIndex).uuid
src/music-player/musicbaseandsonglist/SideBarItem.qml
Prevent drag-and-drop errors by validating the target index before updating the dragFlag state.
  • Wrap sideModel.setProperty(toIndex, "dragFlag", false) in an index bounds check against sideModel.count
  • Ensure scroll timers are still stopped regardless of index validity
src/music-player/musicbaseandsonglist/SideBarItem.qml

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 found 1 issue, and left some high level feedback:

  • The toIndex bounds check is now duplicated in both the import logic and onExited; consider extracting a small helper or using a shared utility to avoid repeated index validation logic.
  • The hard-coded "all" UUID used as the default import target could be made a named constant or derived from the model to avoid reliance on a magic string.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `toIndex` bounds check is now duplicated in both the import logic and `onExited`; consider extracting a small helper or using a shared utility to avoid repeated index validation logic.
- The hard-coded "all" UUID used as the default import target could be made a named constant or derived from the model to avoid reliance on a magic string.

## Individual Comments

### Comment 1
<location> `src/music-player/musicbaseandsonglist/SideBarItem.qml:163-168` </location>
<code_context>
                         urlList.push(drop.urls[i])

-                    Presenter.importMetas(urlList, sideModel.get(toIndex).uuid)
+                    // 对于 Library 类型,如果 toIndex 无效,则默认导入到所有音乐
+                    var targetUuid = "all"
+                    if (toIndex >= 0 && toIndex < sideModel.count) {
+                        targetUuid = sideModel.get(toIndex).uuid
+                    }
+                    Presenter.importMetas(urlList, targetUuid)
                 }
             }
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Defaulting to targetUuid = "all" is not constrained to Library type in code

This logic now applies the `"all"` default whenever `toIndex` is invalid, regardless of `control.type` or the model. If this handler is reused by non-Library sidebars, they would also import into the "all" bucket. Please constrain this behavior (e.g., by checking `control.type`/model type) or use a type-specific default so other sections aren’t affected.

Suggested implementation:

```
            onEntered: {
                console.log("onEntered.............", dragForSort)
                for (var i = 0; i < drop.urls.length; i++)
                    urlList.push(drop.urls[i])

                var hasValidIndex = toIndex >= 0 && toIndex < sideModel.count
                var targetUuid = ""

                if (hasValidIndex) {
                    targetUuid = sideModel.get(toIndex).uuid
                } else if (control.type === "Library") {
                    // 对于 Library 类型,如果 toIndex 无效,则默认导入到所有音乐
                    targetUuid = "all"
                } else {
                    console.warn("Drop ignored: invalid toIndex for non-Library sidebar", toIndex)
                    return
                }

                Presenter.importMetas(urlList, targetUuid)
            }

```

This change assumes that a `control` object with a `type` property is available in this QML context and that `"Library"` is the exact value used for the library sidebar. If the actual identifier/value differs (e.g. `control.sectionType`, `"library"`), update the `control.type === "Library"` comparison accordingly.
</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.

- Adjusted the drop area anchoring to fill the parent regardless of control type.
- Enhanced the import logic for Library type to default to importing to all music if the target index is invalid.
- Added bounds checking for the drag flag property to prevent errors during drag-and-drop operations.

log: improve drag-and-drop functionality in SideBarItem

bug: https://pms.uniontech.com/bug-view-309621.html
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码是对 QML 中侧边栏项(SideBarItem)的拖放功能进行改进的补丁。主要改进了边界检查和异常处理逻辑。下面我将从多个维度进行详细审查:

1. 语法逻辑审查

优点:

  • 修复了 anchors.fill 的逻辑:之前只有当 control.type === "playlists" 时才填充父容器,现在统一填充父容器,这使得拖放区域更加一致。
  • 增加了 toIndex 的有效性检查:在 onDroppedonExited 中都添加了 toIndex 的边界检查,防止数组越界错误。
  • 增加了对 library 类型的特殊处理:当 control.type === "library" 时,即使 toIndex 无效,也会使用 "all" 作为目标 UUID。

改进建议:

  • 可以考虑将 hasValidIndex 的检查逻辑提取为一个函数,避免重复代码。
  • targetUuid 的赋值逻辑可以进一步简化,使用三元运算符可能更简洁。

2. 代码质量审查

优点:

  • 增加了 console.warn 输出,有助于调试和日志记录。
  • 变量命名清晰,如 hasValidIndextargetUuid
  • 代码结构清晰,逻辑分层明确。

改进建议:

  • 可以考虑将 "all" 这样的魔法字符串定义为常量,提高代码可维护性。
  • urlList 的构建逻辑可以优化,使用 Array.from 或展开运算符可能更简洁。

3. 代码性能审查

优点:

  • 没有明显的性能问题,边界检查的开销很小。

改进建议:

  • urlList 的构建使用了循环,可以考虑使用 Array.from(drop.urls)[...drop.urls] 来提高可读性(性能差异可以忽略不计)。

4. 代码安全审查

优点:

  • 增加了边界检查,防止数组越界导致的崩溃。
  • toIndex 无效时,增加了 return 语句,避免后续代码执行。

改进建议:

  • sideModel.get(toIndex).uuid 的访问在边界检查后是安全的,但可以考虑添加 uuid 的存在性检查,防止 undefinednull 值。
  • 可以考虑对 drop.urls 进行有效性检查,确保它是一个有效的数组。

5. 改进后的代码示例

// 定义常量
readonly property string LIBRARY_UUID: "all"

// ... 其他代码 ...

onDropped: {
    if (drop.hasUrls) {
        // 使用展开运算符构建 urlList
        const urlList = [...drop.urls]
        
        // 简化边界检查逻辑
        const isValidIndex = toIndex >= 0 && toIndex < sideModel.count
        const targetUuid = isValidIndex ? sideModel.get(toIndex).uuid : 
                          (control.type === "library" ? LIBRARY_UUID : null)
        
        if (targetUuid) {
            Presenter.importMetas(urlList, targetUuid)
        } else {
            console.warn("Drop ignored: invalid toIndex for non-library sidebar", toIndex)
        }
    }
}

onExited: {
    // 简化边界检查
    if (toIndex >= 0 && toIndex < sideModel.count) {
        sideModel.setProperty(toIndex, "dragFlag", false)
    }
    scrollDownTimer.stop()
    scrollUpTimer.stop()
}

总结

这段代码的改进主要增强了健壮性和安全性,通过增加边界检查和异常处理,避免了潜在的运行时错误。建议进一步优化代码结构,减少重复逻辑,并使用常量替代魔法字符串。整体来说,这是一次有价值的改进。

@deepin-ci-robot
Copy link

[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

@dengzhongyuan365-dev
Copy link
Contributor Author

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Feb 5, 2026

This pr force merged! (status: blocked)

@deepin-bot deepin-bot bot merged commit 3778582 into linuxdeepin:master Feb 5, 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