Add inline_code_background field to TextViewStyle#2146
Add inline_code_background field to TextViewStyle#2146KlausUllrich wants to merge 3 commits intolongbridge:mainfrom
Conversation
Adds an `inline_code_background: Option<Hsla>` field to `TextViewStyle`, allowing consumers to customize the background color of inline code spans (backtick text in markdown). When the field is `None` (the default), behavior is unchanged — inline code uses `cx.theme().accent` as before. When set via the new `.inline_code_background(color)` builder method, the provided color is used instead. This follows the same pattern as the existing `heading_font_size` field: an `Option` with a sensible default fallback, plus a builder method on `TextViewStyle` for ergonomic configuration. Motivation: `cx.theme().accent` is designed for interactive elements (buttons, links) and can be visually heavy for inline code mixed with body text. This field lets consumers choose a subtler background without forking or vendoring the crate. Also adds a clarifying comment on the hand-written `PartialEq` impl noting which fields are intentionally excluded from comparison.
2acff2e to
987d0c2
Compare
| pub code_block: StyleRefinement, | ||
| /// Background color for inline code spans (backtick text). | ||
| /// Defaults to `cx.theme().accent` when `None`. | ||
| pub inline_code_background: Option<Hsla>, |
There was a problem hiding this comment.
Use StyleRefinement like the code_block.
There was a problem hiding this comment.
Thanks for the review! Good call — I've updated the PR to use StyleRefinement for consistency with code_block.
Changes:
- Renamed
inline_code_background: Option<Hsla>→inline_code: StyleRefinement - Builder method now matches
code_block's signature - Fully backwards compatible —
StyleRefinement::default()preserves existing behavior
Implementation note: the render path reads inline_code.text.background_color (not element-level background) because inline code renders as a text-run highlight via HighlightStyle, not a container div. text.background_color maps directly to HighlightStyle.background_color — and since Background.solid is pub(crate) in gpui, extracting Hsla from Fill::Color isn't possible from downstream crates.
Will push the updated code shortly.
Per reviewer feedback, use `StyleRefinement` for consistency with the existing `code_block` field. The render path reads `inline_code.text.background_color` (text-level) rather than element-level `background`, since inline code renders as a text-run highlight via `HighlightStyle`, not a container div. Fully backwards compatible — `StyleRefinement::default()` preserves existing behavior (falls back to `cx.theme().accent`). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| .text | ||
| .background_color | ||
| .unwrap_or(cx.theme().accent); | ||
| highlight.background_color = Some(bg); |
There was a problem hiding this comment.
I just realized this is a HighlightStyle type.
-
If modifying the background_color is supported, then there will be other corresponding modifications.
-
Since the
inline_codeis an inline style, it should useHighlightStyleinstead ofStyleRefinement. However,HighlightStylemay not have arefinemethod, so this is more complex and cannot be modified so simply.
There was a problem hiding this comment.
Thank you for the feedback. You were right — HighlightStyle is the correct abstraction for inline text spans. I have updated the PR:
inline_codefield is nowHighlightStyleinstead ofStyleRefinement- The render path merges via
.highlight(), which means all HighlightStyle properties (color, weight, style, background, underline, strikethrough) are now supported for inline code - Default behavior is unchanged: when no background is set, falls back to
cx.theme().accent
Backwards compatible — HighlightStyle::default() preserves existing rendering for consumers who do not set a custom style.
Per review feedback: inline code is a text span, not a box element. HighlightStyle is the correct abstraction — it supports color, weight, style, background, underline, and strikethrough for text runs. - Changed `inline_code` field type from StyleRefinement to HighlightStyle - Render path now merges via `.highlight()` instead of extracting a single field - All HighlightStyle properties (bold, italic, color, underline, etc.) now work for inline code spans - Default behavior unchanged: falls back to cx.theme().accent when no background is set - Fully backwards compatible: HighlightStyle::default() preserves existing rendering for consumers who do not set a custom style Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
Adds an
inline_code: StyleRefinementfield toTextViewStyle, giving consumers full style control over inline code spans (backtick text). Follows the same pattern as the existingcode_block: StyleRefinementfield.Problem
Inline code currently uses
cx.theme().accentas its background highlight. The accent color is designed for interactive/attention-grabbing elements (buttons, links, selections), which makes it visually heavy when applied to inline code mixed with body text. There's no way to customize this without forking or vendoring the crate.Solution
Add a new
StyleRefinementfield toTextViewStylewith a builder method, matchingcode_block:Default behavior is unchanged — when the field is
StyleRefinement::default(), inline code continues to usecx.theme().accentexactly as before. Consumers who want a different background can set it:Implementation
Two files changed:
crates/ui/src/text/style.rs: Addedinline_code: StyleRefinementfield,StyleRefinement::default()default, and builder methodcrates/ui/src/text/node.rs: InParagraph::render(), readsnode_cx.style.inline_code.text.background_colorwith fallback tocx.theme().accentThe render path uses
text.background_color(not element-levelbackground) because inline code renders as a text-run highlight viaHighlightStyle, not a container div.Backwards Compatibility
Fully backwards compatible:
StyleRefinement::default()→ existing behavior preservedcode_blockfieldcargo checkpasses cleanly.