Skip to content

Truly independent web browser

LibGfx+LibWeb+LibWebView+UI/Qt: Follow the system selection color

Andreas Kling33124e44449b
diff --git a/Base/res/themes/Dark.ini b/Base/res/themes/Dark.ini
index 7fe1e88a1b7..a113b7e2aa3 100644
--- a/Base/res/themes/Dark.ini
+++ b/Base/res/themes/Dark.ini
@@ -34,7 +34,8 @@ ThreedShadow2=#2e2f30
HoverHighlight=#696969
Selection=#14141a
InactiveSelection=#606060
-InactiveSelectionText=white
+LightSelection=#3daee980
+DarkSelection=#14141a
RubberBandFill=#8080803c
RubberBandBorder=black
Link=#88c
diff --git a/Base/res/themes/Default.ini b/Base/res/themes/Default.ini
index 697ae5009be..62f64d2ef14 100644
--- a/Base/res/themes/Default.ini
+++ b/Base/res/themes/Default.ini
@@ -42,7 +42,8 @@ ThreedShadow2=#404040
HoverHighlight=#e3dfdb
Selection=#84351a
InactiveSelection=#606060
-InactiveSelectionText=white
+LightSelection=#84351a
+DarkSelection=#3daee980
PlaceholderText=#808080
RubberBandFill=#f4ca9e3c
RubberBandBorder=#6e2209
diff --git a/Libraries/LibGfx/Palette.h b/Libraries/LibGfx/Palette.h
index b3ed5430931..4e33aa7de36 100644
--- a/Libraries/LibGfx/Palette.h
+++ b/Libraries/LibGfx/Palette.h
@@ -56,7 +56,8 @@ public:
Color window_text() const { return color(ColorRole::WindowText); }
Color selection() const { return color(ColorRole::Selection); }
Color inactive_selection() const { return color(ColorRole::InactiveSelection); }
- Color inactive_selection_text() const { return color(ColorRole::InactiveSelectionText); }
+ Color light_selection() const { return color(ColorRole::LightSelection); }
+ Color dark_selection() const { return color(ColorRole::DarkSelection); }
Color desktop_background() const { return color(ColorRole::DesktopBackground); }
Color active_window_border1() const { return color(ColorRole::ActiveWindowBorder1); }
Color active_window_border2() const { return color(ColorRole::ActiveWindowBorder2); }
diff --git a/Libraries/LibGfx/SystemTheme.h b/Libraries/LibGfx/SystemTheme.h
index 441221cf10f..5d029a20f6f 100644
--- a/Libraries/LibGfx/SystemTheme.h
+++ b/Libraries/LibGfx/SystemTheme.h
@@ -57,12 +57,13 @@ namespace Gfx {
C(HighlightSearchingText) \
C(HoverHighlight) \
C(InactiveSelection) \
- C(InactiveSelectionText) \
C(InactiveWindowBorder1) \
C(InactiveWindowBorder2) \
C(InactiveWindowTitle) \
C(InactiveWindowTitleShadow) \
C(InactiveWindowTitleStripes) \
+ C(LightSelection) \
+ C(DarkSelection) \
C(Link) \
C(Magenta) \
C(MenuBase) \
diff --git a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
index 6721848af2a..f92ee976cff 100644
--- a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
+++ b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
@@ -612,8 +612,8 @@ Optional<DisplayListRecording> start_rust_display_list_recording(Layout::BegunRe
inputs.palette_is_dark = palette.is_dark();
auto palette_color_scheme = palette.is_dark() ? CSS::PreferredColorScheme::Dark : CSS::PreferredColorScheme::Light;
inputs.selection_background_from_palette = CSS::SystemColor::transform_selection_background_color(palette.selection(), palette_color_scheme);
- inputs.selection_background_light = CSS::SystemColor::transform_selection_background_color(CSS::SystemColor::highlight(CSS::PreferredColorScheme::Light), CSS::PreferredColorScheme::Light);
- inputs.selection_background_dark = CSS::SystemColor::transform_selection_background_color(CSS::SystemColor::highlight(CSS::PreferredColorScheme::Dark), CSS::PreferredColorScheme::Dark);
+ inputs.selection_background_light = CSS::SystemColor::transform_selection_background_color(palette.light_selection(), CSS::PreferredColorScheme::Light);
+ inputs.selection_background_dark = CSS::SystemColor::transform_selection_background_color(palette.dark_selection(), CSS::PreferredColorScheme::Dark);
inputs.inactive_selection_background_from_palette = CSS::SystemColor::transform_selection_background_color(palette.inactive_selection(), palette_color_scheme);
inputs.inactive_selection_background_light = CSS::SystemColor::transform_selection_background_color(CSS::SystemColor::inactive_highlight(CSS::PreferredColorScheme::Light), CSS::PreferredColorScheme::Light);
inputs.inactive_selection_background_dark = CSS::SystemColor::transform_selection_background_color(CSS::SystemColor::inactive_highlight(CSS::PreferredColorScheme::Dark), CSS::PreferredColorScheme::Dark);
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txt
index 07dae5e2fca..d040d67740b 100644
--- a/Libraries/LibWebView/CMakeLists.txt
+++ b/Libraries/LibWebView/CMakeLists.txt
@@ -101,7 +101,6 @@ if (APPLE)
list(APPEND SOURCES
CrashReportPOSIX.cpp
NotificationBackendMacOS.mm
- PlatformColorsMacOS.cpp
ProcessReaper.cpp
QuarantineMacOS.mm
StringConversionsMacOS.mm
diff --git a/Libraries/LibWebView/PlatformColors.h b/Libraries/LibWebView/PlatformColors.h
deleted file mode 100644
index 0a5495e8587..00000000000
--- a/Libraries/LibWebView/PlatformColors.h
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * 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();
-WEBVIEW_API Gfx::Color macos_web_inactive_selection_color();
-WEBVIEW_API Gfx::Color macos_web_inactive_selection_text_color();
-
-}
diff --git a/Libraries/LibWebView/PlatformColorsMacOS.cpp b/Libraries/LibWebView/PlatformColorsMacOS.cpp
deleted file mode 100644
index ab69cdac2c2..00000000000
--- a/Libraries/LibWebView/PlatformColorsMacOS.cpp
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * 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);
-}
-
-Gfx::Color macos_web_inactive_selection_color()
-{
- return Gfx::Color(183, 183, 183, 153);
-}
-
-Gfx::Color macos_web_inactive_selection_text_color()
-{
- return Gfx::Color::Black;
-}
-
-}
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index 6142bc100ca..9d7b343615f 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -152,6 +152,7 @@ if (APPLE)
MicrophoneAuthorizerMacOS.mm
NativeTextEditingBindings.mm
Menu.mm
+ PlatformColorsMac.mm
ScrollbarPreferencesMac.mm
SpellCheckerMac.mm
SystemAppearanceMac.mm
@@ -163,6 +164,7 @@ if (APPLE)
set_source_files_properties(
LadybirdAccessibilityElement.mm
MicrophoneAuthorizerMacOS.mm
+ PlatformColorsMac.mm
SpellCheckerMac.mm
TranslateIconMac.mm
TranslatorMac.mm
diff --git a/UI/Qt/PlatformColors.h b/UI/Qt/PlatformColors.h
new file mode 100644
index 00000000000..84dcce4c3fd
--- /dev/null
+++ b/UI/Qt/PlatformColors.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibGfx/Color.h>
+#include <LibWebCommon/CSS/PreferredColorScheme.h>
+
+namespace Ladybird {
+
+Gfx::Color macos_web_selection_color(Web::CSS::PreferredColorScheme);
+Gfx::Color macos_web_inactive_selection_color();
+
+}
diff --git a/UI/Qt/PlatformColorsMac.mm b/UI/Qt/PlatformColorsMac.mm
new file mode 100644
index 00000000000..4def5bd7c40
--- /dev/null
+++ b/UI/Qt/PlatformColorsMac.mm
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Math.h>
+#include <AK/Optional.h>
+#include <UI/Qt/PlatformColors.h>
+
+#import <AppKit/AppKit.h>
+
+namespace Ladybird {
+
+static Optional<Gfx::Color> color_in_appearance(NSColor* color, Web::CSS::PreferredColorScheme color_scheme)
+{
+ __block Optional<Gfx::Color> result;
+ auto* appearance = [NSAppearance appearanceNamed:color_scheme == Web::CSS::PreferredColorScheme::Dark ? NSAppearanceNameDarkAqua : NSAppearanceNameAqua];
+ [appearance performAsCurrentDrawingAppearance:^{
+ auto* srgb_color = [color colorUsingColorSpace:[NSColorSpace sRGBColorSpace]];
+ if (!srgb_color)
+ return;
+ auto channel = [](CGFloat value) { return static_cast<u8>(clamp(AK::round(value * 255), 0, 255)); };
+ result = Gfx::Color(channel(srgb_color.redComponent), channel(srgb_color.greenComponent), channel(srgb_color.blueComponent),
+ channel(srgb_color.alphaComponent));
+ }];
+ return result;
+}
+
+// NB: The system's text selection color follows the user's highlight color. LibWeb makes it translucent.
+Gfx::Color macos_web_selection_color(Web::CSS::PreferredColorScheme color_scheme)
+{
+ return color_in_appearance([NSColor selectedTextBackgroundColor], color_scheme).value_or(Gfx::Color(128, 188, 254, 153));
+}
+
+Gfx::Color macos_web_inactive_selection_color()
+{
+ return Gfx::Color(183, 183, 183, 153);
+}
+
+}
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index fe11f9d134d..7252e0caccc 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -27,11 +27,11 @@
#include <LibGfx/Palette.h>
#include <LibGfx/Rect.h>
#include <LibGfx/SystemTheme.h>
+#include <LibWebCommon/CSS/SystemColor.h>
#include <LibWebCommon/UIEvents/KeyCode.h>
#include <LibWebCommon/UIEvents/MouseButton.h>
#include <LibWebView/Application.h>
#include <LibWebView/CrashReport.h>
-#include <LibWebView/PlatformColors.h>
#include <LibWebView/Utilities.h>
#include <LibWebView/WebCompatReport.h>
#include <LibWebView/WebContentClient.h>
@@ -48,6 +48,7 @@
#include <UI/Qt/InputMethodUtils.h>
#include <UI/Qt/Menu.h>
#include <UI/Qt/PlainText.h>
+#include <UI/Qt/PlatformColors.h>
#include <UI/Qt/SelectDropdown.h>
#include <UI/Qt/StringUtils.h>
#include <UI/Qt/SystemAppearance.h>
@@ -2000,19 +2001,42 @@ 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);
+ // NB: Content whose color scheme is not the palette's own takes the selection color for its scheme.
#ifdef AK_OS_MACOS
- palette.set_color(Gfx::ColorRole::Selection, WebView::macos_web_selection_color());
- palette.set_color(Gfx::ColorRole::InactiveSelection, WebView::macos_web_inactive_selection_color());
- palette.set_color(Gfx::ColorRole::InactiveSelectionText, WebView::macos_web_inactive_selection_text_color());
+ palette.set_color(Gfx::ColorRole::Selection, macos_web_selection_color(dark ? Web::CSS::PreferredColorScheme::Dark : Web::CSS::PreferredColorScheme::Light));
+ palette.set_color(Gfx::ColorRole::LightSelection, macos_web_selection_color(Web::CSS::PreferredColorScheme::Light));
+ palette.set_color(Gfx::ColorRole::DarkSelection, macos_web_selection_color(Web::CSS::PreferredColorScheme::Dark));
+ palette.set_color(Gfx::ColorRole::InactiveSelection, macos_web_inactive_selection_color());
#else
- translate(Gfx::ColorRole::Selection, QPalette::ColorRole::Highlight);
+ // NB: Desktop highlight colors are mostly strong ones meant for white text, but selected content keeps its own text
+ // color. On light content, a strong highlight becomes a tint, as in GTK's text views, so dark text and links
+ // stay readable on it, while a light one already suits dark text. On dark content, a highlight is kept as long
+ // as light text stays readable on it, so dark accents stay visible against the page, and lighter ones become
+ // a tint.
+ auto selection_color = [](QColor highlight, Web::CSS::PreferredColorScheme color_scheme) {
+ auto color = Gfx::Color::from_bgra(highlight.rgba());
+ if (color.alpha() < 255)
+ return color;
+ if (color_scheme == Web::CSS::PreferredColorScheme::Dark) {
+ auto canvas = Web::CSS::SystemColor::canvas(color_scheme);
+ auto selection = canvas.blend(Web::CSS::SystemColor::transform_selection_background_color(color, color_scheme));
+ if (Web::CSS::SystemColor::canvas_text(color_scheme).contrast_ratio(selection) >= 4.5)
+ return color;
+ return color.with_alpha(115); // 45%
+ }
+ if (highlight.lightnessF() < 0.75f)
+ return color.with_alpha(77); // 30%
+ return color;
+ };
auto active_highlight = qt_palette.color(QPalette::Active, QPalette::ColorRole::Highlight);
auto inactive_highlight = qt_palette.color(QPalette::Inactive, QPalette::ColorRole::Highlight);
+ auto palette_color_scheme = dark ? Web::CSS::PreferredColorScheme::Dark : Web::CSS::PreferredColorScheme::Light;
+ palette.set_color(Gfx::ColorRole::Selection, selection_color(active_highlight, palette_color_scheme));
+ palette.set_color(Gfx::ColorRole::LightSelection, selection_color(active_highlight, Web::CSS::PreferredColorScheme::Light));
+ palette.set_color(Gfx::ColorRole::DarkSelection, selection_color(active_highlight, Web::CSS::PreferredColorScheme::Dark));
if (inactive_highlight != active_highlight) {
- palette.set_color(Gfx::ColorRole::InactiveSelection, Gfx::Color::from_bgra(inactive_highlight.rgba()));
- auto inactive_highlighted_text = qt_palette.color(QPalette::Inactive, QPalette::ColorRole::HighlightedText);
- palette.set_color(Gfx::ColorRole::InactiveSelectionText, Gfx::Color::from_bgra(inactive_highlighted_text.rgba()));
+ palette.set_color(Gfx::ColorRole::InactiveSelection, selection_color(inactive_highlight, palette_color_scheme));
} else {
// The Qt theme does not differentiate inactive selections; use a neutral gray.
auto inactive_selection = dark ? Gfx::Color(0x60, 0x60, 0x60) : Gfx::Color(0xd4, 0xd4, 0xd4);