LibGfx+LibWeb: Preserve text color for selections

Selecting text without custom ::selection styling changed the
foreground color of the selected content. This was especially visible
for links, where the text changed color but the underline did not.

The default selection style supplied both a selection background and a
foreground color from the palette or HighlightText system color. That
made ordinary selections behave as if the page had explicitly styled
::selection color.

Only provide a default selection background, so selected content keeps
its own foreground color unless CSS overrides it. Remove the now-unused
SelectionText palette role.
This commit is contained in:
Jelle Raaijmakers 2026-06-18 13:09:01 +02:00 committed by Jelle Raaijmakers
parent 91d3d135eb
commit d819152b4e
18 changed files with 15 additions and 30 deletions

View file

@ -35,7 +35,6 @@ ThreedShadow1=#3d3e40
ThreedShadow2=#2e2f30
HoverHighlight=#696969
Selection=#14141a
SelectionText=white
InactiveSelection=#606060
InactiveSelectionText=white
RubberBandFill=#8080803c

View file

@ -43,7 +43,6 @@ ThreedShadow1=#808080
ThreedShadow2=#404040
HoverHighlight=#e3dfdb
Selection=#84351a
SelectionText=white
InactiveSelection=#606060
InactiveSelectionText=white
PlaceholderText=#808080

View file

@ -65,7 +65,6 @@ public:
Color window() const { return color(ColorRole::Window); }
Color window_text() const { return color(ColorRole::WindowText); }
Color selection() const { return color(ColorRole::Selection); }
Color selection_text() const { return color(ColorRole::SelectionText); }
Color inactive_selection() const { return color(ColorRole::InactiveSelection); }
Color inactive_selection_text() const { return color(ColorRole::InactiveSelectionText); }
Color desktop_background() const { return color(ColorRole::DesktopBackground); }

View file

@ -86,7 +86,6 @@ namespace Gfx {
C(RulerBorder) \
C(RulerInactiveText) \
C(Selection) \
C(SelectionText) \
C(SyntaxComment) \
C(SyntaxControlKeyword) \
C(SyntaxIdentifier) \

View file

@ -302,20 +302,11 @@ Paintable::SelectionStyle Paintable::selection_style() const
auto default_style_for_color_scheme = [&](CSS::PreferredColorScheme color_scheme, bool use_palette_for_normal_color_scheme = true) {
auto palette = document().page().palette();
auto palette_color_scheme = palette.is_dark() ? CSS::PreferredColorScheme::Dark : CSS::PreferredColorScheme::Light;
if (color_scheme == palette_color_scheme || use_palette_for_normal_color_scheme) {
return SelectionStyle {
CSS::SystemColor::transform_selection_background_color(palette.selection()),
palette.selection_text(),
{},
{},
};
}
if (color_scheme == palette_color_scheme || use_palette_for_normal_color_scheme)
return SelectionStyle { CSS::SystemColor::transform_selection_background_color(palette.selection()) };
return SelectionStyle {
CSS::SystemColor::transform_selection_background_color(CSS::SystemColor::highlight(color_scheme)),
CSS::SystemColor::highlight_text(color_scheme),
{},
{},
CSS::SystemColor::transform_selection_background_color(CSS::SystemColor::highlight(color_scheme))
};
};

View file

@ -128,9 +128,9 @@ public:
};
struct SelectionStyle {
Color background_color;
Optional<Color> text_color;
Optional<Vector<ShadowData>> text_shadow;
Optional<TextDecorationStyle> text_decoration;
Optional<Color> text_color {};
Optional<Vector<ShadowData>> text_shadow {};
Optional<TextDecorationStyle> text_decoration {};
bool has_styling() const
{

View file

@ -7,11 +7,11 @@ p {
}
.selected-light {
background: rgba(101, 2, 0, 0.8);
color: white;
color: black;
}
.selected-dark {
background: rgba(61, 174, 233, 0.5);
color: rgb(20, 20, 20);
color: black;
}
</style>

View file

@ -7,7 +7,7 @@ p {
}
.selected {
background: rgba(61, 174, 233, 0.5);
color: rgb(20, 20, 20);
color: black;
}
</style>

View file

@ -6,7 +6,8 @@ td {
}
.selected {
background: rgba(101, 2, 0, 0.8);
color: white;
color: #000000;
text-decoration: none;
}
</style>

View file

@ -8,7 +8,7 @@ p {
}
.selected {
background: rgba(61, 174, 233, 0.5);
color: rgb(20, 20, 20);
color: rgb(235, 235, 235);
}
</style>

View file

@ -7,7 +7,7 @@ p {
}
.selected {
background: rgba(101, 2, 0, 0.8);
color: white;
color: black;
}
</style>

View file

@ -1,7 +1,7 @@
<!DOCTYPE html>
<style>
img { width: 50px; height: 50px; vertical-align: middle; }
.selected { background: rgba(101, 2, 0, 0.8); color: white; }
.selected { background: rgba(101, 2, 0, 0.8); color: black; }
.img-wrapper {
display: inline-block;
position: relative;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View file

@ -1,5 +1,4 @@
<!DOCTYPE html>
<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-417">
<style>
span {
color: red;

View file

@ -18,7 +18,7 @@ SaveLayer@0
DrawLine@1 from=[57,70] to=[67,70] color=rgb(0, 0, 0) thickness=10
Save@1
AddClipRect@1 rect=[69,-17 47x100]
DrawGlyphRun@1 rect=[8,8 169x50] translation=[8,62.984375] color=rgb(255, 255, 255)
DrawGlyphRun@1 rect=[8,8 169x50] translation=[8,62.984375] color=rgb(0, 0, 0)
Restore@1
DrawLine@1 from=[109,70] to=[116,70] color=rgb(0, 0, 0) thickness=10
Save@1

View file

@ -40,7 +40,6 @@ Core::AnonymousBuffer create_system_palette()
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::SelectionText, Gfx::Color::Black);
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

@ -858,7 +858,6 @@ static Core::AnonymousBuffer make_system_theme_from_qt_palette(QWidget& widget,
translate(Gfx::ColorRole::Button, QPalette::ColorRole::Button);
translate(Gfx::ColorRole::ButtonText, QPalette::ColorRole::ButtonText);
translate(Gfx::ColorRole::Selection, QPalette::ColorRole::Highlight);
translate(Gfx::ColorRole::SelectionText, QPalette::ColorRole::HighlightedText);
palette.set_flag(Gfx::FlagRole::IsDark, is_using_dark_system_theme(widget));