Skip to content

fix: restore find widget state on tab switch - #426

Open
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bug/3db6092fd479
Open

pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bug/3db6092fd479

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

fix: restore find widget state on tab switch

  1. Root cause: hideFindWidget() called deleteLater() and set
    m_findWidget to nullptr, destroying the FindWidget on tab switch;
    restoreSavedViewState() had no field to save/restore search bar
    visibility, so switching back showed no search bar
  2. Fix: add findWidgetVisible field to SheetOperation, save it in
    saveCurrentViewState() and restore via handlePrepareSearch() in
    restoreSavedViewState(); change hideFindWidget() to only hide()
    without destroying the widget; reorder onTabChanged() to call
    saveCurrentViewState() before closeFindWidget()
  3. Impact: search bar visibility is preserved across tab switches;
    no behavior change for normal hide/show operations

Log: Fixed search bar not restoring after switching document tabs

Influence:

  1. Test switching tabs with search bar open, verify it restores
  2. Test switching tabs with search bar closed, verify no change
  3. Test search functionality after multiple tab switches
  4. Verify no search state leakage between documents

fix: 切换标签页时恢复搜索框状态

  1. 根因:hideFindWidget() 调用 deleteLater() 并将 m_findWidget 置空,
    切换标签页时销毁了搜索框;restoreSavedViewState() 无搜索框可见性
    字段,切回原标签页时搜索框不显示
  2. 方案:SheetOperation 添加 findWidgetVisible 字段,saveCurrentViewState()
    保存搜索框可见性,restoreSavedViewState() 通过 handlePrepareSearch()
    恢复;hideFindWidget() 改为仅 hide() 不销毁;onTabChanged() 调整为
    先 saveCurrentViewState() 再 closeFindWidget()
  3. 影响:切换标签页后搜索框可见性正确恢复,正常隐藏/显示无行为变化

Log: 修复切换文档标签页后搜索框不恢复的问题

Influence:

  1. 测试打开搜索框后切换标签页,验证搜索框恢复
  2. 测试关闭搜索框后切换标签页,验证无变化
  3. 测试多次切换标签页后搜索功能正常
  4. 验证不同文档间搜索状态不串档

PMS: BUG-379133

Summary by Sourcery

Restore each document's find widget state across tab switches without changing normal search hide/show behavior.

Bug Fixes:

  • Preserve find widget visibility when switching between document tabs.

Enhancements:

  • Retain the find widget instance while hidden and restore its visibility as part of each document's saved view state.

1. Root cause: hideFindWidget() called deleteLater() and set
   m_findWidget to nullptr, destroying the FindWidget on tab switch;
   restoreSavedViewState() had no field to save/restore search bar
   visibility, so switching back showed no search bar
2. Fix: add findWidgetVisible field to SheetOperation, save it in
   saveCurrentViewState() and restore via handlePrepareSearch() in
   restoreSavedViewState(); change hideFindWidget() to only hide()
   without destroying the widget; reorder onTabChanged() to call
   saveCurrentViewState() before closeFindWidget()
3. Impact: search bar visibility is preserved across tab switches;
   no behavior change for normal hide/show operations

Log: Fixed search bar not restoring after switching document tabs

Influence:
1. Test switching tabs with search bar open, verify it restores
2. Test switching tabs with search bar closed, verify no change
3. Test search functionality after multiple tab switches
4. Verify no search state leakage between documents

fix: 切换标签页时恢复搜索框状态

1. 根因:hideFindWidget() 调用 deleteLater() 并将 m_findWidget 置空,
   切换标签页时销毁了搜索框;restoreSavedViewState() 无搜索框可见性
   字段,切回原标签页时搜索框不显示
2. 方案:SheetOperation 添加 findWidgetVisible 字段,saveCurrentViewState()
   保存搜索框可见性,restoreSavedViewState() 通过 handlePrepareSearch()
   恢复;hideFindWidget() 改为仅 hide() 不销毁;onTabChanged() 调整为
   先 saveCurrentViewState() 再 closeFindWidget()
3. 影响:切换标签页后搜索框可见性正确恢复,正常隐藏/显示无行为变化

Log: 修复切换文档标签页后搜索框不恢复的问题

Influence:
1. 测试打开搜索框后切换标签页,验证搜索框恢复
2. 测试关闭搜索框后切换标签页,验证无变化
3. 测试多次切换标签页后搜索功能正常
4. 验证不同文档间搜索状态不串档

PMS: BUG-379133
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

@sourcery-ai

sourcery-ai Bot commented Oct 21, 2026

Copy link
Copy Markdown

Reviewer's Guide

The fix preserves each document tab’s find-widget visibility by saving it before tab-switch cleanup, retaining the widget for reuse, and restoring it when the tab becomes active again without changing normal hide/show behavior.

Sequence diagram for restoring find widget state on tab switch

sequenceDiagram
    participant CentralDocPage
    participant PreviousSheet
    participant DocSheet
    participant SheetBrowser
    participant FindWidget

    CentralDocPage->>PreviousSheet: saveCurrentViewState()
    PreviousSheet->>SheetBrowser: isFindWidgetVisible()
    SheetBrowser-->>PreviousSheet: visibility state
    PreviousSheet->>PreviousSheet: store findWidgetVisible
    CentralDocPage->>PreviousSheet: closeFindWidget()
    PreviousSheet->>SheetBrowser: hideFindWidget()
    SheetBrowser->>FindWidget: hide()
    CentralDocPage->>DocSheet: restoreSavedViewState()
    DocSheet->>SheetBrowser: handlePrepareSearch()
    SheetBrowser-->>FindWidget: show search widget
Loading

File-Level Changes

Change Details Files
Persist and restore find-widget visibility as part of each document tab’s view state.
  • Added a per-sheet visibility flag to SheetOperation.
  • Captured visibility when saving the current view state.
  • Reopened the search widget during view-state restoration only when it was previously visible.
reader/uiframe/DocSheet.h
reader/uiframe/DocSheet.cpp
Keep the find widget reusable instead of destroying it during tab transitions.
  • Changed hideFindWidget() to hide without deleteLater() or clearing the widget pointer.
  • Added an accessor for determining current widget visibility.
reader/browser/SheetBrowser.h
reader/browser/SheetBrowser.cpp
Preserve the outgoing tab’s search state before applying tab-switch cleanup.
  • Reordered tab-change handling to save view state before closing the outgoing tab’s find widget.
reader/uiframe/CentralDocPage.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

@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 reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 79 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过(及格),存在内存泄漏与重复信号连接问题需修复
评分详情 本次提交目的是修复切换标签页时搜索框状态丢失的问题,核心思路(保存搜索框可见性状态、 切换标签页时先保存状态再关闭搜索框、恢复时重建搜索框)逻辑正确,代码结构与注释良好, 无安全漏洞。但移除 hideFindWidget 中的 deleteLater() 后未在析构函数中补充清理, 导致 FindWidget 因父对象为 window 而在 SheetBrowser 销毁时泄漏(严重错误); handlePrepareSearch 中 connect 位于创建块外部,每次调用都新增重复信号连接, 本次新增的恢复路径会加剧该问题(明显性能问题)。建议修复后再合入。

🔍 详细分析

1. 语法逻辑 ❌

评价: 存在逻辑缺陷 ❌ 不通过

潜在问题:
✅ 未发现明显问题

建议: ['在 SheetBrowser::~SheetBrowser() 中添加 m_findWidget 的清理:if (!m_findWidget.isNull()) { delete m_findWidget; m_findWidget = nullptr; },或将 FindWidget 的父对象改为 this 使其随 SheetBrowser 自动销毁']


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


3. 代码性能 ❌

评价: 存在性能问题 ❌ 不通过

潜在问题:
✅ 未发现明显问题

建议: ['为 connect 添加 Qt::UniqueConnection 标志防止重复连接,或将 connect 移入 if (m_findWidget == nullptr) 创建块内仅连接一次']


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例

// ===== 修复1:SheetBrowser.cpp 析构函数中补充 FindWidget 清理 =====
SheetBrowser::~SheetBrowser()
{
    disconnect(this, SIGNAL(sigAddHighLightAnnot(BrowserPage *, QString, QColor)),
               this, SLOT(onAddHighLightAnnot(BrowserPage *, QString, QColor)));

    qDeleteAll(m_items);

    if (nullptr != m_noteEditWidget) {
        delete m_noteEditWidget;
    }

    // 新增:清理 FindWidget,防止因父对象为 window 而泄漏
    if (!m_findWidget.isNull()) {
        delete m_findWidget;
        m_findWidget = nullptr;
    }
}

// ===== 修复2:handlePrepareSearch 中使用 Qt::UniqueConnection 防止重复连接 =====
void SheetBrowser::handlePrepareSearch()
{
    qCDebug(appLog) << "Preparing search for file type:" << m_sheet->fileType();

    if (!Dr::supportsSearch(m_sheet->fileType())) {
        qCDebug(appLog) << "Search not supported for current file type";
        return;
    }

    if (m_findWidget == nullptr) {
        m_findWidget = new FindWidget(this->window());
        int windowY = this->mapTo(this->window(), QPoint(0, 0)).y();
        m_findWidget->setYOff(windowY);
        m_findWidget->setDocSheet(m_sheet);
        qCDebug(appLog) << "Created new FindWidget with Y offset:" << windowY;
    }

    m_findWidget->updatePosition();
    m_findWidget->setSearchEditFocus();
    qCDebug(appLog) << "FindWidget positioned and focused";
#ifdef DTKWIDGET_CLASS_DSizeMode
    // 使用 Qt::UniqueConnection 防止重复连接
    connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::sizeModeChanged,
            this, [=](DGuiApplicationHelper::SizeMode sizeMode) {
        if (m_findWidget && m_findWidget->isVisible()) {
            m_findWidget->updatePosition();
        }
    }, Qt::UniqueConnection);
#endif
}

本报告由 AI 代码审查工具自动生成

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