Fix inconsistent function definition
[TortoiseGit.git] / src / Utils / Windows10Colors.cpp
blob4a137a209e59bfe018a3971c7301ce58e682015b
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.
20 #include "stdafx.h"
21 #include "SysInfo.h"
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
31 class HStringRef
33 HSTRING hstr = nullptr;
34 HSTRING_HEADER str_header{};
36 public:
37 HStringRef() = default;
38 // String ref doesn't need dtor
40 template <size_t N>
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);
55 if (FAILED(hr))
56 return hr;
57 return inspectable.As(&instance);
60 Win10Colors Win10Colors::instance;
62 Win10Colors::Win10Colors()
64 if (!modules_loaded)
66 modules_loaded = true;
67 if (!SysInfo::Instance().IsWin8OrLater())
68 return;
69 winrt = LoadLibraryW(L"api-ms-win-core-winrt-l1-1-0.dll");
70 if (winrt)
71 pRoActivateInstance = reinterpret_cast<pfnRoActivateInstance>(GetProcAddress(winrt, "RoActivateInstance"));
72 winrt_string = LoadLibraryW(L"api-ms-win-core-winrt-string-l1-1-0.dll");
73 if (winrt_string)
74 pWindowsCreateStringReference = reinterpret_cast<pfnWindowsCreateStringReference>(GetProcAddress(winrt_string, "WindowsCreateStringReference"));
78 Win10Colors::~Win10Colors()
80 if (winrt)
81 FreeLibrary(winrt);
82 if (winrt_string)
83 FreeLibrary(winrt_string);
86 HRESULT Win10Colors::GetAccentColor(AccentColor& color)
88 HStringRef classId;
89 auto hr = classId.Set(L"Windows.UI.ViewManagement.UISettings");
90 if (FAILED(hr))
91 return hr;
92 Microsoft::WRL::ComPtr<WindowsUI::ViewManagement::IUISettings> settings;
93 hr = ActivateInstance(classId, settings);
94 if (FAILED(hr))
95 return hr;
97 ComPtr<WindowsUI::ViewManagement::IUISettings3> settings3;
98 hr = settings.As(&settings3);
99 if (!settings3)
100 return E_FAIL;
102 WindowsUI::Color ui_color;
103 hr = settings3->GetColorValue(WindowsUI::ViewManagement::UIColorType_Foreground, &ui_color);
104 if (FAILED(hr))
105 return hr;
106 color.foreground = ToRGBA(ui_color);
107 hr = settings3->GetColorValue(WindowsUI::ViewManagement::UIColorType_Background, &ui_color);
108 if (FAILED(hr))
109 return hr;
110 color.background = ToRGBA(ui_color);
111 hr = settings3->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentDark3, &ui_color);
112 if (FAILED(hr))
113 return hr;
114 color.darkest = ToRGBA(ui_color);
115 hr = settings3->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentDark2, &ui_color);
116 if (FAILED(hr))
117 return hr;
118 color.darker = ToRGBA(ui_color);
119 hr = settings3->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentDark1, &ui_color);
120 if (FAILED(hr))
121 return hr;
122 color.dark = ToRGBA(ui_color);
123 hr = settings3->GetColorValue(WindowsUI::ViewManagement::UIColorType_Accent, &ui_color);
124 if (FAILED(hr))
125 return hr;
126 color.accent = ToRGBA(ui_color);
127 hr = settings3->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentLight1, &ui_color);
128 if (FAILED(hr))
129 return hr;
130 color.light = ToRGBA(ui_color);
131 hr = settings3->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentLight2, &ui_color);
132 if (FAILED(hr))
133 return hr;
134 color.lighter = ToRGBA(ui_color);
135 hr = settings3->GetColorValue(WindowsUI::ViewManagement::UIColorType_AccentLight3, &ui_color);
136 if (FAILED(hr))
137 return hr;
138 color.lightest = ToRGBA(ui_color);
140 return S_OK;