-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontrol_panel.cpp
More file actions
5279 lines (4456 loc) · 232 KB
/
Copy pathcontrol_panel.cpp
File metadata and controls
5279 lines (4456 loc) · 232 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 "control_panel.h"
#include "preferences.h"
#include "preferences.h"
#include "volume_popup.h"
#include "artwork_bridge.h"
#include <dwmapi.h>
#pragma comment(lib, "dwmapi.lib")
// Timer constants
#define TIMEOUT_TIMER_ID 9999 // Use unique timer ID to avoid conflicts
#define ANIMATION_TIMER_ID 4003
#define OVERLAY_TIMER_ID 4004
#define FADE_TIMER_ID 4005
#define BUTTON_FADE_TIMER_ID 9001
#define SLIDE_TIMER_ID 4010
// Static instance
control_panel* control_panel::s_instance = nullptr;
// External declaration from main.cpp
extern HINSTANCE g_hIns;
// Helper function to get DPI-scaled font height from point size
// Returns negative value as required by CreateFont for character height
static int get_dpi_scaled_font_height(int point_size) {
HDC hdc = GetDC(nullptr);
int dpi = GetDeviceCaps(hdc, LOGPIXELSY);
int height = -MulDiv(point_size, dpi, 72); // Points to pixels: size * dpi / 72
ReleaseDC(nullptr, hdc);
return height;
}
//=============================================================================
// control_panel - Media control panel popup
//=============================================================================
control_panel::control_panel()
: m_control_window(nullptr)
, m_initialized(false)
, m_visible(false)
, m_current_time(0.0)
, m_track_length(0.0)
, m_is_playing(false)
, m_is_paused(false)
, m_is_undocked(false)
, m_is_artwork_expanded(false)
, m_last_click_time(0)
, m_saved_undocked_width(338)
, m_saved_undocked_height(120)
, m_saved_expanded_width(400)
, m_saved_expanded_height(400)
, m_saved_miniplayer_x(-1)
, m_saved_miniplayer_y(-1)
, m_saved_miniplayer_width(0)
, m_saved_miniplayer_height(0)
, m_has_saved_miniplayer_state(false)
, m_saved_was_undocked(false)
, m_saved_was_expanded(false)
, m_saved_was_compact(false)
, m_overlay_visible(false)
, m_last_mouse_move_time(0)
, m_overlay_opacity(0)
, m_fade_start_time(0)
, m_undocked_overlay_visible(false)
, m_undocked_overlay_opacity(0)
, m_undocked_fade_start_time(0)
, m_is_dragging(false)
, m_buttons_visible(true)
, m_button_opacity(100)
, m_button_fade_start_time(0)
, m_last_button_mouse_time(0)
, m_is_compact_mode(false)
, m_saved_normal_width(338)
, m_saved_normal_height(120)
, m_saved_compact_width(320)
, m_was_compact_before_expanded(false)
, m_is_rolling_animation(false)
, m_rolling_to_compact(false)
, m_roll_animation_step(0)
, m_roll_animation_start_time(0)
, m_cover_art_bitmap(nullptr)
, m_cover_art_bitmap_large(nullptr)
, m_cover_art_bitmap_original(nullptr)
, m_original_art_width(0)
, m_original_art_height(0)
, m_online_artwork_pending(false)
, m_artwork_from_bridge(false)
, m_artist_font(nullptr)
, m_track_font(nullptr)
, m_animating(false)
, m_closing(false)
, m_animation_step(0)
, m_start_x(0)
, m_start_y(0)
, m_final_x(0)
, m_final_y(0)
, m_compact_controls_visible(false)
, m_last_compact_mouse_time(0)
, m_mouse_over_close_button(false)
, m_mouse_in_window(false) // For collapse triangle hover visibility
, m_hovered_button(0) // Initialize hover state
, m_shuffle_active(false)
, m_repeat_mode(0)
, m_is_slid_to_side(false)
, m_sliding_animation(false)
, m_sliding_to_side(false)
, m_pre_slide_x(0)
, m_pre_slide_y(0)
, m_slide_start_x(0)
, m_slide_target_x(0)
, m_slide_animation_step(0)
// Theme colors - default to dark mode
, m_is_dark_mode(true)
, m_bg_color(RGB(32, 32, 32))
, m_text_color(RGB(255, 255, 255))
, m_text_dim_color(RGB(180, 180, 180))
, m_placeholder_color(RGB(60, 60, 60))
, m_progress_bg_color(RGB(80, 80, 80))
, m_progress_fill_color(RGB(100, 149, 237))
, m_icon_color(RGB(255, 255, 255))
{
m_last_click_pos.x = 0;
m_last_click_pos.y = 0;
// Update theme colors based on current settings
update_theme_colors();
}
control_panel::~control_panel() {
cleanup();
}
control_panel& control_panel::get_instance() {
if (!s_instance) {
s_instance = new control_panel();
}
return *s_instance;
}
void control_panel::initialize() {
if (m_initialized) return;
create_control_window();
load_fonts();
m_initialized = true;
}
void control_panel::cleanup() {
if (m_visible) {
hide_control_panel_immediate(); // Use immediate hide during cleanup
}
// Kill update timers
if (m_control_window) {
KillTimer(m_control_window, UPDATE_TIMER_ID);
KillTimer(m_control_window, UPDATE_TIMER_ID + 1);
KillTimer(m_control_window, TIMEOUT_TIMER_ID);
KillTimer(m_control_window, ANIMATION_TIMER_ID);
KillTimer(m_control_window, BUTTON_FADE_TIMER_ID);
KillTimer(m_control_window, BUTTON_FADE_TIMER_ID + 1);
KillTimer(m_control_window, ARTWORK_POLL_TIMER_ID);
}
cleanup_cover_art();
cleanup_fonts();
if (m_control_window) {
DestroyWindow(m_control_window);
m_control_window = nullptr;
}
m_initialized = false;
}
void control_panel::show_control_panel(bool force_docked) {
if (!m_initialized || m_visible) return;
// Force docked state if requested (e.g., when opened from tray icon)
if (force_docked) {
m_is_undocked = false;
}
// Reset undocked overlay state
m_undocked_overlay_visible = false;
m_undocked_overlay_opacity = 0;
// Sync shuffle/repeat state with foobar2000
update_playback_order_state();
// Update theme colors in case foobar2000 theme changed
update_theme_colors();
// Position control panel
position_control_panel();
// Show window immediately with proper topmost behavior for docked mode
ShowWindow(m_control_window, SW_SHOWNOACTIVATE);
// Ensure topmost behavior for both docked and undocked modes
SetWindowPos(m_control_window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
m_visible = true;
// Enable mouse tracking to detect when cursor leaves window
TRACKMOUSEEVENT tme = {0};
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_control_window;
TrackMouseEvent(&tme);
// Schedule track info update for next timer tick (asynchronous), but not if we're trying to keep it docked
if (!force_docked) {
SetTimer(m_control_window, UPDATE_TIMER_ID + 2, 50, nullptr); // 50ms delayed update
} else {
// For forced docked mode, do immediate minimal update to avoid state changes
if (m_control_window) {
InvalidateRect(m_control_window, nullptr, TRUE);
}
}
// Start update timer - only if not in artwork expanded mode
if (!m_is_artwork_expanded) {
// For force_docked (tray icon), use slower timer to avoid interference with timeout
int timer_interval;
if (force_docked) {
timer_interval = 2000; // 2 second intervals for tray-opened panels
} else {
timer_interval = m_is_undocked ? 500 : 1000; // 500ms when undocked, 1000ms when docked
}
SetTimer(m_control_window, UPDATE_TIMER_ID, timer_interval, nullptr);
}
// Start timeout timer for docked panels (5 seconds auto-hide)
// Force timeout timer when force_docked is true (from tray icon)
if (force_docked || (!m_is_undocked && !m_is_artwork_expanded)) {
SetTimer(m_control_window, TIMEOUT_TIMER_ID, 5000, nullptr);
} else {
// Kill any existing timeout timer if we're not setting one
KillTimer(m_control_window, TIMEOUT_TIMER_ID);
}
}
void control_panel::show_control_panel_simple() {
if (!m_initialized || m_visible) return;
// Force clean docked state - ignore any previous undocked state
m_is_undocked = false;
m_is_artwork_expanded = false;
m_is_compact_mode = false; // Ensure compact mode is disabled in docked state
// Note: Do NOT clear m_has_saved_miniplayer_state here - preserve it for menu restoration
// Reset undocked overlay state
m_undocked_overlay_visible = false;
m_undocked_overlay_opacity = 0;
// Update with current track info (but don't force cleanup for speed)
update_track_info();
// Position control panel
position_control_panel();
// Show window with topmost behavior (like original)
ShowWindow(m_control_window, SW_SHOWNOACTIVATE);
SetWindowPos(m_control_window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
m_visible = true;
// Enable mouse tracking to detect when cursor leaves window
TRACKMOUSEEVENT tme = {0};
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_control_window;
TrackMouseEvent(&tme);
// Always start both timers (like original) - use timer ID from original version
SetTimer(m_control_window, UPDATE_TIMER_ID, 1000, nullptr);
SetTimer(m_control_window, TIMEOUT_TIMER_ID, 5000, nullptr); // 5 seconds auto-close
}
void control_panel::hide_control_panel() {
if (!m_visible || m_animating) {
OutputDebugStringA("hide_control_panel - early return (not visible or animating)\n");
return;
}
// Start slide out animation (used for timeout)
start_slide_out_animation();
}
void control_panel::hide_control_panel_immediate() {
if (!m_visible) return;
// Stop any ongoing animation
if (m_animating) {
m_animating = false;
m_closing = false;
KillTimer(m_control_window, ANIMATION_TIMER_ID);
}
// Stop other timers
KillTimer(m_control_window, UPDATE_TIMER_ID);
KillTimer(m_control_window, UPDATE_TIMER_ID + 1);
KillTimer(m_control_window, TIMEOUT_TIMER_ID);
KillTimer(m_control_window, SLIDE_TIMER_ID);
// Reset slide-to-side state so panel reopens at original position
if (m_is_slid_to_side && m_pre_slide_x != 0) {
// Restore position before hiding
RECT window_rect;
GetWindowRect(m_control_window, &window_rect);
int window_height = window_rect.bottom - window_rect.top;
int window_width = window_rect.right - window_rect.left;
SetWindowPos(m_control_window, nullptr, m_pre_slide_x, window_rect.top,
window_width, window_height, SWP_NOACTIVATE | SWP_NOZORDER);
}
m_is_slid_to_side = false;
m_sliding_animation = false;
// Hide immediately without animation
ShowWindow(m_control_window, SW_HIDE);
m_visible = false;
}
void control_panel::toggle_control_panel() {
if (m_visible) {
hide_control_panel_immediate(); // Use immediate hide for manual toggle
} else {
// Reset to clean state for basic popup behavior
m_is_undocked = false;
m_is_artwork_expanded = false;
m_is_compact_mode = false; // Reset compact mode when toggling via tray
show_control_panel_simple(); // Use simple popup behavior like original
}
}
void control_panel::hide_and_remember_miniplayer() {
if (!m_control_window || !m_visible) return;
// Only save state if in a non-docked mode (undocked, expanded, or compact)
if (m_is_undocked || m_is_artwork_expanded || m_is_compact_mode) {
RECT rect;
GetWindowRect(m_control_window, &rect);
m_saved_miniplayer_x = rect.left;
m_saved_miniplayer_y = rect.top;
m_saved_miniplayer_width = rect.right - rect.left;
m_saved_miniplayer_height = rect.bottom - rect.top;
m_saved_was_undocked = m_is_undocked;
m_saved_was_expanded = m_is_artwork_expanded;
m_saved_was_compact = m_is_compact_mode;
m_has_saved_miniplayer_state = true;
}
// Hide immediately without animation
KillTimer(m_control_window, UPDATE_TIMER_ID);
KillTimer(m_control_window, ANIMATION_TIMER_ID);
KillTimer(m_control_window, TIMEOUT_TIMER_ID);
ShowWindow(m_control_window, SW_HIDE);
m_visible = false;
}
void control_panel::show_miniplayer_at_saved_position() {
if (!m_initialized) {
initialize();
}
if (!m_control_window) {
create_control_window();
}
// Update track info and load cover art
update_track_info();
// Restore the saved mode state
m_is_undocked = m_saved_was_undocked;
m_is_artwork_expanded = m_saved_was_expanded;
m_is_compact_mode = m_saved_was_compact;
// Use saved position and dimensions
int x = m_saved_miniplayer_x;
int y = m_saved_miniplayer_y;
int width = m_saved_miniplayer_width;
int height = m_saved_miniplayer_height;
// Provide defaults if no valid saved state
if (x < 0 || y < 0 || width <= 0 || height <= 0) {
int screen_width = GetSystemMetrics(SM_CXSCREEN);
int screen_height = GetSystemMetrics(SM_CYSCREEN);
if (m_is_artwork_expanded) {
width = m_saved_expanded_width > 0 ? m_saved_expanded_width : 400;
height = m_saved_expanded_height > 0 ? m_saved_expanded_height : 400;
} else if (m_is_compact_mode) {
width = m_saved_compact_width > 0 ? m_saved_compact_width : 320;
height = 75;
} else {
width = m_saved_undocked_width > 0 ? m_saved_undocked_width : 338;
height = m_saved_undocked_height > 0 ? m_saved_undocked_height : 120;
}
x = (screen_width - width) / 2;
y = (screen_height - height) / 2;
}
// Position and show the window
SetWindowPos(m_control_window, HWND_TOPMOST, x, y, width, height, SWP_NOACTIVATE);
ShowWindow(m_control_window, SW_SHOWNOACTIVATE);
m_visible = true;
// Start update timer
SetTimer(m_control_window, UPDATE_TIMER_ID, 500, nullptr);
// Trigger repaint
InvalidateRect(m_control_window, nullptr, TRUE);
}
void control_panel::show_undocked_miniplayer() {
// If the docked panel is currently animating (closing), wait for animation to complete
// This prevents the animation from being interrupted when user opens MiniPlayer via tray menu
if (m_animating && m_closing) {
// Let the animation complete - do nothing and return
// The user will need to click again after the animation finishes
return;
}
// If MiniPlayer is slid-to-side (peeking), slide it back out
if (m_visible && m_is_slid_to_side) {
slide_back_from_side();
return;
}
// If in Docked mode (visible but not undocked/expanded/compact), close it first and proceed
// This allows the MiniPlayer to open when clicking "Launch MiniPlayer" while docked popup is shown
if (m_visible && !m_is_undocked && !m_is_artwork_expanded && !m_is_compact_mode) {
// Kill all timers and hide immediately - don't animate since we're switching modes
KillTimer(m_control_window, UPDATE_TIMER_ID);
KillTimer(m_control_window, UPDATE_TIMER_ID + 1);
KillTimer(m_control_window, TIMEOUT_TIMER_ID);
KillTimer(m_control_window, ANIMATION_TIMER_ID);
m_animating = false;
m_closing = false;
ShowWindow(m_control_window, SW_HIDE);
m_visible = false;
// Don't return - proceed to show MiniPlayer below
}
// If MiniPlayer (Undocked/Expanded/Compact) is fully visible, either slide to side or hide
else if (m_visible) {
// If "Always Slide-to-Side" is enabled, slide instead of hiding
if (get_always_slide_to_side()) {
slide_to_side();
} else {
hide_and_remember_miniplayer();
}
return;
}
// Initialize if needed
if (!m_initialized) {
initialize();
}
if (!m_control_window) {
create_control_window();
}
// Update track info and load cover art
update_track_info();
// Restore saved mode if available, otherwise default to Undocked
if (m_has_saved_miniplayer_state) {
m_is_undocked = m_saved_was_undocked;
m_is_artwork_expanded = m_saved_was_expanded;
m_is_compact_mode = m_saved_was_compact;
} else {
// Default to Undocked mode for first launch
m_is_undocked = true;
m_is_artwork_expanded = false;
m_is_compact_mode = false;
}
// Set dimensions based on mode
int width, height;
if (m_is_artwork_expanded) {
width = m_saved_expanded_width > 0 ? m_saved_expanded_width : 400;
height = m_saved_expanded_height > 0 ? m_saved_expanded_height : 400;
} else if (m_is_compact_mode) {
width = m_saved_compact_width > 0 ? m_saved_compact_width : 320;
height = 75;
} else {
// Undocked mode
width = m_saved_undocked_width > 0 ? m_saved_undocked_width : 338;
height = m_saved_undocked_height > 0 ? m_saved_undocked_height : 120;
}
// Use saved position if available, otherwise center on screen
int x, y;
if (m_has_saved_miniplayer_state && m_saved_miniplayer_x >= 0 && m_saved_miniplayer_y >= 0) {
x = m_saved_miniplayer_x;
y = m_saved_miniplayer_y;
} else {
int screen_width = GetSystemMetrics(SM_CXSCREEN);
int screen_height = GetSystemMetrics(SM_CYSCREEN);
x = (screen_width - width) / 2;
y = (screen_height - height) / 2;
}
// Position and show the window
SetWindowPos(m_control_window, HWND_TOPMOST, x, y, width, height, SWP_NOACTIVATE);
ShowWindow(m_control_window, SW_SHOWNOACTIVATE);
m_visible = true;
// Reset slide state
m_is_slid_to_side = false;
// Enable mouse tracking for button fade functionality
TRACKMOUSEEVENT tme = {0};
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_control_window;
TrackMouseEvent(&tme);
// Reset button opacity and visibility
m_buttons_visible = true;
m_button_opacity = 100;
// Start update timer
SetTimer(m_control_window, UPDATE_TIMER_ID, 500, nullptr);
// Trigger repaint
InvalidateRect(m_control_window, nullptr, TRUE);
}
void control_panel::update_track_info() {
try {
auto playback = playback_control::get();
// Get current track
metadb_handle_ptr track;
if (playback->get_now_playing(track) && track.is_valid()) {
// Check if this is a stream
pfc::string8 path = track->get_path();
bool is_stream = strstr(path.get_ptr(), "://") != nullptr;
m_current_title = "";
m_current_artist = "";
// Use configurable display format strings
format_display_lines(m_current_title, m_current_artist);
// Fallback for streams if format returned empty
if (is_stream && m_current_title.is_empty() && m_current_artist.is_empty()) {
file_info_impl info;
if (track->get_info(info)) {
if (info.meta_exists("server")) {
m_current_title = info.meta_get("server", 0);
} else if (info.meta_exists("SERVER")) {
m_current_title = info.meta_get("SERVER", 0);
}
}
}
if (m_current_title.is_empty()) m_current_title = "Unknown Title";
if (m_current_artist.is_empty()) m_current_artist = "Unknown Artist";
// Get track length
m_track_length = track->get_length();
} else {
m_current_artist = "No track";
m_current_title = "";
m_track_length = 0.0;
}
// Get playback state and position
m_is_playing = playback->is_playing();
m_is_paused = playback->is_paused();
m_current_time = playback->playback_get_position();
// Load cover art - always reload when track info updates
if (track.is_valid()) {
load_cover_art();
// Adjust window size for new artwork aspect ratio when in expanded mode
if (m_is_artwork_expanded && m_control_window && m_original_art_width > 0 && m_original_art_height > 0) {
RECT current_rect;
GetWindowRect(m_control_window, ¤t_rect);
int current_width = current_rect.right - current_rect.left;
int current_height = current_rect.bottom - current_rect.top;
float image_aspect = (float)m_original_art_width / (float)m_original_art_height;
float window_aspect = (float)current_width / (float)current_height;
// If aspect ratios differ significantly, resize the window to match the new image
if (abs(image_aspect - window_aspect) > 0.05f) {
int new_width, new_height;
// Keep the larger dimension, adjust the smaller one
if (image_aspect >= 1.0f) {
// Landscape or square - keep width, adjust height
new_width = current_width;
new_height = (int)((float)current_width / image_aspect);
} else {
// Portrait - keep height, adjust width
new_height = current_height;
new_width = (int)((float)current_height * image_aspect);
}
// Ensure minimum size
if (new_width < 200) new_width = 200;
if (new_height < 200) new_height = 200;
// Update saved dimensions
m_saved_expanded_width = new_width;
m_saved_expanded_height = new_height;
// Resize window to match new aspect ratio
SetWindowPos(m_control_window, HWND_TOPMOST, 0, 0, new_width, new_height,
SWP_NOMOVE | SWP_NOACTIVATE);
}
}
} else {
// Clear artwork if no valid track
cleanup_cover_art();
}
} catch (...) {
m_current_artist = "Error";
m_current_title = "";
m_is_playing = false;
m_is_paused = false;
m_current_time = 0.0;
m_track_length = 0.0;
}
// Refresh display
// Always update if visible (including artwork expanded mode for track changes)
if (m_control_window && (m_is_undocked || m_visible || m_is_artwork_expanded)) {
InvalidateRect(m_control_window, nullptr, TRUE);
}
}
void control_panel::create_control_window() {
const char* class_name = "TrayControlsControlPanel";
// Register window class (only once)
static bool class_registered = false;
if (!class_registered) {
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = control_window_proc;
wc.hInstance = g_hIns;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(32, 32, 32)); // Dark background
wc.lpszClassName = L"TrayControlsControlPanel";
ATOM class_atom = RegisterClassEx(&wc);
if (class_atom != 0) {
class_registered = true;
}
}
// Create control window (initially hidden) - temporarily remove WS_EX_NOACTIVATE for testing
m_control_window = CreateWindowEx(
WS_EX_TOOLWINDOW | WS_EX_TOPMOST, // Removed WS_EX_NOACTIVATE for testing
L"TrayControlsControlPanel",
L"Media Controls",
WS_POPUP,
0, 0, 338, 120, // Increased width by 15% (294 * 1.15 = 338)
nullptr,
nullptr,
g_hIns,
this
);
if (!m_control_window) {
throw exception_win32(GetLastError());
}
// Apply window corner preference (rounded/square corners)
apply_window_corner_preference();
}
void control_panel::position_control_panel() {
if (!m_control_window) return;
// Get screen dimensions
int screen_width = GetSystemMetrics(SM_CXSCREEN);
int screen_height = GetSystemMetrics(SM_CYSCREEN);
// Get taskbar info
APPBARDATA abd = {};
abd.cbSize = sizeof(APPBARDATA);
SHAppBarMessage(ABM_GETTASKBARPOS, &abd);
// Calculate position (bottom-right, above tray area)
const int panel_width = 338;
const int panel_height = 120;
const int margin = 10;
int x = screen_width - panel_width - margin;
int y = screen_height - panel_height - margin;
// Adjust for taskbar position
if (abd.rc.bottom == screen_height && abd.rc.left == 0 && abd.rc.right == screen_width) {
// Taskbar at bottom
y = abd.rc.top - panel_height - margin;
} else if (abd.rc.right == screen_width && abd.rc.top == 0 && abd.rc.bottom == screen_height) {
// Taskbar at right
x = abd.rc.left - panel_width - margin;
} else if (abd.rc.top == 0 && abd.rc.left == 0 && abd.rc.right == screen_width) {
// Taskbar at top
y = abd.rc.bottom + margin;
} else if (abd.rc.left == 0 && abd.rc.top == 0 && abd.rc.bottom == screen_height) {
// Taskbar at left
x = abd.rc.right + margin;
}
// Position the window - but don't override the Z-order set by show_control_panel
SetWindowPos(m_control_window, nullptr, x, y, panel_width, panel_height, SWP_NOACTIVATE | SWP_NOZORDER);
}
void control_panel::load_cover_art() {
// Check if artwork has arrived via callback (from our request or foo_nowbar's).
// Pick it up immediately regardless of who triggered the search.
if (has_pending_online_artwork()) {
HBITMAP bitmap = get_pending_online_artwork();
if (bitmap) {
cleanup_cover_art();
m_cover_art_bitmap = bitmap;
m_cover_art_bitmap_large = nullptr;
m_cover_art_bitmap_original = nullptr;
m_artwork_from_bridge = false; // We own the copy, cleanup_cover_art will DeleteObject
m_online_artwork_pending = false;
if (m_control_window) KillTimer(m_control_window, ARTWORK_POLL_TIMER_ID);
return;
}
}
// If we previously had bridge artwork for the current stream, check if metadata
// is still the same - if so, re-acquire it from the bridge cache.
if (!m_last_stream_artist.is_empty()) {
try {
auto playback = playback_control::get();
metadb_handle_ptr track;
if (playback->get_now_playing(track) && track.is_valid()) {
pfc::string8 path = track->get_path();
bool is_stream = strstr(path.get_ptr(), "://") != nullptr;
if (is_stream && is_artwork_bridge_available()) {
pfc::string8 artist, title;
service_ptr_t<titleformat_object> script_artist, script_title;
titleformat_compiler::get()->compile_safe(script_artist, "%artist%");
titleformat_compiler::get()->compile_safe(script_title, "%title%");
playback->playback_format_title(nullptr, artist, script_artist, nullptr, playback_control::display_level_all);
playback->playback_format_title(nullptr, title, script_title, nullptr, playback_control::display_level_all);
// Same metadata as last request - re-acquire the bitmap from our bridge cache
if (artist == m_last_stream_artist && title == m_last_stream_title) {
HBITMAP existing = get_last_online_artwork();
if (existing) {
cleanup_cover_art();
m_cover_art_bitmap = existing;
m_artwork_from_bridge = false; // We own the copy
return;
}
}
}
}
} catch (...) {}
}
try {
auto playback = playback_control::get();
metadb_handle_ptr track;
if (playback->get_now_playing(track) && track.is_valid()) {
// Try local/embedded artwork first (in its own try-catch so exceptions
// don't prevent the online artwork fallback from running)
try {
auto api = album_art_manager_v2::get();
if (api.is_valid()) {
auto extractor = api->open(pfc::list_single_ref_t<metadb_handle_ptr>(track),
pfc::list_single_ref_t<GUID>(album_art_ids::cover_front),
fb2k::noAbort);
if (extractor.is_valid()) {
auto data = extractor->query(album_art_ids::cover_front, fb2k::noAbort);
if (data.is_valid() && data->get_size() > 0) {
// Found local/embedded artwork - replace old artwork
cleanup_cover_art();
m_cover_art_bitmap = convert_album_art_to_bitmap(data);
m_cover_art_bitmap_large = convert_album_art_to_bitmap_large(data);
m_cover_art_bitmap_original = convert_album_art_to_bitmap_original(data);
m_last_stream_artist.reset();
m_last_stream_title.reset();
return;
}
}
}
} catch (...) {}
// No embedded/local artwork found - try online via foo_artwork for streams
pfc::string8 path = track->get_path();
bool is_stream = strstr(path.get_ptr(), "://") != nullptr;
if (is_stream && is_artwork_bridge_available()) {
// Extract artist/title using playback_format_title for stream metadata
pfc::string8 artist, title;
service_ptr_t<titleformat_object> script_artist, script_title;
titleformat_compiler::get()->compile_safe(script_artist, "%artist%");
titleformat_compiler::get()->compile_safe(script_title, "%title%");
playback->playback_format_title(nullptr, artist, script_artist, nullptr, playback_control::display_level_all);
playback->playback_format_title(nullptr, title, script_title, nullptr, playback_control::display_level_all);
// Check if artwork already arrived before we even requested
// (e.g. foo_nowbar triggered foo_artwork first)
if (has_pending_online_artwork()) {
HBITMAP bitmap = get_pending_online_artwork();
if (bitmap) {
cleanup_cover_art();
m_cover_art_bitmap = bitmap;
m_artwork_from_bridge = false;
m_last_stream_artist = artist;
m_last_stream_title = title;
m_online_artwork_pending = false;
if (m_control_window) KillTimer(m_control_window, ARTWORK_POLL_TIMER_ID);
return;
}
}
// Avoid re-requesting the same artist/title
if (artist != m_last_stream_artist || title != m_last_stream_title) {
m_last_stream_artist = artist;
m_last_stream_title = title;
m_online_artwork_pending = true;
request_online_artwork(artist.c_str(), title.c_str());
// Start polling for artwork results (async search)
// Keep old artwork visible until new artwork arrives via callback
if (m_control_window) {
SetTimer(m_control_window, ARTWORK_POLL_TIMER_ID, ARTWORK_POLL_INTERVAL, nullptr);
}
}
// Don't cleanup - keep existing artwork visible during async fetch
return;
}
// Not a stream and no local artwork found - clear artwork
cleanup_cover_art();
} else {
// No valid track - clear artwork
cleanup_cover_art();
}
} catch (...) {
// Ignore errors
}
}
void control_panel::cleanup_cover_art() {
if (m_cover_art_bitmap) {
// Do NOT delete bitmaps owned by foo_artwork bridge
if (!m_artwork_from_bridge) {
DeleteObject(m_cover_art_bitmap);
}
m_cover_art_bitmap = nullptr;
}
if (m_cover_art_bitmap_large) {
DeleteObject(m_cover_art_bitmap_large);
m_cover_art_bitmap_large = nullptr;
}
if (m_cover_art_bitmap_original) {
DeleteObject(m_cover_art_bitmap_original);
m_cover_art_bitmap_original = nullptr;
}
m_original_art_width = 0;
m_original_art_height = 0;
m_artwork_from_bridge = false;
}
HBITMAP control_panel::convert_album_art_to_bitmap(album_art_data_ptr art_data) {
if (!art_data.is_valid() || art_data->get_size() == 0) {
return nullptr;
}
HBITMAP result = nullptr;
// Initialize GDI+ if not already done
static ULONG_PTR gdiplusToken = 0;
static bool gdiplus_initialized = false;
if (!gdiplus_initialized) {
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
if (Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr) == Gdiplus::Ok) {
gdiplus_initialized = true;
} else {
return nullptr;
}
}
try {
// Create IStream from memory buffer
CComPtr<IStream> stream;
stream.p = SHCreateMemStream(reinterpret_cast<const BYTE*>(art_data->get_ptr()),
static_cast<UINT>(art_data->get_size()));
if (!stream) {
return nullptr;
}
// Load image from stream using GDI+
Gdiplus::Image image(stream);
if (image.GetLastStatus() != Gdiplus::Ok) {
return nullptr;
}
// Create a bitmap with the desired size (80x80 for the cover area)
const int target_size = 80;
Gdiplus::Bitmap bitmap(target_size, target_size, PixelFormat32bppARGB);
if (bitmap.GetLastStatus() != Gdiplus::Ok) {
return nullptr;
}
// Draw the image scaled to fit the target size
Gdiplus::Graphics graphics(&bitmap);
graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
// Calculate scaling to maintain aspect ratio
UINT img_width = image.GetWidth();
UINT img_height = image.GetHeight();
int draw_width = target_size;
int draw_height = target_size;
int offset_x = 0;
int offset_y = 0;
if (img_width != img_height) {
if (img_width > img_height) {
draw_height = (target_size * img_height) / img_width;
offset_y = (target_size - draw_height) / 2;
} else {
draw_width = (target_size * img_width) / img_height;
offset_x = (target_size - draw_width) / 2;
}
}
// Clear background to dark gray to match panel
graphics.Clear(Gdiplus::Color(255, 32, 32, 32));
// Draw the scaled image
graphics.DrawImage(&image, offset_x, offset_y, draw_width, draw_height);
// Convert to HBITMAP
if (bitmap.GetHBITMAP(Gdiplus::Color(32, 32, 32), &result) != Gdiplus::Ok) {
result = nullptr;
}
} catch (...) {
result = nullptr;
}
return result;
}
HBITMAP control_panel::convert_album_art_to_bitmap_large(album_art_data_ptr art_data) {
if (!art_data.is_valid() || art_data->get_size() == 0) {
return nullptr;
}
HBITMAP result = nullptr;
// Initialize GDI+ if not already done
static ULONG_PTR gdiplusToken = 0;
static bool gdiplus_initialized = false;
if (!gdiplus_initialized) {
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
if (Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr) == Gdiplus::Ok) {
gdiplus_initialized = true;
} else {
return nullptr;
}
}
try {
// Create IStream from memory buffer
CComPtr<IStream> stream;
stream.p = SHCreateMemStream(reinterpret_cast<const BYTE*>(art_data->get_ptr()),
static_cast<UINT>(art_data->get_size()));
if (!stream) {
return nullptr;
}
// Load image from stream using GDI+
Gdiplus::Image image(stream);
if (image.GetLastStatus() != Gdiplus::Ok) {
return nullptr;
}
// Create a high-quality bitmap (300x300 for expanded view)
const int target_size = 300;
Gdiplus::Bitmap bitmap(target_size, target_size, PixelFormat32bppARGB);
if (bitmap.GetLastStatus() != Gdiplus::Ok) {
return nullptr;