Skip to content

Truly independent web browser

UI/Qt: Read the desktop appearance from the settings portal

Andreas Klinga1e5cf9f7d58
diff --git a/UI/Qt/SystemAppearance.cpp b/UI/Qt/SystemAppearance.cpp
index bce60679a36..97bfb9c0335 100644
--- a/UI/Qt/SystemAppearance.cpp
+++ b/UI/Qt/SystemAppearance.cpp
@@ -4,7 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/ByteString.h>
+#include <AK/HashTable.h>
#include <AK/Platform.h>
+#include <AK/kmalloc.h>
#include <UI/Qt/SystemAppearance.h>
#include <UI/Qt/Theme.h>
@@ -13,6 +16,14 @@
#include <QStyleHints>
#include <QWidget>
+#if defined(AK_OS_LINUX)
+// NB: Qt's signals macro clashes with a member of the same name in GLib's D-Bus headers.
+# pragma push_macro("signals")
+# undef signals
+# include <gio/gio.h>
+# pragma pop_macro("signals")
+#endif
+
namespace Ladybird {
SystemAppearance& SystemAppearance::the()
@@ -107,18 +118,169 @@ void SystemAppearance::apply_color_theme(WebView::ColorTheme theme)
}
#if !defined(AK_OS_MACOS)
+# if defined(AK_OS_LINUX)
+// The color scheme the settings portal reports. Unlike Qt's, it stays the system's while the interface theme overrides
+// Qt's color scheme.
+static Optional<bool> s_portal_prefers_dark;
+# endif
+
bool SystemAppearance::query_system_prefers_dark() const
{
- // FIXME: Ask the platform directly. While the interface theme overrides Qt's color scheme, Qt reports the override,
- // so we keep the appearance we last saw from the system.
+# if defined(AK_OS_LINUX)
+ if (s_portal_prefers_dark.has_value())
+ return *s_portal_prefers_dark;
+# endif
+ // FIXME: Ask the platform directly where there is no settings portal. While the interface theme overrides Qt's
+ // color scheme, Qt reports the override, so we keep the appearance we last saw from the system.
if (m_interface_theme != WebView::ThemePreference::System)
return m_prefers_dark;
return QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark;
}
+# if defined(AK_OS_LINUX)
+// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Settings.html
+static constexpr auto PORTAL_BUS_NAME = "org.freedesktop.portal.Desktop";
+static constexpr auto PORTAL_OBJECT_PATH = "/org/freedesktop/portal/desktop";
+static constexpr auto SETTINGS_INTERFACE = "org.freedesktop.portal.Settings";
+static constexpr auto APPEARANCE_NAMESPACE = "org.freedesktop.appearance";
+static constexpr auto COLOR_SCHEME_KEY = "color-scheme"sv;
+static constexpr auto ACCENT_COLOR_KEY = "accent-color"sv;
+
+// The accent color is three sRGB components from 0 to 1. Any component out of that range means that there is none.
+static Optional<QColor> accent_color_from_variant(GVariant* value)
+{
+ if (!g_variant_is_of_type(value, G_VARIANT_TYPE("(ddd)")))
+ return {};
+ double red = 0;
+ double green = 0;
+ double blue = 0;
+ g_variant_get(value, "(ddd)", &red, &green, &blue);
+ for (auto component : { red, green, blue }) {
+ if (component < 0 || component > 1)
+ return {};
+ }
+ return QColor::fromRgbF(red, green, blue);
+}
+
+// The color scheme is 1 for dark and 2 for light. 0 means no preference, which portals also report on desktops that
+// have no such setting, so Qt's color scheme decides then.
+static Optional<bool> prefers_dark_from_variant(GVariant* value)
+{
+ if (!g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32))
+ return {};
+ switch (g_variant_get_uint32(value)) {
+ case 1:
+ return true;
+ case 2:
+ return false;
+ default:
+ return {};
+ }
+}
+
+void SystemAppearance::apply_portal_setting(StringView key, GVariant* value)
+{
+ if (key == COLOR_SCHEME_KEY) {
+ s_portal_prefers_dark = prefers_dark_from_variant(value);
+ update();
+ } else if (key == ACCENT_COLOR_KEY) {
+ auto accent = accent_color_from_variant(value);
+ if (accent == Theme::desktop_accent())
+ return;
+ Theme::set_desktop_accent(move(accent));
+ notify_widgets_of_palette_change();
+ }
+}
+
+// NB: Settings.ReadOne is only in version 2 of the portal. Older portals have only Settings.Read, which wraps the
+// value in one more variant.
+struct PortalSettingRead {
+ AK_ALLOC_WITH_KMALLOC;
+
+ char* key { nullptr };
+ bool is_deprecated_read { false };
+};
+
+static void read_portal_setting(GDBusConnection*, PortalSettingRead*);
+
+// The settings that changed since we asked for them, whose first read could otherwise undo the change.
+static HashTable<ByteString> s_portal_settings_changed;
+
+static void did_read_portal_setting(GObject* source, GAsyncResult* result, gpointer user_data)
+{
+ auto* read = static_cast<PortalSettingRead*>(user_data);
+ auto* connection = G_DBUS_CONNECTION(source);
+ auto* reply = g_dbus_connection_call_finish(connection, result, nullptr);
+ if (!reply && !read->is_deprecated_read) {
+ read->is_deprecated_read = true;
+ read_portal_setting(connection, read);
+ return;
+ }
+ if (reply) {
+ GVariant* value = nullptr;
+ g_variant_get(reply, "(v)", &value);
+ if (read->is_deprecated_read && g_variant_is_of_type(value, G_VARIANT_TYPE_VARIANT)) {
+ auto* inner_value = g_variant_get_variant(value);
+ g_variant_unref(value);
+ value = inner_value;
+ }
+ StringView key { read->key, strlen(read->key) };
+ if (!s_portal_settings_changed.contains(ByteString { key }))
+ SystemAppearance::the().apply_portal_setting(key, value);
+ g_variant_unref(value);
+ g_variant_unref(reply);
+ }
+ g_free(read->key);
+ delete read;
+}
+
+static void read_portal_setting(GDBusConnection* connection, PortalSettingRead* read)
+{
+ g_dbus_connection_call(connection, PORTAL_BUS_NAME, PORTAL_OBJECT_PATH, SETTINGS_INTERFACE,
+ read->is_deprecated_read ? "Read" : "ReadOne", g_variant_new("(ss)", APPEARANCE_NAMESPACE, read->key),
+ G_VARIANT_TYPE("(v)"), G_DBUS_CALL_FLAGS_NONE, -1, nullptr, did_read_portal_setting, read);
+}
+
+static void read_portal_setting(GDBusConnection* connection, StringView key)
+{
+ read_portal_setting(connection, new PortalSettingRead { .key = g_strndup(key.characters_without_null_termination(), key.length()) });
+}
+
+static void portal_setting_changed(GDBusConnection*, char const*, char const*, char const*, char const*, GVariant* parameters, gpointer)
+{
+ char const* key = nullptr;
+ GVariant* value = nullptr;
+ g_variant_get(parameters, "(&s&sv)", nullptr, &key, &value);
+ StringView key_view { key, strlen(key) };
+ s_portal_settings_changed.set(ByteString { key_view });
+ SystemAppearance::the().apply_portal_setting(key_view, value);
+ g_variant_unref(value);
+}
+
+static void did_get_session_bus(GObject*, GAsyncResult* result, gpointer)
+{
+ // NB: The connection is kept for the lifetime of the process, as the subscription below needs it.
+ auto* connection = g_bus_get_finish(result, nullptr);
+ if (!connection)
+ return;
+ g_dbus_connection_signal_subscribe(connection, PORTAL_BUS_NAME, SETTINGS_INTERFACE, "SettingChanged", PORTAL_OBJECT_PATH,
+ APPEARANCE_NAMESPACE, G_DBUS_SIGNAL_FLAGS_NONE, portal_setting_changed, nullptr, nullptr);
+ read_portal_setting(connection, COLOR_SCHEME_KEY);
+ read_portal_setting(connection, ACCENT_COLOR_KEY);
+}
+
+// NB: Qt's palette does not follow the accent color of desktops such as GNOME, and Qt's color scheme reports the
+// interface theme's override instead of the system's. The settings portal reports both. Everything here is
+// asynchronous, so a missing portal or session bus never holds up startup.
+void SystemAppearance::observe_platform_changes()
+{
+ g_bus_get(G_BUS_TYPE_SESSION, nullptr, did_get_session_bus, nullptr);
+}
+# else
void SystemAppearance::observe_platform_changes()
{
}
+# endif
bool SystemAppearance::prefers_reduced_motion()
{
diff --git a/UI/Qt/SystemAppearance.h b/UI/Qt/SystemAppearance.h
index d3be76abbea..fc9d08c2e0c 100644
--- a/UI/Qt/SystemAppearance.h
+++ b/UI/Qt/SystemAppearance.h
@@ -6,10 +6,16 @@
#pragma once
+#include <AK/Platform.h>
+#include <AK/StringView.h>
#include <LibWebView/Settings.h>
#include <QObject>
+#if defined(AK_OS_LINUX)
+typedef struct _GVariant GVariant;
+#endif
+
namespace Ladybird {
// The light or dark appearance chosen in the system's settings. The interface theme setting overrides the appearance
@@ -30,6 +36,11 @@ public:
void apply_interface_theme(WebView::ThemePreference);
void apply_color_theme(WebView::ColorTheme);
+#if defined(AK_OS_LINUX)
+ // Takes in a setting that the desktop's settings portal reported.
+ void apply_portal_setting(StringView key, GVariant* value);
+#endif
+
signals:
void changed();
// Our windows switched between the light and dark appearance, whether the system or the interface theme switched
diff --git a/UI/Qt/Theme.cpp b/UI/Qt/Theme.cpp
index 3f8909236da..61aeb4b9192 100644
--- a/UI/Qt/Theme.cpp
+++ b/UI/Qt/Theme.cpp
@@ -443,11 +443,23 @@ static AccentColor const& system_accent_color()
return *closest;
}
+static Optional<QColor> s_desktop_accent;
+
+void set_desktop_accent(Optional<QColor> color)
+{
+ s_desktop_accent = move(color);
+}
+
+Optional<QColor> const& desktop_accent()
+{
+ return s_desktop_accent;
+}
+
// The theme's accent clears 3:1 on the toolbar, darker on the light appearance and lighter on the dark one.
static QColor accent(bool dark, QColor const& toolbar)
{
- if (s_color_theme.accent.has_value())
- return with_contrast(*s_color_theme.accent, toolbar, 3, dark ? Adjustment::Lighten : Adjustment::Darken);
+ if (auto const& base_color = s_color_theme.accent.has_value() ? s_color_theme.accent : s_desktop_accent; base_color.has_value())
+ return with_contrast(*base_color, toolbar, 3, dark ? Adjustment::Lighten : Adjustment::Darken);
auto const& accent = system_accent_color();
return dark ? accent.dark : accent.light;
}
@@ -484,8 +496,9 @@ QColor focus_ring()
static TextSelectionColors text_selection(QPalette::ColorGroup group, bool dark, QColor const& toolbar, QColor const& text)
{
- // NB: The system's colors suit only chrome of the system's own appearance, which a private window may not have.
- if (!s_color_theme.accent.has_value()) {
+ // NB: The system's colors suit only chrome of the system's own appearance, which a private window may not have. A
+ // desktop accent is tinted like a theme's, as the desktop's own text fields show it.
+ if (!s_color_theme.accent.has_value() && !s_desktop_accent.has_value()) {
auto system_palette = QGuiApplication::palette();
auto system_is_dark = system_palette.color(QPalette::Window).lightness() < 128;
if (system_is_dark == dark)
diff --git a/UI/Qt/Theme.h b/UI/Qt/Theme.h
index 226c8cdebbd..a6a66d09ecb 100644
--- a/UI/Qt/Theme.h
+++ b/UI/Qt/Theme.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Optional.h>
#include <AK/Types.h>
#include <QColor>
@@ -92,6 +93,10 @@ bool is_dark(QPalette const&);
bool is_dark(QWidget const&);
PanelColors const& panel();
+// The accent color the desktop's settings portal reports, as Qt's palette does not follow it.
+void set_desktop_accent(Optional<QColor>);
+Optional<QColor> const& desktop_accent();
+
// The color theme's accent, or the system's accent color if the theme has none, adjusted so it stays clear on the chrome
// in light and dark appearances. It is used only for focus rings, selection and load progress.
QColor accent();
@@ -101,7 +106,7 @@ QColor focus_ring();
// Selected text in a field. A color theme tints it with its accent and keeps the text in its normal color, so it stays
// readable on every theme and looks unlike a selected row. A window in the background shows a gray tint instead.
-// Without a color theme, it is the system's own text selection color.
+// Without a color theme or a desktop accent, it is the system's own text selection color.
struct TextSelectionColors {
QColor background;
QColor text;
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index 7252e0caccc..dcd91f2288c 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -2029,16 +2029,18 @@ static Core::AnonymousBuffer make_system_theme_from_qt_palette(QWidget& widget,
return color;
};
- auto active_highlight = qt_palette.color(QPalette::Active, QPalette::ColorRole::Highlight);
+ // NB: Qt's palette does not follow the desktop's accent color, which desktops such as GNOME select text in.
+ auto active_highlight = Theme::desktop_accent().value_or(qt_palette.color(QPalette::Active, QPalette::ColorRole::Highlight));
auto inactive_highlight = qt_palette.color(QPalette::Inactive, QPalette::ColorRole::Highlight);
auto palette_color_scheme = dark ? Web::CSS::PreferredColorScheme::Dark : Web::CSS::PreferredColorScheme::Light;
palette.set_color(Gfx::ColorRole::Selection, selection_color(active_highlight, palette_color_scheme));
palette.set_color(Gfx::ColorRole::LightSelection, selection_color(active_highlight, Web::CSS::PreferredColorScheme::Light));
palette.set_color(Gfx::ColorRole::DarkSelection, selection_color(active_highlight, Web::CSS::PreferredColorScheme::Dark));
- if (inactive_highlight != active_highlight) {
+ if (qt_palette_matches && !Theme::desktop_accent().has_value() && inactive_highlight != active_highlight) {
palette.set_color(Gfx::ColorRole::InactiveSelection, selection_color(inactive_highlight, palette_color_scheme));
} else {
- // The Qt theme does not differentiate inactive selections; use a neutral gray.
+ // NB: Where the Qt theme has no inactive selection color of its own, its palette has the other appearance
+ // than pages get, or the selection takes the desktop's accent, inactive selections are a neutral gray.
auto inactive_selection = dark ? Gfx::Color(0x60, 0x60, 0x60) : Gfx::Color(0xd4, 0xd4, 0xd4);
palette.set_color(Gfx::ColorRole::InactiveSelection, inactive_selection);
}