-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Uses the new `IconImage` inside the `Icon` template. This may affect styling.
- Loading branch information
Showing
8 changed files
with
189 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using Avalonia; | ||
using Avalonia.Media; | ||
|
||
#nullable enable | ||
|
||
namespace Projektanker.Icons.Avalonia | ||
{ | ||
public class IconImage : AvaloniaObject, IImage | ||
{ | ||
public static readonly StyledProperty<string?> ValueProperty = | ||
AvaloniaProperty.Register<IconImage, string?>(nameof(Value)); | ||
|
||
public static readonly StyledProperty<IBrush?> BrushProperty = | ||
AvaloniaProperty.Register<IconImage, IBrush?>(nameof(Brush)); | ||
|
||
public static readonly StyledProperty<IPen?> PenProperty = | ||
AvaloniaProperty.Register<IconImage, IPen?>(nameof(Pen)); | ||
|
||
private Rect _bounds; | ||
private GeometryDrawing? _drawing; | ||
|
||
public IconImage() : this(string.Empty, new SolidColorBrush(Colors.Black)) | ||
{ | ||
} | ||
|
||
public IconImage(string? value, IBrush? brush) | ||
{ | ||
Value = value; | ||
Brush = brush; | ||
HandleValueChanged(); | ||
HandleBrushChanged(); | ||
} | ||
|
||
public string? Value | ||
{ | ||
get => GetValue(ValueProperty); | ||
set => SetValue(ValueProperty, value); | ||
} | ||
|
||
public IBrush? Brush | ||
{ | ||
get => GetValue(BrushProperty); | ||
set => SetValue(BrushProperty, value); | ||
} | ||
|
||
public IPen? Pen | ||
{ | ||
get => GetValue(PenProperty); | ||
set => SetValue(PenProperty, value); | ||
} | ||
|
||
/// <inheritdoc> | ||
public Size Size => _bounds.Size; | ||
|
||
/// <inheritdoc/> | ||
void IImage.Draw( | ||
DrawingContext context, | ||
Rect sourceRect, | ||
Rect destRect) | ||
{ | ||
var drawing = _drawing; | ||
if (drawing == null) | ||
{ | ||
return; | ||
} | ||
|
||
var bounds = _bounds; | ||
var scale = Matrix.CreateScale( | ||
destRect.Width / sourceRect.Width, | ||
destRect.Height / sourceRect.Height); | ||
var translate = Matrix.CreateTranslation( | ||
-sourceRect.X + destRect.X - bounds.X, | ||
-sourceRect.Y + destRect.Y - bounds.Y); | ||
|
||
using (context.PushClip(destRect)) | ||
using (context.PushTransform(translate * scale)) | ||
{ | ||
drawing.Draw(context); | ||
} | ||
} | ||
|
||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) | ||
{ | ||
base.OnPropertyChanged(change); | ||
if (change.Property == ValueProperty) | ||
{ | ||
HandleValueChanged(); | ||
} | ||
else if (change.Property == BrushProperty) | ||
{ | ||
HandleBrushChanged(); | ||
} | ||
else if (change.Property == PenProperty) | ||
{ | ||
HandlePenChanged(); | ||
} | ||
} | ||
|
||
private void HandleValueChanged() | ||
{ | ||
var iconModel = IconProvider.Current.GetIcon(Value); | ||
|
||
_bounds = new Rect( | ||
x: iconModel.ViewBox.X, | ||
y: iconModel.ViewBox.Y, | ||
width: iconModel.ViewBox.Width, | ||
height: iconModel.ViewBox.Height); | ||
|
||
var drawing = GetGeometryDrawing(); | ||
drawing.Geometry = StreamGeometry.Parse(iconModel.Path); | ||
} | ||
|
||
private void HandleBrushChanged() | ||
{ | ||
var drawing = GetGeometryDrawing(); | ||
drawing.Brush = Brush; | ||
} | ||
|
||
private void HandlePenChanged() | ||
{ | ||
var drawing = GetGeometryDrawing(); | ||
drawing.Pen = Pen; | ||
} | ||
|
||
private GeometryDrawing GetGeometryDrawing() | ||
{ | ||
return _drawing ??= new GeometryDrawing(); | ||
} | ||
} | ||
} |
Oops, something went wrong.