UI: Share macOS web selection color

Move the active macOS web selection color into LibWebView so AppKit
and Qt use the same value when building the WebContent system palette.
Qt now also asks AppKit for inactive selection colors on macOS, matching
the AppKit frontend while leaving other Qt platforms on QPalette.
This commit is contained in:
Andreas Kling 2026-06-21 01:22:50 +02:00 committed by Andreas Kling
parent 4b2f404da1
commit 202159be18
7 changed files with 81 additions and 1 deletions

View file

@ -42,6 +42,12 @@ set(SOURCES
CompositorClient.cpp
)
if (APPLE)
list(APPEND SOURCES
PlatformColorsMacOS.cpp
)
endif()
set(GENERATED_SOURCES ${CURRENT_LIB_GENERATED})
compile_ipc(UIProcessServer.ipc UIProcessServerEndpoint.h)

View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGfx/Color.h>
#include <LibWebView/Forward.h>
namespace WebView {
WEBVIEW_API Gfx::Color macos_web_selection_color();
}

View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWebView/PlatformColors.h>
namespace WebView {
Gfx::Color macos_web_selection_color()
{
return Gfx::Color(128, 188, 254, 153);
}
}

View file

@ -8,6 +8,7 @@
#include <LibCore/Resource.h>
#include <LibGfx/Palette.h>
#include <LibGfx/SystemTheme.h>
#include <LibWebView/PlatformColors.h>
#import <Cocoa/Cocoa.h>
#import <Interface/Palette.h>
@ -39,7 +40,7 @@ Core::AnonymousBuffer create_system_palette()
auto palette = Gfx::Palette(move(palette_impl));
palette.set_flag(Gfx::FlagRole::IsDark, is_dark);
palette.set_color(Gfx::ColorRole::Accent, ns_color_to_gfx_color([NSColor controlAccentColor]));
palette.set_color(Gfx::ColorRole::Selection, Gfx::Color(128, 188, 254, 153));
palette.set_color(Gfx::ColorRole::Selection, WebView::macos_web_selection_color());
palette.set_color(Gfx::ColorRole::InactiveSelection, ns_color_to_gfx_color([NSColor unemphasizedSelectedTextBackgroundColor]));
palette.set_color(Gfx::ColorRole::InactiveSelectionText, ns_color_to_gfx_color([NSColor unemphasizedSelectedTextColor]));
// FIXME: There are more system colors we currently don't use (https://developer.apple.com/documentation/appkit/nscolor/3000782-controlaccentcolor?language=objc)

View file

@ -10,6 +10,11 @@
class QWidget;
class QColor;
namespace Gfx {
class Color;
}
namespace Ladybird {
@ -19,6 +24,8 @@ void install_always_active_window_control_hover_tracking(QWidget&, void (*hover_
void install_appkit_event_capture();
void make_appkit_window_first_responder(QWidget&);
bool start_appkit_window_drag(QWidget&);
Gfx::Color appkit_web_inactive_selection_color();
Gfx::Color appkit_web_inactive_selection_text_color();
#endif
}

View file

@ -6,6 +6,7 @@
#include <UI/Qt/MacWindow.h>
#include <LibGfx/Color.h>
#include <QAbstractNativeEventFilter>
#include <QColor>
#include <QCoreApplication>
@ -66,6 +67,19 @@ namespace Ladybird {
static NSEvent* s_latest_window_drag_event;
static Gfx::Color ns_color_to_gfx_color(NSColor* color)
{
auto* rgb_color = [color colorUsingColorSpace:NSColorSpace.genericRGBColorSpace];
if (rgb_color != nil)
return {
static_cast<u8>([rgb_color redComponent] * 255),
static_cast<u8>([rgb_color greenComponent] * 255),
static_cast<u8>([rgb_color blueComponent] * 255),
static_cast<u8>([rgb_color alphaComponent] * 255)
};
return {};
}
static bool is_window_drag_on_gesture_enabled()
{
return [[NSUserDefaults standardUserDefaults] boolForKey:@"NSWindowShouldDragOnGesture"];
@ -248,4 +262,14 @@ bool start_appkit_window_drag(QWidget& widget)
return true;
}
Gfx::Color appkit_web_inactive_selection_color()
{
return ns_color_to_gfx_color([NSColor unemphasizedSelectedTextBackgroundColor]);
}
Gfx::Color appkit_web_inactive_selection_text_color()
{
return ns_color_to_gfx_color([NSColor unemphasizedSelectedTextColor]);
}
}

View file

@ -24,9 +24,13 @@
#include <LibWeb/UIEvents/KeyCode.h>
#include <LibWeb/UIEvents/MouseButton.h>
#include <LibWebView/Application.h>
#include <LibWebView/PlatformColors.h>
#include <LibWebView/Utilities.h>
#include <LibWebView/WebContentClient.h>
#include <UI/Qt/Application.h>
#ifdef AK_OS_MACOS
# include <UI/Qt/MacWindow.h>
#endif
#include <UI/Qt/StringUtils.h>
#include <UI/Qt/WebContentView.h>
@ -857,7 +861,13 @@ static Core::AnonymousBuffer make_system_theme_from_qt_palette(QWidget& widget,
translate(Gfx::ColorRole::VisitedLink, QPalette::ColorRole::LinkVisited);
translate(Gfx::ColorRole::Button, QPalette::ColorRole::Button);
translate(Gfx::ColorRole::ButtonText, QPalette::ColorRole::ButtonText);
#ifdef AK_OS_MACOS
palette.set_color(Gfx::ColorRole::Selection, WebView::macos_web_selection_color());
palette.set_color(Gfx::ColorRole::InactiveSelection, appkit_web_inactive_selection_color());
palette.set_color(Gfx::ColorRole::InactiveSelectionText, appkit_web_inactive_selection_text_color());
#else
translate(Gfx::ColorRole::Selection, QPalette::ColorRole::Highlight);
#endif
palette.set_flag(Gfx::FlagRole::IsDark, is_using_dark_system_theme(widget));