From bc7eb9611030aed3204aff4e662c318cbf9143a6 Mon Sep 17 00:00:00 2001 From: Michael Niksa Date: Mon, 3 Feb 2020 23:13:31 +0000 Subject: [PATCH] Merged PR 4271163: [Git2Git] Remove use of private theme APIs References MSFT:24418178 --- src/inc/conint.h | 6 + src/interactivity/win32/lib/win32.LIB.vcxproj | 2 - .../win32/lib/win32.LIB.vcxproj.filters | 6 - src/interactivity/win32/sources.inc | 1 - src/interactivity/win32/window.cpp | 10 +- src/interactivity/win32/windowproc.cpp | 11 +- src/interactivity/win32/windowtheme.cpp | 130 ------------------ src/interactivity/win32/windowtheme.hpp | 33 ----- src/internal/stubs.cpp | 5 + 9 files changed, 17 insertions(+), 187 deletions(-) delete mode 100644 src/interactivity/win32/windowtheme.cpp delete mode 100644 src/interactivity/win32/windowtheme.hpp diff --git a/src/inc/conint.h b/src/inc/conint.h index fcb77fd3d02..aa1e410d334 100644 --- a/src/inc/conint.h +++ b/src/inc/conint.h @@ -32,6 +32,8 @@ namespace Microsoft::Console::Internal [[nodiscard]] HRESULT CheckIntegrityLevelPolicy(const HANDLE hOtherToken, bool& fIsWrongWayBlocked) noexcept; + + } namespace EdpPolicy @@ -39,4 +41,8 @@ namespace Microsoft::Console::Internal void AuditClipboard(const std::wstring_view destinationName) noexcept; } + namespace Theming + { + [[nodiscard]] HRESULT TrySetDarkMode(HWND hwnd) noexcept; + } } diff --git a/src/interactivity/win32/lib/win32.LIB.vcxproj b/src/interactivity/win32/lib/win32.LIB.vcxproj index 61ecaefc94b..e79e8c0b9c7 100644 --- a/src/interactivity/win32/lib/win32.LIB.vcxproj +++ b/src/interactivity/win32/lib/win32.LIB.vcxproj @@ -36,7 +36,6 @@ - @@ -59,7 +58,6 @@ - diff --git a/src/interactivity/win32/lib/win32.LIB.vcxproj.filters b/src/interactivity/win32/lib/win32.LIB.vcxproj.filters index 4a4ad011590..4204622caea 100644 --- a/src/interactivity/win32/lib/win32.LIB.vcxproj.filters +++ b/src/interactivity/win32/lib/win32.LIB.vcxproj.filters @@ -66,9 +66,6 @@ Source Files - - Source Files - Source Files @@ -131,9 +128,6 @@ Header Files - - Header Files - Header Files diff --git a/src/interactivity/win32/sources.inc b/src/interactivity/win32/sources.inc index 5e8cdc6cd3b..a4b05c5a99c 100644 --- a/src/interactivity/win32/sources.inc +++ b/src/interactivity/win32/sources.inc @@ -56,7 +56,6 @@ SOURCES = \ ..\windowio.cpp \ ..\WindowMetrics.cpp \ ..\windowproc.cpp \ - ..\windowtheme.cpp \ ..\windowUiaProvider.cpp \ INCLUDES = \ diff --git a/src/interactivity/win32/window.cpp b/src/interactivity/win32/window.cpp index 3da33af586d..7be68695d4e 100644 --- a/src/interactivity/win32/window.cpp +++ b/src/interactivity/win32/window.cpp @@ -10,7 +10,8 @@ #include "windowio.hpp" #include "windowdpiapi.hpp" #include "windowmetrics.hpp" -#include "windowtheme.hpp" + +#include "..\..\inc\conint.h" #include "..\..\host\globals.h" #include "..\..\host\dbcs.h" @@ -349,12 +350,7 @@ void Window::_UpdateSystemMetrics() const siAttached.PostUpdateWindowSize(); // Locate window theming modules and try to set the dark mode. - try - { - WindowTheme theme; - LOG_IF_FAILED(theme.TrySetDarkMode(_hWnd)); - } - CATCH_LOG(); + LOG_IF_FAILED(Microsoft::Console::Internal::Theming::TrySetDarkMode(_hWnd)); } } } diff --git a/src/interactivity/win32/windowproc.cpp b/src/interactivity/win32/windowproc.cpp index 0398492a44d..767e015e4dc 100644 --- a/src/interactivity/win32/windowproc.cpp +++ b/src/interactivity/win32/windowproc.cpp @@ -25,7 +25,8 @@ #include "..\inc\ServiceLocator.hpp" -#include "..\interactivity\win32\windowtheme.hpp" +#include "..\..\inc\conint.h" + #include "..\interactivity\win32\CustomWindowMessages.h" #include "..\interactivity\win32\windowUiaProvider.hpp" @@ -336,13 +337,7 @@ using namespace Microsoft::Console::Types; case WM_SETTINGCHANGE: { - try - { - WindowTheme theme; - LOG_IF_FAILED(theme.TrySetDarkMode(hWnd)); - } - CATCH_LOG(); - + LOG_IF_FAILED(Microsoft::Console::Internal::Theming::TrySetDarkMode(hWnd)); gci.GetCursorBlinker().SettingsChanged(); } __fallthrough; diff --git a/src/interactivity/win32/windowtheme.cpp b/src/interactivity/win32/windowtheme.cpp deleted file mode 100644 index bad40bc01d6..00000000000 --- a/src/interactivity/win32/windowtheme.cpp +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -#include "precomp.h" - -#include "windowtheme.hpp" - -#include - -#ifdef __INSIDE_WINDOWS -#include -#endif - -using namespace Microsoft::Console::Interactivity::Win32; - -#define DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 19 - -#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE -#define DWMWA_USE_IMMERSIVE_DARK_MODE 20 -#endif - -#define DARK_MODE_STRING_NAME L"DarkMode_Explorer" -#define UXTHEME_DLL_NAME L"uxtheme.dll" -#define UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL 132 - -// Routine Description: -// - Constructs window theme class (holding module references for function lookups) -WindowTheme::WindowTheme() -{ - // NOTE: Use LoadLibraryExW with LOAD_LIBRARY_SEARCH_SYSTEM32 flag below to avoid unneeded directory traversal. - // This has triggered CPG boot IO warnings in the past. - _module.reset(LoadLibraryExW(UXTHEME_DLL_NAME, nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)); -} - -// Routine Description: -// - Attempts to set the dark mode on the given HWND. -// - Will check the system for user preferences and high contrast to see if it's a good idea -// before setting it. -// Arguments: -// - hwnd - Window to apply dark mode to -// Return Value: -// - S_OK or suitable HRESULT from theming or DWM engines. -[[nodiscard]] HRESULT WindowTheme::TrySetDarkMode(HWND hwnd) const noexcept -{ - // I have to be a big B BOOL or DwnSetWindowAttribute will be upset (E_INVALIDARG) when I am passed in. - const BOOL isDarkMode = !!_IsDarkMode(); - - if (isDarkMode) - { - RETURN_IF_FAILED(SetWindowTheme(hwnd, DARK_MODE_STRING_NAME, nullptr)); - } - else - { - RETURN_IF_FAILED(SetWindowTheme(hwnd, L"", nullptr)); - } - - if (FAILED(DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &isDarkMode, sizeof(isDarkMode)))) - { - RETURN_IF_FAILED(DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, &isDarkMode, sizeof(isDarkMode))); - } - - return S_OK; -} - -// Routine Description: -// - Logical determination of if we should use the dark mode or not. -// - Combines user preferences and high contrast accessibility settings. -// Arguments: -// - -// Return Value: -// - TRUE if dark mode is allowed. FALSE if it is not. -bool WindowTheme::_IsDarkMode() const noexcept -{ - if (_ShouldAppsUseDarkMode() && !_IsHighContrast()) - { - return true; - } - else - { - return false; - } -} - -// Routine Description: -// - Looks up the high contrast state of the system. -// Arguments: -// - -// Return Value: -// - True if the system is in high contrast (shouldn't change theme further.) False otherwise. -bool WindowTheme::_IsHighContrast() const noexcept -{ - BOOL fHighContrast = FALSE; - HIGHCONTRAST hc = { sizeof(hc) }; - if (SystemParametersInfo(SPI_GETHIGHCONTRAST, sizeof(hc), &hc, 0)) - { - fHighContrast = (HCF_HIGHCONTRASTON & hc.dwFlags); - } - return fHighContrast; -} - -// Routine Description: -// - Looks up the user preference for dark mode. -// Arguments: -// - -// Return Value: -// - True if the user chose dark mode in settings. False otherwise. -bool WindowTheme::_ShouldAppsUseDarkMode() const noexcept -{ - if (_module.get() != nullptr) - { - typedef bool(WINAPI * PfnShouldAppsUseDarkMode)(); - - static bool tried = false; - static PfnShouldAppsUseDarkMode pfn = nullptr; - - if (!tried) - { - pfn = (PfnShouldAppsUseDarkMode)GetProcAddress(_module.get(), MAKEINTRESOURCEA(UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL)); - } - - tried = true; - - if (pfn != nullptr) - { - return pfn(); - } - } - - return false; -} diff --git a/src/interactivity/win32/windowtheme.hpp b/src/interactivity/win32/windowtheme.hpp deleted file mode 100644 index 701b55cc207..00000000000 --- a/src/interactivity/win32/windowtheme.hpp +++ /dev/null @@ -1,33 +0,0 @@ -/*++ -Copyright (c) Microsoft Corporation -Licensed under the MIT license. - -Module Name: -- windowtheme.hpp - -Abstract: -- This module is used for abstracting calls to set window themes. - -Author(s): -- Michael Niksa (MiNiksa) Oct-2018 ---*/ -#pragma once - -namespace Microsoft::Console::Interactivity::Win32 -{ - class WindowTheme final - { - public: - WindowTheme(); - - [[nodiscard]] HRESULT TrySetDarkMode(HWND hwnd) const noexcept; - - private: - bool _IsDarkMode() const noexcept; - - bool _IsHighContrast() const noexcept; - bool _ShouldAppsUseDarkMode() const noexcept; - - wil::unique_hmodule _module; - }; -} diff --git a/src/internal/stubs.cpp b/src/internal/stubs.cpp index cf456989248..d6b61ea120f 100644 --- a/src/internal/stubs.cpp +++ b/src/internal/stubs.cpp @@ -24,3 +24,8 @@ using namespace Microsoft::Console::Internal; void EdpPolicy::AuditClipboard(const std::wstring_view /*destinationName*/) noexcept { } + +[[nodiscard]] HRESULT Theming::TrySetDarkMode(HWND /*hwnd*/) noexcept +{ + return S_FALSE; +}