| 
									
										
										
										
											2022-06-14 12:27:28 -04:00
										 |  |  | // Copyright (c) 2022 Microsoft Inc. All rights reserved.
 | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | // Use of this source code is governed by a BSD-style license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE-CHROMIUM file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "shell/browser/win/dark_mode.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <dwmapi.h>  // DwmSetWindowAttribute()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "base/win/windows_version.h"
 | 
					
						
							| 
									
										
										
										
											2024-07-22 04:31:32 -05:00
										 |  |  | #include "ui/native_theme/native_theme.h"
 | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 12:27:28 -04:00
										 |  |  | // This flag works since Win10 20H1 but is not documented until Windows 11
 | 
					
						
							|  |  |  | #define DWMWA_USE_IMMERSIVE_DARK_MODE 20
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | // This namespace contains code originally from
 | 
					
						
							| 
									
										
										
										
											2022-06-14 12:27:28 -04:00
										 |  |  | // https://github.com/microsoft/terminal
 | 
					
						
							|  |  |  | // governed by the MIT license and (c) Microsoft Corporation.
 | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 12:27:28 -04:00
										 |  |  | // https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
 | 
					
						
							|  |  |  | HRESULT TrySetWindowTheme(HWND hWnd, bool dark) { | 
					
						
							|  |  |  |   const BOOL isDarkMode = dark; | 
					
						
							|  |  |  |   HRESULT result = DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, | 
					
						
							|  |  |  |                                          &isDarkMode, sizeof(isDarkMode)); | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 12:27:28 -04:00
										 |  |  |   if (FAILED(result)) | 
					
						
							|  |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 12:27:28 -04:00
										 |  |  |   return S_OK; | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 12:55:47 -07:00
										 |  |  | namespace electron::win { | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 12:27:28 -04:00
										 |  |  | bool IsDarkModeSupported() { | 
					
						
							|  |  |  |   auto* os_info = base::win::OSInfo::GetInstance(); | 
					
						
							|  |  |  |   auto const version = os_info->version(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return version >= base::win::Version::WIN10_20H1; | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 12:27:28 -04:00
										 |  |  | void SetDarkModeForWindow(HWND hWnd) { | 
					
						
							|  |  |  |   ui::NativeTheme* theme = ui::NativeTheme::GetInstanceForNativeUi(); | 
					
						
							|  |  |  |   bool dark = | 
					
						
							|  |  |  |       theme->ShouldUseDarkColors() && !theme->UserHasContrastPreference(); | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 12:27:28 -04:00
										 |  |  |   TrySetWindowTheme(hWnd, dark); | 
					
						
							| 
									
										
										
										
											2020-10-28 15:00:21 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 12:55:47 -07:00
										 |  |  | }  // namespace electron::win
 |