Skip to content

Commit 5f9239e

Browse files
committed
ThemeEngine: Add accentColor property
1 parent fe1bf79 commit 5f9239e

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/ui/internal/themeengine.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using namespace scratchcpp;
77

88
static const QString MODULE = "ui";
99
static const QString THEME_KEY = "theme";
10+
static const QString ACCENT_COLOR_KEY = "accentColor";
1011

1112
std::shared_ptr<ThemeEngine> ThemeEngine::m_instance = std::make_shared<ThemeEngine>();
1213

@@ -31,6 +32,7 @@ void ThemeEngine::reloadTheme()
3132
emit bgColorChanged();
3233
emit foregroundColorChanged();
3334
emit borderColorChanged();
35+
emit accentColorChanged();
3436
emit themeChanged();
3537
}
3638

@@ -59,3 +61,14 @@ const QColor &ThemeEngine::borderColor() const
5961
static const QColor light = QColor(0, 0, 0, 64);
6062
return theme() == Theme::DarkTheme ? dark : light;
6163
}
64+
65+
QColor ThemeEngine::accentColor() const
66+
{
67+
return settings()->getValue(MODULE, ACCENT_COLOR_KEY).value<QColor>();
68+
}
69+
70+
void ThemeEngine::setAccentColor(const QColor &newAccentColor)
71+
{
72+
settings()->setValue(MODULE, ACCENT_COLOR_KEY, newAccentColor);
73+
emit accentColorChanged();
74+
}

src/ui/internal/themeengine.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ThemeEngine : public IThemeEngine
2121
Q_PROPERTY(QColor bgColor READ bgColor NOTIFY bgColorChanged FINAL)
2222
Q_PROPERTY(QColor foregroundColor READ foregroundColor NOTIFY foregroundColorChanged FINAL)
2323
Q_PROPERTY(QColor borderColor READ borderColor NOTIFY borderColorChanged FINAL)
24+
Q_PROPERTY(QColor accentColor READ accentColor WRITE setAccentColor NOTIFY accentColorChanged FINAL)
2425
public:
2526
static std::shared_ptr<ThemeEngine> instance();
2627

@@ -33,11 +34,15 @@ class ThemeEngine : public IThemeEngine
3334
const QColor &foregroundColor() const override;
3435
const QColor &borderColor() const override;
3536

37+
QColor accentColor() const override;
38+
void setAccentColor(const QColor &newAccentColor) override;
39+
3640
signals:
3741
void themeChanged();
3842
void bgColorChanged();
3943
void foregroundColorChanged();
4044
void borderColorChanged();
45+
void accentColorChanged();
4146

4247
private:
4348
static std::shared_ptr<ThemeEngine> m_instance;

src/ui/ithemeengine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class IThemeEngine
3232
virtual const QColor &bgColor() const = 0;
3333
virtual const QColor &foregroundColor() const = 0;
3434
virtual const QColor &borderColor() const = 0;
35+
36+
virtual QColor accentColor() const = 0;
37+
virtual void setAccentColor(const QColor &newAccentColor) = 0;
3538
};
3639

3740
} // namespace scratchcpp

src/ui/test/themeengine.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using ::testing::_;
1313

1414
static const QString MODULE = "ui";
1515
static const QString THEME_KEY = "theme";
16+
static const QString ACCENT_COLOR_KEY = "accentColor";
1617

1718
class ThemeEngineTest : public testing::Test
1819
{
@@ -26,6 +27,7 @@ class ThemeEngineTest : public testing::Test
2627
m_themeSpies.push_back(std::make_unique<QSignalSpy>(&m_themeEngine, &ThemeEngine::bgColorChanged));
2728
m_themeSpies.push_back(std::make_unique<QSignalSpy>(&m_themeEngine, &ThemeEngine::foregroundColorChanged));
2829
m_themeSpies.push_back(std::make_unique<QSignalSpy>(&m_themeEngine, &ThemeEngine::borderColorChanged));
30+
m_themeSpies.push_back(std::make_unique<QSignalSpy>(&m_themeEngine, &ThemeEngine::accentColorChanged));
2931
}
3032

3133
void TearDown() override { m_themeEngine.setsettings(nullptr); }
@@ -70,3 +72,24 @@ TEST_F(ThemeEngineTest, Theme)
7072
m_themeEngine.reloadTheme();
7173
checkThemeSpies(4);
7274
}
75+
76+
TEST_F(ThemeEngineTest, AccentColor)
77+
{
78+
QSignalSpy spy(&m_themeEngine, &ThemeEngine::accentColorChanged);
79+
80+
EXPECT_CALL(*m_settings, getValue(MODULE, ACCENT_COLOR_KEY)).WillOnce(Return(QColor(255, 0, 0)));
81+
ASSERT_EQ(m_themeEngine.accentColor(), QColor(255, 0, 0));
82+
ASSERT_EQ(spy.count(), 0);
83+
84+
EXPECT_CALL(*m_settings, getValue(MODULE, ACCENT_COLOR_KEY)).WillOnce(Return(QColor(0, 255, 128)));
85+
ASSERT_EQ(m_themeEngine.accentColor(), QColor(0, 255, 128));
86+
ASSERT_EQ(spy.count(), 0);
87+
88+
EXPECT_CALL(*m_settings, setValue(MODULE, ACCENT_COLOR_KEY, QVariant(QColor(255, 255, 255))));
89+
m_themeEngine.setAccentColor(QColor(255, 255, 255));
90+
ASSERT_EQ(spy.count(), 1);
91+
92+
EXPECT_CALL(*m_settings, setValue(MODULE, ACCENT_COLOR_KEY, QVariant(QColor(0, 0, 0))));
93+
m_themeEngine.setAccentColor(QColor(0, 0, 0));
94+
ASSERT_EQ(spy.count(), 2);
95+
}

0 commit comments

Comments
 (0)