Skip to content

Conversation

@wjyrich
Copy link
Contributor

@wjyrich wjyrich commented Feb 2, 2026

  1. Changed from onCurrentIndexChanged to onCurrentItemChanged for focus reset to ensure target page is loaded before setting focus
  2. Added pendingFocusIndex property to track desired focus position when switching pages
  3. Enhanced resetGridFocus function to handle focus index calculation and delayed execution via Qt.callLater
  4. Added left/right key navigation for both grid view modes (icon and list) to enable cross-page navigation
  5. Implemented circular navigation within pages and between pages
  6. Improved model count detection to handle different model types (count property vs rowCount method)

Log: Improved folder navigation with keyboard arrow keys and fixed focus issues when switching pages

Influence:

  1. Test left/right arrow key navigation within a folder page
  2. Verify focus correctly transfers when navigating between pages using arrow keys
  3. Test circular navigation when reaching page boundaries
  4. Verify focus position is maintained when switching pages
  5. Test with empty folders and single-page folders
  6. Verify both icon view and list view modes work correctly
  7. Test focus behavior when quickly switching between pages

fix: 改进文件夹网格视图导航和焦点处理

  1. 将焦点重置从 onCurrentIndexChanged 改为 onCurrentItemChanged,确保目 标页面加载完成后再设置焦点
  2. 添加 pendingFocusIndex 属性来跟踪切换页面时所需的焦点位置
  3. 增强 resetGridFocus 函数,通过 Qt.callLater 处理焦点索引计算和延迟 执行
  4. 为两种网格视图模式(图标和列表)添加左右键导航功能,支持跨页面导航
  5. 实现页面内和页面间的循环导航
  6. 改进模型数量检测,处理不同类型的模型(count 属性 vs rowCount 方法)

Log: 改进文件夹导航,支持键盘方向键导航,修复切换页面时的焦点问题

Influence:

  1. 测试文件夹页面内的左右方向键导航
  2. 验证使用方向键在页面间导航时焦点是否正确转移
  3. 测试到达页面边界时的循环导航
  4. 验证切换页面时焦点位置是否正确保持
  5. 测试空文件夹和单页文件夹的情况
  6. 验证图标视图和列表视图模式都能正常工作
  7. 测试快速切换页面时的焦点行为

PMS: BUG-333261 BUG-333255

1. Changed from onCurrentIndexChanged to onCurrentItemChanged for focus
reset to ensure target page is loaded before setting focus
2. Added pendingFocusIndex property to track desired focus position when
switching pages
3. Enhanced resetGridFocus function to handle focus index calculation
and delayed execution via Qt.callLater
4. Added left/right key navigation for both grid view modes (icon and
list) to enable cross-page navigation
5. Implemented circular navigation within pages and between pages
6. Improved model count detection to handle different model types (count
property vs rowCount method)

Log: Improved folder navigation with keyboard arrow keys and fixed focus
issues when switching pages

Influence:
1. Test left/right arrow key navigation within a folder page
2. Verify focus correctly transfers when navigating between pages using
arrow keys
3. Test circular navigation when reaching page boundaries
4. Verify focus position is maintained when switching pages
5. Test with empty folders and single-page folders
6. Verify both icon view and list view modes work correctly
7. Test focus behavior when quickly switching between pages

fix: 改进文件夹网格视图导航和焦点处理

1. 将焦点重置从 onCurrentIndexChanged 改为 onCurrentItemChanged,确保目
标页面加载完成后再设置焦点
2. 添加 pendingFocusIndex 属性来跟踪切换页面时所需的焦点位置
3. 增强 resetGridFocus 函数,通过 Qt.callLater 处理焦点索引计算和延迟
执行
4. 为两种网格视图模式(图标和列表)添加左右键导航功能,支持跨页面导航
5. 实现页面内和页面间的循环导航
6. 改进模型数量检测,处理不同类型的模型(count 属性 vs rowCount 方法)

Log: 改进文件夹导航,支持键盘方向键导航,修复切换页面时的焦点问题

Influence:
1. 测试文件夹页面内的左右方向键导航
2. 验证使用方向键在页面间导航时焦点是否正确转移
3. 测试到达页面边界时的循环导航
4. 验证切换页面时焦点位置是否正确保持
5. 测试空文件夹和单页文件夹的情况
6. 验证图标视图和列表视图模式都能正常工作
7. 测试快速切换页面时的焦点行为

PMS: BUG-333261 BUG-333255
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 @wjyrich, 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

deepin pr auto review

Git Diff 代码审查

总体评估

这段代码修改了文件夹网格视图的键盘导航行为,主要实现了跨页面导航功能(在第一页按左键跳转到最后一页,在最后一页按右键跳转到第一页),并改进了焦点管理机制。代码整体逻辑清晰,但存在一些可以改进的地方。

详细审查

1. 代码质量与逻辑

优点

  • 使用 onCurrentItemChanged 替代 onCurrentIndexChanged 是正确的改进,解决了 Loader 未完成加载时焦点设置的问题
  • 使用 Qt.callLater 延迟执行焦点设置,确保组件已加载完成
  • 添加了 pendingFocusIndex 属性来跟踪焦点状态

改进建议

  1. 代码重复问题:Keys.onLeftPressedKeys.onRightPressed 的处理逻辑在两个地方完全重复,应该提取为共享函数或组件
  2. 注释可以更清晰:pendingFocusIndex 的注释可以更详细说明其作用和可能的值
  3. 模型数量获取逻辑重复:获取模型数量的逻辑在多处重复,可以提取为辅助函数

2. 性能考虑

  • 使用 Qt.callLater 是合理的,但可以考虑添加超时机制,防止极端情况下焦点设置失败
  • resetGridFocus 中多次访问 gridViewContainerLoader.item,可以缓存引用以提高性能

3. 安全性考虑

  • 在访问 gridViewContainerLoader.item.model 时进行了空值检查,这是好的做法
  • 但在 resetGridFocus 函数中,gridViewContainerLoader 的空值检查可以更全面

4. 具体改进建议

4.1 提取重复代码

// 在组件级别定义共享函数
function getModelCount(model) {
    if (!model) return 0
    if (model.count !== undefined) return model.count
    if (model.rowCount !== undefined) return model.rowCount()
    return 0
}

function handleKeyPress(event, direction) {
    event.accepted = true
    let count = getModelCount(sortProxyModel)
    if (count === 0) return
    
    let current = folderGridViewContainer.currentIndex
    
    // 处理同一页内的导航
    if (direction === "left" && current > 0) {
        folderGridViewContainer.currentIndex = current - 1
        return
    }
    if (direction === "right" && current < count - 1) {
        folderGridViewContainer.currentIndex = current + 1
        return
    }
    
    // 处理跨页导航
    let pageCount = folderPagesView.count
    if (pageCount <= 1) {
        folderGridViewContainer.currentIndex = direction === "left" ? count - 1 : 0
        return
    }
    
    // 设置焦点标志并切换页面
    folderPagesView.pendingFocusIndex = direction === "left" ? -1 : 0
    if (direction === "left") {
        folderPagesView.setCurrentIndex(folderPagesView.currentIndex === 0 ? pageCount - 1 : folderPagesView.currentIndex - 1)
    } else {
        folderPagesView.setCurrentIndex(folderPagesView.currentIndex === pageCount - 1 ? 0 : folderPagesView.currentIndex + 1)
    }
}

然后在键盘事件处理中使用:

Keys.onLeftPressed: function(event) {
    handleKeyPress(event, "left")
}

Keys.onRightPressed: function(event) {
    handleKeyPress(event, "right")
}

4.2 改进 resetGridFocus 函数

function resetGridFocus(focusIndex) {
    if (!gridViewContainerLoader || !gridViewContainerLoader.item)
        return
    
    const container = gridViewContainerLoader.item
    container.forceActiveFocus()
    
    Qt.callLater(function() {
        if (!gridViewContainerLoader || !gridViewContainerLoader.item)
            return
            
        const count = getModelCount(gridViewContainerLoader.item.model)
        let targetIndex = 0
        
        if (count > 0) {
            if (focusIndex < 0) {
                targetIndex = count - 1
            } else {
                targetIndex = Math.min(focusIndex, count - 1)
            }
        }
        
        gridViewContainerLoader.item.currentIndex = targetIndex
        folderPagesView.pendingFocusIndex = 0
    })
}

4.3 改进注释

// 焦点索引标志,用于跨页面导航时确定焦点位置
// -1: 跨页向左导航,焦点应设置在新页面的最后一个项目
// 0: 跨页向右导航,焦点应设置在新页面的第一个项目
property int pendingFocusIndex: 0

5. 其他建议

  1. 考虑添加对键盘导航的单元测试,确保各种边界情况(空列表、单页、多页等)都能正确处理
  2. 考虑添加键盘导航的可配置性,例如允许用户禁用跨页导航功能
  3. 对于大型列表,可以考虑添加虚拟滚动优化性能

总结

这段代码实现了跨页面键盘导航功能,整体逻辑正确,但存在代码重复问题。通过提取共享函数和改进注释,可以提高代码的可维护性和可读性。性能方面,可以通过缓存引用和优化模型访问来改进。安全性方面,建议加强空值检查。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

@wjyrich wjyrich merged commit a4d25d7 into linuxdeepin:master Feb 2, 2026
10 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