forked from ALTaleX531/OpenGlass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaptionTextHandler.cpp
More file actions
1358 lines (1250 loc) · 41.4 KB
/
Copy pathCaptionTextHandler.cpp
File metadata and controls
1358 lines (1250 loc) · 41.4 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 "pch.h"
#include "MsstyleInternals.hpp"
#include "CaptionTextHandler.hpp"
#include "Shared.hpp"
#include "GlassKernel.hpp"
#include "uDWMProjection.hpp"
#include "dwmcoreProjection.hpp"
#include "dcompPrivates.hpp"
#include "CustomThemeAtlasLoader.hpp"
using namespace OpenGlass;
namespace OpenGlass::CaptionTextHandler
{
int WINAPI MyDrawTextW(
HDC hdc,
LPCWSTR lpchText,
int cchText,
LPRECT lprc,
UINT format
);
HBITMAP WINAPI MyCreateBitmap(
int nWidth,
int nHeight,
UINT nPlanes,
UINT nBitCount,
const void* lpBits
);
HRESULT MyIWICImagingFactory2_CreateBitmapFromHBITMAP(
IWICImagingFactory2* This,
HBITMAP hBitmap,
HPALETTE hPalette,
WICBitmapAlphaChannelOption options,
IWICBitmap** ppIBitmap
);
HRESULT MyCText_ValidateResources(uDWM::CText* This);
HRESULT MyCText_InitializeVisualTreeClone(uDWM::CText* This, uDWM::CText* clonedVisual, UINT cloneOption);
HRESULT MyCText_CloneVisualTree(uDWM::CText* This, uDWM::CText** clonedVisual, bool unknown1, bool unknown2, bool unknown3);
HRESULT MyCText_scalar_deleting_destructor(uDWM::CText* This, BYTE flag);
HRESULT MyCChannel_MatrixTransformUpdate(dwmcore::CChannel* This, UINT handleId, MilMatrix3x2D* matrix);
void MyID2D1DeviceContext_DrawTextLayout(
ID2D1DeviceContext* This,
D2D1_POINT_2F origin,
IDWriteTextLayout* textLayout,
ID2D1Brush* defaultFillBrush,
D2D1_DRAW_TEXT_OPTIONS options
);
HRESULT MyICompositionGraphicsDevice_CreateDrawingSurface(
abi::ICompositionGraphicsDevice* This,
abi::Size sizePixels,
abi::DirectXPixelFormat pixelFormat,
abi::DirectXAlphaMode alphaMode,
abi::ICompositionDrawingSurface** result
);
HRESULT MyICompositionSurfaceBrush2_put_Offset(
abi::ICompositionSurfaceBrush2* This,
abi::Vector2 value
);
HRESULT MyCDWriteText_ValidateVisual(uDWM::CDWriteText* This);
HRESULT MyCDWriteText_UpdateOffset(uDWM::CDWriteText* This);
HRESULT MyCDWriteText_SetSize(uDWM::CDWriteText* This, const SIZE* size);
HRESULT MyCDWriteText_InitializeVisualTreeClone(uDWM::CDWriteText* This, uDWM::CDWriteText* clonedVisual, UINT cloneOption);
HRESULT MyCDWriteText_scalar_deleting_destructor(uDWM::CDWriteText* This, BYTE flag);
decltype(&MyDrawTextW) g_DrawTextW_Org{ nullptr };
decltype(&MyCreateBitmap) g_CreateBitmap_Org{ nullptr };
decltype(&MyIWICImagingFactory2_CreateBitmapFromHBITMAP) g_IWICImagingFactory2_CreateBitmapFromHBITMAP_Org{ nullptr };
decltype(&MyCText_ValidateResources) g_CText_ValidateResources_Org{ nullptr };
decltype(&MyCText_InitializeVisualTreeClone) g_CText_InitializeVisualTreeClone_Org{ nullptr };
decltype(&MyCText_CloneVisualTree) g_CText_CloneVisualTree_Org{ nullptr };
decltype(&MyCText_scalar_deleting_destructor) g_CText_scalar_deleting_destructor_Org{ nullptr };
decltype(&MyCText_scalar_deleting_destructor)* g_CText_scalar_deleting_destructor_Org_Address{ nullptr };
decltype(&MyCChannel_MatrixTransformUpdate) g_CChannel_MatrixTransformUpdate_Org{ nullptr };
decltype(&MyIWICImagingFactory2_CreateBitmapFromHBITMAP)* g_IWICImagingFactory2_CreateBitmapFromHBITMAP_Org_Address{ nullptr };
decltype(&MyID2D1DeviceContext_DrawTextLayout) g_ID2D1DeviceContext_DrawTextLayout_Org{ nullptr };
decltype(&MyID2D1DeviceContext_DrawTextLayout)* g_ID2D1DeviceContext_DrawTextLayout_Org_Address{ nullptr };
decltype(&MyICompositionGraphicsDevice_CreateDrawingSurface) g_ICompositionGraphicsDevice_CreateDrawingSurface_Org{ nullptr };
decltype(&MyICompositionGraphicsDevice_CreateDrawingSurface)* g_ICompositionGraphicsDevice_CreateDrawingSurface_Org_Address{ nullptr };
decltype(&MyICompositionSurfaceBrush2_put_Offset) g_ICompositionSurfaceBrush2_put_Offset_Org{ nullptr };
decltype(&MyICompositionSurfaceBrush2_put_Offset)* g_ICompositionSurfaceBrush2_put_Offset_Org_Address{ nullptr };
decltype(&MyCDWriteText_ValidateVisual) g_CDWriteText_ValidateVisual_Org{ nullptr };
decltype(&MyCDWriteText_UpdateOffset) g_CDWriteText_UpdateOffset_Org{ nullptr };
decltype(&MyCDWriteText_UpdateOffset)* g_CDWriteText_UpdateOffset_Org_Address{ nullptr };
decltype(&MyCDWriteText_SetSize) g_CDWriteText_SetSize_Org{ nullptr };
decltype(&MyCDWriteText_SetSize)* g_CDWriteText_SetSize_Org_Address{ nullptr };
decltype(&MyCDWriteText_InitializeVisualTreeClone) g_CDWriteText_InitializeVisualTreeClone_Org{ nullptr };
decltype(&MyCDWriteText_scalar_deleting_destructor) g_CDWriteText_scalar_deleting_destructor_Org{ nullptr };
decltype(&MyCDWriteText_scalar_deleting_destructor)* g_CDWriteText_scalar_deleting_destructor_Org_Address{ nullptr };
static union
{
uDWM::CText* g_textVisual;
uDWM::CDWriteText* g_dwriteTextVisual;
};
uDWM::CTopLevelWindow* g_window{ nullptr };
// original sizes, no glow included
static union
{
SIZE g_textSize;
abi::Size g_textSizeF;
};
COLORREF g_captionActiveColor{};
COLORREF g_captionInactiveColor{};
COLORREF g_captionActiveColorMaximized{};
COLORREF g_captionInactiveColorMaximized{};
MARGINS g_contentMargins{}, g_sizingMargins{};
struct CWindowState
{
bool active{};
bool maximized{};
LONG windowRectLeft{};
};
std::unordered_map<uDWM::CVisual*, CWindowState> g_textVisualStateMap{};
winrt::com_ptr<ID2D1DCRenderTarget> g_textGlowRT{};
winrt::com_ptr<ID2D1Bitmap1> g_textGlowD2DBitmap{};
winrt::com_ptr<ID2D1Effect> g_textGlowEffect{};
winrt::com_ptr<ID2D1Effect> g_textMorphologyEffect{};
COLORREF g_textGlowColor{};
int g_textGlowSize{};
int g_textGlowIntensity{};
int g_centerCaption{ 0 };
void CalculateRealizedTextGlowParams(int textGlowMode);
}
int WINAPI CaptionTextHandler::MyDrawTextW(
HDC hdc,
LPCWSTR lpchText,
int cchText,
LPRECT lprc,
UINT format
)
{
int result{ 0 };
auto drawTextCallback = [](HDC hdc, LPWSTR pszText, int cchText, LPRECT prc, UINT dwFlags, LPARAM lParam) static -> int
{
return *reinterpret_cast<int*>(lParam) = g_DrawTextW_Org(hdc, pszText, cchText, prc, dwFlags);
};
if ((format & DT_CALCRECT))
{
return g_DrawTextW_Org(hdc, lpchText, cchText, lprc, format);
}
// clear the background, so the text can be shown transparent
// with this hack, we don't need to hook FillRect any more
BITMAP bmp{};
if (GetObjectW(GetCurrentObject(hdc, OBJ_BITMAP), sizeof(bmp), &bmp) && bmp.bmBits)
{
memset(bmp.bmBits, 0, 4 * bmp.bmWidth * bmp.bmHeight);
}
OffsetRect(lprc, g_textGlowSize, g_textGlowSize);
const auto& windowState = g_textVisualStateMap[g_dwriteTextVisual];
const auto textColor = GetTextColor(hdc);
const auto textColorOverride = windowState.active ? (windowState.maximized ? g_captionActiveColorMaximized : g_captionActiveColor) : (windowState.maximized ? g_captionInactiveColorMaximized : g_captionInactiveColor);
DTTOPTS options
{
sizeof(DTTOPTS),
DTT_TEXTCOLOR | DTT_COMPOSITED | DTT_CALLBACK | DTT_GLOWSIZE,
textColorOverride != 0xFFFFFFFF ? textColorOverride : textColor,
0,
0,
0,
{},
0,
0,
0,
0,
FALSE,
0,
drawTextCallback,
(LPARAM)&result
};
auto glowDrawRect = *lprc;
if (Shared::g_textGlowMode == 1 || Shared::g_textGlowMode == 2)
{
glowDrawRect.left -= g_contentMargins.cxLeftWidth;
glowDrawRect.top -= g_contentMargins.cyTopHeight;
glowDrawRect.right += g_contentMargins.cxRightWidth;
glowDrawRect.bottom += g_contentMargins.cyBottomHeight;
}
else if (LOWORD(Shared::g_textGlowMode) == 3)
{
options.iGlowSize = g_textGlowSize;
glowDrawRect.left -= g_textGlowSize;
glowDrawRect.top -= g_textGlowSize;
glowDrawRect.right += g_textGlowSize;
glowDrawRect.bottom += g_textGlowSize;
}
const auto calcGlowClipRect = [&windowState](LPCRECT lprc, RECT& glowClipRect, bool mirrored)
{
LONG offset = 0;
offset += windowState.windowRectLeft;
offset -= g_textVisual->GetX();
offset -= g_centerCaption ? static_cast<LONG>(std::round(static_cast<DOUBLE>(g_textVisual->GetWidth() - g_textSize.cx) / 2.)) : 0l;
if (!mirrored)
{
glowClipRect.left = std::max(
glowClipRect.left,
lprc->left +
offset
);
}
else
{
glowClipRect.right = std::min(
glowClipRect.right,
lprc->right -
offset
);
}
};
auto glowClipRect = glowDrawRect;
calcGlowClipRect(lprc, glowClipRect, g_textVisual->IsRTLMirrored());
SaveDC(hdc);
const auto dcPaintScope = wil::scope_exit([hdc]
{
RestoreDC(hdc, -1);
});
IntersectClipRect(hdc, glowClipRect.left, glowClipRect.top, glowClipRect.right, glowClipRect.bottom);
if (Shared::g_textGlowMode == 1 || Shared::g_textGlowMode == 2)
{
if (!g_textGlowRT)
{
winrt::com_ptr<ID2D1Factory> factory{};
uDWM::CDesktopManager::GetInstance()->GetD2DDevice()->GetFactory(factory.put());
D2D1_RENDER_TARGET_PROPERTIES properties = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_SOFTWARE,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)
);
THROW_IF_FAILED(
factory->CreateDCRenderTarget(
&properties,
g_textGlowRT.put()
)
);
}
winrt::com_ptr<ID2D1DeviceContext> context{};
THROW_IF_FAILED(g_textGlowRT->QueryInterface(context.put()));
if (!g_textGlowD2DBitmap)
{
THROW_IF_FAILED(
context->CreateBitmap(
D2D1::SizeU(
Shared::g_textGlowBitmapInfo.bmiHeader.biWidth,
-Shared::g_textGlowBitmapInfo.bmiHeader.biHeight
),
Shared::g_textGlowBitmapPixels,
Shared::g_textGlowBitmapInfo.bmiHeader.biWidth * 4,
D2D1::BitmapProperties1(
D2D1_BITMAP_OPTIONS_NONE,
D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED
)
),
g_textGlowD2DBitmap.put()
)
);
}
RECT targetRect
{
lprc->left - g_textGlowSize,
lprc->top - g_textGlowSize,
lprc->right + g_textGlowSize,
lprc->bottom + g_textGlowSize
};
g_textGlowRT->BindDC(hdc, &targetRect);
g_textGlowRT->BeginDraw();
Util::DrawNineGridBitmap(
context.get(),
g_textGlowD2DBitmap.get(),
D2D1::RectF(
static_cast<float>(glowDrawRect.left),
static_cast<float>(glowDrawRect.top),
static_cast<float>(glowDrawRect.right),
static_cast<float>(glowDrawRect.bottom)
),
g_sizingMargins,
Shared::g_textGlowMode == 2 ? (windowState.active ? (windowState.maximized ? Shared::g_glowOpacityMaximized : Shared::g_glowOpacity) : (windowState.maximized ? Shared::g_glowOpacityInactiveMaximized : Shared::g_glowOpacityInactive)) : 1.f
);
LOG_IF_FAILED(g_textGlowRT->EndDraw());
/*{
FrameRect(
hdc,
&glowClipRect,
GetStockBrush(WHITE_BRUSH)
);
}*/
}
wil::unique_htheme hTheme{ OpenThemeData(nullptr, L"CompositedWindow::Window") };
if (hTheme)
{
THROW_IF_FAILED(
DrawThemeTextEx(
hTheme.get(),
hdc,
0,
0,
lpchText,
cchText,
format,
lprc,
&options
)
);
}
else
{
THROW_HR_IF_NULL(E_FAIL, hTheme);
result = g_DrawTextW_Org(hdc, lpchText, cchText, lprc, format);
}
// override that so we can use the correct param in CDrawImageInstruction::Create
lprc->left -= g_textGlowSize;
lprc->top -= g_textGlowSize;
lprc->right += g_textGlowSize;
lprc->bottom += g_textGlowSize;
return result;
}
HBITMAP WINAPI CaptionTextHandler::MyCreateBitmap(
int nWidth,
int nHeight,
[[maybe_unused]] UINT nPlanes,
[[maybe_unused]] UINT nBitCount,
[[maybe_unused]] const void* lpBits
)
{
if (!g_textVisual)
{
return g_CreateBitmap_Org(nWidth, nHeight, nPlanes, nBitCount, lpBits);
}
g_textSize = { nWidth, nHeight };
nWidth += g_textGlowSize * 2;
nHeight += g_textGlowSize * 2;
PVOID bits{ nullptr };
BITMAPINFO bitmapInfo{ {sizeof(bitmapInfo.bmiHeader), nWidth, -nHeight, 1, 32, BI_RGB} };
HBITMAP bitmap{ CreateDIBSection(nullptr, &bitmapInfo, DIB_RGB_COLORS, &bits, nullptr, 0) };
memset(bits, 0, sizeof(nWidth * nHeight * 4));
return bitmap;
}
HRESULT CaptionTextHandler::MyIWICImagingFactory2_CreateBitmapFromHBITMAP(
IWICImagingFactory2* This,
HBITMAP hBitmap,
HPALETTE hPalette,
[[maybe_unused]] WICBitmapAlphaChannelOption options,
IWICBitmap** ppIBitmap
)
{
return g_IWICImagingFactory2_CreateBitmapFromHBITMAP_Org(
This,
hBitmap,
hPalette,
WICBitmapAlphaChannelOption::WICBitmapUsePremultipliedAlpha,
ppIBitmap
);
}
HRESULT CaptionTextHandler::MyCText_ValidateResources(uDWM::CText* This)
{
if (g_centerCaption)
{
// update alignment transform
This->SetDirtyFlags(0x8000);
// redraw the text to get the width and height
This->SetDirtyFlags(0x1000);
}
if (!g_CText_scalar_deleting_destructor_Org)
{
g_CText_scalar_deleting_destructor_Org_Address = HookHelper::get_vftable_from<decltype(g_CText_scalar_deleting_destructor_Org)>(This);
HookHelper::PatchPointerT(
g_CText_scalar_deleting_destructor_Org_Address,
MyCText_scalar_deleting_destructor,
&g_CText_scalar_deleting_destructor_Org
);
}
if (g_window = uDWM::TryGetWindowFromVisual(This); g_window && g_window->GetData())
{
auto& windowState = g_textVisualStateMap[This];
windowState.active = g_window->TreatAsActiveWindow();
windowState.maximized = g_window->TreatAsMaximized();
RECT windowRect{};
g_window->GetActualWindowRect(&windowRect, true, false, true);
windowState.windowRectLeft = windowRect.left;
}
g_textVisual = This;
const auto hr = g_CText_ValidateResources_Org(This);
g_textVisual = nullptr;
g_window = nullptr;
return hr;
}
HRESULT CaptionTextHandler::MyCText_InitializeVisualTreeClone(uDWM::CText* This, uDWM::CText* clonedVisual, UINT cloneOption)
{
g_textVisualStateMap[clonedVisual] = g_textVisualStateMap[This];
return g_CText_InitializeVisualTreeClone_Org(This, clonedVisual, cloneOption);
}
HRESULT CaptionTextHandler::MyCText_CloneVisualTree(uDWM::CText* This, uDWM::CText** clonedVisual, bool unknown1, bool unknown2, bool unknown3)
{
const auto result = g_CText_CloneVisualTree_Org(This, clonedVisual, unknown1, unknown2, unknown3);
if (clonedVisual && *clonedVisual)
{
g_textVisualStateMap[*clonedVisual] = g_textVisualStateMap[This];
}
return result;
}
HRESULT CaptionTextHandler::MyCText_scalar_deleting_destructor(uDWM::CText* This, BYTE flag)
{
g_textVisualStateMap.erase(This);
return g_CText_scalar_deleting_destructor_Org(This, flag);
}
HRESULT CaptionTextHandler::MyCChannel_MatrixTransformUpdate(dwmcore::CChannel* This, UINT handleId, MilMatrix3x2D* matrix)
{
if (g_textVisual)
{
matrix->DX -= static_cast<DOUBLE>(g_textGlowSize);
matrix->DY -= static_cast<DOUBLE>(g_textGlowSize);
if (g_centerCaption == 1)
{
const DOUBLE offset = std::floor((static_cast<DOUBLE>(g_textVisual->GetWidth()) - static_cast<DOUBLE>(g_textSize.cx)) / 2.0);
matrix->DX += g_textVisual->IsRTLMirrored() ? -offset : offset;
}
else if (g_centerCaption == 2)
{
// Code shamefully stolen from AWM (Adapted for Windows 11)
// https://github.com/Dulappy/aero-window-manager/blob/fc44618f507e5a39c3d4ef7f66eaef61dbd01858/awmdll/awmdll.cpp#L1795
const DOUBLE parentWidth = static_cast<DOUBLE>(g_textVisual->GetTransformParent()->GetWidth());
const DOUBLE textWidth = static_cast<DOUBLE>(g_textSize.cx);
const DOUBLE visualWidth = static_cast<DOUBLE>(g_textVisual->GetWidth());
DOUBLE xoffset = std::floor((parentWidth - textWidth) / 2.0 + 0.5) - static_cast<DOUBLE>(g_textVisual->GetX());
xoffset = std::max(0.0, xoffset);
if (xoffset + textWidth >= visualWidth)
{
xoffset = std::max(0.0, (visualWidth - textWidth) / 2.0);
}
const DOUBLE offset = std::floor(xoffset);
matrix->DX += g_textVisual->IsRTLMirrored() ? -offset : offset;
}
}
return g_CChannel_MatrixTransformUpdate_Org(This, handleId, matrix);
}
void CaptionTextHandler::MyID2D1DeviceContext_DrawTextLayout(
ID2D1DeviceContext* This,
D2D1_POINT_2F origin,
IDWriteTextLayout* textLayout,
ID2D1Brush* defaultFillBrush,
D2D1_DRAW_TEXT_OPTIONS options
)
{
if (!g_dwriteTextVisual)
{
return g_ID2D1DeviceContext_DrawTextLayout_Org(
This,
origin,
textLayout,
defaultFillBrush,
options
);
}
winrt::com_ptr<ID2D1SolidColorBrush> solidColorBrush{};
if (FAILED(defaultFillBrush->QueryInterface(solidColorBrush.put())))
{
return g_ID2D1DeviceContext_DrawTextLayout_Org(
This,
origin,
textLayout,
defaultFillBrush,
options
);
}
const auto color = solidColorBrush->GetColor();
const auto cleanup = wil::scope_exit([&]
{
solidColorBrush->SetColor(color);
});
const auto& windowState = g_textVisualStateMap[g_dwriteTextVisual];
const auto textColorOverride = windowState.active ? (windowState.maximized ? g_captionActiveColorMaximized : g_captionActiveColor) : (windowState.maximized ? g_captionInactiveColorMaximized : g_captionInactiveColor);
if (textColorOverride != 0xFFFFFFFF)
{
solidColorBrush->SetColor(Color::FromAbgr(textColorOverride));
}
if (!g_textGlowSize)
{
return g_ID2D1DeviceContext_DrawTextLayout_Org(
This,
origin,
textLayout,
defaultFillBrush,
options
);
}
origin.x += g_textGlowSize;
origin.y += g_textGlowSize;
DWRITE_TEXT_METRICS metrics{};
THROW_IF_FAILED(
textLayout->GetMetrics(
&metrics
)
);
if (!metrics.width || !metrics.height)
{
return g_ID2D1DeviceContext_DrawTextLayout_Org(
This,
origin,
textLayout,
defaultFillBrush,
options
);
}
if (LOWORD(Shared::g_textGlowMode) == 3 && g_textGlowIntensity)
{
winrt::com_ptr<ID2D1BitmapRenderTarget> bitmapRT{};
THROW_IF_FAILED(
This->CreateCompatibleRenderTarget(
D2D1::SizeF(
std::ceil(metrics.left + metrics.width) + static_cast<float>(g_textGlowSize * 2),
std::ceil(metrics.top + metrics.height) + static_cast<float>(g_textGlowSize * 2)
),
bitmapRT.put()
)
);
bitmapRT->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE);
bitmapRT->BeginDraw();
bitmapRT->Clear();
bitmapRT->DrawTextLayout(
D2D1::Point2F(),
textLayout,
defaultFillBrush,
options
);
THROW_IF_FAILED(bitmapRT->EndDraw());
winrt::com_ptr<ID2D1Bitmap> bitmap{};
THROW_IF_FAILED(bitmapRT->GetBitmap(bitmap.put()));
if (!g_textMorphologyEffect)
{
THROW_IF_FAILED(
This->CreateEffect(
CLSID_D2D1Morphology,
g_textMorphologyEffect.put()
)
);
THROW_IF_FAILED(
g_textMorphologyEffect->SetValue(
D2D1_MORPHOLOGY_PROP_MODE,
D2D1_MORPHOLOGY_MODE_DILATE
)
);
THROW_IF_FAILED(
g_textMorphologyEffect->SetValue(
D2D1_MORPHOLOGY_PROP_WIDTH,
3 + g_textGlowSize / 12
)
);
THROW_IF_FAILED(
g_textMorphologyEffect->SetValue(
D2D1_MORPHOLOGY_PROP_HEIGHT,
3 + g_textGlowSize / 12
)
);
}
if (!g_textGlowEffect)
{
THROW_IF_FAILED(
This->CreateEffect(
CLSID_D2D1Shadow,
g_textGlowEffect.put()
)
);
THROW_IF_FAILED(
g_textGlowEffect->SetValue(
D2D1_SHADOW_PROP_OPTIMIZATION,
D2D1_GAUSSIANBLUR_OPTIMIZATION_SPEED
)
);
g_textGlowEffect->SetInputEffect(0, g_textMorphologyEffect.get());
}
THROW_IF_FAILED(
g_textGlowEffect->SetValue(
D2D1_SHADOW_PROP_COLOR,
Color::FromAbgr(g_textGlowColor | (std::min(g_textGlowIntensity, 255) << 24), false)
)
);
THROW_IF_FAILED(
g_textGlowEffect->SetValue(
D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION,
std::max(
0.f,
(static_cast<float>(g_textGlowSize)) / 3.f + 0.5f
)
)
);
g_textMorphologyEffect->SetInput(0, bitmap.get());
This->DrawImage(
g_textGlowEffect.get(),
&origin,
nullptr,
D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
D2D1_COMPOSITE_MODE_SOURCE_COPY
);
This->DrawImage(
bitmap.get(),
&origin,
nullptr,
D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR
);
return;
}
if (Shared::g_textGlowMode == 1 || Shared::g_textGlowMode == 2)
{
if (!g_textGlowD2DBitmap)
{
THROW_IF_FAILED(
This->CreateBitmap(
D2D1::SizeU(
Shared::g_textGlowBitmapInfo.bmiHeader.biWidth,
-Shared::g_textGlowBitmapInfo.bmiHeader.biHeight
),
Shared::g_textGlowBitmapPixels,
Shared::g_textGlowBitmapInfo.bmiHeader.biWidth * 4,
D2D1::BitmapProperties1(
D2D1_BITMAP_OPTIONS_NONE,
D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED
)
),
g_textGlowD2DBitmap.put()
)
);
}
DWRITE_OVERHANG_METRICS overhangs{};
THROW_IF_FAILED(
textLayout->GetOverhangMetrics(
&overhangs
)
);
const D2D1_RECT_F textBoundingBox
{
origin.x + (std::floor(-overhangs.left) - 1.f),
origin.y + std::floor(metrics.top) - 1.f,
origin.x + (std::floor(-overhangs.left) - 1.f) + g_textSizeF.Width,
origin.y + std::floor(metrics.top + metrics.height) + 1.f
};
D2D1_RECT_F glowRect
{
textBoundingBox.left - static_cast<float>(g_contentMargins.cxLeftWidth),
textBoundingBox.top - static_cast<float>(g_contentMargins.cyTopHeight),
textBoundingBox.right + static_cast<float>(g_contentMargins.cxRightWidth),
textBoundingBox.bottom + static_cast<float>(g_contentMargins.cyBottomHeight)
};
const auto calcGlowClipRect = [&windowState](const D2D1_RECT_F& textRect, D2D1_RECT_F& glowClipRect, bool mirrored)
{
LONG offset = 0;
offset += windowState.windowRectLeft;
offset -= g_dwriteTextVisual->GetX();
offset -= g_centerCaption ? static_cast<LONG>(std::round((static_cast<float>(g_dwriteTextVisual->GetWidth()) - g_textSizeF.Width) / 2.f)) : 0l;
if (!mirrored)
{
glowClipRect.left = std::max(
glowClipRect.left,
textRect.left +
offset
);
}
else
{
glowClipRect.right = std::min(
glowClipRect.right,
textRect.right -
offset
);
}
};
calcGlowClipRect(textBoundingBox, glowRect, g_dwriteTextVisual->IsRTLMirrored());
THROW_IF_FAILED(
Util::DrawNineGridBitmap(
This,
g_textGlowD2DBitmap.get(),
glowRect,
g_sizingMargins,
Shared::g_textGlowMode == 2 ? (windowState.active ? (windowState.maximized ? Shared::g_glowOpacityMaximized : Shared::g_glowOpacity) : (windowState.maximized ? Shared::g_glowOpacityInactiveMaximized : Shared::g_glowOpacityInactive)) : 1.f
)
);
/*{
winrt::com_ptr<ID2D1SolidColorBrush> brush{};
THROW_IF_FAILED(This->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Red), brush.put()));
This->DrawRectangle(
glowRect,
brush.get(),
1.f,
nullptr
);
}*/
}
return g_ID2D1DeviceContext_DrawTextLayout_Org(
This,
origin,
textLayout,
defaultFillBrush,
options
);
}
HRESULT CaptionTextHandler::MyICompositionGraphicsDevice_CreateDrawingSurface(
abi::ICompositionGraphicsDevice* This,
abi::Size sizePixels,
abi::DirectXPixelFormat pixelFormat,
abi::DirectXAlphaMode alphaMode,
abi::ICompositionDrawingSurface** result
)
{
if (g_dwriteTextVisual)
{
g_textSizeF = sizePixels;
sizePixels.Width += g_textGlowSize * 2;
sizePixels.Height += g_textGlowSize * 2;
}
return g_ICompositionGraphicsDevice_CreateDrawingSurface_Org(
This,
sizePixels,
pixelFormat,
alphaMode,
result
);
}
HRESULT CaptionTextHandler::MyICompositionSurfaceBrush2_put_Offset(
abi::ICompositionSurfaceBrush2* This,
abi::Vector2 value
)
{
if (g_dwriteTextVisual)
{
value.Y -= g_textGlowSize;
value.X -= g_textGlowSize;
// offset, glowSize
// 40, 17
// 11, 17
// CDWriteVisual is rtl mirrored, but CSpriteVisual is not rtl mirrored
if (auto& offset = const_cast<POINT&>(g_dwriteTextVisual->GetOffset()); g_dwriteTextVisual->IsRTLMirrored())
{
if (offset.x > g_textGlowSize)
{
value.X += g_textGlowSize;
}
else
{
value.X += g_textGlowSize + g_textGlowSize - offset.x;
}
}
else
{
value.X += offset.x - std::max(offset.x - g_textGlowSize, 0l);
}
if (g_centerCaption > 0)
{
if (g_centerCaption == 1)
{
const float offset = std::floor((static_cast<float>(g_dwriteTextVisual->GetWidth()) - g_textSizeF.Width) / 2.0f);
value.X += g_dwriteTextVisual->IsRTLMirrored() ? -offset : offset;
}
else if (g_centerCaption == 2)
{
// Code shamefully stolen and adapted from AWM
// https://github.com/Dulappy/aero-window-manager/blob/fc44618f507e5a39c3d4ef7f66eaef61dbd01858/awmdll/awmdll.cpp#L1795
const float parentWidth = static_cast<float>(g_dwriteTextVisual->GetTransformParent()->GetWidth());
const float textWidth = g_textSizeF.Width;
const float visualWidth = static_cast<float>(g_dwriteTextVisual->GetWidth());
float xoffset = std::floor((parentWidth - textWidth) / 2.0f + 0.5f) - static_cast<float>(g_dwriteTextVisual->GetX());
xoffset = std::max(0.0f, xoffset);
if (xoffset + textWidth >= visualWidth)
{
xoffset = std::max(0.0f, (visualWidth - textWidth) / 2.0f);
}
const float offset = std::floor(xoffset);
value.X += g_dwriteTextVisual->IsRTLMirrored() ? -offset : offset;
}
}
}
return g_ICompositionSurfaceBrush2_put_Offset_Org(
This,
value
);
}
HRESULT CaptionTextHandler::MyCDWriteText_ValidateVisual(uDWM::CDWriteText* This)
{
// 0x2 redraw text
// 0x8 offset changed
// 0x10 rtl mirrored changed
if ((This->GetDirtyFlags() & (0x8 | 0x10)))
{
This->SetDirtyFlags(0x2);
}
if ((This->GetDirtyFlags() & 0x2))
{
This->SetDirtyFlags(0x8);
}
if (!g_CDWriteText_scalar_deleting_destructor_Org)
{
g_CDWriteText_scalar_deleting_destructor_Org_Address = HookHelper::get_vftable_from<decltype(g_CDWriteText_scalar_deleting_destructor_Org)>(This);
HookHelper::PatchPointerT(
g_CDWriteText_scalar_deleting_destructor_Org_Address,
MyCDWriteText_scalar_deleting_destructor,
&g_CDWriteText_scalar_deleting_destructor_Org
);
}
if (!g_CDWriteText_UpdateOffset_Org)
{
PVOID CVisual_UpdateOffset_Org{ nullptr };
PVOID CSpriteVisual_SetSize_Org{ nullptr };
uDWM::g_projectionArray.ApplyToVariable("CVisual::UpdateOffset", CVisual_UpdateOffset_Org);
uDWM::g_projectionArray.ApplyToVariable("CSpriteVisual::SetSize", CSpriteVisual_SetSize_Org);
for (auto& vf : std::span{ HookHelper::get_vftable_from(This), 32})
{
if (vf == CVisual_UpdateOffset_Org)
{
g_CDWriteText_UpdateOffset_Org_Address = reinterpret_cast<decltype(g_CDWriteText_UpdateOffset_Org_Address)>(&vf);
HookHelper::PatchPointerT(
g_CDWriteText_UpdateOffset_Org_Address,
MyCDWriteText_UpdateOffset,
&g_CDWriteText_UpdateOffset_Org
);
}
if (vf == CSpriteVisual_SetSize_Org)
{
g_CDWriteText_SetSize_Org_Address = reinterpret_cast<decltype(g_CDWriteText_SetSize_Org_Address)>(&vf);
HookHelper::PatchPointerT(
g_CDWriteText_SetSize_Org_Address,
MyCDWriteText_SetSize,
&g_CDWriteText_SetSize_Org
);
}
}
}
if (g_window = uDWM::TryGetWindowFromVisual(This); g_window && g_window->GetData())
{
auto& windowState = g_textVisualStateMap[This];
windowState.active = g_window->TreatAsActiveWindow();
windowState.maximized = g_window->TreatAsMaximized();
RECT windowRect{};
g_window->GetActualWindowRect(&windowRect, true, false, true);
windowState.windowRectLeft = windowRect.left;
}
g_dwriteTextVisual = This;
const auto hr = g_CDWriteText_ValidateVisual_Org(This);
g_dwriteTextVisual = nullptr;
g_window = nullptr;
return hr;
}
HRESULT CaptionTextHandler::MyCDWriteText_UpdateOffset(uDWM::CDWriteText* This)
{
if (!g_textGlowSize)
{
return g_CDWriteText_UpdateOffset_Org(This);
}
// SpriteVisual will crop what exceeds its bounding rectangle,
// here we make it offset x minus the size of the glow,
// and add it back later in the ICompositionSurfaceBrush2::put_Offset method
//
// This gives us enough space to render the glow.
auto& offset = const_cast<POINT&>(This->GetOffset());
const auto actualOffsetX = offset.x;
if (!This->IsRTLMirrored())
{
offset.x = std::max(offset.x - g_textGlowSize, 0l);
}
const auto hr = g_CDWriteText_UpdateOffset_Org(This);
offset.x = actualOffsetX;
return hr;
}
HRESULT CaptionTextHandler::MyCDWriteText_SetSize(uDWM::CDWriteText* This, const SIZE* size)
{
if (!g_textGlowSize)
{
return g_CDWriteText_SetSize_Org(This, size);
}
const auto hr = g_CDWriteText_SetSize_Org(This, size);
// SpriteVisual will crop what exceeds its bounding rectangle,
// expand it to ensure enough space to render the glow.
auto& offset = const_cast<POINT&>(This->GetOffset());
if (This->IsRTLMirrored())
{
This->GetVisualProxy()->SetSize(
static_cast<double>(size->cx + offset.x - std::max(offset.x - g_textGlowSize, 0l)),
static_cast<double>(size->cy)
);
}
else
{
This->GetVisualProxy()->SetSize(
static_cast<double>(size->cx + offset.x - std::max(offset.x - g_textGlowSize, 0l) + g_textGlowSize),
static_cast<double>(size->cy)
);
}
return hr;
}
HRESULT CaptionTextHandler::MyCDWriteText_InitializeVisualTreeClone(uDWM::CDWriteText* This, uDWM::CDWriteText* clonedVisual, UINT cloneOption)
{
g_textVisualStateMap[clonedVisual] = g_textVisualStateMap[This];
return g_CDWriteText_InitializeVisualTreeClone_Org(This, clonedVisual, cloneOption);
}
HRESULT CaptionTextHandler::MyCDWriteText_scalar_deleting_destructor(uDWM::CDWriteText* This, BYTE flag)
{
g_textVisualStateMap.erase(This);
return g_CDWriteText_scalar_deleting_destructor_Org(This, flag);
}
void CaptionTextHandler::CalculateRealizedTextGlowParams(int textGlowMode)
{
if (textGlowMode == 0)
{
g_textGlowSize = 0;
}
else if (textGlowMode == 1 || textGlowMode == 2)
{
const auto themeHandle = CustomThemeAtlasLoader::GetThemeHandle();
CustomThemeAtlasLoader::MyGetThemeMargins(
themeHandle,
nullptr,
static_cast<int>(DWM_WINDOW_THEME_PART::TEXTGLOW),
0,
TMT_SIZINGMARGINS,
nullptr,
&g_sizingMargins
);
CustomThemeAtlasLoader::MyGetThemeMargins(
themeHandle,
nullptr,
static_cast<int>(DWM_WINDOW_THEME_PART::TEXTGLOW),
0,
TMT_CONTENTMARGINS,
nullptr,
&g_contentMargins
);
g_textGlowSize = std::max(
{
g_contentMargins.cxLeftWidth,
g_contentMargins.cxRightWidth,
g_contentMargins.cyTopHeight,
g_contentMargins.cyBottomHeight