1
// TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2021, 2023 - TortoiseGit
4 // Copyright (C) 2020 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "Windows10Colors.h"
24 #pragma comment(lib, "dwmapi.lib")
25 #pragma comment(lib, "ntdll.lib")
27 using namespace Microsoft::WRL
;
28 namespace WindowsUI
= ABI::Windows::UI
;
30 /// Wrapper class for WinRT string reference
33 HSTRING hstr
= nullptr;
34 HSTRING_HEADER str_header
{};
37 HStringRef() = default;
38 // String ref doesn't need dtor
41 HRESULT
Set(const wchar_t (&str
)[N
])
43 return Win10Colors::WindowsCreateStringReference(str
, N
- 1, &str_header
, &hstr
);
46 operator HSTRING() const { return hstr
; }
49 /// Call RoActivateInstance and query an interface
50 template <typename IF
>
51 static HRESULT
ActivateInstance(HSTRING classId
, ComPtr
<IF
>& instance
)
53 ComPtr
<IInspectable
> inspectable
;
54 auto hr
= Win10Colors::RoActivateInstance(classId
, &inspectable
);
57 return inspectable
.As(&instance
);
60 Win10Colors
Win10Colors::instance
;
62 Win10Colors::Win10Colors()
66 modules_loaded
= true;
67 if (!SysInfo::Instance().IsWin8OrLater())
69 winrt
= LoadLibraryW(L
"api-ms-win-core-winrt-l1-1-0.dll");
71 pRoActivateInstance
= reinterpret_cast<pfnRoActivateInstance
>(GetProcAddress(winrt
, "RoActivateInstance"));
72 winrt_string
= LoadLibraryW(L
"api-ms-win-core-winrt-string-l1-1-0.dll");
74 pWindowsCreateStringReference
= reinterpret_cast<pfnWindowsCreateStringReference
>(GetProcAddress(winrt_string
, "WindowsCreateStringReference"));
78 Win10Colors::~Win10Colors()
83 FreeLibrary(winrt_string
);
86 HRESULT
Win10Colors::GetAccentColor(AccentColor
& color
)
89 auto hr
= classId
.Set(L
"Windows.UI.ViewManagement.UISettings");
92 Microsoft::WRL::ComPtr
<WindowsUI::ViewManagement::IUISettings
> settings
;
93 hr
= ActivateInstance(classId
, settings
);
97 ComPtr
<WindowsUI::ViewManagement::IUISettings3
> settings3
;
98 hr
= settings
.As(&settings3
);
102 WindowsUI::Color ui_color
;
103 hr
= settings3
->GetColorValue(WindowsUI::ViewManagement::UIColorType_Foreground
, &ui_color
);
106 color
.foreground
= ToRGBA(ui_color
);
107 hr
= settings3
->GetColorValue(WindowsUI::ViewManagement::UIColorType_Background
, &ui_color
);
110 color
.background
= ToRGBA(ui_color
);
111 hr
= settings3
->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentDark3
, &ui_color
);
114 color
.darkest
= ToRGBA(ui_color
);
115 hr
= settings3
->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentDark2
, &ui_color
);
118 color
.darker
= ToRGBA(ui_color
);
119 hr
= settings3
->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentDark1
, &ui_color
);
122 color
.dark
= ToRGBA(ui_color
);
123 hr
= settings3
->GetColorValue(WindowsUI::ViewManagement::UIColorType_Accent
, &ui_color
);
126 color
.accent
= ToRGBA(ui_color
);
127 hr
= settings3
->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentLight1
, &ui_color
);
130 color
.light
= ToRGBA(ui_color
);
131 hr
= settings3
->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentLight2
, &ui_color
);
134 color
.lighter
= ToRGBA(ui_color
);
135 hr
= settings3
->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentLight3
, &ui_color
);
138 color
.lightest
= ToRGBA(ui_color
);