Skip to content

Commit

Permalink
Entire function wrapped in condition, added change file
Browse files Browse the repository at this point in the history
Wrapping function in DarkModeSupported condition, added change file.
  • Loading branch information
Jnschrber committed Nov 28, 2024
1 parent 2c03515 commit 1f62329
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changes/dark-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

Windows: Executing the function allow_dark_mode_for_app only if darkmode is supported.
94 changes: 46 additions & 48 deletions src/platform_impl/windows/dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,62 +42,60 @@ static DARK_MODE_SUPPORTED: Lazy<bool> = Lazy::new(|| {
});

pub fn allow_dark_mode_for_app(is_dark_mode: bool) {
const UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL: u16 = 135;
type AllowDarkModeForApp = unsafe extern "system" fn(bool) -> bool;
static ALLOW_DARK_MODE_FOR_APP: Lazy<Option<AllowDarkModeForApp>> = Lazy::new(|| unsafe {
if HMODULE(*HUXTHEME as _).is_invalid() {
return None;
}
if *DARK_MODE_SUPPORTED {
const UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL: u16 = 135;
type AllowDarkModeForApp = unsafe extern "system" fn(bool) -> bool;
static ALLOW_DARK_MODE_FOR_APP: Lazy<Option<AllowDarkModeForApp>> = Lazy::new(|| unsafe {
if HMODULE(*HUXTHEME as _).is_invalid() {
return None;
}

GetProcAddress(
HMODULE(*HUXTHEME as _),
PCSTR::from_raw(UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL as usize as *mut _),
)
.map(|handle| std::mem::transmute(handle))
});
GetProcAddress(
HMODULE(*HUXTHEME as _),
PCSTR::from_raw(UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL as usize as *mut _),
)
.map(|handle| std::mem::transmute(handle))
});

#[repr(C)]
enum PreferredAppMode {
Default,
AllowDark,
// ForceDark,
// ForceLight,
// Max,
}
const UXTHEME_SETPREFERREDAPPMODE_ORDINAL: u16 = 135;
type SetPreferredAppMode = unsafe extern "system" fn(PreferredAppMode) -> PreferredAppMode;
static SET_PREFERRED_APP_MODE: Lazy<Option<SetPreferredAppMode>> = Lazy::new(|| unsafe {
if HMODULE(*HUXTHEME as _).is_invalid() {
return None;
#[repr(C)]
enum PreferredAppMode {
Default,
AllowDark,
// ForceDark,
// ForceLight,
// Max,
}
const UXTHEME_SETPREFERREDAPPMODE_ORDINAL: u16 = 135;
type SetPreferredAppMode = unsafe extern "system" fn(PreferredAppMode) -> PreferredAppMode;
static SET_PREFERRED_APP_MODE: Lazy<Option<SetPreferredAppMode>> = Lazy::new(|| unsafe {
if HMODULE(*HUXTHEME as _).is_invalid() {
return None;
}

GetProcAddress(
HMODULE(*HUXTHEME as _),
PCSTR::from_raw(UXTHEME_SETPREFERREDAPPMODE_ORDINAL as usize as *mut _),
)
.map(|handle| std::mem::transmute(handle))
});
GetProcAddress(
HMODULE(*HUXTHEME as _),
PCSTR::from_raw(UXTHEME_SETPREFERREDAPPMODE_ORDINAL as usize as *mut _),
)
.map(|handle| std::mem::transmute(handle))
});

if let Some(ver) = *WIN10_BUILD_VERSION {
// in case Windows 10 Build version < 18362, we have to call a special command (_allow_dark_mode_for_app)
if ver < 18362 {
// but only call that command if the dark mode is supported...
if *DARK_MODE_SUPPORTED {
if let Some(_allow_dark_mode_for_app) = *ALLOW_DARK_MODE_FOR_APP {
unsafe { _allow_dark_mode_for_app(is_dark_mode) };
}
if let Some(ver) = *WIN10_BUILD_VERSION {
if ver < 18362 {
if let Some(_allow_dark_mode_for_app) = *ALLOW_DARK_MODE_FOR_APP {
unsafe { _allow_dark_mode_for_app(is_dark_mode) };
}
} else if let Some(_set_preferred_app_mode) = *SET_PREFERRED_APP_MODE {
let mode = if is_dark_mode {
PreferredAppMode::AllowDark
} else {
PreferredAppMode::Default
};
unsafe { _set_preferred_app_mode(mode) };
} else if let Some(_set_preferred_app_mode) = *SET_PREFERRED_APP_MODE {
let mode = if is_dark_mode {
PreferredAppMode::AllowDark
} else {
PreferredAppMode::Default
};
unsafe { _set_preferred_app_mode(mode) };
}
}
}

refresh_immersive_color_policy_state();
refresh_immersive_color_policy_state();
}
}

fn refresh_immersive_color_policy_state() {
Expand Down

0 comments on commit 1f62329

Please sign in to comment.