From ca7b3a623d9604de9b5e3e355d3d321cc02082e8 Mon Sep 17 00:00:00 2001 From: karliss Date: Mon, 4 May 2020 12:49:40 +0300 Subject: [PATCH] Detect when native theme's darkness doesn't match color theme. (#2176) --- src/common/Configuration.cpp | 25 +++++++++++++++++++++++++ src/common/Configuration.h | 5 +++++ src/core/MainWindow.cpp | 2 +- src/dialogs/WelcomeDialog.cpp | 6 +----- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/common/Configuration.cpp b/src/common/Configuration.cpp index 66138903..136ecf76 100644 --- a/src/common/Configuration.cpp +++ b/src/common/Configuration.cpp @@ -33,6 +33,9 @@ const QHash Configuration::relevantThemes = { { "tango", LightFlag }, { "white", LightFlag } }; +static const QString DEFAULT_LIGHT_COLOR_THEME = "cutter"; +static const QString DEFAULT_DARK_COLOR_THEME = "ayu"; + const QHash> Configuration::cutterOptionColors = { { "gui.cflow", { { DarkFlag, QColor(0xff, 0xff, 0xff) }, @@ -440,6 +443,8 @@ void Configuration::setInterfaceTheme(int theme) setColor(it.key(), it.value()[interfaceTheme.flag]); } + adjustColorThemeDarkness(); + emit interfaceThemeChanged(); emit colorsUpdated(); #ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING @@ -543,6 +548,26 @@ void Configuration::setColorTheme(const QString &theme) emit colorsUpdated(); } +void Configuration::adjustColorThemeDarkness() +{ + bool windowIsDark = windowColorIsDark(); + int windowDarkness = windowIsDark ? DarkFlag : LightFlag; + int currentColorThemeDarkness = colorThemeDarkness(getColorTheme()); + + if ((currentColorThemeDarkness & windowDarkness) == 0) { + setColorTheme(windowIsDark ? DEFAULT_DARK_COLOR_THEME : DEFAULT_LIGHT_COLOR_THEME); + } +} + +int Configuration::colorThemeDarkness(const QString &colorTheme) const +{ + auto flags = relevantThemes.find(colorTheme); + if (flags != relevantThemes.end()) { + return static_cast(*flags); + } + return DarkFlag | LightFlag; +} + void Configuration::resetToDefaultAsmOptions() { for (auto it = asmOptions.cbegin(); it != asmOptions.cend(); it++) { diff --git a/src/common/Configuration.h b/src/common/Configuration.h index 1bfae6c5..36407626 100644 --- a/src/common/Configuration.h +++ b/src/common/Configuration.h @@ -141,6 +141,11 @@ public: QString getColorTheme() const { return s.value("theme", "cutter").toString(); } void setColorTheme(const QString &theme); + /** + * @brief Change current color theme if it doesnt't much native theme's darkness. + */ + void adjustColorThemeDarkness(); + int colorThemeDarkness(const QString &colorTheme) const; void setColor(const QString &name, const QColor &color); const QColor getColor(const QString &name) const; diff --git a/src/core/MainWindow.cpp b/src/core/MainWindow.cpp index 9c015145..b08ee72c 100644 --- a/src/core/MainWindow.cpp +++ b/src/core/MainWindow.cpp @@ -550,7 +550,7 @@ void MainWindow::finalizeOpen() // Add fortune message core->message("\n" + core->cmdRaw("fo")); showMaximized(); - + Config()->adjustColorThemeDarkness(); QSettings s; QStringList unsync = s.value("unsync").toStringList(); diff --git a/src/dialogs/WelcomeDialog.cpp b/src/dialogs/WelcomeDialog.cpp index 4d25ab56..ed92cac2 100644 --- a/src/dialogs/WelcomeDialog.cpp +++ b/src/dialogs/WelcomeDialog.cpp @@ -37,6 +37,7 @@ WelcomeDialog::WelcomeDialog(QWidget *parent) : this, &WelcomeDialog::onLanguageComboBox_currentIndexChanged); + Config()->adjustColorThemeDarkness(); } /** @@ -55,11 +56,6 @@ void WelcomeDialog::on_themeComboBox_currentIndexChanged(int index) { Config()->setInterfaceTheme(index); - // use "ayu" as the default color theme for dark interface - if (Config()->windowColorIsDark()) { - Config()->setColorTheme("ayu"); - } - // make sure that Cutter's logo changes its color according to the selected theme ui->logoSvgWidget->load(Config()->getLogoFile()); }