UI/AppKit: Set WC's max FPS to the max FPS of the current screen

This commit is contained in:
Luke Wilde 2025-07-25 13:23:50 +01:00 committed by Alexander Kalenik
commit b0d97c991f
Notes: github-actions[bot] 2025-07-26 14:10:35 +00:00
6 changed files with 40 additions and 6 deletions

View file

@ -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 - (void)clearHistory:(id)sender
{ {
for (TabController* controller in self.managed_tabs) { for (TabController* controller in self.managed_tabs) {
@ -783,6 +791,11 @@
return YES; return YES;
} }
- (void)applicationDidChangeScreenParameters:(NSNotification*)notification
{
[self broadcastDisplayRefreshRateChange];
}
- (BOOL)validateMenuItem:(NSMenuItem*)item - (BOOL)validateMenuItem:(NSMenuItem*)item
{ {
if ([item action] == @selector(setAutoPreferredColorScheme:)) { if ([item action] == @selector(setAutoPreferredColorScheme:)) {

View file

@ -70,6 +70,7 @@
- (void)handleResize; - (void)handleResize;
- (void)handleDevicePixelRatioChange; - (void)handleDevicePixelRatioChange;
- (void)handleDisplayRefreshRateChange;
- (void)handleVisibility:(BOOL)is_visible; - (void)handleVisibility:(BOOL)is_visible;
- (void)setPreferredColorScheme:(Web::CSS::PreferredColorScheme)color_scheme; - (void)setPreferredColorScheme:(Web::CSS::PreferredColorScheme)color_scheme;

View file

@ -131,8 +131,9 @@ struct HideCursor {
// This returns device pixel ratio of the screen the window is opened in // This returns device pixel ratio of the screen the window is opened in
auto device_pixel_ratio = [[NSScreen mainScreen] backingScaleFactor]; 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]; [self setWebViewCallbacks];
auto* area = [[NSTrackingArea alloc] initWithRect:[self bounds] auto* area = [[NSTrackingArea alloc] initWithRect:[self bounds]
@ -212,6 +213,11 @@ struct HideCursor {
[self updateStatusLabelPosition]; [self updateStatusLabelPosition];
} }
- (void)handleDisplayRefreshRateChange
{
m_web_view_bridge->set_maximum_frames_per_second([[[self window] screen] maximumFramesPerSecond]);
}
- (void)handleVisibility:(BOOL)is_visible - (void)handleVisibility:(BOOL)is_visible
{ {
m_web_view_bridge->set_system_visibility_state(is_visible m_web_view_bridge->set_system_visibility_state(is_visible

View file

@ -20,18 +20,19 @@ static T scale_for_device(T size, float device_pixel_ratio)
return size.template to_type<float>().scaled(device_pixel_ratio).template to_type<int>(); return size.template to_type<float>().scaled(device_pixel_ratio).template to_type<int>();
} }
ErrorOr<NonnullOwnPtr<WebViewBridge>> WebViewBridge::create(Vector<Web::DevicePixelRect> screen_rects, float device_pixel_ratio, Web::CSS::PreferredColorScheme preferred_color_scheme, Web::CSS::PreferredContrast preferred_contrast, Web::CSS::PreferredMotion preferred_motion) ErrorOr<NonnullOwnPtr<WebViewBridge>> WebViewBridge::create(Vector<Web::DevicePixelRect> 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<Web::DevicePixelRect> 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<Web::DevicePixelRect> 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_screen_rects(move(screen_rects))
, m_preferred_color_scheme(preferred_color_scheme) , m_preferred_color_scheme(preferred_color_scheme)
, m_preferred_contrast(preferred_contrast) , m_preferred_contrast(preferred_contrast)
, m_preferred_motion(preferred_motion) , m_preferred_motion(preferred_motion)
{ {
m_device_pixel_ratio = device_pixel_ratio; m_device_pixel_ratio = device_pixel_ratio;
m_maximum_frames_per_second = static_cast<double>(maximum_frames_per_second);
} }
WebViewBridge::~WebViewBridge() = default; WebViewBridge::~WebViewBridge() = default;
@ -50,6 +51,12 @@ void WebViewBridge::set_viewport_rect(Gfx::IntRect viewport_rect)
handle_resize(); handle_resize();
} }
void WebViewBridge::set_maximum_frames_per_second(u64 maximum_frames_per_second)
{
m_maximum_frames_per_second = static_cast<double>(maximum_frames_per_second);
client().async_set_maximum_frames_per_second(m_client_state.page_index, maximum_frames_per_second);
}
void WebViewBridge::update_palette() void WebViewBridge::update_palette()
{ {
auto theme = create_system_palette(); auto theme = create_system_palette();

View file

@ -20,7 +20,7 @@ namespace Ladybird {
class WebViewBridge final : public WebView::ViewImplementation { class WebViewBridge final : public WebView::ViewImplementation {
public: public:
static ErrorOr<NonnullOwnPtr<WebViewBridge>> create(Vector<Web::DevicePixelRect> screen_rects, float device_pixel_ratio, Web::CSS::PreferredColorScheme, Web::CSS::PreferredContrast, Web::CSS::PreferredMotion); static ErrorOr<NonnullOwnPtr<WebViewBridge>> create(Vector<Web::DevicePixelRect> screen_rects, float device_pixel_ratio, u64 maximum_frames_per_second, Web::CSS::PreferredColorScheme, Web::CSS::PreferredContrast, Web::CSS::PreferredMotion);
virtual ~WebViewBridge() override; virtual ~WebViewBridge() override;
virtual void initialize_client(CreateNewClient = CreateNewClient::Yes) override; virtual void initialize_client(CreateNewClient = CreateNewClient::Yes) override;
@ -32,6 +32,8 @@ public:
void set_viewport_rect(Gfx::IntRect); void set_viewport_rect(Gfx::IntRect);
void set_maximum_frames_per_second(u64 maximum_frames_per_second);
void update_palette(); void update_palette();
void set_preferred_color_scheme(Web::CSS::PreferredColorScheme); void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);
void set_preferred_contrast(Web::CSS::PreferredContrast); void set_preferred_contrast(Web::CSS::PreferredContrast);
@ -50,7 +52,7 @@ public:
Function<void()> on_zoom_level_changed; Function<void()> on_zoom_level_changed;
private: private:
WebViewBridge(Vector<Web::DevicePixelRect> screen_rects, float device_pixel_ratio, Web::CSS::PreferredColorScheme, Web::CSS::PreferredContrast, Web::CSS::PreferredMotion); WebViewBridge(Vector<Web::DevicePixelRect> 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 void update_zoom() override;
virtual Web::DevicePixelSize viewport_size() const override; virtual Web::DevicePixelSize viewport_size() const override;

View file

@ -660,6 +660,11 @@ static NSString* const TOOLBAR_TAB_OVERVIEW_IDENTIFIER = @"ToolbarTabOverviewIde
[[[self tab] web_view] handleDevicePixelRatioChange]; [[[self tab] web_view] handleDevicePixelRatioChange];
} }
- (void)windowDidChangeScreen:(NSNotification*)notification
{
[[[self tab] web_view] handleDisplayRefreshRateChange];
}
- (BOOL)validateMenuItem:(NSMenuItem*)item - (BOOL)validateMenuItem:(NSMenuItem*)item
{ {
if ([item action] == @selector(toggleLineBoxBorders:)) { if ([item action] == @selector(toggleLineBoxBorders:)) {