From 35ff0f508cf9cb52048f2ed67bd07f4aec17f2bf Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 22 Jul 2025 08:15:07 +0300 Subject: Fix GUI background image generation race conditions - Add file.Sync() after image download to ensure data is flushed to disk - Add double-checking in UI updates to prevent wrong card updates - Fix background job completion to reload files when user navigates back - Add file size validation in image display widget - Improve error messages for image loading failures This fixes two issues: 1. 'png: invalid format: not enough pixel data' error when navigating during generation 2. Images not updating when navigating back to a card after background generation completes --- internal/gui/widgets.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'internal/gui/widgets.go') diff --git a/internal/gui/widgets.go b/internal/gui/widgets.go index 6ad428c..df74027 100644 --- a/internal/gui/widgets.go +++ b/internal/gui/widgets.go @@ -73,7 +73,20 @@ func (d *ImageDisplay) SetImage(imagePath string) { } defer file.Close() - img, _, err := image.Decode(file) + // Get file info to ensure it's fully written + stat, err := file.Stat() + if err != nil { + d.imageLabel.SetText(fmt.Sprintf("Error getting file info: %v", err)) + return + } + + // If file size is 0, it might still be writing + if stat.Size() == 0 { + d.imageLabel.SetText("Image file is empty") + return + } + + img, format, err := image.Decode(file) if err != nil { d.imageLabel.SetText(fmt.Sprintf("Error decoding image: %v", err)) return @@ -83,8 +96,8 @@ func (d *ImageDisplay) SetImage(imagePath string) { d.imageCanvas.Image = img d.imageCanvas.Refresh() - // Update label - d.imageLabel.SetText(filepath.Base(imagePath)) + // Update label with format info + d.imageLabel.SetText(fmt.Sprintf("%s (%s)", filepath.Base(imagePath), format)) } // SetImages sets multiple images but only displays the first one -- cgit v1.2.3