diff options
author | Gerald Combs <gerald@wireshark.org> | 2020-10-05 14:50:29 -0700 |
---|---|---|
committer | Wireshark GitLab Utility <gerald+gitlab-utility@wireshark.org> | 2020-10-06 19:24:59 +0000 |
commit | 511fa081df72d5b1d93b1ef14045340abb351a6d (patch) | |
tree | 6b0e8ab8202bcd95900edb417b35fde00ff966f0 /ui | |
parent | 2402521a6938251802cdd84601cf42bc0d08e350 (diff) |
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.
Diffstat (limited to 'ui')
-rw-r--r-- | ui/qt/font_color_preferences_frame.cpp | 15 | ||||
-rw-r--r-- | ui/qt/utils/color_utils.cpp | 9 | ||||
-rw-r--r-- | ui/qt/utils/color_utils.h | 16 | ||||
-rw-r--r-- | ui/qt/widgets/syntax_line_edit.cpp | 64 | ||||
-rw-r--r-- | ui/qt/widgets/syntax_line_edit.h | 1 |
5 files changed, 71 insertions, 34 deletions
diff --git a/ui/qt/font_color_preferences_frame.cpp b/ui/qt/font_color_preferences_frame.cpp index 83bde7034f..5ec6af15d0 100644 --- a/ui/qt/font_color_preferences_frame.cpp +++ b/ui/qt/font_color_preferences_frame.cpp @@ -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) diff --git a/ui/qt/utils/color_utils.cpp b/ui/qt/utils/color_utils.cpp index 2fe6cbbcc5..e8972a5679 100644 --- a/ui/qt/utils/color_utils.cpp +++ b/ui/qt/utils/color_utils.cpp @@ -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 * diff --git a/ui/qt/utils/color_utils.h b/ui/qt/utils/color_utils.h index 28fc84e7fb..660ef87309 100644 --- a/ui/qt/utils/color_utils.h +++ b/ui/qt/utils/color_utils.h @@ -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: diff --git a/ui/qt/widgets/syntax_line_edit.cpp b/ui/qt/widgets/syntax_line_edit.cpp index 9d8b954517..75a496c684 100644 --- a/ui/qt/widgets/syntax_line_edit.cpp +++ b/ui/qt/widgets/syntax_line_edit.cpp @@ -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;" + " color: %2;" + " background-color: %3;" "}" - "SyntaxLineEdit[syntaxState=\"%2\"] {" + "SyntaxLineEdit[syntaxState=\"%4\"] {" " color: %5;" - " background-color: %8;" + " background-color: %6;" "}" - "SyntaxLineEdit[syntaxState=\"%3\"] {" - " color: %5;" + "SyntaxLineEdit[syntaxState=\"%7\"] {" + " color: %8;" " background-color: %9;" "}" - "SyntaxLineEdit[syntaxState=\"%4\"] {" - " color: %10;" - " background-color: %6;" + "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(Deprecated) - .arg(Busy) + .arg(invalid_fg.name()) + .arg(invalid_bg.name()) - // Normal foreground / background - .arg("palette(text)") - .arg("palette(base)") + .arg(Deprecated) + .arg(deprecated_fg.name()) + .arg(deprecated_bg.name()) - // 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) + .arg(busy_fg.name()) + .arg(palette().base().color().name()) ; setStyleSheet(style_sheet_); } diff --git a/ui/qt/widgets/syntax_line_edit.h b/ui/qt/widgets/syntax_line_edit.h index c63af3de26..d97b8d6119 100644 --- a/ui/qt/widgets/syntax_line_edit.h +++ b/ui/qt/widgets/syntax_line_edit.h @@ -70,7 +70,6 @@ private: QString state_style_sheet_; QString syntax_error_message_; QString token_chars_; - QColor busy_fg_; bool completion_enabled_; private slots: |