Qt: Handle dark mode in syntax highlighting.
Add ColorUtils::contrastingTextColor, which chooses an appropriate text color from the current application palette for a given background. Use it in SyntaxLineEdit and FontColorPreferencesFrame for each state background color. Fixes #15840.
This commit is contained in:
parent
2402521a69
commit
511fa081df
|
@ -256,32 +256,35 @@ void FontColorPreferencesFrame::updateWidgets()
|
|||
//
|
||||
// Sample valid filter
|
||||
//
|
||||
QColor ss_bg = ColorUtils::fromColorT(prefs_get_color_value(pref_valid_bg_, pref_stashed));
|
||||
ui->validFilterBGPushButton->setStyleSheet(color_button_ss.arg(
|
||||
ColorUtils::fromColorT(prefs_get_color_value(pref_valid_bg_, pref_stashed)).name())
|
||||
.arg(0));
|
||||
ui->validFilterSampleLineEdit->setStyleSheet(sample_text_ss.arg(
|
||||
"palette(text)",
|
||||
ColorUtils::fromColorT(prefs_get_color_value(pref_valid_bg_, pref_stashed)).name()));
|
||||
ColorUtils::contrastingTextColor(ss_bg).name(),
|
||||
ss_bg.name()));
|
||||
|
||||
//
|
||||
// Sample invalid filter
|
||||
//
|
||||
ss_bg = ColorUtils::fromColorT(prefs_get_color_value(pref_invalid_bg_, pref_stashed));
|
||||
ui->invalidFilterBGPushButton->setStyleSheet(color_button_ss.arg(
|
||||
ColorUtils::fromColorT(prefs_get_color_value(pref_invalid_bg_, pref_stashed)).name())
|
||||
.arg(0));
|
||||
ui->invalidFilterSampleLineEdit->setStyleSheet(sample_text_ss.arg(
|
||||
"palette(text)",
|
||||
ColorUtils::fromColorT(prefs_get_color_value(pref_invalid_bg_, pref_stashed)).name()));
|
||||
ColorUtils::contrastingTextColor(ss_bg).name(),
|
||||
ss_bg.name()));
|
||||
|
||||
//
|
||||
// Sample warning filter
|
||||
//
|
||||
ss_bg = ColorUtils::fromColorT(prefs_get_color_value(pref_deprecated_bg_, pref_stashed));
|
||||
ui->deprecatedFilterBGPushButton->setStyleSheet(color_button_ss.arg(
|
||||
ColorUtils::fromColorT(prefs_get_color_value(pref_deprecated_bg_, pref_stashed)).name())
|
||||
.arg(0));
|
||||
ui->deprecatedFilterSampleLineEdit->setStyleSheet(sample_text_ss.arg(
|
||||
"palette(text)",
|
||||
ColorUtils::fromColorT(prefs_get_color_value(pref_deprecated_bg_, pref_stashed)).name()));
|
||||
ColorUtils::contrastingTextColor(ss_bg).name(),
|
||||
ss_bg.name()));
|
||||
}
|
||||
|
||||
void FontColorPreferencesFrame::changeColor(pref_t *pref)
|
||||
|
|
|
@ -179,6 +179,15 @@ QString ColorUtils::themeLinkStyle()
|
|||
return link_style;
|
||||
}
|
||||
|
||||
const QColor ColorUtils::contrastingTextColor(const QColor color)
|
||||
{
|
||||
bool background_is_light = color.lightness() > 127;
|
||||
if ( (background_is_light && !ColorUtils::themeIsDark()) || (!background_is_light && ColorUtils::themeIsDark()) ) {
|
||||
return QApplication::palette().text().color();
|
||||
}
|
||||
return QApplication::palette().base().color();
|
||||
}
|
||||
|
||||
/*
|
||||
* Editor modelines
|
||||
*
|
||||
|
|
|
@ -52,8 +52,24 @@ public:
|
|||
* @return true if we're running in dark mode, false otherwise.
|
||||
*/
|
||||
static bool themeIsDark();
|
||||
/**
|
||||
* Returns an appropriate link color for the current mode.
|
||||
* @return A brush suitable for setting a text color.
|
||||
*/
|
||||
static QBrush themeLinkBrush();
|
||||
/**
|
||||
* Returns an appropriate HTML+CSS link style for the current mode.
|
||||
* @return A "<style>a:link { color: ... ; }</style>" string
|
||||
*/
|
||||
static QString themeLinkStyle();
|
||||
/**
|
||||
* Returns either QPalette::Text or QPalette::Base as appropriate for the
|
||||
* specified foreground color
|
||||
*
|
||||
* @param color The background color.
|
||||
* @return A contrasting foreground color for the current mode / theme.
|
||||
*/
|
||||
static const QColor contrastingTextColor(const QColor color);
|
||||
|
||||
signals:
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include <ui/qt/utils/stock_icon.h>
|
||||
|
||||
#include <QAbstractItemView>
|
||||
#include <QApplication>
|
||||
#include <QCompleter>
|
||||
#include <QKeyEvent>
|
||||
#include <QPainter>
|
||||
|
@ -41,13 +42,6 @@ SyntaxLineEdit::SyntaxLineEdit(QWidget *parent) :
|
|||
completion_model_(NULL),
|
||||
completion_enabled_(false)
|
||||
{
|
||||
// Try to matche QLineEdit's placeholder text color (which sets the
|
||||
// alpha channel to 50%, which doesn't work in style sheets).
|
||||
// Setting the foreground color lets us avoid yet another background
|
||||
// color preference and should hopefully make things easier to
|
||||
// distinguish for color blind folk.
|
||||
busy_fg_ = ColorUtils::alphaBlend(palette().text(), palette().base(), 0.5);
|
||||
|
||||
setSyntaxState();
|
||||
setMaxLength(std::numeric_limits<quint32>::max());
|
||||
}
|
||||
|
@ -83,43 +77,59 @@ void SyntaxLineEdit::allowCompletion(bool enabled)
|
|||
|
||||
void SyntaxLineEdit::setSyntaxState(SyntaxState state) {
|
||||
syntax_state_ = state;
|
||||
|
||||
QColor valid_bg = ColorUtils::fromColorT(&prefs.gui_text_valid);
|
||||
QColor valid_fg = ColorUtils::contrastingTextColor(valid_bg);
|
||||
QColor invalid_bg = ColorUtils::fromColorT(&prefs.gui_text_invalid);
|
||||
QColor invalid_fg = ColorUtils::contrastingTextColor(invalid_bg);
|
||||
QColor deprecated_bg = ColorUtils::fromColorT(&prefs.gui_text_deprecated);
|
||||
QColor deprecated_fg = ColorUtils::contrastingTextColor(deprecated_bg);
|
||||
|
||||
// Try to matche QLineEdit's placeholder text color (which sets the
|
||||
// alpha channel to 50%, which doesn't work in style sheets).
|
||||
// Setting the foreground color lets us avoid yet another background
|
||||
// color preference and should hopefully make things easier to
|
||||
// distinguish for color blind folk.
|
||||
QColor busy_fg = ColorUtils::alphaBlend(QApplication::palette().text(), QApplication::palette().base(), 0.5);
|
||||
|
||||
state_style_sheet_ = QString(
|
||||
"SyntaxLineEdit[syntaxState=\"%1\"] {"
|
||||
" color: %5;"
|
||||
" background-color: %7;"
|
||||
"}"
|
||||
|
||||
"SyntaxLineEdit[syntaxState=\"%2\"] {"
|
||||
" color: %5;"
|
||||
" background-color: %8;"
|
||||
"}"
|
||||
|
||||
"SyntaxLineEdit[syntaxState=\"%3\"] {"
|
||||
" color: %5;"
|
||||
" background-color: %9;"
|
||||
" color: %2;"
|
||||
" background-color: %3;"
|
||||
"}"
|
||||
|
||||
"SyntaxLineEdit[syntaxState=\"%4\"] {"
|
||||
" color: %10;"
|
||||
" color: %5;"
|
||||
" background-color: %6;"
|
||||
"}"
|
||||
|
||||
"SyntaxLineEdit[syntaxState=\"%7\"] {"
|
||||
" color: %8;"
|
||||
" background-color: %9;"
|
||||
"}"
|
||||
|
||||
"SyntaxLineEdit[syntaxState=\"%10\"] {"
|
||||
" color: %11;"
|
||||
" background-color: %12;"
|
||||
"}"
|
||||
)
|
||||
|
||||
// CSS selectors
|
||||
// CSS selector, foreground, background
|
||||
.arg(Valid)
|
||||
.arg(valid_fg.name())
|
||||
.arg(valid_bg.name())
|
||||
|
||||
.arg(Invalid)
|
||||
.arg(invalid_fg.name())
|
||||
.arg(invalid_bg.name())
|
||||
|
||||
.arg(Deprecated)
|
||||
.arg(deprecated_fg.name())
|
||||
.arg(deprecated_bg.name())
|
||||
|
||||
.arg(Busy)
|
||||
|
||||
// Normal foreground / background
|
||||
.arg("palette(text)")
|
||||
.arg("palette(base)")
|
||||
|
||||
// Special foreground / background
|
||||
.arg(ColorUtils::fromColorT(&prefs.gui_text_valid).name())
|
||||
.arg(ColorUtils::fromColorT(&prefs.gui_text_invalid).name())
|
||||
.arg(ColorUtils::fromColorT(&prefs.gui_text_deprecated).name())
|
||||
.arg(busy_fg_.name())
|
||||
.arg(busy_fg.name())
|
||||
.arg(palette().base().color().name())
|
||||
;
|
||||
setStyleSheet(style_sheet_);
|
||||
}
|
||||
|
|
|
@ -70,7 +70,6 @@ private:
|
|||
QString state_style_sheet_;
|
||||
QString syntax_error_message_;
|
||||
QString token_chars_;
|
||||
QColor busy_fg_;
|
||||
bool completion_enabled_;
|
||||
|
||||
private slots:
|
||||
|
|
Loading…
Reference in New Issue