Skip to content

fix: set focus to search edit when launcher becomes visible#729

Closed
Ivy233 wants to merge 1 commit intolinuxdeepin:masterfrom
Ivy233:fix/search-edit-focus-on-show
Closed

fix: set focus to search edit when launcher becomes visible#729
Ivy233 wants to merge 1 commit intolinuxdeepin:masterfrom
Ivy233:fix/search-edit-focus-on-show

Conversation

@Ivy233
Copy link
Contributor

@Ivy233 Ivy233 commented Mar 10, 2026

When the launcher is shown, the keyboard focus remains on the root InputEventItem instead of the search edit. This causes two issues:

  1. The cursor does not appear in the search bar on each open.
  2. The IME candidate window is not visible during the preedit phase (e.g. typing pinyin), because InputEventItem is not a text input widget and cannot provide cursor geometry for IME positioning.

Fix by calling forceActiveFocus() on the search edit when the launcher becomes visible, for both windowed and fullscreen modes.

修复启动器显示时搜索框未获得焦点的问题。启动器打开时键盘焦点停留在根节点
InputEventItem 上而非搜索框,导致光标不在搜索栏,且中文输入法预编辑阶段
(如拼音输入时)候选框不可见。在启动器可见时对搜索框调用 forceActiveFocus()
以修复此问题,同时修复窗口模式和全屏模式。

PMS: BUG-301743

When the launcher is shown, the keyboard focus remains on the root
InputEventItem instead of the search edit. This causes two issues:
1. The cursor does not appear in the search bar on each open.
2. The IME candidate window is not visible during the preedit phase
   (e.g. typing pinyin), because InputEventItem is not a text input
   widget and cannot provide cursor geometry for IME positioning.

Fix by calling forceActiveFocus() on the search edit when the launcher
becomes visible, for both windowed and fullscreen modes.

修复启动器显示时搜索框未获得焦点的问题。启动器打开时键盘焦点停留在根节点
InputEventItem 上而非搜索框,导致光标不在搜索栏,且中文输入法预编辑阶段
(如拼音输入时)候选框不可见。在启动器可见时对搜索框调用 forceActiveFocus()
以修复此问题,同时修复窗口模式和全屏模式。

PMS: BUG-301743
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 @Ivy233, 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: Ivy233

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

@deepin-ci-robot
Copy link

deepin pr auto review

这份代码变更主要涉及启动器在可见性变化时的焦点管理和清理逻辑。以下是对代码的详细审查和改进建议:

代码审查

1. 逻辑变更分析

  • 原逻辑:当启动器可见时直接返回,不可见时执行清理操作(清空搜索框、重置焦点等)。
  • 新逻辑:当启动器可见时,强制搜索框获取焦点并返回;不可见时执行清理操作。
  • 目的:确保启动器显示时搜索框自动获得焦点,提升用户体验。

2. 语法与逻辑检查

  • 语法正确,符合 QML 规范。
  • 逻辑清晰,通过 if-else 结构明确区分了可见和不可见时的处理逻辑。

3. 代码质量

  • 优点
    • 使用了显式的 if-else 结构,比原来的单 if 更清晰。
    • 注释保留了原意,帮助理解代码意图。
  • 改进点
    • 两个文件中的变更逻辑类似,可以考虑提取为公共函数或组件,减少重复代码。
    • forceActiveFocus() 可能会在某些情况下失败(如组件未加载完成),建议增加错误处理或日志记录。

4. 性能

  • forceActiveFocus() 是轻量级操作,对性能影响可忽略。
  • 清理逻辑(清空搜索框、重置焦点)仅在启动器隐藏时执行,频率较低,性能影响不大。

5. 安全性

  • 没有明显的安全隐患。
  • forceActiveFocus() 不会引入安全问题,但需注意避免在不可见或未完全加载的组件上调用。

改进建议

1. 增加错误处理

if (LauncherController.visible) {
    if (searchEdit) {  // 确保组件存在
        searchEdit.forceActiveFocus()
    }
    return
}

2. 提取公共逻辑

如果多个文件中需要类似的逻辑,可以定义一个公共函数:

// 在公共 JS 文件中
function handleLauncherVisibleChanged(searchEdit) {
    if (LauncherController.visible) {
        if (searchEdit) {
            searchEdit.forceActiveFocus()
        }
        return
    }
    // 清理逻辑
    searchEdit.text = ""
    // 其他清理操作...
}

3. 增加日志记录

if (LauncherController.visible) {
    console.debug("Launcher visible, forcing focus on searchEdit")
    searchEdit.forceActiveFocus()
    return
}

4. 考虑异步加载

如果 searchEdit 是动态加载的,可能需要延迟焦点设置:

if (LauncherController.visible) {
    Qt.callLater(() => {
        if (searchEdit) {
            searchEdit.forceActiveFocus()
        }
    })
    return
}

最终优化后的代码示例

FullscreenFrame.qml

Connections {
    target: LauncherController
    function onVisibleChanged() {
        if (LauncherController.visible) {
            if (searchEdit) {  // 安全检查
                searchEdit.forceActiveFocus()
            }
            return
        }

        // 清理逻辑
        searchEdit.text = ""
        if (listviewPage.currentItem) {
            // 其他清理操作...
        }
    }
}

WindowedFrame.qml

Connections {
    target: LauncherController
    function onVisibleChanged() {
        if (LauncherController.visible) {
            if (bottomBar.searchEdit) {  // 安全检查
                bottomBar.searchEdit.forceActiveFocus()
            }
            return
        }

        // 清理逻辑
        bottomBar.searchEdit.text = ""
        // 其他清理操作...
    }
}

总结

原代码变更逻辑正确,但可以通过增加安全检查、提取公共逻辑和日志记录来提高代码的健壮性和可维护性。改进后的代码更安全、更清晰,且易于维护。

@Ivy233 Ivy233 closed this Mar 10, 2026
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.

2 participants