Skip to content

Commit

Permalink
Merge pull request #10 from wmchuang/master
Browse files Browse the repository at this point in the history
feat: ImageBrushLoader
  • Loading branch information
SKProCH authored May 22, 2023
2 parents badc101 + 19753b4 commit fea2e75
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
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);
}
}
}

0 comments on commit fea2e75

Please sign in to comment.