Skip to content

fix: adjust focus handling in search edit widget#3723

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
Johnson-zs:master
Apr 28, 2026
Merged

fix: adjust focus handling in search edit widget#3723
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
Johnson-zs:master

Conversation

@Johnson-zs
Copy link
Copy Markdown
Contributor

@Johnson-zs Johnson-zs commented Apr 27, 2026

Removed Qt::ActiveWindowFocusReason from the focus out event condition
to prevent unnecessary widget collapse when switching windows. This
improves user experience by maintaining search widget visibility during
window switching operations.

Log: Fixed issue where search widget would collapse unexpectedly when
switching between windows

Influence:

  1. Test search widget behavior when clicking on popup menus
  2. Verify search box remains visible when switching between application
    windows
  3. Check that proper collapse behavior remains for other focus out
    reasons
  4. Test with different window managers and desktop environments

fix: 调整搜索编辑小部件的焦点处理

从焦点移出事件条件中移除了 Qt::ActiveWindowFocusReason,以防止在切换窗口
时不必要的控件折叠。这改善了用户通过窗口切换操作时保持搜索小部件可见性的
体验。

Log: 修复了在窗口间切换时搜索小工具意外折叠的问题

Influence:

  1. 测试点击弹出菜单时的搜索小部件行为
  2. 验证在切换应用程序窗口时搜索框是否保持可见
  3. 检查其他焦点移出原因是否仍保持正确的折叠行为
  4. 在不同窗口管理器和桌面环境中进行测试

Summary by Sourcery

Bug Fixes:

  • Prevent search widget from collapsing when switching between application windows while preserving non-popup focus-out collapse behavior.

Removed Qt::ActiveWindowFocusReason from the focus out event condition
to prevent unnecessary widget collapse when switching windows. This
improves user experience by maintaining search widget visibility during
window switching operations.

Log: Fixed issue where search widget would collapse unexpectedly when
switching between windows

Influence:
1. Test search widget behavior when clicking on popup menus
2. Verify search box remains visible when switching between application
windows
3. Check that proper collapse behavior remains for other focus out
reasons
4. Test with different window managers and desktop environments

fix: 调整搜索编辑小部件的焦点处理

从焦点移出事件条件中移除了 Qt::ActiveWindowFocusReason,以防止在切换窗口
时不必要的控件折叠。这改善了用户通过窗口切换操作时保持搜索小部件可见性的
体验。

Log: 修复了在窗口间切换时搜索小工具意外折叠的问题

Influence:
1. 测试点击弹出菜单时的搜索小部件行为
2. 验证在切换应用程序窗口时搜索框是否保持可见
3. 检查其他焦点移出原因是否仍保持正确的折叠行为
4. 在不同窗口管理器和桌面环境中进行测试
@deepin-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Johnson-zs

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

Copy link
Copy Markdown

@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.

Hey - I've left some high level feedback:

  • Consider documenting in-code (e.g., a short comment near the condition) why Qt::ActiveWindowFocusReason is intentionally excluded now, so future maintainers don’t reintroduce it when adjusting focus handling logic.
  • You might want to centralize the list of focus reasons that should not trigger collapse (e.g., via a small helper or static function) so it’s easier to extend or adjust this behavior consistently if new special cases arise.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider documenting in-code (e.g., a short comment near the condition) why `Qt::ActiveWindowFocusReason` is intentionally excluded now, so future maintainers don’t reintroduce it when adjusting focus handling logic.
- You might want to centralize the list of focus reasons that should not trigger collapse (e.g., via a small helper or static function) so it’s easier to extend or adjust this behavior consistently if new special cases arise.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 27, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts focus handling in the search edit widget so that only popup-related focus changes are exempt from collapse, preventing the widget from collapsing when the active window changes while preserving existing popup behavior.

Sequence diagram for updated focus out handling in SearchEditWidget

sequenceDiagram
    actor User
    participant WindowManager
    participant Application
    participant SearchEditWidget
    participant QFocusEvent

    User->>SearchEditWidget: Interact with search box

    rect rgb(230,230,255)
        User->>WindowManager: Switch to another window
        WindowManager-->>Application: Change active window
        Application->>SearchEditWidget: QFocusEvent (ActiveWindowFocusReason)
        SearchEditWidget->>QFocusEvent: reason()
        QFocusEvent-->>SearchEditWidget: ActiveWindowFocusReason
        alt reason is PopupFocusReason
            SearchEditWidget->>QFocusEvent: accept()
            SearchEditWidget->>SearchEditWidget: restoreFocusIfNeeded()
            SearchEditWidget-->>Application: Do not collapse
        else reason is ActiveWindowFocusReason
            SearchEditWidget-->>Application: Proceed with default collapse handling
        end
    end

    rect rgb(230,255,230)
        User->>Application: Open popup (e.g. context menu)
        Application->>SearchEditWidget: QFocusEvent (PopupFocusReason)
        SearchEditWidget->>QFocusEvent: reason()
        QFocusEvent-->>SearchEditWidget: PopupFocusReason
        SearchEditWidget->>QFocusEvent: accept()
        SearchEditWidget->>SearchEditWidget: restoreFocusIfNeeded()
        SearchEditWidget-->>Application: Keep widget expanded
    end
Loading

Class diagram for SearchEditWidget focus handling logic

classDiagram
    class SearchEditWidget {
        +handleFocusOutEvent(e: QFocusEvent*) void
        -restoreFocusIfNeeded() void
    }

    class QFocusEvent {
        +reason() QtFocusReason
        +accept() void
    }

    class QtFocusReason {
        <<enumeration>>
        PopupFocusReason
        ActiveWindowFocusReason
        OtherReasons
    }

    SearchEditWidget --> QFocusEvent : uses
    QFocusEvent --> QtFocusReason : returns

    %% Focus handling detail
    class FocusHandlingLogic {
        +onFocusOut(e: QFocusEvent*) void
    }

    SearchEditWidget ..> FocusHandlingLogic : delegates logic

    class FocusHandlingLogic {
        +ignoreWhenPopup(reason: QtFocusReason) bool
    }
Loading

File-Level Changes

Change Details Files
Refined focus-out handling to ignore only popup focus reasons, allowing active-window focus changes to collapse behavior to proceed normally.
  • Updated the focus-out condition to remove Qt::ActiveWindowFocusReason from the set of reasons that skip collapse handling
  • Kept Qt::PopupFocusReason as the only special case where the event is accepted and focus is restored without collapsing the widget
src/plugins/filemanager/dfmplugin-titlebar/views/searcheditwidget.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot
Copy link
Copy Markdown
Contributor

deepin pr auto review

这段代码的修改涉及 SearchEditWidget 类中的 handleFocusOutEvent 函数,主要修改了焦点丢失事件的处理逻辑。以下是对这段代码的详细审查意见:

1. 代码逻辑分析

原代码逻辑:

if (e->reason() == Qt::PopupFocusReason || e->reason() == Qt::ActiveWindowFocusReason) {
    e->accept();
    restoreFocusIfNeeded();
    return;
}

当焦点丢失原因是 PopupFocusReason(弹出窗口导致焦点丢失)或 ActiveWindowFocusReason(窗口激活导致焦点丢失)时,不触发搜索框的折叠行为,并尝试恢复焦点。

修改后代码:

if (e->reason() == Qt::PopupFocusReason) {
    e->accept();
    restoreFocusIfNeeded();
    return;
}

移除了对 ActiveWindowFocusReason 的判断,现在只有当焦点丢失原因是 PopupFocusReason 时才不触发折叠。

2. 改进意见

2.1 逻辑正确性

  • 潜在问题:移除 ActiveWindowFocusReason 的判断可能导致某些场景下的行为变化。当用户切换到另一个活动窗口时,搜索框可能会折叠,这可能是预期的行为,但也可能导致用户体验下降。
  • 建议:确认移除 ActiveWindowFocusReason 是否符合产品需求。如果确实需要移除,建议添加注释说明原因。

2.2 代码可读性

  • 改进建议:可以将焦点丢失原因提取为命名常量,提高代码可读性:
    const Qt::FocusReason kIgnoredFocusReasons = Qt::PopupFocusReason;
    if (e->reason() == kIgnoredFocusReasons) {
        e->accept();
        restoreFocusIfNeeded();
        return;
    }

2.3 代码安全性

  • 潜在风险e->accept() 会接受焦点丢失事件,但 restoreFocusIfNeeded() 可能会尝试恢复焦点。如果 restoreFocusIfNeeded() 内部逻辑不当,可能导致焦点循环或无限重绘。
  • 建议:检查 restoreFocusIfNeeded() 的实现,确保它不会导致焦点问题。

2.4 性能影响

  • 影响分析:修改后的代码减少了一次 e->reason() 的比较,性能影响可以忽略不计。

3. 最终建议

  1. 确认需求:确保移除 ActiveWindowFocusReason 是符合产品需求的修改。
  2. 添加注释:如果确实需要移除,建议添加注释说明原因,例如:
    // 只忽略弹出窗口导致的焦点丢失,允许窗口切换时折叠搜索框
    if (e->reason() == Qt::PopupFocusReason) {
        e->accept();
        restoreFocusIfNeeded();
        return;
    }
  3. 测试覆盖:确保测试以下场景:
    • 打开弹出菜单时焦点丢失。
    • 切换到另一个窗口时焦点丢失。
    • 点击其他控件时焦点丢失。

4. 修改后的代码示例

void SearchEditWidget::handleFocusOutEvent(QFocusEvent *e)
{
    if (!e) {
        return;
    }

    // 只忽略弹出窗口导致的焦点丢失,允许窗口切换时折叠搜索框
    if (e->reason() == Qt::PopupFocusReason) {
        e->accept();
        restoreFocusIfNeeded();
        return;
    }

    // 其他处理逻辑...
}

总结

这段代码的修改主要是调整了焦点丢失事件的处理逻辑,移除了对 ActiveWindowFocusReason 的特殊处理。建议确认需求后添加注释,并进行充分测试以确保行为符合预期。

@Johnson-zs
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented Apr 28, 2026

This pr force merged! (status: blocked)

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

2 participants