Skip to content

fix: set translucent background attribute for window - #587

Open
mhduiy wants to merge 1 commit into
linuxdeepin:masterfrom
mhduiy:fix/bug-339947-translucent-background
Open

mhduiy wants to merge 1 commit into
linuxdeepin:masterfrom
mhduiy:fix/bug-339947-translucent-background

Conversation

@mhduiy

@mhduiy mhduiy commented Sep 15, 2026

Copy link
Copy Markdown

Root Cause Analysis

deepin-editor's Window class inherits DMainWindow but does not call setAttribute(Qt::WA_TranslucentBackground) in its constructor, causing X11 to use a 24-bit visual without an alpha channel. Under the XRender compositing mode (best performance, kwin compositing type=4), kwin detects hasAlpha: false for the window and skips alpha blending in the Alt+Tab switcher, resulting in a residual preview of deepin-editor when switching to other applications.

Key evidence: kwin logs show "deepin-editor" hasAlpha: false vs "deepin-terminal" hasAlpha: true; deepin-terminal explicitly calls setAttribute(Qt::WA_TranslucentBackground) in its MainWindow::initWindow(), while deepin-editor's Window constructor does not. DMainWindow (dtkwidget) does not set this attribute by default.

Fix

Added setAttribute(Qt::WA_TranslucentBackground) at the beginning of the Window constructor body in src/widgets/window.cpp, consistent with deepin-terminal's approach. This enables a 32-bit visual with alpha channel, allowing kwin to correctly handle window alpha blending in the Alt+Tab switcher.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • The change adds one line (setAttribute(Qt::WA_TranslucentBackground)) to the Window constructor body, with no function signature changes, no logic modifications, and no external callers affected
  • The target code region's blame history shows only recent find-bar-related fixes (ef4cf7a), unrelated to window transparency; this change does not revert any historical fix

Business Impact Scope

Affects deepin-editor's window rendering under X11 compositing. In XRender (best performance) mode, the Alt+Tab window switcher will now correctly clear deepin-editor's preview when switching to other applications. OpenGL compositing mode and normal window operations (drag, maximize, minimize) are unaffected.

Verification Suggestion

  1. Test Alt+Tab window switching under best performance (XRender) mode — verify deepin-editor preview no longer persists
  2. Verify window rendering under OpenGL compositing mode — no regression
  3. Verify normal window operations (drag, maximize, minimize) — no regression

根因分析

deepin-editor 的 Window 类继承 DMainWindow 但在构造函数中未调用 setAttribute(Qt::WA_TranslucentBackground),导致 X11 使用 24-bit visual(无 alpha 通道)。在 XRender 合成模式(最佳性能,kwin compositing type=4)下,kwin 检测到该窗口 hasAlpha: false,在 Alt+Tab 切换器的透明度混合特效中跳过该窗口,导致切换到其他应用时文本编辑器的预览窗口残留。

关键证据:kwin 日志显示 "deepin-editor" hasAlpha: false"deepin-terminal" hasAlpha: true;deepin-terminal 在 MainWindow::initWindow() 中显式调用了 setAttribute(Qt::WA_TranslucentBackground),而 deepin-editor 的 Window 构造函数未设置。DMainWindow(dtkwidget)默认不设置该属性。

修复方案

src/widgets/window.cppWindow 构造函数体开头添加 setAttribute(Qt::WA_TranslucentBackground),与 deepin-terminal 的做法一致。该属性启用 32-bit visual(带 alpha 通道),使 kwin 在 Alt+Tab 切换器中正确处理窗口透明度混合。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 本次改动仅在 Window 构造函数中新增一行 setAttribute(Qt::WA_TranslucentBackground) 调用,不修改函数签名、不改变已有逻辑流程、无外部调用者受影响
  • 目标代码区域的 blame 历史仅显示最近的查找栏相关修复(ef4cf7a),与窗口透明属性无关,本次改动不会撤销任何历史修复

业务影响范围

影响 deepin-editor 在 X11 合成模式下的窗口渲染。在 XRender(最佳性能)模式下,Alt+Tab 窗口切换器将正确清除文本编辑器的预览窗口。OpenGL 合成模式和窗口正常操作(拖拽、最大化、最小化)不受影响。

验证建议

  1. 测试最佳性能(XRender)模式下 Alt+Tab 窗口切换——验证文本编辑器预览不再残留
  2. 验证 OpenGL 合成模式下窗口渲染——无回归
  3. 验证窗口正常操作(拖拽、最大化、最小化)——无回归

Summary by Sourcery

Bug Fixes:

  • Enable translucent window backgrounds so X11 compositing correctly alpha-blends the editor in the Alt+Tab switcher and prevents stale previews.

1. Root cause: Window constructor inherited DMainWindow but did not
   call setAttribute(Qt::WA_TranslucentBackground), causing X11 to
   use 24-bit visual without alpha channel
2. Fix: add setAttribute(Qt::WA_TranslucentBackground) in Window
   constructor to enable 32-bit visual with alpha channel
3. Impact: kwin XRender compositor now correctly handles window alpha
   blending in Alt+Tab switcher, no effect on OpenGL compositing

Log: fix window preview residual in Alt+Tab switcher under best performance mode

Influence:
1. Test Alt+Tab window switching under best performance (XRender) mode
2. Verify window rendering under OpenGL compositing mode
3. Verify normal window operations (drag, maximize, minimize)

fix: 设置窗口透明背景属性

1. 根因:Window 构造函数继承 DMainWindow 但未调用
   setAttribute(Qt::WA_TranslucentBackground),导致 X11 使用 24-bit
   visual 无 alpha 通道
2. 方案:在 Window 构造函数中添加 setAttribute(Qt::WA_TranslucentBackground)
   启用 32-bit visual 带 alpha 通道
3. 影响:kwin XRender 合成器在 Alt+Tab 切换器中正确处理窗口透明度
   混合,不影响 OpenGL 合成模式

Log: 修复最佳性能模式下 Alt+Tab 切换器窗口预览残留问题

Influence:
1. 测试最佳性能(XRender)模式下 Alt+Tab 窗口切换
2. 验证 OpenGL 合成模式下窗口渲染
3. 验证窗口正常操作(拖拽、最大化、最小化)

PMS: BUG-339947

@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.

Sorry @mhduiy, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 2 hours and 41 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Sep 15, 2026

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

Reviewer's Guide

The PR fixes residual deepin-editor previews during Alt+Tab under XRender compositing by setting the window's translucent-background attribute during construction, matching the established deepin-terminal approach. The change is localized and low risk; review should focus on constructor timing and validating XRender, OpenGL, and normal window operations.

Sequence diagram for translucent window initialization

sequenceDiagram
    participant Application
    participant Window
    participant Qt
    participant KWin
    Application->>Window: Window::Window(parent)
    Window->>Qt: setAttribute(Qt::WA_TranslucentBackground)
    Qt-->>Window: Window uses alpha-capable visual
    Window-->>Application: Constructed
    KWin->>Window: Read window alpha during Alt+Tab
    KWin-->>Window: Blend preview correctly
Loading

File-Level Changes

Change Details Files
Enable an alpha-capable X11 visual for the main editor window.
  • Set Qt::WA_TranslucentBackground at the start of the window constructor.
  • Allow KWin to perform alpha blending correctly in the XRender Alt+Tab switcher.
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

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mhduiy

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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 100 分,大于 70 分通过阈值。本次提交仅在 Window 构造函数中新增一行 setAttribute(Qt::WA_TranslucentBackground) 调用,修复了 XRender 合成模式下 Alt+Tab 切换器窗口预览残留问题。代码语法正确、逻辑清晰,无安全漏洞,无性能问题,代码质量良好。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

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

建议: 代码语法正确,逻辑清晰。setAttribute(Qt::WA_TranslucentBackground) 是 Qt QWidget 的标准 API 调用,DMainWindow 通过继承链 DMainWindow → QMainWindow → QWidget 可正常访问该方法。Qt::WA_TranslucentBackground 是有效的 Qt::WidgetAttribute 枚举值。属性设置放置在构造函数体开头,在窗口显示之前生效,符合 Qt 文档建议的使用方式。与 deepin-terminal 的 MainWindow::initWindow() 中相同的调用方式保持一致。


2. 代码质量 ✅

评价: 优秀 ✅ 通过

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

建议: 代码改动极小且聚焦(仅新增1行),无重复代码,无残留调试代码。PR 描述提供了详尽的根因分析、修复方案和影响评估,文档质量优秀。改动遵循与 deepin-terminal 一致的实现模式,可维护性良好。


3. 代码性能 ✅

评价: 优秀 ✅ 通过

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

建议: setAttribute() 是轻量级操作,仅设置窗口属性标志位,仅在构造时调用一次。启用半透明背景后,合成器需处理 alpha 通道混合,但这是预期行为且与 deepin-terminal 一致,不会引入性能瓶颈。


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

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

建议: 本次改动不涉及用户输入处理、网络操作、文件系统访问、密码学操作或命令执行,不存在安全风险。


💡 改进建议代码示例

// 当前代码已是最优实现,无需修改
// src/widgets/window.cpp - Window::Window(DMainWindow *parent)
{
    setAttribute(Qt::WA_TranslucentBackground);  // 启用32-bit visual,支持alpha通道
    // ... 其余构造函数代码
}

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

@deepin-bot

deepin-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.6.3
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #594

@deepin-bot

deepin-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.6.4
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #596

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