Skip to content

Mini Gallery Fixes #344

Description

@nikimagic

Mini Gallery Fixes

Problem
When a new image file is created in the currently viewed folder by an external program, PicView receives the FileSystemWatcher.Created event and adds a new item to the bottom gallery.

Sometimes the newly added gallery item appears as an empty or incorrectly sized square.

Cause 1: missing DataContext
During normal gallery loading, items are created in:

src/PicView.Avalonia/Gallery/GalleryLoad.cs

var galleryItem = new GalleryItem
{
DataContext = vm,
...
};
However, when an item is added through the file watcher path, it is created in:

src/PicView.Avalonia/Gallery/GalleryFunctions.cs

galleryItem = new GalleryItem
{
FileLocation = { Text = galleryThumbInfo.FileLocation },
...
};
In that code path, DataContext = vm was missing.

GalleryItem.axaml uses compiled bindings that require MainViewModel, for example:

Height="{CompiledBinding Gallery.GalleryItem.ItemHeight.Value}"
Width="{CompiledBinding Gallery.GalleryItem.ItemWidth.Value}"
Stretch="{CompiledBinding Gallery.GalleryStretch.Value}"
Without DataContext = vm, items added through FileSystemWatcher.Created do not receive the same size and stretch bindings as items created during normal gallery loading.

Fix 1
Add DataContext = vm in GalleryFunctions.AddGalleryItem:

galleryItem = new GalleryItem
{
DataContext = vm,
FileLocation = { Text = galleryThumbInfo.FileLocation },
FileDate = { Text = galleryThumbInfo.FileDate },
FileSize = { Text = galleryThumbInfo.FileSize },
FileName = { Text = galleryThumbInfo.FileName }
};
Cause 2: FileSystemWatcher may fire too early
FileSystemWatcher.Created can be raised before the external process has finished writing and closed the file.

In that case, thumbnail generation may try to read a partially written or temporarily locked file. GetThumbnails.GetThumbAsync then catches the failure and returns null, leaving the gallery item empty.

Fix 2
Before generating the thumbnail in GalleryFunctions.AddGalleryItem, wait briefly until the file is ready:

the file exists;
the file can be opened for reading;
the file size is stable between checks.
This reduces the chance of empty thumbnails when images are copied, downloaded, or created by another program.

Changed file
src/PicView.Avalonia/Gallery/GalleryFunctions.cs

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions