Skip to content

fix(search): preserve text selection when mouse released outside titl…#626

Merged
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/search-edit-selection-cancel
Jun 10, 2026
Merged

fix(search): preserve text selection when mouse released outside titl…#626
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/search-edit-selection-cancel

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

…ebar

Check search edit selection before transferring focus to web view to prevent deselection caused by focus loss.

修复标题栏搜索框框选文本时,鼠标释放在标题栏外导致选中
被取消的问题,增加搜索框选中文本的判断条件。

Log: 修复搜索框框选文字被取消的问题
PMS: BUG-361733
Influence: 修复后用户在搜索框中拖拽框选文字,即使鼠标释放在标题栏外也不会丢失选中状态。

Summary by Sourcery

Bug Fixes:

  • Preserve text selection in the title bar search field when the mouse button is released outside the search area by avoiding unnecessary focus transfer to the web view.

…ebar

Check search edit selection before transferring focus to web view
to prevent deselection caused by focus loss.

修复标题栏搜索框框选文本时,鼠标释放在标题栏外导致选中
被取消的问题,增加搜索框选中文本的判断条件。

Log: 修复搜索框框选文字被取消的问题
PMS: BUG-361733
Influence: 修复后用户在搜索框中拖拽框选文字,即使鼠标释放在标题栏外也不会丢失选中状态。
@pengfeixx pengfeixx force-pushed the fix/search-edit-selection-cancel branch from 4b7694a to 81cde58 Compare June 10, 2026 07:18
@sourcery-ai

sourcery-ai Bot commented Jun 10, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts the focus-transfer condition on mouse release so that the web view only gains focus if neither the web view nor the search field currently has a text selection, preventing unintended loss of selection in the search box when releasing the mouse outside its bounds.

Sequence diagram for updated focus handling on mouse release

sequenceDiagram
    actor User
    participant WebWindow
    participant search_edit
    participant search_lineEdit
    participant web_view

    User->>WebWindow: eventFilter(MouseButtonRelease)
    WebWindow->>web_view: selectedText()
    WebWindow->>search_edit: hasWidgetRect(search_edit)
    WebWindow->>search_lineEdit: selectedText()
    alt web_view.selectedText is empty
        alt QCursor not in hasWidgetRect(search_edit) and search_lineEdit.selectedText is empty
            WebWindow->>web_view: setFocus()
        else search_lineEdit has selection or cursor inside search_edit
            WebWindow-->>User: keep current focus and selection
        end
    else web_view has selection
        WebWindow-->>User: keep current focus and selection
    end
Loading

File-Level Changes

Change Details Files
Tighten the eventFilter mouse-release condition to avoid moving focus from the search field to the web view when there is selected text in the search field.
  • Extend the MouseButtonRelease handling condition to additionally check whether the search_edit_ line edit currently has selected text
  • Only transfer focus to the web_view_ when there is no selection in either the web_view_ or the search_edit_ and the cursor is outside the search_edit_ rect
src/view/web_window.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

deepin pr auto review

这段代码是一个事件过滤器,用于处理鼠标释放事件。我来分析一下这段代码并提出改进建议:

  1. 语法逻辑:

    • 代码整体逻辑清晰,当鼠标释放时,检查当前活动窗口是否是WebWindow实例
    • 检查是否有选中的文本,以及光标是否不在搜索框内
    • 新增了对搜索框选中文本的检查
  2. 代码质量:

    • 变量命名规范,如web_view_search_edit_
    • 使用了Qt的日志系统进行调试输出
    • 代码格式规范,缩进正确
  3. 代码性能:

    • 多次调用QCursor::pos()可能会影响性能,可以考虑将其结果存储在变量中
    • web_view_->selectedText()search_edit_->lineEdit()->selectedText()可能会触发不必要的文本选择计算
  4. 代码安全:

    • 代码中存在潜在的空指针风险,没有对web_view_search_edit_->lineEdit()进行空指针检查
    • 如果search_edit_没有关联的lineEdit(),可能会导致程序崩溃

改进建议:

  1. 性能优化:
if (event->type() == QEvent::MouseButtonRelease && qApp->activeWindow() == this) {
    qCDebug(app) << "eventFilter mouse release";
    QRect rect = hasWidgetRect(search_edit_);
    QPoint cursorPos = QCursor::pos();  // 只获取一次光标位置
    
    if (web_view_ && web_view_->selectedText().isEmpty() && !rect.contains(cursorPos) 
        && (search_edit_ ? search_edit_->lineEdit()->selectedText().isEmpty() : true)) {
        qCDebug(app) << "set focus to web view to maintain scroll responsiveness";
        web_view_->setFocus();
    }
}
  1. 安全性增强:
if (event->type() == QEvent::MouseButtonRelease && qApp->activeWindow() == this) {
    qCDebug(app) << "eventFilter mouse release";
    
    // 添加空指针检查
    if (!web_view_ || !search_edit_) {
        return false;
    }
    
    QRect rect = hasWidgetRect(search_edit_);
    QPoint cursorPos = QCursor::pos();
    
    // 添加lineEdit()的空指针检查
    QString searchText = search_edit_->lineEdit() ? search_edit_->lineEdit()->selectedText() : "";
    
    if (web_view_->selectedText().isEmpty() && !rect.contains(cursorPos) && searchText.isEmpty()) {
        qCDebug(app) << "set focus to web view to maintain scroll responsiveness";
        web_view_->setFocus();
    }
}
  1. 可读性改进:
if (event->type() == QEvent::MouseButtonRelease && qApp->activeWindow() == this) {
    qCDebug(app) << "eventFilter mouse release";
    
    // 安全检查
    if (!web_view_ || !search_edit_) {
        return false;
    }
    
    // 获取必要信息
    QRect rect = hasWidgetRect(search_edit_);
    QPoint cursorPos = QCursor::pos();
    QString webSelectedText = web_view_->selectedText();
    QString searchSelectedText = search_edit_->lineEdit() ? search_edit_->lineEdit()->selectedText() : "";
    
    // 判断条件
    bool hasNoSelection = webSelectedText.isEmpty() && searchSelectedText.isEmpty();
    bool cursorOutsideSearchBox = !rect.contains(cursorPos);
    
    // 设置焦点
    if (hasNoSelection && cursorOutsideSearchBox) {
        qCDebug(app) << "set focus to web view to maintain scroll responsiveness";
        web_view_->setFocus();
    }
}

这些改进提高了代码的安全性、性能和可读性,同时保持了原有的功能不变。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

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:

  • The new condition dereferences search_edit_->lineEdit() without checking that search_edit_ (and its lineEdit()) is non-null, which could introduce a crash in edge cases where the search field isn’t initialized; consider guarding this access.
  • The compound if condition is becoming a bit long and harder to scan; you could extract the focus-transfer decision into a small helper (e.g., shouldTransferFocusToWebView()) to make the intent clearer and easier to maintain.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new condition dereferences `search_edit_->lineEdit()` without checking that `search_edit_` (and its `lineEdit()`) is non-null, which could introduce a crash in edge cases where the search field isn’t initialized; consider guarding this access.
- The compound `if` condition is becoming a bit long and harder to scan; you could extract the focus-transfer decision into a small helper (e.g., `shouldTransferFocusToWebView()`) to make the intent clearer and easier to maintain.

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.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pengfeixx

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

@pengfeixx pengfeixx merged commit 1e3a075 into linuxdeepin:master Jun 10, 2026
17 of 19 checks passed
@pengfeixx pengfeixx deleted the fix/search-edit-selection-cancel branch June 10, 2026 07:24
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