Skip to content

fix: cache bg color in setMode to prevent white flash - #727

Draft
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bugfix/378219
Draft

pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bugfix/378219

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

When switching md file view mode from live preview to read-only under dark theme, MarkdownView::setMode() only emits a signal to the JS bridge without re-applying the cached theme background color. The reparent operation (from LiveSplitter to ReadPage) triggers Chromium recomposite, which uses the default white background set in init() (page()->setBackgroundColor(Qt::white)), causing a brief white flash (~33ms, 1 frame).

Key evidence: markdownview.cpp:176 (setMode() only emits signal), markdownview.cpp:191 (applyTheme() sets bg color but doesn't cache it), GIF frame analysis showing brightness jump 46.5→135.3.

Fix Approach

Add QColor m_lastBgColor private member to MarkdownView. Cache bgColor in applyTheme() when valid. In setMode(), before emitting the signal, re-apply m_lastBgColor via page()->setBackgroundColor() when m_hasTheme && m_lastBgColor.isValid(). This complements the prior partial fix 01856b21 without reverting any of its changes.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • No function signature changes; only adds a private member variable and a conditional branch in setMode()
  • Target line in applyTheme() was introduced by prior fix 01856b21; this change preserves it and only adds caching
  • setMode() was never modified since introduction (81fbb72f); no historical fix is reverted

Business Impact Scope

Affects only the Markdown view mode switch path (live preview ↔ read-only). Users on dark theme will no longer see white flash when switching to read-only mode. Other theme/mode operations unaffected.

Verification Suggestion

Test dark theme live-preview→read-only switch for no white flash. Test light theme mode switch for no regression. Test theme change followed by mode switch.


根因分析

深色主题下从实时预览切换为只读模式时,MarkdownView::setMode() 仅向 JS bridge 发出信号,未重新应用缓存的主题背景色。reparent 操作(从 LiveSplitter 到 ReadPage)触发 Chromium 重合成,使用 init() 中设置的默认白色背景,导致短暂白色闪烁(约 33ms,1 帧)。

关键证据:markdownview.cpp:176(setMode() 仅发信号)、markdownview.cpp:191(applyTheme() 设置背景色但未缓存)、GIF 帧分析显示亮度跳变 46.5→135.3。

修复方案

在 MarkdownView 中新增 QColor m_lastBgColor 私有成员。在 applyTheme() 中当颜色有效时缓存到 m_lastBgColor。在 setMode() 中 emit 信号前,当 m_hasTheme && m_lastBgColor.isValid() 时通过 page()->setBackgroundColor() 重新应用背景色。与先前部分修复 01856b21 互补,不撤销其任何改动。

改动安全评估

代码安全

  • 风险等级:低
  • 无函数签名变更;仅新增私有成员变量和 setMode() 中的条件分支
  • applyTheme() 目标行由先前修复 01856b21 引入;本次保留该行不变,仅新增缓存
  • setMode() 自引入(81fbb72f)以来从未被修改;不撤销任何历史修复

业务影响范围

仅影响 Markdown 视图模式切换路径(实时预览↔只读)。深色主题用户切换到只读模式时不再出现白色闪烁。其他主题/模式操作不受影响。

验证建议

测试深色主题下实时预览→只读切换无白色闪烁。测试浅色主题模式切换无回归。测试主题切换后再切换视图模式。

PMS: BUG-378219

Summary by Sourcery

Prevent transient white flashes during Markdown view mode changes by consistently applying themed background colors to the view and its page.

Bug Fixes:

  • Prevent white flashes when switching Markdown views under themed backgrounds by applying the theme color to the Markdown widget and page.
  • Keep Markdown view widget backgrounds synchronized with theme changes to avoid white surfaces during reparenting.

@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 Nov 13, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

UI

Set the MarkdownView widget palette and enable background filling when applying a theme or handling theme changes, preventing a white surface during view-mode reparenting.

  • src/editor/editwrapper.cpp L1295-1302
  • src/editor/markdown/markdownview.cpp L191-197

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

@pengfeixx
pengfeixx marked this pull request as draft November 13, 2026 19:33

@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 found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/editor/markdown/markdownview.cpp" line_range="196" />
<code_context>
-    if (bgColor.isValid())
+    if (bgColor.isValid()) {
         page()->setBackgroundColor(bgColor);
+        m_lastBgColor = bgColor;
+    }
     m_lastThemeJson = json;
</code_context>
<issue_to_address>
**Old background returns after theme change**

When a valid theme is followed by a theme with a missing or invalid background, `applyTheme()` leaves `m_lastBgColor` unchanged while marking the new theme present, so `setMode()` reapplies the previous color even though the current serialized theme falls back to white; the page background no longer matches the theme.

Clear or update `m_lastBgColor` to the current theme’s fallback when `applyTheme()` receives no valid background.

Also at `src/editor/markdown/markdownview.cpp:179-180`, `src/editor/markdown/markdownview.cpp:197`.
</issue_to_address>

### Comment 2
<location path="src/editor/markdown/markdownview.cpp" line_range="178-180" />
<code_context>

 void MarkdownView::setMode(int mode)
 {
+    // 切换模式时恢复最近一次主题背景色,防止 Chromium 渲染层回退到默认白色
+    if (m_hasTheme && m_lastBgColor.isValid())
+        page()->setBackgroundColor(m_lastBgColor);
     emit m_bridge->setModeRequested(mode == Editable);
 }
</code_context>
<issue_to_address>
**Dark mode still flashes white**

When `EditWrapper::setViewMode()` reparents and shows the Markdown view before `setMode()` updates its background, chromium recomposites or commits a frame during that earlier transition, before `setBackgroundColor()` runs, so the later update cannot prevent users from seeing a white flash.

Apply the theme background before reparenting and showing the view, or prevent rendering until the background has been updated.
</issue_to_address>

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

Comment thread src/editor/markdown/markdownview.cpp Outdated
if (bgColor.isValid())
if (bgColor.isValid()) {
page()->setBackgroundColor(bgColor);
m_lastBgColor = bgColor;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Old background returns after theme change

When a valid theme is followed by a theme with a missing or invalid background, applyTheme() leaves m_lastBgColor unchanged while marking the new theme present, so setMode() reapplies the previous color even though the current serialized theme falls back to white; the page background no longer matches the theme.

Clear or update m_lastBgColor to the current theme’s fallback when applyTheme() receives no valid background.

Also at src/editor/markdown/markdownview.cpp:179-180, src/editor/markdown/markdownview.cpp:197.

Prompt for AI agents
In `src/editor/markdown/markdownview.cpp` at line 196:

**Old background returns after theme change**

When a valid theme is followed by a theme with a missing or invalid background, `applyTheme()` leaves `m_lastBgColor` unchanged while marking the new theme present, so `setMode()` reapplies the previous color even though the current serialized theme falls back to white; the page background no longer matches the theme.

Clear or update `m_lastBgColor` to the current theme’s fallback when `applyTheme()` receives no valid background.

Also at `src/editor/markdown/markdownview.cpp:179-180`, `src/editor/markdown/markdownview.cpp:197`.

Comment thread src/editor/markdown/markdownview.cpp Outdated
Comment on lines +178 to +180
// 切换模式时恢复最近一次主题背景色,防止 Chromium 渲染层回退到默认白色
if (m_hasTheme && m_lastBgColor.isValid())
page()->setBackgroundColor(m_lastBgColor);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Dark mode still flashes white

When EditWrapper::setViewMode() reparents and shows the Markdown view before setMode() updates its background, chromium recomposites or commits a frame during that earlier transition, before setBackgroundColor() runs, so the later update cannot prevent users from seeing a white flash.

Apply the theme background before reparenting and showing the view, or prevent rendering until the background has been updated.

Prompt for AI agents
In `src/editor/markdown/markdownview.cpp` at lines 178-180:

**Dark mode still flashes white**

When `EditWrapper::setViewMode()` reparents and shows the Markdown view before `setMode()` updates its background, chromium recomposites or commits a frame during that earlier transition, before `setBackgroundColor()` runs, so the later update cannot prevent users from seeing a white flash.

Apply the theme background before reparenting and showing the view, or prevent rendering until the background has been updated.

1. Root cause: commit 01856b2 set page-level background color on
   QWebEngineView but missed widget-level palette, causing reparent
   Show event to paint default white surface during view mode switch
2. Fix: add setPalette + setAutoFillBackground for m_pMarkdownView in
   OnThemeChangeSlot and applyTheme, mirroring existing m_pReadPage
   pattern
3. Impact: eliminates white flash on dark theme LivePreview to
   ReadView switch; no effect on light theme

Log: fix white flash when switching from live preview to read-only mode under dark theme

Influence:
1. Test dark theme LivePreview to ReadView switch for no white flash
2. Test light theme switch remains unaffected
3. Verify theme change correctly updates MarkdownView background

fix: 为 MarkdownView 设置 widget 级 palette 防止白色闪烁

1. 根因:提交 01856b2 设置了 QWebEngineView 的 page 级背景色但遗漏
   widget 级 palette,reparent 时 Show 事件绘制默认白色表面
2. 方案:在 OnThemeChangeSlot 和 applyTheme 中为 m_pMarkdownView 补全
   setPalette + setAutoFillBackground,与 m_pReadPage 写法同构
3. 影响:消除深色主题下实时预览切只读模式的白色闪烁;浅色主题不受影响

Log: 修复深色主题下实时预览切换只读模式时的白色闪烁问题

Influence:
1. 测试深色主题下实时预览切换只读模式无白色闪烁
2. 测试浅色主题切换不受影响
3. 验证主题切换时 MarkdownView 背景色正确更新

PMS: BUG-378219
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