Skip to content

fix: reduce print preview delay by yielding UI events - #683

Open
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/pms-bug-bot/40729ba2ffef
Open

pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/pms-bug-bot/40729ba2ffef

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

fix: reduce print preview delay by yielding UI events

  1. Root cause: updateHighlighterAll() rehighlights every block
    synchronously without yielding, blocking UI thread for ~6s on
    medium files during print preview
  2. Fix: call qApp->processEvents() every 100 blocks to keep UI
    responsive, consistent with cloneLargeDocument() pattern
  3. Cleanup: remove dead DPrinter printer(QPrinter::HighResolution)
    in popupPrintDialog(), introduced by commit 9fcf3e4, which
    triggers unnecessary cups plugin load and printer enumeration
  4. Impact: print preview opens faster, no behavior change for
    syntax highlighting or print output

Log: 优化打印预览页面打开速度,减少 UI 阻塞

Influence:

  1. Test print preview with medium/large syntax-highlighted files
  2. Verify syntax highlighting renders correctly in print preview
  3. Test print preview with plain text files (no highlighter)
  4. Verify no -Wunused-variable warnings after dead code removal

fix: 优化打印预览页面响应速度,减少 UI 线程阻塞

  1. 根因:updateHighlighterAll() 同步遍历所有文本块执行语法高亮,
    无 processEvents 间歇,中小文件打印预览时阻塞 UI 线程约 6 秒
  2. 方案:循环中每 100 块调用 qApp->processEvents() 保持 UI 响应,
    与 cloneLargeDocument() 现有模式一致
  3. 清理:删除 popupPrintDialog() 中 DPrinter printer(QPrinter::
    HighResolution) 死代码,由 commit 9fcf3e4 引入,会触发不必要的
    cups 插件加载和打印机枚举
  4. 影响:打印预览打开速度提升,语法高亮和打印输出行为不变

Log: 优化打印预览页面打开速度,减少 UI 阻塞

Influence:

  1. 测试中等/大型语法高亮文件的打印预览
  2. 验证打印预览中语法高亮渲染正确
  3. 测试纯文本文件(无语法高亮)的打印预览
  4. 验证删除死代码后无 -Wunused-variable 告警残留

PMS: BUG-375053

Summary by Sourcery

Improve print preview responsiveness without changing syntax highlighting or print output behavior.

Bug Fixes:

  • Reduce print preview UI blocking by periodically yielding while reapplying syntax highlighting across document blocks.

Enhancements:

  • Remove unused printer initialization during print preview setup to avoid unnecessary printer discovery and plugin loading.

1. Root cause: updateHighlighterAll() rehighlights every block
   synchronously without yielding, blocking UI thread for ~6s on
   medium files during print preview
2. Fix: call qApp->processEvents() every 100 blocks to keep UI
   responsive, consistent with cloneLargeDocument() pattern
3. Cleanup: remove dead DPrinter printer(QPrinter::HighResolution)
   in popupPrintDialog(), introduced by commit 9fcf3e4, which
   triggers unnecessary cups plugin load and printer enumeration
4. Impact: print preview opens faster, no behavior change for
   syntax highlighting or print output

Log: 优化打印预览页面打开速度,减少 UI 阻塞

Influence:
1. Test print preview with medium/large syntax-highlighted files
2. Verify syntax highlighting renders correctly in print preview
3. Test print preview with plain text files (no highlighter)
4. Verify no -Wunused-variable warnings after dead code removal

fix: 优化打印预览页面响应速度,减少 UI 线程阻塞

1. 根因:updateHighlighterAll() 同步遍历所有文本块执行语法高亮,
   无 processEvents 间歇,中小文件打印预览时阻塞 UI 线程约 6 秒
2. 方案:循环中每 100 块调用 qApp->processEvents() 保持 UI 响应,
   与 cloneLargeDocument() 现有模式一致
3. 清理:删除 popupPrintDialog() 中 DPrinter printer(QPrinter::
   HighResolution) 死代码,由 commit 9fcf3e4 引入,会触发不必要的
   cups 插件加载和打印机枚举
4. 影响:打印预览打开速度提升,语法高亮和打印输出行为不变

Log: 优化打印预览页面打开速度,减少 UI 阻塞

Influence:
1. 测试中等/大型语法高亮文件的打印预览
2. 验证打印预览中语法高亮渲染正确
3. 测试纯文本文件(无语法高亮)的打印预览
4. 验证删除死代码后无 -Wunused-variable 告警残留

PMS: BUG-375053
@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 23, 2026

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

Reviewer's Guide

Improves print-preview responsiveness by periodically yielding during large syntax-highlighting passes and removes an unused printer object that triggered needless printer enumeration, without changing highlighting or print output behavior.

Sequence diagram for responsive print preview highlighting

sequenceDiagram
    participant Window
    participant Preview as DPrintPreviewDialog
    participant Editor as EditWrapper
    participant Highlighter as QSyntaxHighlighter
    participant Qt as qApp

    Window->>Preview: setAttribute
    Preview->>Editor: updateHighlighterAll()
    loop Each text block
        Editor->>Highlighter: rehighlightBlock(var)
        opt Every 100 blocks
            Editor->>Qt: processEvents()
        end
    end
    Editor->>Highlighter: rehighlightBlock(endBlock)
    Preview-->>Window: responsive preview UI
Loading

File-Level Changes

Change Details Files
Yield to Qt UI event processing during bulk syntax-highlighting updates.
  • Track processed text blocks and call qApp->processEvents() after every 100 blocks.
  • Retain the existing block rehighlighting flow and output behavior.
src/editor/editwrapper.cpp
Remove unused printer construction from the print-preview setup.
  • Delete the dead high-resolution DPrinter local that caused unnecessary printer/plugin initialization.
  • Leave print-preview dialog creation and configuration unchanged.
src/widgets/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

@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 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过,但存在逻辑缺陷建议修复
评分详情 本次提交通过在语法高亮重处理循环中定期调用 processEvents() 来减少打印预览延迟,移除了未使用的 DPrinter 对象,整体方向正确。但 processEvents() 在循环内调用缺少 QPointer 守卫和取消标志检查,存在重入导致的 use-after-free 崩溃风险。代码库中 cloneLargeDocument() 已有正确模式可参考。四维度中语法逻辑维度未通过,其余维度通过。

🔍 详细分析

1. 语法逻辑 ❌

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

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

建议: ['在 processEvents() 调用后添加 m_bQuit 和 m_pSyntaxHighlighter 有效性检查,若无效则 return 退出循环', '使用 QPointer 守卫对象,processEvents() 后检查守卫是否仍然有效', '在 processEvents() 后重新验证 QTextBlock 迭代器 var 的有效性(var.isValid()),若失效则退出循环', '参考同代码库 cloneLargeDocument() 函数(约2255-2261行)的 QPointer + 取消标志模式']


2. 代码质量 ✅

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

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

建议: []


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

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

建议: ['将魔法数字 100 提取为命名常量,如 static const int kProcessEventInterval = 100;']


4. 代码安全 🔒

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

🔐 发现 0 个安全漏洞

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

建议: []


💡 改进建议代码示例

// src/editor/editwrapper.cpp - updateHighlighterAll() 修复示例
// 参考 cloneLargeDocument() 的 QPointer + 取消标志模式

int blockCount = 0;
for (QTextBlock var = beginBlock; var != endBlock; var = var.next()) {
    m_pSyntaxHighlighter->rehighlightBlock(var);
    if (++blockCount % 100 == 0) {
        qApp->processEvents();
        // 检查退出标志和对象有效性
        if (m_bQuit || !m_pSyntaxHighlighter) {
            return;
        }
        // 重新验证迭代器有效性(文档可能已被修改)
        if (!var.isValid()) {
            return;
        }
    }
}

本报告由 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