fix(search): preserve text selection when mouse released outside titl…#626
Conversation
…ebar Check search edit selection before transferring focus to web view to prevent deselection caused by focus loss. 修复标题栏搜索框框选文本时,鼠标释放在标题栏外导致选中 被取消的问题,增加搜索框选中文本的判断条件。 Log: 修复搜索框框选文字被取消的问题 PMS: BUG-361733 Influence: 修复后用户在搜索框中拖拽框选文字,即使鼠标释放在标题栏外也不会丢失选中状态。
4b7694a to
81cde58
Compare
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdjusts 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 releasesequenceDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review这段代码是一个事件过滤器,用于处理鼠标释放事件。我来分析一下这段代码并提出改进建议:
改进建议:
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();
}
}
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();
}
}
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();
}
}这些改进提高了代码的安全性、性能和可读性,同时保持了原有的功能不变。 |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new condition dereferences
search_edit_->lineEdit()without checking thatsearch_edit_(and itslineEdit()) is non-null, which could introduce a crash in edge cases where the search field isn’t initialized; consider guarding this access. - The compound
ifcondition 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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
…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: