Skip to content

Commit 1c3ecc7

Browse files
mohnjilesclaude
andcommitted
Image viewer: refresh ImageSource template key before navigating
ImageSource.TemplateKey defaults to Default when constructed from a URL; the image viewer's template selector uses that key to pick between the static-image control and the animated-WebP control. Without the key being refreshed, animated WebP previews fall through to the static control and render as "Unsupported format" — which is exactly what happened when navigating between images with arrow keys. Same fix the CivitAI flow already applies. Extract a shared PrepareImageSourceAsync helper that does GetBitmapAsync + GetOrRefreshTemplateKeyAsync + a defensive try/catch (so a single bad image doesn't break the whole navigation chain). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 0ddad70 commit 1c3ecc7

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CivArchiveDetailsPageViewModel.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ private async Task ShowImageDialog(CivArchiveModelImage? image)
418418
}
419419

420420
var currentIndex = Images.IndexOf(image);
421-
var imageSource = new ImageSource(new Uri(image.Url));
422-
423-
await imageSource.GetBitmapAsync();
421+
var imageSource = await PrepareImageSourceAsync(image.Url);
422+
if (imageSource is null)
423+
return;
424424

425425
var vm = vmFactory.Get<ImageViewerViewModel>();
426426
vm.ImageSource = imageSource;
@@ -447,8 +447,10 @@ private async Task ShowImageDialog(CivArchiveModelImage? image)
447447
return;
448448
}
449449

450-
var newSource = new ImageSource(new Uri(newImage.Url));
451-
await newSource.GetBitmapAsync();
450+
var newSource = await PrepareImageSourceAsync(newImage.Url);
451+
if (newSource is null)
452+
return;
453+
452454
sender.ImageSource = newSource;
453455
currentIndex = newIndex;
454456
}
@@ -459,6 +461,27 @@ private async Task ShowImageDialog(CivArchiveModelImage? image)
459461
await vm.GetDialog().ShowAsync();
460462
}
461463

464+
/// <summary>
465+
/// Build an <see cref="ImageSource"/> ready for the image viewer to render. Critically,
466+
/// <see cref="ImageSource.GetOrRefreshTemplateKeyAsync"/> must run before assigning so the
467+
/// viewer's template selector picks the right control for animated WebPs vs static images
468+
/// — otherwise navigation to certain images shows "Unsupported format".
469+
/// </summary>
470+
private static async Task<ImageSource?> PrepareImageSourceAsync(string url)
471+
{
472+
try
473+
{
474+
var source = new ImageSource(new Uri(url));
475+
await source.GetBitmapAsync();
476+
await source.GetOrRefreshTemplateKeyAsync();
477+
return source;
478+
}
479+
catch
480+
{
481+
return null;
482+
}
483+
}
484+
462485
[RelayCommand]
463486
private async Task SelectVersion(CivArchiveVersionReference? versionRef)
464487
{

0 commit comments

Comments
 (0)