Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ImageBrushLoader #10

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions AsyncImageLoader.Avalonia.Demo/Pages/AttachedPropertiesPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,44 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AsyncImageLoader.Avalonia.Demo.Pages.AttachedPropertiesPage">
<StackPanel Orientation="Vertical">
<StackPanel.Styles>
<Style Selector="Image">
<Setter Property="Width" Value="400" />
<Setter Property="Height" Value="300" />
</Style>
<Style Selector="Border">
<Setter Property="Width" Value="400" />
<Setter Property="Height" Value="300" />
</Style>
</StackPanel.Styles>
<TextBlock TextAlignment="Center">Images from Internet</TextBlock>
<StackPanel Orientation="Horizontal">
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat0.jpg" />
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat1.jpg" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat2.jpg" />
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat3.jpg" />
<Border>
<Border.Background>
<ImageBrush asyncImageLoader:ImageBrushLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat0.jpg"></ImageBrush>
</Border.Background>
</Border>
<Border>
<Border.Background>
<ImageBrush asyncImageLoader:ImageBrushLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat1.jpg"></ImageBrush>
</Border.Background>
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Border>
<Border.Background>
<ImageBrush asyncImageLoader:ImageBrushLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat2.jpg"></ImageBrush>
</Border.Background>
</Border>
<Border>
<Border.Background>
<ImageBrush asyncImageLoader:ImageBrushLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat3.jpg"></ImageBrush>
</Border.Background>
</Border>
</StackPanel>
<TextBlock TextAlignment="Center">Images from Assets (AvaloniaResource)</TextBlock>
<StackPanel Orientation="Horizontal">
Expand Down
48 changes: 48 additions & 0 deletions AsyncImageLoader.Avalonia/ImageBrushLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Reactive.Linq;
using AsyncImageLoader.Loaders;
using Avalonia;
using Avalonia.Media;

namespace AsyncImageLoader {
public static class ImageBrushLoader {
public static IAsyncImageLoader AsyncImageLoader { get; set; } = new RamCachedWebImageLoader();
static ImageBrushLoader() {
SourceProperty.Changed
.Where(args => args.IsEffectiveValueChange)
.Subscribe(args => OnSourceChanged((ImageBrush)args.Sender, args.NewValue.Value));
}

private static async void OnSourceChanged(ImageBrush sender, string? url) {
SetIsLoading(sender, true);

var bitmap = url == null
? null
: await AsyncImageLoader.ProvideImageAsync(url);
if (GetSource(sender) != url) return;
sender.Source = bitmap;

SetIsLoading(sender, false);
}

public static readonly AttachedProperty<string?> SourceProperty = AvaloniaProperty.RegisterAttached<ImageBrush, string?>("Source", typeof(ImageLoader));

public static string? GetSource(ImageBrush element) {
return element.GetValue(SourceProperty);
}

public static void SetSource(ImageBrush element, string? value) {
element.SetValue(SourceProperty, value);
}

public static readonly AttachedProperty<bool> IsLoadingProperty = AvaloniaProperty.RegisterAttached<ImageBrush, bool>("IsLoading", typeof(ImageLoader));

public static bool GetIsLoading(ImageBrush element) {
return element.GetValue(IsLoadingProperty);
}

private static void SetIsLoading(ImageBrush element, bool value) {
element.SetValue(IsLoadingProperty, value);
}
}
}