From 87fc91477250e033c8eb3717abfc0025639f5149 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Fri, 30 Aug 2024 13:59:09 +0200 Subject: [PATCH] wip --- src/internal/stubs.cpp | 27 +++++++++++++++++++++++++-- src/types/ThemeUtils.cpp | 19 ------------------- src/types/inc/ThemeUtils.h | 8 -------- src/types/lib/types.vcxproj | 2 -- src/types/lib/types.vcxproj.filters | 6 ------ src/types/sources.inc | 1 - 6 files changed, 25 insertions(+), 38 deletions(-) delete mode 100644 src/types/ThemeUtils.cpp delete mode 100644 src/types/inc/ThemeUtils.h diff --git a/src/internal/stubs.cpp b/src/internal/stubs.cpp index f92f409696..224c6b1c38 100644 --- a/src/internal/stubs.cpp +++ b/src/internal/stubs.cpp @@ -3,6 +3,9 @@ #include "precomp.h" +#include +#include + #include "../inc/conint.h" using namespace Microsoft::Console::Internal; @@ -21,7 +24,27 @@ using namespace Microsoft::Console::Internal; return S_OK; } -[[nodiscard]] HRESULT Theming::TrySetDarkMode(HWND /*hwnd*/) noexcept +[[nodiscard]] HRESULT Theming::TrySetDarkMode(HWND hwnd) noexcept { - return S_FALSE; + static constexpr auto subKey = LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize)"; + static constexpr auto get = [](HKEY key, const wchar_t* value, DWORD& data) { + DWORD dataType = 0; + DWORD size = sizeof(data); + return RegGetValueW(key, subKey, value, RRF_RT_REG_DWORD, &dataType, &data, &size); + }; + + // This is the approach that WinUI3 used in its initial source code release at the time of writing. + DWORD useLightTheme = 0; + if (get(HKEY_CURRENT_USER, L"AppsUseLightTheme", useLightTheme) != ERROR_SUCCESS) + { + if (get(HKEY_LOCAL_MACHINE, L"SystemUsesLightTheme", useLightTheme) != ERROR_SUCCESS) + { + useLightTheme = 0; + } + } + + const BOOL useDarkMode = useLightTheme == 0; + LOG_IF_FAILED(SetWindowTheme(hwnd, useLightTheme ? L"" : L"DarkMode_Explorer", nullptr)); + LOG_IF_FAILED(DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &useDarkMode, sizeof(useDarkMode))); + return S_OK; } diff --git a/src/types/ThemeUtils.cpp b/src/types/ThemeUtils.cpp deleted file mode 100644 index ecc4af6a34..0000000000 --- a/src/types/ThemeUtils.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "precomp.h" -#include "inc/ThemeUtils.h" - -namespace Microsoft::Console::ThemeUtils -{ - // Routine Description: - // - Attempts to enable/disable the dark mode on the frame of a window. - // Arguments: - // - hwnd: handle to the window to change - // - enabled: whether to enable or not the dark mode on the window's frame - // Return Value: - // - S_OK or suitable HRESULT from DWM engines. - [[nodiscard]] HRESULT SetWindowFrameDarkMode(HWND /* hwnd */, bool /* enabled */) noexcept - { - // TODO:GH #3425 implement the new DWM API and change - // src/interactivity/win32/windowtheme.cpp to use it. - return S_OK; - } -} diff --git a/src/types/inc/ThemeUtils.h b/src/types/inc/ThemeUtils.h deleted file mode 100644 index ffe972301f..0000000000 --- a/src/types/inc/ThemeUtils.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include - -namespace Microsoft::Console::ThemeUtils -{ - [[nodiscard]] HRESULT SetWindowFrameDarkMode(HWND hwnd, bool enabled) noexcept; -} diff --git a/src/types/lib/types.vcxproj b/src/types/lib/types.vcxproj index 90756e2be2..62c90830ba 100644 --- a/src/types/lib/types.vcxproj +++ b/src/types/lib/types.vcxproj @@ -18,7 +18,6 @@ - @@ -38,7 +37,6 @@ - diff --git a/src/types/lib/types.vcxproj.filters b/src/types/lib/types.vcxproj.filters index 2ecdd122c9..84c26d80b1 100644 --- a/src/types/lib/types.vcxproj.filters +++ b/src/types/lib/types.vcxproj.filters @@ -42,9 +42,6 @@ Source Files - - Source Files - Source Files @@ -107,9 +104,6 @@ Header Files - - Header Files - Header Files diff --git a/src/types/sources.inc b/src/types/sources.inc index 44f252a2a1..afc6577456 100644 --- a/src/types/sources.inc +++ b/src/types/sources.inc @@ -35,7 +35,6 @@ SOURCES= \ ..\convert.cpp \ ..\colorTable.cpp \ ..\utils.cpp \ - ..\ThemeUtils.cpp \ ..\ScreenInfoUiaProviderBase.cpp \ ..\sgrStack.cpp \ ..\UiaTextRangeBase.cpp \