Truly independent web browser
LibWebView+Hub+UI/Qt: Let pages follow the system’s reduced motion
sideshowbarker23725ea040fb
diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cppindex 0050211f938..d6adf5a5851 100644--- a/Libraries/LibWeb/HTML/Window.cpp+++ b/Libraries/LibWeb/HTML/Window.cpp@@ -703,7 +703,8 @@ CSS::Parser::ValueParserFFI::FfiMediaFeatureValue Window::query_media_feature(CSreturn ident_media_feature_value(CSS::Keyword::Reduce);case CSS::PreferredMotion::Auto:default:- // FIXME: Fallback to system settings+ // The browser resolves Auto from the platform's settings before a page sees it, so Auto only remains where+ // nothing reads them.return ident_media_feature_value(CSS::Keyword::NoPreference);}case CSS::MediaFeatureID::PrefersReducedTransparency:diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex acb075d7cb5..89f66ef6381 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -2933,6 +2933,17 @@ void Application::set_preferred_motion(Web::CSS::PreferredMotion motion)});}+void Application::platform_motion_preference_changed()+{+ // Only Auto follows the platform.+ if (m_motion != Web::CSS::PreferredMotion::Auto)+ return;+ ViewImplementation::for_each_view([&](ViewImplementation& view) {+ view.set_preferred_motion(m_motion);+ return IterationDecision::Continue;+ });+}+void Application::set_user_agent_string(String user_agent){m_user_agent_string = move(user_agent);diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex af23532a8fc..be3f191a60b 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -153,6 +153,10 @@ public:virtual bool platform_scrollbar_track_press_jumps() const { return false; }// Tells the compositor of a change to what platform_scrollbar_track_press_jumps() returns.void platform_scrollbar_track_press_behavior_changed();+ // Returns true if the platform's settings ask for less motion, which pages see while the motion preference is Auto.+ virtual bool platform_prefers_reduced_motion() const { return false; }+ // Tells every page of a change to what platform_prefers_reduced_motion() returns.+ void platform_motion_preference_changed();// The languages the user prefers, as the platform lists them (BCP 47 tags, most preferred first).virtual Vector<String> platform_preferred_languages() const { return {}; }// The GPU the platform presents on, which the compositor paints on too, so that it can share its frames without a copy.diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 8484b8cfe99..9e95e9ed7b3 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -1601,11 +1601,19 @@ void ViewImplementation::set_preferred_contrast(Web::CSS::PreferredContrast cont});}+// Pages can't read the platform's settings, so Auto reaches them as what the platform asks for.+static Web::CSS::PreferredMotion motion_for_pages(Web::CSS::PreferredMotion motion)+{+ if (motion != Web::CSS::PreferredMotion::Auto)+ return motion;+ return Application::the().platform_prefers_reduced_motion() ? Web::CSS::PreferredMotion::Reduce : Web::CSS::PreferredMotion::NoPreference;+}+void ViewImplementation::set_preferred_motion(Web::CSS::PreferredMotion motion){m_preferred_motion = motion;traversable().for_each_hosting_page([&](WebContentPage& page) {- page.async_set_preferred_motion(motion);+ page.async_set_preferred_motion(motion_for_pages(motion));});}@@ -1644,7 +1652,7 @@ void ViewImplementation::send_preferences_to_page(WebContentPage& page)send_system_theme_to_page(page);page.async_set_preferred_color_scheme(preferred_color_scheme_for_page(page));page.async_set_preferred_contrast(m_preferred_contrast);- page.async_set_preferred_motion(m_preferred_motion);+ page.async_set_preferred_motion(motion_for_pages(m_preferred_motion));page.async_set_preferred_languages(Application::settings().languages());page.async_set_zoom_level(m_zoom_level);send_browsing_behavior(page);diff --git a/Services/Hub/Application.cpp b/Services/Hub/Application.cppindex bfb4e470203..d2ca60feb9f 100644--- a/Services/Hub/Application.cpp+++ b/Services/Hub/Application.cpp@@ -71,9 +71,12 @@ bool Application::ui_has_translator() constvoid Application::set_ui_info(Hub::UIInfo info){auto scrollbar_track_press_jumps_changed = !m_ui_info.has_value() || m_ui_info->scrollbar_track_press_jumps != info.scrollbar_track_press_jumps;+ auto prefers_reduced_motion_changed = !m_ui_info.has_value() || m_ui_info->prefers_reduced_motion != info.prefers_reduced_motion;m_ui_info = move(info);if (scrollbar_track_press_jumps_changed)platform_scrollbar_track_press_behavior_changed();+ if (prefers_reduced_motion_changed)+ platform_motion_preference_changed();}View& Application::create_view(View::InitialState initial_state, Optional<WebView::CanonicalTraversable&> traversable)@@ -574,6 +577,15 @@ bool Application::platform_scrollbar_track_press_jumps() constreturn WebView::Application::platform_scrollbar_track_press_jumps();}+bool Application::platform_prefers_reduced_motion() const+{+ if (m_host)+ return m_host->platform_prefers_reduced_motion();+ if (auto const* info = ui_info())+ return info->prefers_reduced_motion;+ return WebView::Application::platform_prefers_reduced_motion();+}+Vector<String> Application::platform_preferred_languages() const{if (m_host)diff --git a/Services/Hub/Application.h b/Services/Hub/Application.hindex 4dbbcc77f98..2d7bfbaf3dc 100644--- a/Services/Hub/Application.h+++ b/Services/Hub/Application.h@@ -37,6 +37,7 @@ public:virtual bool platform_reports_scroll_momentum() const = 0;virtual bool platform_scrolls_elastically() const = 0;virtual bool platform_scrollbar_track_press_jumps() const = 0;+ virtual bool platform_prefers_reduced_motion() const = 0;virtual Vector<String> platform_preferred_languages() const = 0;virtual Optional<Gfx::GpuDeviceIdentity> platform_presentation_gpu_device() const = 0;virtual Vector<u64> platform_presentation_drm_format_modifiers() const = 0;@@ -120,6 +121,7 @@ private:virtual bool platform_reports_scroll_momentum() const override;virtual bool platform_scrolls_elastically() const override;virtual bool platform_scrollbar_track_press_jumps() const override;+ virtual bool platform_prefers_reduced_motion() const override;virtual Vector<String> platform_preferred_languages() const override;virtual Optional<Gfx::GpuDeviceIdentity> platform_presentation_gpu_device() const override;virtual Vector<u64> platform_presentation_drm_format_modifiers() const override;diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipcindex d294704f8e4..d42f9656d97 100644--- a/Services/Hub/HubClient.ipc+++ b/Services/Hub/HubClient.ipc@@ -133,6 +133,7 @@ struct Hub::UIInfo {bool reports_scroll_momentum;bool scrolls_elastically;bool scrollbar_track_press_jumps;+ bool prefers_reduced_motion;Vector<String> preferred_languages;Optional<Gfx::GpuDeviceIdentity> presentation_gpu_device;Vector<u64> presentation_drm_format_modifiers;diff --git a/Tests/UI/Qt/CMakeLists.txt b/Tests/UI/Qt/CMakeLists.txtindex 105bdc2cf8b..bc9aabfc41a 100644--- a/Tests/UI/Qt/CMakeLists.txt+++ b/Tests/UI/Qt/CMakeLists.txt@@ -57,6 +57,16 @@ if (APPLE OR LINUX))endif()+# Only Linux reads the system's settings from the desktop's settings portal.+if (LINUX AND NOT ANDROID)+ list(APPEND TEST_SOURCES+ TestSystemAppearance.cpp+ )+ find_package(PkgConfig REQUIRED)+ pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0)+ target_link_libraries(TestUIQt PRIVATE PkgConfig::GIO)+endif()+target_sources(TestUIQt PRIVATE ${TEST_SOURCES})set_property(SOURCE TestProcessManagerWindow.cpp APPEND PROPERTY COMPILE_DEFINITIONS TEST_RESOURCE_ROOT="${LADYBIRD_SOURCE_DIR}/Base/res")diff --git a/Tests/UI/Qt/TestSystemAppearance.cpp b/Tests/UI/Qt/TestSystemAppearance.cppnew file mode 100644index 00000000000..40e3fca2a02--- /dev/null+++ b/Tests/UI/Qt/TestSystemAppearance.cpp@@ -0,0 +1,131 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibTest/TestCase.h>+#include <UI/Qt/SystemAppearance.h>++// 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")++using Ladybird::SystemAppearance;++static constexpr auto APPEARANCE = "org.freedesktop.appearance"sv;+static constexpr auto GNOME_INTERFACE = "org.gnome.desktop.interface"sv;+static constexpr auto KDE_GLOBALS = "org.kde.kdeglobals.KDE"sv;++static void apply(StringView namespace_, StringView key, GVariant* value)+{+ g_variant_ref_sink(value);+ SystemAppearance::the().apply_portal_setting(namespace_, key, value);+ g_variant_unref(value);+}++static void reset_motion_settings()+{+ // A value of the wrong type leaves the portal's own setting unset, as a portal without it does.+ apply(APPEARANCE, "reduced-motion"sv, g_variant_new_string(""));+ apply(GNOME_INTERFACE, "enable-animations"sv, g_variant_new_boolean(true));+ apply(KDE_GLOBALS, "AnimationDurationFactor"sv, g_variant_new_double(1));+}++TEST_CASE(gnome_animations_switched_off_ask_for_less_motion)+{+ reset_motion_settings();+ size_t changes = 0;+ auto connection = QObject::connect(&SystemAppearance::the(), &SystemAppearance::reduced_motion_changed, [&] { ++changes; });++ EXPECT(!SystemAppearance::prefers_reduced_motion());+ apply(GNOME_INTERFACE, "enable-animations"sv, g_variant_new_boolean(false));+ EXPECT(SystemAppearance::prefers_reduced_motion());+ EXPECT_EQ(changes, 1u);++ // The same value again is no change.+ apply(GNOME_INTERFACE, "enable-animations"sv, g_variant_new_boolean(false));+ EXPECT_EQ(changes, 1u);++ apply(GNOME_INTERFACE, "enable-animations"sv, g_variant_new_boolean(true));+ EXPECT(!SystemAppearance::prefers_reduced_motion());+ EXPECT_EQ(changes, 2u);++ QObject::disconnect(connection);+}++TEST_CASE(kde_instant_animations_ask_for_less_motion)+{+ reset_motion_settings();++ apply(KDE_GLOBALS, "AnimationDurationFactor"sv, g_variant_new_double(0));+ EXPECT(SystemAppearance::prefers_reduced_motion());+ apply(KDE_GLOBALS, "AnimationDurationFactor"sv, g_variant_new_double(0.5));+ EXPECT(!SystemAppearance::prefers_reduced_motion());++ // KDE's portal can report the factor as the text it read from kdeglobals.+ apply(KDE_GLOBALS, "AnimationDurationFactor"sv, g_variant_new_string("0"));+ EXPECT(SystemAppearance::prefers_reduced_motion());+ apply(KDE_GLOBALS, "AnimationDurationFactor"sv, g_variant_new_string("2"));+ EXPECT(!SystemAppearance::prefers_reduced_motion());+}++TEST_CASE(either_desktop_asking_for_less_motion_is_enough)+{+ reset_motion_settings();++ apply(KDE_GLOBALS, "AnimationDurationFactor"sv, g_variant_new_double(0));+ apply(GNOME_INTERFACE, "enable-animations"sv, g_variant_new_boolean(true));+ EXPECT(SystemAppearance::prefers_reduced_motion());+}++TEST_CASE(the_portals_reduced_motion_setting_asks_for_less_motion)+{+ reset_motion_settings();+ size_t changes = 0;+ auto connection = QObject::connect(&SystemAppearance::the(), &SystemAppearance::reduced_motion_changed, [&] { ++changes; });++ apply(APPEARANCE, "reduced-motion"sv, g_variant_new_uint32(1));+ EXPECT(SystemAppearance::prefers_reduced_motion());+ EXPECT_EQ(changes, 1u);+ apply(APPEARANCE, "reduced-motion"sv, g_variant_new_uint32(0));+ EXPECT(!SystemAppearance::prefers_reduced_motion());+ EXPECT_EQ(changes, 2u);++ // Values the portal doesn't define mean no preference.+ apply(APPEARANCE, "reduced-motion"sv, g_variant_new_uint32(2));+ EXPECT(!SystemAppearance::prefers_reduced_motion());++ QObject::disconnect(connection);+}++TEST_CASE(the_portals_reduced_motion_setting_comes_before_the_desktops_own)+{+ reset_motion_settings();++ apply(GNOME_INTERFACE, "enable-animations"sv, g_variant_new_boolean(false));+ apply(KDE_GLOBALS, "AnimationDurationFactor"sv, g_variant_new_double(0));+ apply(APPEARANCE, "reduced-motion"sv, g_variant_new_uint32(0));+ EXPECT(!SystemAppearance::prefers_reduced_motion());+}++TEST_CASE(settings_of_the_same_name_in_other_namespaces_are_ignored)+{+ reset_motion_settings();++ apply("org.freedesktop.appearance"sv, "enable-animations"sv, g_variant_new_boolean(false));+ apply("org.kde.kdeglobals.General"sv, "AnimationDurationFactor"sv, g_variant_new_double(0));+ EXPECT(!SystemAppearance::prefers_reduced_motion());+}++TEST_CASE(a_value_of_the_wrong_type_follows_no_preference)+{+ reset_motion_settings();++ apply(GNOME_INTERFACE, "enable-animations"sv, g_variant_new_boolean(false));+ EXPECT(SystemAppearance::prefers_reduced_motion());+ apply(GNOME_INTERFACE, "enable-animations"sv, g_variant_new_uint32(0));+ EXPECT(!SystemAppearance::prefers_reduced_motion());+}diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cppindex 137795d3b1f..127d9260cd5 100644--- a/UI/Qt/Application.cpp+++ b/UI/Qt/Application.cpp@@ -719,6 +719,12 @@ ErrorOr<NonnullOwnPtr<Application>> Application::create(Main::Arguments const& aauto transports = TRY(IPC::TransportHandle::create_pair());application->m_connection_from_ui = Hub::ConnectionFromUI::construct(TRY(transports.first.create_transport()));application->m_hub_connection = adopt_ref(*new HubConnection(TRY(transports.second.create_transport())));++ // The Hub reads the platform's preferences from us when it needs them, but open pages only learn of a change once+ // the Hub sends the preference again.+ QObject::connect(&SystemAppearance::the(), &SystemAppearance::reduced_motion_changed, [] {+ hub().platform_motion_preference_changed();+ });#endifreturn application;}@@ -767,6 +773,7 @@ Hub::UIInfo Application::current_ui_info() constinfo.reports_scroll_momentum = platform_reports_scroll_momentum();info.scrolls_elastically = platform_scrolls_elastically();info.scrollbar_track_press_jumps = platform_scrollbar_track_press_jumps();+ info.prefers_reduced_motion = platform_prefers_reduced_motion();info.preferred_languages = platform_preferred_languages();info.presentation_gpu_device = platform_presentation_gpu_device();info.presentation_drm_format_modifiers = platform_presentation_drm_format_modifiers();@@ -996,6 +1003,9 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)};QObject::connect(&SystemAppearance::the(), &SystemAppearance::changed, send_ui_info);QObject::connect(&SystemAppearance::the(), &SystemAppearance::interface_appearance_changed, send_ui_info);+ QObject::connect(&SystemAppearance::the(), &SystemAppearance::reduced_motion_changed, send_ui_info);+ // The Hub may have started with what we told it before a change that came in the meantime.+ send_ui_info();s_browser_has_started = true;return {};}@@ -1311,6 +1321,11 @@ bool Application::platform_scrollbar_track_press_jumps() const#endif}+bool Application::platform_prefers_reduced_motion() const+{+ return SystemAppearance::prefers_reduced_motion();+}+Vector<String> Application::platform_preferred_languages() const{// The system's UI languages, most preferred first, as BCP 47 tags ("en-US", then "en").diff --git a/UI/Qt/Application.h b/UI/Qt/Application.hindex af64caff9f8..ddf8dd9840d 100644--- a/UI/Qt/Application.h+++ b/UI/Qt/Application.h@@ -298,6 +298,7 @@ private:virtual bool platform_reports_scroll_momentum() const override;virtual bool platform_scrolls_elastically() const override;virtual bool platform_scrollbar_track_press_jumps() const override;+ virtual bool platform_prefers_reduced_motion() const override;virtual Vector<String> platform_preferred_languages() const override;virtual Optional<Gfx::GpuDeviceIdentity> platform_presentation_gpu_device() const override;virtual Vector<u64> platform_presentation_drm_format_modifiers() const override;diff --git a/UI/Qt/SystemAppearance.cpp b/UI/Qt/SystemAppearance.cppindex a6ea4d80776..1f73b13dbfa 100644--- a/UI/Qt/SystemAppearance.cpp+++ b/UI/Qt/SystemAppearance.cpp@@ -144,9 +144,23 @@ bool SystemAppearance::query_system_prefers_dark() conststatic 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 APPEARANCE_NAMESPACE = "org.freedesktop.appearance"sv;static constexpr auto COLOR_SCHEME_KEY = "color-scheme"sv;static constexpr auto ACCENT_COLOR_KEY = "accent-color"sv;+static constexpr auto REDUCED_MOTION_KEY = "reduced-motion"sv;+// Portals older than the reduced-motion setting serve their desktop's own settings: GNOME's serve its interface+// settings, and KDE's those of kdeglobals.+static constexpr auto GNOME_INTERFACE_NAMESPACE = "org.gnome.desktop.interface"sv;+static constexpr auto ENABLE_ANIMATIONS_KEY = "enable-animations"sv;+static constexpr auto KDE_GLOBALS_NAMESPACE = "org.kde.kdeglobals.KDE"sv;+static constexpr auto ANIMATION_DURATION_FACTOR_KEY = "AnimationDurationFactor"sv;++// The motion settings the portal reports: its own reduced-motion setting, and where a portal has none, GNOME's switch+// for animations and KDE's animation speed, where 0 is "Instant". Either of those turned all the way down asks for less+// motion.+static Optional<bool> s_portal_prefers_reduced_motion;+static Optional<bool> s_portal_enables_animations;+static Optional<double> s_portal_animation_duration_factor;// 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)@@ -180,17 +194,48 @@ static Optional<bool> prefers_dark_from_variant(GVariant* value)}}-void SystemAppearance::apply_portal_setting(StringView key, GVariant* value)+// KDE's portal reports the settings of kdeglobals as it reads them from the file, so the factor can come as a string.+static Optional<double> animation_duration_factor_from_variant(GVariant* value){- if (key == COLOR_SCHEME_KEY) {+ if (g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE))+ return g_variant_get_double(value);+ if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {+ gsize length = 0;+ auto const* characters = g_variant_get_string(value, &length);+ return StringView { characters, length }.to_number<double>();+ }+ return {};+}++void SystemAppearance::apply_portal_setting(StringView namespace_, StringView key, GVariant* value)+{+ if (namespace_ == APPEARANCE_NAMESPACE && key == COLOR_SCHEME_KEY) {s_portal_prefers_dark = prefers_dark_from_variant(value);update();- } else if (key == ACCENT_COLOR_KEY) {+ } else if (namespace_ == APPEARANCE_NAMESPACE && 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();+ } else if ((namespace_ == APPEARANCE_NAMESPACE && key == REDUCED_MOTION_KEY) || (namespace_ == GNOME_INTERFACE_NAMESPACE && key == ENABLE_ANIMATIONS_KEY) || (namespace_ == KDE_GLOBALS_NAMESPACE && key == ANIMATION_DURATION_FACTOR_KEY)) {+ auto prefers_reduced_motion = SystemAppearance::prefers_reduced_motion();+ if (key == REDUCED_MOTION_KEY) {+ // The setting is 1 for reduced motion. Any other value means no preference.+ if (g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32))+ s_portal_prefers_reduced_motion = g_variant_get_uint32(value) == 1;+ else+ s_portal_prefers_reduced_motion = {};+ } else if (key == ENABLE_ANIMATIONS_KEY) {+ if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN))+ s_portal_enables_animations = g_variant_get_boolean(value);+ else+ s_portal_enables_animations = {};+ } else {+ s_portal_animation_duration_factor = animation_duration_factor_from_variant(value);+ }+ if (SystemAppearance::prefers_reduced_motion() != prefers_reduced_motion)+ emit reduced_motion_changed();}}@@ -199,6 +244,7 @@ void SystemAppearance::apply_portal_setting(StringView key, GVariant* value)struct PortalSettingRead {AK_ALLOC_WITH_KMALLOC;+ char* namespace_ { nullptr };char* key { nullptr };bool is_deprecated_read { false };};@@ -208,6 +254,11 @@ 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 ByteString portal_setting_name(StringView namespace_, StringView key)+{+ return ByteString::formatted("{}.{}", namespace_, key);+}+static void did_read_portal_setting(GObject* source, GAsyncResult* result, gpointer user_data){auto* read = static_cast<PortalSettingRead*>(user_data);@@ -226,12 +277,14 @@ static void did_read_portal_setting(GObject* source, GAsyncResult* result, gpoing_variant_unref(value);value = inner_value;}+ StringView namespace_ { read->namespace_, strlen(read->namespace_) };StringView key { read->key, strlen(read->key) };- if (!s_portal_settings_changed.contains(ByteString { key }))- SystemAppearance::the().apply_portal_setting(key, value);+ if (!s_portal_settings_changed.contains(portal_setting_name(namespace_, key)))+ SystemAppearance::the().apply_portal_setting(namespace_, key, value);g_variant_unref(value);g_variant_unref(reply);}+ g_free(read->namespace_);g_free(read->key);delete read;}@@ -239,36 +292,49 @@ static void did_read_portal_setting(GObject* source, GAsyncResult* result, gpoinstatic 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),+ read->is_deprecated_read ? "Read" : "ReadOne", g_variant_new("(ss)", read->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)+static void read_portal_setting(GDBusConnection* connection, StringView namespace_, StringView key){- read_portal_setting(connection, new PortalSettingRead { .key = g_strndup(key.characters_without_null_termination(), key.length()) });+ read_portal_setting(connection, new PortalSettingRead {+ .namespace_ = g_strndup(namespace_.characters_without_null_termination(), namespace_.length()),+ .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* namespace_ = nullptr;char const* key = nullptr;GVariant* value = nullptr;- g_variant_get(parameters, "(&s&sv)", nullptr, &key, &value);+ g_variant_get(parameters, "(&s&sv)", &namespace_, &key, &value);+ StringView namespace_view { namespace_, strlen(namespace_) };StringView key_view { key, strlen(key) };- s_portal_settings_changed.set(ByteString { key_view });- SystemAppearance::the().apply_portal_setting(key_view, value);+ s_portal_settings_changed.set(portal_setting_name(namespace_view, key_view));+ SystemAppearance::the().apply_portal_setting(namespace_view, key_view, value);g_variant_unref(value);}+// NB: The connection is kept for the lifetime of the process, as the subscription below needs it.+static GDBusConnection* s_session_bus;+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;+ s_session_bus = connection;+ // The signal's first argument is the namespace, and the settings we read are in several, so every change arrives,+ // and apply_portal_setting() picks out the ones we read.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);+ nullptr, G_DBUS_SIGNAL_FLAGS_NONE, portal_setting_changed, nullptr, nullptr);+ read_portal_setting(connection, APPEARANCE_NAMESPACE, COLOR_SCHEME_KEY);+ read_portal_setting(connection, APPEARANCE_NAMESPACE, ACCENT_COLOR_KEY);+ read_portal_setting(connection, APPEARANCE_NAMESPACE, REDUCED_MOTION_KEY);+ read_portal_setting(connection, GNOME_INTERFACE_NAMESPACE, ENABLE_ANIMATIONS_KEY);+ read_portal_setting(connection, KDE_GLOBALS_NAMESPACE, ANIMATION_DURATION_FACTOR_KEY);}// NB: Qt's palette does not follow the accent color of desktops such as GNOME, and Qt's color scheme reports the@@ -277,6 +343,13 @@ static void did_get_session_bus(GObject*, GAsyncResult* result, gpointer)void SystemAppearance::observe_platform_changes(){g_bus_get(G_BUS_TYPE_SESSION, nullptr, did_get_session_bus, nullptr);++ // KDE's portals report no change to the animation speed, which is set in another application. So where the portal+ // has no reduced-motion setting, the speed is read again whenever we become the active application.+ QObject::connect(qGuiApp, &QGuiApplication::applicationStateChanged, this, [](Qt::ApplicationState state) {+ if (state == Qt::ApplicationActive && s_session_bus && !s_portal_prefers_reduced_motion.has_value())+ read_portal_setting(s_session_bus, KDE_GLOBALS_NAMESPACE, ANIMATION_DURATION_FACTOR_KEY);+ });}# elsevoid SystemAppearance::observe_platform_changes()@@ -284,11 +357,22 @@ void SystemAppearance::observe_platform_changes()}# endif+# if defined(AK_OS_LINUX)+bool SystemAppearance::prefers_reduced_motion()+{+ if (s_portal_prefers_reduced_motion.has_value())+ return *s_portal_prefers_reduced_motion;+ if (s_portal_enables_animations == false)+ return true;+ return s_portal_animation_duration_factor.has_value() && *s_portal_animation_duration_factor <= 0;+}+# elsebool SystemAppearance::prefers_reduced_motion(){// FIXME: Ask the platform.return false;}+# endif#endif}diff --git a/UI/Qt/SystemAppearance.h b/UI/Qt/SystemAppearance.hindex fc9d08c2e0c..8f41f835f65 100644--- a/UI/Qt/SystemAppearance.h+++ b/UI/Qt/SystemAppearance.h@@ -30,7 +30,8 @@ public:static SystemAppearance& the();bool prefers_dark() const { return m_prefers_dark; }- // Whether the system asks for less motion. The chrome then moves nothing, though it still fades.+ // Whether the system asks for less motion. The chrome then moves nothing, though it still fades, and pages whose+ // motion preference is Auto see prefers-reduced-motion: reduce.static bool prefers_reduced_motion();void apply_interface_theme(WebView::ThemePreference);@@ -38,11 +39,13 @@ public:#if defined(AK_OS_LINUX)// Takes in a setting that the desktop's settings portal reported.- void apply_portal_setting(StringView key, GVariant* value);+ void apply_portal_setting(StringView namespace_, StringView key, GVariant* value);#endifsignals:void changed();+ // The system started or stopped asking for less motion.+ void reduced_motion_changed();// Our windows switched between the light and dark appearance, whether the system or the interface theme switched// them.void interface_appearance_changed();diff --git a/UI/Qt/SystemAppearanceMac.mm b/UI/Qt/SystemAppearanceMac.mmindex b2730b7fe51..04c268b52e3 100644--- a/UI/Qt/SystemAppearanceMac.mm+++ b/UI/Qt/SystemAppearanceMac.mm@@ -35,6 +35,17 @@ void SystemAppearance::observe_platform_changes()// The defaults are updated after the notification is posted.QMetaObject::invokeMethod(this, [this] { update(); }, Qt::QueuedConnection);}];+ // The notification comes for a change to any of the display options, such as the contrast.+ __block auto prefers_reduced_motion = SystemAppearance::prefers_reduced_motion();+ [[[NSWorkspace sharedWorkspace] notificationCenter] addObserverForName:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification+ object:nil+ queue:[NSOperationQueue mainQueue]+ usingBlock:^(NSNotification*) {+ if (SystemAppearance::prefers_reduced_motion() == prefers_reduced_motion)+ return;+ prefers_reduced_motion = !prefers_reduced_motion;+ emit reduced_motion_changed();+ }];}}