Skip to content

Truly independent web browser

LibWebView+Compositor+UI/Qt: Obey the macOS scroll bar click setting

Andreas Kling9fea776ce69b
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 0cf1ac26346..36bcbc45855 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -1789,6 +1789,7 @@ ErrorOr<void> Application::launch_compositor_process()
auto transport = TRY(presentation.take_handle().create_transport());
m_compositor_presentation_client = adopt_ref(*new CompositorPresentationClient(move(transport)));
m_compositor_presentation_id = presentation.presentation_id();
+ m_compositor_presentation_client->async_set_scrollbar_track_press_jumps(platform_scrollbar_track_press_jumps());
m_compositor_presentation_client->on_death = [this, presentation_client = m_compositor_presentation_client->make_weak_ptr<CompositorPresentationClient>()] {
// NB: This runs on a later turn, by which time a new Compositor can have replaced the one this belonged to.
if (!presentation_client || presentation_client.ptr() != m_compositor_presentation_client.ptr())
@@ -1826,6 +1827,12 @@ void Application::when_compositor_gpu_device_identity_is_known(CompositorGpuDevi
m_callbacks_awaiting_compositor_gpu_device_identity.append(move(callback));
}
+void Application::platform_scrollbar_track_press_behavior_changed()
+{
+ if (m_compositor_presentation_client)
+ m_compositor_presentation_client->async_set_scrollbar_track_press_jumps(platform_scrollbar_track_press_jumps());
+}
+
void Application::notify_compositor_gpu_presentation_unavailable()
{
if (m_reported_compositor_gpu_presentation_unavailable)
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index f95e5fc98bb..3d0442f0a6b 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -133,6 +133,10 @@ public:
// Returns true if a touchpad may pull a scroller past the edges of its scroll range, from where it springs back.
// This needs the platform to say when the fingers lift from the touchpad, or the scroller would stay stretched.
virtual bool platform_scrolls_elastically() const { return false; }
+ // Returns true if a press on a scrollbar track beside the thumb jumps to that spot, rather than paging toward it.
+ 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();
// The languages the user prefers, as the platform lists them (BCP 47 tags, most preferred first).
virtual Vector<String> platform_preferred_languages() const { return {}; }
diff --git a/Services/Compositor/CompositorPresentationServer.ipc b/Services/Compositor/CompositorPresentationServer.ipc
index 0b208e2e64e..51e763324e7 100644
--- a/Services/Compositor/CompositorPresentationServer.ipc
+++ b/Services/Compositor/CompositorPresentationServer.ipc
@@ -8,4 +8,5 @@ endpoint CompositorPresentationServer
handle_and_dispatch_mouse_event(Web::CompositorContextId context_id, Web::MouseEvent event) =|
presented_bitmap_ready_to_paint(Web::CompositorContextId context_id, i32 bitmap_id) =|
set_client_gpu_presentation_capability(bool supported, u64 adapter_luid) =|
+ set_scrollbar_track_press_jumps(bool jumps) =|
}
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index d2abc74b365..ab5e8fe71fc 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -1463,15 +1463,22 @@ BackingStoreManager::GpuSharing CompositorState::gpu_sharing_for_context([[maybe
return BackingStoreManager::GpuSharing::Allowed;
}
-// A context scrolls elastically if the presentation of its root context does.
+// A context scrolls elastically, and takes presses on scrollbar tracks, as the presentation of its root context does.
void CompositorState::update_scroll_capabilities()
{
for (auto& [context_id, context] : m_contexts) {
auto* presentation = presentation_for_context(root_context_of(*context)->context_id());
context->set_scrolls_elastically(presentation && m_presentations.get(presentation)->scrolls_elastically);
+ context->set_scrollbar_track_press_jumps(presentation && m_presentations.get(presentation)->scrollbar_track_press_jumps);
}
}
+void CompositorState::set_scrollbar_track_press_jumps(CompositorStateClient& presentation, bool jumps)
+{
+ m_presentations.get(&presentation)->scrollbar_track_press_jumps = jumps;
+ update_scroll_capabilities();
+}
+
String CompositorState::scroll_state_for_testing(Web::CompositorContextId context_id, Web::AsyncScrollNodeStableID stretched_scroller) const
{
auto const* context = context_if_present(context_id);
diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.h
index 64c5cb6d380..3f9e7e4734b 100644
--- a/Services/Compositor/CompositorState.h
+++ b/Services/Compositor/CompositorState.h
@@ -88,6 +88,8 @@ public:
struct PresentationCapabilities {
bool synthesizes_scroll_momentum { false };
bool scrolls_elastically { false };
+ // Whether a press on a scrollbar track beside the thumb jumps there rather than paging toward it.
+ bool scrollbar_track_press_jumps { false };
// LUID of the GPU adapter the presentation can present shared GPU textures on, if any.
Optional<u64> gpu_presentation_adapter_luid;
};
@@ -141,6 +143,7 @@ public:
bool request_screenshot(Web::CompositorContextId, Gfx::ShareableBitmap&);
void presented_bitmap_ready_to_paint(Web::CompositorContextId, i32 bitmap_id);
void set_client_gpu_presentation_capability(CompositorStateClient&, bool supported, u64 adapter_luid);
+ void set_scrollbar_track_press_jumps(CompositorStateClient&, bool jumps);
String scroll_state_for_testing(Web::CompositorContextId, Web::AsyncScrollNodeStableID stretched_scroller) const;
Optional<Compositing::FirstWheelStepRouteForTesting> route_first_wheel_step_for_testing(Web::CompositorContextId, Gfx::FloatPoint position, Gfx::FloatPoint delta_in_css_pixels);
void begin_input_trace_for_testing(Web::CompositorContextId);
diff --git a/Services/Compositor/ConnectionFromPresentation.cpp b/Services/Compositor/ConnectionFromPresentation.cpp
index ae416959b3d..1fbcac04831 100644
--- a/Services/Compositor/ConnectionFromPresentation.cpp
+++ b/Services/Compositor/ConnectionFromPresentation.cpp
@@ -174,4 +174,9 @@ void ConnectionFromPresentation::set_client_gpu_presentation_capability(bool sup
m_compositor_state->set_client_gpu_presentation_capability(*this, supported, adapter_luid);
}
+void ConnectionFromPresentation::set_scrollbar_track_press_jumps(bool jumps)
+{
+ m_compositor_state->set_scrollbar_track_press_jumps(*this, jumps);
+}
+
}
diff --git a/Services/Compositor/ConnectionFromPresentation.h b/Services/Compositor/ConnectionFromPresentation.h
index 1a12c2c2b90..18f663eff64 100644
--- a/Services/Compositor/ConnectionFromPresentation.h
+++ b/Services/Compositor/ConnectionFromPresentation.h
@@ -43,6 +43,7 @@ private:
virtual void handle_and_dispatch_mouse_event(Web::CompositorContextId, Web::MouseEvent) override;
virtual void presented_bitmap_ready_to_paint(Web::CompositorContextId, i32 bitmap_id) override;
virtual void set_client_gpu_presentation_capability(bool supported, u64 adapter_luid) override;
+ virtual void set_scrollbar_track_press_jumps(bool jumps) override;
bool is_bound_to(Web::CompositorContextId) const;
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index dd1709fcca3..537a27bf32e 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -28,11 +28,12 @@ namespace Compositor {
static constexpr auto scrollbar_paging_initial_delay = AK::Duration::from_milliseconds(250);
static constexpr auto scrollbar_paging_repeat_interval = AK::Duration::from_milliseconds(50);
-// INTEROP: The modifier that makes a press beside the thumb jump there instead of paging, as in Chromium and Firefox.
+// INTEROP: The modifier that makes a press beside the thumb page instead of jumping there or the other way around, as in
+// Chromium and Firefox.
#if defined(AK_OS_MACOS)
-static constexpr auto scrollbar_jump_modifier = Web::UIEvents::Mod_Alt;
+static constexpr auto scrollbar_track_press_inverting_modifier = Web::UIEvents::Mod_Alt;
#else
-static constexpr auto scrollbar_jump_modifier = Web::UIEvents::Mod_Shift;
+static constexpr auto scrollbar_track_press_inverting_modifier = Web::UIEvents::Mod_Shift;
#endif
static Web::AsyncScrollNodeStableID viewport_stable_id_from(Compositing::AsyncScrollNodeID node_id)
@@ -425,8 +426,10 @@ ContextState::ContextUpdateResult ContextState::handle_mouse_event(Web::MouseEve
if (!result.scrollbar_dragged_by_compositor.has_value())
result.frame_to_present = frame_repainting_scrollbars_painted_by_compositor();
- // A press beside the thumb pages toward the pointer, unless the modifier that jumps there instead is held.
- if (drag->is_press_beside_thumb && !(event.modifiers & scrollbar_jump_modifier)) {
+ // A press beside the thumb pages toward the pointer or jumps there, as the platform says, and a modifier swaps
+ // the two.
+ auto press_jumps = m_scrollbar_track_press_jumps != static_cast<bool>(event.modifiers & scrollbar_track_press_inverting_modifier);
+ if (drag->is_press_beside_thumb && !press_jumps) {
auto const& scrollbar = m_scrollbar_controller.scrollbars()[drag->scrollbar_index];
auto scroll_offset = m_async_scroll_tree.scroll_offset_for_node(scrollbar.scroll_node_id, m_scroll_state_snapshot);
VERIFY(scroll_offset.has_value());
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index 977c5d943b2..62b35264a8d 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -208,6 +208,7 @@ public:
// Ends the fling before a wheel event from a new gesture or other input.
Optional<Web::MouseEvent> interrupt_scroll_fling(Web::MouseEvent const&, Optional<MonotonicTime> now_for_testing = {});
void set_scrolls_elastically(bool);
+ void set_scrollbar_track_press_jumps(bool jumps) { m_scrollbar_track_press_jumps = jumps; }
Optional<Gfx::IntRect> advance_elastic_overscroll_rebounds(MonotonicTime frame_time);
Optional<PendingFrame> advance_scrollbar_fades(MonotonicTime frame_time);
// The installed scrolling state, its routing admission, and how far the given scroller is stretched, as JSON.
@@ -367,6 +368,7 @@ private:
float primary_position { 0 };
};
Optional<ScrollbarPaging> m_scrollbar_paging;
+ bool m_scrollbar_track_press_jumps { false };
RefPtr<Core::Timer> m_scrollbar_paging_timer;
ScrollFlingController m_scroll_fling_controller;
ElasticOverscrollController m_elastic_overscroll_controller;
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 7e2c8ef9220..5f525f0697f 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -24,6 +24,7 @@
#if defined(AK_OS_MACOS)
# include <UI/Qt/MacWindow.h>
+# include <UI/Qt/ScrollbarPreferencesMac.h>
#endif
#include <QAction>
@@ -585,6 +586,11 @@ Core::EventLoop& Application::create_platform_event_loop()
if (!browser_options().headless_mode.has_value()) {
#if defined(AK_OS_LINUX)
Ladybird::initialize_external_url_handler(event_loop);
+#endif
+#if defined(AK_OS_MACOS)
+ Ladybird::on_scrollbar_track_press_behavior_change([this] {
+ platform_scrollbar_track_press_behavior_changed();
+ });
#endif
static_cast<EventLoopImplementationQt&>(event_loop.impl()).set_main_loop();
}
@@ -615,6 +621,17 @@ bool Application::platform_scrolls_elastically() const
return QGuiApplication::platformName() == QStringLiteral("cocoa");
}
+bool Application::platform_scrollbar_track_press_jumps() const
+{
+#if defined(AK_OS_MACOS)
+ if (!m_application)
+ return false;
+ return scrollbar_track_press_jumps();
+#else
+ return false;
+#endif
+}
+
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.h
index 29ab8a57bb8..f9385e0e322 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -173,6 +173,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 Vector<String> platform_preferred_languages() const override;
virtual void update_tabs_display() const override;
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index 0e3c42aa0f1..0f8f2d023ff 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -93,6 +93,7 @@ if (APPLE)
MacWindowFocus.mm
NativeTextEditingBindings.mm
Menu.mm
+ ScrollbarPreferencesMac.mm
SpellCheckerMac.mm
WebContentViewAccessibility.mm
WebContentViewMac.mm
diff --git a/UI/Qt/ScrollbarPreferencesMac.h b/UI/Qt/ScrollbarPreferencesMac.h
new file mode 100644
index 00000000000..f12db301594
--- /dev/null
+++ b/UI/Qt/ScrollbarPreferencesMac.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Function.h>
+
+namespace Ladybird {
+
+// Whether a press on a scrollbar track beside the thumb jumps to that spot rather than paging toward it, as the "Click
+// in the scroll bar to" setting of the system says.
+bool scrollbar_track_press_jumps();
+
+// Calls the given function whenever that setting may have changed.
+void on_scrollbar_track_press_behavior_change(Function<void()>);
+
+}
diff --git a/UI/Qt/ScrollbarPreferencesMac.mm b/UI/Qt/ScrollbarPreferencesMac.mm
new file mode 100644
index 00000000000..6a5fa0b63fe
--- /dev/null
+++ b/UI/Qt/ScrollbarPreferencesMac.mm
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#import <Cocoa/Cocoa.h>
+#import <UI/Qt/ScrollbarPreferencesMac.h>
+
+namespace Ladybird {
+
+bool scrollbar_track_press_jumps()
+{
+ return [[NSUserDefaults standardUserDefaults] boolForKey:@"AppleScrollerPagingBehavior"];
+}
+
+void on_scrollbar_track_press_behavior_change(Function<void()> callback)
+{
+ // NB: The system announces a change of this setting with the notification that Chromium also watches for it.
+ static Function<void()> s_callback;
+ auto should_observe = !s_callback;
+ s_callback = move(callback);
+ if (!should_observe)
+ return;
+ [NSDistributedNotificationCenter.defaultCenter addObserverForName:@"AppleNoRedisplayAppearancePreferenceChanged"
+ object:nil
+ queue:NSOperationQueue.mainQueue
+ usingBlock:^(NSNotification*) {
+ if (s_callback)
+ s_callback();
+ }];
+}
+
+}