Skip to content

Commit cb8f875

Browse files
committed
fixed blinking on image load
1 parent b0b20fa commit cb8f875

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

MacImageManager/MacImageManager/Views/PaneImageViewer.swift

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,43 @@ struct PaneImageViewer: View {
1212
@State private var loadedImage: NSImage?
1313
@State private var currentLoadingURL: URL?
1414
@State private var loadingTask: Task<Void, Never>?
15+
@State private var isLoading: Bool = false
1516

1617
var body: some View {
1718
Group {
1819
if selectedImage != nil {
19-
if let loadedImage = loadedImage, currentLoadingURL == selectedImage {
20-
ScrollView([.horizontal, .vertical]) {
21-
Image(nsImage: loadedImage)
22-
.resizable()
23-
.aspectRatio(contentMode: .fit) // These 2 lines: fit image to view
24-
.containerRelativeFrame([.horizontal, .vertical]) // These 2 lines: fit image to view
25-
}
26-
.background(Color.black)
27-
} else {
28-
ProgressView("Loading...")
29-
.frame(maxWidth: .infinity, maxHeight: .infinity)
20+
ZStack {
21+
// Show the loaded image (either current or previous)
22+
if let loadedImage = loadedImage {
23+
ScrollView([.horizontal, .vertical]) {
24+
Image(nsImage: loadedImage)
25+
.resizable()
26+
.aspectRatio(contentMode: .fit) // These 2 lines: fit image to view
27+
.containerRelativeFrame([.horizontal, .vertical]) // These 2 lines: fit image to view
28+
}
3029
.background(Color.black)
30+
} else {
31+
// Only show black background with spinner if no image has been loaded yet
32+
ProgressView("Loading...")
33+
.frame(maxWidth: .infinity, maxHeight: .infinity)
34+
.background(Color.black)
35+
}
36+
37+
// Show loading indicator in corner when loading a new image but previous image exists
38+
if isLoading && loadedImage != nil {
39+
VStack {
40+
HStack {
41+
Spacer()
42+
ProgressView()
43+
.scaleEffect(0.8)
44+
.padding()
45+
.background(Color.black.opacity(0.6))
46+
.clipShape(RoundedRectangle(cornerRadius: 6))
47+
.padding()
48+
}
49+
Spacer()
50+
}
51+
}
3152
}
3253
} else {
3354
VStack {
@@ -58,21 +79,26 @@ struct PaneImageViewer: View {
5879
guard let url = url else {
5980
loadedImage = nil
6081
currentLoadingURL = nil
82+
isLoading = false
6183
return
6284
}
6385

6486
// Don't reload if it's the exact same URL and we already have the image loaded for it
6587
if currentLoadingURL == url && loadedImage != nil {
88+
isLoading = false
6689
return
6790
}
6891

69-
// Set the new loading URL and clear image if switching URLs
92+
// Set the new loading URL but keep the previous image
7093
let previousURL = currentLoadingURL
7194
currentLoadingURL = url
7295

73-
// Clear the loaded image if we're switching to a different URL
74-
if previousURL != url {
75-
loadedImage = nil
96+
// Only clear the loaded image if we don't have any image yet
97+
if loadedImage == nil {
98+
isLoading = true
99+
} else {
100+
// We have a previous image, show loading indicator but keep the image
101+
isLoading = true
76102
}
77103

78104
loadingTask = Task {
@@ -88,6 +114,7 @@ struct PaneImageViewer: View {
88114
if !Task.isCancelled && currentLoadingURL == url {
89115
await MainActor.run {
90116
self.loadedImage = image
117+
self.isLoading = false
91118
}
92119
}
93120
}

0 commit comments

Comments
 (0)