Skip to content

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

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
Ivy233:fix/search-edit-focus-on-show
Mar 11, 2026
Merged

fix: set focus to search edit when launcher becomes visible#729
deepin-bot[bot] merged 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

@Ivy233 Ivy233 closed this Mar 10, 2026
@Ivy233 Ivy233 reopened this Mar 11, 2026
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码修改主要涉及 QML 中对启动器(Launcher)显示/隐藏状态变化的处理逻辑。修改的核心目的是在启动器显示时自动聚焦到搜索框,同时保留了隐藏时的清理逻辑。

以下是针对这段 diff 的详细审查意见:

1. 语法逻辑

审查结果:通过

  • 逻辑清晰度:修改后的代码将“显示时聚焦”和“隐藏时清理”的逻辑通过 if-else 结构(利用 return 提前退出)分离开来,比原代码单纯使用 if (visible) return 更加直观。
  • 一致性FullscreenFrame.qmlWindowedFrame.qml 两个文件的修改逻辑保持了一致,都遵循了“显示则聚焦,隐藏则清理”的模式。
  • 执行流程
    • LauncherController.visibletrue 时,强制聚焦搜索框并立即退出函数,避免执行后续的清理代码。
    • LauncherController.visiblefalse 时,继续执行原有的清空搜索文本和重置焦点的逻辑。
    • 逻辑分支覆盖完整,没有遗漏。

2. 代码质量

审查结果:良好,有微小的改进空间

  • 可读性:代码结构清晰,注释保留得当,能够准确表达代码意图。
  • 改进建议
    • FullscreenFrame.qml 中,变量名为 searchEdit,而在 WindowedFrame.qml 中变量名为 bottomBar.searchEdit。这属于两个不同文件的上下文差异,但如果项目中有统一命名规范或组件封装,建议确认 searchEdit 是否应该作为属性统一暴露,以减少对内部层级(如 bottomBar)的直接依赖。

3. 代码性能

审查结果:通过

  • 开销分析
    • forceActiveFocus() 是 QML 中改变焦点的标准方法,开销极低,仅在状态切换瞬间触发。
    • text = "" 和焦点重置操作也仅在隐藏时触发一次。
  • 无性能隐患:没有引入循环、定时器或高频调用的风险,性能影响可以忽略不计。

4. 代码安全

审查结果:通过

  • 空指针安全
    • 代码直接调用了 searchEdit.forceActiveFocus()bottomBar.searchEdit.forceActiveFocus()。这隐含了一个假设:searchEdit 对象(或 bottomBar 对象)在 LauncherController 变为可见时一定已经存在且已初始化完成。
    • 在 QML 的生命周期中,如果 Connections 的目标对象 LauncherController 是单例或长期存在的,而 searchEdit 是该 QML 文件根对象的子组件,通常情况下子组件会在根组件加载时初始化,因此风险较低。但如果 searchEdit 是动态创建或延迟加载的(例如在 Loader 中),则需要在调用前检查对象是否存在(如 if (searchEdit) ...)。
  • 状态一致性:逻辑确保了显示时聚焦、隐藏时清空,避免了 UI 状态与逻辑状态不一致的情况(例如显示后焦点不在搜索框)。

总结与建议

这段代码修改是安全且合理的,成功实现了启动器显示时自动聚焦搜索框的功能,且没有破坏原有的清理逻辑。

建议

  1. 确认对象生命周期:请确保 searchEdit(全屏模式)和 bottomBar.searchEdit(窗口模式)在 onVisibleChanged 触发时已经完成构造。如果这些组件有可能在 LauncherController 变为可见时还未准备好,建议添加非空判断:
    if (LauncherController.visible) {
        if (searchEdit) searchEdit.forceActiveFocus()
        return
    }
  2. 焦点策略:目前的逻辑是“强制聚焦”。如果用户在启动器显示前正在操作其他应用,强制聚焦可能会打断用户操作。不过,对于启动器这种显式由用户呼出的界面,强制聚焦通常是符合预期的交互行为。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

@Ivy233
Copy link
Contributor Author

Ivy233 commented Mar 11, 2026

/merge

@deepin-bot deepin-bot bot merged commit a8dd424 into linuxdeepin:master Mar 11, 2026
17 of 18 checks passed
@Ivy233 Ivy233 deleted the fix/search-edit-focus-on-show branch March 11, 2026 05:33
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