-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_element.cpp
More file actions
2476 lines (2052 loc) · 97.1 KB
/
ui_element.cpp
File metadata and controls
2476 lines (2052 loc) · 97.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include <windowsx.h>
#include "artwork_manager.h"
#include "artwork_viewer_popup.h"
#include "metadata_cleaner.h"
#include "webp_decoder.h"
#include <gdiplus.h>
#include <atlbase.h>
#include <atlwin.h>
#include <algorithm>
#include <regex>
#include <vector>
// Include CUI color system for proper foobar2000 theme colors
#include "columns_ui/columns_ui-sdk/colours.h"
// infobar
#pragma comment(lib, "gdiplus.lib")
// External configuration variables
extern cfg_bool cfg_enable_custom_logos;
extern cfg_string cfg_logos_folder;
extern cfg_bool cfg_clear_panel_when_not_playing;
extern cfg_bool cfg_use_noart_image;
extern cfg_bool cfg_show_osd;
extern cfg_bool cfg_infobar;
// External custom logo loading functions
extern HBITMAP load_station_logo(metadb_handle_ptr track);
extern Gdiplus::Bitmap* load_station_logo_gdiplus(metadb_handle_ptr track);
extern HBITMAP load_noart_logo(metadb_handle_ptr track);
extern std::unique_ptr<Gdiplus::Bitmap> load_noart_logo_gdiplus(metadb_handle_ptr track);
extern HBITMAP load_generic_noart_logo(metadb_handle_ptr track);
extern std::unique_ptr<Gdiplus::Bitmap> load_generic_noart_logo_gdiplus();
// External functions for triggering main component search (same as CUI)
extern void trigger_main_component_search(metadb_handle_ptr track);
extern void trigger_main_component_search_with_metadata(const std::string& artist, const std::string& title);
extern void trigger_main_component_local_search(metadb_handle_ptr track);
// External function to get main component artwork bitmap (for priority checking)
extern HBITMAP get_main_component_artwork_bitmap();
// Global list to track DUI artwork element instances for external refresh
static pfc::list_t<class artwork_ui_element*> g_dui_artwork_panels;
// Forward declarations for the event system (matching sdk_main.cpp)
enum class ArtworkEventType {
ARTWORK_LOADED, // New artwork loaded successfully
ARTWORK_LOADING, // Search started
ARTWORK_FAILED, // Search failed
ARTWORK_CLEARED // Artwork cleared
};
struct ArtworkEvent {
ArtworkEventType type;
HBITMAP bitmap;
std::string source;
std::string artist;
std::string title;
ArtworkEvent(ArtworkEventType t, HBITMAP bmp = nullptr, const std::string& src = "",
const std::string& art = "", const std::string& ttl = "")
: type(t), bitmap(bmp), source(src), artist(art), title(ttl) {}
};
// Artwork event listener interface
class IArtworkEventListener {
public:
virtual ~IArtworkEventListener() = default;
virtual void on_artwork_event(const ArtworkEvent& event) = 0;
};
// Forward declare the event manager class
class ArtworkEventManager {
public:
static ArtworkEventManager& get();
void subscribe(IArtworkEventListener* listener);
void unsubscribe(IArtworkEventListener* listener);
void notify(const ArtworkEvent& event);
};
// External references to event manager methods
extern void subscribe_to_artwork_events(IArtworkEventListener* listener);
extern void unsubscribe_from_artwork_events(IArtworkEventListener* listener);
// Custom message for artwork loading completion
#define WM_USER_ARTWORK_LOADED (WM_USER + 100)
// Custom message for thread-safe artwork event dispatching
#define WM_USER_ARTWORK_EVENT (WM_USER + 101)
// Heap-allocated struct for passing artwork event data via PostMessage
struct ArtworkEventData {
ArtworkEventType type;
HBITMAP bitmap;
std::string source;
std::string artist;
std::string title;
};
// For now, we'll create a simplified version without ATL dependencies
// This creates a basic component that can be extended later
class artwork_ui_element : public ui_element_instance, public CWindowImpl<artwork_ui_element>, public IArtworkEventListener {
public:
artwork_ui_element(ui_element_config::ptr cfg, ui_element_instance_callback::ptr callback);
virtual ~artwork_ui_element();
DECLARE_WND_CLASS_EX(L"foo_artwork_ui_element", CS_DBLCLKS, NULL);
BEGIN_MSG_MAP(artwork_ui_element)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
MESSAGE_HANDLER(WM_MOUSELEAVE, OnMouseLeave)
MESSAGE_HANDLER(WM_SETCURSOR, OnSetCursor)
MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
MESSAGE_HANDLER(WM_LBUTTONDBLCLK, OnLButtonDblClk)
MESSAGE_HANDLER(WM_USER_ARTWORK_LOADED, OnArtworkLoaded)
MESSAGE_HANDLER(WM_USER_ARTWORK_EVENT, OnArtworkEvent)
MESSAGE_HANDLER(WM_TIMER, OnTimer)
END_MSG_MAP()
// ui_element_instance methods
HWND get_wnd() override { return m_hWnd; }
void set_configuration(ui_element_config::ptr cfg) override {}
ui_element_config::ptr get_configuration() override {
return ui_element_config::g_create_empty(get_guid());
}
GUID get_guid() override {
return GUID { 0x12345690, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf8 } };
}
GUID get_subclass() override {
return ui_element_subclass_utility;
}
void initialize_window(HWND parent);
void shutdown();
void notify(const GUID& p_what, t_size p_param1, const void* p_param2, t_size p_param2size) override;
// IArtworkEventListener implementation
void on_artwork_event(const ArtworkEvent& event) override;
// Getter for artwork source (for main component access)
std::string get_artwork_source() const { return m_artwork_source; }
// service_base implementation
int service_add_ref() throw() { return 1; }
int service_release() throw() { return 1; }
bool service_query(service_ptr & p_out, const GUID & p_guid) override {
if (p_guid == ui_element_instance::class_guid) {
p_out = this;
return true;
}
return false;
}
// Artwork handling (public for external refresh)
void on_playback_new_track(metadb_handle_ptr track);
private:
// Message handlers
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnMouseLeave(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnSetCursor(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnLButtonDblClk(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnArtworkLoaded(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnArtworkEvent(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
void on_dynamic_info_track(const file_info& p_info);
void on_artwork_loaded(const artwork_manager::artwork_result& result);
void start_artwork_search();
void update_artwork_display();
void clear_artwork();
// Drawing
void draw_artwork(HDC hdc, const RECT& rect);
void draw_placeholder(HDC hdc, const RECT& rect);
// GDI+ helpers
bool load_image_from_memory(const t_uint8* data, size_t size);
void cleanup_gdiplus_image();
// OSD functions
void show_osd(const std::string& text);
void hide_osd();
void update_osd_animation();
void update_clear_panel_timer(); // Start/stop clear panel monitoring timer
void load_noart_image(); // Load noart image for "use noart image" option
void paint_osd(HDC hdc);
// Timer functions
bool is_internet_stream(metadb_handle_ptr track);
bool is_stream_with_possible_artwork(metadb_handle_ptr track);
bool is_youtube_stream(metadb_handle_ptr track);
void start_delayed_search();
// Metadata validation and cleaning
bool is_metadata_valid_for_search(const char* artist, const char* title);
std::string clean_metadata_for_search(const char* metadata);
// Local artwork priority checking
bool should_prefer_local_artwork();
bool load_local_artwork_from_main_component();
// Inverted stream detection
bool is_inverted_internet_stream(metadb_handle_ptr track, const file_info& p_info);
ui_element_instance_callback::ptr m_callback;
// Artwork data
Gdiplus::Image* m_artwork_image;
IStream* m_artwork_stream = nullptr;
IStream* m_infobar_stream = nullptr;
metadb_handle_ptr m_current_track;
bool m_artwork_loading;
// Delayed search metadata storage
std::string m_delayed_artist;
std::string m_delayed_title;
bool m_has_delayed_metadata;
//Metadata infobar
void clear_infobar();
void cleanup_gdiplus_infobar_image();
std::wstring stringToWstring(const std::string& str) {
if (str.empty()) {
return std::wstring();
}
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), NULL, 0);
if (size_needed == 0) {
return std::wstring();
}
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
std::wstring m_infobar_artist;
std::wstring m_infobar_title;
std::wstring m_infobar_album;
std::wstring m_infobar_station;
std::wstring m_infobar_result;
Gdiplus::Bitmap* m_infobar_image = nullptr;
Gdiplus::Bitmap* m_infobar_bitmap = nullptr;
// Stream dynamic info metadata storage
void clear_dinfo();
std::string m_dinfo_artist;
std::string m_dinfo_title;
//First run counter
void reset_m_counter();
int m_counter = 0;
// Download icon hover state
bool m_mouse_hovering;
bool m_hover_over_download;
RECT m_download_icon_rect;
BYTE m_download_fade_alpha;
UINT_PTR m_download_fade_timer_id;
// UI state
RECT m_client_rect;
// GDI+ token
ULONG_PTR m_gdiplus_token;
// OSD (On-Screen Display) system
bool m_show_osd;
std::string m_osd_text;
std::string m_artwork_source;
DWORD m_osd_start_time;
int m_osd_slide_offset;
UINT_PTR m_osd_timer_id;
bool m_osd_visible;
bool m_was_playing; // Track previous playback state for clear panel functionality
// OSD constants
static const int OSD_DELAY_DURATION = 1000; // 1 second delay before animation starts
static const int OSD_DURATION_MS = 5000; // 5 seconds visible duration
static const int OSD_ANIMATION_SPEED = 8; // 120 FPS: 1000ms / 120fps ≈ 8ms
static const int OSD_SLIDE_DISTANCE = 200;
static const int OSD_SLIDE_IN_DURATION = 300; // 300ms smooth slide in
static const int OSD_SLIDE_OUT_DURATION = 300; // 300ms smooth slide out
// Playback callback
class playback_callback_impl : public play_callback {
public:
playback_callback_impl(artwork_ui_element* parent) : m_parent(parent) {}
void on_playback_new_track(metadb_handle_ptr p_track) override {
if (m_parent) {
m_parent->on_playback_new_track(p_track);
}
}
void on_playback_stop(play_control::t_stop_reason p_reason) override {
// Only clear artwork if the preference is enabled (like CUI)
if (m_parent && cfg_clear_panel_when_not_playing) {
m_parent->clear_artwork();
}
m_parent->clear_infobar();
m_parent->clear_dinfo();
m_parent->reset_m_counter();
}
// Required by play_callback base class
void on_playback_starting(play_control::t_track_command p_command, bool p_paused) override {}
void on_playback_seek(double p_time) override {}
void on_playback_pause(bool p_state) override {}
void on_playback_edited(metadb_handle_ptr p_track) override {}
void on_playback_dynamic_info(const file_info& p_info) override {
}
void on_playback_dynamic_info_track(const file_info& p_info) override {
if (m_parent) m_parent->on_dynamic_info_track(p_info);
}
void on_playback_time(double p_time) override {}
void on_volume_change(float p_new_val) override {}
void set_parent(artwork_ui_element* parent) { m_parent = parent; }
private:
artwork_ui_element* m_parent;
};
playback_callback_impl m_playback_callback;
};
//=============================================================================
// Download icon overlay helper
//=============================================================================
static void draw_download_icon(HDC hdc, const RECT& client_rect, bool hovered, RECT& out_icon_rect, BYTE fade_alpha = 255)
{
HMODULE hGrab = GetModuleHandle(L"foo_artgrab.dll");
if (!hGrab || fade_alpha == 0) {
SetRectEmpty(&out_icon_rect);
return;
}
const int icon_size = 24;
const int padding = 10;
const int bg_pad = 6;
int ix = client_rect.left + padding;
int iy = client_rect.bottom - padding - icon_size;
RECT bg = { ix - bg_pad, iy - bg_pad, ix + icon_size + bg_pad, iy + icon_size + bg_pad };
Gdiplus::Graphics g(hdc);
g.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
BYTE base_alpha = hovered ? (BYTE)200 : (BYTE)120;
BYTE alpha = (BYTE)((int)base_alpha * fade_alpha / 255);
Gdiplus::SolidBrush bgBrush(Gdiplus::Color(alpha, 0, 0, 0));
int radius = 6;
{
Gdiplus::GraphicsPath path;
Gdiplus::RectF rf((Gdiplus::REAL)bg.left, (Gdiplus::REAL)bg.top,
(Gdiplus::REAL)(bg.right - bg.left), (Gdiplus::REAL)(bg.bottom - bg.top));
float d = (float)radius * 2.0f;
path.AddArc(rf.X, rf.Y, d, d, 180, 90);
path.AddArc(rf.X + rf.Width - d, rf.Y, d, d, 270, 90);
path.AddArc(rf.X + rf.Width - d, rf.Y + rf.Height - d, d, d, 0, 90);
path.AddArc(rf.X, rf.Y + rf.Height - d, d, d, 90, 90);
path.CloseFigure();
g.FillPath(&bgBrush, &path);
}
float scale = (float)icon_size / 960.0f;
float ox = (float)ix;
float oy = (float)iy + (float)icon_size;
BYTE icon_alpha = (BYTE)((int)230 * fade_alpha / 255);
Gdiplus::SolidBrush iconBrush(Gdiplus::Color(icon_alpha, 255, 255, 255));
// Arrow portion
{
Gdiplus::GraphicsPath arrow;
Gdiplus::PointF pts[] = {
{ ox + 480*scale, oy + (-320)*scale },
{ ox + 280*scale, oy + (-520)*scale },
{ ox + 336*scale, oy + (-578)*scale },
{ ox + 440*scale, oy + (-474)*scale },
{ ox + 440*scale, oy + (-800)*scale },
{ ox + 520*scale, oy + (-800)*scale },
{ ox + 520*scale, oy + (-474)*scale },
{ ox + 624*scale, oy + (-578)*scale },
{ ox + 680*scale, oy + (-520)*scale },
};
arrow.AddPolygon(pts, 9);
g.FillPath(&iconBrush, &arrow);
}
// Tray portion
{
Gdiplus::GraphicsPath tray;
Gdiplus::PointF pts[] = {
{ ox + 160*scale, oy + (-240)*scale },
{ ox + 160*scale, oy + (-360)*scale },
{ ox + 240*scale, oy + (-360)*scale },
{ ox + 240*scale, oy + (-280)*scale },
{ ox + 720*scale, oy + (-280)*scale },
{ ox + 720*scale, oy + (-360)*scale },
{ ox + 800*scale, oy + (-360)*scale },
{ ox + 800*scale, oy + (-240)*scale },
{ ox + 720*scale, oy + (-160)*scale },
{ ox + 240*scale, oy + (-160)*scale },
};
tray.AddPolygon(pts, 10);
g.FillPath(&iconBrush, &tray);
}
out_icon_rect = bg;
}
artwork_ui_element::artwork_ui_element(ui_element_config::ptr cfg, ui_element_instance_callback::ptr callback)
: m_callback(callback), m_artwork_image(nullptr), m_artwork_stream(nullptr), m_infobar_stream(nullptr),
m_artwork_loading(false), m_gdiplus_token(0), m_playback_callback(this),
m_show_osd(true), m_osd_start_time(0), m_osd_slide_offset(OSD_SLIDE_DISTANCE),
m_osd_timer_id(0), m_osd_visible(false), m_has_delayed_metadata(false), m_was_playing(false),
m_mouse_hovering(false), m_hover_over_download(false), m_download_icon_rect{},
m_download_fade_alpha(0), m_download_fade_timer_id(0) {
// Initialize GDI+
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_gdiplus_token, &gdiplusStartupInput, NULL);
// Subscribe to artwork events for proper API fallback
subscribe_to_artwork_events(this);
g_dui_artwork_panels.add_item(this);
// Start playback monitoring timer if clear panel option is enabled
// Note: We'll start the timer after window creation
SetRect(&m_client_rect, 0, 0, 0, 0);
// Initialize current playback state
static_api_ptr_t<playback_control> pc;
m_was_playing = pc->is_playing();
}
artwork_ui_element::~artwork_ui_element() {
g_dui_artwork_panels.remove_item(this);
// Unsubscribe from artwork events
unsubscribe_from_artwork_events(this);
cleanup_gdiplus_image();
cleanup_gdiplus_infobar_image();
if (m_gdiplus_token) {
Gdiplus::GdiplusShutdown(m_gdiplus_token);
}
}
// Re-trigger artwork lookup on all DUI panels using the now-playing track
void refresh_all_dui_artwork_panels() {
static_api_ptr_t<playback_control> pc;
metadb_handle_ptr track;
if (!pc->get_now_playing(track) || !track.is_valid()) return;
for (t_size i = 0; i < g_dui_artwork_panels.get_count(); i++) {
artwork_ui_element* panel = g_dui_artwork_panels[i];
if (panel && panel->m_hWnd) {
panel->on_playback_new_track(track);
}
}
}
void artwork_ui_element::initialize_window(HWND parent) {
Create(parent, NULL, NULL, WS_CHILD | WS_VISIBLE);
// Register for playback callbacks including dynamic info
m_playback_callback.set_parent(this);
static_api_ptr_t<play_callback_manager>()->register_callback(&m_playback_callback,
play_callback::flag_on_playback_new_track |
play_callback::flag_on_playback_stop |
play_callback::flag_on_playback_dynamic_info_track,
false);
// Load artwork for currently playing track
static_api_ptr_t<playback_control> pc;
metadb_handle_ptr track;
if (pc->get_now_playing(track)) {
on_playback_new_track(track);
}
}
void artwork_ui_element::shutdown() {
// Unregister callbacks
static_api_ptr_t<play_callback_manager>()->unregister_callback(&m_playback_callback);
m_playback_callback.set_parent(nullptr);
if (IsWindow()) {
DestroyWindow();
}
}
void artwork_ui_element::notify(const GUID& p_what, t_size p_param1, const void* p_param2, t_size p_param2size) {
// Handle color/theme change notifications
if (p_what == ui_element_notify_colors_changed || p_what == ui_element_notify_font_changed) {
// We use global colors and fonts - trigger a repaint whenever these change
if (IsWindow()) {
Invalidate();
}
}
}
LRESULT artwork_ui_element::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
if (m_download_fade_timer_id) {
KillTimer(1002);
m_download_fade_timer_id = 0;
}
cleanup_gdiplus_image();
bHandled = TRUE;
return 0;
}
LRESULT artwork_ui_element::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(&ps);
// Use double buffering to eliminate flicker during resizing
RECT client_rect;
GetClientRect(&client_rect);
// Create memory DC and bitmap for double buffering
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP memBitmap = CreateCompatibleBitmap(hdc, client_rect.right, client_rect.bottom);
HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, memBitmap);
// Paint to memory DC first (off-screen)
draw_artwork(memDC, client_rect);
// Draw download icon overlay when hovering (only when a track is playing, skip streams)
if (m_mouse_hovering || m_download_fade_alpha > 0) {
bool should_show = false;
if (m_mouse_hovering) {
static_api_ptr_t<playback_control> pc;
if (pc->is_playing()) {
bool is_stream = false;
if (m_current_track.is_valid()) {
pfc::string8 path = m_current_track->get_path();
is_stream = strstr(path.c_str(), "://") && !strstr(path.c_str(), "file://");
}
should_show = !is_stream;
}
}
if (should_show) {
// Fade in: jump to full opacity and stop any fade timer
if (m_download_fade_alpha < 255) {
m_download_fade_alpha = 255;
if (m_download_fade_timer_id) {
KillTimer(m_download_fade_timer_id);
m_download_fade_timer_id = 0;
}
}
draw_download_icon(memDC, client_rect, m_hover_over_download, m_download_icon_rect, m_download_fade_alpha);
} else if (m_download_fade_alpha > 0) {
// Fading out - draw at current fade alpha
draw_download_icon(memDC, client_rect, false, m_download_icon_rect, m_download_fade_alpha);
} else {
SetRectEmpty(&m_download_icon_rect);
}
}
// Paint OSD overlay on memory DC before copying to screen
if (m_osd_visible) {
paint_osd(memDC);
}
// Copy the entire off-screen buffer to screen in one operation (flicker-free)
BitBlt(hdc, 0, 0, client_rect.right, client_rect.bottom, memDC, 0, 0, SRCCOPY);
// Cleanup
SelectObject(memDC, oldBitmap);
DeleteObject(memBitmap);
DeleteDC(memDC);
EndPaint(&ps);
bHandled = TRUE;
return 0;
}
LRESULT artwork_ui_element::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
GetClientRect(&m_client_rect);
// Use RedrawWindow for flicker-free resizing instead of Invalidate()
RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_NOCHILDREN);
bHandled = TRUE;
return 0;
}
LRESULT artwork_ui_element::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
// Start the clear panel monitoring timer if enabled
update_clear_panel_timer();
bHandled = FALSE; // Let default processing continue
return 0;
}
LRESULT artwork_ui_element::OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
bHandled = TRUE;
return TRUE; // We handle all drawing in OnPaint
}
LRESULT artwork_ui_element::OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
if (!m_mouse_hovering) {
m_mouse_hovering = true;
TRACKMOUSEEVENT tme = { sizeof(tme), TME_LEAVE, m_hWnd, 0 };
TrackMouseEvent(&tme);
Invalidate(FALSE);
}
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
bool over = !IsRectEmpty(&m_download_icon_rect) &&
PtInRect(&m_download_icon_rect, pt);
if (over != m_hover_over_download) {
m_hover_over_download = over;
InvalidateRect(&m_download_icon_rect, FALSE);
}
bHandled = FALSE;
return 0;
}
LRESULT artwork_ui_element::OnMouseLeave(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
m_mouse_hovering = false;
m_hover_over_download = false;
// Start fade-out animation if icon was visible
if (m_download_fade_alpha > 0 && !m_download_fade_timer_id) {
m_download_fade_timer_id = SetTimer(1002, 16); // ~60 FPS
}
if (IsRectEmpty(&m_download_icon_rect)) {
Invalidate(FALSE);
}
bHandled = TRUE;
return 0;
}
LRESULT artwork_ui_element::OnSetCursor(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
if (LOWORD(lParam) == HTCLIENT && m_hover_over_download) {
SetCursor(LoadCursor(NULL, IDC_HAND));
bHandled = TRUE;
return TRUE;
}
bHandled = FALSE;
return 0;
}
LRESULT artwork_ui_element::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
if (!IsRectEmpty(&m_download_icon_rect) && PtInRect(&m_download_icon_rect, pt)) {
typedef void (*pfn_open)(const char*, const char*, const char*);
HMODULE hGrab = GetModuleHandle(L"foo_artgrab.dll");
pfn_open pOpen = hGrab ? (pfn_open)GetProcAddress(hGrab, "foo_artgrab_open") : nullptr;
if (pOpen && m_current_track.is_valid()) {
std::string artist, album;
int len = WideCharToMultiByte(CP_UTF8, 0, m_infobar_artist.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (len > 0) { artist.resize(len - 1); WideCharToMultiByte(CP_UTF8, 0, m_infobar_artist.c_str(), -1, artist.data(), len, nullptr, nullptr); }
len = WideCharToMultiByte(CP_UTF8, 0, m_infobar_album.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (len > 0) { album.resize(len - 1); WideCharToMultiByte(CP_UTF8, 0, m_infobar_album.c_str(), -1, album.data(), len, nullptr, nullptr); }
pfc::string8 file_path = m_current_track->get_path();
pOpen(artist.c_str(), album.c_str(), file_path.get_ptr());
}
bHandled = TRUE;
return 0;
}
bHandled = FALSE;
return 0;
}
LRESULT artwork_ui_element::OnLButtonDblClk(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
// Open artwork viewer popup on double-click
if (m_artwork_image) {
try {
// Create source info string
std::string source_info = m_artwork_source;
if (source_info.empty()) {
source_info = "Local file"; // Default assumption for unknown source
}
// Create and show the popup viewer
ArtworkViewerPopup* popup = new ArtworkViewerPopup(m_artwork_image, source_info);
if (popup) {
popup->ShowPopup(m_hWnd);
// Note: The popup will delete itself when closed
}
} catch (...) {
// Handle any errors silently
}
}
bHandled = TRUE;
return 0;
}
LRESULT artwork_ui_element::OnArtworkLoaded(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
// Safely handle artwork loading completion on UI thread
bHandled = TRUE;
std::unique_ptr<artwork_manager::artwork_result> result(reinterpret_cast<artwork_manager::artwork_result*>(lParam));
if (result) {
on_artwork_loaded(*result);
}
return 0;
}
LRESULT artwork_ui_element::OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
if (wParam == 1002) {
// Download icon fade-out animation
const BYTE fade_step = 20; // ~200ms total fade at 60fps
if (m_download_fade_alpha <= fade_step) {
m_download_fade_alpha = 0;
KillTimer(1002);
m_download_fade_timer_id = 0;
SetRectEmpty(&m_download_icon_rect);
} else {
m_download_fade_alpha -= fade_step;
}
Invalidate(FALSE);
bHandled = TRUE;
return 0;
} else if (wParam == m_osd_timer_id) {
// OSD animation timer
update_osd_animation();
bHandled = TRUE;
return 0;
} else if (wParam == 100) {
// Metadata arrival timer - no metadata received within grace period
KillTimer(100);
// CHECK: Only trigger fallback if we don't already have tagged artwork
if (m_artwork_image && !m_artwork_source.empty() && m_artwork_source == "Local artwork") {
bHandled = TRUE;
return 0;
}
// Skip API search for streams with no metadata - go directly to station logo fallback
if (m_current_track.is_valid() && is_internet_stream(m_current_track)) {
// Simulate ARTWORK_FAILED to trigger station logo fallback without API search
ArtworkEventManager::get().notify(ArtworkEvent(
ArtworkEventType::ARTWORK_FAILED,
nullptr,
"No metadata - skipped API search",
"",
""
));
}
bHandled = TRUE;
return 0;
} else if (wParam == 101) {
// Timer for delayed artwork search on radio streams
KillTimer(101);
start_delayed_search();
bHandled = TRUE;
return 0;
} else if (wParam == 102) {
// Timer ID 102 - playback state monitoring for clear panel functionality
static_api_ptr_t<playback_control> pc;
bool is_playing = pc->is_playing();
// If we were playing but now we're not, handle clear panel functionality
if (m_was_playing && !is_playing && cfg_clear_panel_when_not_playing) {
if (cfg_use_noart_image) {
// Load and display noart image instead of clearing
load_noart_image();
} else {
// Just clear the panel
clear_artwork();
}
}
// Update the previous state
m_was_playing = is_playing;
// If option is disabled, stop the timer
if (!cfg_clear_panel_when_not_playing) {
KillTimer(102);
}
bHandled = TRUE;
return 0;
}
bHandled = FALSE;
return 0;
}
void artwork_ui_element::on_playback_new_track(metadb_handle_ptr track) {
//Set counter to 1 to define first run
m_counter++;
// Check if it's an internet stream and custom logos enabled
if (is_internet_stream(track) && cfg_enable_custom_logos) {
pfc::string8 path = track->get_path();
pfc::string8 result = path;
for (size_t i = 0; i < result.length(); i++) {
if (result[i] == '/') { result.set_char(i, '-'); }
else if (result[i] == '\\') { result.set_char(i, '-'); }
else if (result[i] == '|') { result.set_char(i, '-'); }
else if (result[i] == ':') { result.set_char(i, '-'); }
else if (result[i] == '*') { result.set_char(i, 'x'); }
else if (result[i] == '"') { result.set_char(i, '\'\''); }
else if (result[i] == '<') { result.set_char(i, '_'); }
else if (result[i] == '>') { result.set_char(i, '_'); }
else if (result[i] == '?') { result.set_char(i, '_'); }
}
std::string str = "foo_artwork - Filename for Full URL Path Matching LOGO: ";
str.append(result);
const char* cstr = str.c_str();
//console log it for the user to know what filename to use
console::info(cstr);
}
//try to get infobar values
try {
const file_info& info = track->get_info_ref()->info();
std::string artist, title, album, station;
if (info.meta_get("ARTIST", 0)) {
artist = info.meta_get("ARTIST", 0);
}
if (info.meta_get("TITLE", 0)) {
title = info.meta_get("TITLE", 0);
}
if (info.meta_get("ALBUM", 0)) {
album = info.meta_get("ALBUM", 0);
}
if (info.meta_get("STREAM_NAME", 0)) {
station = info.meta_get("STREAM_NAME", 0);
}
//store metadata for infobar
m_infobar_artist = stringToWstring(artist);
m_infobar_title = stringToWstring(title);
m_infobar_album = stringToWstring(album);
m_infobar_station = stringToWstring(station);
//clear infobar logo
cleanup_gdiplus_infobar_image();
}
catch (...) {
// Handle any errors silently
}
m_current_track = track;
m_artwork_loading = true;
// Clear any pending delayed metadata from previous track
m_has_delayed_metadata = false;
m_delayed_artist.clear();
m_delayed_title.clear();
// Keep previous artwork visible until replaced (like CUI)
// Don't clear artwork here - let new artwork replace it when found
if (track.is_valid()) {
bool get_all_album_art_manager = true;
if (get_all_album_art_manager) {
// Get any art for all cases via album_art_manager_v2 , for radio display logo on startup
// Clear any existing artwork first to avoid conflicts
cleanup_gdiplus_image();
m_artwork_loading = true;
artwork_manager::get_artwork_async(track, [this, track](const artwork_manager::artwork_result& result) {
auto* heap_result = new artwork_manager::artwork_result(result);
::PostMessage(m_hWnd, WM_USER_ARTWORK_LOADED, 0, reinterpret_cast<LPARAM>(heap_result));
});
} else {
//Don't search yet,wait for the first on_dynamic_info_track to get called
//Fixes wrong first image with radio
// Use minimal delay for internet streams
}
}
}
// HELPER Inverted internet stream detection
bool artwork_ui_element::is_inverted_internet_stream(metadb_handle_ptr track, const file_info& p_info) {
if (!track.is_valid()) {
return false;
}
pfc::string8 path = track->get_path();
if (path.is_empty()) return false;
// Search if parameter "?inverted" or "&inverted" in path
std::string path_str = path.c_str();
if ((path_str.find("?inverted") != std::string::npos) || (path_str.find("&inverted") != std::string::npos)) {
return true;
}
// Search if field %STREAM_INVERTED% exists and equals 1
const char* stream_inverted_ptr = p_info.meta_get("STREAM_INVERTED", 0);
pfc::string8 inverted = stream_inverted_ptr ? stream_inverted_ptr : "";
if (inverted == "1") {
return true;
}
return false;
}
void artwork_ui_element::on_dynamic_info_track(const file_info& p_info) {
try {
// Get artist and track from the updated info safely
const char* artist_ptr = p_info.meta_get("ARTIST", 0);
const char* track_ptr = p_info.meta_get("TITLE", 0);
const char* album_ptr = p_info.meta_get("ALBUM", 0);
const char* station_ptr = p_info.meta_get("STREAM_NAME", 0);
pfc::string8 artist = artist_ptr ? artist_ptr : "";
pfc::string8 track = track_ptr ? track_ptr : "";
pfc::string8 album = album_ptr ? album_ptr : "";
pfc::string8 station = station_ptr ? station_ptr : "";
// Extract only the first artist for better artwork search results
std::string first_artist = MetadataCleaner::extract_first_artist(artist.c_str());
// Clean metadata using the unified UTF-8 safe cleaner
std::string cleaned_artist = MetadataCleaner::clean_for_search(first_artist.c_str(), true);
std::string cleaned_track = MetadataCleaner::clean_for_search(track.c_str(), true);
//If inverted swap artist title
bool is_inverted_stream = is_inverted_internet_stream(m_current_track,p_info);
if (is_inverted_stream) {
std::string clean_artist_old = cleaned_artist;
std::string clean_title_old = cleaned_track;
cleaned_artist = clean_title_old;
cleaned_track = clean_artist_old;
}
//If no artist and title is "artist - title" (eg https://stream.radioclub80.cl:8022/retro80.opus) split
if (cleaned_artist.empty()) {
std::string delimiter = " - ";
size_t pos = cleaned_track.find(delimiter);
if (pos != std::string::npos) {
std::string lvalue = cleaned_track.substr(0, pos);
std::string rvalue = cleaned_track.substr(pos + delimiter.length());
cleaned_artist = lvalue;
cleaned_track = rvalue;
}
//or "artist ˗ title" (eg https ://energybasel.ice.infomaniak.ch/energybasel-high.mp3)
std::string delimiter2 = " ˗ ";
size_t pos2 = cleaned_track.find(delimiter2);
if (pos2 != std::string::npos) {
std::string lvalue = cleaned_track.substr(0, pos2);
std::string rvalue = cleaned_track.substr(pos2 + delimiter2.length());
cleaned_artist = lvalue;
cleaned_track = rvalue;
}
//or "artist / title" (eg https://radiostream.pl/tuba8-1.mp3?cache=1650763965 )
std::string delimiter3 = " / ";
size_t pos3 = cleaned_track.find(delimiter3);
if (pos3 != std::string::npos) {
std::string lvalue = cleaned_track.substr(0, pos3);
std::string rvalue = cleaned_track.substr(pos3 + delimiter3.length());
cleaned_artist = lvalue;
cleaned_track = rvalue;
}
}
//with inverted (eg https://icy.unitedradio.it/um049.mp3?inverted )
else if (cleaned_track.empty() && is_inverted_stream) {
std::string delimiter = " - ";