mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-13 17:44:48 -07:00
Utility method to check if a theme is supposed to be in dark or light mode. (#6785)
* Utility method to check if a theme is supposed to be in dark or light mode. Took 22 minutes Took 4 seconds * Method is public. Took 3 minutes * Add a utility method to check if we're using a built-in theme Took 3 minutes Took 3 seconds * Use built-in theme detection for home screen. Took 6 minutes * Re-polish on theme change Took 2 minutes * Fetch background on theme change. Took 4 minutes Took 6 seconds * No need to double polish. Took 4 minutes * No need to repaint. Took 32 seconds * Only repolish visible widgets. Took 5 minutes --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
43bee2316e
commit
a4c2b1411f
3 changed files with 77 additions and 11 deletions
|
|
@ -15,6 +15,7 @@
|
||||||
#include <QStyle>
|
#include <QStyle>
|
||||||
#include <QStyleFactory>
|
#include <QStyleFactory>
|
||||||
#include <QStyleHints>
|
#include <QStyleHints>
|
||||||
|
#include <QWidget>
|
||||||
#include <Qt>
|
#include <Qt>
|
||||||
|
|
||||||
#define NONE_THEME_NAME "Default"
|
#define NONE_THEME_NAME "Default"
|
||||||
|
|
@ -112,6 +113,39 @@ void ThemeManager::ensureThemeDirectoryExists()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ThemeManager::isDarkMode()
|
||||||
|
{
|
||||||
|
auto themeName = SettingsCache::instance().getThemeName();
|
||||||
|
// Explicit Dark Mode
|
||||||
|
if (themeName == FUSION_THEME_NAME_LIGHT || themeName.endsWith("(Light)")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Explicit Light Mode
|
||||||
|
if (themeName == FUSION_THEME_NAME_DARK || themeName.endsWith("(Dark)")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto detection on compatible Qt versions
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
|
||||||
|
if (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark &&
|
||||||
|
(themeName == NONE_THEME_NAME || themeName == FUSION_THEME_NAME || themeName.endsWith("(System Default)"))) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
// Default to light mode
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ThemeManager::isBuiltInTheme()
|
||||||
|
{
|
||||||
|
const auto themeName = SettingsCache::instance().getThemeName();
|
||||||
|
|
||||||
|
return themeName == NONE_THEME_NAME || themeName == FUSION_THEME_NAME || themeName == FUSION_THEME_NAME_LIGHT ||
|
||||||
|
themeName == FUSION_THEME_NAME_DARK;
|
||||||
|
}
|
||||||
|
|
||||||
QStringMap &ThemeManager::getAvailableThemes()
|
QStringMap &ThemeManager::getAvailableThemes()
|
||||||
{
|
{
|
||||||
QDir dir;
|
QDir dir;
|
||||||
|
|
@ -335,26 +369,53 @@ void ThemeManager::themeChangedSlot()
|
||||||
qApp->setStyleSheet("");
|
qApp->setStyleSheet("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QStyle *newStyle = nullptr;
|
||||||
|
QPalette newPalette;
|
||||||
|
|
||||||
if (themeName == FUSION_THEME_NAME) {
|
if (themeName == FUSION_THEME_NAME) {
|
||||||
QStyle *fusionStyle = QStyleFactory::create("Fusion");
|
newStyle = QStyleFactory::create("Fusion");
|
||||||
qApp->setStyle(fusionStyle);
|
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
|
||||||
// Start from Fusion's own palette so dark mode is handled correctly,
|
// Start from Fusion's own palette so dark mode is handled correctly,
|
||||||
// then apply any tweaks on top of it.
|
// then apply any tweaks on top of it.
|
||||||
QPalette palette = fusionStyle->standardPalette();
|
newPalette = newStyle->standardPalette();
|
||||||
if (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark) {
|
if (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark) {
|
||||||
palette.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
|
newPalette.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
|
||||||
}
|
}
|
||||||
qApp->setPalette(palette);
|
#else
|
||||||
|
newPalette = qApp->palette();
|
||||||
#endif
|
#endif
|
||||||
} else if (themeName == FUSION_THEME_NAME_LIGHT) {
|
} else if (themeName == FUSION_THEME_NAME_LIGHT) {
|
||||||
qApp->setStyle(QStyleFactory::create("Fusion"));
|
newStyle = QStyleFactory::create("Fusion");
|
||||||
qApp->setPalette(createLightGreenFusionPalette());
|
newPalette = createLightGreenFusionPalette();
|
||||||
} else if (themeName == FUSION_THEME_NAME_DARK) {
|
} else if (themeName == FUSION_THEME_NAME_DARK) {
|
||||||
qApp->setStyle(QStyleFactory::create("Fusion"));
|
newStyle = QStyleFactory::create("Fusion");
|
||||||
qApp->setPalette(createDarkGreenFusionPalette());
|
newPalette = createDarkGreenFusionPalette();
|
||||||
} else {
|
} else {
|
||||||
qApp->setStyle(QStyleFactory::create(defaultStyleName)); // setting the style also sets the palette
|
newStyle = QStyleFactory::create(defaultStyleName);
|
||||||
|
// Use the style's default palette.
|
||||||
|
newPalette = newStyle->standardPalette();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply palette FIRST.
|
||||||
|
qApp->setPalette(newPalette);
|
||||||
|
// Then apply style.
|
||||||
|
qApp->setStyle(newStyle);
|
||||||
|
|
||||||
|
// Force every widget to re-polish and repaint immediately rather than
|
||||||
|
// waiting for natural expose events, which produces a patchwork of old
|
||||||
|
// and new colours during a live preview.
|
||||||
|
// Note: we do NOT call widget->setPalette(base) here — qApp->setPalette()
|
||||||
|
// already propagates to all widgets that haven't explicitly overridden their
|
||||||
|
// palette (WA_SetPalette not set). Calling it unconditionally would clobber
|
||||||
|
// intentional per-widget palette customisations across the whole app.
|
||||||
|
for (QWidget *widget : qApp->allWidgets()) {
|
||||||
|
if (widget->isVisible()) {
|
||||||
|
newStyle->unpolish(widget);
|
||||||
|
newStyle->polish(widget);
|
||||||
|
|
||||||
|
widget->update();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dirPath.isEmpty()) {
|
if (dirPath.isEmpty()) {
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,8 @@ protected:
|
||||||
QBrush loadExtraBrush(QString fileName, QBrush &fallbackBrush);
|
QBrush loadExtraBrush(QString fileName, QBrush &fallbackBrush);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
bool isBuiltInTheme();
|
||||||
|
bool isDarkMode();
|
||||||
QStringMap &getAvailableThemes();
|
QStringMap &getAvailableThemes();
|
||||||
|
|
||||||
QBrush &getBgBrush(Role zone);
|
QBrush &getBgBrush(Role zone);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "../../../client/settings/cache_settings.h"
|
#include "../../../client/settings/cache_settings.h"
|
||||||
#include "../../../interface/widgets/tabs/tab_supervisor.h"
|
#include "../../../interface/widgets/tabs/tab_supervisor.h"
|
||||||
|
#include "../../theme_manager.h"
|
||||||
#include "../../window_main.h"
|
#include "../../window_main.h"
|
||||||
#include "background_sources.h"
|
#include "background_sources.h"
|
||||||
#include "home_styled_button.h"
|
#include "home_styled_button.h"
|
||||||
|
|
@ -46,6 +47,8 @@ HomeWidget::HomeWidget(QWidget *parent, TabSupervisor *_tabSupervisor)
|
||||||
&HomeWidget::onBackgroundShuffleFrequencyChanged);
|
&HomeWidget::onBackgroundShuffleFrequencyChanged);
|
||||||
// Lambda is cleaner to read than overloading this
|
// Lambda is cleaner to read than overloading this
|
||||||
connect(&SettingsCache::instance(), &SettingsCache::homeTabDisplayCardNameChanged, this, [this] { repaint(); });
|
connect(&SettingsCache::instance(), &SettingsCache::homeTabDisplayCardNameChanged, this, [this] { repaint(); });
|
||||||
|
connect(&SettingsCache::instance(), &SettingsCache::themeChanged, this,
|
||||||
|
&HomeWidget::initializeBackgroundFromSource);
|
||||||
connect(&SettingsCache::instance(), &SettingsCache::themeChanged, this,
|
connect(&SettingsCache::instance(), &SettingsCache::themeChanged, this,
|
||||||
&HomeWidget::updateButtonsToBackgroundColor);
|
&HomeWidget::updateButtonsToBackgroundColor);
|
||||||
}
|
}
|
||||||
|
|
@ -256,7 +259,7 @@ void HomeWidget::updateConnectButton(const ClientStatus status)
|
||||||
|
|
||||||
QPair<QColor, QColor> HomeWidget::extractDominantColors(const QPixmap &pixmap)
|
QPair<QColor, QColor> HomeWidget::extractDominantColors(const QPixmap &pixmap)
|
||||||
{
|
{
|
||||||
if (SettingsCache::instance().getThemeName() == "Default" &&
|
if (themeManager->isBuiltInTheme() &&
|
||||||
SettingsCache::instance().getHomeTabBackgroundSource() == BackgroundSources::toId(BackgroundSources::Theme)) {
|
SettingsCache::instance().getHomeTabBackgroundSource() == BackgroundSources::toId(BackgroundSources::Theme)) {
|
||||||
return QPair<QColor, QColor>(QColor::fromRgb(20, 140, 60), QColor::fromRgb(120, 200, 80));
|
return QPair<QColor, QColor>(QColor::fromRgb(20, 140, 60), QColor::fromRgb(120, 200, 80));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue