comparison foosdk/sdk/foobar2000/helpers/albumArtCache.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2
3 #include <functional>
4 #include <SDK/titleformat.h>
5 #include <SDK/timer.h>
6
7 namespace fb2k {
8 class albumArtCache {
9 public:
10 albumArtCache( imageSize_t s = imageSizeMake(0,0) ) : m_tfSplit(titleformat_patterns::patternAlbumSplit()), imageSize(s), timeOut(0) {
11 }
12 bool isLoading() const { return m_imageLoader.is_valid(); }
13 bool isLoadingExpired() const { return timeOut > 0 && m_imageLoader.is_valid() && m_timeOutTimer.is_empty(); }
14 bool isLoadingNonExpired() const { return isLoading() && !isLoadingExpired(); }
15 pfc::string8 albumIDOfTrack( trackRef trk ) {
16 pfc::string8 pattern;
17 if (trk.is_valid()) {
18 trk->format_title(nullptr, pattern, m_tfSplit, nullptr);
19 }
20
21 return pattern;
22 }
23
24 std::function<void () > onLoaded;
25 imageSize_t imageSize;
26 double timeOut;
27 imageRef getImage( trackRef trk, std::function<void (imageRef)> asyncRecv = nullptr ) {
28 auto key = albumIDOfTrack( trk );
29 if (strcmp(key, m_imageKey) != 0) {
30 m_timeOutTimer.release();
31 m_imageKey = key;
32 m_image.release();
33 m_imageLoader.release();
34
35 imageLocation_t loc;
36 if (loc.setTrack2( trk ) ) {
37 auto recv = makeObjReceiver( [this, asyncRecv] (objRef obj) {
38 m_image ^= obj;
39 if (m_imageLoader.is_valid()) {
40 m_imageLoader.release();
41 m_timeOutTimer.release();
42 if (onLoaded) onLoaded();
43 if (asyncRecv) asyncRecv(m_image);
44 }
45 } );
46 m_imageLoader = imageLoader::get()->beginLoad(loc, this->imageSize, recv);
47
48 if (m_imageLoader.is_valid() && timeOut > 0) {
49 m_timeOutTimer = fb2k::registerTimer( timeOut, [=, this] {
50 m_timeOutTimer.release();
51 if (onLoaded) onLoaded();
52 if (asyncRecv) asyncRecv(nullptr);
53 } );
54 }
55 }
56 }
57 return m_image;
58 }
59 void getImage2( trackRef trk, std::function<void (imageRef)> recv ) {
60 auto img = getImage( trk, recv );
61 if (! this->isLoading() ) recv( img );
62 }
63 void reset() {
64 m_imageKey = "";
65 m_image.release();
66 m_imageLoader.release();
67 m_timeOutTimer.release();
68 }
69 imageRef current() const { return m_image; }
70 private:
71
72 titleformat_object_cache m_tfSplit;
73 pfc::string8 m_imageKey;
74 imageRef m_image;
75 objRef m_imageLoader, m_timeOutTimer;
76 };
77 }