1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:47:35 +00:00

Ladybird: Add a menu item to the AppKit chrome to choose a color scheme

This lets the user choose a color scheme which differs from the active
system theme. Upon changing the color scheme, the scheme is broadcast to
all active tabs, and will be used in new tabs.
This commit is contained in:
Timothy Flynn 2023-08-26 23:44:52 -04:00 committed by Tim Flynn
parent 3fc0c21b6c
commit 6e3177e2fc
6 changed files with 99 additions and 6 deletions

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/Forward.h>
#include <LibWeb/CSS/PreferredColorScheme.h>
#import <System/Cocoa.h>
@ -18,4 +19,6 @@
- (void)handleScroll;
- (void)handleVisibility:(BOOL)is_visible;
- (void)setPreferredColorScheme:(Web::CSS::PreferredColorScheme)color_scheme;
@end

View file

@ -85,7 +85,7 @@ struct HideCursor {
auto device_pixel_ratio = [[NSScreen mainScreen] backingScaleFactor];
m_web_view_bridge = MUST(Ladybird::WebViewBridge::create(move(screen_rects), device_pixel_ratio, [delegate webdriverContentIPCPath]));
m_web_view_bridge = MUST(Ladybird::WebViewBridge::create(move(screen_rects), device_pixel_ratio, [delegate webdriverContentIPCPath], [delegate preferredColorScheme]));
[self setWebViewCallbacks];
auto* area = [[NSTrackingArea alloc] initWithRect:[self bounds]
@ -122,6 +122,11 @@ struct HideCursor {
m_web_view_bridge->set_system_visibility_state(is_visible);
}
- (void)setPreferredColorScheme:(Web::CSS::PreferredColorScheme)color_scheme
{
m_web_view_bridge->set_preferred_color_scheme(color_scheme);
}
#pragma mark - Private methods
- (void)updateViewportRect:(Ladybird::WebViewBridge::ForResize)for_resize

View file

@ -23,14 +23,15 @@ 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>();
}
ErrorOr<NonnullOwnPtr<WebViewBridge>> WebViewBridge::create(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path)
ErrorOr<NonnullOwnPtr<WebViewBridge>> WebViewBridge::create(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme preferred_color_scheme)
{
return adopt_nonnull_own_or_enomem(new (nothrow) WebViewBridge(move(screen_rects), device_pixel_ratio, move(webdriver_content_ipc_path)));
return adopt_nonnull_own_or_enomem(new (nothrow) WebViewBridge(move(screen_rects), device_pixel_ratio, move(webdriver_content_ipc_path), preferred_color_scheme));
}
WebViewBridge::WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path)
WebViewBridge::WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme preferred_color_scheme)
: m_screen_rects(move(screen_rects))
, m_webdriver_content_ipc_path(move(webdriver_content_ipc_path))
, m_preferred_color_scheme(preferred_color_scheme)
{
m_device_pixel_ratio = device_pixel_ratio;
m_inverse_device_pixel_ratio = 1.0 / device_pixel_ratio;
@ -93,6 +94,12 @@ void WebViewBridge::update_palette()
client().async_update_system_theme(move(theme));
}
void WebViewBridge::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
{
m_preferred_color_scheme = color_scheme;
client().async_set_preferred_color_scheme(color_scheme);
}
void WebViewBridge::mouse_down_event(Gfx::IntPoint position, GUI::MouseButton button, KeyModifier modifiers)
{
client().async_mouse_down(to_content_position(position), to_underlying(button), to_underlying(button), modifiers);
@ -179,6 +186,7 @@ void WebViewBridge::create_client(WebView::EnableCallgrindProfiling enable_callg
client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio);
client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
client().async_set_preferred_color_scheme(m_preferred_color_scheme);
update_palette();
if (!m_screen_rects.is_empty()) {

View file

@ -11,6 +11,7 @@
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
#include <LibGfx/StandardCursor.h>
#include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWebView/ViewImplementation.h>
// FIXME: These should not be included outside of Serenity.
@ -21,7 +22,7 @@ namespace Ladybird {
class WebViewBridge final : public WebView::ViewImplementation {
public:
static ErrorOr<NonnullOwnPtr<WebViewBridge>> create(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path);
static ErrorOr<NonnullOwnPtr<WebViewBridge>> create(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme);
virtual ~WebViewBridge() override;
float device_pixel_ratio() const { return m_device_pixel_ratio; }
@ -36,6 +37,7 @@ public:
void set_viewport_rect(Gfx::IntRect, ForResize = ForResize::No);
void update_palette();
void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);
void mouse_down_event(Gfx::IntPoint, GUI::MouseButton, KeyModifier);
void mouse_up_event(Gfx::IntPoint, GUI::MouseButton, KeyModifier);
@ -52,7 +54,7 @@ public:
Optional<Paintable> paintable();
private:
WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path);
WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme);
virtual void update_zoom() override;
virtual Gfx::IntRect viewport_rect() const override;
@ -67,6 +69,7 @@ private:
float m_inverse_device_pixel_ratio { 1.0 };
Optional<StringView> m_webdriver_content_ipc_path;
Web::CSS::PreferredColorScheme m_preferred_color_scheme { Web::CSS::PreferredColorScheme::Auto };
};
}