On Windows, remove internal WindowWrapper (#3294)

HWND in windows-sys doesn't require a newtype wrapper for Send/Sync.
This commit is contained in:
Markus Siglreithmaier 2023-12-23 17:06:43 +01:00 committed by GitHub
parent a8f49dc8ef
commit 745cfaab2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -84,7 +84,7 @@ use crate::{
/// The Win32 implementation of the main `Window` object.
pub(crate) struct Window {
/// Main handle for the window.
window: WindowWrapper,
window: HWND,
/// The current window state.
window_state: Arc<Mutex<WindowState>>,
@ -128,11 +128,11 @@ impl Window {
}
pub fn set_transparent(&self, transparent: bool) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::TRANSPARENT, transparent)
});
});
@ -142,11 +142,11 @@ impl Window {
#[inline]
pub fn set_visible(&self, visible: bool) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::VISIBLE, visible)
});
});
@ -154,7 +154,7 @@ impl Window {
#[inline]
pub fn is_visible(&self) -> Option<bool> {
Some(unsafe { IsWindowVisible(self.window.0) == 1 })
Some(unsafe { IsWindowVisible(self.window) == 1 })
}
#[inline]
@ -190,10 +190,10 @@ impl Window {
let (x, y): (i32, i32) = position.to_physical::<i32>(self.scale_factor()).into();
let window_state = Arc::clone(&self.window_state);
let window = self.window.clone();
let window = self.window;
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::MAXIMIZED, false)
});
});
@ -247,10 +247,10 @@ impl Window {
if physical_size != self.inner_size() {
let window_state = Arc::clone(&self.window_state);
let window = self.window.clone();
let window = self.window;
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::MAXIMIZED, false)
});
});
@ -285,12 +285,12 @@ impl Window {
#[inline]
pub fn set_resizable(&self, resizable: bool) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::RESIZABLE, resizable)
});
});
@ -304,12 +304,12 @@ impl Window {
#[inline]
pub fn set_enabled_buttons(&self, buttons: WindowButtons) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(
WindowFlags::MINIMIZABLE,
buttons.contains(WindowButtons::MINIMIZE),
@ -343,14 +343,14 @@ impl Window {
/// Returns the `hwnd` of this window.
#[inline]
pub fn hwnd(&self) -> HWND {
self.window.0
self.window
}
#[cfg(feature = "rwh_04")]
#[inline]
pub fn raw_window_handle_rwh_04(&self) -> rwh_04::RawWindowHandle {
let mut window_handle = rwh_04::Win32Handle::empty();
window_handle.hwnd = self.window.0 as *mut _;
window_handle.hwnd = self.window as *mut _;
let hinstance = unsafe { super::get_window_long(self.hwnd(), GWLP_HINSTANCE) };
window_handle.hinstance = hinstance as *mut _;
rwh_04::RawWindowHandle::Win32(window_handle)
@ -360,7 +360,7 @@ impl Window {
#[inline]
pub fn raw_window_handle_rwh_05(&self) -> rwh_05::RawWindowHandle {
let mut window_handle = rwh_05::Win32WindowHandle::empty();
window_handle.hwnd = self.window.0 as *mut _;
window_handle.hwnd = self.window as *mut _;
let hinstance = unsafe { super::get_window_long(self.hwnd(), GWLP_HINSTANCE) };
window_handle.hinstance = hinstance as *mut _;
rwh_05::RawWindowHandle::Win32(window_handle)
@ -377,8 +377,7 @@ impl Window {
pub fn raw_window_handle_rwh_06(&self) -> Result<rwh_06::RawWindowHandle, rwh_06::HandleError> {
let mut window_handle = rwh_06::Win32WindowHandle::new(unsafe {
// SAFETY: Handle will never be zero.
let window = self.window.0;
std::num::NonZeroIsize::new_unchecked(window)
std::num::NonZeroIsize::new_unchecked(self.window)
});
let hinstance = unsafe { super::get_window_long(self.hwnd(), GWLP_HINSTANCE) };
window_handle.hinstance = std::num::NonZeroIsize::new(hinstance);
@ -429,7 +428,7 @@ impl Window {
}
};
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
let (tx, rx) = channel();
@ -439,7 +438,7 @@ impl Window {
.lock()
.unwrap()
.mouse
.set_cursor_flags(window.0, |f| f.set(CursorFlags::GRABBED, confine))
.set_cursor_flags(window, |f| f.set(CursorFlags::GRABBED, confine))
.map_err(|e| ExternalError::Os(os_error!(e)));
let _ = tx.send(result);
});
@ -448,7 +447,7 @@ impl Window {
#[inline]
pub fn set_cursor_visible(&self, visible: bool) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
let (tx, rx) = channel();
@ -458,7 +457,7 @@ impl Window {
.lock()
.unwrap()
.mouse
.set_cursor_flags(window.0, |f| f.set(CursorFlags::HIDDEN, !visible))
.set_cursor_flags(window, |f| f.set(CursorFlags::HIDDEN, !visible))
.map_err(|e| e.to_string());
let _ = tx.send(result);
});
@ -488,7 +487,7 @@ impl Window {
}
unsafe fn handle_os_dragging(&self, wparam: WPARAM) {
let window = self.window.clone();
let window = self.window;
let window_state = self.window_state.clone();
self.thread_executor.execute_in_thread(move || {
@ -516,7 +515,7 @@ impl Window {
unsafe {
PostMessageW(
window.0,
window,
WM_NCLBUTTONDOWN,
wparam,
&points as *const _ as LPARAM,
@ -631,10 +630,10 @@ impl Window {
#[inline]
pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::IGNORE_CURSOR_EVENT, !hittest)
});
});
@ -649,7 +648,7 @@ impl Window {
#[inline]
pub fn set_minimized(&self, minimized: bool) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
let is_minimized = util::is_minimized(self.hwnd());
@ -659,7 +658,7 @@ impl Window {
WindowState::set_window_flags_in_place(&mut window_state.lock().unwrap(), |f| {
f.set(WindowFlags::MINIMIZED, is_minimized)
});
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::MINIMIZED, minimized)
});
});
@ -672,12 +671,12 @@ impl Window {
#[inline]
pub fn set_maximized(&self, maximized: bool) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::MAXIMIZED, maximized)
});
});
@ -697,7 +696,7 @@ impl Window {
#[inline]
pub fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
let mut window_state_lock = window_state.lock().unwrap();
@ -708,7 +707,7 @@ impl Window {
_ if old_fullscreen == fullscreen => return,
// Return if saved Borderless(monitor) is the same as current monitor when requested fullscreen is Borderless(None)
(Some(Fullscreen::Borderless(Some(monitor))), Some(Fullscreen::Borderless(None)))
if *monitor == monitor::current_monitor(window.0) =>
if *monitor == monitor::current_monitor(window) =>
{
return
}
@ -777,7 +776,7 @@ impl Window {
}
// Update window style
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(
WindowFlags::MARKER_EXCLUSIVE_FULLSCREEN,
matches!(fullscreen, Some(Fullscreen::Exclusive(_))),
@ -793,7 +792,7 @@ impl Window {
// this needs to be called before the below fullscreen SetWindowPos as this itself
// will generate WM_SIZE messages of the old window size that can race with what we set below
unsafe {
taskbar_mark_fullscreen(window.0, fullscreen.is_some());
taskbar_mark_fullscreen(window, fullscreen.is_some());
}
// Update window bounds
@ -802,7 +801,7 @@ impl Window {
// Save window bounds before entering fullscreen
let placement = unsafe {
let mut placement = mem::zeroed();
GetWindowPlacement(window.0, &mut placement);
GetWindowPlacement(window, &mut placement);
placement
};
@ -811,7 +810,7 @@ impl Window {
let monitor = match &fullscreen {
Fullscreen::Exclusive(video_mode) => video_mode.monitor(),
Fullscreen::Borderless(Some(monitor)) => monitor.clone(),
Fullscreen::Borderless(None) => monitor::current_monitor(window.0),
Fullscreen::Borderless(None) => monitor::current_monitor(window),
};
let position: (i32, i32) = monitor.position().into();
@ -819,7 +818,7 @@ impl Window {
unsafe {
SetWindowPos(
window.0,
window,
0,
position.0,
position.1,
@ -827,7 +826,7 @@ impl Window {
size.1 as i32,
SWP_ASYNCWINDOWPOS | SWP_NOZORDER,
);
InvalidateRgn(window.0, 0, false.into());
InvalidateRgn(window, 0, false.into());
}
}
None => {
@ -835,8 +834,8 @@ impl Window {
if let Some(SavedWindow { placement }) = window_state_lock.saved_window.take() {
drop(window_state_lock);
unsafe {
SetWindowPlacement(window.0, &placement);
InvalidateRgn(window.0, 0, false.into());
SetWindowPlacement(window, &placement);
InvalidateRgn(window, 0, false.into());
}
}
}
@ -846,12 +845,12 @@ impl Window {
#[inline]
pub fn set_decorations(&self, decorations: bool) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::MARKER_DECORATIONS, decorations)
});
});
@ -867,12 +866,12 @@ impl Window {
#[inline]
pub fn set_window_level(&self, level: WindowLevel) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(
WindowFlags::ALWAYS_ON_TOP,
level == WindowLevel::AlwaysOnTop,
@ -921,21 +920,21 @@ impl Window {
#[inline]
pub fn set_ime_cursor_area(&self, spot: Position, size: Size) {
let window = self.window.clone();
let window = self.window;
let state = self.window_state.clone();
self.thread_executor.execute_in_thread(move || unsafe {
let scale_factor = state.lock().unwrap().scale_factor;
ImeContext::current(window.0).set_ime_cursor_area(spot, size, scale_factor);
ImeContext::current(window).set_ime_cursor_area(spot, size, scale_factor);
});
}
#[inline]
pub fn set_ime_allowed(&self, allowed: bool) {
let window = self.window.clone();
let window = self.window;
let state = self.window_state.clone();
self.thread_executor.execute_in_thread(move || unsafe {
state.lock().unwrap().ime_allowed = allowed;
ImeContext::set_ime_allowed(window.0, allowed);
ImeContext::set_ime_allowed(window, allowed);
})
}
@ -944,14 +943,13 @@ impl Window {
#[inline]
pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) {
let window = self.window.clone();
let window = self.window;
let active_window_handle = unsafe { GetActiveWindow() };
if window.0 == active_window_handle {
if window == active_window_handle {
return;
}
self.thread_executor.execute_in_thread(move || unsafe {
let _ = &window;
let (flags, count) = request_type
.map(|ty| match ty {
UserAttentionType::Critical => (FLASHW_ALL | FLASHW_TIMERNOFG, u32::MAX),
@ -961,7 +959,7 @@ impl Window {
let flash_info = FLASHWINFO {
cbSize: mem::size_of::<FLASHWINFO>() as u32,
hwnd: window.0,
hwnd: window,
dwFlags: flags,
uCount: count,
dwTimeout: 0,
@ -972,7 +970,7 @@ impl Window {
#[inline]
pub fn set_theme(&self, theme: Option<Theme>) {
try_theme(self.window.0, theme);
try_theme(self.window, theme);
}
#[inline]
@ -987,9 +985,9 @@ impl Window {
}
pub fn title(&self) -> String {
let len = unsafe { GetWindowTextLengthW(self.window.0) } + 1;
let len = unsafe { GetWindowTextLengthW(self.window) } + 1;
let mut buf = vec![0; len as usize];
unsafe { GetWindowTextW(self.window.0, buf.as_mut_ptr(), len) };
unsafe { GetWindowTextW(self.window, buf.as_mut_ptr(), len) };
util::decode_wide(&buf).to_string_lossy().to_string()
}
@ -1001,12 +999,12 @@ impl Window {
#[inline]
pub fn set_undecorated_shadow(&self, shadow: bool) {
let window = self.window.clone();
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| {
f.set(WindowFlags::MARKER_UNDECORATED_SHADOW, shadow)
});
});
@ -1014,15 +1012,14 @@ impl Window {
#[inline]
pub fn focus_window(&self) {
let window = self.window.clone();
let window_flags = self.window_state_lock().window_flags();
let is_visible = window_flags.contains(WindowFlags::VISIBLE);
let is_minimized = util::is_minimized(self.hwnd());
let is_foreground = window.0 == unsafe { GetForegroundWindow() };
let is_foreground = self.window == unsafe { GetForegroundWindow() };
if is_visible && !is_minimized && !is_foreground {
unsafe { force_window_active(window.0) };
unsafe { force_window_active(self.window) };
}
}
@ -1072,18 +1069,6 @@ impl Drop for Window {
}
}
/// A simple non-owning wrapper around a window.
#[doc(hidden)]
#[derive(Clone)]
pub struct WindowWrapper(HWND);
// Send and Sync are not implemented for HWND and HDC, we have to wrap it and implement them manually.
// For more info see:
// https://github.com/retep998/winapi-rs/issues/360
// https://github.com/retep998/winapi-rs/issues/396
unsafe impl Sync for WindowWrapper {}
unsafe impl Send for WindowWrapper {}
pub(super) struct InitData<'a, T: 'static> {
// inputs
pub event_loop: &'a EventLoopWindowTarget<T>,
@ -1131,7 +1116,7 @@ impl<'a, T: 'static> InitData<'a, T> {
unsafe { ImeContext::set_ime_allowed(window, false) };
Window {
window: WindowWrapper(window),
window,
window_state,
thread_executor: self.event_loop.create_thread_executor(),
}
@ -1154,7 +1139,7 @@ impl<'a, T: 'static> InitData<'a, T> {
let file_drop_runner = self.event_loop.runner_shared.clone();
let file_drop_handler = FileDropHandler::new(
win.window.0,
win.window,
Box::new(move |event| {
if let Ok(e) = event.map_nonuser_event() {
file_drop_runner.send_event(e)
@ -1166,7 +1151,7 @@ impl<'a, T: 'static> InitData<'a, T> {
unsafe { &mut (*file_drop_handler.data).interface as *mut _ as *mut c_void };
assert_eq!(
unsafe { RegisterDragDrop(win.window.0, handler_interface_ptr) },
unsafe { RegisterDragDrop(win.window, handler_interface_ptr) },
S_OK
);
Some(file_drop_handler)
@ -1243,7 +1228,7 @@ impl<'a, T: 'static> InitData<'a, T> {
if attributes.fullscreen.0.is_some() {
win.set_fullscreen(attributes.fullscreen.0.map(Into::into));
unsafe { force_window_active(win.window.0) };
unsafe { force_window_active(win.window) };
} else {
let size = attributes
.inner_size