skins: Switch automatically when color scheme changes
This commit is contained in:
parent
d33e1f1a90
commit
2f7564333f
|
|
@ -146,12 +146,25 @@ namespace
|
|||
private:
|
||||
void populateMenu( QskMenu* menu ) override
|
||||
{
|
||||
const auto names = qskSkinManager->skinNames();
|
||||
const auto skins = qskSkinManager->skinInfos();
|
||||
|
||||
for ( const auto& name : names )
|
||||
menu->addOption( QUrl(), name );
|
||||
for ( const auto& skin : skins )
|
||||
{
|
||||
auto text = skin.first;
|
||||
|
||||
if ( const auto index = names.indexOf( qskSetup->skinName() ) )
|
||||
if( skin.second == QskSkin::LightScheme )
|
||||
{
|
||||
text.append( " Light" );
|
||||
}
|
||||
else if( skin.second == QskSkin::DarkScheme )
|
||||
{
|
||||
text.append( " Dark" );
|
||||
}
|
||||
|
||||
menu->addOption( QUrl(),text );
|
||||
}
|
||||
|
||||
if ( const auto index = skins.indexOf( qskSetup->skinInfo() ) )
|
||||
menu->setCurrentIndex( index );
|
||||
|
||||
connect( menu, &QskMenu::triggered, this, &SkinButton::changeSkin );
|
||||
|
|
@ -159,10 +172,10 @@ namespace
|
|||
|
||||
void changeSkin( int index )
|
||||
{
|
||||
const auto names = qskSkinManager->skinNames();
|
||||
const auto names = qskSkinManager->skinInfos();
|
||||
|
||||
if ( ( index >= 0 ) && ( index < names.size() )
|
||||
&& ( index != names.indexOf( qskSetup->skinName() ) ) )
|
||||
&& ( index != names.indexOf( qskSetup->skinInfo() ) ) )
|
||||
{
|
||||
Skinny::setSkin( index, 500 );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,21 +33,25 @@ namespace
|
|||
{
|
||||
}
|
||||
|
||||
QStringList skinNames() const override
|
||||
QVector< QskSkin::SkinInfo > skins() const override
|
||||
{
|
||||
return { "DaytimeSkin", "NighttimeSkin" };
|
||||
return { qMakePair( QStringLiteral( "Skin" ), QskSkin::LightScheme ),
|
||||
qMakePair( QStringLiteral( "Skin" ), QskSkin::DarkScheme ) };
|
||||
}
|
||||
|
||||
QskSkin* createSkin( const QString& skinName ) override
|
||||
QskSkin* createSkin( QskSkin::SkinInfo info ) override
|
||||
{
|
||||
if( skinName == "DaytimeSkin" )
|
||||
if( info.first == "Skin" )
|
||||
{
|
||||
return new DaytimeSkin;
|
||||
}
|
||||
if( info.second == QskSkin::LightScheme )
|
||||
{
|
||||
return new DaytimeSkin;
|
||||
}
|
||||
|
||||
if( skinName == "NighttimeSkin" )
|
||||
{
|
||||
return new NighttimeSkin;
|
||||
if( info.second == QskSkin::DarkScheme )
|
||||
{
|
||||
return new NighttimeSkin;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
|
|
@ -76,7 +80,7 @@ int main( int argc, char* argv[] )
|
|||
qskSkinManager->registerFactory(
|
||||
QStringLiteral( "SampleSkinFactory" ), new SkinFactory() );
|
||||
|
||||
qskSetup->setSkin( "DaytimeSkin" );
|
||||
qskSetup->setSkin( { "Skin", QskSkin::LightScheme } );
|
||||
|
||||
#ifdef USE_SHORTCUTS
|
||||
// With CTRL-B you can rotate a couple of visual debug modes
|
||||
|
|
|
|||
|
|
@ -222,17 +222,18 @@ namespace
|
|||
};
|
||||
}
|
||||
|
||||
QStringList MySkinFactory::skinNames() const
|
||||
QVector< QskSkin::SkinInfo > MySkinFactory::skins() const
|
||||
{
|
||||
return { QStringLiteral( "Blue" ), QStringLiteral( "Pink" ) };
|
||||
return { qMakePair( QStringLiteral( "Blue" ), QskSkin::UnknownScheme ),
|
||||
qMakePair( QStringLiteral( "Pink" ), QskSkin::UnknownScheme ) };
|
||||
}
|
||||
|
||||
QskSkin* MySkinFactory::createSkin( const QString& skinName )
|
||||
QskSkin* MySkinFactory::createSkin( QskSkin::SkinInfo info )
|
||||
{
|
||||
if ( skinName == QStringLiteral( "Blue" ) )
|
||||
if ( info.first == QStringLiteral( "Blue" ) )
|
||||
return new SkinBlue();
|
||||
|
||||
if ( skinName == QStringLiteral( "Pink" ) )
|
||||
if ( info.first == QStringLiteral( "Pink" ) )
|
||||
return new SkinPink();
|
||||
|
||||
return nullptr;
|
||||
|
|
|
|||
|
|
@ -13,6 +13,6 @@ class MySkinFactory : public QskSkinFactory
|
|||
using Inherited = QskSkinFactory;
|
||||
|
||||
public:
|
||||
QStringList skinNames() const override;
|
||||
QskSkin* createSkin( const QString& skinName ) override;
|
||||
virtual QVector< QskSkin::SkinInfo > skins() const override;
|
||||
virtual QskSkin* createSkin( QskSkin::SkinInfo ) override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -79,8 +79,8 @@ class Window : public QskWindow
|
|||
{
|
||||
auto button = new MyToggleButton();
|
||||
|
||||
button->setText( false, alternativeSkin( false ) );
|
||||
button->setText( true, alternativeSkin( true ) );
|
||||
button->setText( false, alternativeSkin( false ).first );
|
||||
button->setText( true, alternativeSkin( true ).first );
|
||||
button->setLayoutAlignmentHint( Qt::AlignRight );
|
||||
|
||||
auto box = new QskLinearBox( Qt::Vertical );
|
||||
|
|
@ -120,10 +120,10 @@ class Window : public QskWindow
|
|||
delete oldSkin;
|
||||
}
|
||||
|
||||
QString alternativeSkin( bool on ) const
|
||||
QskSkin::SkinInfo alternativeSkin( bool on ) const
|
||||
{
|
||||
const auto skinNames = qskSkinManager->skinNames();
|
||||
return skinNames[ on ];
|
||||
const auto skins = qskSkinManager->skinInfos();
|
||||
return skins[ on ];
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -75,14 +75,14 @@ namespace
|
|||
|
||||
}
|
||||
|
||||
QStringList SkinFactory::skinNames() const
|
||||
QVector< QskSkin::SkinInfo > SkinFactory::skins() const
|
||||
{
|
||||
return { "Skin" };
|
||||
return { qMakePair( QStringLiteral( "Skin" ), QskSkin::UnknownScheme ) };
|
||||
}
|
||||
|
||||
QskSkin* SkinFactory::createSkin( const QString& skinName )
|
||||
QskSkin* SkinFactory::createSkin( QskSkin::SkinInfo info )
|
||||
{
|
||||
if ( skinName == "Skin" )
|
||||
if ( info.first == "Skin" )
|
||||
return new Skin();
|
||||
|
||||
return nullptr;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,6 @@ class SkinFactory : public QskSkinFactory
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QStringList skinNames() const override;
|
||||
QskSkin* createSkin( const QString& ) override;
|
||||
QVector< QskSkin::SkinInfo > skins() const override;
|
||||
QskSkin* createSkin( QskSkin::SkinInfo ) override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -208,7 +208,7 @@ int main( int argc, char** argv )
|
|||
|
||||
Skinny::init(); // we need a skin
|
||||
SkinnyShortcut::enable( SkinnyShortcut::Quit | SkinnyShortcut::DebugBackground );
|
||||
qskSetup->setSkin( "squiek" );
|
||||
qskSetup->setSkin( qMakePair( QStringLiteral( "squiek" ), QskSkin::UnknownScheme ) );
|
||||
|
||||
QskWindow window;
|
||||
window.setColor( QskRgb::Wheat );
|
||||
|
|
|
|||
|
|
@ -27,10 +27,10 @@ QskMain::QskMain( QObject* parent )
|
|||
this, &QskMain::itemUpdateFlagsChanged, Qt::QueuedConnection );
|
||||
}
|
||||
|
||||
QStringList QskMain::skinList() const
|
||||
QVector< QskSkin::SkinInfo > QskMain::skinList() const
|
||||
{
|
||||
auto manager = QskSkinManager::instance();
|
||||
return manager ? manager->skinNames() : QStringList();
|
||||
return manager ? manager->skinInfos() : QVector< QskSkin::SkinInfo >();
|
||||
}
|
||||
|
||||
QQmlListProperty< QObject > QskMain::data()
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include "QskQmlGlobal.h"
|
||||
|
||||
#include <QskSetup.h>
|
||||
#include <QskSkin.h>
|
||||
|
||||
#include <qobject.h>
|
||||
#include <qqmllist.h>
|
||||
|
|
@ -32,10 +33,10 @@ class QskMain : public QObject
|
|||
public:
|
||||
Q_OBJECT
|
||||
|
||||
Q_PRIVATE_PROPERTY( setup(), QString skin READ skinName
|
||||
Q_PRIVATE_PROPERTY( setup(), QskSkin::SkinInfo skin READ skinInfo
|
||||
WRITE setSkin NOTIFY skinChanged )
|
||||
|
||||
Q_PROPERTY( QStringList skinList READ skinList NOTIFY skinListChanged )
|
||||
Q_PROPERTY( QVector< QskSkin::SkinInfo > skinList READ skinList NOTIFY skinListChanged )
|
||||
|
||||
Q_PRIVATE_PROPERTY( setup(), QskSetupFlagsQml itemUpdateFlags
|
||||
READ itemUpdateFlags WRITE setItemUpdateFlags NOTIFY itemUpdateFlagsChanged )
|
||||
|
|
@ -46,7 +47,7 @@ class QskMain : public QObject
|
|||
public:
|
||||
QskMain( QObject* parent = nullptr );
|
||||
|
||||
QStringList skinList() const;
|
||||
QVector< QskSkin::SkinInfo > skinList() const;
|
||||
QQmlListProperty< QObject > data();
|
||||
|
||||
Q_SIGNALS:
|
||||
|
|
|
|||
|
|
@ -7,8 +7,7 @@
|
|||
#include "QskFluent2Skin.h"
|
||||
#include "QskFluent2Theme.h"
|
||||
|
||||
static const QString nameLight = QStringLiteral( "Fluent2 Light" );
|
||||
static const QString nameDark = QStringLiteral( "Fluent2 Dark" );
|
||||
static const QString fluent2SkinName = QStringLiteral( "Fluent2" );
|
||||
|
||||
namespace
|
||||
{
|
||||
|
|
@ -27,27 +26,16 @@ QskFluent2SkinFactory::~QskFluent2SkinFactory()
|
|||
{
|
||||
}
|
||||
|
||||
QStringList QskFluent2SkinFactory::skinNames() const
|
||||
QVector< QskSkin::SkinInfo > QskFluent2SkinFactory::skins() const
|
||||
{
|
||||
return { nameLight, nameDark };
|
||||
return { qMakePair( fluent2SkinName, QskSkin::LightScheme ),
|
||||
qMakePair( fluent2SkinName, QskSkin::DarkScheme ) };
|
||||
}
|
||||
|
||||
QskSkin* QskFluent2SkinFactory::createSkin( const QString& skinName )
|
||||
QskSkin* QskFluent2SkinFactory::createSkin( QskSkin::SkinInfo info )
|
||||
{
|
||||
QskSkin::ColorScheme colorScheme;
|
||||
|
||||
if ( QString::compare( skinName, nameLight, Qt::CaseInsensitive ) == 0 )
|
||||
{
|
||||
colorScheme = QskSkin::LightScheme;
|
||||
}
|
||||
else if ( QString::compare( skinName, nameDark, Qt::CaseInsensitive ) == 0 )
|
||||
{
|
||||
colorScheme = QskSkin::DarkScheme;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
// default to light:
|
||||
const auto colorScheme = info.second == QskSkin::UnknownScheme ? QskSkin::LightScheme : info.second;
|
||||
|
||||
struct
|
||||
{
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ class QSK_FLUENT2_EXPORT QskFluent2SkinFactory : public QskSkinFactory
|
|||
QskFluent2SkinFactory( QObject* parent = nullptr );
|
||||
~QskFluent2SkinFactory() override;
|
||||
|
||||
QStringList skinNames() const override;
|
||||
QskSkin* createSkin( const QString& skinName ) override;
|
||||
QVector< QskSkin::SkinInfo > skins() const override;
|
||||
QskSkin* createSkin( QskSkin::SkinInfo ) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"FactoryId": "Fluent2Factory",
|
||||
"Skins": [ { "Name": "Fluent2 Light", "Scheme": "Light" },
|
||||
{ "Name": "Fluent2 Dark", "Scheme": "Dark" } ]
|
||||
"Skins": [ { "Name": "Fluent2", "Scheme": "LightScheme" },
|
||||
{ "Name": "Fluent2", "Scheme": "DarkScheme" } ]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,7 @@
|
|||
#include "QskMaterial3SkinFactory.h"
|
||||
#include "QskMaterial3Skin.h"
|
||||
|
||||
static const QString materialLightSkinName = QStringLiteral( "Material3 Light" );
|
||||
static const QString materialDarkSkinName = QStringLiteral( "Material3 Dark" );
|
||||
static const QString materialSkinName = QStringLiteral( "Material3" );
|
||||
|
||||
QskMaterial3SkinFactory::QskMaterial3SkinFactory( QObject* parent )
|
||||
: QskSkinFactory( parent )
|
||||
|
|
@ -18,21 +17,20 @@ QskMaterial3SkinFactory::~QskMaterial3SkinFactory()
|
|||
{
|
||||
}
|
||||
|
||||
QStringList QskMaterial3SkinFactory::skinNames() const
|
||||
QVector< QskSkin::SkinInfo > QskMaterial3SkinFactory::skins() const
|
||||
{
|
||||
return { materialLightSkinName, materialDarkSkinName };
|
||||
return { qMakePair( materialSkinName, QskSkin::LightScheme ),
|
||||
qMakePair( materialSkinName, QskSkin::DarkScheme ) };
|
||||
}
|
||||
|
||||
QskSkin* QskMaterial3SkinFactory::createSkin( const QString& skinName )
|
||||
QskSkin* QskMaterial3SkinFactory::createSkin( QskSkin::SkinInfo info )
|
||||
{
|
||||
if ( QString::compare( skinName, materialLightSkinName, Qt::CaseInsensitive ) == 0 )
|
||||
// default to light:
|
||||
const auto scheme = info.second == QskSkin::UnknownScheme ? QskSkin::LightScheme : info.second;
|
||||
|
||||
if( QString::compare( info.first, materialSkinName, Qt::CaseInsensitive ) == 0 )
|
||||
{
|
||||
QskMaterial3Theme theme( QskSkin::LightScheme );
|
||||
return new QskMaterial3Skin( theme );
|
||||
}
|
||||
else if ( QString::compare( skinName, materialDarkSkinName, Qt::CaseInsensitive ) == 0 )
|
||||
{
|
||||
QskMaterial3Theme theme( QskSkin::DarkScheme );
|
||||
QskMaterial3Theme theme( scheme );
|
||||
return new QskMaterial3Skin( theme );
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ class QSK_MATERIAL3_EXPORT QskMaterial3SkinFactory : public QskSkinFactory
|
|||
QskMaterial3SkinFactory( QObject* parent = nullptr );
|
||||
~QskMaterial3SkinFactory() override;
|
||||
|
||||
QStringList skinNames() const override;
|
||||
QskSkin* createSkin( const QString& skinName ) override;
|
||||
QVector< QskSkin::SkinInfo > skins() const override;
|
||||
QskSkin* createSkin( QskSkin::SkinInfo ) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"FactoryId": "Material3Factory",
|
||||
"Skins": [ { "Name": "Material3 Light", "Scheme": "Light" },
|
||||
{ "Name": "Material3 Dark", "Scheme": "Dark" } ]
|
||||
"Skins": [ { "Name": "Material3", "Scheme": "LightScheme" },
|
||||
{ "Name": "Material3", "Scheme": "DarkScheme" } ]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,15 +17,17 @@ QskSquiekSkinFactory::~QskSquiekSkinFactory()
|
|||
{
|
||||
}
|
||||
|
||||
QStringList QskSquiekSkinFactory::skinNames() const
|
||||
QVector< QskSkin::SkinInfo > QskSquiekSkinFactory::skins() const
|
||||
{
|
||||
return { squiekSkinName };
|
||||
return { qMakePair( squiekSkinName, QskSkin::UnknownScheme ) };
|
||||
}
|
||||
|
||||
QskSkin* QskSquiekSkinFactory::createSkin( const QString& skinName )
|
||||
QskSkin* QskSquiekSkinFactory::createSkin( QskSkin::SkinInfo info )
|
||||
{
|
||||
if ( QString::compare( skinName, squiekSkinName, Qt::CaseInsensitive ) == 0 )
|
||||
if ( QString::compare( info.first, squiekSkinName, Qt::CaseInsensitive ) == 0 )
|
||||
{
|
||||
return new QskSquiekSkin();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ class QSK_SQUIEK_EXPORT QskSquiekSkinFactory : public QskSkinFactory
|
|||
QskSquiekSkinFactory( QObject* parent = nullptr );
|
||||
~QskSquiekSkinFactory() override;
|
||||
|
||||
QStringList skinNames() const override;
|
||||
QskSkin* createSkin( const QString& skinName ) override;
|
||||
QVector< QskSkin::SkinInfo > skins() const override;
|
||||
QskSkin* createSkin( QskSkin::SkinInfo ) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
{
|
||||
"FactoryId": "SquiekFactory",
|
||||
"Skins": [ { "Name": "Squiek", "Scheme": "Unknown" } ]
|
||||
"Skins": [ { "Name": "Squiek", "Scheme": "UnknownScheme" } ]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ class QskSetup::PrivateData
|
|||
{
|
||||
}
|
||||
|
||||
QString skinName;
|
||||
QskSkin::SkinInfo skinInfo;
|
||||
QPointer< QskSkin > skin;
|
||||
|
||||
QskGraphicProviderMap graphicProviders;
|
||||
|
|
@ -91,6 +91,28 @@ class QskSetup::PrivateData
|
|||
QskSetup::QskSetup()
|
||||
: m_data( new PrivateData() )
|
||||
{
|
||||
#if QT_VERSION >= QT_VERSION_CHECK( 6, 5, 0 )
|
||||
const auto* hints = qGuiApp->styleHints();
|
||||
|
||||
connect( hints, &QStyleHints::colorSchemeChanged, this, [this]( Qt::ColorScheme scheme )
|
||||
{
|
||||
auto info = m_data->skinInfo;
|
||||
|
||||
const auto currentScheme = static_cast< Qt::ColorScheme >( info.second );
|
||||
|
||||
if( currentScheme == scheme )
|
||||
return;
|
||||
|
||||
info.second = static_cast< QskSkin::ColorScheme >( scheme );
|
||||
|
||||
const auto skins = QskSkinManager::instance()->skinInfos();
|
||||
|
||||
if( skins.contains( info ) )
|
||||
{
|
||||
setSkin( info );
|
||||
}
|
||||
} );
|
||||
#endif
|
||||
}
|
||||
|
||||
QskSetup::~QskSetup()
|
||||
|
|
@ -152,12 +174,12 @@ bool QskSetup::testItemUpdateFlag( QskQuickItem::UpdateFlag flag )
|
|||
return m_data->itemUpdateFlags.testFlag( flag );
|
||||
}
|
||||
|
||||
QskSkin* QskSetup::setSkin( const QString& skinName )
|
||||
QskSkin* QskSetup::setSkin( QskSkin::SkinInfo info )
|
||||
{
|
||||
if ( m_data->skin && ( skinName == m_data->skinName ) )
|
||||
if ( m_data->skin && ( info == m_data->skinInfo ) )
|
||||
return m_data->skin;
|
||||
|
||||
auto skin = QskSkinManager::instance()->createSkin( skinName );
|
||||
auto skin = QskSkinManager::instance()->createSkin( info );
|
||||
if ( skin == nullptr )
|
||||
return nullptr;
|
||||
|
||||
|
|
@ -167,7 +189,7 @@ QskSkin* QskSetup::setSkin( const QString& skinName )
|
|||
const auto oldSkin = m_data->skin;
|
||||
|
||||
m_data->skin = skin;
|
||||
m_data->skinName = skinName;
|
||||
m_data->skinInfo = info;
|
||||
|
||||
if ( oldSkin )
|
||||
{
|
||||
|
|
@ -180,20 +202,19 @@ QskSkin* QskSetup::setSkin( const QString& skinName )
|
|||
return m_data->skin;
|
||||
}
|
||||
|
||||
QString QskSetup::skinName() const
|
||||
QskSkin::SkinInfo QskSetup::skinInfo() const
|
||||
{
|
||||
return m_data->skinName;
|
||||
return m_data->skinInfo;
|
||||
}
|
||||
|
||||
QskSkin* QskSetup::skin()
|
||||
{
|
||||
if ( m_data->skin == nullptr )
|
||||
{
|
||||
m_data->skin = QskSkinManager::instance()->createSkin( QString() );
|
||||
m_data->skin = QskSkinManager::instance()->createSkin( {} );
|
||||
Q_ASSERT( m_data->skin );
|
||||
|
||||
m_data->skin->setParent( this );
|
||||
m_data->skinName = m_data->skin->objectName();
|
||||
}
|
||||
|
||||
return m_data->skin;
|
||||
|
|
|
|||
|
|
@ -8,11 +8,11 @@
|
|||
|
||||
#include "QskGlobal.h"
|
||||
#include "QskQuickItem.h"
|
||||
#include "QskSkin.h"
|
||||
|
||||
#include <qobject.h>
|
||||
#include <memory>
|
||||
|
||||
class QskSkin;
|
||||
class QQuickItem;
|
||||
class QskGraphicProvider;
|
||||
|
||||
|
|
@ -38,8 +38,8 @@ class QSK_EXPORT QskSetup : public QObject
|
|||
void resetItemUpdateFlag( QskQuickItem::UpdateFlag );
|
||||
bool testItemUpdateFlag( QskQuickItem::UpdateFlag );
|
||||
|
||||
QskSkin* setSkin( const QString& );
|
||||
QString skinName() const;
|
||||
QskSkin* setSkin( QskSkin::SkinInfo );
|
||||
QskSkin::SkinInfo skinInfo() const;
|
||||
|
||||
QskSkin* skin();
|
||||
|
||||
|
|
|
|||
|
|
@ -58,6 +58,8 @@ class QSK_EXPORT QskSkin : public QObject
|
|||
Q_ENUM( ColorScheme )
|
||||
#endif
|
||||
|
||||
typedef QPair< QString, QskSkin::ColorScheme > SkinInfo;
|
||||
|
||||
QskSkin( QObject* parent = nullptr );
|
||||
~QskSkin() override;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@
|
|||
#define QSK_SKIN_FACTORY_H
|
||||
|
||||
#include "QskGlobal.h"
|
||||
#include <qobject.h>
|
||||
#include "QskSkin.h"
|
||||
|
||||
class QskSkin;
|
||||
#include <qobject.h>
|
||||
|
||||
class QSK_EXPORT QskSkinFactory : public QObject
|
||||
{
|
||||
|
|
@ -19,8 +19,8 @@ class QSK_EXPORT QskSkinFactory : public QObject
|
|||
QskSkinFactory( QObject* parent = nullptr );
|
||||
~QskSkinFactory() override;
|
||||
|
||||
virtual QStringList skinNames() const = 0;
|
||||
virtual QskSkin* createSkin( const QString& skinName ) = 0;
|
||||
virtual QVector< QskSkin::SkinInfo > skins() const = 0;
|
||||
virtual QskSkin* createSkin( QskSkin::SkinInfo ) = 0;
|
||||
};
|
||||
|
||||
#define QskSkinFactoryIID "org.qskinny.Qsk.QskSkinFactory/1.0"
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "QskSkinManager.h"
|
||||
#include "QskSkinFactory.h"
|
||||
#include "QskSkin.h"
|
||||
|
||||
#include <qdir.h>
|
||||
#include <qglobalstatic.h>
|
||||
|
|
@ -104,41 +105,42 @@ namespace
|
|||
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;
|
||||
QskSkin::ColorScheme scheme;
|
||||
|
||||
if( schemeString == QStringLiteral( "Light" ) )
|
||||
if( schemeString == QStringLiteral( "LightScheme" ) )
|
||||
{
|
||||
scheme = Qt::ColorScheme::Light;
|
||||
scheme = QskSkin::LightScheme;
|
||||
}
|
||||
else if( schemeString == QStringLiteral( "Dark" ) )
|
||||
else if( schemeString == QStringLiteral( "DarkScheme" ) )
|
||||
{
|
||||
scheme = Qt::ColorScheme::Dark;
|
||||
scheme = QskSkin::DarkScheme;
|
||||
}
|
||||
else
|
||||
{
|
||||
scheme = Qt::ColorScheme::Unknown;
|
||||
scheme = QskSkin::UnknownScheme;
|
||||
}
|
||||
|
||||
const auto pair = qMakePair( name, scheme );
|
||||
|
||||
#if QT_VERSION >= QT_VERSION_CHECK( 6, 5, 0 )
|
||||
const auto systemScheme = qGuiApp->styleHints()->colorScheme();
|
||||
|
||||
if( scheme == systemScheme )
|
||||
if( static_cast< Qt::ColorScheme >( scheme ) == systemScheme )
|
||||
{
|
||||
m_skinNames.prepend( name );
|
||||
m_skins.prepend( pair );
|
||||
}
|
||||
else
|
||||
{
|
||||
m_skinNames.append( name );
|
||||
m_skins.append( pair );
|
||||
}
|
||||
#else
|
||||
Q_UNUSED( TokenScheme )
|
||||
m_skinNames += name;
|
||||
m_skins.append( pair );
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return !m_skinNames.isEmpty();
|
||||
return !m_skins.isEmpty();
|
||||
}
|
||||
|
||||
inline QString factoryId() const
|
||||
|
|
@ -158,9 +160,9 @@ namespace
|
|||
return factory;
|
||||
}
|
||||
|
||||
inline QStringList skinNames() const
|
||||
inline QVector< QskSkin::SkinInfo > skins() const
|
||||
{
|
||||
return m_skinNames;
|
||||
return m_skins;
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -168,7 +170,7 @@ namespace
|
|||
QObject* instance() = delete;
|
||||
|
||||
QString m_factoryId;
|
||||
QStringList m_skinNames;
|
||||
QVector< QskSkin::SkinInfo > m_skins;
|
||||
};
|
||||
|
||||
class FactoryMap
|
||||
|
|
@ -210,17 +212,17 @@ namespace
|
|||
|
||||
void reset()
|
||||
{
|
||||
m_skinNames.clear();
|
||||
m_skins.clear();
|
||||
m_skinMap.clear();
|
||||
m_factoryMap.clear();
|
||||
}
|
||||
|
||||
QskSkinFactory* factory( const QString& skinName )
|
||||
QskSkinFactory* factory( QskSkin::SkinInfo info )
|
||||
{
|
||||
if ( !m_isValid )
|
||||
rebuild();
|
||||
|
||||
const auto it = m_skinMap.constFind( skinName );
|
||||
const auto it = m_skinMap.constFind( info );
|
||||
if ( it != m_skinMap.constEnd() )
|
||||
{
|
||||
auto it2 = m_factoryMap.find( it.value() );
|
||||
|
|
@ -237,12 +239,12 @@ namespace
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
QStringList skinNames() const
|
||||
QVector< QskSkin::SkinInfo > skins() const
|
||||
{
|
||||
if ( !m_isValid )
|
||||
const_cast< FactoryMap* >( this )->rebuild();
|
||||
|
||||
return m_skinNames;
|
||||
return m_skins;
|
||||
}
|
||||
|
||||
void insertFactory( FactoryLoader* loader )
|
||||
|
|
@ -269,7 +271,7 @@ namespace
|
|||
data.factory = factory;
|
||||
|
||||
m_skinMap.clear();
|
||||
m_skinNames.clear();
|
||||
m_skins.clear();
|
||||
m_isValid = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -287,7 +289,7 @@ namespace
|
|||
for ( auto it = m_skinMap.constBegin();
|
||||
it != m_skinMap.constEnd(); ++it )
|
||||
{
|
||||
if ( it.key() == factoryId )
|
||||
if ( it.key().first == factoryId )
|
||||
{
|
||||
m_isValid = false;
|
||||
break;
|
||||
|
|
@ -296,7 +298,7 @@ namespace
|
|||
|
||||
if ( !m_isValid )
|
||||
{
|
||||
m_skinNames.clear();
|
||||
m_skins.clear();
|
||||
m_skinMap.clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -311,7 +313,7 @@ namespace
|
|||
void rebuild()
|
||||
{
|
||||
m_skinMap.clear();
|
||||
m_skinNames.clear();
|
||||
m_skins.clear();
|
||||
|
||||
// first we try all factories, that have been added manually
|
||||
for ( auto it = m_factoryMap.constBegin(); it != m_factoryMap.constEnd(); ++it )
|
||||
|
|
@ -319,7 +321,7 @@ namespace
|
|||
const auto& data = it.value();
|
||||
|
||||
if ( data.loader == nullptr && data.factory )
|
||||
rebuild( it.key(), data.factory->skinNames() );
|
||||
rebuild( it.key(), data.factory->skins() );
|
||||
}
|
||||
|
||||
// all factories from plugins are following
|
||||
|
|
@ -327,27 +329,27 @@ namespace
|
|||
{
|
||||
const auto& data = it.value();
|
||||
if ( data.loader )
|
||||
rebuild( it.key(), data.loader->skinNames() );
|
||||
rebuild( it.key(), data.loader->skins() );
|
||||
}
|
||||
|
||||
m_isValid = true;
|
||||
}
|
||||
|
||||
void rebuild( const QString& factoryId, const QStringList& skinNames )
|
||||
void rebuild( const QString& factoryId, const QVector< QskSkin::SkinInfo >& skins )
|
||||
{
|
||||
for ( const auto& name : skinNames )
|
||||
for ( const auto& skin : skins )
|
||||
{
|
||||
if ( !m_skinMap.contains( name ) )
|
||||
if ( !m_skinMap.contains( skin ) )
|
||||
{
|
||||
m_skinMap.insert( name, factoryId );
|
||||
m_skinNames += name;
|
||||
m_skinMap.insert( skin, factoryId );
|
||||
m_skins += skin;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QMap< QString, Data > m_factoryMap; // factoryId -> data
|
||||
QMap< QString, QString > m_skinMap; // skinName -> factoryId
|
||||
QStringList m_skinNames;
|
||||
QMap< QskSkin::SkinInfo, QString > m_skinMap; // skinName -> factoryId
|
||||
QVector< QskSkin::SkinInfo > m_skins;
|
||||
|
||||
bool m_isValid;
|
||||
};
|
||||
|
|
@ -515,32 +517,34 @@ void QskSkinManager::unregisterFactories()
|
|||
m_data->factoryMap.reset();
|
||||
}
|
||||
|
||||
QStringList QskSkinManager::skinNames() const
|
||||
QVector< QskSkin::SkinInfo > QskSkinManager::skinInfos() const
|
||||
{
|
||||
m_data->ensurePlugins();
|
||||
return m_data->factoryMap.skinNames();
|
||||
return m_data->factoryMap.skins();
|
||||
}
|
||||
|
||||
QskSkin* QskSkinManager::createSkin( const QString& skinName ) const
|
||||
QskSkin* QskSkinManager::createSkin( QskSkin::SkinInfo info ) const
|
||||
{
|
||||
m_data->ensurePlugins();
|
||||
|
||||
auto& map = m_data->factoryMap;
|
||||
|
||||
auto name = skinName;
|
||||
auto i = info;
|
||||
|
||||
auto factory = map.factory( info );
|
||||
|
||||
auto factory = map.factory( name );
|
||||
if ( factory == nullptr )
|
||||
{
|
||||
const auto names = map.skinNames();
|
||||
if ( !names.isEmpty() )
|
||||
const auto infos = map.skins();
|
||||
|
||||
if ( !infos.isEmpty() )
|
||||
{
|
||||
name = names.first();
|
||||
factory = map.factory( name );
|
||||
i = infos.first();
|
||||
factory = map.factory( i );
|
||||
}
|
||||
}
|
||||
|
||||
return factory ? factory->createSkin( name ) : nullptr;
|
||||
return factory ? factory->createSkin( info ) : nullptr;
|
||||
}
|
||||
|
||||
#include "moc_QskSkinManager.cpp"
|
||||
|
|
|
|||
|
|
@ -7,11 +7,11 @@
|
|||
#define QSK_SKIN_MANAGER_H
|
||||
|
||||
#include "QskGlobal.h"
|
||||
#include "QskSkin.h"
|
||||
|
||||
#include <qobject.h>
|
||||
#include <memory>
|
||||
|
||||
class QskSkin;
|
||||
class QskSkinFactory;
|
||||
|
||||
#if defined( qskSkinManager )
|
||||
|
|
@ -37,9 +37,9 @@ class QSK_EXPORT QskSkinManager : public QObject
|
|||
void unregisterFactory( const QString& factoryId );
|
||||
void unregisterFactories();
|
||||
|
||||
QStringList skinNames() const;
|
||||
QVector< QskSkin::SkinInfo > skinInfos() const;
|
||||
|
||||
QskSkin* createSkin( const QString& skinName ) const;
|
||||
QskSkin* createSkin( QskSkin::SkinInfo ) const;
|
||||
|
||||
protected:
|
||||
QskSkinManager();
|
||||
|
|
|
|||
|
|
@ -655,10 +655,10 @@ QskWindow::EventAcceptance QskWindow::eventAcceptance() const
|
|||
return d_func()->eventAcceptance;
|
||||
}
|
||||
|
||||
void QskWindow::setSkin( const QString& skinName )
|
||||
void QskWindow::setSkin( QskSkin::SkinInfo info )
|
||||
{
|
||||
// we should compare the skinName with the previous one
|
||||
auto skin = QskSkinManager::instance()->createSkin( skinName );
|
||||
auto skin = QskSkinManager::instance()->createSkin( info );
|
||||
setSkin( skin );
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,11 +7,12 @@
|
|||
#define QSK_WINDOW_H
|
||||
|
||||
#include "QskGlobal.h"
|
||||
#include "QskSkin.h"
|
||||
|
||||
#include <qquickwindow.h>
|
||||
|
||||
class QskWindowPrivate;
|
||||
class QskObjectAttributes;
|
||||
class QskSkin;
|
||||
|
||||
class QSK_EXPORT QskWindow : public QQuickWindow
|
||||
{
|
||||
|
|
@ -72,7 +73,7 @@ class QSK_EXPORT QskWindow : public QQuickWindow
|
|||
|
||||
// each window might have its own skin
|
||||
void setSkin( QskSkin* );
|
||||
void setSkin( const QString& );
|
||||
void setSkin( QskSkin::SkinInfo info );
|
||||
QskSkin* skin() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ static bool pluginPath = initPluginPath();
|
|||
|
||||
static void initSkins()
|
||||
{
|
||||
auto skinNames = qskSkinManager->skinNames();
|
||||
auto skinNames = qskSkinManager->skinInfos();
|
||||
|
||||
if ( skinNames.isEmpty() )
|
||||
{
|
||||
|
|
@ -70,7 +70,7 @@ static bool pluginPath = initPluginPath();
|
|||
|
||||
qWarning() << "Couldn't find skin plugins, adding some manually.";
|
||||
|
||||
skinNames = qskSkinManager->skinNames();
|
||||
skinNames = qskSkinManager->skinInfos();
|
||||
}
|
||||
|
||||
if ( !skinNames.isEmpty() )
|
||||
|
|
@ -133,10 +133,10 @@ Q_COREAPP_STARTUP_FUNCTION( initFonts )
|
|||
|
||||
void Skinny::changeSkin( QskAnimationHint hint )
|
||||
{
|
||||
const auto names = qskSkinManager->skinNames();
|
||||
const auto names = qskSkinManager->skinInfos();
|
||||
if ( names.size() > 1 )
|
||||
{
|
||||
auto index = names.indexOf( qskSetup->skinName() );
|
||||
auto index = names.indexOf( qskSetup->skinInfo() );
|
||||
index = ( index + 1 ) % names.size();
|
||||
|
||||
setSkin( index, hint );
|
||||
|
|
@ -145,11 +145,11 @@ void Skinny::changeSkin( QskAnimationHint hint )
|
|||
|
||||
void Skinny::setSkin( int index, QskAnimationHint hint )
|
||||
{
|
||||
const auto names = qskSkinManager->skinNames();
|
||||
const auto names = qskSkinManager->skinInfos();
|
||||
if ( names.size() <= 1 )
|
||||
return;
|
||||
|
||||
if ( index == names.indexOf( qskSetup->skinName() ) )
|
||||
if ( index == names.indexOf( qskSetup->skinInfo() ) )
|
||||
return;
|
||||
|
||||
auto oldSkin = qskSetup->skin();
|
||||
|
|
|
|||
Loading…
Reference in New Issue