Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

User-visible changes to NX Library are recorded here. Entries are grouped by release, with the newest first. Engine changes also have their own [upgrade notes](docs/upstream-upgrades/README.md).

## Unreleased

- Game icons load in the background and are cached locally between launches, so opening the collection can show a placeholder while a network game folder is slow or unavailable.

## 0.1.4.3 — 2026-11-12

### Added
Expand Down
2 changes: 1 addition & 1 deletion Sources/NXLibrary/ViewModels/AppViewModel+AppIcon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ extension AppViewModel {
self.appIconProgress[game.id] = again ? .updating : status
if changed {
Self.refreshIcon(of: app)
GameIconProvider.forget(game)
GameIconProvider.shared.forget(game)
GameArtLoader.shared.forget(game)
ArtworkPicks.shared.touch()
self.logs.append(LogEntry(level: .info, message: L10n.format("Updated the app icon of %@.", [String(describing: (game.name))])))
Expand Down
3 changes: 2 additions & 1 deletion Sources/NXLibrary/Views/AddGame/AddGameStepThreeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import AppKit
import NXLibraryCore

struct AddGameStepThreeView: View {
@ObservedObject private var gameIcons = GameIconProvider.shared
@ObservedObject var viewModel: AppViewModel
@ObservedObject var router: AppRouter
var artwork: ArtworkStore? = nil
Expand Down Expand Up @@ -74,7 +75,7 @@ struct AddGameStepThreeView: View {
if let artwork {
SpotlightTileArtwork(game: game, side: side, artwork: artwork, ownIconFirst: true)
} else {
Image(nsImage: GameIconProvider.squareArt(for: game) ?? GameIconProvider.icon(for: game))
Image(nsImage: gameIcons.squareArt(for: game) ?? gameIcons.icon(for: game))
.resizable()
.aspectRatio(contentMode: .fill)
}
Expand Down
69 changes: 0 additions & 69 deletions Sources/NXLibrary/Views/Components/GameIconProvider.swift

This file was deleted.

7 changes: 4 additions & 3 deletions Sources/NXLibrary/Views/Components/SpotlightTileArtwork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import NXLibraryCore
/// generic icon. Crossfades when fetched art arrives.
/// The caller applies the corner radius.
struct SpotlightTileArtwork: View {
@ObservedObject private var gameIcons = GameIconProvider.shared
let game: PortedGame
/// Side length in points, already scaled by the caller.
let side: CGFloat
Expand All @@ -29,7 +30,7 @@ struct SpotlightTileArtwork: View {
.resizable()
.interpolation(.high)
.aspectRatio(contentMode: .fill)
} else if ownIconFirst, let own = GameIconProvider.squareArt(for: game) {
} else if ownIconFirst, let own = gameIcons.squareArt(for: game) {
Image(nsImage: own)
.resizable()
.interpolation(.high)
Expand All @@ -40,7 +41,7 @@ struct SpotlightTileArtwork: View {
.aspectRatio(contentMode: .fill)
.transition(.opacity)
} else {
Image(nsImage: GameIconProvider.icon(for: game))
Image(nsImage: gameIcons.icon(for: game))
.resizable()
.aspectRatio(contentMode: .fill)
}
Expand All @@ -53,7 +54,7 @@ struct SpotlightTileArtwork: View {
return
}
picked = nil
guard !ownIconFirst || GameIconProvider.squareArt(for: game) == nil else { return }
guard !ownIconFirst || gameIcons.squareArt(for: game) == nil else { return }
let key = game.id.uuidString as NSString
if let memo = TileArtMemo.shared.object(forKey: key) {
better = memo
Expand Down
5 changes: 3 additions & 2 deletions Sources/NXLibrary/Views/Components/SwitchBoxArtCover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import NXLibraryCore
/// is found, or when GameTDB has none, a composed cover: the blurred Title DB
/// banner, the game's icon and its name. The caller applies the corner radius.
struct SwitchBoxArtCover: View {
@ObservedObject private var gameIcons = GameIconProvider.shared
let game: PortedGame
let width: CGFloat
let height: CGFloat
Expand Down Expand Up @@ -56,7 +57,7 @@ struct SwitchBoxArtCover: View {
}
LinearGradient(colors: [.black.opacity(0.05), .black.opacity(0.6)], startPoint: .top, endPoint: .bottom)
VStack(spacing: height * 0.05) {
Image(nsImage: icon ?? GameIconProvider.icon(for: game))
Image(nsImage: gameIcons.squareArt(for: game) ?? icon ?? gameIcons.icon(for: game))
.resizable()
.interpolation(.high)
.aspectRatio(contentMode: .fill)
Expand Down Expand Up @@ -106,7 +107,7 @@ struct SwitchBoxArtCover: View {

/// The banner and Title DB icon for the composed cover (both cached on disk).
private func loadComposedArt() async {
if let own = GameIconProvider.squareArt(for: game) {
if let own = gameIcons.squareArt(for: game) {
icon = own
} else if let titleId = game.titleId,
let url = await artwork.automaticIcon(titleID: titleId, name: game.name) {
Expand Down
3 changes: 2 additions & 1 deletion Sources/NXLibrary/Views/Detail/GameDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class DetailsModelCache {
/// with a title ID) and More like this, one band pitch (230) apart. A band below the one in focus peeks in, blurred, until
/// it is focused. Input lives in GameDetailView+Input.swift.
struct GameDetailView: View {
@ObservedObject private var gameIcons = GameIconProvider.shared
@ObservedObject var viewModel: AppViewModel
@ObservedObject var router: AppRouter
@ObservedObject var settings: AppSettingsStore
Expand Down Expand Up @@ -134,7 +135,7 @@ struct GameDetailView: View {
cache.model = model
return ZStack(alignment: .topLeading) {
DetailsBackground(image: art.image, imageKey: art.key,
fallback: GameIconProvider.icon(for: game),
fallback: gameIcons.icon(for: game),
shade: settings.settings.backgroundShade)
SeleneBottomScrim()
stage(model)
Expand Down
9 changes: 3 additions & 6 deletions Sources/NXLibrary/Views/Theme/ThemePreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import NXLibraryCore
/// a palette that need not be active yet, over its wallpaper or, without one,
/// its own moonlight backdrop. Tiles show the library's own square art.
struct ThemePreview: View {
@ObservedObject private var gameIcons = GameIconProvider.shared
@Environment(\.seleneCanvas) private var canvas
let palette: SelenePalette
let wallpaperURL: URL?
Expand All @@ -18,7 +19,6 @@ struct ThemePreview: View {
var shade: BackgroundShade = .balanced

@Environment(\.seleneBackdropStyle) private var style
@State private var art: [NSImage] = []
@State private var videoReady = false

var body: some View {
Expand Down Expand Up @@ -59,9 +59,6 @@ struct ThemePreview: View {
.animation(.easeOut(duration: 0.25), value: palette.id)
.animation(.easeOut(duration: 0.25), value: wallpaperURL)
}
.task(id: games.map(\.id)) {
art = games.prefix(6).map { GameIconProvider.squareArt(for: $0) ?? GameIconProvider.icon(for: $0) }
}
.accessibilityHidden(true)
}

Expand All @@ -80,8 +77,8 @@ struct ThemePreview: View {
ForEach(0..<6, id: \.self) { index in
ZStack {
glass
if index < art.count {
Image(nsImage: art[index]).resizable().interpolation(.high).aspectRatio(contentMode: .fill)
if index < games.count {
Image(nsImage: gameIcons.icon(for: games[index])).resizable().interpolation(.high).aspectRatio(contentMode: .fill)
}
}
.frame(width: tile, height: tile)
Expand Down
138 changes: 138 additions & 0 deletions Sources/NXLibraryCore/Services/GameIconProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import AppKit
import Combine
import ImageIO

/// View bodies only consult memory. Disk hits run independently of slow bundle reads so an
/// unavailable share can't hold cached artwork for other games behind it.
@MainActor
public final class GameIconProvider: ObservableObject {
public static let shared = GameIconProvider()
private let placeholder = NSImage(systemSymbolName: "gamecontroller.fill", accessibilityDescription: nil)
?? NSImage(size: NSSize(width: 64, height: 64))
private let directory: URL
private let disk = DispatchQueue(label: "local.nxlibrary.icon-cache", qos: .utility)
private let sources = OperationQueue()
private let read: @Sendable (Revision) -> Data?
private let images = NSCache<NSUUID, NSImage>()
private var entries: [UUID: Entry] = [:]

struct Revision: Codable, Equatable, Sendable {
let titleId: String?
let app: URL
let date: Date
let workspace: String?
}

private struct Thumbnail: Codable {
let revision: Revision
let png: Data
}

private struct Entry {
let revision: Revision
let token = UUID()
var loading = true
var retryAfter = Date.distantPast
}

init(directory: URL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
.appendingPathComponent("NXLibrary/GameIcons", isDirectory: true),
read: @escaping @Sendable (Revision) -> Data? = { GameIconProvider.readThumbnail($0) }) {
self.directory = directory
self.read = read
sources.maxConcurrentOperationCount = 2
sources.qualityOfService = .utility
images.countLimit = 128
}

public func icon(for game: PortedGame) -> NSImage {
squareArt(for: game) ?? placeholder
}

public func squareArt(for game: PortedGame) -> NSImage? {
let revision = Revision(titleId: game.titleId, app: game.url, date: game.modificationDate, workspace: game.workspacePath)
let id = game.id
if let entry = entries[id], entry.revision == revision {
if let image = images.object(forKey: id as NSUUID) { return image }
if entry.loading || entry.retryAfter > Date() { return nil }
}

let entry = Entry(revision: revision)
entries[id] = entry
images.removeObject(forKey: id as NSUUID)
let file = cacheFile(id)
let read = self.read
let sources = self.sources
disk.async {
if let data = try? Data(contentsOf: file),
let thumbnail = try? PropertyListDecoder().decode(Thumbnail.self, from: data),
thumbnail.revision == revision, let image = NSImage(data: thumbnail.png) {
Task { @MainActor in self.finish(image, id: id, entry: entry) }
return
}
sources.addOperation {
let data = read(revision)
let image = data.flatMap(NSImage.init(data:))
Task { @MainActor in
guard self.entries[id]?.token == entry.token else { return }
if let data, image != nil {
self.disk.async {
try? FileManager.default.createDirectory(at: file.deletingLastPathComponent(),
withIntermediateDirectories: true)
if let record = try? PropertyListEncoder().encode(Thumbnail(revision: revision, png: data)) {
try? record.write(to: file, options: .atomic)
}
}
}
self.finish(image, id: id, entry: entry)
}
}
}
return nil
}

private func finish(_ image: NSImage?, id: UUID, entry: Entry) {
guard entries[id]?.token == entry.token else { return }
entries[id]?.loading = false
if let image {
images.setObject(image, forKey: id as NSUUID)
objectWillChange.send()
} else {
entries[id]?.retryAfter = Date().addingTimeInterval(30)
}
}

private func cacheFile(_ id: UUID) -> URL {
directory.appendingPathComponent(id.uuidString, isDirectory: false)
}

public func forget(_ game: PortedGame) {
entries.removeValue(forKey: game.id)
images.removeObject(forKey: game.id as NSUUID)
let file = cacheFile(game.id)
disk.async { try? FileManager.default.removeItem(at: file) }
objectWillChange.send()
}

nonisolated static func readThumbnail(_ revision: Revision) -> Data? {
var urls: [URL] = []
if revision.app.pathExtension.lowercased() == "app", let bundle = Bundle(url: revision.app) {
let name = bundle.object(forInfoDictionaryKey: "CFBundleIconFile") as? String ?? "AppIcon"
let file = (name as NSString).pathExtension.isEmpty ? name + ".icns" : name
urls.append(revision.app.appendingPathComponent("Contents/Resources/" + file))
}
if let path = revision.workspace, let id = revision.titleId, !id.isEmpty {
urls.append(URL(fileURLWithPath: path).appendingPathComponent("\(id)_build/extract/v0/icon.jpg"))
}
let options: [CFString: Any] = [kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: 512]
for url in urls {
if let source = CGImageSourceCreateWithURL(url as CFURL, nil),
let image = CGImageSourceCreateThumbnailAtIndex(source, 0, options as CFDictionary) {
return NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])
}
}
return nil
}
}
Loading