Skip to content

fix(info-dialog): auto-save filename on focus loss#287

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-323761-auto-save-filename-on-focus-loss
Apr 28, 2026
Merged

fix(info-dialog): auto-save filename on focus loss#287
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-323761-auto-save-filename-on-focus-loss

Conversation

@add-uos
Copy link
Copy Markdown
Contributor

@add-uos add-uos commented Apr 28, 2026

Add focus handling to property delegates and auto-save filename when input loses focus, preventing data loss when clicking other items.

为属性委托添加焦点处理,输入框失去焦点时自动保存文件名,
防止点击其他项时数据丢失。

Log: 修复信息对话框文件名编辑失去焦点时自动保存
PMS: BUG-323761
Influence: 修复文件名编辑功能,用户在信息对话框中编辑文件名后点击其他区域时,会自动保存文件名,避免数据丢失。

Add focus handling to property delegates and auto-save filename
when input loses focus, preventing data loss when clicking other
items.

为属性委托添加焦点处理,输入框失去焦点时自动保存文件名,
防止点击其他项时数据丢失。

Log: 修复信息对话框文件名编辑失去焦点时自动保存
PMS: BUG-323761
Influence: 修复文件名编辑功能,用户在信息对话框中编辑文件名后点击其他区域时,会自动保存文件名,避免数据丢失。
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这份代码diff主要包含三个文件的修改:.gitignorePropertyActionItemDelegate.qmlPropertyItemDelegate.qml。修改涉及构建配置、QML组件的焦点管理和文件重命名逻辑。以下是对代码的详细审查和改进建议:

1. .gitignore 文件修改

修改内容:
添加了 obj-x86_64-linux-gnu/ 到忽略列表。

审查意见:

  • 逻辑与规范:修改合理。这是Debian/Ubuntu系统下常见的CMake构建输出目录,将其加入忽略列表是正确的做法。
  • 改进建议
    • 考虑到跨平台编译的可能性,建议使用更通用的匹配规则。例如 obj-*/ 可以忽略 obj-x86_64-linux-gnuobj-x86_64-w64-mingw32 等不同架构的构建目录。
    • 修改建议:
      # 原代码
      # obj-x86_64-linux-gnu/
      
      # 建议修改为
      obj-*/

2. PropertyActionItemDelegate.qml 文件修改

修改内容:

  1. 更新了版权年份。
  2. 添加了 focus: true
  3. 新增了 commitAndClose()isEditing() 函数。
  4. nameedit (TextInput) 中添加了焦点丢失处理逻辑 (onActiveFocusChanged) 和 Timer 延迟提交机制。

审查意见:

  • 版权年份:更新至2026年属于常规维护,无问题。

  • 焦点管理

    • 逻辑:添加 focus: true 是为了支持焦点转移,配合 PropertyItemDelegate 中的点击获取焦点逻辑,旨在解决编辑时焦点混乱的问题。
    • 性能:引入了一个 Timer (commitAndCloseTimer),间隔为10ms。虽然间隔很短,但在高频操作下可能会有微小的性能开销。不过为了解决焦点竞争导致的UI闪烁或逻辑错误,这种延迟通常是必要的权衡。
    • 代码质量与逻辑风险(重点)
      • 冗余逻辑onActiveFocusChanged 中调用了 commitAndCloseTimer.restart(),而 Timer 的 onTriggered 中又调用了 dealShowPicLabelClick()。但是,onEditingFinished 信号中也调用了 dealShowPicLabelClick()。这可能导致重复提交或逻辑冲突。
      • commitAndClose 函数未被直接使用:定义了 commitAndClose 函数,但在 Timer 的 onTriggeredonEditingFinished 中实际调用的是旧的 dealShowPicLabelClick()dealShowPicLabelClick 内部逻辑是切换 showPicLabel.visible,并没有包含 commitAndClose 中的文件重命名逻辑(IV.FileControl.slotFileReName)。
      • 严重逻辑缺陷:根据 diff,commitAndClose 包含了 IV.FileControl.slotFileReName(保存文件名)的逻辑,但当前的焦点丢失处理流程(Timer -> dealShowPicLabelClick并没有调用 commitAndClose。这意味着当用户点击外部导致输入框失去焦点时,文件重命名操作可能根本不会发生,只是关闭了编辑框。
  • 改进建议

    1. 统一提交逻辑:将保存文件名的核心逻辑放在 commitAndClose 中,并确保所有触发提交的路径(Timer、EditingFinished、回车键等)都调用此函数。
    2. 移除 onEditingFinished 的重复调用onEditingFinished 通常在回车或失去焦点时触发。既然已经使用了 onActiveFocusChanged + Timer 的组合来精确控制失去焦点的行为,建议移除 onEditingFinished 中的逻辑,或者在其中进行严格的状态判断,避免双重触发。
    3. 修正 Timer 触发器
      Timer {
          id: commitAndCloseTimer
          interval: 10 // 或者稍长一点,如 50ms,确保更稳定
          onTriggered: {
              // 确保焦点确实丢失且处于编辑模式
              if (!nameedit.activeFocus && nameedit.visible && !showPicLabel.visible) {
                  commitAndClose(); // 修改为调用新的提交函数
              }
          }
      }
    4. 安全性:在 commitAndClose 中,检查 name.length > 0 是好的,但建议增加对非法字符(如 /, \ 等)的检查,防止文件系统错误。

3. PropertyItemDelegate.qml 文件修改

修改内容:

  1. 更新了版权年份。
  2. 添加了 focus: true
  3. 添加了一个覆盖全组件的 MouseArea,点击时调用 control.forceActiveFocus()

审查意见:

  • 逻辑:这是一个典型的"点击空白处取消焦点/重置状态"的实现。通过 forceActiveFocus 将焦点从编辑框(PropertyActionItemDelegate)转移到当前项,从而触发编辑框的 onActiveFocusChanged 事件,进而保存更改。
  • 代码质量
    • MouseArea 设置了 cursorShape: Qt.ArrowCursor,这是正确的,防止鼠标悬停时变成手型,误导用户认为这是一个按钮。
    • 潜在问题:如果在 PropertyActionItemDelegate 内部也有复杂的点击交互(例如按钮),这个覆盖全父控件的 MouseArea 可能会拦截事件。但从代码结构看,PropertyActionItemDelegate 是作为列表项使用的,点击行本身通常意味着选中或取消编辑,因此这里的实现是合理的。
  • 改进建议
    • 如果 PropertyItemDelegate 内部包含其他可点击元素(如复选框、按钮),需要确保 MouseArea 不会阻止它们的点击。可以通过 propagateComposedEvents: true 或在子元素上设置更高的 z 值来解决。目前代码看起来是纯展示项,暂无此问题。

总结与核心修改建议

这份 diff 的主要目的是实现**"点击外部区域自动保存并关闭文件名编辑框"**的功能。

最关键的修改建议
PropertyActionItemDelegate.qml 中,必须修改 Timer 的 onTriggered 逻辑,使其调用 commitAndClose() 而不是 dealShowPicLabelClick()。否则,新写的文件重命名逻辑将失效,导致用户修改文件名后点击外部,编辑框关闭但文件名未保存。

建议修改后的 PropertyActionItemDelegate.qml 片段:

// 在 nameedit 内部
onActiveFocusChanged: {
    if (!activeFocus && visible && !showPicLabel.visible) {
        commitAndCloseTimer.restart();
    }
}

// 移除或修改 onEditingFinished,避免冲突
// onEditingFinished: { ... } 

Timer {
    id: commitAndCloseTimer
    interval: 50 // 稍微增加间隔以提高稳定性
    onTriggered: {
        if (!nameedit.activeFocus && nameedit.visible && !showPicLabel.visible) {
            // 核心修改:调用包含重命名逻辑的函数
            commitAndClose(); 
        }
    }
}

其他方面的修改(.gitignore、焦点转移机制)大体上是合理且必要的。

@deepin-ci-robot
Copy link
Copy Markdown

[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
Copy Markdown
Contributor Author

add-uos commented Apr 28, 2026

/forcemerge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented Apr 28, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit 7fd908d into linuxdeepin:master Apr 28, 2026
20 of 21 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