Skip to content

Commit

Permalink
Use geometry cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dme-compunet committed Dec 28, 2024
1 parent 3e62bd7 commit da6abbe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/Lucide.Avalonia/LucideIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public class LucideIcon : Control
public static readonly StyledProperty<double> StrokeWidthProperty =
AvaloniaProperty.Register<LucideIcon, double>(nameof(StrokeWidth), 1.5);

public static readonly StyledProperty<LucideIconKind> KindProperty =
AvaloniaProperty.Register<LucideIcon, LucideIconKind>(nameof(Kind));
public static readonly StyledProperty<LucideIconKind?> KindProperty =
AvaloniaProperty.Register<LucideIcon, LucideIconKind?>(nameof(Kind));

private readonly Pen _strokePen;
private Geometry _geometry;
private Pen? _stroke;
private Geometry? _geometry;

public double Size
{
Expand All @@ -36,7 +36,7 @@ public double StrokeWidth
get => GetValue(StrokeWidthProperty);
set => SetValue(StrokeWidthProperty, value);
}
public LucideIconKind Kind
public LucideIconKind? Kind
{
get => GetValue(KindProperty);
set => SetValue(KindProperty, value);
Expand All @@ -53,24 +53,30 @@ static LucideIcon()
AffectsMeasure<LucideIcon>(SizeProperty);
}

public LucideIcon()
{
_geometry = Geometry.Parse(IconToGeometry.CreateGeometryString(Kind));
_strokePen = new Pen(Foreground, StrokeWidth, null, PenLineCap.Round, PenLineJoin.Round);
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
if (change.Property == KindProperty)
{
var kind = change.GetNewValue<LucideIconKind>();
var kind = change.GetNewValue<LucideIconKind?>();

_geometry = Geometry.Parse(IconToGeometry.CreateGeometryString(kind));
_geometry = kind == null
? null
: IconToGeometry.CreateGeometry(kind.Value);

Check failure on line 64 in src/Lucide.Avalonia/LucideIcon.cs

View workflow job for this annotation

GitHub Actions / publish

'IconToGeometry' does not contain a definition for 'CreateGeometry'

Check failure on line 64 in src/Lucide.Avalonia/LucideIcon.cs

View workflow job for this annotation

GitHub Actions / publish

'IconToGeometry' does not contain a definition for 'CreateGeometry'

Check failure on line 64 in src/Lucide.Avalonia/LucideIcon.cs

View workflow job for this annotation

GitHub Actions / publish

'IconToGeometry' does not contain a definition for 'CreateGeometry'
}
else if (change.Property == ForegroundProperty || change.Property == StrokeWidthProperty)
{
_strokePen.Brush = Foreground;
_strokePen.Thickness = StrokeWidth;
var brush = Foreground;
var width = StrokeWidth;

if (_stroke == null)
{
_stroke = new Pen(brush, width, null, PenLineCap.Round, PenLineJoin.Round);
}
else
{
_stroke.Brush = brush;
_stroke.Thickness = width;
}
}

base.OnPropertyChanged(change);
Expand All @@ -82,11 +88,16 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang

public override void Render(DrawingContext context)
{
if (_geometry is null)
{
return;
}

AddScale(context);

context.DrawRectangle(Brushes.Transparent, null, new Rect(0, 0, Bounds.Width, Bounds.Height));

context.DrawGeometry(null, _strokePen, _geometry);
context.DrawGeometry(null, _stroke, _geometry);
}

private void AddScale(DrawingContext context)
Expand Down
17 changes: 17 additions & 0 deletions src/LucideIcons.Generator/IconToGeometryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ public string Build()
sb.AppendLine();
sb.AppendLine("internal static class IconToGeometry");
sb.AppendLine("{");
sb.AppendLine(" private static readonly Dictionary<LucideIconKind, Geometry> _geometryCache = [];");
sb.AppendLine();
sb.AppendLine(" public static Geometry CreateGeometry(LucideIconKind kind)");
sb.AppendLine(" {");
sb.AppendLine(" if (_geometryCache.TryGetValue(kind, out var geometry))");
sb.AppendLine(" {");
sb.AppendLine(" return geometry;");
sb.AppendLine(" }");
sb.AppendLine();
sb.AppendLine();
sb.AppendLine(" geometry = Geometry.Parse(CreateGeometryString(kind));");
sb.AppendLine();
sb.AppendLine(" _geometryCache.Add(kind, geometry);");
sb.AppendLine();
sb.AppendLine(" return geometry;");
sb.AppendLine(" }");
sb.AppendLine();
sb.AppendLine(" public static string CreateGeometryString(LucideIconKind kind)");
sb.AppendLine(" {");
sb.AppendLine(" return kind switch");
Expand Down

0 comments on commit da6abbe

Please sign in to comment.