Skins: Order skins depending on system color scheme

Resolves #320
This commit is contained in:
Peter Hartmann 2023-06-27 15:13:48 +02:00 committed by uwerat
parent 9a37a1ef9d
commit b84593a6f4
4 changed files with 47 additions and 6 deletions

View File

@ -1,4 +1,5 @@
{ {
"FactoryId": "Fluent2Factory", "FactoryId": "Fluent2Factory",
"Skins": [ "Fluent2 Light", "Fluent2 Dark" ] "Skins": [ { "Name": "Fluent2 Light", "Scheme": "Light" },
{ "Name": "Fluent2 Dark", "Scheme": "Dark" } ]
} }

View File

@ -1,4 +1,5 @@
{ {
"FactoryId": "Material3Factory", "FactoryId": "Material3Factory",
"Skins": [ "Material3 Light", "Material3 Dark" ] "Skins": [ { "Name": "Material3 Light", "Scheme": "Light" },
{ "Name": "Material3 Dark", "Scheme": "Dark" } ]
} }

View File

@ -1,4 +1,4 @@
{ {
"FactoryId": "SquiekFactory", "FactoryId": "SquiekFactory",
"Skins": [ "Squiek" ] "Skins": [ { "Name": "Squiek", "Scheme": "Unknown" } ]
} }

View File

@ -8,12 +8,14 @@
#include <qdir.h> #include <qdir.h>
#include <qglobalstatic.h> #include <qglobalstatic.h>
#include <qguiapplication.h>
#include <qjsonarray.h> #include <qjsonarray.h>
#include <qjsonobject.h> #include <qjsonobject.h>
#include <qmap.h> #include <qmap.h>
#include <qpluginloader.h> #include <qpluginloader.h>
#include <qpointer.h> #include <qpointer.h>
#include <qset.h> #include <qset.h>
#include <qstylehints.h>
static inline QStringList qskSplitPath( const QString& s ) static inline QStringList qskSplitPath( const QString& s )
{ {
@ -74,6 +76,8 @@ namespace
const QLatin1String TokenData( "MetaData" ); const QLatin1String TokenData( "MetaData" );
const QLatin1String TokenFactoryId( "FactoryId" ); const QLatin1String TokenFactoryId( "FactoryId" );
const QLatin1String TokenSkins( "Skins" ); const QLatin1String TokenSkins( "Skins" );
const QLatin1String TokenName( "Name" );
const QLatin1String TokenScheme( "Scheme" );
const QLatin1String InterfaceId( QskSkinFactoryIID ); const QLatin1String InterfaceId( QskSkinFactoryIID );
@ -94,10 +98,45 @@ namespace
m_factoryId = QStringLiteral( "skin_factory_" ) + QString::number( i++ ); m_factoryId = QStringLiteral( "skin_factory_" ) + QString::number( i++ );
} }
const auto skinNames = factoryData.value( TokenSkins ).toArray(); const auto skins = factoryData.value( TokenSkins ).toArray();
for ( const auto& name : skinNames ) for ( const auto& skin : skins )
m_skinNames += name.toString(); {
const auto& skinObject = skin.toObject();
const auto& name = skinObject.value( TokenName ).toString();
#if QT_VERSION >= QT_VERSION_CHECK( 6, 5, 0 )
const auto& schemeString = skinObject.value( TokenScheme ).toString();
Qt::ColorScheme scheme;
if( schemeString == QStringLiteral( "Light" ) )
{
scheme = Qt::ColorScheme::Light;
}
else if( schemeString == QStringLiteral( "Dark" ) )
{
scheme = Qt::ColorScheme::Dark;
}
else
{
scheme = Qt::ColorScheme::Unknown;
}
const auto systemScheme = qGuiApp->styleHints()->colorScheme();
if( scheme == systemScheme )
{
m_skinNames.prepend( name );
}
else
{
m_skinNames.append( name );
}
#else
Q_UNUSED( TokenScheme )
m_skinNames += name;
#endif
}
} }
return !m_skinNames.isEmpty(); return !m_skinNames.isEmpty();