Skip to content

Commit

Permalink
feat: migrate manual implementation to the light windows-version cr…
Browse files Browse the repository at this point in the history
…ate (#8243)
  • Loading branch information
amrbashir authored Nov 19, 2023
1 parent f93148e commit 5e84e92
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 82 deletions.
5 changes: 5 additions & 0 deletions .changes/tauri-utils-windows-version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-utils': 'minor:breaking'
---

Changed `platform::windows_version` to return a `(u32, u32, u32)` instead of `Option<(u32, u32, u32)>`
11 changes: 2 additions & 9 deletions core/tauri-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,8 @@ log = "0.4.20"
[target."cfg(target_os = \"linux\")".dependencies]
heck = "0.4"

[target."cfg(windows)".dependencies.windows]
version = "0.51.1"
features = [
"implement",
"Win32_Foundation",
"Win32_System_Com",
"Win32_System_LibraryLoader",
"Win32_System_SystemInformation"
]
[target."cfg(windows)".dependencies]
windows-version = "0.1"

[features]
build = [ "proc-macro2", "quote" ]
Expand Down
78 changes: 6 additions & 72 deletions core/tauri-utils/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,85 +257,19 @@ pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> crate::Result<Path
}

#[cfg(windows)]
pub use windows_platform::{get_function_impl, is_windows_7, windows_version};
pub use windows_platform::{is_windows_7, windows_version};

#[cfg(windows)]
mod windows_platform {
use std::{iter::once, os::windows::prelude::OsStrExt};
use windows::{
core::{PCSTR, PCWSTR},
Win32::{
Foundation::FARPROC,
System::{
LibraryLoader::{GetProcAddress, LoadLibraryW},
SystemInformation::OSVERSIONINFOW,
},
},
};

/// Checks if we're running on Windows 7.
pub fn is_windows_7() -> bool {
if let Some(v) = windows_version() {
// windows 7 is 6.1
if v.0 == 6 && v.1 == 1 {
return true;
}
}
false
}

fn encode_wide(string: impl AsRef<std::ffi::OsStr>) -> Vec<u16> {
string.as_ref().encode_wide().chain(once(0)).collect()
}

/// Helper function to dynamically load function pointer.
/// `library` and `function` must be null-terminated.
pub fn get_function_impl(library: &str, function: &str) -> Option<FARPROC> {
let library = encode_wide(library);
assert_eq!(function.chars().last(), Some('\0'));
let function = PCSTR::from_raw(function.as_ptr());

// Library names we will use are ASCII so we can use the A version to avoid string conversion.
let module = unsafe { LoadLibraryW(PCWSTR::from_raw(library.as_ptr())) }.unwrap_or_default();
if module.is_invalid() {
None
} else {
Some(unsafe { GetProcAddress(module, function) })
}
}

macro_rules! get_function {
($lib:expr, $func:ident) => {
get_function_impl(concat!($lib, '\0'), concat!(stringify!($func), '\0'))
.map(|f| unsafe { std::mem::transmute::<windows::Win32::Foundation::FARPROC, $func>(f) })
};
let v = windows_version();
v.0 == 6 && v.1 == 1
}

/// Returns a tuple of (major, minor, buildnumber) for the Windows version.
pub fn windows_version() -> Option<(u32, u32, u32)> {
type RtlGetVersion = unsafe extern "system" fn(*mut OSVERSIONINFOW) -> i32;
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],
};

let status = (rtl_get_version)(&mut vi as _);

if status >= 0 {
Some((vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber))
} else {
None
}
}
} else {
None
}
pub fn windows_version() -> (u32, u32, u32) {
let v = windows_version::OsVersion::current();
(v.major, v.minor, v.build)
}
}
2 changes: 1 addition & 1 deletion core/tauri/src/vibrancy/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::ffi::c_void;
use crate::utils::config::WindowEffectsConfig;
use crate::window::{Color, Effect};
use raw_window_handle::HasRawWindowHandle;
use tauri_utils::platform::{get_function_impl, is_windows_7, windows_version};
use tauri_utils::platform::{is_windows_7, windows_version};
use windows::Win32::Foundation::HWND;

pub fn apply_effects(window: impl HasRawWindowHandle, effects: WindowEffectsConfig) {
Expand Down

0 comments on commit 5e84e92

Please sign in to comment.