Skip to content

feat: support setColorScheme() API as well#322

Merged
BLumia merged 1 commit into
linuxdeepin:masterfrom
BLumia:pms-369921
Jul 14, 2026
Merged

feat: support setColorScheme() API as well#322
BLumia merged 1 commit into
linuxdeepin:masterfrom
BLumia:pms-369921

Conversation

@BLumia

@BLumia BLumia commented Jul 13, 2026

Copy link
Copy Markdown
Member

支持 Qt 6.8 的 QStyleHints::setColorScheme() 以及对应的
resetColorScheme() 接口

This amends: ba6d77f

PMS: BUG-369921

可供测试的 QML 程序:

import QtQuick
import QtQuick.Controls

Rectangle {
    id: root
    property bool isDarkMode: Application.styleHints.colorScheme === Qt.ColorScheme.Dark
    color: isDarkMode ? "#333" : "white"

    Column {
        anchors.centerIn: parent
        spacing: 16

        Text {
            anchors.horizontalCenter: parent.horizontalCenter
            font.pixelSize: 20
            color: root.isDarkMode ? "white" : "black"
            text: "ColorScheme: " + Application.styleHints.colorScheme
        }

        Row {
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 12

            Button {
                text: "Set Dark"
                onClicked: Application.styleHints.colorScheme = Qt.ColorScheme.Dark
            }

            Button {
                text: "Set Light"
                onClicked: Application.styleHints.colorScheme = Qt.ColorScheme.Light
            }

            Button {
                text: "Reset"
                onClicked: Application.styleHints.colorScheme = Qt.ColorScheme.Unknown
            }
        }

        Text {
            anchors.horizontalCenter: parent.horizontalCenter
            font.pixelSize: 14
            color: root.isDarkMode ? "#aaa" : "#666"
            text: "colorSchemeChanged signal updates this display"
        }
    }

    Connections {
        target: Application.styleHints
        function onColorSchemeChanged(scheme) {
            console.log("colorSchemeChanged:", scheme)
            root.isDarkMode = (scheme === Qt.ColorScheme.Dark)
        }
    }
}

Summary by Sourcery

Support Qt 6.8 color scheme requests in the Deepin platform theme and align color scheme resolution with DTK’s palette and system theme semantics.

New Features:

  • Implement requestColorScheme() to propagate Qt color scheme requests to DTK and trigger theme updates.

Enhancements:

  • Refine colorScheme() to honor explicitly configured DTK palette types and system theme names before falling back to a default scheme.
  • Track the last requested color scheme within QDeepinTheme to coordinate Qt and DTK behavior across sessions.

@BLumia
BLumia requested a review from 18202781743 July 13, 2026 07:17
@sourcery-ai

sourcery-ai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Reviewer's Guide

Add full support for Qt 6.5+ colorScheme() and Qt 6.8+ requestColorScheme() by tracking an explicit requested scheme, integrating with DTK paletteType/themeName-based detection, and propagating scheme changes into DTK while ensuring theme change handling is correctly triggered.

Sequence diagram for Qt setColorScheme() handling and DTK integration

sequenceDiagram
    actor QMLApp
    participant QStyleHints
    participant QPlatformTheme
    participant QDeepinTheme
    participant DGuiApplicationHelper
    participant DPlatformTheme
    participant QWindowSystemInterface

    QMLApp->>QStyleHints: setColorScheme(Dark/Light/Unknown)
    QStyleHints->>QPlatformTheme: requestColorScheme(scheme)
    QPlatformTheme->>QDeepinTheme: requestColorScheme(scheme)
    QDeepinTheme->>QDeepinTheme: set m_requestedColorScheme
    QDeepinTheme->>QWindowSystemInterface: handleThemeChange()
    QDeepinTheme->>DGuiApplicationHelper: setPaletteType(DarkType/LightType/UnknownType)

    QMLApp->>QStyleHints: colorScheme()
    QStyleHints->>QPlatformTheme: colorScheme()
    QPlatformTheme->>QDeepinTheme: colorScheme()
    alt m_requestedColorScheme != Unknown
        QDeepinTheme-->>QPlatformTheme: return m_requestedColorScheme
    else helper->paletteType() != UnknownType
        QDeepinTheme->>DGuiApplicationHelper: paletteType()
        DGuiApplicationHelper-->>QDeepinTheme: DarkType/LightType
        QDeepinTheme-->>QPlatformTheme: return Dark/Light
    else theme detected from applicationTheme()
        QDeepinTheme->>DGuiApplicationHelper: applicationTheme()
        DGuiApplicationHelper-->>QDeepinTheme: DPlatformTheme
        QDeepinTheme->>DPlatformTheme: themeName()
        DPlatformTheme-->>QDeepinTheme: themeName
        QDeepinTheme-->>QPlatformTheme: return Dark if themeName endsWith("dark") else Light
    end
Loading

File-Level Changes

Change Details Files
Improve colorScheme() resolution logic to respect an explicitly requested scheme, DTK paletteType, and system theme name, avoiding reliance on themeType() during theme change handling.
  • Return the stored requested color scheme when it is non-Unknown before consulting DTK state.
  • Use DGuiApplicationHelper::paletteType() (Dark/Light) as the primary DTK source for determining the color scheme, falling back only when it is Unknown.
  • Add a fallback using DPlatformTheme::themeName() suffix "dark" to infer a Dark color scheme and default to Light otherwise.
  • Avoid use of themeType() to prevent incorrect luminance-based fallback while palettes are not yet updated during QGuiApplicationPrivate::handleThemeChanged().
platformthemeplugin/qdeepintheme.cpp
Implement requestColorScheme() for Qt 6.8+ to synchronize Qt’s color scheme requests with DTK’s palette type and reliably trigger theme updates.
  • Add QDeepinTheme::requestColorScheme() override that stores the requested scheme in a member, calls base QPlatformTheme::requestColorScheme(), and emits a theme change via QWindowSystemInterface::handleThemeChange().
  • Ensure Unknown requests are not short-circuited so they can reset DTK palette type even when the stored requested scheme is already Unknown.
  • Map Qt::ColorScheme values (Dark/Light/Unknown) to DGuiApplicationHelper::setPaletteType() to keep DTK palette type in sync with Qt.
  • Guard the new API with QT_VERSION >= 6.8.0 and the requested scheme member with QT_VERSION >= 6.5.0.
platformthemeplugin/qdeepintheme.cpp
platformthemeplugin/qdeepintheme.h
Extend QDeepinTheme class interface and state to support tracking the requested color scheme across sessions and API calls.
  • Declare the requestColorScheme(Qt::ColorScheme) override in the QDeepinTheme class for Qt 6.8+ builds.
  • Introduce a m_requestedColorScheme member default-initialized to Qt::ColorScheme::Unknown for Qt 6.5+ builds to track explicit requests.
  • Wire the new member into colorScheme() so that explicit user or application requests take precedence over DTK-derived values.
platformthemeplugin/qdeepintheme.h

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 left some high level feedback:

  • The colorScheme() fallback logic now always returns Light when DTK does not provide a palette type or a theme name ending with dark; if a neutral/unknown state is desirable, consider returning Unknown instead to avoid forcing light mode in ambiguous cases.
  • In colorScheme(), the themeName.endsWith("dark") check may miss themes with different casing or naming conventions; consider making this comparison case-insensitive or more robust to avoid misclassifying dark themes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `colorScheme()` fallback logic now always returns `Light` when DTK does not provide a palette type or a theme name ending with `dark`; if a neutral/unknown state is desirable, consider returning `Unknown` instead to avoid forcing light mode in ambiguous cases.
- In `colorScheme()`, the `themeName.endsWith("dark")` check may miss themes with different casing or naming conventions; consider making this comparison case-insensitive or more robust to avoid misclassifying dark themes.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread platformthemeplugin/qdeepintheme.cpp Outdated
支持 Qt 6.8 的 QStyleHints::setColorScheme() 以及对应的
resetColorScheme() 接口

This amends: ba6d77f

PMS: BUG-369921
Log:
@deepin-ci-robot

Copy link
Copy Markdown
Contributor

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码实现了Qt6平台主题插件中颜色方案的优先级判断和动态设置,逻辑严谨且注释清晰
逻辑正确且无安全漏洞,代码质量高,性能良好

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

colorScheme() 函数中优先检查 m_requestedColorScheme,随后依次检查 paletteTypethemeName,逻辑层次清晰。requestColorScheme() 函数中对 Unknown 状态的特殊处理符合重置 DTK palette type 的需求。对 helpertheme 指针均进行了空指针检查,避免了空指针解引用。
潜在问题:无
建议:无

  • 2.代码质量(良好)✓

代码注释详尽,特别是在 colorScheme() 中解释了为何不使用 themeType() 而改用 themeName() 的原因,以及在 requestColorScheme() 中解释了为何不跳过 Unknown 请求的原因,极大地提高了可维护性。变量命名符合 Qt 规范。
潜在问题:无
建议:无

  • 3.代码性能(无性能问题)✓

colorScheme() 函数仅涉及简单的条件判断和字符串后缀匹配,无复杂计算或频繁的系统调用,性能开销极低。
潜在问题:无
建议:无

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码主要处理内部主题状态和颜色方案,不涉及外部输入解析、文件操作或网络通信,不存在可利用的安全风险。

  • 建议:无

■ 【改进建议代码示例】

// 代码逻辑完善,无需修改

@BLumia
BLumia requested a review from 18202781743 July 13, 2026 09:37
@deepin-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, BLumia

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

@BLumia
BLumia merged commit aa5a09c into linuxdeepin:master Jul 14, 2026
23 checks passed
@BLumia
BLumia deleted the pms-369921 branch July 14, 2026 03:07
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.

3 participants