Skip to content

Commit

Permalink
Migrate to common pipelines, bump dependencies in demo
Browse files Browse the repository at this point in the history
  • Loading branch information
SKProCH committed Aug 17, 2024
1 parent f6fbf1e commit f02aaab
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 111 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Build and publish

on:
push:
branches:
- master
- main
- release/**
paths-ignore:
- Material.Avalonia.Demo*/**
tags:
- v**

jobs:
build-and-test:
uses: SKProCH/CommonWorkflows/.github/workflows/build-publish.yml@main
secrets:
NUGET_KEY: ${{ secrets.NUGET_KEY }}
with:
publish-nightly: false
dotnet-version: 8
21 changes: 0 additions & 21 deletions .github/workflows/build.yml

This file was deleted.

24 changes: 0 additions & 24 deletions .github/workflows/publish.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<AvaloniaResource Include="Assets/avalonia-logo.ico" />
<None Remove=".gitignore" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.0" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.0" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.0" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0" />
<PackageReference Include="Avalonia" Version="11.1.3" />
<PackageReference Include="Avalonia.Desktop" Version="11.1.3" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.1.3" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.1.3" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.1.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AsyncImageLoader.Avalonia\AsyncImageLoader.Avalonia.csproj" />
Expand Down
117 changes: 62 additions & 55 deletions AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Avalonia.Media.Imaging;
using Avalonia.Platform;

namespace AsyncImageLoader;
namespace AsyncImageLoader;

public class AdvancedImage : ContentControl
{
Expand Down Expand Up @@ -177,16 +177,18 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
base.OnPropertyChanged(change);
}

private void ClearSourceIfUserProvideImage() {
if (CurrentImage is not null and not ImageWrapper) {
private void ClearSourceIfUserProvideImage()
{
if (CurrentImage is not null and not ImageWrapper)
{
// User provided image himself
Source = null;
}
}

private async void UpdateImage(string? source, IAsyncImageLoader? loader)
{
var cancellationTokenSource = new CancellationTokenSource();
var cancellationTokenSource = new CancellationTokenSource();

var oldCancellationToken = Interlocked.Exchange(ref _updateCancellationToken, cancellationTokenSource);

Expand All @@ -197,59 +199,59 @@ private async void UpdateImage(string? source, IAsyncImageLoader? loader)
catch (ObjectDisposedException)
{
}

if (source is null && CurrentImage is not ImageWrapper) {

if (source is null && CurrentImage is not ImageWrapper)
{
// User provided image himself
return;
}
IsLoading = true;
CurrentImage = null;

var bitmap = await Task.Run(async () =>
{
try
{
if (source == null)
return null;

// A small delay allows to cancel early if the image goes out of screen too fast (eg. scrolling)
// The Bitmap constructor is expensive and cannot be cancelled
await Task.Delay(10, cancellationTokenSource.Token);

// Hack to support relative URI
// TODO: Refactor IAsyncImageLoader to support BaseUri
try
{
var uri = new Uri(source, UriKind.RelativeOrAbsolute);
if (AssetLoader.Exists(uri, _baseUri))

IsLoading = true;
CurrentImage = null;


var bitmap = await Task.Run(async () =>
{
try
{
if (source == null)
return null;

// A small delay allows to cancel early if the image goes out of screen too fast (eg. scrolling)
// The Bitmap constructor is expensive and cannot be cancelled
await Task.Delay(10, cancellationTokenSource.Token);

// Hack to support relative URI
// TODO: Refactor IAsyncImageLoader to support BaseUri
try
{
var uri = new Uri(source, UriKind.RelativeOrAbsolute);
if (AssetLoader.Exists(uri, _baseUri))
return new Bitmap(AssetLoader.Open(uri, _baseUri));
}
catch (Exception)
{
// ignored
}

loader ??= ImageLoader.AsyncImageLoader;
return await loader.ProvideImageAsync(source);
}
catch (TaskCanceledException)
{
return null;
}
finally
{
cancellationTokenSource.Dispose();
}

}, CancellationToken.None);

if (cancellationTokenSource.IsCancellationRequested)
}
catch (Exception)
{
// ignored
}

loader ??= ImageLoader.AsyncImageLoader;
return await loader.ProvideImageAsync(source);
}
catch (TaskCanceledException)
{
return null;
}
finally
{
cancellationTokenSource.Dispose();
}
}, CancellationToken.None);

if (cancellationTokenSource.IsCancellationRequested)
return;
CurrentImage = bitmap is null ? null : new ImageWrapper(bitmap);
IsLoading = false;
}
}

private void UpdateCornerRadius(CornerRadius radius)
{
Expand Down Expand Up @@ -308,18 +310,23 @@ protected override Size ArrangeOverride(Size finalSize)
? Stretch.CalculateSize(finalSize, CurrentImage.Size)
: base.ArrangeOverride(finalSize);
}

public sealed class ImageWrapper : IImage {

public sealed class ImageWrapper : IImage
{
public IImage ImageImplementation { get; }
internal ImageWrapper(IImage imageImplementation) {

internal ImageWrapper(IImage imageImplementation)
{
ImageImplementation = imageImplementation;
}

/// <inheritdoc />
public void Draw(DrawingContext context, Rect sourceRect, Rect destRect) {
public void Draw(DrawingContext context, Rect sourceRect, Rect destRect)
{
ImageImplementation.Draw(context, sourceRect, destRect);
}

/// <inheritdoc />
public Size Size => ImageImplementation.Size;
}
}
}
6 changes: 1 addition & 5 deletions AsyncImageLoader.Avalonia/AsyncImageLoader.Avalonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<RootNamespace>AsyncImageLoader</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

<Title>AsyncImageLoader.Avalonia</Title>
<Authors>SKProCH</Authors>
<Description>Provides way to asynchronous bitmap loading from web for Avalonia Image control and more</Description>
Expand All @@ -15,10 +15,6 @@
<RepositoryType>git</RepositoryType>
<PackageTags>image cross-platform avalonia avaloniaui c-sharp-library</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>3.2.1</Version>
<PackageReleaseNotes>
- Fix ObjectDisposedException in Cancellation token access (#19)
</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit f02aaab

Please sign in to comment.