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.iniindex 7fe1e88a1b7..a113b7e2aa3 100644--- a/Base/res/themes/Dark.ini+++ b/Base/res/themes/Dark.ini@@ -34,7 +34,8 @@ ThreedShadow2=#2e2f30HoverHighlight=#696969Selection=#14141aInactiveSelection=#606060-InactiveSelectionText=white+LightSelection=#3daee980+DarkSelection=#14141aRubberBandFill=#8080803cRubberBandBorder=blackLink=#88cdiff --git a/Base/res/themes/Default.ini b/Base/res/themes/Default.iniindex 697ae5009be..62f64d2ef14 100644--- a/Base/res/themes/Default.ini+++ b/Base/res/themes/Default.ini@@ -42,7 +42,8 @@ ThreedShadow2=#404040HoverHighlight=#e3dfdbSelection=#84351aInactiveSelection=#606060-InactiveSelectionText=white+LightSelection=#84351a+DarkSelection=#3daee980PlaceholderText=#808080RubberBandFill=#f4ca9e3cRubberBandBorder=#6e2209diff --git a/Libraries/LibGfx/Palette.h b/Libraries/LibGfx/Palette.hindex 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.hindex 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.cppindex 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::BegunReinputs.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.txtindex 07dae5e2fca..d040d67740b 100644--- a/Libraries/LibWebView/CMakeLists.txt+++ b/Libraries/LibWebView/CMakeLists.txt@@ -101,7 +101,6 @@ if (APPLE)list(APPEND SOURCESCrashReportPOSIX.cppNotificationBackendMacOS.mm- PlatformColorsMacOS.cppProcessReaper.cppQuarantineMacOS.mmStringConversionsMacOS.mmdiff --git a/Libraries/LibWebView/PlatformColors.h b/Libraries/LibWebView/PlatformColors.hdeleted file mode 100644index 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.cppdeleted file mode 100644index 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.txtindex 6142bc100ca..9d7b343615f 100644--- a/UI/Qt/CMakeLists.txt+++ b/UI/Qt/CMakeLists.txt@@ -152,6 +152,7 @@ if (APPLE)MicrophoneAuthorizerMacOS.mmNativeTextEditingBindings.mmMenu.mm+ PlatformColorsMac.mmScrollbarPreferencesMac.mmSpellCheckerMac.mmSystemAppearanceMac.mm@@ -163,6 +164,7 @@ if (APPLE)set_source_files_properties(LadybirdAccessibilityElement.mmMicrophoneAuthorizerMacOS.mm+ PlatformColorsMac.mmSpellCheckerMac.mmTranslateIconMac.mmTranslatorMac.mmdiff --git a/UI/Qt/PlatformColors.h b/UI/Qt/PlatformColors.hnew file mode 100644index 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.mmnew file mode 100644index 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.cppindex 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);