Migrate from lazy_static to once_cell
This commit is contained in:
parent
4c39b3188c
commit
2c01e9e747
18 changed files with 477 additions and 519 deletions
|
|
@ -2,6 +2,7 @@
|
|||
/// which is inspired by the solution in https://github.com/ysc3839/win32-darkmode
|
||||
use std::{ffi::c_void, ptr};
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use windows_sys::{
|
||||
core::PCSTR,
|
||||
Win32::{
|
||||
|
|
@ -22,47 +23,45 @@ use crate::window::Theme;
|
|||
|
||||
use super::util;
|
||||
|
||||
lazy_static! {
|
||||
static ref WIN10_BUILD_VERSION: Option<u32> = {
|
||||
type RtlGetVersion = unsafe extern "system" fn (*mut OSVERSIONINFOW) -> NTSTATUS;
|
||||
let handle = get_function!("ntdll.dll", RtlGetVersion);
|
||||
static WIN10_BUILD_VERSION: Lazy<Option<u32>> = Lazy::new(|| {
|
||||
type RtlGetVersion = unsafe extern "system" fn(*mut OSVERSIONINFOW) -> NTSTATUS;
|
||||
let handle = get_function!("ntdll.dll", RtlGetVersion);
|
||||
|
||||
if let Some(rtl_get_version) = handle {
|
||||
unsafe {
|
||||
let mut vi = OSVERSIONINFOW {
|
||||
dwOSVersionInfoSize: 0,
|
||||
dwMajorVersion: 0,
|
||||
dwMinorVersion: 0,
|
||||
dwBuildNumber: 0,
|
||||
dwPlatformId: 0,
|
||||
szCSDVersion: [0; 128],
|
||||
};
|
||||
if let Some(rtl_get_version) = handle {
|
||||
unsafe {
|
||||
let mut vi = OSVERSIONINFOW {
|
||||
dwOSVersionInfoSize: 0,
|
||||
dwMajorVersion: 0,
|
||||
dwMinorVersion: 0,
|
||||
dwBuildNumber: 0,
|
||||
dwPlatformId: 0,
|
||||
szCSDVersion: [0; 128],
|
||||
};
|
||||
|
||||
let status = (rtl_get_version)(&mut vi);
|
||||
let status = (rtl_get_version)(&mut vi);
|
||||
|
||||
if status >= 0 && vi.dwMajorVersion == 10 && vi.dwMinorVersion == 0 {
|
||||
Some(vi.dwBuildNumber)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if status >= 0 && vi.dwMajorVersion == 10 && vi.dwMinorVersion == 0 {
|
||||
Some(vi.dwBuildNumber)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
static ref DARK_MODE_SUPPORTED: bool = {
|
||||
// We won't try to do anything for windows versions < 17763
|
||||
// (Windows 10 October 2018 update)
|
||||
match *WIN10_BUILD_VERSION {
|
||||
Some(v) => v >= 17763,
|
||||
None => false
|
||||
}
|
||||
};
|
||||
static DARK_MODE_SUPPORTED: Lazy<bool> = Lazy::new(|| {
|
||||
// We won't try to do anything for windows versions < 17763
|
||||
// (Windows 10 October 2018 update)
|
||||
match *WIN10_BUILD_VERSION {
|
||||
Some(v) => v >= 17763,
|
||||
None => false,
|
||||
}
|
||||
});
|
||||
|
||||
static ref DARK_THEME_NAME: Vec<u16> = util::encode_wide("DarkMode_Explorer");
|
||||
static ref LIGHT_THEME_NAME: Vec<u16> = util::encode_wide("");
|
||||
}
|
||||
static DARK_THEME_NAME: Lazy<Vec<u16>> = Lazy::new(|| util::encode_wide("DarkMode_Explorer"));
|
||||
static LIGHT_THEME_NAME: Lazy<Vec<u16>> = Lazy::new(|| util::encode_wide(""));
|
||||
|
||||
/// Attempt to set a theme on a window, if necessary.
|
||||
/// Returns the theme that was picked
|
||||
|
|
@ -113,10 +112,8 @@ fn set_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) -> bool {
|
|||
cbData: usize,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref SET_WINDOW_COMPOSITION_ATTRIBUTE: Option<SetWindowCompositionAttribute> =
|
||||
get_function!("user32.dll", SetWindowCompositionAttribute);
|
||||
}
|
||||
static SET_WINDOW_COMPOSITION_ATTRIBUTE: Lazy<Option<SetWindowCompositionAttribute>> =
|
||||
Lazy::new(|| get_function!("user32.dll", SetWindowCompositionAttribute));
|
||||
|
||||
if let Some(set_window_composition_attribute) = *SET_WINDOW_COMPOSITION_ATTRIBUTE {
|
||||
unsafe {
|
||||
|
|
@ -144,23 +141,19 @@ fn should_use_dark_mode() -> bool {
|
|||
|
||||
fn should_apps_use_dark_mode() -> bool {
|
||||
type ShouldAppsUseDarkMode = unsafe extern "system" fn() -> bool;
|
||||
lazy_static! {
|
||||
static ref SHOULD_APPS_USE_DARK_MODE: Option<ShouldAppsUseDarkMode> = {
|
||||
unsafe {
|
||||
const UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL: PCSTR = 132 as PCSTR;
|
||||
static SHOULD_APPS_USE_DARK_MODE: Lazy<Option<ShouldAppsUseDarkMode>> = Lazy::new(|| unsafe {
|
||||
const UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL: PCSTR = 132 as PCSTR;
|
||||
|
||||
let module = LoadLibraryA("uxtheme.dll\0".as_ptr());
|
||||
let module = LoadLibraryA("uxtheme.dll\0".as_ptr());
|
||||
|
||||
if module == 0 {
|
||||
return None;
|
||||
}
|
||||
if module == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let handle = GetProcAddress(module, UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL);
|
||||
let handle = GetProcAddress(module, UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL);
|
||||
|
||||
handle.map(|handle| std::mem::transmute(handle))
|
||||
}
|
||||
};
|
||||
}
|
||||
handle.map(|handle| std::mem::transmute(handle))
|
||||
});
|
||||
|
||||
SHOULD_APPS_USE_DARK_MODE
|
||||
.map(|should_apps_use_dark_mode| unsafe { (should_apps_use_dark_mode)() })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue