Skip to content

Commit 2b94b64

Browse files
Colin MackayColin Mackay
authored andcommitted
WIP
1 parent 74c635e commit 2b94b64

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/.idea/.idea.Stravaig.Avalonia.Controls.Icons/.idea/avalonia.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Stravaig.Avalonia.Controls.Icons/PhosphorIcon/PhosphorIcon.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using Avalonia;
33
using Avalonia.Controls;
4+
using Avalonia.Media;
45
using Avalonia.Svg.Skia;
6+
using Svg.Model;
57

68
namespace Stravaig.Avalonia.Controls.Icons;
79

@@ -19,6 +21,10 @@ public class PhosphorIcon : Image
1921
AvaloniaProperty.Register<PhosphorIcon, PhosphorIconName>(
2022
nameof(IconName));
2123

24+
public static readonly StyledProperty<HsvColor> ColorProperty =
25+
AvaloniaProperty.Register<PhosphorIcon, HsvColor>(
26+
nameof(Color));
27+
2228
public PhosphorIconType IconType
2329
{
2430
get => GetValue(IconTypeProperty);
@@ -31,10 +37,17 @@ public PhosphorIconName IconName
3137
set => SetValue(IconNameProperty, value);
3238
}
3339

40+
public HsvColor Color
41+
{
42+
get => GetValue(ColorProperty);
43+
set => SetValue(ColorProperty, value);
44+
}
45+
3446
static PhosphorIcon()
3547
{
3648
IconTypeProperty.Changed.AddClassHandler<PhosphorIcon>((x, _) => x.UpdateImageSource());
3749
IconNameProperty.Changed.AddClassHandler<PhosphorIcon>((x, _) => x.UpdateImageSource());
50+
ColorProperty.Changed.AddClassHandler<PhosphorIcon>((x, _) => x.UpdateImageSource());
3851
}
3952

4053
public PhosphorIcon()
@@ -55,12 +68,16 @@ private void UpdateImageSource()
5568
return;
5669
}
5770

58-
// Delegate actual SVG rendering to Avalonia's Image via SvgImage
5971
try
6072
{
73+
var css = BuildCss();
74+
Console.WriteLine($"Loading {resource}\n with CSS: {css}");
75+
var sourceImage = SvgSource.Load(resource);
6176
var svg = new SvgImage
6277
{
63-
Source = SvgSource.Load(resource),
78+
Source = sourceImage,
79+
Css = css,
80+
//CurrentCss = css,
6481
};
6582
Source = svg;
6683
}
@@ -69,4 +86,11 @@ private void UpdateImageSource()
6986
Source = null;
7087
}
7188
}
89+
90+
private string BuildCss()
91+
{
92+
var rgb = Color.ToRgb();
93+
var css = $"svg {{ fill: rgb({rgb.R}, {rgb.G}, {rgb.B}); }}";
94+
return css;
95+
}
7296
}

src/Stravaig.Icons.Example/ViewModels/PhosphorIconDemoViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public PhosphorIconDemoViewModel()
3636
_selectedIconTypeIndex = 0;
3737
_selectedIconType = IconTypes[0].Key;
3838
_iconSize = 24;
39-
_colour = new HsvColor(Color.FromRgb(0x80, 0x80, 0x80));
39+
_colour = new HsvColor(Color.FromRgb(0xFF, 0x00, 0x00));
4040
_rgbColour = _colour.ToRgb().ToString();
4141
}
4242

@@ -46,7 +46,7 @@ public PhosphorIconDemoViewModel()
4646

4747
partial void OnColourChanged(HsvColor value)
4848
{
49-
RgbColour = Colour.ToRgb().ToString();
49+
RgbColour = value.ToRgb().ToString();
5050
}
5151

5252
partial void OnSelectedIconTypeIndexChanged(int? value)

src/Stravaig.Icons.Example/Views/PhosphorIconDemoView.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
HorizontalAlignment="Left">
9898
<StackPanel Orientation="Vertical">
9999
<icons:PhosphorIcon
100+
Color="{Binding #PhosphorIconDemo.((vm:PhosphorIconDemoViewModel)DataContext).Colour, FallbackValue=Gray}"
100101
IconName="{Binding Key}"
101102
IconType="{Binding #PhosphorIconDemo.((vm:PhosphorIconDemoViewModel)DataContext).SelectedIconType, FallbackValue={x:Static icons:PhosphorIconType.Regular}}"
102103
Width="{Binding #PhosphorIconDemo.((vm:PhosphorIconDemoViewModel)DataContext).IconSize, FallbackValue=24}"

0 commit comments

Comments
 (0)