Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 58 additions & 57 deletions include/qml_material/core/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,65 @@

namespace qml_material
{
struct MdScheme {
QRgb primary;
QRgb on_primary;
QRgb primary_container;
QRgb on_primary_container;
QRgb primary_fixed;
QRgb primary_fixed_dim;
QRgb on_primary_fixed;
QRgb on_primary_fixed_variant;
QRgb secondary;
QRgb on_secondary;
QRgb secondary_container;
QRgb on_secondary_container;
QRgb secondary_fixed;
QRgb secondary_fixed_dim;
QRgb on_secondary_fixed;
QRgb on_secondary_fixed_variant;
QRgb tertiary;
QRgb on_tertiary;
QRgb tertiary_container;
QRgb on_tertiary_container;
QRgb tertiary_fixed;
QRgb tertiary_fixed_dim;
QRgb on_tertiary_fixed;
QRgb on_tertiary_fixed_variant;
QRgb error;
QRgb on_error;
QRgb error_container;
QRgb on_error_container;
QRgb background;
QRgb on_background;
QRgb surface;
QRgb on_surface;
QRgb surface_variant;
QRgb on_surface_variant;
QRgb outline;
QRgb outline_variant;
QRgb shadow;
QRgb scrim;
QRgb inverse_surface;
QRgb inverse_on_surface;
QRgb inverse_primary;

// surface
QRgb surface_1;
QRgb surface_2;
QRgb surface_3;
QRgb surface_4;
QRgb surface_5;
#define QM_SCHEME_ROLES(X) \
X(primary) \
X(on_primary) \
X(primary_container) \
X(on_primary_container) \
X(primary_fixed) \
X(primary_fixed_dim) \
X(on_primary_fixed) \
X(on_primary_fixed_variant) \
X(secondary) \
X(on_secondary) \
X(secondary_container) \
X(on_secondary_container) \
X(secondary_fixed) \
X(secondary_fixed_dim) \
X(on_secondary_fixed) \
X(on_secondary_fixed_variant) \
X(tertiary) \
X(on_tertiary) \
X(tertiary_container) \
X(on_tertiary_container) \
X(tertiary_fixed) \
X(tertiary_fixed_dim) \
X(on_tertiary_fixed) \
X(on_tertiary_fixed_variant) \
X(error) \
X(on_error) \
X(error_container) \
X(on_error_container) \
X(background) \
X(on_background) \
X(surface) \
X(on_surface) \
X(surface_variant) \
X(on_surface_variant) \
X(outline) \
X(outline_variant) \
X(shadow) \
X(scrim) \
X(inverse_surface) \
X(inverse_on_surface) \
X(inverse_primary) \
X(surface_1) \
X(surface_2) \
X(surface_3) \
X(surface_4) \
X(surface_5) \
X(surface_dim) \
X(surface_bright) \
X(surface_container) \
X(surface_container_low) \
X(surface_container_lowest) \
X(surface_container_high) \
X(surface_container_highest)

// surface v2
QRgb surface_dim;
QRgb surface_bright;
QRgb surface_container;
QRgb surface_container_low;
QRgb surface_container_lowest;
QRgb surface_container_high;
QRgb surface_container_highest;
struct MdScheme {
#define X(_n_) QRgb _n_;
QM_SCHEME_ROLES(X)
#undef X

material_color_utilities::TonalPalette neutral_palette { 0 };
};
Expand Down
14 changes: 14 additions & 0 deletions include/qml_material/token/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class QML_MATERIAL_API MdColorMgr : public QObject {
Q_PROPERTY(bool useSysAccentColor READ useSysAccentColor WRITE setUseSysAccentColor NOTIFY
useSysAccentColorChanged FINAL)

/**
* @property overrides
* @brief Per-role colors applied on top of the generated scheme
*
* Maps a role name (e.g. "surface_container") to a color or a "#rrggbb" string.
* Unknown roles are ignored.
*/
Q_PROPERTY(
QVariantMap overrides READ overrides WRITE setOverrides NOTIFY overridesChanged FINAL)

#define X(_n_) \
Q_PROPERTY(QColor _n_ READ _n_ NOTIFY schemeChanged) \
QColor _n_() const { return m_scheme.value()._n_; }
Expand Down Expand Up @@ -204,6 +214,8 @@ class QML_MATERIAL_API MdColorMgr : public QObject {
bool useSysAccentColor() const;
auto paletteType() const -> Enum::PaletteType;
void setPaletteType(Enum::PaletteType);
auto overrides() const -> const QVariantMap&;
void setOverrides(const QVariantMap&);

Q_INVOKABLE QColor getOn(QColor) const;

Expand All @@ -222,6 +234,7 @@ class QML_MATERIAL_API MdColorMgr : public QObject {
Q_SIGNAL void accentColorChanged(QColor);
Q_SIGNAL void useSysColorSMChanged();
Q_SIGNAL void useSysAccentColorChanged();
Q_SIGNAL void overridesChanged();

private:
void genSchemeImpl(Enum::ThemeMode);
Expand All @@ -231,6 +244,7 @@ class QML_MATERIAL_API MdColorMgr : public QObject {
Enum::ThemeMode m_last_mode;
Enum::PaletteType m_scheme_type;
QProperty<MdScheme> m_scheme;
QVariantMap m_overrides;
bool m_use_sys_color_scheme;
bool m_use_sys_accent_color;
};
Expand Down
27 changes: 27 additions & 0 deletions src/token/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ using namespace qml_material;
namespace
{
constexpr QRgb BASE_COLOR { qRgb(190, 231, 253) };

auto scheme_role(MdScheme& s, QStringView name) -> QRgb* {
#define X(_n_) \
if (name == QLatin1StringView(#_n_)) return &s._n_;
QM_SCHEME_ROLES(X)
#undef X
return nullptr;
}
} // namespace

MdColorMgr::MdColorMgr(QObject* parent)
Expand Down Expand Up @@ -54,6 +62,20 @@ void MdColorMgr::setPaletteType(Enum::PaletteType t) {
}
}

auto MdColorMgr::overrides() const -> const QVariantMap& { return m_overrides; }
void MdColorMgr::setOverrides(const QVariantMap& v) {
if (m_overrides == v) return;
MdScheme probe;
for (auto it = v.cbegin(); it != v.cend(); ++it) {
if (! scheme_role(probe, it.key())) {
qWarning() << "MdColorMgr: unknown color role in overrides:" << it.key();
}
}
m_overrides = v;
Q_EMIT overridesChanged();
genSchemeImpl(m_last_mode);
}

void MdColorMgr::setAccentColor(QColor v) {
if (std::exchange(m_accent_color, v) != v) {
Q_EMIT accentColorChanged(m_accent_color);
Expand Down Expand Up @@ -99,6 +121,11 @@ void MdColorMgr::genSchemeImpl(Enum::ThemeMode mode) {
auto scheme = mode == Enum::ThemeMode::Light
? material_light_color_scheme(m_accent_color.rgb(), paletteType())
: material_dark_color_scheme(m_accent_color.rgb(), paletteType());
for (auto it = m_overrides.cbegin(); it != m_overrides.cend(); ++it) {
auto role = scheme_role(scheme, it.key());
auto color = it.value().value<QColor>();
if (role && color.isValid()) *role = color.rgba();
}
m_last_mode = mode;
const QPointer<MdColorMgr> guard(this);
m_scheme = std::move(scheme);
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ set(QM_TEST_SOURCES
shape/material_shapes.cpp
style/control_feedback.cpp
style/state_lifetime.cpp
token/color_overrides.cpp
view/lazy_grid.cpp
view/lazy_masonry.cpp
view/lazy_list.cpp
Expand Down
103 changes: 103 additions & 0 deletions tests/token/color_overrides.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <QColor>
#include <QMetaProperty>
#include <QQmlComponent>
#include <QQmlEngine>
#include <QSignalSpy>
#include <QtTest>
#include <memory>

#include "qml_material/token/color.hpp"

using qml_material::MdColorMgr;

namespace
{
auto roleColors(const QObject& obj) -> QVariantMap {
QVariantMap out;
auto* meta = obj.metaObject();
for (int i = meta->propertyOffset(); i < meta->propertyCount(); ++i) {
auto prop = meta->property(i);
if (prop.metaType() == QMetaType::fromType<QColor>())
out.insert(prop.name(), prop.read(&obj));
}
return out;
}
} // namespace

class ColorOverridesTest : public QObject {
Q_OBJECT

private Q_SLOTS:
void emptyKeepsScheme() {
MdColorMgr mgr;
mgr.setUseSysColorSM(false);
mgr.setMode(qml_material::Enum::ThemeMode::Dark);
const auto before = roleColors(mgr);
mgr.setOverrides({ { "surface_container", "#1b2838" } });
mgr.setOverrides({});
QCOMPARE(roleColors(mgr), before);
}

void overrideOneRole() {
MdColorMgr mgr;
mgr.setUseSysColorSM(false);
mgr.setMode(qml_material::Enum::ThemeMode::Dark);
const auto before = roleColors(mgr);

QSignalSpy scheme(&mgr, &MdColorMgr::schemeChanged);
mgr.setOverrides({ { "surface_container", "#1b2838" },
{ "on_surface", QColor(0xc7, 0xd5, 0xe0) },
{ "no_such_role", "#ffffff" } });
QCOMPARE(scheme.count(), 1);

auto after = roleColors(mgr);
QCOMPARE(after.take("surface_container").value<QColor>(), QColor("#1b2838"));
QCOMPARE(after.take("on_surface").value<QColor>(), QColor(0xc7, 0xd5, 0xe0));
auto expected = before;
expected.remove("surface_container");
expected.remove("on_surface");
QCOMPARE(after, expected);

// overrides survive a scheme regeneration
mgr.setMode(qml_material::Enum::ThemeMode::Light);
QCOMPARE(mgr.surface_container(), QColor("#1b2838"));
}

void qmlBinding() {
QQmlEngine engine;
QQmlComponent component(&engine);
component.setData(R"(
import QtQuick
import Qcm.Material as MD

QtObject {
property color c: MD.Token.color.surface_container
property color other: MD.Token.color.surface
function set(v) { MD.Token.color.overrides = v; }
}
)",
QUrl(QStringLiteral("qrc:/tests/ColorOverrides.qml")));
QVERIFY2(! component.isError(), qPrintable(component.errorString()));
std::unique_ptr<QObject> obj(component.create());
QVERIFY(obj);

const auto c0 = obj->property("c").value<QColor>();
const auto other0 = obj->property("other").value<QColor>();
const QVariant steam = QVariantMap { { "surface_container", "#1b2838" } };
QVERIFY(QMetaObject::invokeMethod(obj.get(), "set", Q_ARG(QVariant, steam)));
QCOMPARE(obj->property("c").value<QColor>(), QColor("#1b2838"));
QCOMPARE(obj->property("other").value<QColor>(), other0);

const QVariant none = QVariantMap {};
QVERIFY(QMetaObject::invokeMethod(obj.get(), "set", Q_ARG(QVariant, none)));
QCOMPARE(obj->property("c").value<QColor>(), c0);
}
};

int run_color_overrides(int argc, char** argv) {
ColorOverridesTest test;
QTEST_SET_MAIN_SOURCE_PATH
return QTest::qExec(&test, argc, argv);
}

#include "color_overrides.moc"
Loading