-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskbar-fluent-media.wh.cpp
More file actions
3149 lines (2757 loc) · 130 KB
/
taskbar-fluent-media.wh.cpp
File metadata and controls
3149 lines (2757 loc) · 130 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
// ==WindhawkMod==
// @id taskbar-fluent-media-player
// @name Taskbar Fluent Media Player
// @description Embeds a Fluent Design media player inside the Windows 11 taskbar.
// @version 0.2.0-beta
// @author Salyts
// @github https://github.com/Salyts
// @include explorer.exe
// @architecture x86-64
// @compilerOptions -lole32 -loleaut32 -lruntimeobject -lversion -luuid -luser32 -lwindowsapp -lshell32 -lgdi32
// ==/WindhawkMod==
// ==WindhawkModReadme==
/*
# Taskbar Fluent Media Player
Embeds a Fluent Design media player inside the Windows 11 taskbar.
This mod is currently in beta testing. Some features may be unstable, may not work correctly, or may change in future updates. During use, errors, crashes, conflicts with other mods, or unexpected system behavior may occur.
By installing and using this mod, you do so at your own risk. The author is not responsible for any possible consequences, including data loss, system instability, software conflicts, or other issues arising from the use of the mod.
*/
// ==/WindhawkModReadme==
// ==WindhawkModSettings==
/*
- PositionSettings:
- position: "tray_left"
$name: Player position
$options:
- "tray_left": "Tray - Far edge - Left"
- "tray_right": "Tray - Far edge - Right"
- "tray_before_clock": "Tray - Clock - Left"
- "tray_after_clock": "Tray - Clock - Right"
- "tray_before_omni_left": "Tray - Network/volume - Left"
- "tray_before_omni_right": "Tray - Network/volume - Right"
- "tray_after_showdesktop_left": "Tray - Show Desktop - Left"
- "tray_after_showdesktop_right": "Tray - Show Desktop - Right"
- "taskbar_left_start": "Taskbar - Start button - Left"
- "taskbar_right_start": "Taskbar - Start button - Right"
- "taskbar_after_search_left": "Taskbar - Search button - Left"
- "taskbar_after_search_right": "Taskbar - Search button - Right"
- "taskbar_after_taskview_left": "Taskbar - Task View button - Left"
- "taskbar_after_taskview_right": "Taskbar - Task View button - Right"
- "taskbar_after_widgets_left": "Taskbar - Widgets button - Left"
- "taskbar_after_widgets_right": "Taskbar - Widgets button - Right"
- "taskbar_left_edge": "Taskbar - far edge (Overlay) - Left"
- "taskbar_right_edge": "Taskbar - far edge (Overlay) - Right"
- playerMargin: "5 5"
$name: Player margin (left right, px)
$name: Position Settings
- DisplaySettings:
- mirrorLayout: false
$name: Mirror layout (flip album art, text, and buttons to opposite sides)
- mediaButtonsMargin: "5 5"
$name: Media buttons margin (left right, px)
- showTrackTitle: true
$name: Show track title
- showTrackArtist: true
$name: Show artist name
- showAlbumArt: true
$name: Show album art
- albumArtEmptyBehavior: "show"
$name: Album art behavior when no cover available
$options:
- "show": "Show area (default)"
- "hide": "Hide area"
- "show_question": "Show area with question mark"
- "show_note": "Show area with music note"
- showPauseOverlay: false
$name: Show pause icon overlay on album art when paused
- showPrevButton: true
$name: Show Previous button
- showPlayButton: true
$name: Show Play/Pause button
- showNextButton: true
$name: Show Next button
- fillButtonIcons: false
$name: Fill button icons (solid instead of outline)
- showAppIcon: false
$name: Show media app icon overlay
- appIconCorner: "bottom_right"
$name: App icon corner
$options:
- "top_left": "Top left"
- "top_right": "Top right"
- "bottom_left": "Bottom left"
- "bottom_right": "Bottom right"
- appIconSize: 12
$name: App icon size (px)
- playerWidth: "0 280"
$name: Player width (min max, px, 0 = no limit)
- playerHeight: 40
$name: Player height (px)
$name: Display Settings
- AppearanceSettings:
- theme: "auto"
$name: Theme
$options:
- "auto": "Follow system"
- "dark": "Always dark"
- "light": "Always light"
- "custom": "Custom colors"
- backgroundType: "none"
$name: Background type
$options:
- "none": "None (transparent)"
- "solid": "Solid color"
- "acrylic": "Acrylic"
- "mica": "Mica"
- "mica_alt": "Mica Alt"
- "album_art": "Album art"
- "album_art_blur": "Album art - Blur"
- backgroundOpacity: 100
$name: Background opacity (0-100, for solid color)
- backgroundColor: "32 32 32"
$name: Background color (R G B 0-255)
- cornerRadius: 5
$name: Panel corner radius (px)
- albumArtSize: 30
$name: Album art size (px)
- albumArtOpacity: 100
$name: Album art opacity (0-100)
- albumArtCornerRadius: 4
$name: Album art corner radius (px)
- albumArtMargin: "0 0"
$name: Album art margin (left right, px)
- textMargin: "5 5"
$name: Text margin (left right, px)
- controlsMargin: "0 0"
$name: Controls margin (left right, px)
- buttonSpacing: 0
$name: Spacing between media buttons (px)
- buttonCornerRadius: 7
$name: Media button corner radius (px)
- titleFontSize: 12
$name: Title font size (pt)
- artistFontSize: 11
$name: Artist font size (pt)
- textSpacing: 1
$name: Spacing between title and artist (px)
- customButtonColor: "255 255 255"
$name: Custom theme - Button color (R G B 0-255)
- customTitleColor: "255 255 255"
$name: Custom theme - Title color (R G B 0-255)
- customArtistColor: "200 200 200"
$name: Custom theme - Artist color (R G B 0-255)
$name: Appearance Settings
- BehaviorSettings:
- albumArtLeftClick: "play_pause"
$name: Album art - Left click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- albumArtRightClick: "none"
$name: Album art - Right click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- albumArtMiddleClick: "none"
$name: Album art - Middle click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- albumArtLeftDoubleClick: "none"
$name: Album art - Left double click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- albumArtRightDoubleClick: "none"
$name: Album art - Right double click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- albumArtWheelAction: "switch_tracks"
$name: Album art - Mouse wheel
$options:
- "none": "switch_sessions"
- "switch_tracks": "Switch tracks (up=prev, down=next)"
- "switch_sessions": "Switch sessions (up/down)"
- playerLeftClick: "none"
$name: Player - Left click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- playerRightClick: "none"
$name: Player - Right click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- playerMiddleClick: "stop"
$name: Player - Middle click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- playerLeftDoubleClick: "none"
$name: Player - Left double click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- playerRightDoubleClick: "none"
$name: Player - Right double click
$options:
- "none": "Nothing"
- "switch_session": "Switch active media session"
- "play_pause": "Play/Pause"
- "next_track": "Next track"
- "prev_track": "Previous track"
- "stop": "Stop playback"
- playerWheelAction: "switch_tracks"
$name: Player - Mouse wheel
$options:
- "none": "Nothing"
- "switch_tracks": "Switch tracks (up=prev, down=next)"
- "switch_sessions": "Switch sessions (up/down)"
- hideWhenNoMedia: true
$name: Hide when no media is playing
- hideFullscreen: true
$name: Hide when a fullscreen app is running
- idleHideSeconds: 0
$name: Idle auto-hide timeout (seconds, 0 = disabled)
$name: Behavior Settings
- DebugSettings:
- enableTreeDump: false
$name: Dump XAML element names to log on inject
- showDebugBorders: false
$name: Show debug borders (hitboxes)
$name: Debug Settings
*/
// ==/WindhawkModSettings==
#undef GetCurrentTime
#include <winrt/base.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.UI.Core.h>
#include <winrt/Windows.UI.Text.h>
#include <winrt/Windows.UI.Input.h>
#include <winrt/Windows.UI.Xaml.h>
#include <winrt/Windows.UI.Xaml.Controls.Primitives.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.UI.Xaml.Media.h>
#include <winrt/Windows.UI.Xaml.Media.Imaging.h>
#include <winrt/Windows.UI.Xaml.Shapes.h>
#include <winrt/Windows.UI.Xaml.Input.h>
#include <winrt/Windows.UI.Xaml.Automation.h>
#include <winrt/Windows.UI.ViewManagement.h>
#include <winrt/Windows.Media.Control.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <robuffer.h>
#include <windows.h>
#include <shellapi.h>
#include <windhawk_utils.h>
#include <atomic>
#include <mutex>
#include <string>
#include <vector>
#include <algorithm>
#include <thread>
#include <cmath>
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Controls;
using namespace winrt::Windows::UI::Xaml::Media;
using namespace winrt::Windows::UI::Xaml::Media::Imaging;
using namespace winrt::Windows::UI::Xaml::Input;
using namespace winrt::Windows::Media::Control;
using namespace winrt::Windows::Storage::Streams;
#define LOG(fmt, ...) Wh_Log(L"[FMP] " fmt, ##__VA_ARGS__)
struct ModSettings {
std::wstring position = L"tray_right";
std::wstring albumArtLeftClick = L"switch_session";
std::wstring albumArtRightClick = L"none";
std::wstring albumArtMiddleClick = L"none";
std::wstring albumArtLeftDoubleClick = L"none";
std::wstring albumArtRightDoubleClick = L"none";
std::wstring albumArtWheelAction = L"switch_tracks";
std::wstring playerLeftClick = L"none";
std::wstring playerRightClick = L"none";
std::wstring playerMiddleClick = L"stop";
std::wstring playerLeftDoubleClick = L"none";
std::wstring playerRightDoubleClick = L"none";
std::wstring playerWheelAction = L"none";
bool mirrorLayout = false;
int playerMinWidth = 200;
int playerMaxWidth = 400;
int playerHeight = 40;
bool showAlbumArt = true;
std::wstring albumArtEmptyBehavior = L"show";
bool showPauseOverlay = true;
int albumArtSize = 30;
int albumArtOpacity = 100;
int albumArtLeftMargin = 2;
int albumArtRightMargin = 0;
bool showTrackTitle = true;
bool showTrackArtist = true;
bool showPrevButton = true;
bool showPlayButton = true;
bool showNextButton = true;
bool fillButtonIcons = false;
bool showAppIcon = false;
std::wstring appIconCorner = L"bottom_right";
int appIconSize = 12;
bool hideWhenNoMedia = true;
int textMarginLeft = 5;
int textMarginRight = 0;
int controlsMarginLeft = 4;
int controlsMarginRight = 0;
int playerMarginLeft = 0;
int playerMarginRight = 0;
int mediaButtonsLeftMargin = 0;
int mediaButtonsRightMargin = 1;
bool hideFullscreen = true;
int idleHideSeconds = 0;
std::wstring theme = L"auto";
std::wstring backgroundType = L"none";
int backgroundOpacity = 0;
std::wstring backgroundColor = L"32 32 32";
int cornerRadius = 5;
int albumArtCornerRadius = 4;
int buttonSpacing = 0;
int buttonCornerRadius = 14;
int titleFontSize = 12;
int artistFontSize = 10;
int textSpacing = 1;
std::wstring customButtonColor = L"255 255 255";
std::wstring customTitleColor = L"255 255 255";
std::wstring customArtistColor = L"200 200 200";
bool enableTreeDump = false;
bool showDebugBorders = false;
};
static ModSettings g_settings;
static void LoadSettings() {
auto Str = [](const wchar_t* key, const wchar_t* def) -> std::wstring {
PCWSTR p = Wh_GetStringSetting(key);
std::wstring r = p ? p : def;
Wh_FreeStringSetting(p);
return r;
};
auto Int = [](const wchar_t* key, int lo, int hi, int def) -> int {
int v = Wh_GetIntSetting(key);
if (v == 0 && def != 0) v = def;
return std::clamp(v, lo, hi);
};
auto ParseMargin = [&Str](const wchar_t* key, const wchar_t* def, int& left, int& right) {
std::wstring val = Str(key, def);
size_t space = val.find(L' ');
if (space != std::wstring::npos) {
left = std::stoi(val.substr(0, space));
right = std::stoi(val.substr(space + 1));
} else {
left = right = std::stoi(val);
}
};
g_settings.position = Str(L"PositionSettings.position", L"tray_right");
ParseMargin(L"PositionSettings.playerMargin", L"0 0", g_settings.playerMarginLeft, g_settings.playerMarginRight);
g_settings.mirrorLayout = Wh_GetIntSetting(L"DisplaySettings.mirrorLayout") != 0;
ParseMargin(L"DisplaySettings.mediaButtonsMargin", L"0 1", g_settings.mediaButtonsLeftMargin, g_settings.mediaButtonsRightMargin);
g_settings.showTrackTitle = Wh_GetIntSetting(L"DisplaySettings.showTrackTitle") != 0;
g_settings.showTrackArtist = Wh_GetIntSetting(L"DisplaySettings.showTrackArtist") != 0;
g_settings.showAlbumArt = Wh_GetIntSetting(L"DisplaySettings.showAlbumArt") != 0;
g_settings.albumArtEmptyBehavior = Str(L"DisplaySettings.albumArtEmptyBehavior", L"show");
g_settings.showPauseOverlay = Wh_GetIntSetting(L"DisplaySettings.showPauseOverlay") != 0;
g_settings.showPrevButton = Wh_GetIntSetting(L"DisplaySettings.showPrevButton") != 0;
g_settings.showPlayButton = Wh_GetIntSetting(L"DisplaySettings.showPlayButton") != 0;
g_settings.showNextButton = Wh_GetIntSetting(L"DisplaySettings.showNextButton") != 0;
g_settings.fillButtonIcons = Wh_GetIntSetting(L"DisplaySettings.fillButtonIcons") != 0;
g_settings.showAppIcon = Wh_GetIntSetting(L"DisplaySettings.showAppIcon") != 0;
g_settings.appIconCorner = Str(L"DisplaySettings.appIconCorner", L"bottom_right");
g_settings.appIconSize = Int(L"DisplaySettings.appIconSize", 8, 32, 12);
ParseMargin(L"DisplaySettings.playerWidth", L"80 320", g_settings.playerMinWidth, g_settings.playerMaxWidth);
g_settings.playerHeight = Int(L"DisplaySettings.playerHeight", 20, 80, 40);
g_settings.theme = Str(L"AppearanceSettings.theme", L"auto");
g_settings.backgroundType = Str(L"AppearanceSettings.backgroundType", L"none");
g_settings.backgroundOpacity = Int(L"AppearanceSettings.backgroundOpacity", 0, 100, 0);
g_settings.backgroundColor = Str(L"AppearanceSettings.backgroundColor", L"32 32 32");
g_settings.cornerRadius = Int(L"AppearanceSettings.cornerRadius", 0, 24, 5);
g_settings.albumArtSize = Int(L"AppearanceSettings.albumArtSize", 16, 60, 30);
g_settings.albumArtOpacity = Int(L"AppearanceSettings.albumArtOpacity", 0, 100, 100);
g_settings.albumArtCornerRadius = Int(L"AppearanceSettings.albumArtCornerRadius", 0, 24, 4);
ParseMargin(L"AppearanceSettings.albumArtMargin", L"2 0", g_settings.albumArtLeftMargin, g_settings.albumArtRightMargin);
ParseMargin(L"AppearanceSettings.textMargin", L"5 0", g_settings.textMarginLeft, g_settings.textMarginRight);
ParseMargin(L"AppearanceSettings.controlsMargin", L"4 0", g_settings.controlsMarginLeft, g_settings.controlsMarginRight);
g_settings.buttonSpacing = Wh_GetIntSetting(L"AppearanceSettings.buttonSpacing");
g_settings.buttonCornerRadius = Int(L"AppearanceSettings.buttonCornerRadius", 0, 24, 14);
g_settings.titleFontSize = Int(L"AppearanceSettings.titleFontSize", 7, 24, 12);
g_settings.artistFontSize = Int(L"AppearanceSettings.artistFontSize", 7, 24, 10);
g_settings.textSpacing = Int(L"AppearanceSettings.textSpacing", 0, 20, 1);
g_settings.customButtonColor = Str(L"AppearanceSettings.customButtonColor", L"255 255 255");
g_settings.customTitleColor = Str(L"AppearanceSettings.customTitleColor", L"255 255 255");
g_settings.customArtistColor = Str(L"AppearanceSettings.customArtistColor", L"200 200 200");
g_settings.albumArtLeftClick = Str(L"BehaviorSettings.albumArtLeftClick", L"switch_session");
g_settings.albumArtRightClick = Str(L"BehaviorSettings.albumArtRightClick", L"none");
g_settings.albumArtMiddleClick = Str(L"BehaviorSettings.albumArtMiddleClick", L"none");
g_settings.albumArtLeftDoubleClick = Str(L"BehaviorSettings.albumArtLeftDoubleClick", L"none");
g_settings.albumArtRightDoubleClick = Str(L"BehaviorSettings.albumArtRightDoubleClick", L"none");
g_settings.albumArtWheelAction = Str(L"BehaviorSettings.albumArtWheelAction", L"switch_tracks");
g_settings.playerLeftClick = Str(L"BehaviorSettings.playerLeftClick", L"none");
g_settings.playerRightClick = Str(L"BehaviorSettings.playerRightClick", L"none");
g_settings.playerMiddleClick = Str(L"BehaviorSettings.playerMiddleClick", L"stop");
g_settings.playerLeftDoubleClick = Str(L"BehaviorSettings.playerLeftDoubleClick", L"none");
g_settings.playerRightDoubleClick = Str(L"BehaviorSettings.playerRightDoubleClick", L"none");
g_settings.playerWheelAction = Str(L"BehaviorSettings.playerWheelAction", L"none");
g_settings.hideWhenNoMedia = Wh_GetIntSetting(L"BehaviorSettings.hideWhenNoMedia") != 0;
g_settings.hideFullscreen = Wh_GetIntSetting(L"BehaviorSettings.hideFullscreen") != 0;
g_settings.idleHideSeconds = std::max(Wh_GetIntSetting(L"BehaviorSettings.idleHideSeconds"), 0);
g_settings.enableTreeDump = Wh_GetIntSetting(L"DebugSettings.enableTreeDump") != 0;
g_settings.showDebugBorders = Wh_GetIntSetting(L"DebugSettings.showDebugBorders") != 0;
if (g_settings.position == L"taskbar_left")
g_settings.position = L"taskbar_left_start";
else if (g_settings.position == L"taskbar_right")
g_settings.position = L"taskbar_right_start";
else if (g_settings.position == L"taskbar_after_start")
g_settings.position = L"taskbar_after_search_right";
else if (g_settings.position == L"taskbar_after_search")
g_settings.position = L"taskbar_after_search_right";
else if (g_settings.position == L"tray_before_omni")
g_settings.position = L"tray_before_omni_right";
else if (g_settings.position == L"tray_after_showdesktop")
g_settings.position = L"tray_after_showdesktop_right";
if (g_settings.albumArtLeftClick == L"open_app") g_settings.albumArtLeftClick = L"none";
if (g_settings.albumArtRightClick == L"open_app") g_settings.albumArtRightClick = L"none";
if (g_settings.albumArtMiddleClick== L"open_app") g_settings.albumArtMiddleClick= L"none";
if (g_settings.playerLeftClick == L"open_app") g_settings.playerLeftClick = L"none";
if (g_settings.playerRightClick == L"open_app") g_settings.playerRightClick = L"none";
if (g_settings.playerMiddleClick == L"open_app") g_settings.playerMiddleClick = L"none";
LOG(L"Settings loaded: position=%ls minW=%d maxW=%d h=%d bgType=%ls dump=%d",
g_settings.position.c_str(),
g_settings.playerMinWidth,
g_settings.playerMaxWidth,
g_settings.playerHeight,
g_settings.backgroundType.c_str(),
(int)g_settings.enableTreeDump);
}
static HWND FindCurrentProcessTaskbarWnd();
static void DispatchMediaUpdate();
static void ApplySettings();
static std::atomic<bool> g_unloading{false};
static std::atomic<bool> g_applyingSettings{false};
static HWND g_taskbarWnd = nullptr;
static Grid g_playerGrid = nullptr;
static FrameworkElement g_injectionParent = nullptr;
static std::atomic<bool> g_needsUiUpdate{false};
static HWND g_themeWatchWnd = nullptr;
static HANDLE g_themeWatchThread = nullptr;
static HANDLE g_themeWatchStop = nullptr;
static FrameworkElement g_trackedElement = nullptr;
static std::wstring g_trackPosition = L"";
static winrt::event_token g_layoutUpdateToken{};
using CTaskBand_GetTaskbarHost_t = void*(WINAPI*)(void*, void*);
using TaskbarHost_FrameHeight_t = int (WINAPI*)(void*);
using Std_Ref_Decref_t = void (WINAPI*)(void*);
static CTaskBand_GetTaskbarHost_t CTaskBand_GetTaskbarHost_Original = nullptr;
static TaskbarHost_FrameHeight_t TaskbarHost_FrameHeight_Original = nullptr;
static Std_Ref_Decref_t Std_Ref_Decref_Original = nullptr;
static void* CTaskBand_ITaskListWndSite_vftable = nullptr;
static bool g_taskbarViewDllLoaded = false;
using WindowThreadProc = void(*)(void*);
static bool RunFromWindowThread(HWND hWnd, WindowThreadProc proc, void* param) {
static const UINT kMsg =
RegisterWindowMessage(L"Windhawk_RunFromWindowThread_" WH_MOD_ID);
struct Payload { WindowThreadProc proc; void* param; };
DWORD tid = GetWindowThreadProcessId(hWnd, nullptr);
if (!tid) return false;
if (tid == GetCurrentThreadId()) {
proc(param);
return true;
}
HHOOK hook = SetWindowsHookExW(WH_CALLWNDPROC,
[](int code, WPARAM w, LPARAM l) -> LRESULT {
if (code == HC_ACTION) {
auto* cwp = reinterpret_cast<const CWPSTRUCT*>(l);
static const UINT kM =
RegisterWindowMessage(L"Windhawk_RunFromWindowThread_" WH_MOD_ID);
if (cwp->message == kM) {
auto* p = reinterpret_cast<Payload*>(cwp->lParam);
p->proc(p->param);
}
}
return CallNextHookEx(nullptr, code, w, l);
}, nullptr, tid);
if (!hook) {
LOG(L"RunFromWindowThread: SetWindowsHookExW failed (%u)", GetLastError());
return false;
}
Payload pay{proc, param};
SendMessageW(hWnd, kMsg, 0, reinterpret_cast<LPARAM>(&pay));
UnhookWindowsHookEx(hook);
return true;
}
struct MediaState {
std::wstring title;
std::wstring artist;
std::wstring appUserModelId;
bool isPlaying = false;
bool hasMedia = false;
std::vector<BYTE> thumbnailBytes;
uint64_t thumbnailHash = 0;
std::vector<BYTE> appIconBytes;
std::wstring appIconKey;
};
static MediaState g_media;
static std::mutex g_mediaMtx;
static GlobalSystemMediaTransportControlsSessionManager g_sessionMgr = nullptr;
static GlobalSystemMediaTransportControlsSession g_currentSession = nullptr;
static std::mutex g_sessionMtx;
static winrt::event_token g_evSessionsChanged{};
static winrt::event_token g_evCurrentChanged{};
static winrt::event_token g_evMediaProps{};
static winrt::event_token g_evPlayback{};
static HANDLE g_mediaThread = nullptr;
static HANDLE g_mediaStopEvent = nullptr;
static bool IsSystemLightTheme() {
DWORD v = 0, sz = sizeof(v);
RegGetValueW(HKEY_CURRENT_USER,
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
L"AppsUseLightTheme", RRF_RT_DWORD, nullptr, &v, &sz);
return v != 0;
}
static bool IsLightTheme() {
if (g_settings.theme == L"light") return true;
if (g_settings.theme == L"dark") return false;
return IsSystemLightTheme();
}
static winrt::Windows::UI::Color TextColor() {
if (g_settings.theme == L"custom") {
int r = 255, g = 255, b = 255;
size_t pos1 = g_settings.customTitleColor.find(L' ');
size_t pos2 = g_settings.customTitleColor.find(L' ', pos1 + 1);
if (pos1 != std::wstring::npos && pos2 != std::wstring::npos) {
try {
r = std::stoi(g_settings.customTitleColor.substr(0, pos1));
g = std::stoi(g_settings.customTitleColor.substr(pos1 + 1, pos2 - pos1 - 1));
b = std::stoi(g_settings.customTitleColor.substr(pos2 + 1));
r = std::clamp(r, 0, 255);
g = std::clamp(g, 0, 255);
b = std::clamp(b, 0, 255);
} catch (...) {}
}
return winrt::Windows::UI::Color{0xE5, (BYTE)r, (BYTE)g, (BYTE)b};
}
return IsLightTheme()
? winrt::Windows::UI::Color{0xE5, 0x00, 0x00, 0x00}
: winrt::Windows::UI::Color{0xE5, 0xFF, 0xFF, 0xFF};
}
static winrt::Windows::UI::Color ArtistColor() {
if (g_settings.theme == L"custom") {
int r = 200, g = 200, b = 200;
size_t pos1 = g_settings.customArtistColor.find(L' ');
size_t pos2 = g_settings.customArtistColor.find(L' ', pos1 + 1);
if (pos1 != std::wstring::npos && pos2 != std::wstring::npos) {
try {
r = std::stoi(g_settings.customArtistColor.substr(0, pos1));
g = std::stoi(g_settings.customArtistColor.substr(pos1 + 1, pos2 - pos1 - 1));
b = std::stoi(g_settings.customArtistColor.substr(pos2 + 1));
r = std::clamp(r, 0, 255);
g = std::clamp(g, 0, 255);
b = std::clamp(b, 0, 255);
} catch (...) {}
}
return winrt::Windows::UI::Color{0x99, (BYTE)r, (BYTE)g, (BYTE)b};
}
auto c = TextColor();
return winrt::Windows::UI::Color{0x99, c.R, c.G, c.B};
}
static winrt::Windows::UI::Color ButtonColor() {
if (g_settings.theme == L"custom") {
int r = 255, g = 255, b = 255;
size_t pos1 = g_settings.customButtonColor.find(L' ');
size_t pos2 = g_settings.customButtonColor.find(L' ', pos1 + 1);
if (pos1 != std::wstring::npos && pos2 != std::wstring::npos) {
try {
r = std::stoi(g_settings.customButtonColor.substr(0, pos1));
g = std::stoi(g_settings.customButtonColor.substr(pos1 + 1, pos2 - pos1 - 1));
b = std::stoi(g_settings.customButtonColor.substr(pos2 + 1));
r = std::clamp(r, 0, 255);
g = std::clamp(g, 0, 255);
b = std::clamp(b, 0, 255);
} catch (...) {}
}
return winrt::Windows::UI::Color{0xFF, (BYTE)r, (BYTE)g, (BYTE)b};
}
return TextColor();
}
static winrt::Windows::UI::Color PanelBgColor() {
if (g_settings.backgroundType != L"solid") {
return winrt::Windows::UI::Color{0x00, 0x00, 0x00, 0x00};
}
BYTE a = (BYTE)std::clamp((int)(g_settings.backgroundOpacity * 2.55), 0, 255);
int r = 32, g = 32, b = 32;
size_t pos1 = g_settings.backgroundColor.find(L' ');
size_t pos2 = g_settings.backgroundColor.find(L' ', pos1 + 1);
if (pos1 != std::wstring::npos && pos2 != std::wstring::npos) {
try {
r = std::stoi(g_settings.backgroundColor.substr(0, pos1));
g = std::stoi(g_settings.backgroundColor.substr(pos1 + 1, pos2 - pos1 - 1));
b = std::stoi(g_settings.backgroundColor.substr(pos2 + 1));
r = std::clamp(r, 0, 255);
g = std::clamp(g, 0, 255);
b = std::clamp(b, 0, 255);
} catch (...) {}
}
return winrt::Windows::UI::Color{
a,
(BYTE)r,
(BYTE)g,
(BYTE)b
};
}
static SolidColorBrush MakeBrush(winrt::Windows::UI::Color c) {
SolidColorBrush b; b.Color(c); return b;
}
static Brush MakeBackgroundBrush() {
auto& t = g_settings.backgroundType;
if (t == L"acrylic") {
try {
winrt::Windows::UI::Xaml::Media::AcrylicBrush brush;
brush.BackgroundSource(
winrt::Windows::UI::Xaml::Media::AcrylicBackgroundSource::HostBackdrop);
auto col = IsLightTheme()
? winrt::Windows::UI::Color{0x99, 0xF3, 0xF3, 0xF3}
: winrt::Windows::UI::Color{0x99, 0x20, 0x20, 0x20};
brush.TintColor(col);
brush.TintOpacity(0.6);
brush.FallbackColor(IsLightTheme()
? winrt::Windows::UI::Color{0xCC, 0xF3, 0xF3, 0xF3}
: winrt::Windows::UI::Color{0xCC, 0x20, 0x20, 0x20});
return brush;
} catch (...) {
LOG(L"MakeBackgroundBrush: AcrylicBrush failed, falling back to solid");
}
}
if (t == L"mica" || t == L"mica_alt") {
auto col = IsLightTheme()
? winrt::Windows::UI::Color{0x33, 0xF3, 0xF3, 0xF3}
: winrt::Windows::UI::Color{0x33, 0x20, 0x20, 0x20};
return MakeBrush(col);
}
if (t == L"solid") {
return MakeBrush(PanelBgColor());
}
return MakeBrush({0x00, 0x00, 0x00, 0x00});
}
static FrameworkElement FindChildByName(
FrameworkElement const& root,
std::wstring_view name,
int depth = 32)
{
if (!root || depth == 0) return nullptr;
int n = VisualTreeHelper::GetChildrenCount(root);
for (int i = 0; i < n; ++i) {
auto child = VisualTreeHelper::GetChild(root, i).try_as<FrameworkElement>();
if (!child) continue;
if (child.Name() == name) return child;
if (auto found = FindChildByName(child, name, depth - 1)) return found;
}
return nullptr;
}
static void DumpXamlTree(DependencyObject const& node, int depth, int maxDepth) {
if (!node || depth > maxDepth) return;
std::wstring indent(depth * 2, L' ');
auto fe = node.try_as<FrameworkElement>();
std::wstring name = fe ? std::wstring(fe.Name()) : L"";
winrt::hstring typeHstr = winrt::get_class_name(node);
std::wstring type = std::wstring(typeHstr);
auto dot = type.rfind(L'.');
if (dot != std::wstring::npos) type = type.substr(dot + 1);
int col = fe ? Grid::GetColumn(fe) : -1;
if (!name.empty())
LOG(L"%ls[%ls] name='%ls' col=%d", indent.c_str(), type.c_str(), name.c_str(), col);
else
LOG(L"%ls[%ls]", indent.c_str(), type.c_str());
int n = VisualTreeHelper::GetChildrenCount(node);
for (int i = 0; i < n; ++i) {
auto child = VisualTreeHelper::GetChild(node, i);
if (child) DumpXamlTree(child, depth + 1, maxDepth);
}
}
static constexpr wchar_t kGridName[] = L"FluentMediaBar";
static constexpr wchar_t kArtImageName[] = L"FluentMedia_Art";
static constexpr wchar_t kAppIconImageName[]= L"FluentMedia_AppIcon";
static constexpr wchar_t kTextStackName[] = L"FluentMedia_TextStack";
static constexpr wchar_t kTitleBlockName[] = L"FluentMedia_Title";
static constexpr wchar_t kArtistBlockName[] = L"FluentMedia_Artist";
static constexpr wchar_t kPlayBtnName[] = L"FluentMedia_Play";
static constexpr wchar_t kPrevBtnName[] = L"FluentMedia_Prev";
static constexpr wchar_t kNextBtnName[] = L"FluentMedia_Next";
static int g_idleSeconds = 0;
static bool g_hiddenByIdle = false;
static void SendMediaCommandAsync(int cmd) {
std::thread([cmd]() {
if (g_unloading) return;
winrt::init_apartment(winrt::apartment_type::multi_threaded);
try {
GlobalSystemMediaTransportControlsSession session{nullptr};
{ std::lock_guard<std::mutex> lk(g_sessionMtx); session = g_currentSession; }
if (session) {
switch (cmd) {
case 1: session.TrySkipPreviousAsync().get(); break;
case 2: session.TryTogglePlayPauseAsync().get(); break;
case 3: session.TrySkipNextAsync().get(); break;
}
}
} catch (...) {}
winrt::uninit_apartment();
}).detach();
}
static void FetchMediaPropertiesAsync();
static void FetchPlaybackInfoAsync();
static void OnSessionsChanged();
static void AttachToSession(GlobalSystemMediaTransportControlsSession session);
static void SwitchMediaSession() {
GlobalSystemMediaTransportControlsSession nextSession{nullptr};
{
std::lock_guard<std::mutex> lk(g_sessionMtx);
if (!g_sessionMgr) return;
try {
auto sessions = g_sessionMgr.GetSessions();
int count = (int)sessions.Size();
if (count <= 1) return;
int currentIndex = -1;
if (g_currentSession) {
auto curId = g_currentSession.SourceAppUserModelId();
for (int i = 0; i < count; ++i) {
if (sessions.GetAt(i).SourceAppUserModelId() == curId) {
currentIndex = i;
break;
}
}
}
int nextIndex = (currentIndex + 1) % count;
nextSession = sessions.GetAt(nextIndex);
} catch (...) { return; }
}
if (nextSession) {
LOG(L"Switching to next media session");
AttachToSession(nextSession);
}
}
static void StopMediaPlayback() {
std::thread([]() {
if (g_unloading) return;
winrt::init_apartment(winrt::apartment_type::multi_threaded);
try {
GlobalSystemMediaTransportControlsSession session{nullptr};
{ std::lock_guard<std::mutex> lk(g_sessionMtx); session = g_currentSession; }
if (session) {
session.TryStopAsync().get();
}
} catch (...) {}
winrt::uninit_apartment();
}).detach();
}
static void ExecuteMediaAction(const std::wstring& action) {
if (action == L"none") {
return;
} else if (action == L"switch_session") {
SwitchMediaSession();
} else if (action == L"play_pause") {
SendMediaCommandAsync(2);
DispatchMediaUpdate();
} else if (action == L"next_track") {
SendMediaCommandAsync(3);
DispatchMediaUpdate();
} else if (action == L"prev_track") {
SendMediaCommandAsync(1);
DispatchMediaUpdate();
} else if (action == L"stop") {
StopMediaPlayback();
}
}
static std::vector<BYTE> FetchAppIconBytes(const std::wstring& appUserModelId, int iconSize) {
std::vector<BYTE> result;
if (appUserModelId.empty()) return result;
HICON hIcon = nullptr;
struct FindData { const std::wstring* aumid; HICON icon; DWORD pid; };
FindData fd{&appUserModelId, nullptr, 0};
EnumWindows([](HWND hWnd, LPARAM lParam) -> BOOL {
auto* fd2 = reinterpret_cast<FindData*>(lParam);
if (!IsWindowVisible(hWnd)) return TRUE;
DWORD pid = 0;
GetWindowThreadProcessId(hWnd, &pid);
if (!pid) return TRUE;
wchar_t procPath[MAX_PATH] = {};
HANDLE hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (hProc) {
DWORD sz = MAX_PATH;
QueryFullProcessImageNameW(hProc, 0, procPath, &sz);
CloseHandle(hProc);
}
std::wstring procName = procPath;
auto slash = procName.rfind(L'\\');
if (slash != std::wstring::npos) procName = procName.substr(slash + 1);
auto dot = procName.rfind(L'.');
if (dot != std::wstring::npos) procName = procName.substr(0, dot);
const std::wstring& aumid = *fd2->aumid;
std::wstring aumidLower = aumid;
std::wstring procLower = procName;
for (auto& c : aumidLower) c = towlower(c);
for (auto& c : procLower) c = towlower(c);
if (aumidLower.find(procLower) == std::wstring::npos &&
procLower.find(aumidLower) == std::wstring::npos)
return TRUE;
HICON icon = (HICON)GetClassLongPtrW(hWnd, GCLP_HICONSM);
if (!icon) icon = (HICON)GetClassLongPtrW(hWnd, GCLP_HICON);
if (!icon) icon = (HICON)SendMessageW(hWnd, WM_GETICON, ICON_SMALL, 0);
if (!icon) icon = (HICON)SendMessageW(hWnd, WM_GETICON, ICON_BIG, 0);
if (icon) {
fd2->icon = icon;
fd2->pid = pid;
return FALSE;
}
return TRUE;
}, reinterpret_cast<LPARAM>(&fd));
hIcon = fd.icon;
if (!hIcon && fd.pid) {
wchar_t procPath[MAX_PATH] = {};
HANDLE hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, fd.pid);
if (hProc) {
DWORD sz = MAX_PATH;
QueryFullProcessImageNameW(hProc, 0, procPath, &sz);
CloseHandle(hProc);
}
if (procPath[0]) {
SHFILEINFOW sfi{};
if (SHGetFileInfoW(procPath, 0, &sfi, sizeof(sfi),
SHGFI_ICON | SHGFI_SMALLICON))
hIcon = sfi.hIcon;
}
}
if (!hIcon) return result;
ICONINFO ii{};
if (!GetIconInfo(hIcon, &ii)) return result;
BITMAP bm{};
GetObjectW(ii.hbmColor ? ii.hbmColor : ii.hbmMask, sizeof(bm), &bm);
int w = bm.bmWidth ? bm.bmWidth : iconSize;
int h = bm.bmHeight ? bm.bmHeight : iconSize;
HDC hdc = CreateCompatibleDC(nullptr);
BITMAPINFO bi{};
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = -h;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
std::vector<BYTE> pixels(w * h * 4, 0);
HBITMAP hBmp = CreateCompatibleBitmap(GetDC(nullptr), w, h);
HBITMAP hOld = (HBITMAP)SelectObject(hdc, hBmp);
DrawIconEx(hdc, 0, 0, hIcon, w, h, 0, nullptr, DI_NORMAL);
GetDIBits(hdc, hBmp, 0, h, pixels.data(), &bi, DIB_RGB_COLORS);
SelectObject(hdc, hOld);
DeleteObject(hBmp);
DeleteDC(hdc);
if (ii.hbmColor) DeleteObject(ii.hbmColor);
if (ii.hbmMask) DeleteObject(ii.hbmMask);
for (int i = 0; i + 3 < (int)pixels.size(); i += 4) {
std::swap(pixels[i], pixels[i + 2]);
}
if (w != iconSize || h != iconSize) {
std::vector<BYTE> scaled(iconSize * iconSize * 4, 0);
for (int dy = 0; dy < iconSize; ++dy) {
for (int dx = 0; dx < iconSize; ++dx) {
int sx = dx * w / iconSize;
int sy = dy * h / iconSize;
int si = (sy * w + sx) * 4;
int di = (dy * iconSize + dx) * 4;
scaled[di+0] = pixels[si+0];
scaled[di+1] = pixels[si+1];
scaled[di+2] = pixels[si+2];
scaled[di+3] = pixels[si+3];
}
}
result = std::move(scaled);
} else {
result = std::move(pixels);
}
return result;
}