Skip to content

Commit

Permalink
Switched texture previews to convert images to Avalonia bitmaps async…
Browse files Browse the repository at this point in the history
…hronously.
  • Loading branch information
MeltyPlayer committed Nov 29, 2024
1 parent de195ae commit 794582a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,8 @@ namespace uni.ui.avalonia.common.progress;
public class AsyncPanelViewModelForDesigner
: AsyncPanelViewModel {
public AsyncPanelViewModelForDesigner() {
this.Progress = new AsyncProgress();

var secondsToWait = 3;
var start = DateTime.Now;

Task.Run(
async () => {
DateTime current;
double elapsedSeconds;
do {
current = DateTime.Now;
elapsedSeconds = (current - start).TotalSeconds;
await Task.Delay(50);
} while (elapsedSeconds < secondsToWait);

this.Progress.ReportCompletion("Hello world!");
});
this.Progress = AsyncProgress.FromTask(
Task.Delay(3000).ContinueWith(_ => Task.FromResult("Hello world!")));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

using fin.util.progress;

Expand All @@ -12,6 +13,21 @@ public class AsyncProgress
: ViewModelBase, IMutableIndeterminateProgressValue<object> {
private bool isComplete_;

public static AsyncProgress FromResult<T>(T t) {
var progress = new AsyncProgress();
progress.ReportCompletion(t!);
return progress;
}

public static AsyncProgress FromTask<T>(Task<T> t) {
var progress = new AsyncProgress();
Task.Run(() => {
t.Wait();
progress.ReportCompletion(t.Result!);
});
return progress;
}

public object? Value { get; private set; }

public void ReportCompletion(object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:textures="clr-namespace:uni.ui.avalonia.resources.texture"
xmlns:progress="clr-namespace:uni.ui.avalonia.common.progress"
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="100"
x:Class="uni.ui.avalonia.resources.texture.TexturePreview"
x:DataType="textures:TexturePreviewViewModel">
Expand All @@ -23,11 +24,16 @@
BorderBrush="{StaticResource BorderBrush}"
Background="{StaticResource ColorControlCheckeredBackgroundBrush}" />

<Image x:Name="image_"
Margin="{Binding ImageMargin}"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Source="{Binding Image}"
RenderOptions.BitmapInterpolationMode="None" />
<progress:AsyncPanel DataContext="{Binding ImageAsyncPanelViewModel}">
<progress:AsyncPanel.DataTemplate>
<DataTemplate x:DataType="progress:AsyncProgress">
<Image Margin="{Binding Value.Margin}"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Source="{Binding Value.Image}"
RenderOptions.BitmapInterpolationMode="None" />
</DataTemplate>
</progress:AsyncPanel.DataTemplate>
</progress:AsyncPanel>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Drawing;
using System.IO;
using System.Threading.Tasks;

using Avalonia;
using Avalonia.Controls;
Expand All @@ -15,6 +16,7 @@

using SixLabors.ImageSharp.PixelFormats;

using uni.ui.avalonia.common.progress;
using uni.ui.avalonia.icons;
using uni.ui.avalonia.resources.model;
using uni.ui.avalonia.ViewModels;
Expand All @@ -30,6 +32,18 @@ public TexturePreviewViewModelForDesigner() {
}
}

public class AsyncImageViewModel : ViewModelBase {
public Bitmap Image {
get;
set => this.RaiseAndSetIfChanged(ref field, value);
}

public Thickness Margin {
get;
set => this.RaiseAndSetIfChanged(ref field, value);
}
}

public class TexturePreviewViewModel : ViewModelBase {
private static readonly Bitmap missingImage_
= FinImage.Create1x1FromColor(Color.Magenta).AsAvaloniaImage();
Expand All @@ -38,11 +52,18 @@ public required IReadOnlyTexture? Texture {
get;
set {
this.RaiseAndSetIfChanged(ref field, value);
this.Image = value?.AsMergedMipmapAvaloniaImage() ?? missingImage_;
this.ImageAsyncPanelViewModel = new AsyncPanelViewModel {
Progress = AsyncProgress.FromTask(
Task.Run(() => new AsyncImageViewModel {
Image = value?.AsMergedMipmapAvaloniaImage() ??
missingImage_,
Margin = this.ImageMargin
}))
};
}
}

public Bitmap Image {
public AsyncPanelViewModel ImageAsyncPanelViewModel {
get;
private set => this.RaiseAndSetIfChanged(ref field, value);
}
Expand Down

0 comments on commit 794582a

Please sign in to comment.