Detect when native theme's darkness doesn't match color theme. (#2176)

This commit is contained in:
karliss 2020-05-04 12:49:40 +03:00 committed by GitHub
parent f65f5cab4b
commit ca7b3a623d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 6 deletions

View File

@ -33,6 +33,9 @@ const QHash<QString, ColorFlags> Configuration::relevantThemes = {
{ "tango", LightFlag },
{ "white", LightFlag }
};
static const QString DEFAULT_LIGHT_COLOR_THEME = "cutter";
static const QString DEFAULT_DARK_COLOR_THEME = "ayu";
const QHash<QString, QHash<ColorFlags, QColor>> 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<int>(*flags);
}
return DarkFlag | LightFlag;
}
void Configuration::resetToDefaultAsmOptions()
{
for (auto it = asmOptions.cbegin(); it != asmOptions.cend(); it++) {

View File

@ -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;

View File

@ -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();

View File

@ -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());
}