-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from wmchuang/master
feat: ImageBrushLoader
- Loading branch information
Showing
2 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |