Skip to content

Commit

Permalink
chore: add the ActiveColorDark setting
Browse files Browse the repository at this point in the history
get the active color according to system theme(light or dark).
There are two different active colors between light and dark theme.

Log:
  • Loading branch information
wangfei authored and FeiWang1119 committed Nov 13, 2024
1 parent f0a7d5d commit b975592
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
4 changes: 4 additions & 0 deletions include/kernel/dplatformtheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class DPlatformTheme : public DNativeSettings
Q_PROPERTY(QByteArray gtkFontName READ gtkFontName WRITE setGtkFontName NOTIFY gtkFontNameChanged)

Q_PROPERTY(QColor activeColor READ activeColor WRITE setActiveColor NOTIFY activeColorChanged)
Q_PROPERTY(QColor darkActiveColor READ darkActiveColor WRITE setDarkActiveColor NOTIFY darkActiveColorChanged)
#if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
// QPalette
Q_PROPERTY(QColor window READ window WRITE setWindow NOTIFY windowChanged)
Expand Down Expand Up @@ -101,6 +102,7 @@ class DPlatformTheme : public DNativeSettings
QByteArray gtkFontName() const;

QColor activeColor() const;
QColor darkActiveColor() const;

bool isValidPalette() const;

Expand Down Expand Up @@ -152,6 +154,7 @@ public Q_SLOTS:
void setFontPointSize(qreal fontPointSize);
void setGtkFontName(const QByteArray &fontName);
void setActiveColor(const QColor activeColor);
void setDarkActiveColor(const QColor &activeColor);
#if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
void setWindow(const QColor &window);
void setWindowText(const QColor &windowText);
Expand Down Expand Up @@ -201,6 +204,7 @@ public Q_SLOTS:
void fontPointSizeChanged(qreal fontPointSize);
void gtkFontNameChanged(QByteArray fontName);
void activeColorChanged(QColor activeColor);
void darkActiveColorChanged(QColor activeColor);
void paletteChanged(DPalette palette);
#if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
void windowChanged(QColor window);
Expand Down
32 changes: 28 additions & 4 deletions src/kernel/dguiapplicationhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QDirIterator>
#include <QDesktopServices>
#include <QLibraryInfo>
#include <QTimer>

#ifdef Q_OS_UNIX
#include <QDBusError>
Expand Down Expand Up @@ -345,10 +346,19 @@ void DGuiApplicationHelperPrivate::_q_initApplicationTheme(bool notifyChange)
// 监听与程序主题相关的改变
QObject::connect(appTheme, &DPlatformTheme::themeNameChanged, app, onAppThemeChanged);
QObject::connect(appTheme, &DPlatformTheme::paletteChanged, app, onAppThemeChanged);
QObject::connect(appTheme, &DPlatformTheme::activeColorChanged, app, [this] {
QTimer *timer = new QTimer(app);
timer->setInterval(100);
timer->setSingleShot(true);
QObject::connect(timer, &QTimer::timeout, timer, [this] {
if (!appPalette)
notifyAppThemeChanged();
});
QObject::connect(appTheme, &DPlatformTheme::activeColorChanged, app, [timer]{
timer->start();
});
QObject::connect(appTheme, &DPlatformTheme::darkActiveColorChanged, app, [timer] {
timer->start();
});

// appTheme在此之前可能由systemTheme所代替被使用,此时在创建appTheme
// 并初始化之后,应当发送信号通知程序主题的改变
Expand Down Expand Up @@ -750,7 +760,7 @@ static QColor dark_qpalette[QPalette::NColorRoles] {
QColor("#282828"), //Base
QColor("#252525"), //Window
QColor(0, 0, 0, 0.05 * 255), //Shadow
QColor("#0081ff"), //Highlight
QColor("#024CCA"), //Highlight
QColor("#F1F6FF"), //HighlightedText
QColor("#0082fa"), //Link
QColor("#ad4579"), //LinkVisited
Expand Down Expand Up @@ -1024,6 +1034,20 @@ void DGuiApplicationHelper::generatePalette(DPalette &base, ColorType type)
}
}

static inline QColor getActiveColor(const DPlatformTheme *theme, DGuiApplicationHelper::ColorType type)
{
QColor activeColor;
if (type == DGuiApplicationHelper::DarkType) {
activeColor = theme->darkActiveColor();
if (!activeColor.isValid()) {
activeColor = theme->activeColor();
}
} else {
activeColor = theme->activeColor();
}

return activeColor;
}
/*!
\brief 获取调色板数据.
Expand All @@ -1049,7 +1073,7 @@ DPalette DGuiApplicationHelper::fetchPalette(const DPlatformTheme *theme)

bool ok = false;
base_palette = theme->fetchPalette(standardPalette(type), &ok);
const QColor &active_color = theme->activeColor();
const QColor &active_color = getActiveColor(theme, type);

if (active_color.isValid()) {
base_palette.setColor(QPalette::Normal, QPalette::Highlight, active_color);
Expand Down Expand Up @@ -1220,7 +1244,7 @@ DPalette DGuiApplicationHelper::applicationPalette(ColorType paletteType) const
// 覆盖DPalette中的的QPalette数据
pa.QPalette::operator =(qGuiApp->palette());
} else {
const QColor &active_color = theme->activeColor();
const QColor &active_color = getActiveColor(theme, type);

if (active_color.isValid()) {
// 应用Active Color
Expand Down
14 changes: 14 additions & 0 deletions src/kernel/dplatformtheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ QColor DPlatformTheme::activeColor() const
return qvariant_cast<QColor>(value);
}

QColor DPlatformTheme::darkActiveColor() const
{
FETCH_PROPERTY("Qt/DarkActiveColor", darkActiveColor)

return qvariant_cast<QColor>(value);
}

bool DPlatformTheme::isValidPalette() const
{
return !allKeys().isEmpty();
Expand Down Expand Up @@ -744,6 +751,13 @@ void DPlatformTheme::setActiveColor(const QColor activeColor)
d->theme->setSetting("Qt/ActiveColor", activeColor);
}

void DPlatformTheme::setDarkActiveColor(const QColor &activeColor)
{
D_D(DPlatformTheme);

d->theme->setSetting("Qt/DarkActiveColor", activeColor);
}

#define SET_COLOR(Role) setSetting(QByteArrayLiteral(#Role), Role)
#if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
void DPlatformTheme::setWindow(const QColor &window)
Expand Down

0 comments on commit b975592

Please sign in to comment.