Skip to content

Commit

Permalink
Adaptive annotation Thumbnail scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Neakita committed Oct 23, 2024
1 parent 0b56dfd commit 00c1125
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion SightKeeper.Avalonia/Annotation/ScreenshotViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private unsafe WriteableBitmap LoadImage()
return bitmap;
}

private unsafe WriteableBitmap LoadImage(int maximumLargestDimension)
internal unsafe WriteableBitmap LoadImage(int maximumLargestDimension)
{
PixelSize size = ComputeThumbnailSize(maximumLargestDimension);
WriteableBitmap bitmap = _bitmapPool.Rent(size, PixelFormat.Rgb32);
Expand Down
22 changes: 15 additions & 7 deletions SightKeeper.Avalonia/Annotation/Screenshots.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SightKeeper.Avalonia.Annotation"
xmlns:behaviors="clr-namespace:SightKeeper.Avalonia.Behaviors"
xmlns:instance="clr-namespace:SightKeeper.Avalonia"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SightKeeper.Avalonia.Annotation.Screenshots"
x:DataType="local:ScreenshotsViewModel">
Expand All @@ -18,13 +19,20 @@
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image Name="Image"
Source="{Binding Thumbnail}"
Stretch="UniformToFill">
<Interaction.Behaviors>
<behaviors:ExecuteCommandOnSourceChanged Command="{Binding ReturnBitmapToPoolCommand}"/>
</Interaction.Behaviors>
</Image>
<Decorator Name="ImageContainer">
<Image Name="Image"
Stretch="UniformToFill">
<Interaction.Behaviors>
<behaviors:ExecuteCommandOnSourceChanged Command="{Binding ReturnBitmapToPoolCommand}"/>
</Interaction.Behaviors>
<Image.Source>
<MultiBinding Converter="{x:Static instance:ScreenshotToScaledBitmapConverter.Instance}">
<Binding/>
<Binding Path="#ImageContainer.Bounds.Size"/>
</MultiBinding>
</Image.Source>
</Image>
</Decorator>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Expand Down
36 changes: 36 additions & 0 deletions SightKeeper.Avalonia/ScreenshotToScaledBitmapConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Avalonia;
using Avalonia.Data.Converters;
using SightKeeper.Avalonia.Annotation;

namespace SightKeeper.Avalonia;

internal sealed class ScreenshotToScaledBitmapConverter : IMultiValueConverter
{
private const int SizeStep = 20;
private const int MinimumSize = 20;

public static ScreenshotToScaledBitmapConverter Instance { get; } = new();

public object Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{
if (values[0] is not ScreenshotViewModel screenshot ||
values[1] is not Size targetSize ||
targetSize.Width == 0 ||
targetSize.Height == 0)
return AvaloniaProperty.UnsetValue;
var largestSizeDimension = Math.Max(targetSize.Width, targetSize.Height);
var roundedLargestSizeDimension = RoundSize(largestSizeDimension);
return screenshot.LoadImage(roundedLargestSizeDimension);
}

private static int RoundSize(double size)
{
var rounded = (int)Math.Round(size / SizeStep) * SizeStep;
if (rounded < MinimumSize)
return MinimumSize;
return rounded;
}
}

0 comments on commit 00c1125

Please sign in to comment.