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
Notes: sideshowbarker 2024-07-18 04:38:32 +09:00
6 changed files with 99 additions and 6 deletions

View file

@ -10,6 +10,7 @@
#include <AK/StringView.h>
#include <AK/URL.h>
#include <Browser/CookieJar.h>
#include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWeb/HTML/ActivateTab.h>
#import <System/Cocoa.h>
@ -31,5 +32,6 @@
- (Browser::CookieJar&)cookieJar;
- (Optional<StringView> const&)webdriverContentIPCPath;
- (Web::CSS::PreferredColorScheme)preferredColorScheme;
@end

View file

@ -7,6 +7,7 @@
#include <BrowserSettings/Defaults.h>
#import <Application/ApplicationDelegate.h>
#import <UI/LadybirdWebView.h>
#import <UI/Tab.h>
#import <UI/TabController.h>
#import <Utilities/URL.h>
@ -24,6 +25,8 @@
Optional<Browser::CookieJar> m_cookie_jar;
Optional<StringView> m_webdriver_content_ipc_path;
Web::CSS::PreferredColorScheme m_preferred_color_scheme;
}
@property (nonatomic, strong) NSMutableArray<TabController*>* managed_tabs;
@ -68,6 +71,8 @@
m_webdriver_content_ipc_path = webdriver_content_ipc_path;
}
m_preferred_color_scheme = Web::CSS::PreferredColorScheme::Auto;
// Reduce the tooltip delay, as the default delay feels quite long.
[[NSUserDefaults standardUserDefaults] setObject:@100 forKey:@"NSInitialToolTipDelay"];
}
@ -119,6 +124,11 @@
return m_webdriver_content_ipc_path;
}
- (Web::CSS::PreferredColorScheme)preferredColorScheme
{
return m_preferred_color_scheme;
}
#pragma mark - Private methods
- (void)closeCurrentTab:(id)sender
@ -134,6 +144,32 @@
[controller focusLocationToolbarItem];
}
- (void)setAutoPreferredColorScheme:(id)sender
{
m_preferred_color_scheme = Web::CSS::PreferredColorScheme::Auto;
[self broadcastPreferredColorSchemeUpdate];
}
- (void)setDarkPreferredColorScheme:(id)sender
{
m_preferred_color_scheme = Web::CSS::PreferredColorScheme::Dark;
[self broadcastPreferredColorSchemeUpdate];
}
- (void)setLightPreferredColorScheme:(id)sender
{
m_preferred_color_scheme = Web::CSS::PreferredColorScheme::Light;
[self broadcastPreferredColorSchemeUpdate];
}
- (void)broadcastPreferredColorSchemeUpdate
{
for (TabController* controller in self.managed_tabs) {
auto* tab = (Tab*)[controller window];
[[tab web_view] setPreferredColorScheme:m_preferred_color_scheme];
}
}
- (void)clearHistory:(id)sender
{
for (TabController* controller in self.managed_tabs) {
@ -229,6 +265,25 @@
auto* menu = [[NSMenuItem alloc] init];
auto* submenu = [[NSMenu alloc] initWithTitle:@"View"];
auto* color_scheme_menu = [[NSMenu alloc] init];
[color_scheme_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Auto"
action:@selector(setAutoPreferredColorScheme:)
keyEquivalent:@""]];
[color_scheme_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Dark"
action:@selector(setDarkPreferredColorScheme:)
keyEquivalent:@""]];
[color_scheme_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Light"
action:@selector(setLightPreferredColorScheme:)
keyEquivalent:@""]];
auto* color_scheme_menu_item = [[NSMenuItem alloc] initWithTitle:@"Color Scheme"
action:nil
keyEquivalent:@""];
[color_scheme_menu_item setSubmenu:color_scheme_menu];
[submenu addItem:color_scheme_menu_item];
[submenu addItem:[NSMenuItem separatorItem]];
[menu setSubmenu:submenu];
return menu;
}
@ -310,4 +365,21 @@
return YES;
}
- (BOOL)validateMenuItem:(NSMenuItem*)item
{
using enum Web::CSS::PreferredColorScheme;
if ([item action] == @selector(setAutoPreferredColorScheme:)) {
[item setState:(m_preferred_color_scheme == Auto) ? NSControlStateValueOn : NSControlStateValueOff];
}
if ([item action] == @selector(setDarkPreferredColorScheme:)) {
[item setState:(m_preferred_color_scheme == Dark) ? NSControlStateValueOn : NSControlStateValueOff];
}
if ([item action] == @selector(setLightPreferredColorScheme:)) {
[item setState:(m_preferred_color_scheme == Light) ? NSControlStateValueOn : NSControlStateValueOff];
}
return YES;
}
@end

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 };
};
}