From ecc88eb33b98896f344f254be3a770fbcbcfdc65 Mon Sep 17 00:00:00 2001 From: yeshanshan Date: Tue, 22 Sep 2026 12:22:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20read=20palette=20colors=20from=20dtk=20?= =?UTF-8?q?preference=20dconfig=20with=20size=20validation=20feat:=20?= =?UTF-8?q?=E4=BB=8E=20dtk=20preference=20dconfig=20=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E8=B0=83=E8=89=B2=E6=9D=BF=E9=A2=9C=E8=89=B2=E5=B9=B6=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=95=B0=E7=BB=84=E9=95=BF=E5=BA=A6=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Remove separate palette dconfig schema files, read palette arrays from org.deepin.dtk.preference dconfig 2. Add size validation: if dconfig array length doesn't match QPalette::NColorRoles or DPalette::NColorTypes, fall back to static arrays 3. Remove all async/threading code (QAtomicInt, generation tracking), use synchronous main-thread dconfig reads 4. Add paletteColorsFromDConfig() helper for size-validated array reading with fallback 5. Add paletteWindowColor() helper for generatePaletteColor_helper 6. Connect valueChanged signal to invalidate cached palettes on config change 1. 移除独立的调色板 dconfig schema 文件,改为从 org.deepin.dtk.preference dconfig 读取调色板数组 2. 增加数组长度校验:若 dconfig 数组长度与 QPalette::NColorRoles 或 DPalette::NColorTypes 不一致,回退到静态数组 3. 移除所有异步/多线程代码(QAtomicInt、generation 跟踪),使用主线程同步读取 dconfig 4. 新增 paletteColorsFromDConfig() 辅助函数,实现带长度校验和回退的数组读取 5. 新增 paletteWindowColor() 辅助函数供 generatePaletteColor_helper 使用 6. 连接 valueChanged 信号,配置变化时失效缓存的调色板 PMS: DDE-330 Log: 调色板 dconfig 读取方式从独立 schema 文件改为 preference 数组,增加长度校验和回退,移除异步代码 Influence: dtkgui 调色板 dconfig 读取方式变更 --- src/kernel/dguiapplicationhelper.cpp | 107 +++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 8 deletions(-) diff --git a/src/kernel/dguiapplicationhelper.cpp b/src/kernel/dguiapplicationhelper.cpp index 38afda7b..5427ef25 100644 --- a/src/kernel/dguiapplicationhelper.cpp +++ b/src/kernel/dguiapplicationhelper.cpp @@ -866,6 +866,51 @@ static QColor dark_dpalette[DPalette::NColorTypes] { QColor(255, 255, 255, 0.1 * 255) //ObviousBackground }; +// Helper: read a palette color array from dconfig with size validation. +// Returns a pointer to a local buffer if the dconfig array is valid, +// otherwise returns the fallback static array. +static const QColor *paletteColorsFromDConfig(const QList &arr, + const QColor *fallback, + int expectedSize, + QColor *buffer) +{ + if (arr.size() != expectedSize) + return fallback; + + for (int i = 0; i < expectedSize; ++i) { + const QString &str = arr.at(i).toString(); + if (str.isEmpty()) { + buffer[i] = fallback[i]; + } else { + QColor c(str); + buffer[i] = c.isValid() ? c : fallback[i]; + } + } + return buffer; +} + +// Helper: get the Window color for generatePaletteColor_helper. +// Reads from dconfig with size validation, falls back to static array. +static QColor paletteWindowColor(DGuiApplicationHelper::ColorType type) +{ + if (_d_dconfig && _d_dconfig->isInitializeSucceeded()) { + QList arr = (type == DGuiApplicationHelper::DarkType) + ? _d_dconfig->darkQPalette() + : _d_dconfig->lightQPalette(); + if (arr.size() == QPalette::NColorRoles) { + const QString &str = arr.at(QPalette::Window).toString(); + if (!str.isEmpty()) { + QColor c(str); + if (c.isValid()) + return c; + } + } + } + return (type == DGuiApplicationHelper::DarkType) + ? dark_qpalette[QPalette::Window] + : light_qpalette[QPalette::Window]; +} + /*! \brief 根据主题获取标准调色板. @@ -878,6 +923,22 @@ DPalette DGuiApplicationHelper::standardPalette(DGuiApplicationHelper::ColorType static const DPalette *alpha_light_palette = nullptr, *alpha_dark_palette = nullptr; const bool allowCompositingColor = DGuiApplicationHelper::testAttribute(ColorCompositing); + // One-time connection: invalidate cached palettes when dconfig palette values change. + static bool s_paletteDConfigConnected = false; + if (!s_paletteDConfigConnected) { + s_paletteDConfigConnected = true; + QObject::connect(_d_dconfig.operator()(), &OrgDeepinDTKPreference::valueChanged, + qApp, [](const QString &key, const QVariant &) { + if (key == QLatin1String("lightQPalette") || key == QLatin1String("darkQPalette") || + key == QLatin1String("lightDPalette") || key == QLatin1String("darkDPalette")) { + delete light_palette; light_palette = nullptr; + delete dark_palette; dark_palette = nullptr; + delete alpha_light_palette; alpha_light_palette = nullptr; + delete alpha_dark_palette; alpha_dark_palette = nullptr; + } + }); + } + if (type == LightType) { if (Q_UNLIKELY(allowCompositingColor)) { if (Q_LIKELY(alpha_light_palette)) { @@ -905,6 +966,10 @@ DPalette DGuiApplicationHelper::standardPalette(DGuiApplicationHelper::ColorType DPalette *pa; const QColor *qcolor_list, *dcolor_list; + // Local buffers for dconfig-derived color arrays. + QColor local_qpalette[QPalette::NColorRoles]; + QColor local_dpalette[DPalette::NColorTypes]; + if (type == DarkType) { pa = new DPalette(); @@ -913,8 +978,21 @@ DPalette DGuiApplicationHelper::standardPalette(DGuiApplicationHelper::ColorType else dark_palette = pa; - qcolor_list = dark_qpalette; - dcolor_list = dark_dpalette; + // Try reading palette arrays from dconfig with size validation. + bool dconfigOk = false; + if (_d_dconfig && _d_dconfig->isInitializeSucceeded()) { + QList qArr = _d_dconfig->darkQPalette(); + QList dArr = _d_dconfig->darkDPalette(); + if (qArr.size() == QPalette::NColorRoles && dArr.size() == DPalette::NColorTypes) { + qcolor_list = paletteColorsFromDConfig(qArr, dark_qpalette, QPalette::NColorRoles, local_qpalette); + dcolor_list = paletteColorsFromDConfig(dArr, dark_dpalette, DPalette::NColorTypes, local_dpalette); + dconfigOk = true; + } + } + if (!dconfigOk) { + qcolor_list = dark_qpalette; + dcolor_list = dark_dpalette; + } } else { pa = new DPalette(); @@ -923,8 +1001,21 @@ DPalette DGuiApplicationHelper::standardPalette(DGuiApplicationHelper::ColorType else light_palette = pa; - qcolor_list = light_qpalette; - dcolor_list = light_dpalette; + // Try reading palette arrays from dconfig with size validation. + bool dconfigOk = false; + if (_d_dconfig && _d_dconfig->isInitializeSucceeded()) { + QList qArr = _d_dconfig->lightQPalette(); + QList dArr = _d_dconfig->lightDPalette(); + if (qArr.size() == QPalette::NColorRoles && dArr.size() == DPalette::NColorTypes) { + qcolor_list = paletteColorsFromDConfig(qArr, light_qpalette, QPalette::NColorRoles, local_qpalette); + dcolor_list = paletteColorsFromDConfig(dArr, light_dpalette, DPalette::NColorTypes, local_dpalette); + dconfigOk = true; + } + } + if (!dconfigOk) { + qcolor_list = light_qpalette; + dcolor_list = light_dpalette; + } } for (int i = 0; i < DPalette::NColorRoles; ++i) { @@ -1006,13 +1097,13 @@ static void generatePaletteColor_helper(DPalette &base, M role, DGuiApplicationH QColor disable_mask_color, inactive_mask_color; if (type == DGuiApplicationHelper::DarkType) { - disable_mask_color = dark_qpalette[QPalette::Window]; - inactive_mask_color = dark_qpalette[QPalette::Window]; + disable_mask_color = paletteWindowColor(DGuiApplicationHelper::DarkType); + inactive_mask_color = disable_mask_color; disable_mask_color.setAlphaF(0.7); inactive_mask_color.setAlphaF(0.6); } else { - disable_mask_color = light_qpalette[QPalette::Window]; - inactive_mask_color = light_qpalette[QPalette::Window]; + disable_mask_color = paletteWindowColor(DGuiApplicationHelper::LightType); + inactive_mask_color = disable_mask_color; disable_mask_color.setAlphaF(0.6); inactive_mask_color.setAlphaF(0.4); }