Skip to content

Add currentColor support for SVG controls#518

Merged
wieslawsoltes merged 5 commits into
masterfrom
fix/discussion-440-current-color
May 9, 2026
Merged

Add currentColor support for SVG controls#518
wieslawsoltes merged 5 commits into
masterfrom
fix/discussion-440-current-color

Conversation

@wieslawsoltes
Copy link
Copy Markdown
Owner

PR Summary: Add host-provided currentColor support

Summary

This change adds a host-facing CurrentColor API so Avalonia consumers can theme SVG assets that use fill="currentColor" or stroke="currentColor" without rewriting the SVG or injecting fill/stroke-specific CSS selectors.

The new color value is passed through SvgParameters and applied as the SVG root color only when the SVG document does not already define its own root color, or when the root explicitly asks to inherit/use currentColor. Explicit root colors such as color="red" or style="color:red" are preserved.

Motivation

Discussion #440 requested a Foreground-style mechanism for SVG controls so icon assets authored with currentColor can follow application theme colors. Before this change, callers had to override concrete paint properties with CSS, which is awkward for reusable icon sets and does not map well to how currentColor is expected to work in SVG/CSS.

Changes

Shared model and loading

  • Added CurrentColor to SvgParameters.
  • Preserved the existing two-argument SvgParameters(entities, css) constructor and deconstruction shape for source compatibility.
  • Updated SvgService load paths to apply SvgParameters.CurrentColor after SVG or VectorDrawable parsing.
  • Added a root-color guard so host-provided CurrentColor does not override explicit SVG root color values.
  • Added SvgService.FromSvg(string, SvgParameters?) so inline SVG parsing can carry the same parameters as stream/path loading.

Avalonia controls

  • Added inherited CurrentColor attached/instance property to Avalonia.Svg.Skia.Svg.
  • Added inherited CurrentColor attached/instance property to Avalonia.Svg.Svg.
  • Updated path, inline source, CSS, and current CSS reload flows to include CurrentColor.
  • Updated Skia Avalonia path cache keys so cached sources are separated by CSS and CurrentColor.

Skia Avalonia image/resource APIs

  • Added SvgImage.CurrentColor.
  • Added SvgSource.CurrentColor and preserved it when cloning or cloning parameters.
  • Updated SvgImageExtension to read inherited Svg.CurrentColor from the target control and bind future changes to SvgImage.CurrentColor.
  • Updated SvgResourceExtension with a CurrentColor property for resource/brush creation.
  • Preserved existing source CSS/currentColor parameters when SvgImage applies additional style overrides.

Uno preservation

  • Included SvgParameters.CurrentColor in Uno SvgSource parameter cloning.
  • Included CurrentColor in Uno cache keys.
  • Preserved source CurrentColor when Uno controls rebuild external sources with additional CSS.

Documentation

  • Added a README example showing CurrentColor usage with SVG assets that contain fill="currentColor" or stroke="currentColor".

Tests

Focused verification passed:

  • dotnet test tests/Svg.Skia.UnitTests/Svg.Skia.UnitTests.csproj -c Release --filter "FullyQualifiedName~SKSvgTests.Load_CurrentColorParameter"
  • dotnet test tests/Svg.Controls.Skia.Avalonia.UnitTests/Svg.Controls.Skia.Avalonia.UnitTests.csproj -c Release --filter "FullyQualifiedName~SvgControlTests|FullyQualifiedName~SvgImageTests|FullyQualifiedName~SvgResourceExtensionTests"
  • dotnet test tests/Svg.Controls.Skia.Uno.UnitTests/Svg.Controls.Skia.Uno.UnitTests.csproj -c Release --filter "FullyQualifiedName~SvgSourceTests.CacheKey|FullyQualifiedName~SvgSourceTests.BuildParameters"
  • dotnet test tests/Svg.Controls.Avalonia.UnitTests/Svg.Controls.Avalonia.UnitTests.csproj -c Release
  • dotnet build src/Svg.Controls.Avalonia/Svg.Controls.Avalonia.csproj -c Release -f net10.0 --no-restore
  • dotnet build src/Svg.Controls.Skia.Avalonia/Svg.Controls.Skia.Avalonia.csproj -c Release -f net10.0 --no-restore
  • dotnet build src/Svg.Controls.Skia.Uno/Svg.Controls.Skia.Uno.csproj -c Release --no-restore
  • git diff --check

The full solution build was attempted with:

  • dotnet build Svg.Skia.slnx -c Release --no-restore

It still fails before completing because several sample/editor/benchmark projects do not have project.assets.json generated in this worktree, and some net461 editor targets require the .NET Framework 4.6.1 targeting pack.

Notes For Review

  • Host-provided CurrentColor intentionally does not override explicit SVG root color.
  • Root color="currentColor" is treated as asking for the host current color.
  • The Skia Avalonia cache key now includes style inputs because the same SVG path can render differently under different CSS/currentColor values.
  • The PR summary file itself is intentionally left uncommitted.

@wieslawsoltes wieslawsoltes marked this pull request as ready for review May 9, 2026 09:39
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5ec94d8e9b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +192 to +196
var baseCss = string.IsNullOrWhiteSpace(css) && string.IsNullOrWhiteSpace(currentCss)
? source.Parameters?.Css ?? source.Css
: null;
var combinedCss = CombineCss(baseCss, css, currentCss);
var drawingColor = ToDrawingColor(currentColor) ?? source.Parameters?.CurrentColor ?? ToDrawingColor(source.CurrentColor);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not preserve stale SvgImage override parameters

When a SvgImage override is cleared, this falls back to source.Parameters, but source.Parameters is overwritten by the previous source.ReLoad(parameters) call. For example, after CurrentColor (or Css/CurrentCss) is set once and later becomes null/empty via an inherited binding, BuildParameters reuses the old parameter value, HasSameParameters sees no change, and the image keeps rendering with the stale tint/style instead of reverting to the source defaults.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant