Skip to content

Commit

Permalink
Add text support for extensions. Thanks, @sn4k3
Browse files Browse the repository at this point in the history
Merge pull request #39 from SKProCH/textExtensions
  • Loading branch information
SKProCH authored Jan 21, 2025
2 parents fc5ab12 + 9e4a557 commit fe5a4c5
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 19 deletions.
12 changes: 6 additions & 6 deletions Material.Icons.Avalonia.Demo/Material.Icons.Avalonia.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
<AvaloniaResource Include="Assets\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.0-rc1.1" />
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.0.0-rc1.1" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.0-rc1.1" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.0-rc1.1" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0-rc1.1" />
<PackageReference Include="Avalonia.Themes.Simple" Version="11.0.0-rc1.1" />
<PackageReference Include="Avalonia" Version="11.2.3" />
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.1.5" />
<PackageReference Include="Avalonia.Desktop" Version="11.2.3" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.2.3" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.2.3" />
<PackageReference Include="Avalonia.Themes.Simple" Version="11.2.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Material.Icons.Avalonia\Material.Icons.Avalonia.csproj" />
Expand Down
7 changes: 3 additions & 4 deletions Material.Icons.Avalonia/Material.Icons.Avalonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
<AssemblyOriginatorKeyFile>..\key.snk</AssemblyOriginatorKeyFile>
<PackageTags>material icons material-design google-material avalonia</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>- Icons set updated according to materialdesignicons.com at Mon, 20 Jan 2025 12:22:08 GMT
Check out changes at https://pictogrammers.com/library/mdi/history/</PackageReleaseNotes>
<Version>2.1.12</Version>
<PackageReleaseNotes>- Add text support for extensions. Thanks, @sn4k3</PackageReleaseNotes>
<Version>2.2.0</Version>
</PropertyGroup>

<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.0-rc1.1" />
<PackageReference Include="Avalonia" Version="11.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
58 changes: 58 additions & 0 deletions Material.Icons.Avalonia/MaterialIconTextExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;

namespace Material.Icons.Avalonia
{
public class MaterialIconTextExt : MaterialIconExt {
public MaterialIconTextExt() { }
public MaterialIconTextExt(MaterialIconKind kind) : base(kind) { }

public MaterialIconTextExt(MaterialIconKind kind, double? size) : base(kind, size) { }

[ConstructorArgument("spacing")]
public double Spacing { get; set; } = 5;

[ConstructorArgument("orientation")]
public Orientation Orientation { get; set; } = Orientation.Horizontal;

[ConstructorArgument("text")]
public string? Text { get; set; }

[ConstructorArgument("textfirst")]
public bool TextFirst { get; set; } = false;

[ConstructorArgument("isTextSelectable")]
public bool IsTextSelectable { get; set; } = false;

public override object ProvideValue(IServiceProvider serviceProvider) {
var icon = (Control)base.ProvideValue(serviceProvider);

if (string.IsNullOrWhiteSpace(Text)) return icon;

var textBlock = IsTextSelectable ?
new SelectableTextBlock {
Text = Text,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
}
: new TextBlock {
Text = Text,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};

if (Size.HasValue) textBlock.FontSize = Size.Value;

return new StackPanel {
Orientation = Orientation,
Spacing = Spacing,
Children = {
TextFirst ? textBlock : icon,
TextFirst ? icon : textBlock
}
};
}
}
}
5 changes: 2 additions & 3 deletions Material.Icons.WPF/Material.Icons.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
<PackageTags>material icons material-design google-material wpf</PackageTags>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\key.snk</AssemblyOriginatorKeyFile>
<Version>2.1.12</Version>
<PackageReleaseNotes>- Icons set updated according to materialdesignicons.com at Mon, 20 Jan 2025 12:22:08 GMT
Check out changes at https://pictogrammers.com/library/mdi/history/</PackageReleaseNotes>
<Version>2.2.0</Version>
<PackageReleaseNotes>- Add text support for extensions. Thanks, @sn4k3</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
64 changes: 64 additions & 0 deletions Material.Icons.WPF/MaterialIconTextExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace Material.Icons.WPF
{
[MarkupExtensionReturnType(typeof(MaterialIcon))]
public class MaterialIconTextExt : MaterialIconExt {
public MaterialIconTextExt() { }
public MaterialIconTextExt(MaterialIconKind kind) : base(kind) { }

public MaterialIconTextExt(MaterialIconKind kind, double size) : base(kind, size) { }

[ConstructorArgument("spacing")]
public double Spacing { get; set; } = 5;

[ConstructorArgument("orientation")]
public Orientation Orientation { get; set; } = Orientation.Horizontal;

[ConstructorArgument("text")]
public string? Text { get; set; }

[ConstructorArgument("textfirst")]
public bool TextFirst { get; set; } = false;

public override object ProvideValue(IServiceProvider serviceProvider) {
var icon = (Control)base.ProvideValue(serviceProvider);

if (string.IsNullOrWhiteSpace(Text))
return icon;

var textBlock = new TextBlock {
Text = Text,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};

if (Size.HasValue)
textBlock.FontSize = Size.Value;

if (Spacing > 0) {
if (TextFirst) {
textBlock.Margin = Orientation == Orientation.Horizontal
? new Thickness(0, 0, Spacing, 0)
: new Thickness(0, 0, 0, Spacing);
}
else {
textBlock.Margin = Orientation == Orientation.Horizontal
? new Thickness(Spacing, 0, 0, 0)
: new Thickness(0, Spacing, 0, 0);
}
}

return new StackPanel {
Orientation = Orientation,
Children = {
TextFirst ? textBlock : icon,
TextFirst ? icon : textBlock
}
};
}
}
}
5 changes: 2 additions & 3 deletions Material.Icons.WinUI3/Material.Icons.WinUI3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
<PackageTags>material icons material-design google-material wpf</PackageTags>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\key.snk</AssemblyOriginatorKeyFile>
<Version>2.1.12</Version>
<PackageReleaseNotes>- Icons set updated according to materialdesignicons.com at Mon, 20 Jan 2025 12:22:08 GMT
Check out changes at https://pictogrammers.com/library/mdi/history/</PackageReleaseNotes>
<Version>2.2.0</Version>
<PackageReleaseNotes>- Add text support for extensions. Thanks, @sn4k3</PackageReleaseNotes>
<EnableMsixTooling>true</EnableMsixTooling>

<WindowsSdkPackageVersion>10.0.22621.38</WindowsSdkPackageVersion>
Expand Down
42 changes: 42 additions & 0 deletions Material.Icons.WinUI3/MaterialIconTextExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Material.Icons.WinUI3;

public partial class MaterialIconTextExt : MaterialIconExt {
public MaterialIconTextExt() { }
public MaterialIconTextExt(MaterialIconKind kind) : base(kind) { }

public MaterialIconTextExt(MaterialIconKind kind, double size) : base(kind, size) { }

public double Spacing { get; set; } = 5;

public Orientation Orientation { get; set; } = Orientation.Horizontal;

public string? Text { get; set; }

public bool TextFirst { get; set; } = false;

protected override object ProvideValue(IXamlServiceProvider serviceProvider) {
var icon = (Control)base.ProvideValue(serviceProvider);

if (string.IsNullOrWhiteSpace(Text)) return icon;

var textBlock = new TextBlock {
Text = Text,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};

if (Size.HasValue) textBlock.FontSize = Size.Value;

return new StackPanel {
Orientation = Orientation,
Spacing = Spacing,
Children = {
TextFirst ? textBlock : icon,
TextFirst ? icon : textBlock
}
};
}
}
5 changes: 2 additions & 3 deletions Material.Icons/Material.Icons.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>material icons material-design google-material</PackageTags>
<RootNamespace>Material.Icons</RootNamespace>
<Version>2.1.12</Version>
<PackageReleaseNotes>- Icons set updated according to materialdesignicons.com at Mon, 20 Jan 2025 12:22:08 GMT
Check out changes at https://pictogrammers.com/library/mdi/history/</PackageReleaseNotes>
<Version>2.2.0</Version>
<PackageReleaseNotes>- Add text support for extensions. Thanks, @sn4k3</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ Also, there is `MaterialIconExt` which allows you to use is as the markup extens
```xaml
<Button Content="{materialIcons:MaterialIconExt Kind=Abacus}" />
```
Or with a text:
```xaml
<Button Content="{materialIcons:MaterialIconExt Kind=Play, Text=Play}" />
```

## Avalonia FuncUI (F#)
#### Getting started
Expand Down

0 comments on commit fe5a4c5

Please sign in to comment.