Skip to content

Conversation

@ComixHe
Copy link
Contributor

@ComixHe ComixHe commented Jan 31, 2026

Replace dynamic menu item insertion with visible property binding to ensure UI updates immediately when connection status changes.

Log: fix send file button not showing due to stale state
Pms: BUG-311737

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 @ComixHe, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: caixr23, ComixHe

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

Replace dynamic menu item insertion with visible property binding
to ensure UI updates immediately when connection status changes.

Log: fix send file button not showing due to stale state
Pms: BUG-311737
Signed-off-by: ComixHe <heyuming@deepin.org>
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码的修改主要是对蓝牙设备列表中"发送文件"菜单项的显示/隐藏逻辑进行了重构。以下是对该代码变更的详细审查和改进建议:

1. 代码逻辑改进

优点:

  • 移除了复杂的 takeIteminsertItem 动态操作菜单项的逻辑,改用更直观的 visible 属性控制显示
  • showSendFile 提升为 readonly 属性,防止外部修改,提高了封装性
  • 通过设置 height 为 0 和 padding 为 0 来完全隐藏菜单项,解决了原注释中提到的空白显示问题

潜在问题:

  • 虽然设置了 height: 0,但在某些情况下,菜单项可能仍会占据布局空间。建议同时设置 implicitHeightheight
    height: sendFile.visible ? implicitHeight : 0
    implicitHeight: sendFile.visible ? undefined : 0

2. 性能优化

建议:

  1. showSendFile 的计算逻辑移到模型层,而不是在 QML 中计算,这样可以:

    • 减少重复计算
    • 统一业务逻辑
    • 便于单元测试
  2. 如果 model.connectStatus 是枚举值,建议使用具名常量代替魔法数字:

readonly property bool showSendFile: model.canSendFile && model.connectStatus === BlueToothModel.Connected

3. 代码安全

建议:

  1. 添加对 model 的 null 检查:

    readonly property bool showSendFile: model ? (model.canSendFile && model.connectStatus === 2) : false
  2. 考虑添加对 itemCtl 的 null 检查,虽然在这个上下文中不太可能为 null:

    visible: itemCtl ? itemCtl.showSendFile : false

4. 代码质量

建议:

  1. 菜单项的 ID sendFile 与其 visible 属性的引用形成了循环依赖(虽然 Qt/QML 能处理这种情况,但不够清晰):

    // 更清晰的写法
    id: sendFileItem
    visible: itemCtl.showSendFile
    height: visible ? implicitHeight : 0
  2. 考虑将菜单项的样式提取为单独的组件,特别是如果其他地方也有类似的菜单项。

5. 改进后的代码示例

D.ItemDelegate {
    id: itemCtl
    readonly property bool showSendFile: model ? (model.canSendFile && model.connectStatus === BlueToothModel.Connected) : false
    
    // ... 其他属性 ...

    D.Menu {
        id: contextMenu
        
        D.MenuItem {
            id: sendFileItem
            padding: 0
            text: qsTr("Send Files")
            visible: itemCtl.showSendFile
            height: visible ? implicitHeight : 0
            implicitHeight: visible ? undefined : 0
            topPadding: visible ? undefined : 0
            bottomPadding: visible ? undefined : 0

            onTriggered: {
                fileDlg.open()
            }
        }
        
        // ... 其他菜单项 ...
    }
}

总结

这次修改总体上是积极的,简化了代码逻辑并解决了可见性问题。主要改进方向是:

  1. 进一步确保完全隐藏不可见的菜单项
  2. 将业务逻辑移到模型层
  3. 添加必要的 null 检查
  4. 使用具名常量代替魔法数字
  5. 避免循环依赖,提高代码可读性

@ComixHe
Copy link
Contributor Author

ComixHe commented Jan 31, 2026

/forcemerge

@deepin-bot
Copy link

deepin-bot bot commented Jan 31, 2026

This pr force merged! (status: blocked)

@deepin-bot deepin-bot bot merged commit da2d6a7 into linuxdeepin:master Jan 31, 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