Skip to content

Commit

Permalink
Use file-scoped namespace declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxuang committed Apr 7, 2024
1 parent d0872c1 commit 1c16419
Show file tree
Hide file tree
Showing 21 changed files with 3,575 additions and 3,590 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ indent_style = space
indent_size = 4
trim_trailing_whitespace = true

csharp_style_namespace_declarations = file_scoped

[*.{js,ts,jsx,tsx,py,json,yml,yaml,toml,svg}]
indent_size = 2

Expand Down
13 changes: 6 additions & 7 deletions FluentIcons.Avalonia.Fluent/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Avalonia;

namespace FluentIcons.Avalonia.Fluent
namespace FluentIcons.Avalonia.Fluent;

public static class Extensions
{
public static class Extensions
public static AppBuilder UseSegoeMetrics(this AppBuilder builder)
{
public static AppBuilder UseSegoeMetrics(this AppBuilder builder)
{
SymbolIcon.UseSegoeMetricsDefaultValue = true;
return builder;
}
SymbolIcon.UseSegoeMetricsDefaultValue = true;
return builder;
}
}
265 changes: 132 additions & 133 deletions FluentIcons.Avalonia.Fluent/SymbolIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,165 +10,164 @@
using FluentIcons.Common.Internals;
using Symbol = FluentIcons.Common.Symbol;

namespace FluentIcons.Avalonia.Fluent
namespace FluentIcons.Avalonia.Fluent;

[TypeConverter(typeof(SymbolIconConverter))]
public partial class SymbolIcon : FAIconElement
{
[TypeConverter(typeof(SymbolIconConverter))]
public partial class SymbolIcon : FAIconElement
internal static readonly Typeface System = new("avares://FluentIcons.Avalonia.Fluent/Assets#Fluent System Icons");
internal static readonly Typeface Seagull = new("avares://FluentIcons.Avalonia.Fluent/Assets#Seagull Fluent Icons");
internal static bool UseSegoeMetricsDefaultValue = false;

public static readonly StyledProperty<Symbol> SymbolProperty =
AvaloniaProperty.Register<SymbolIcon, Symbol>(nameof(Symbol), Symbol.Home);
public static readonly StyledProperty<bool> IsFilledProperty =
AvaloniaProperty.Register<SymbolIcon, bool>(nameof(IsFilled));
public static readonly StyledProperty<bool> UseSegoeMetricsProperty =
AvaloniaProperty.Register<SymbolIcon, bool>(nameof(UseSegoeMetrics));
public static readonly StyledProperty<double> FontSizeProperty =
AvaloniaProperty.Register<SymbolIcon, double>(nameof(FontSize), 20d, false);

private TextLayout? _textLayout;

public SymbolIcon()
{
internal static readonly Typeface System = new("avares://FluentIcons.Avalonia.Fluent/Assets#Fluent System Icons");
internal static readonly Typeface Seagull = new("avares://FluentIcons.Avalonia.Fluent/Assets#Seagull Fluent Icons");
internal static bool UseSegoeMetricsDefaultValue = false;

public static readonly StyledProperty<Symbol> SymbolProperty =
AvaloniaProperty.Register<SymbolIcon, Symbol>(nameof(Symbol), Symbol.Home);
public static readonly StyledProperty<bool> IsFilledProperty =
AvaloniaProperty.Register<SymbolIcon, bool>(nameof(IsFilled));
public static readonly StyledProperty<bool> UseSegoeMetricsProperty =
AvaloniaProperty.Register<SymbolIcon, bool>(nameof(UseSegoeMetrics));
public static readonly StyledProperty<double> FontSizeProperty =
AvaloniaProperty.Register<SymbolIcon, double>(nameof(FontSize), 20d, false);

private TextLayout? _textLayout;

public SymbolIcon()
{
UseSegoeMetrics = UseSegoeMetricsDefaultValue;
}
UseSegoeMetrics = UseSegoeMetricsDefaultValue;
}

public Symbol Symbol
{
get => GetValue(SymbolProperty);
set => SetValue(SymbolProperty, value);
}
public Symbol Symbol
{
get => GetValue(SymbolProperty);
set => SetValue(SymbolProperty, value);
}

public bool IsFilled
{
get => GetValue(IsFilledProperty);
set => SetValue(IsFilledProperty, value);
}
public bool IsFilled
{
get => GetValue(IsFilledProperty);
set => SetValue(IsFilledProperty, value);
}

public bool UseSegoeMetrics
{
get => GetValue(UseSegoeMetricsProperty);
set => SetValue(UseSegoeMetricsProperty, value);
}
public bool UseSegoeMetrics
{
get => GetValue(UseSegoeMetricsProperty);
set => SetValue(UseSegoeMetricsProperty, value);
}

public double FontSize
{
get => GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public double FontSize
{
get => GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}

protected override void OnLoaded(RoutedEventArgs e)
{
InvalidateText();
base.OnLoaded(e);
}

protected override void OnLoaded(RoutedEventArgs e)
protected override void OnUnloaded(RoutedEventArgs e)
{
_textLayout?.Dispose();
_textLayout = null;
base.OnUnloaded(e);
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
if (change.Property == FontSizeProperty)
{
InvalidateMeasure();
InvalidateText();
base.OnLoaded(e);
}

protected override void OnUnloaded(RoutedEventArgs e)
else if (change.Property == ForegroundProperty ||
change.Property == SymbolProperty ||
change.Property == IsFilledProperty ||
change.Property == UseSegoeMetricsProperty)
{
_textLayout?.Dispose();
_textLayout = null;
base.OnUnloaded(e);
InvalidateText();
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
if (change.Property == FontSizeProperty)
{
InvalidateMeasure();
InvalidateText();
}
else if (change.Property == ForegroundProperty ||
change.Property == SymbolProperty ||
change.Property == IsFilledProperty ||
change.Property == UseSegoeMetricsProperty)
{
InvalidateText();
}

base.OnPropertyChanged(change);
}
base.OnPropertyChanged(change);
}

protected override Size MeasureOverride(Size availableSize)
{
double size = FontSize;
return new Size(
Width.Or(
HorizontalAlignment == HorizontalAlignment.Stretch
? availableSize.Width.Or(size)
: Math.Min(availableSize.Width, size)),
Height.Or(
VerticalAlignment == VerticalAlignment.Stretch
? availableSize.Height.Or(size)
: Math.Min(availableSize.Height, size)));
}
protected override Size MeasureOverride(Size availableSize)
{
double size = FontSize;
return new Size(
Width.Or(
HorizontalAlignment == HorizontalAlignment.Stretch
? availableSize.Width.Or(size)
: Math.Min(availableSize.Width, size)),
Height.Or(
VerticalAlignment == VerticalAlignment.Stretch
? availableSize.Height.Or(size)
: Math.Min(availableSize.Height, size)));
}

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

public override void Render(DrawingContext context)
double size = FontSize;
Rect bounds = Bounds;
using (context.PushClip(new Rect(bounds.Size)))
{
if (_textLayout is null)
return;

double size = FontSize;
Rect bounds = Bounds;
using (context.PushClip(new Rect(bounds.Size)))
{
var origin = new Point(
HorizontalAlignment switch
{
HorizontalAlignment.Left => 0,
HorizontalAlignment.Right => bounds.Width - size,
_ => (bounds.Width - size) / 2,
},
VerticalAlignment switch
{
VerticalAlignment.Top => 0,
VerticalAlignment.Bottom => bounds.Height - size,
_ => (bounds.Height - size) / 2,
});
_textLayout.Draw(context, origin);
}
var origin = new Point(
HorizontalAlignment switch
{
HorizontalAlignment.Left => 0,
HorizontalAlignment.Right => bounds.Width - size,
_ => (bounds.Width - size) / 2,
},
VerticalAlignment switch
{
VerticalAlignment.Top => 0,
VerticalAlignment.Bottom => bounds.Height - size,
_ => (bounds.Height - size) / 2,
});
_textLayout.Draw(context, origin);
}
}

private void InvalidateText()
private void InvalidateText()
{
if (!IsLoaded)
return;

_textLayout?.Dispose();
_textLayout = new TextLayout(
Symbol.ToString(IsFilled, FlowDirection == FlowDirection.RightToLeft),
UseSegoeMetrics ? Seagull : System,
FontSize,
Foreground,
TextAlignment.Center);

InvalidateVisual();
}
}

public class SymbolIconConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
if (sourceType == typeof(string) || sourceType == typeof(Symbol))
{
if (!IsLoaded)
return;

_textLayout?.Dispose();
_textLayout = new TextLayout(
Symbol.ToString(IsFilled, FlowDirection == FlowDirection.RightToLeft),
UseSegoeMetrics ? Seagull : System,
FontSize,
Foreground,
TextAlignment.Center);

InvalidateVisual();
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public class SymbolIconConverter : TypeConverter
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
if (value is string val)
{
if (sourceType == typeof(string) || sourceType == typeof(Symbol))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
return new SymbolIcon { Symbol = (Symbol)Enum.Parse(typeof(Symbol), val) };
}

public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
else if (value is Symbol symbol)
{
if (value is string val)
{
return new SymbolIcon { Symbol = (Symbol)Enum.Parse(typeof(Symbol), val) };
}
else if (value is Symbol symbol)
{
return new SymbolIcon { Symbol = symbol };
}
return base.ConvertFrom(context, culture, value);
return new SymbolIcon { Symbol = symbol };
}
return base.ConvertFrom(context, culture, value);
}
}
Loading

0 comments on commit 1c16419

Please sign in to comment.