Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(windows): apply dark mode to menus and other controls #751

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/apply-dark-mode-to-menus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

On Windows an additional undocumented function needs to be called to apply the dark mode theme to menus.
75 changes: 75 additions & 0 deletions src/platform_impl/windows/dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ use std::ffi::c_void;

use crate::{platform_impl::platform::util, window::Theme};

#[allow(dead_code)]
enum PreferredAppMode {
Default,
AllowDark,
ForceDark,
ForceLight,
Max,
}

lazy_static! {
static ref WIN10_BUILD_VERSION: Option<u32> = {
// FIXME: RtlGetVersion is a documented windows API,
Expand Down Expand Up @@ -141,6 +150,7 @@ fn set_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) -> bool {
};

let status = set_window_composition_attribute(hwnd, &mut data as *mut _);
set_preferred_app_mode(is_dark_mode);
amrbashir marked this conversation as resolved.
Show resolved Hide resolved

status.as_bool()
}
Expand Down Expand Up @@ -181,6 +191,71 @@ fn should_apps_use_dark_mode() -> bool {
.unwrap_or(false)
}

// This enables dark mode menus
fn set_preferred_app_mode(is_dark_mode: bool) -> bool {
let build_version = (*WIN10_BUILD_VERSION).unwrap();

// SetPreferredAppMode is for Windows 1903+ (18362)
if build_version >= 18362 {
type SetPreferredAppMode = unsafe extern "system" fn(PreferredAppMode) -> bool;
lazy_static! {
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
static ref SET_PREFERRED_APP_MODE: Option<SetPreferredAppMode> = {
unsafe {
const UXTHEME_SETPREFERREDAPPMODE_ORDINAL: u16 = 135;

let module = LoadLibraryA(s!("uxtheme.dll")).unwrap_or_default();

if module.is_invalid() {
return None;
}

let handle = GetProcAddress(
module,
PCSTR::from_raw(UXTHEME_SETPREFERREDAPPMODE_ORDINAL as usize as *mut _),
);

handle.map(|handle| std::mem::transmute(handle))
}
};
}

let app_mode = if is_dark_mode {
PreferredAppMode::ForceDark
} else {
PreferredAppMode::ForceLight
};
SET_PREFERRED_APP_MODE
.map(|set_preferred_app_mode| unsafe { (set_preferred_app_mode)(app_mode) })
.unwrap_or(false)
} else {
type AllowDarkModeForApp = unsafe extern "system" fn(bool) -> bool;
lazy_static! {
static ref ALLOW_DARK_MODE_FOR_APP: Option<AllowDarkModeForApp> = {
unsafe {
const UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL: u16 = 135;

let module = LoadLibraryA(s!("uxtheme.dll")).unwrap_or_default();

if module.is_invalid() {
return None;
}

let handle = GetProcAddress(
module,
PCSTR::from_raw(UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL as usize as *mut _),
);

handle.map(|handle| std::mem::transmute(handle))
}
};
}

ALLOW_DARK_MODE_FOR_APP
.map(|allow_dark_mode_for_app| unsafe { (allow_dark_mode_for_app)(is_dark_mode) })
.unwrap_or(false)
}
}

const HCF_HIGHCONTRASTON: u32 = 1;

fn is_high_contrast() -> bool {
Expand Down
Loading