/
NppDarkMode.cpp
3265 lines (2727 loc) · 81.6 KB
/
NppDarkMode.cpp
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
// This file is part of Notepad++ project
// Copyright (C)2021 adzm / Adam D. Walling
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// at your option any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "NppDarkMode.h"
#include "DarkMode/DarkMode.h"
#include "DarkMode/UAHMenuBar.h"
#include <dwmapi.h>
#include <uxtheme.h>
#include <vssym32.h>
#include "Parameters.h"
#include "resource.h"
#include <shlwapi.h>
#include <array>
#ifdef __GNUC__
#include <cmath>
#define WINAPI_LAMBDA WINAPI
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif
#else
#define WINAPI_LAMBDA
#endif
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#endif
// already added in project files
// keep for plugin authors
//#ifdef _MSC_VER
//#pragma comment(lib, "dwmapi.lib")
//#pragma comment(lib, "uxtheme.lib")
//#endif
constexpr COLORREF HEXRGB(DWORD rrggbb) {
// from 0xRRGGBB like natural #RRGGBB
// to the little-endian 0xBBGGRR
return
((rrggbb & 0xFF0000) >> 16) |
((rrggbb & 0x00FF00)) |
((rrggbb & 0x0000FF) << 16);
}
namespace NppDarkMode
{
struct Brushes
{
HBRUSH background = nullptr;
HBRUSH softerBackground = nullptr;
HBRUSH hotBackground = nullptr;
HBRUSH pureBackground = nullptr;
HBRUSH errorBackground = nullptr;
HBRUSH edgeBrush = nullptr;
HBRUSH hotEdgeBrush = nullptr;
HBRUSH disabledEdgeBrush = nullptr;
Brushes(const Colors& colors)
: background(::CreateSolidBrush(colors.background))
, softerBackground(::CreateSolidBrush(colors.softerBackground))
, hotBackground(::CreateSolidBrush(colors.hotBackground))
, pureBackground(::CreateSolidBrush(colors.pureBackground))
, errorBackground(::CreateSolidBrush(colors.errorBackground))
, edgeBrush(::CreateSolidBrush(colors.edge))
, hotEdgeBrush(::CreateSolidBrush(colors.hotEdge))
, disabledEdgeBrush(::CreateSolidBrush(colors.disabledEdge))
{}
~Brushes()
{
::DeleteObject(background); background = nullptr;
::DeleteObject(softerBackground); softerBackground = nullptr;
::DeleteObject(hotBackground); hotBackground = nullptr;
::DeleteObject(pureBackground); pureBackground = nullptr;
::DeleteObject(errorBackground); errorBackground = nullptr;
::DeleteObject(edgeBrush); edgeBrush = nullptr;
::DeleteObject(hotEdgeBrush); hotEdgeBrush = nullptr;
::DeleteObject(disabledEdgeBrush); disabledEdgeBrush = nullptr;
}
void change(const Colors& colors)
{
::DeleteObject(background);
::DeleteObject(softerBackground);
::DeleteObject(hotBackground);
::DeleteObject(pureBackground);
::DeleteObject(errorBackground);
::DeleteObject(edgeBrush);
::DeleteObject(hotEdgeBrush);
::DeleteObject(disabledEdgeBrush);
background = ::CreateSolidBrush(colors.background);
softerBackground = ::CreateSolidBrush(colors.softerBackground);
hotBackground = ::CreateSolidBrush(colors.hotBackground);
pureBackground = ::CreateSolidBrush(colors.pureBackground);
errorBackground = ::CreateSolidBrush(colors.errorBackground);
edgeBrush = ::CreateSolidBrush(colors.edge);
hotEdgeBrush = ::CreateSolidBrush(colors.hotEdge);
disabledEdgeBrush = ::CreateSolidBrush(colors.disabledEdge);
}
};
struct Pens
{
HPEN darkerTextPen = nullptr;
HPEN edgePen = nullptr;
HPEN hotEdgePen = nullptr;
HPEN disabledEdgePen = nullptr;
Pens(const Colors& colors)
: darkerTextPen(::CreatePen(PS_SOLID, 1, colors.darkerText))
, edgePen(::CreatePen(PS_SOLID, 1, colors.edge))
, hotEdgePen(::CreatePen(PS_SOLID, 1, colors.hotEdge))
, disabledEdgePen(::CreatePen(PS_SOLID, 1, colors.disabledEdge))
{}
~Pens()
{
::DeleteObject(darkerTextPen); darkerTextPen = nullptr;
::DeleteObject(edgePen); edgePen = nullptr;
::DeleteObject(hotEdgePen); hotEdgePen = nullptr;
::DeleteObject(disabledEdgePen); disabledEdgePen = nullptr;
}
void change(const Colors& colors)
{
::DeleteObject(darkerTextPen);
::DeleteObject(edgePen);
::DeleteObject(hotEdgePen);
::DeleteObject(disabledEdgePen);
darkerTextPen = ::CreatePen(PS_SOLID, 1, colors.darkerText);
edgePen = ::CreatePen(PS_SOLID, 1, colors.edge);
hotEdgePen = ::CreatePen(PS_SOLID, 1, colors.hotEdge);
disabledEdgePen = ::CreatePen(PS_SOLID, 1, colors.disabledEdge);
}
};
// black (default)
static const Colors darkColors{
HEXRGB(0x202020), // background
HEXRGB(0x404040), // softerBackground
HEXRGB(0x404040), // hotBackground
HEXRGB(0x202020), // pureBackground
HEXRGB(0xB00000), // errorBackground
HEXRGB(0xE0E0E0), // textColor
HEXRGB(0xC0C0C0), // darkerTextColor
HEXRGB(0x808080), // disabledTextColor
HEXRGB(0xFFFF00), // linkTextColor
HEXRGB(0x646464), // edgeColor
HEXRGB(0x9B9B9B), // hotEdgeColor
HEXRGB(0x484848) // disabledEdgeColor
};
// red tone
static const Colors darkRedColors{
HEXRGB(0x302020), // background
HEXRGB(0x504040), // softerBackground
HEXRGB(0x504040), // hotBackground
HEXRGB(0x302020), // pureBackground
HEXRGB(0xC00000), // errorBackground
HEXRGB(0xE0E0E0), // textColor
HEXRGB(0xC0C0C0), // darkerTextColor
HEXRGB(0x808080), // disabledTextColor
HEXRGB(0xFFFF00), // linkTextColor
HEXRGB(0x908080), // edgeColor
HEXRGB(0xBBABAB), // hotEdgeColor
HEXRGB(0x584848) // disabledEdgeColor
};
// green tone
static const Colors darkGreenColors{
HEXRGB(0x203020), // background
HEXRGB(0x405040), // softerBackground
HEXRGB(0x405040), // hotBackground
HEXRGB(0x203020), // pureBackground
HEXRGB(0xB01000), // errorBackground
HEXRGB(0xE0E0E0), // textColor
HEXRGB(0xC0C0C0), // darkerTextColor
HEXRGB(0x808080), // disabledTextColor
HEXRGB(0xFFFF00), // linkTextColor
HEXRGB(0x809080), // edgeColor
HEXRGB(0xABBBAB), // hotEdgeColor
HEXRGB(0x485848) // disabledEdgeColor
};
// blue tone
static const Colors darkBlueColors{
HEXRGB(0x202040), // background
HEXRGB(0x404060), // softerBackground
HEXRGB(0x404060), // hotBackground
HEXRGB(0x202040), // pureBackground
HEXRGB(0xB00020), // errorBackground
HEXRGB(0xE0E0E0), // textColor
HEXRGB(0xC0C0C0), // darkerTextColor
HEXRGB(0x808080), // disabledTextColor
HEXRGB(0xFFFF00), // linkTextColor
HEXRGB(0x8080A0), // edgeColor
HEXRGB(0xABABCB), // hotEdgeColor
HEXRGB(0x484868) // disabledEdgeColor
};
// purple tone
static const Colors darkPurpleColors{
HEXRGB(0x302040), // background
HEXRGB(0x504060), // softerBackground
HEXRGB(0x504060), // hotBackground
HEXRGB(0x302040), // pureBackground
HEXRGB(0xC00020), // errorBackground
HEXRGB(0xE0E0E0), // textColor
HEXRGB(0xC0C0C0), // darkerTextColor
HEXRGB(0x808080), // disabledTextColor
HEXRGB(0xFFFF00), // linkTextColor
HEXRGB(0x9080A0), // edgeColor
HEXRGB(0xBBABCB), // hotEdgeColor
HEXRGB(0x584868) // disabledEdgeColor
};
// cyan tone
static const Colors darkCyanColors{
HEXRGB(0x203040), // background
HEXRGB(0x405060), // softerBackground
HEXRGB(0x405060), // hotBackground
HEXRGB(0x203040), // pureBackground
HEXRGB(0xB01020), // errorBackground
HEXRGB(0xE0E0E0), // textColor
HEXRGB(0xC0C0C0), // darkerTextColor
HEXRGB(0x808080), // disabledTextColor
HEXRGB(0xFFFF00), // linkTextColor
HEXRGB(0x8090A0), // edgeColor
HEXRGB(0xBBBBCB), // hotEdgeColor
HEXRGB(0x485868) // disabledEdgeColor
};
// olive tone
static const Colors darkOliveColors{
HEXRGB(0x303020), // background
HEXRGB(0x505040), // softerBackground
HEXRGB(0x505040), // hotBackground
HEXRGB(0x303020), // pureBackground
HEXRGB(0xC01000), // errorBackground
HEXRGB(0xE0E0E0), // textColor
HEXRGB(0xC0C0C0), // darkerTextColor
HEXRGB(0x808080), // disabledTextColor
HEXRGB(0xFFFF00), // linkTextColor
HEXRGB(0x909080), // edgeColor
HEXRGB(0xBBBBAB), // hotEdgeColor
HEXRGB(0x585848) // disabledEdgeColor
};
// customized
Colors darkCustomizedColors{
HEXRGB(0x202020), // background
HEXRGB(0x404040), // softerBackground
HEXRGB(0x404040), // hotBackground
HEXRGB(0x202020), // pureBackground
HEXRGB(0xB00000), // errorBackground
HEXRGB(0xE0E0E0), // textColor
HEXRGB(0xC0C0C0), // darkerTextColor
HEXRGB(0x808080), // disabledTextColor
HEXRGB(0xFFFF00), // linkTextColor
HEXRGB(0x646464), // edgeColor
HEXRGB(0x9B9B9B), // hotEdgeColor
HEXRGB(0x484848) // disabledEdgeColor
};
ColorTone g_colorToneChoice = blackTone;
void setDarkTone(ColorTone colorToneChoice)
{
g_colorToneChoice = colorToneChoice;
}
struct Theme
{
Colors _colors;
Brushes _brushes;
Pens _pens;
Theme(const Colors& colors)
: _colors(colors)
, _brushes(colors)
, _pens(colors)
{}
void change(const Colors& colors)
{
_colors = colors;
_brushes.change(colors);
_pens.change(colors);
}
};
Theme tDefault(darkColors);
Theme tR(darkRedColors);
Theme tG(darkGreenColors);
Theme tB(darkBlueColors);
Theme tP(darkPurpleColors);
Theme tC(darkCyanColors);
Theme tO(darkOliveColors);
Theme tCustom(darkCustomizedColors);
Theme& getTheme()
{
switch (g_colorToneChoice)
{
case redTone:
return tR;
case greenTone:
return tG;
case blueTone:
return tB;
case purpleTone:
return tP;
case cyanTone:
return tC;
case oliveTone:
return tO;
case customizedTone:
return tCustom;
default:
return tDefault;
}
}
static Options _options; // actual runtime options
static AdvancedOptions g_advOptions;
Options configuredOptions()
{
NppGUI nppGui = NppParameters::getInstance().getNppGUI();
Options opt;
opt.enable = nppGui._darkmode._isEnabled;
opt.enableMenubar = opt.enable;
opt.enablePlugin = nppGui._darkmode._isEnabledPlugin;
g_colorToneChoice = nppGui._darkmode._colorTone;
tCustom.change(nppGui._darkmode._customColors);
return opt;
}
static bool g_isAtLeastWindows10 = false;
static bool g_isWine = false;
void initDarkMode()
{
_options = configuredOptions();
initExperimentalDarkMode();
initAdvancedOptions();
g_isAtLeastWindows10 = NppDarkMode::isWindows10();
if (!g_isAtLeastWindows10)
{
g_advOptions._enableWindowsMode = false;
}
else if (NppDarkMode::isWindowsModeEnabled())
{
NppParameters& nppParam = NppParameters::getInstance();
NppGUI& nppGUI = nppParam.getNppGUI();
nppGUI._darkmode._isEnabled = NppDarkMode::isDarkModeReg() && !IsHighContrast();
_options.enable = nppGUI._darkmode._isEnabled;
_options.enableMenubar = _options.enable;
}
setDarkMode(_options.enable, true);
using PWINEGETVERSION = const CHAR* (__cdecl *)(void);
PWINEGETVERSION pWGV = nullptr;
auto hNtdllModule = GetModuleHandle(L"ntdll.dll");
if (hNtdllModule)
{
pWGV = reinterpret_cast<PWINEGETVERSION>(GetProcAddress(hNtdllModule, "wine_get_version"));
}
g_isWine = pWGV != nullptr;
}
// attempts to apply new options from NppParameters, sends NPPM_INTERNAL_REFRESHDARKMODE to hwnd's top level parent
void refreshDarkMode(HWND hwnd, bool forceRefresh)
{
bool supportedChanged = false;
auto config = configuredOptions();
if (_options.enable != config.enable)
{
supportedChanged = true;
_options.enable = config.enable;
setDarkMode(_options.enable, _options.enable);
}
if (_options.enableMenubar != config.enableMenubar)
{
supportedChanged = true;
_options.enableMenubar = config.enableMenubar;
}
// other options not supported to change at runtime currently
if (!supportedChanged && !forceRefresh)
{
// nothing to refresh, changes were not supported.
return;
}
HWND hwndRoot = GetAncestor(hwnd, GA_ROOTOWNER);
// wParam == true, will reset style and toolbar icon
::SendMessage(hwndRoot, NPPM_INTERNAL_REFRESHDARKMODE, static_cast<WPARAM>(!forceRefresh), 0);
}
void initAdvancedOptions()
{
NppGUI& nppGui = NppParameters::getInstance().getNppGUI();
g_advOptions = nppGui._darkmode._advOptions;
}
bool isEnabled()
{
return _options.enable;
}
bool isEnabledForPlugins()
{
return _options.enablePlugin;
}
bool isDarkMenuEnabled()
{
return _options.enableMenubar;
}
bool isExperimentalActive()
{
return g_darkModeEnabled;
}
bool isExperimentalSupported()
{
return g_darkModeSupported;
}
bool isWindowsModeEnabled()
{
return g_advOptions._enableWindowsMode;
}
void setWindowsMode(bool enable)
{
g_advOptions._enableWindowsMode = enable;
}
void setThemeName(const generic_string& newThemeName)
{
if (NppDarkMode::isEnabled())
g_advOptions._darkDefaults._xmlFileName = newThemeName;
else
g_advOptions._lightDefaults._xmlFileName = newThemeName;
}
generic_string getThemeName()
{
auto& theme = NppDarkMode::isEnabled() ? g_advOptions._darkDefaults._xmlFileName : g_advOptions._lightDefaults._xmlFileName;
return (lstrcmp(theme.c_str(), L"stylers.xml") == 0) ? L"" : theme;
}
static bool g_isCustomToolIconUsed = NppParameters::getInstance().getCustomizedToolIcons() != nullptr;
void setToolBarIconSet(int state2Set, bool useDark)
{
if (useDark)
g_advOptions._darkDefaults._toolBarIconSet = state2Set;
else
g_advOptions._lightDefaults._toolBarIconSet = state2Set;
}
int getToolBarIconSet(bool useDark)
{
if (g_isCustomToolIconUsed)
{
return -1;
}
return useDark ? g_advOptions._darkDefaults._toolBarIconSet : g_advOptions._lightDefaults._toolBarIconSet;
}
void setTabIconSet(bool useAltIcons, bool useDark)
{
if (useDark)
g_advOptions._darkDefaults._tabIconSet = useAltIcons ? 1 : 2;
else
g_advOptions._lightDefaults._tabIconSet = useAltIcons ? 1 : 0;
}
int getTabIconSet(bool useDark)
{
return useDark ? g_advOptions._darkDefaults._tabIconSet : g_advOptions._lightDefaults._tabIconSet;
}
bool useTabTheme()
{
return NppDarkMode::isEnabled() ? g_advOptions._darkDefaults._tabUseTheme : g_advOptions._lightDefaults._tabUseTheme;
}
void setAdvancedOptions()
{
NppGUI& nppGui = NppParameters::getInstance().getNppGUI();
auto& advOpt = nppGui._darkmode._advOptions;
advOpt = g_advOptions;
}
bool isWindows10()
{
return IsWindows10();
}
bool isWindows11()
{
return IsWindows11();
}
DWORD getWindowsBuildNumber()
{
return GetWindowsBuildNumber();
}
COLORREF invertLightness(COLORREF c)
{
WORD h = 0;
WORD s = 0;
WORD l = 0;
ColorRGBToHLS(c, &h, &l, &s);
l = 240 - l;
COLORREF invert_c = ColorHLSToRGB(h, l, s);
return invert_c;
}
COLORREF invertLightnessSofter(COLORREF c)
{
WORD h = 0;
WORD s = 0;
WORD l = 0;
ColorRGBToHLS(c, &h, &l, &s);
l = std::min<WORD>(240U - l, 211U);
COLORREF invert_c = ColorHLSToRGB(h, l, s);
return invert_c;
}
static TreeViewStyle g_treeViewStyle = TreeViewStyle::classic;
static COLORREF g_treeViewBg = NppParameters::getInstance().getCurrentDefaultBgColor();
static double g_lighnessTreeView = 50.0;
// adapted from https://stackoverflow.com/a/56678483
double calculatePerceivedLighness(COLORREF c)
{
auto linearValue = [](double colorChannel) -> double
{
colorChannel /= 255.0;
if (colorChannel <= 0.04045)
return colorChannel / 12.92;
return std::pow(((colorChannel + 0.055) / 1.055), 2.4);
};
double r = linearValue(static_cast<double>(GetRValue(c)));
double g = linearValue(static_cast<double>(GetGValue(c)));
double b = linearValue(static_cast<double>(GetBValue(c)));
double luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
double lighness = (luminance <= 216.0 / 24389.0) ? (luminance * 24389.0 / 27.0) : (std::pow(luminance, (1.0 / 3.0)) * 116.0 - 16.0);
return lighness;
}
COLORREF getBackgroundColor() { return getTheme()._colors.background; }
COLORREF getSofterBackgroundColor() { return getTheme()._colors.softerBackground; }
COLORREF getHotBackgroundColor() { return getTheme()._colors.hotBackground; }
COLORREF getDarkerBackgroundColor() { return getTheme()._colors.pureBackground; }
COLORREF getErrorBackgroundColor() { return getTheme()._colors.errorBackground; }
COLORREF getTextColor() { return getTheme()._colors.text; }
COLORREF getDarkerTextColor() { return getTheme()._colors.darkerText; }
COLORREF getDisabledTextColor() { return getTheme()._colors.disabledText; }
COLORREF getLinkTextColor() { return getTheme()._colors.linkText; }
COLORREF getEdgeColor() { return getTheme()._colors.edge; }
COLORREF getHotEdgeColor() { return getTheme()._colors.hotEdge; }
COLORREF getDisabledEdgeColor() { return getTheme()._colors.disabledEdge; }
HBRUSH getBackgroundBrush() { return getTheme()._brushes.background; }
HBRUSH getSofterBackgroundBrush() { return getTheme()._brushes.softerBackground; }
HBRUSH getHotBackgroundBrush() { return getTheme()._brushes.hotBackground; }
HBRUSH getDarkerBackgroundBrush() { return getTheme()._brushes.pureBackground; }
HBRUSH getErrorBackgroundBrush() { return getTheme()._brushes.errorBackground; }
HBRUSH getEdgeBrush() { return getTheme()._brushes.edgeBrush; }
HBRUSH getHotEdgeBrush() { return getTheme()._brushes.hotEdgeBrush; }
HBRUSH getDisabledEdgeBrush() { return getTheme()._brushes.disabledEdgeBrush; }
HPEN getDarkerTextPen() { return getTheme()._pens.darkerTextPen; }
HPEN getEdgePen() { return getTheme()._pens.edgePen; }
HPEN getHotEdgePen() { return getTheme()._pens.hotEdgePen; }
HPEN getDisabledEdgePen() { return getTheme()._pens.disabledEdgePen; }
void setBackgroundColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.background = c;
getTheme().change(clrs);
}
void setSofterBackgroundColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.softerBackground = c;
getTheme().change(clrs);
}
void setHotBackgroundColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.hotBackground = c;
getTheme().change(clrs);
}
void setDarkerBackgroundColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.pureBackground = c;
getTheme().change(clrs);
}
void setErrorBackgroundColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.errorBackground = c;
getTheme().change(clrs);
}
void setTextColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.text = c;
getTheme().change(clrs);
}
void setDarkerTextColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.darkerText = c;
getTheme().change(clrs);
}
void setDisabledTextColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.disabledText = c;
getTheme().change(clrs);
}
void setLinkTextColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.linkText = c;
getTheme().change(clrs);
}
void setEdgeColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.edge = c;
getTheme().change(clrs);
}
void setHotEdgeColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.hotEdge = c;
getTheme().change(clrs);
}
void setDisabledEdgeColor(COLORREF c)
{
Colors clrs = getTheme()._colors;
clrs.disabledEdge = c;
getTheme().change(clrs);
}
Colors getDarkModeDefaultColors()
{
return darkColors;
}
void changeCustomTheme(const Colors& colors)
{
tCustom.change(colors);
}
// handle events
void handleSettingChange(HWND hwnd, LPARAM lParam, bool isFromBtn)
{
UNREFERENCED_PARAMETER(hwnd);
if (!isExperimentalSupported())
{
return;
}
if (IsColorSchemeChangeMessage(lParam) || isFromBtn)
{
// ShouldAppsUseDarkMode() is not reliable from 1903+, use NppDarkMode::isDarkModeReg() instead
g_darkModeEnabled = NppDarkMode::isDarkModeReg() && !IsHighContrast();
}
}
bool isDarkModeReg()
{
DWORD data{};
DWORD dwBufSize = sizeof(data);
LPCTSTR lpSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
LPCTSTR lpValue = L"AppsUseLightTheme";
auto result = RegGetValue(HKEY_CURRENT_USER, lpSubKey, lpValue, RRF_RT_REG_DWORD, nullptr, &data, &dwBufSize);
if (result != ERROR_SUCCESS)
{
return false;
}
// dark mode is 0, light mode is 1
return data == 0UL;
}
// processes messages related to UAH / custom menubar drawing.
// return true if handled, false to continue with normal processing in your wndproc
bool runUAHWndProc(HWND hWnd, UINT message, WPARAM /*wParam*/, LPARAM lParam, LRESULT* lr)
{
static HTHEME g_menuTheme = nullptr;
switch (message)
{
case WM_UAHDRAWMENU:
{
auto pUDM = reinterpret_cast<UAHMENU*>(lParam);
RECT rc{};
// get the menubar rect
{
MENUBARINFO mbi{};
mbi.cbSize = sizeof(MENUBARINFO);
GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi);
RECT rcWindow{};
GetWindowRect(hWnd, &rcWindow);
// the rcBar is offset by the window rect
rc = mbi.rcBar;
OffsetRect(&rc, -rcWindow.left, -rcWindow.top);
rc.top -= 1;
}
FillRect(pUDM->hdc, &rc, NppDarkMode::getDarkerBackgroundBrush());
*lr = 0;
return true;
}
case WM_UAHDRAWMENUITEM:
{
auto pUDMI = reinterpret_cast<UAHDRAWMENUITEM*>(lParam);
// get the menu item string
wchar_t menuString[256] = { '\0' };
MENUITEMINFO mii{};
{
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_STRING;
mii.dwTypeData = menuString;
mii.cch = (sizeof(menuString) / 2) - 1;
GetMenuItemInfo(pUDMI->um.hmenu, pUDMI->umi.iPosition, TRUE, &mii);
}
// get the item state for drawing
DWORD dwFlags = DT_CENTER | DT_SINGLELINE | DT_VCENTER;
int iTextStateID = MBI_NORMAL;
int iBackgroundStateID = MBI_NORMAL;
{
if ((pUDMI->dis.itemState & ODS_INACTIVE) | (pUDMI->dis.itemState & ODS_DEFAULT))
{
// normal display
iTextStateID = MBI_NORMAL;
iBackgroundStateID = MBI_NORMAL;
}
if (pUDMI->dis.itemState & ODS_HOTLIGHT)
{
// hot tracking
iTextStateID = MBI_HOT;
iBackgroundStateID = MBI_HOT;
}
if (pUDMI->dis.itemState & ODS_SELECTED)
{
// clicked
iTextStateID = MBI_PUSHED;
iBackgroundStateID = MBI_PUSHED;
}
if ((pUDMI->dis.itemState & ODS_GRAYED) || (pUDMI->dis.itemState & ODS_DISABLED))
{
// disabled / grey text
iTextStateID = MBI_DISABLED;
iBackgroundStateID = MBI_DISABLED;
}
if (pUDMI->dis.itemState & ODS_NOACCEL)
{
dwFlags |= DT_HIDEPREFIX;
}
}
if (!g_menuTheme)
{
g_menuTheme = OpenThemeData(hWnd, L"Menu");
}
switch (iBackgroundStateID)
{
case MBI_NORMAL:
case MBI_DISABLED:
{
::FillRect(pUDMI->um.hdc, &pUDMI->dis.rcItem, NppDarkMode::getDarkerBackgroundBrush());
break;
}
case MBI_HOT:
case MBI_DISABLEDHOT:
{
::FillRect(pUDMI->um.hdc, &pUDMI->dis.rcItem, NppDarkMode::getHotBackgroundBrush());
break;
}
case MBI_PUSHED:
case MBI_DISABLEDPUSHED:
{
::FillRect(pUDMI->um.hdc, &pUDMI->dis.rcItem, NppDarkMode::getSofterBackgroundBrush());
break;
}
default:
{
::DrawThemeBackground(g_menuTheme, pUDMI->um.hdc, MENU_BARITEM, iBackgroundStateID, &pUDMI->dis.rcItem, nullptr);
break;
}
}
DTTOPTS dttopts{};
dttopts.dwSize = sizeof(DTTOPTS);
if (iTextStateID == MBI_NORMAL || iTextStateID == MBI_HOT || iTextStateID == MBI_PUSHED)
{
dttopts.dwFlags |= DTT_TEXTCOLOR;
dttopts.crText = NppDarkMode::getTextColor();
}
else if (iTextStateID == MBI_DISABLED || iTextStateID == MBI_DISABLEDHOT || iTextStateID == MBI_DISABLEDPUSHED)
{
dttopts.dwFlags |= DTT_TEXTCOLOR;
dttopts.crText = NppDarkMode::getDisabledTextColor();
}
::DrawThemeTextEx(g_menuTheme, pUDMI->um.hdc, MENU_BARITEM, iTextStateID, menuString, mii.cch, dwFlags, &pUDMI->dis.rcItem, &dttopts);
*lr = 0;
return true;
}
case WM_DPICHANGED:
case WM_THEMECHANGED:
{
if (g_menuTheme)
{
CloseThemeData(g_menuTheme);
g_menuTheme = nullptr;
}
// continue processing in main wndproc
return false;
}
default:
return false;
}
}
void drawUAHMenuNCBottomLine(HWND hWnd)
{
MENUBARINFO mbi{};
mbi.cbSize = sizeof(MENUBARINFO);
if (!GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi))
{
return;
}
RECT rcClient{};
GetClientRect(hWnd, &rcClient);
MapWindowPoints(hWnd, nullptr, (POINT*)&rcClient, 2);
RECT rcWindow{};
GetWindowRect(hWnd, &rcWindow);
OffsetRect(&rcClient, -rcWindow.left, -rcWindow.top);
// the rcBar is offset by the window rect
RECT rcAnnoyingLine = rcClient;
rcAnnoyingLine.bottom = rcAnnoyingLine.top;
rcAnnoyingLine.top--;
HDC hdc = GetWindowDC(hWnd);
FillRect(hdc, &rcAnnoyingLine, NppDarkMode::getDarkerBackgroundBrush());
ReleaseDC(hWnd, hdc);
}
// from DarkMode.h
void initExperimentalDarkMode()
{
::InitDarkMode();
}
void setDarkMode(bool useDark, bool fixDarkScrollbar)
{
::SetDarkMode(useDark, fixDarkScrollbar);
}
void allowDarkModeForApp(bool allow)
{
::AllowDarkModeForApp(allow);
}
bool allowDarkModeForWindow(HWND hWnd, bool allow)
{
return ::AllowDarkModeForWindow(hWnd, allow);
}
void setTitleBarThemeColor(HWND hWnd)
{
::RefreshTitleBarThemeColor(hWnd);
}
void enableDarkScrollBarForWindowAndChildren(HWND hwnd)
{
::EnableDarkScrollBarForWindowAndChildren(hwnd);
}
void paintRoundFrameRect(HDC hdc, const RECT rect, const HPEN hpen, int width, int height)
{
auto holdBrush = ::SelectObject(hdc, ::GetStockObject(NULL_BRUSH));
auto holdPen = ::SelectObject(hdc, hpen);
::RoundRect(hdc, rect.left, rect.top, rect.right, rect.bottom, width, height);