5555#endif
5656
5757#include " orgdeepindtkpreference.hpp"
58+ #include " orgdeepindtkpalette.hpp"
59+ #include " orgdeepindtkpalettedark.hpp"
5860
5961#ifdef Q_OS_UNIX
6062class EnvReplaceGuard
@@ -151,6 +153,13 @@ Q_GLOBAL_STATIC(DFontManager, _globalFM)
151153Q_GLOBAL_STATIC_WITH_ARGS (OrgDeepinDTKPreference, _d_dconfig, (DTK_CORE_NAMESPACE ::DConfig::globalThread(), nullptr ,
152154 " org.deepin.dtk.preference" , DTK_CORE_NAMESPACE ::DSGApplication::id(), {}, false , nullptr ))
153155
156+ Q_GLOBAL_STATIC_WITH_ARGS (OrgDeepinDTKPalette, _d_palette_light,
157+ (DTK_CORE_NAMESPACE ::DConfig::globalThread(), nullptr ,
158+ " org.deepin.dtk.palette" , DTK_CORE_NAMESPACE ::DSGApplication::id(), {}, false , nullptr ))
159+ Q_GLOBAL_STATIC_WITH_ARGS (OrgDeepinDTKPaletteDark, _d_palette_dark,
160+ (DTK_CORE_NAMESPACE ::DConfig::globalThread(), nullptr ,
161+ " org.deepin.dtk.palette.dark" , DTK_CORE_NAMESPACE ::DSGApplication::id(), {}, false , nullptr ))
162+
154163// Keys allowed to be forwarded between single-instance client and server.
155164static const QStringList s_singleInstanceForwardEnvKeys = {
156165 QStringLiteral (" XDG_ACTIVATION_TOKEN" )
@@ -866,6 +875,78 @@ static QColor dark_dpalette[DPalette::NColorTypes] {
866875 QColor (255 , 255 , 255 , 0.1 * 255 ) // ObviousBackground
867876};
868877
878+ // Generation tracking for dconfig palette cache invalidation
879+ static QAtomicInt s_paletteGeneration (0 );
880+ static QAtomicInt s_paletteDConfigConnected (0 );
881+
882+ static void ensurePaletteDConfigConnected ()
883+ {
884+ if (s_paletteDConfigConnected.testAndSetOrdered (0 , 1 )) {
885+ QObject::connect (_d_palette_light.operator ()(), &OrgDeepinDTKPalette::valueChanged,
886+ qApp, [](const QString &, const QVariant &) { s_paletteGeneration.fetchAndAddOrdered (1 ); });
887+ QObject::connect (_d_palette_dark.operator ()(), &OrgDeepinDTKPaletteDark::valueChanged,
888+ qApp, [](const QString &, const QVariant &) { s_paletteGeneration.fetchAndAddOrdered (1 ); });
889+ }
890+ }
891+
892+ template <typename ConfigT>
893+ static QColor paletteQColorFromDConfig (ConfigT *config, QPalette::ColorRole role, const QColor &fallback)
894+ {
895+ if (!config || !config->isInitializeSucceeded ())
896+ return fallback;
897+ QString str;
898+ switch (role) {
899+ case QPalette::WindowText: str = config->windowText (); break ;
900+ case QPalette::Button: str = config->button (); break ;
901+ case QPalette::Light: str = config->light (); break ;
902+ case QPalette::Midlight: str = config->midlight (); break ;
903+ case QPalette::Dark: str = config->dark (); break ;
904+ case QPalette::Mid: str = config->mid (); break ;
905+ case QPalette::Text: str = config->text (); break ;
906+ case QPalette::BrightText: str = config->brightText (); break ;
907+ case QPalette::ButtonText: str = config->buttonText (); break ;
908+ case QPalette::Base: str = config->base (); break ;
909+ case QPalette::Window: str = config->window (); break ;
910+ case QPalette::Shadow: str = config->shadow (); break ;
911+ case QPalette::Highlight: str = config->highlight (); break ;
912+ case QPalette::HighlightedText: str = config->highlightedText (); break ;
913+ case QPalette::Link: str = config->link (); break ;
914+ case QPalette::LinkVisited: str = config->linkVisited (); break ;
915+ case QPalette::AlternateBase: str = config->alternateBase (); break ;
916+ case QPalette::NoRole: str = config->noRole (); break ;
917+ case QPalette::ToolTipBase: str = config->toolTipBase (); break ;
918+ case QPalette::ToolTipText: str = config->toolTipText (); break ;
919+ default : return fallback;
920+ }
921+ QColor c (str);
922+ return c.isValid () ? c : fallback;
923+ }
924+
925+ template <typename ConfigT>
926+ static QColor paletteDColorFromDConfig (ConfigT *config, DPalette::ColorType type, const QColor &fallback)
927+ {
928+ if (!config || !config->isInitializeSucceeded ())
929+ return fallback;
930+ QString str;
931+ switch (type) {
932+ case DPalette::NoType: str = config->noType (); break ;
933+ case DPalette::ItemBackground: str = config->itemBackground (); break ;
934+ case DPalette::TextTitle: str = config->textTitle (); break ;
935+ case DPalette::TextTips: str = config->textTips (); break ;
936+ case DPalette::TextWarning: str = config->textWarning (); break ;
937+ case DPalette::TextLively: str = config->textLively (); break ;
938+ case DPalette::LightLively: str = config->lightLively (); break ;
939+ case DPalette::DarkLively: str = config->darkLively (); break ;
940+ case DPalette::FrameBorder: str = config->frameBorder (); break ;
941+ case DPalette::PlaceholderText: str = config->placeholderText (); break ;
942+ case DPalette::FrameShadowBorder: str = config->frameShadowBorder (); break ;
943+ case DPalette::ObviousBackground: str = config->obviousBackground (); break ;
944+ default : return fallback;
945+ }
946+ QColor c (str);
947+ return c.isValid () ? c : fallback;
948+ }
949+
869950/* !
870951 \brief 根据主题获取标准调色板.
871952
@@ -874,29 +955,50 @@ static QColor dark_dpalette[DPalette::NColorTypes] {
874955 */
875956DPalette DGuiApplicationHelper::standardPalette (DGuiApplicationHelper::ColorType type)
876957{
877- static const DPalette *light_palette = nullptr , *dark_palette = nullptr ;
878- static const DPalette *alpha_light_palette = nullptr , *alpha_dark_palette = nullptr ;
958+ Q_ASSERT_X (QThread::currentThread () == qApp->thread (),
959+ " DGuiApplicationHelper::standardPalette" ,
960+ " standardPalette() must be called on the GUI thread" );
961+
962+ ensurePaletteDConfigConnected ();
963+
964+ static QAtomicPointer<const DPalette> light_palette, dark_palette;
965+ static QAtomicPointer<const DPalette> alpha_light_palette, alpha_dark_palette;
966+ static int last_generation = -1 ;
967+
879968 const bool allowCompositingColor = DGuiApplicationHelper::testAttribute (ColorCompositing);
880969
970+ int current_gen = s_paletteGeneration.loadRelaxed ();
971+ if (current_gen != last_generation) {
972+ last_generation = current_gen;
973+ delete light_palette.fetchAndStoreRelaxed (nullptr );
974+ delete dark_palette.fetchAndStoreRelaxed (nullptr );
975+ delete alpha_light_palette.fetchAndStoreRelaxed (nullptr );
976+ delete alpha_dark_palette.fetchAndStoreRelaxed (nullptr );
977+ }
978+
881979 if (type == LightType) {
882980 if (Q_UNLIKELY (allowCompositingColor)) {
883- if (Q_LIKELY (alpha_light_palette)) {
884- return *alpha_light_palette;
981+ const DPalette *p = alpha_light_palette.loadRelaxed ();
982+ if (Q_LIKELY (p)) {
983+ return *p;
885984 }
886985 }
887986
888- if (Q_LIKELY (light_palette)) {
889- return *light_palette;
987+ const DPalette *p = light_palette.loadRelaxed ();
988+ if (Q_LIKELY (p)) {
989+ return *p;
890990 }
891991 } else if (type == DarkType) {
892992 if (Q_UNLIKELY (allowCompositingColor)) {
893- if (Q_LIKELY (alpha_dark_palette)) {
894- return *alpha_dark_palette;
993+ const DPalette *p = alpha_dark_palette.loadRelaxed ();
994+ if (Q_LIKELY (p)) {
995+ return *p;
895996 }
896997 }
897998
898- if (Q_LIKELY (dark_palette)) {
899- return *dark_palette;
999+ const DPalette *p = dark_palette.loadRelaxed ();
1000+ if (Q_LIKELY (p)) {
1001+ return *p;
9001002 }
9011003 } else {
9021004 return DPalette ();
@@ -909,31 +1011,35 @@ DPalette DGuiApplicationHelper::standardPalette(DGuiApplicationHelper::ColorType
9091011 pa = new DPalette ();
9101012
9111013 if (allowCompositingColor)
912- alpha_dark_palette = pa ;
1014+ alpha_dark_palette. storeRelaxed (pa) ;
9131015 else
914- dark_palette = pa ;
1016+ dark_palette. storeRelaxed (pa) ;
9151017
9161018 qcolor_list = dark_qpalette;
9171019 dcolor_list = dark_dpalette;
9181020 } else {
9191021 pa = new DPalette ();
9201022
9211023 if (allowCompositingColor)
922- alpha_light_palette = pa ;
1024+ alpha_light_palette. storeRelaxed (pa) ;
9231025 else
924- light_palette = pa ;
1026+ light_palette. storeRelaxed (pa) ;
9251027
9261028 qcolor_list = light_qpalette;
9271029 dcolor_list = light_dpalette;
9281030 }
9291031
9301032 for (int i = 0 ; i < DPalette::NColorRoles; ++i) {
9311033 QPalette::ColorRole role = static_cast <QPalette::ColorRole>(i);
932- QColor color = qcolor_list[i];
1034+ QColor color = (type == DarkType)
1035+ ? paletteQColorFromDConfig (_d_palette_dark.operator ()(), role, qcolor_list[i])
1036+ : paletteQColorFromDConfig (_d_palette_light.operator ()(), role, qcolor_list[i]);
9331037
9341038#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
9351039 if (role == QPalette::PlaceholderText) {
936- color = dcolor_list[DPalette::PlaceholderText];
1040+ color = (type == DarkType)
1041+ ? paletteDColorFromDConfig (_d_palette_dark.operator ()(), DPalette::PlaceholderText, dcolor_list[DPalette::PlaceholderText])
1042+ : paletteDColorFromDConfig (_d_palette_light.operator ()(), DPalette::PlaceholderText, dcolor_list[DPalette::PlaceholderText]);
9371043 }
9381044#endif
9391045 // 处理半透明色
@@ -970,7 +1076,9 @@ DPalette DGuiApplicationHelper::standardPalette(DGuiApplicationHelper::ColorType
9701076
9711077 for (int i = 0 ; i < DPalette::NColorTypes; ++i) {
9721078 DPalette::ColorType role = static_cast <DPalette::ColorType>(i);
973- QColor color = dcolor_list[i];
1079+ QColor color = (type == DarkType)
1080+ ? paletteDColorFromDConfig (_d_palette_dark.operator ()(), role, dcolor_list[i])
1081+ : paletteDColorFromDConfig (_d_palette_light.operator ()(), role, dcolor_list[i]);
9741082
9751083 // 处理半透明色
9761084 if (allowCompositingColor) {
@@ -1006,13 +1114,13 @@ static void generatePaletteColor_helper(DPalette &base, M role, DGuiApplicationH
10061114 QColor disable_mask_color, inactive_mask_color;
10071115
10081116 if (type == DGuiApplicationHelper::DarkType) {
1009- disable_mask_color = dark_qpalette[QPalette::Window];
1010- inactive_mask_color = dark_qpalette[QPalette::Window] ;
1117+ disable_mask_color = paletteQColorFromDConfig (_d_palette_dark. operator ()(), QPalette::Window, dark_qpalette[QPalette::Window]) ;
1118+ inactive_mask_color = disable_mask_color ;
10111119 disable_mask_color.setAlphaF (0.7 );
10121120 inactive_mask_color.setAlphaF (0.6 );
10131121 } else {
1014- disable_mask_color = light_qpalette[QPalette::Window];
1015- inactive_mask_color = light_qpalette[QPalette::Window] ;
1122+ disable_mask_color = paletteQColorFromDConfig (_d_palette_light. operator ()(), QPalette::Window, light_qpalette[QPalette::Window]) ;
1123+ inactive_mask_color = disable_mask_color ;
10161124 disable_mask_color.setAlphaF (0.6 );
10171125 inactive_mask_color.setAlphaF (0.4 );
10181126 }
0 commit comments