From b0d97c991ffec445ee7fa9909ee7d9906bd444f5 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Fri, 25 Jul 2025 13:23:50 +0100 Subject: [PATCH] UI/AppKit: Set WC's max FPS to the max FPS of the current screen --- UI/AppKit/Application/ApplicationDelegate.mm | 13 +++++++++++++ UI/AppKit/Interface/LadybirdWebView.h | 1 + UI/AppKit/Interface/LadybirdWebView.mm | 8 +++++++- UI/AppKit/Interface/LadybirdWebViewBridge.cpp | 13 ++++++++++--- UI/AppKit/Interface/LadybirdWebViewBridge.h | 6 ++++-- UI/AppKit/Interface/TabController.mm | 5 +++++ 6 files changed, 40 insertions(+), 6 deletions(-) diff --git a/UI/AppKit/Application/ApplicationDelegate.mm b/UI/AppKit/Application/ApplicationDelegate.mm index d6b2e4163ac..79937ddb77a 100644 --- a/UI/AppKit/Application/ApplicationDelegate.mm +++ b/UI/AppKit/Application/ApplicationDelegate.mm @@ -379,6 +379,14 @@ } } +- (void)broadcastDisplayRefreshRateChange +{ + for (TabController* controller in self.managed_tabs) { + auto* tab = (Tab*)[controller window]; + [[tab web_view] handleDisplayRefreshRateChange]; + } +} + - (void)clearHistory:(id)sender { for (TabController* controller in self.managed_tabs) { @@ -783,6 +791,11 @@ return YES; } +- (void)applicationDidChangeScreenParameters:(NSNotification*)notification +{ + [self broadcastDisplayRefreshRateChange]; +} + - (BOOL)validateMenuItem:(NSMenuItem*)item { if ([item action] == @selector(setAutoPreferredColorScheme:)) { diff --git a/UI/AppKit/Interface/LadybirdWebView.h b/UI/AppKit/Interface/LadybirdWebView.h index aef3553bcd8..75069d89d67 100644 --- a/UI/AppKit/Interface/LadybirdWebView.h +++ b/UI/AppKit/Interface/LadybirdWebView.h @@ -70,6 +70,7 @@ - (void)handleResize; - (void)handleDevicePixelRatioChange; +- (void)handleDisplayRefreshRateChange; - (void)handleVisibility:(BOOL)is_visible; - (void)setPreferredColorScheme:(Web::CSS::PreferredColorScheme)color_scheme; diff --git a/UI/AppKit/Interface/LadybirdWebView.mm b/UI/AppKit/Interface/LadybirdWebView.mm index 139e64fb896..ec441f07d6d 100644 --- a/UI/AppKit/Interface/LadybirdWebView.mm +++ b/UI/AppKit/Interface/LadybirdWebView.mm @@ -131,8 +131,9 @@ struct HideCursor { // This returns device pixel ratio of the screen the window is opened in auto device_pixel_ratio = [[NSScreen mainScreen] backingScaleFactor]; + auto maximum_frames_per_second = [[NSScreen mainScreen] maximumFramesPerSecond]; - m_web_view_bridge = MUST(Ladybird::WebViewBridge::create(move(screen_rects), device_pixel_ratio, [delegate preferredColorScheme], [delegate preferredContrast], [delegate preferredMotion])); + m_web_view_bridge = MUST(Ladybird::WebViewBridge::create(move(screen_rects), device_pixel_ratio, maximum_frames_per_second, [delegate preferredColorScheme], [delegate preferredContrast], [delegate preferredMotion])); [self setWebViewCallbacks]; auto* area = [[NSTrackingArea alloc] initWithRect:[self bounds] @@ -212,6 +213,11 @@ struct HideCursor { [self updateStatusLabelPosition]; } +- (void)handleDisplayRefreshRateChange +{ + m_web_view_bridge->set_maximum_frames_per_second([[[self window] screen] maximumFramesPerSecond]); +} + - (void)handleVisibility:(BOOL)is_visible { m_web_view_bridge->set_system_visibility_state(is_visible diff --git a/UI/AppKit/Interface/LadybirdWebViewBridge.cpp b/UI/AppKit/Interface/LadybirdWebViewBridge.cpp index 515414f8342..9bbdfb32182 100644 --- a/UI/AppKit/Interface/LadybirdWebViewBridge.cpp +++ b/UI/AppKit/Interface/LadybirdWebViewBridge.cpp @@ -20,18 +20,19 @@ static T scale_for_device(T size, float device_pixel_ratio) return size.template to_type().scaled(device_pixel_ratio).template to_type(); } -ErrorOr> WebViewBridge::create(Vector screen_rects, float device_pixel_ratio, Web::CSS::PreferredColorScheme preferred_color_scheme, Web::CSS::PreferredContrast preferred_contrast, Web::CSS::PreferredMotion preferred_motion) +ErrorOr> WebViewBridge::create(Vector screen_rects, float device_pixel_ratio, u64 maximum_frames_per_second, Web::CSS::PreferredColorScheme preferred_color_scheme, Web::CSS::PreferredContrast preferred_contrast, Web::CSS::PreferredMotion preferred_motion) { - return adopt_nonnull_own_or_enomem(new (nothrow) WebViewBridge(move(screen_rects), device_pixel_ratio, preferred_color_scheme, preferred_contrast, preferred_motion)); + return adopt_nonnull_own_or_enomem(new (nothrow) WebViewBridge(move(screen_rects), device_pixel_ratio, maximum_frames_per_second, preferred_color_scheme, preferred_contrast, preferred_motion)); } -WebViewBridge::WebViewBridge(Vector screen_rects, float device_pixel_ratio, Web::CSS::PreferredColorScheme preferred_color_scheme, Web::CSS::PreferredContrast preferred_contrast, Web::CSS::PreferredMotion preferred_motion) +WebViewBridge::WebViewBridge(Vector screen_rects, float device_pixel_ratio, u64 maximum_frames_per_second, Web::CSS::PreferredColorScheme preferred_color_scheme, Web::CSS::PreferredContrast preferred_contrast, Web::CSS::PreferredMotion preferred_motion) : m_screen_rects(move(screen_rects)) , m_preferred_color_scheme(preferred_color_scheme) , m_preferred_contrast(preferred_contrast) , m_preferred_motion(preferred_motion) { m_device_pixel_ratio = device_pixel_ratio; + m_maximum_frames_per_second = static_cast(maximum_frames_per_second); } WebViewBridge::~WebViewBridge() = default; @@ -50,6 +51,12 @@ void WebViewBridge::set_viewport_rect(Gfx::IntRect viewport_rect) handle_resize(); } +void WebViewBridge::set_maximum_frames_per_second(u64 maximum_frames_per_second) +{ + m_maximum_frames_per_second = static_cast(maximum_frames_per_second); + client().async_set_maximum_frames_per_second(m_client_state.page_index, maximum_frames_per_second); +} + void WebViewBridge::update_palette() { auto theme = create_system_palette(); diff --git a/UI/AppKit/Interface/LadybirdWebViewBridge.h b/UI/AppKit/Interface/LadybirdWebViewBridge.h index dd27d961104..e8623ac2a1e 100644 --- a/UI/AppKit/Interface/LadybirdWebViewBridge.h +++ b/UI/AppKit/Interface/LadybirdWebViewBridge.h @@ -20,7 +20,7 @@ namespace Ladybird { class WebViewBridge final : public WebView::ViewImplementation { public: - static ErrorOr> create(Vector screen_rects, float device_pixel_ratio, Web::CSS::PreferredColorScheme, Web::CSS::PreferredContrast, Web::CSS::PreferredMotion); + static ErrorOr> create(Vector screen_rects, float device_pixel_ratio, u64 maximum_frames_per_second, Web::CSS::PreferredColorScheme, Web::CSS::PreferredContrast, Web::CSS::PreferredMotion); virtual ~WebViewBridge() override; virtual void initialize_client(CreateNewClient = CreateNewClient::Yes) override; @@ -32,6 +32,8 @@ public: void set_viewport_rect(Gfx::IntRect); + void set_maximum_frames_per_second(u64 maximum_frames_per_second); + void update_palette(); void set_preferred_color_scheme(Web::CSS::PreferredColorScheme); void set_preferred_contrast(Web::CSS::PreferredContrast); @@ -50,7 +52,7 @@ public: Function on_zoom_level_changed; private: - WebViewBridge(Vector screen_rects, float device_pixel_ratio, Web::CSS::PreferredColorScheme, Web::CSS::PreferredContrast, Web::CSS::PreferredMotion); + WebViewBridge(Vector screen_rects, float device_pixel_ratio, u64 maximum_frames_per_second, Web::CSS::PreferredColorScheme, Web::CSS::PreferredContrast, Web::CSS::PreferredMotion); virtual void update_zoom() override; virtual Web::DevicePixelSize viewport_size() const override; diff --git a/UI/AppKit/Interface/TabController.mm b/UI/AppKit/Interface/TabController.mm index 836921562cd..ac7b034bcdc 100644 --- a/UI/AppKit/Interface/TabController.mm +++ b/UI/AppKit/Interface/TabController.mm @@ -660,6 +660,11 @@ static NSString* const TOOLBAR_TAB_OVERVIEW_IDENTIFIER = @"ToolbarTabOverviewIde [[[self tab] web_view] handleDevicePixelRatioChange]; } +- (void)windowDidChangeScreen:(NSNotification*)notification +{ + [[[self tab] web_view] handleDisplayRefreshRateChange]; +} + - (BOOL)validateMenuItem:(NSMenuItem*)item { if ([item action] == @selector(toggleLineBoxBorders:)) {