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

Update the windows-rs crate to 0.37.0 #400

Merged
merged 14 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions .changes/windows-0.37.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"tao": patch
---

Update the `windows` crate to the latest 0.37.0 release.

The `#[implement]` macro in `windows-implement` and the `implement` feature in `windows` depend on some `const` generic features which stabilized in `rustc` 1.61. The MSRV on Windows targets is effectively 1.61, but other targets do not require these features.

Since developers on non-Windows platforms are not always able to upgrade their toolchain with `rustup`, the package remains at 1.56. Windows developers may get less friendly compiler errors about using unstable language features until they upgrade their toolchain if they build `tao` without `wry`, which has some Windows-specific dependencies that transitively raise the MSRV for `wry` to 1.61.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ cc = "1"
[target."cfg(target_os = \"windows\")".dependencies]
parking_lot = "0.11"
unicode-segmentation = "1.8.0"
windows_macros = "0.30.0"
windows-implement = "0.37.0"

[target."cfg(target_os = \"windows\")".dependencies.windows]
version = "0.30.0"
version = "0.37.0"
features = [
"alloc",
"implement",
"Win32_Devices_HumanInterfaceDevice",
"Win32_Foundation",
"Win32_Globalization",
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/windows/accelerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(crate) struct AccelTable {

impl AccelTable {
fn new(accel: &[ACCEL]) -> AccelTable {
let accel = unsafe { CreateAcceleratorTableW(accel as *const _ as *mut _, accel.len() as i32) };
let accel = unsafe { CreateAcceleratorTableW(accel) }.unwrap_or_default();
AccelTable {
accel: AccelHandle(accel.0),
}
Expand Down
39 changes: 20 additions & 19 deletions src/platform_impl/windows/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

use crate::clipboard::{ClipboardFormat, FormatId};
use std::{ffi::OsStr, os::windows::ffi::OsStrExt, ptr};
use windows::Win32::{
Foundation::{HANDLE, HWND, PSTR, PWSTR},
System::{
DataExchange::{
CloseClipboard, EmptyClipboard, GetClipboardData, OpenClipboard, RegisterClipboardFormatA,
SetClipboardData,
use windows::{
core::PWSTR,
Win32::{
Foundation::{HANDLE, HWND},
System::{
DataExchange::{
CloseClipboard, EmptyClipboard, GetClipboardData, OpenClipboard, RegisterClipboardFormatA,
SetClipboardData,
},
Memory::{GlobalAlloc, GlobalLock, GlobalUnlock, GMEM_MOVEABLE},
SystemServices::CF_UNICODETEXT,
},
Memory::{GlobalAlloc, GlobalLock, GlobalUnlock, GMEM_MOVEABLE},
SystemServices::CF_UNICODETEXT,
},
};

Expand All @@ -27,8 +30,8 @@ impl Clipboard {

pub(crate) fn read_text(&self) -> Option<String> {
with_clipboard(|| unsafe {
let handle = GetClipboardData(CF_UNICODETEXT);
if handle.0 == 0 {
let handle = GetClipboardData(CF_UNICODETEXT.0).unwrap_or_default();
if handle.is_invalid() {
None
} else {
let unic_str = PWSTR(GlobalLock(handle.0) as *mut _);
Expand Down Expand Up @@ -63,13 +66,11 @@ impl Clipboard {
continue;
}
};
let result = SetClipboardData(format_id, handle);
if result.0 == 0 {
if let Err(err) = SetClipboardData(format_id, handle) {
#[cfg(debug_assertions)]
println!(
"failed to set clipboard for fmt {}, error: {}",
&format.identifier,
windows::core::Error::from_win32().code().0
&format.identifier, err
);
}
}
Expand All @@ -82,7 +83,7 @@ fn get_format_id(format: FormatId) -> Option<u32> {
return Some(*id);
}
match format {
ClipboardFormat::TEXT => Some(CF_UNICODETEXT),
ClipboardFormat::TEXT => Some(CF_UNICODETEXT.0),
other => register_identifier(other),
}
}
Expand All @@ -108,14 +109,14 @@ unsafe fn make_handle(format: &ClipboardFormat) -> HANDLE {
let s: &OsStr = std::str::from_utf8_unchecked(&format.data).as_ref();
let wstr: Vec<u16> = s.encode_wide().chain(Some(0)).collect();
let handle = GlobalAlloc(GMEM_MOVEABLE, wstr.len() * std::mem::size_of::<u16>());
let locked = PWSTR(GlobalLock(handle) as *mut _);
ptr::copy_nonoverlapping(wstr.as_ptr(), locked.0, wstr.len());
let locked = GlobalLock(handle) as *mut _;
ptr::copy_nonoverlapping(wstr.as_ptr(), locked, wstr.len());
GlobalUnlock(handle);
handle
} else {
let handle = GlobalAlloc(GMEM_MOVEABLE, format.data.len() * std::mem::size_of::<u8>());
let locked = PSTR(GlobalLock(handle) as *mut _);
ptr::copy_nonoverlapping(format.data.as_ptr(), locked.0, format.data.len());
let locked = GlobalLock(handle) as *mut _;
ptr::copy_nonoverlapping(format.data.as_ptr(), locked, format.data.len());
GlobalUnlock(handle);
handle
})
Expand Down
29 changes: 16 additions & 13 deletions src/platform_impl/windows/dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

/// This is a simple implementation of support for Windows Dark Mode,
/// which is inspired by the solution in https://github.com/ysc3839/win32-darkmode
use windows::Win32::{
Foundation::{BOOL, HWND, PSTR, PWSTR},
System::LibraryLoader::*,
UI::{Accessibility::*, Controls::*, WindowsAndMessaging::*},
use windows::{
core::{PCSTR, PCWSTR},
Win32::{
Foundation::{BOOL, HWND},
System::LibraryLoader::*,
UI::{Accessibility::*, Controls::*, WindowsAndMessaging::*},
},
};

use std::ffi::c_void;
Expand Down Expand Up @@ -83,15 +86,15 @@ pub fn try_theme(hwnd: HWND, preferred_theme: Option<Theme>) -> Theme {
} else {
Theme::Light
};
let theme_name = PWSTR(
let theme_name = PCWSTR(
match theme {
Theme::Dark => DARK_THEME_NAME.clone(),
Theme::Light => LIGHT_THEME_NAME.clone(),
}
.as_mut_ptr(),
.as_ptr(),
);

let status = unsafe { SetWindowTheme(hwnd, theme_name, PWSTR::default()) };
let status = unsafe { SetWindowTheme(hwnd, theme_name, PCWSTR::default()) };

if status.is_ok() && set_dark_mode_for_window(hwnd, is_dark_mode) {
return theme;
Expand Down Expand Up @@ -156,15 +159,15 @@ fn should_apps_use_dark_mode() -> bool {
unsafe {
const UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL: u16 = 132;

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

if module.is_invalid() {
return None;
}

let handle = GetProcAddress(
module,
PSTR(UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL as usize as *mut _),
PCSTR(UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL as usize as *mut _),
);

handle.map(|handle| std::mem::transmute(handle))
Expand All @@ -182,18 +185,18 @@ const HCF_HIGHCONTRASTON: u32 = 1;
fn is_high_contrast() -> bool {
let mut hc = HIGHCONTRASTA {
cbSize: 0,
dwFlags: 0,
lpszDefaultScheme: PSTR::default(),
dwFlags: Default::default(),
lpszDefaultScheme: Default::default(),
};

let ok = unsafe {
SystemParametersInfoA(
SPI_GETHIGHCONTRAST,
std::mem::size_of_val(&hc) as _,
&mut hc as *mut _ as _,
0,
Default::default(),
)
};

ok.as_bool() && (HCF_HIGHCONTRASTON & hc.dwFlags) != 0
ok.as_bool() && (HCF_HIGHCONTRASTON & hc.dwFlags.0) != 0
}
Loading