From f678d02193473ac75482f2ad83e3c40e86d33d2b Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 7 Aug 2019 22:10:30 -0700 Subject: [PATCH] use dark mode --- window/Cargo.toml | 1 + window/src/os/windows/window.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/window/Cargo.toml b/window/Cargo.toml index ddce4925b..f5d733e4c 100644 --- a/window/Cargo.toml +++ b/window/Cargo.toml @@ -19,6 +19,7 @@ lazy_static = "1.3" shared_library = "0.1" uds_windows = "0.1" winapi = { version = "0.3", features = [ + "dwmapi", "winuser", "consoleapi", "handleapi", diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 473e4608b..292c04b05 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -142,7 +142,7 @@ impl Window { // Careful: `raw` owns a ref to inner, but there is no Drop impl let raw = arc_to_pointer(&inner); - match Self::create_window(class_name, name, width, height, raw) { + let hwnd = match Self::create_window(class_name, name, width, height, raw) { Ok(hwnd) => hwnd, Err(err) => { // Ensure that we drop the extra ref to raw before we return @@ -151,6 +151,8 @@ impl Window { } }; + enable_dark_mode(hwnd); + Ok(Window { inner }) } @@ -186,6 +188,34 @@ unsafe fn wm_ncdestroy(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> DefWindowProcW(hwnd, msg, wparam, lparam) } +fn enable_dark_mode(hwnd: HWND) { + // Prefer to run in dark mode. This could be made configurable without + // a huge amount of effort, but I think it's fine to just be always + // dark mode by default :-p + // Note that the MS terminal app uses the logic found here for this + // stuff: + // https://github.com/microsoft/terminal/blob/9b92986b49bed8cc41fde4d6ef080921c41e6d9e/src/interactivity/win32/windowtheme.cpp#L62 + use winapi::um::dwmapi::DwmSetWindowAttribute; + use winapi::um::uxtheme::SetWindowTheme; + + const DWMWA_USE_IMMERSIVE_DARK_MODE: DWORD = 19; + unsafe { + SetWindowTheme( + hwnd as _, + wide_string("DarkMode_Explorer").as_slice().as_ptr(), + std::ptr::null_mut(), + ); + + let enabled: BOOL = 1; + DwmSetWindowAttribute( + hwnd as _, + DWMWA_USE_IMMERSIVE_DARK_MODE, + &enabled as *const _ as *const _, + std::mem::size_of_val(&enabled) as u32, + ); + } +} + unsafe fn do_wnd_proc(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> LRESULT { match msg { WM_NCCREATE => return wm_nccreate(hwnd, msg, wparam, lparam),