Truly independent web browser
UI/Qt: Honor the interface theme where Qt can't apply it
Andreas Kling2a068fc66474
diff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cppindex 0a6b475c336..693deba863b 100644--- a/UI/Qt/LocationEdit.cpp+++ b/UI/Qt/LocationEdit.cpp@@ -400,7 +400,7 @@ LocationEdit::LocationEdit(QWidget* parent, WebView::IsPrivate is_private)m_omnibox.cursor_moved(at_end);});- connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, [this] {+ connect(&SystemAppearance::the(), &SystemAppearance::interface_appearance_changed, this, [this] {schedule_chrome_style_update();});}diff --git a/UI/Qt/SystemAppearance.cpp b/UI/Qt/SystemAppearance.cppindex 9b478c64fdb..bce60679a36 100644--- a/UI/Qt/SystemAppearance.cpp+++ b/UI/Qt/SystemAppearance.cpp@@ -39,6 +39,9 @@ SystemAppearance::SystemAppearance()QObject::connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, [this] {update();+ // While the interface theme is Light or Dark, our windows keep their appearance whatever Qt reports.+ if (m_interface_theme == WebView::ThemePreference::System)+ emit interface_appearance_changed();});// The chrome's colors depend on the contrast preference, but changing it does not change the application palette.QObject::connect(QGuiApplication::styleHints()->accessibility(), &QAccessibilityHints::contrastPreferenceChanged, this, [] {@@ -61,7 +64,9 @@ void SystemAppearance::apply_interface_theme(WebView::ThemePreference theme){if (theme == m_interface_theme)return;+ auto was_dark = Theme::is_dark();m_interface_theme = theme;+ Theme::set_interface_theme(theme);switch (theme) {case WebView::ThemePreference::System:@@ -74,6 +79,22 @@ void SystemAppearance::apply_interface_theme(WebView::ThemePreference theme)QGuiApplication::styleHints()->setColorScheme(Qt::ColorScheme::Dark);break;}++ // NB: Some platform themes, such as KDE's, ignore requests for a color scheme. Qt then neither changes its color+ // scheme nor tells anyone, so the chrome learns of the switch from us instead.+ update_interface_appearance(was_dark);+}++void SystemAppearance::update_interface_appearance(bool was_dark)+{+ if (Theme::is_dark() == was_dark)+ return;++ // NB: Qt's color scheme can't tell whether Qt restyles the widgets: it is unchanged where the request was ignored+ // but the system already had the requested scheme, and elsewhere it only changes once the theme change has+ // been delivered.+ notify_widgets_of_palette_change();+ emit interface_appearance_changed();}void SystemAppearance::apply_color_theme(WebView::ColorTheme theme)diff --git a/UI/Qt/SystemAppearance.h b/UI/Qt/SystemAppearance.hindex 32f12032e45..d3be76abbea 100644--- a/UI/Qt/SystemAppearance.h+++ b/UI/Qt/SystemAppearance.h@@ -32,11 +32,15 @@ public:signals:void changed();+ // Our windows switched between the light and dark appearance, whether the system or the interface theme switched+ // them.+ void interface_appearance_changed();private:SystemAppearance();void update();+ void update_interface_appearance(bool was_dark);bool query_system_prefers_dark() const;void observe_platform_changes();diff --git a/UI/Qt/Theme.cpp b/UI/Qt/Theme.cppindex 71071c560d3..a15d1c14821 100644--- a/UI/Qt/Theme.cpp+++ b/UI/Qt/Theme.cpp@@ -17,8 +17,24 @@namespace Ladybird::Theme {+static WebView::ThemePreference s_interface_theme { WebView::ThemePreference::System };++void set_interface_theme(WebView::ThemePreference theme)+{+ s_interface_theme = theme;+}+bool is_dark(){+ switch (s_interface_theme) {+ case WebView::ThemePreference::Light:+ return false;+ case WebView::ThemePreference::Dark:+ return true;+ case WebView::ThemePreference::System:+ break;+ }+auto color_scheme = QGuiApplication::styleHints()->colorScheme();if (color_scheme != Qt::ColorScheme::Unknown)return color_scheme == Qt::ColorScheme::Dark;diff --git a/UI/Qt/Theme.h b/UI/Qt/Theme.hindex 942a6979e4f..68227069463 100644--- a/UI/Qt/Theme.h+++ b/UI/Qt/Theme.h@@ -16,6 +16,7 @@ class QWidget;namespace WebView {enum class ColorTheme : u8;+enum class ThemePreference : u8;}@@ -70,6 +71,10 @@ struct PanelColors {bool is_dark();bool has_increased_contrast();+// The interface theme decides the appearance, as Qt's color scheme only follows it where the platform theme honors the+// request.+void set_interface_theme(WebView::ThemePreference);+// The chrome and panel colors, and the accent, are derived from the color theme's base colors.void set_color_theme(WebView::ColorTheme);diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex d0d1d1d19f6..ac4d246751b 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -191,7 +191,7 @@ WebContentView::WebContentView(QWidget* window, Optional<WebView::CanonicalTraveschedule_repaint();});};- QObject::connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, update_palette_soon);+ QObject::connect(&SystemAppearance::the(), &SystemAppearance::interface_appearance_changed, this, update_palette_soon);QObject::connect(&SystemAppearance::the(), &SystemAppearance::changed, this, update_palette_soon);m_tooltip_hover_timer.setSingleShot(true);@@ -1978,9 +1978,11 @@ static Core::AnonymousBuffer make_system_theme_from_qt_palette(QWidget& widget,auto palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme);auto palette = Gfx::Palette(move(palette_impl));- // The widgets' palette follows the interface theme. When pages get the other appearance, they keep the colors of- // the theme file instead.- auto qt_palette_matches = ChromeStyle::is_dark(qt_palette) == dark;+ // When the widgets' palette has the other appearance than pages get, pages keep the colors of the theme file instead.+ // NB: The palette is judged by its own colors, as the platform theme may keep the system's palette whatever the+ // interface theme asks for.+ auto qt_palette_is_dark = qt_palette.color(QPalette::Window).lightness() < 128;+ auto qt_palette_matches = qt_palette_is_dark == dark;auto translate = [&](Gfx::ColorRole gfx_color_role, QPalette::ColorRole qt_color_role) {if (!qt_palette_matches)return;