Skip to content

Commit

Permalink
Update windows to 0.39.0 (#544)
Browse files Browse the repository at this point in the history
* Update windows to 0.39.0

* Add a change log entry

* Avoid the w!() macro since it relies on HSTRING

* Fix with tray feature enabled

* Use the null() method on PSTR types

* Use the from_raw() method on PSTR types
  • Loading branch information
wravery committed Aug 31, 2022
1 parent be7ee93 commit 84e1a9f
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 77 deletions.
9 changes: 9 additions & 0 deletions .changes/windows-0.39.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"tao": "patch"
---

Update `windows-rs` to the latest 0.39.0 release.

The `alloc` feature has been removed, which means it no longer accepts Rust `String` or `&str` parameters and implicitly converts them to `PWSTR` or `PSTR`.

For string literals, that feature was replaced with `s!()` and `w!()` macros which null terminate the string literal at compile time and convert to UTF-16 if necessary. The `s!()` macro is fine, however the `w!()` macro uses `HSTRING` types from WinRT for maximum compatibility with WinRT types. Since Tao only uses Win32 APIs, this change relies on `util::encode_wide` to convert to a `Vec<u16>` instead.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ cc = "1"
parking_lot = "0.12"
unicode-segmentation = "1.9.0"
image = { version = "0.24", default-features = false }
windows-implement = "0.37.0"
windows-implement = "0.39.0"

[target."cfg(target_os = \"windows\")".dependencies.windows]
version = "0.37.0"
version = "0.39.0"
features = [
"alloc",
"implement",
"Win32_Devices_HumanInterfaceDevice",
"Win32_Foundation",
Expand Down
11 changes: 7 additions & 4 deletions src/platform_impl/windows/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// Copyright 2021-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

use super::util;
use crate::clipboard::{ClipboardFormat, FormatId};
use std::{ffi::OsStr, os::windows::ffi::OsStrExt, ptr};
use windows::{
core::PWSTR,
core::{PCWSTR, PWSTR},
Win32::{
Foundation::{HANDLE, HWND},
System::{
DataExchange::{
CloseClipboard, EmptyClipboard, GetClipboardData, OpenClipboard, RegisterClipboardFormatA,
CloseClipboard, EmptyClipboard, GetClipboardData, OpenClipboard, RegisterClipboardFormatW,
SetClipboardData,
},
Memory::{GlobalAlloc, GlobalLock, GlobalUnlock, GMEM_MOVEABLE},
Expand All @@ -35,7 +36,7 @@ impl Clipboard {
if handle.is_invalid() {
None
} else {
let unic_str = PWSTR(GlobalLock(handle.0) as *mut _);
let unic_str = PWSTR::from_raw(GlobalLock(handle.0) as *mut _);
let mut len = 0;
while *unic_str.0.offset(len) != 0 {
len += 1;
Expand Down Expand Up @@ -91,7 +92,9 @@ fn get_format_id(format: FormatId) -> Option<u32> {

fn register_identifier(ident: &str) -> Option<u32> {
unsafe {
let pb_format = RegisterClipboardFormatA(ident);
let clipboard_format = util::encode_wide(ident);

let pb_format = RegisterClipboardFormatW(PCWSTR::from_raw(clipboard_format.as_ptr()));
if pb_format == 0 {
#[cfg(debug_assertions)]
println!(
Expand Down
12 changes: 6 additions & 6 deletions src/platform_impl/windows/dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// 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::{
core::{PCSTR, PCWSTR},
core::{s, PCSTR, PCWSTR, PSTR},
Win32::{
Foundation::{BOOL, HWND},
System::LibraryLoader::*,
Expand Down Expand Up @@ -87,15 +87,15 @@ pub fn try_theme(hwnd: HWND, preferred_theme: Option<Theme>) -> Theme {
} else {
Theme::Light
};
let theme_name = PCWSTR(
let theme_name = PCWSTR::from_raw(
match theme {
Theme::Dark => DARK_THEME_NAME.clone(),
Theme::Light => LIGHT_THEME_NAME.clone(),
}
.as_ptr(),
);

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

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

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

if module.is_invalid() {
return None;
}

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

handle.map(|handle| std::mem::transmute(handle))
Expand All @@ -187,7 +187,7 @@ fn is_high_contrast() -> bool {
let mut hc = HIGHCONTRASTA {
cbSize: 0,
dwFlags: Default::default(),
lpszDefaultScheme: Default::default(),
lpszDefaultScheme: PSTR::null(),
};

let ok = unsafe {
Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/windows/drop_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl FileDropHandler {
Self {
window,
send_event,
cursor_effect: DROPEFFECT_NONE.into(),
cursor_effect: DROPEFFECT_NONE.0.into(),
hovered_is_valid: false.into(),
}
}
Expand Down Expand Up @@ -122,8 +122,8 @@ impl IDropTarget_Impl for FileDropHandler {
DROPEFFECT_NONE
};
*self.hovered_is_valid.get() = hovered_is_valid;
*self.cursor_effect.get() = cursor_effect;
*pdwEffect = cursor_effect;
*self.cursor_effect.get() = cursor_effect.0;
*pdwEffect = cursor_effect.0;
}
Ok(())
}
Expand Down
32 changes: 16 additions & 16 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{
time::{Duration, Instant},
};
use windows::{
core::PCWSTR,
core::{s, PCWSTR},
Win32::{
Devices::HumanInterfaceDevice::*,
Foundation::{
Expand Down Expand Up @@ -555,56 +555,56 @@ lazy_static! {
/// WPARAM and LPARAM are unused.
static ref USER_EVENT_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Tao::WakeupMsg")
RegisterWindowMessageA(s!("Tao::WakeupMsg"))
}
};
/// Message sent when we want to execute a closure in the thread.
/// WPARAM contains a Box<Box<dyn FnMut()>> that must be retrieved with `Box::from_raw`,
/// and LPARAM is unused.
static ref EXEC_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Tao::ExecMsg")
RegisterWindowMessageA(s!("Tao::ExecMsg"))
}
};
static ref PROCESS_NEW_EVENTS_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Tao::ProcessNewEvents")
RegisterWindowMessageA(s!("Tao::ProcessNewEvents"))
}
};
/// lparam is the wait thread's message id.
static ref SEND_WAIT_THREAD_ID_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Tao::SendWaitThreadId")
RegisterWindowMessageA(s!("Tao::SendWaitThreadId"))
}
};
/// lparam points to a `Box<Instant>` signifying the time `PROCESS_NEW_EVENTS_MSG_ID` should
/// be sent.
static ref WAIT_UNTIL_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Tao::WaitUntil")
RegisterWindowMessageA(s!("Tao::WaitUntil"))
}
};
static ref CANCEL_WAIT_UNTIL_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Tao::CancelWaitUntil")
RegisterWindowMessageA(s!("Tao::CancelWaitUntil"))
}
};
/// Message sent by a `Window` when it wants to be destroyed by the main thread.
/// WPARAM and LPARAM are unused.
pub static ref DESTROY_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Tao::DestroyMsg")
RegisterWindowMessageA(s!("Tao::DestroyMsg"))
}
};
/// WPARAM is a bool specifying the `WindowFlags::MARKER_RETAIN_STATE_ON_SIZE` flag. See the
/// documentation in the `window_state` module for more information.
pub static ref SET_RETAIN_STATE_ON_SIZE_MSG_ID: u32 = unsafe {
RegisterWindowMessageA("Tao::SetRetainMaximized")
RegisterWindowMessageA(s!("Tao::SetRetainMaximized"))
};
/// When the taskbar is created, it registers a message with the "TaskbarCreated" string and then broadcasts this message to all top-level windows
/// When the application receives this message, it should assume that any taskbar icons it added have been removed and add them again.
pub static ref S_U_TASKBAR_RESTART: u32 = unsafe {
RegisterWindowMessageA("TaskbarCreated")
RegisterWindowMessageA(s!("TaskbarCreated"))
};
static ref THREAD_EVENT_TARGET_WINDOW_CLASS: Vec<u16> = unsafe {
let class_name= util::encode_wide("Tao Thread Event Target");
Expand All @@ -615,12 +615,12 @@ lazy_static! {
lpfnWndProc: Some(util::call_default_window_proc),
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: GetModuleHandleW(PCWSTR::default()).unwrap_or_default(),
hInstance: GetModuleHandleW(PCWSTR::null()).unwrap_or_default(),
hIcon: HICON::default(),
hCursor: HCURSOR::default(), // must be null in order for cursor state to work properly
hbrBackground: HBRUSH::default(),
lpszMenuName: Default::default(),
lpszClassName: PCWSTR(class_name.as_ptr()),
lpszMenuName: PCWSTR::null(),
lpszClassName: PCWSTR::from_raw(class_name.as_ptr()),
hIconSm: HICON::default(),
};

Expand All @@ -642,16 +642,16 @@ fn create_event_target_window() -> HWND {
// `explorer.exe` and then starting the process back up.
// It is unclear why the bug is triggered by waiting for several hours.
WS_EX_TOOLWINDOW,
PCWSTR(THREAD_EVENT_TARGET_WINDOW_CLASS.clone().as_ptr()),
PCWSTR::default(),
PCWSTR::from_raw(THREAD_EVENT_TARGET_WINDOW_CLASS.clone().as_ptr()),
PCWSTR::null(),
WS_OVERLAPPED,
0,
0,
0,
0,
HWND::default(),
HMENU::default(),
GetModuleHandleW(PCWSTR::default()).unwrap_or_default(),
GetModuleHandleW(PCWSTR::null()).unwrap_or_default(),
ptr::null_mut(),
)
};
Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/windows/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl WinIcon {
let handle = unsafe {
LoadImageW(
HINSTANCE::default(),
PCWSTR(wide_path.as_ptr()),
PCWSTR::from_raw(wide_path.as_ptr()),
IMAGE_ICON,
width as i32,
height as i32,
Expand All @@ -109,8 +109,8 @@ impl WinIcon {
let (width, height) = size.map(Into::into).unwrap_or((0, 0));
let handle = unsafe {
LoadImageW(
GetModuleHandleW(PCWSTR::default()).unwrap_or_default(),
PCWSTR(resource_id as usize as *const u16),
GetModuleHandleW(PCWSTR::null()).unwrap_or_default(),
PCWSTR::from_raw(resource_id as usize as *const u16),
IMAGE_ICON,
width as i32,
height as i32,
Expand Down
Loading

0 comments on commit 84e1a9f

Please sign in to comment.