Skip to content

Commit

Permalink
Remove usage of std feature
Browse files Browse the repository at this point in the history
  • Loading branch information
clemenswasser committed Nov 10, 2021
1 parent 8ef5363 commit 6629e52
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 59 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ parking_lot = "0.11"
version = "0.26"
features = [
"build",
"std",
"Win32_Devices_HumanInterfaceDevice",
"Win32_Foundation",
"Win32_Globalization",
Expand Down
14 changes: 8 additions & 6 deletions src/platform_impl/windows/dark_mode.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/// 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 std::ffi::{c_void, OsStr};
use std::ffi::c_void;

use windows::{
runtime::Handle,
Win32::{
Foundation::{BOOL, HWND, NTSTATUS, PSTR},
Foundation::{BOOL, HWND, NTSTATUS, PSTR, PWSTR},
System::{
LibraryLoader::{GetProcAddress, LoadLibraryA},
SystemInformation::OSVERSIONINFOW,
Expand All @@ -22,7 +22,7 @@ use windows::{

use crate::window::Theme;

use super::util::has_flag;
use super::util;

lazy_static! {
static ref WIN10_BUILD_VERSION: Option<u32> = {
Expand Down Expand Up @@ -86,7 +86,9 @@ pub fn try_theme(hwnd: HWND, preferred_theme: Option<Theme>) -> Theme {
Theme::Light => LIGHT_THEME_NAME,
};

let status = unsafe { SetWindowTheme(hwnd, OsStr::new(theme_name), None) };
let mut wide_theme_name = util::encode_wide(theme_name);

let status = unsafe { SetWindowTheme(hwnd, PWSTR(wide_theme_name.as_mut_ptr()), None) };

if status.is_ok() && set_dark_mode_for_window(hwnd, is_dark_mode) {
return theme;
Expand Down Expand Up @@ -152,7 +154,7 @@ fn should_apps_use_dark_mode() -> bool {
unsafe {
const UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL: PSTR = PSTR(132 as *mut u8);

let module = LoadLibraryA("uxtheme.dll");
let module = LoadLibraryA(PSTR("uxtheme.dll".as_ptr() as *mut u8));

if module.is_invalid() {
return None;
Expand Down Expand Up @@ -186,5 +188,5 @@ fn is_high_contrast() -> bool {
)
};

ok.as_bool() && has_flag(hc.dwFlags, HCF_HIGHCONTRASTON)
ok.as_bool() && util::has_flag(hc.dwFlags, HCF_HIGHCONTRASTON)
}
45 changes: 18 additions & 27 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use windows::{
Win32::{
Devices::HumanInterfaceDevice::MOUSE_MOVE_RELATIVE,
Foundation::{
BOOL, HANDLE, HWND, LPARAM, LRESULT, POINT, PWSTR, RECT, WAIT_TIMEOUT, WIN32_ERROR,
WPARAM,
BOOL, HANDLE, HWND, LPARAM, LRESULT, POINT, PSTR, PWSTR, RECT, WAIT_TIMEOUT,
WIN32_ERROR, WPARAM,
},
Graphics::Gdi::{
ClientToScreen, GetMonitorInfoW, GetUpdateRect, MonitorFromRect, MonitorFromWindow,
Expand Down Expand Up @@ -99,8 +99,6 @@ use crate::{
};
use runner::{EventLoopRunner, EventLoopRunnerShared};

use super::util::has_flag;

type GetPointerFrameInfoHistory = unsafe extern "system" fn(
pointerId: u32,
entriesCount: *mut u32,
Expand Down Expand Up @@ -601,61 +599,53 @@ lazy_static! {
// WPARAM and LPARAM are unused.
static ref USER_EVENT_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Winit::WakeupMsg")
RegisterWindowMessageA(PSTR("Winit::WakeupMsg".as_ptr() as *mut u8))
}
};
// 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("Winit::ExecMsg")
RegisterWindowMessageA(PSTR("Winit::ExecMsg".as_ptr() as *mut u8))
}
};
static ref PROCESS_NEW_EVENTS_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Winit::ProcessNewEvents")
RegisterWindowMessageA(PSTR("Winit::ProcessNewEvents".as_ptr() as *mut u8))
}
};
/// lparam is the wait thread's message id.
static ref SEND_WAIT_THREAD_ID_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Winit::SendWaitThreadId")
RegisterWindowMessageA(PSTR("Winit::SendWaitThreadId".as_ptr() as *mut u8))
}
};
/// 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("Winit::WaitUntil")
RegisterWindowMessageA(PSTR("Winit::WaitUntil".as_ptr() as *mut u8))
}
};
static ref CANCEL_WAIT_UNTIL_MSG_ID: u32 = {
unsafe {
RegisterWindowMessageA("Winit::CancelWaitUntil")
RegisterWindowMessageA(PSTR("Winit::CancelWaitUntil".as_ptr() as *mut u8))
}
};
// 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("Winit::DestroyMsg")
RegisterWindowMessageA(PSTR("Winit::DestroyMsg".as_ptr() as *mut u8))
}
};
// 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("Winit::SetRetainMaximized")
};
static ref THREAD_EVENT_TARGET_WINDOW_CLASS: Vec<u16> = {
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;

OsStr::new("Winit Thread Event Target")
.encode_wide()
.chain(Some(0).into_iter())
.collect()
RegisterWindowMessageA(PSTR("Winit::SetRetainMaximized".as_ptr() as *mut u8))
};
static ref THREAD_EVENT_TARGET_WINDOW_CLASS: Vec<u16> = util::encode_wide("Winit Thread Event Target");
}

fn create_event_target_window<T: 'static>() -> HWND {
Expand Down Expand Up @@ -1534,11 +1524,11 @@ unsafe fn public_window_callback_inner<T: 'static>(
userdata.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
event: WindowEvent::Touch(Touch {
phase: if has_flag(input.dwFlags, TOUCHEVENTF_DOWN) {
phase: if util::has_flag(input.dwFlags, TOUCHEVENTF_DOWN) {
TouchPhase::Started
} else if has_flag(input.dwFlags, TOUCHEVENTF_UP) {
} else if util::has_flag(input.dwFlags, TOUCHEVENTF_UP) {
TouchPhase::Ended
} else if has_flag(input.dwFlags, TOUCHEVENTF_MOVE) {
} else if util::has_flag(input.dwFlags, TOUCHEVENTF_MOVE) {
TouchPhase::Moved
} else {
continue;
Expand Down Expand Up @@ -1674,11 +1664,12 @@ unsafe fn public_window_callback_inner<T: 'static>(
userdata.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
event: WindowEvent::Touch(Touch {
phase: if has_flag(pointer_info.pointerFlags, POINTER_FLAG_DOWN) {
phase: if util::has_flag(pointer_info.pointerFlags, POINTER_FLAG_DOWN) {
TouchPhase::Started
} else if has_flag(pointer_info.pointerFlags, POINTER_FLAG_UP) {
} else if util::has_flag(pointer_info.pointerFlags, POINTER_FLAG_UP) {
TouchPhase::Ended
} else if has_flag(pointer_info.pointerFlags, POINTER_FLAG_UPDATE) {
} else if util::has_flag(pointer_info.pointerFlags, POINTER_FLAG_UPDATE)
{
TouchPhase::Moved
} else {
continue;
Expand Down
6 changes: 5 additions & 1 deletion src/platform_impl/windows/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use windows::{
use crate::dpi::PhysicalSize;
use crate::icon::*;

use super::util;

impl Pixel {
fn to_bgra(&mut self) {
mem::swap(&mut self.r, &mut self.b);
Expand Down Expand Up @@ -82,10 +84,12 @@ impl WinIcon {
// width / height of 0 along with LR_DEFAULTSIZE tells windows to load the default icon size
let (width, height) = size.map(Into::into).unwrap_or((0, 0));

let mut wide_path = util::encode_wide(path.as_ref());

let handle = unsafe {
LoadImageW(
None,
path.as_ref().as_os_str(),
PWSTR(wide_path.as_mut_ptr()),
IMAGE_ICON,
width as i32,
height as i32,
Expand Down
26 changes: 19 additions & 7 deletions src/platform_impl/windows/util.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::{
io, mem,
ffi::OsStr,
io,
iter::once,
mem,
ops::BitAnd,
os::raw::c_void,
os::{raw::c_void, windows::prelude::OsStrExt},
ptr,
sync::atomic::{AtomicBool, Ordering},
};

use windows::{
runtime::{Handle, HRESULT},
Win32::{
Foundation::{BOOL, HWND, PWSTR, RECT},
Foundation::{BOOL, HWND, PSTR, PWSTR, RECT},
Graphics::Gdi::{ClientToScreen, InvalidateRgn, HMONITOR},
System::LibraryLoader::{GetProcAddress, LoadLibraryA},
UI::{
Expand All @@ -30,6 +33,10 @@ use windows::{

use crate::{dpi::PhysicalSize, window::CursorIcon};

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

pub fn has_flag<T>(bitset: T, flag: T) -> bool
where
T: Copy + PartialEq + BitAnd<T, Output = T>,
Expand Down Expand Up @@ -220,20 +227,25 @@ impl CursorIcon {
}

// Helper function to dynamically load function pointer.
// `library` and `function` must be zero-terminated.
pub(super) fn get_function_impl(library: &str, function: &str) -> Option<*const c_void> {
// Library names we will use are ASCII so we can use the A version to avoid string conversion.
let module = unsafe { LoadLibraryA(library) };
let module = unsafe { LoadLibraryA(PSTR(library.as_ptr() as *mut u8)) };
if module.is_invalid() {
return None;
}

unsafe { GetProcAddress(module, function) }.map(|function_ptr| function_ptr as _)
unsafe { GetProcAddress(module, PSTR(function.as_ptr() as *mut u8)) }
.map(|function_ptr| function_ptr as _)
}

macro_rules! get_function {
($lib:expr, $func:ident) => {
crate::platform_impl::platform::util::get_function_impl($lib, stringify!($func))
.map(|f| unsafe { std::mem::transmute::<*const _, $func>(f) })
crate::platform_impl::platform::util::get_function_impl(
concat!($lib, '\0'),
concat!(stringify!($func), '\0'),
)
.map(|f| unsafe { std::mem::transmute::<*const _, $func>(f) })
};
}

Expand Down
31 changes: 14 additions & 17 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use parking_lot::Mutex;
use raw_window_handle::{windows::WindowsHandle, RawWindowHandle};
use std::{
cell::{Cell, RefCell},
ffi::OsStr,
io, mem,
os::windows::prelude::OsStrExt,
panic, ptr,
io, mem, panic, ptr,
sync::{mpsc::channel, Arc},
};

Expand Down Expand Up @@ -154,8 +151,10 @@ impl Window {
}

pub fn set_title(&self, text: &str) {
let mut wide_text = util::encode_wide(text);

unsafe {
SetWindowTextW(self.hwnd(), text);
SetWindowTextW(self.hwnd(), PWSTR(wide_text.as_mut_ptr()));
}
}

Expand Down Expand Up @@ -751,10 +750,6 @@ where
T: 'static,
F: Fn(&mut Window) -> WindowData<T>,
{
// registering the window class
let mut class_name =
register_window_class::<T>(&attributes.window_icon, &pl_attribs.taskbar_icon);

let mut window_flags = WindowFlags::empty();
window_flags.set(WindowFlags::DECORATIONS, attributes.decorations);
window_flags.set(WindowFlags::ALWAYS_ON_TOP, attributes.always_on_top);
Expand Down Expand Up @@ -800,11 +795,16 @@ where
window: None,
};

let mut wide_class_name =
register_window_class::<T>(&attributes.window_icon, &pl_attribs.taskbar_icon);

let mut wide_title = util::encode_wide(&attributes.title);

let (style, ex_style) = window_flags.to_window_styles();
let handle = CreateWindowExW(
ex_style,
PWSTR(class_name.as_mut_ptr()),
attributes.title.as_str(),
PWSTR(wide_class_name.as_mut_ptr()),
PWSTR(wide_title.as_mut_ptr()),
style,
CW_USEDEFAULT,
CW_USEDEFAULT,
Expand Down Expand Up @@ -918,10 +918,7 @@ unsafe fn register_window_class<T: 'static>(
window_icon: &Option<Icon>,
taskbar_icon: &Option<Icon>,
) -> Vec<u16> {
let mut class_name: Vec<_> = OsStr::new("Window Class")
.encode_wide()
.chain(Some(0).into_iter())
.collect();
let mut wide_class_name = util::encode_wide("Window Class");

let h_icon = taskbar_icon
.as_ref()
Expand All @@ -943,7 +940,7 @@ unsafe fn register_window_class<T: 'static>(
hCursor: HCURSOR(0), // must be null in order for cursor state to work properly
hbrBackground: HBRUSH(0),
lpszMenuName: PWSTR::default(),
lpszClassName: PWSTR(class_name.as_mut_ptr()),
lpszClassName: PWSTR(wide_class_name.as_mut_ptr()),
hIconSm: h_icon_small,
};

Expand All @@ -953,7 +950,7 @@ unsafe fn register_window_class<T: 'static>(
// call to fail.
RegisterClassExW(&class);

class_name
wide_class_name
}

struct ComInitialized(*mut ());
Expand Down

0 comments on commit 6629e52

Please sign in to comment.