Skip to content

Conversation

@add-uos
Copy link
Contributor

@add-uos add-uos commented Jan 21, 2026

Add functionality to display the current image filename in the window title when an image source is loaded. The title now shows the filename along with its file extension, providing better user context about the currently displayed image.

log: show image filename in window title
bug: https://pms.uniontech.com/bug-view-336755.html

Add functionality to display the current image filename in the window
title when an image source is loaded. The title now shows the filename
along with its file extension, providing better user context about the
currently displayed image.

log: show image filename in window title
bug: https://pms.uniontech.com/bug-view-336755.html
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码是在 QML 文件中修改了窗口初始化逻辑,增加了根据当前图片源设置窗口标题的功能。以下是对这段代码的详细审查和改进建议:

1. 语法逻辑审查

  • 现状:代码逻辑基本正确,在窗口完成显示(onCompleted)时,检查 IV.GControl.currentSource 是否为空字符串,如果不为空,则调用函数获取文件名和后缀,并拼接赋值给 window.title
  • 潜在问题
    • toString() 的必要性IV.GControl.currentSource 通常是一个 url 类型的 QML 属性。虽然调用 toString() 是安全的,但在 QML 中,url 类型可以直接与字符串比较,或者检查其是否为空。
    • 空值判断:仅仅检查是否不等于 ""(空字符串)可能不够健壮。如果 currentSourceundefined 或者无效的 URL,虽然通常不会等于空字符串,但后续的 slotGetFileName 可能会返回非预期结果。

2. 代码质量审查

  • 现状:使用了注释解释了代码意图,这很好。
  • 改进建议
    • 逻辑封装:设置标题的逻辑可能会在多个地方触发(例如打开新图片时)。建议将设置标题的逻辑封装成一个函数,或者绑定到属性上,以便复用。
    • 可读性:拼接字符串的代码较长,可以考虑稍微优化格式或使用字符串模板(如果 QML 环境支持,如 Qt 5.12+ 的 qsTr 或标准 JS 模板字符串)。

3. 代码性能审查

  • 现状:在启动时执行一次,性能影响微乎其微。
  • 改进建议
    • 如果 slotGetFileNameslotFileSuffix 涉及复杂的文件系统 I/O 操作(虽然从命名看像是在内存中处理 URL),频繁调用可能会有性能开销。但在 onCompleted 中调用一次是可以接受的。
    • 如果 IV.GControl.currentSource 是一个动态变化的属性,更好的做法是使用 属性绑定,这样当源改变时标题会自动更新,无需手动调用。

4. 代码安全审查

  • 现状:直接拼接文件名和后缀。
  • 潜在风险
    • 如果文件名包含特殊字符,虽然通常不会导致 QML 注入,但在某些 UI 渲染或底层窗口管理器处理时可能会出现显示异常。
    • 如果 slotGetFileNameslotFileSuffix 返回空值或非字符串类型,会导致标题变成 "undefined" 或 "null"。

综合改进建议代码

建议使用 属性绑定 来替代命令式的 if 判断,这样代码更符合 QML 的声明式风格,且能自动响应数据变化。

方案一:使用属性绑定(推荐)

ApplicationWindow {
    id: window
    
    // 使用绑定自动更新标题
    title: {
        var source = IV.GControl.currentSource.toString();
        if (source !== "" && source !== "undefined") {
            return IV.FileControl.slotGetFileName(IV.GControl.currentSource) + 
                   IV.FileControl.slotFileSuffix(IV.GControl.currentSource);
        }
        return "默认应用标题"; // 或者返回 IV.FileControl.getAppName() 等
    }

    Component.onCompleted: {
        setX(IV.FileControl.getPrimaryScreenCenterX(width));
        setY(IV.FileControl.getPrimaryScreenCenterY(height));
    }
    // ...
}

方案二:如果必须保留在 onCompleted 中(例如仅初始化时设置)

如果只在启动时检查,后续通过其他信号更新标题,建议优化判断逻辑:

    Component.onCompleted: {
        setX(IV.FileControl.getPrimaryScreenCenterX(width));
        setY(IV.FileControl.getPrimaryScreenCenterY(height));

        // 优化后的判断逻辑
        var source = IV.GControl.currentSource;
        if (source && source.toString() !== "") {
            var fileName = IV.FileControl.slotGetFileName(source);
            var suffix = IV.FileControl.slotFileSuffix(source);
            // 简单的防空值保护
            if (fileName && suffix) {
                window.title = fileName + suffix;
            }
        }
    }

总结

原代码逻辑上是可用的,但不够“QML 化”。最大的改进点在于利用 QML 的 属性绑定机制 来处理 UI 状态与数据的同步,这比在生命周期钩子中手动修改属性更健壮、更易维护。同时,增加对返回值的防空值检查可以提高代码的健壮性。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, 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

@add-uos
Copy link
Contributor Author

add-uos commented Jan 22, 2026

/merge

@deepin-bot deepin-bot bot merged commit 8f94869 into linuxdeepin:master Jan 22, 2026
20 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