From 8729119536970f7c2885c17a1fa48eb651b85484 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 31 Aug 2022 18:32:19 +0200 Subject: [PATCH] Remove parking_lot dependency (#2423) --- CHANGELOG.md | 1 + Cargo.toml | 6 +- src/platform/unix.rs | 2 +- src/platform_impl/linux/mod.rs | 17 ++-- .../linux/x11/event_processor.rs | 63 ++++++------ .../linux/x11/ime/input_method.rs | 3 +- src/platform_impl/linux/x11/monitor.rs | 6 +- src/platform_impl/linux/x11/util/atom.rs | 4 +- src/platform_impl/linux/x11/util/cursor.rs | 1 + src/platform_impl/linux/x11/util/wm.rs | 11 ++- src/platform_impl/linux/x11/window.rs | 75 +++++++------- src/platform_impl/linux/x11/xdisplay.rs | 9 +- src/platform_impl/windows/event_loop.rs | 99 ++++++++++--------- .../windows/event_loop/runner.rs | 2 +- src/platform_impl/windows/window.rs | 71 +++++++------ src/platform_impl/windows/window_state.rs | 2 +- 16 files changed, 197 insertions(+), 175 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60ab4234..dd83565b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased +- Removed `parking_lot` dependency. - On Windows, added `WindowExtWindows::set_undecorated_shadow` and `WindowBuilderExtWindows::with_undecorated_shadow` to draw the drop shadow behind a borderless window. - On Windows, fixed default window features (ie snap, animations, shake, etc.) when decorations are disabled. - **Breaking:** On macOS, add support for two-finger touchpad magnification and rotation gestures with new events `WindowEvent::TouchpadMagnify` and `WindowEvent::TouchpadRotate`. diff --git a/Cargo.toml b/Cargo.toml index 9cf20795..32a5e714 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ targets = [ [features] default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"] -x11 = ["x11-dl", "mio", "percent-encoding", "parking_lot"] +x11 = ["x11-dl", "mio", "percent-encoding"] wayland = ["wayland-client", "wayland-protocols", "sctk"] wayland-dlopen = ["sctk/dlopen", "wayland-client/dlopen"] wayland-csd-adwaita = ["sctk-adwaita", "sctk-adwaita/ab_glyph"] @@ -70,9 +70,6 @@ core-foundation = "0.9" core-graphics = "0.22" dispatch = "0.2.0" -[target.'cfg(target_os = "windows")'.dependencies] -parking_lot = "0.12" - [target.'cfg(target_os = "windows")'.dependencies.windows-sys] version = "0.36" features = [ @@ -110,7 +107,6 @@ sctk-adwaita = { version = "0.5.1", default_features = false, optional = true } mio = { version = "0.8", features = ["os-ext"], optional = true } x11-dl = { version = "2.18.5", optional = true } percent-encoding = { version = "2.0", optional = true } -parking_lot = { version = "0.12.0", optional = true } libc = "0.2.64" [target.'cfg(target_arch = "wasm32")'.dependencies.web_sys] diff --git a/src/platform/unix.rs b/src/platform/unix.rs index 99e82619..9b027d55 100644 --- a/src/platform/unix.rs +++ b/src/platform/unix.rs @@ -56,7 +56,7 @@ pub type XlibErrorHook = pub fn register_xlib_error_hook(hook: XlibErrorHook) { // Append new hook. unsafe { - XLIB_ERROR_HOOKS.lock().push(hook); + XLIB_ERROR_HOOKS.lock().unwrap().push(hook); } } diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index f3c9e9c2..79ca7a32 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -14,12 +14,15 @@ use std::error::Error; use std::{collections::VecDeque, env, fmt}; #[cfg(feature = "x11")] -use std::{ffi::CStr, mem::MaybeUninit, os::raw::*, sync::Arc}; +use std::{ + ffi::CStr, + mem::MaybeUninit, + os::raw::*, + sync::{Arc, Mutex}, +}; #[cfg(feature = "x11")] use once_cell::sync::Lazy; -#[cfg(feature = "x11")] -use parking_lot::Mutex; use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; #[cfg(feature = "x11")] @@ -595,11 +598,11 @@ unsafe extern "C" fn x_error_callback( display: *mut x11::ffi::Display, event: *mut x11::ffi::XErrorEvent, ) -> c_int { - let xconn_lock = X11_BACKEND.lock(); + let xconn_lock = X11_BACKEND.lock().unwrap(); if let Ok(ref xconn) = *xconn_lock { // Call all the hooks. let mut error_handled = false; - for hook in XLIB_ERROR_HOOKS.lock().iter() { + for hook in XLIB_ERROR_HOOKS.lock().unwrap().iter() { error_handled |= hook(display as *mut _, event as *mut _); } @@ -626,7 +629,7 @@ unsafe extern "C" fn x_error_callback( error!("X11 error: {:#?}", error); } - *xconn.latest_error.lock() = Some(error); + *xconn.latest_error.lock().unwrap() = Some(error); } // Fun fact: this return value is completely ignored. 0 @@ -729,7 +732,7 @@ impl EventLoop { #[cfg(feature = "x11")] fn new_x11_any_thread() -> Result, XNotSupported> { - let xconn = match X11_BACKEND.lock().as_ref() { + let xconn = match X11_BACKEND.lock().unwrap().as_ref() { Ok(xconn) => xconn.clone(), Err(err) => return Err(err.clone()), }; diff --git a/src/platform_impl/linux/x11/event_processor.rs b/src/platform_impl/linux/x11/event_processor.rs index 54cdc59b..d7ae5ff8 100644 --- a/src/platform_impl/linux/x11/event_processor.rs +++ b/src/platform_impl/linux/x11/event_processor.rs @@ -2,8 +2,6 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc, slice, sync::Arc}; use libc::{c_char, c_int, c_long, c_uint, c_ulong}; -use parking_lot::MutexGuard; - use super::{ events, ffi, get_xtarget, mkdid, mkwid, monitor, util, Device, DeviceId, DeviceInfo, Dnd, DndState, GenericEventCookie, ImeReceiver, ScrollOrientation, UnownedWindow, WindowId, @@ -351,9 +349,9 @@ impl EventProcessor { let new_inner_size = (xev.width as u32, xev.height as u32); let new_inner_position = (xev.x as i32, xev.y as i32); - let mut shared_state_lock = window.shared_state.lock(); - let (mut resized, moved) = { + let mut shared_state_lock = window.shared_state_lock(); + let resized = util::maybe_change(&mut shared_state_lock.size, new_inner_size); let moved = if is_synthetic { @@ -380,7 +378,13 @@ impl EventProcessor { (resized, moved) }; - let new_outer_position = if moved || shared_state_lock.position.is_none() { + let position = window.shared_state_lock().position; + + let new_outer_position = if let (Some(position), false) = (position, moved) { + position + } else { + let mut shared_state_lock = window.shared_state_lock(); + // We need to convert client area position to window position. let frame_extents = shared_state_lock .frame_extents @@ -395,21 +399,21 @@ impl EventProcessor { let outer = frame_extents .inner_pos_to_outer(new_inner_position.0, new_inner_position.1); shared_state_lock.position = Some(outer); + + // Unlock shared state to prevent deadlock in callback below + drop(shared_state_lock); + if moved { - // Temporarily unlock shared state to prevent deadlock - MutexGuard::unlocked(&mut shared_state_lock, || { - callback(Event::WindowEvent { - window_id, - event: WindowEvent::Moved(outer.into()), - }); + callback(Event::WindowEvent { + window_id, + event: WindowEvent::Moved(outer.into()), }); } outer - } else { - shared_state_lock.position.unwrap() }; if is_synthetic { + let mut shared_state_lock = window.shared_state_lock(); // If we don't use the existing adjusted value when available, then the user can screw up the // resizing by dragging across monitors *without* dropping the window. let (width, height) = shared_state_lock @@ -441,15 +445,15 @@ impl EventProcessor { let old_inner_size = PhysicalSize::new(width, height); let mut new_inner_size = PhysicalSize::new(new_width, new_height); - // Temporarily unlock shared state to prevent deadlock - MutexGuard::unlocked(&mut shared_state_lock, || { - callback(Event::WindowEvent { - window_id, - event: WindowEvent::ScaleFactorChanged { - scale_factor: new_scale_factor, - new_inner_size: &mut new_inner_size, - }, - }); + // Unlock shared state to prevent deadlock in callback below + drop(shared_state_lock); + + callback(Event::WindowEvent { + window_id, + event: WindowEvent::ScaleFactorChanged { + scale_factor: new_scale_factor, + new_inner_size: &mut new_inner_size, + }, }); if new_inner_size != old_inner_size { @@ -457,7 +461,8 @@ impl EventProcessor { new_inner_size.width, new_inner_size.height, ); - shared_state_lock.dpi_adjusted = Some(new_inner_size.into()); + window.shared_state_lock().dpi_adjusted = + Some(new_inner_size.into()); // if the DPI factor changed, force a resize event to ensure the logical // size is computed with the right DPI factor resized = true; @@ -465,6 +470,8 @@ impl EventProcessor { } } + let mut shared_state_lock = window.shared_state_lock(); + // This is a hack to ensure that the DPI adjusted resize is actually applied on all WMs. KWin // doesn't need this, but Xfwm does. The hack should not be run on other WMs, since tiling // WMs constrain the window size, making the resize fail. This would cause an endless stream of @@ -478,10 +485,10 @@ impl EventProcessor { } } - if resized { - // Drop the shared state lock to prevent deadlock - drop(shared_state_lock); + // Unlock shared state to prevent deadlock in callback below + drop(shared_state_lock); + if resized { callback(Event::WindowEvent { window_id, event: WindowEvent::Resized(new_inner_size.into()), @@ -747,7 +754,7 @@ impl EventProcessor { update_modifiers!(modifiers, None); let cursor_moved = self.with_window(xev.event, |window| { - let mut shared_state_lock = window.shared_state.lock(); + let mut shared_state_lock = window.shared_state_lock(); util::maybe_change(&mut shared_state_lock.cursor_pos, new_cursor_pos) }); if cursor_moved == Some(true) { @@ -1215,7 +1222,7 @@ impl EventProcessor { new_monitor.scale_factor, width, height, - &*window.shared_state.lock(), + &*window.shared_state_lock(), ); let window_id = crate::window::WindowId(*window_id); diff --git a/src/platform_impl/linux/x11/ime/input_method.rs b/src/platform_impl/linux/x11/ime/input_method.rs index a747f972..fecea81a 100644 --- a/src/platform_impl/linux/x11/ime/input_method.rs +++ b/src/platform_impl/linux/x11/ime/input_method.rs @@ -4,11 +4,10 @@ use std::{ fmt, os::raw::c_char, ptr, - sync::Arc, + sync::{Arc, Mutex}, }; use once_cell::sync::Lazy; -use parking_lot::Mutex; use super::{ffi, util, XConnection, XError}; diff --git a/src/platform_impl/linux/x11/monitor.rs b/src/platform_impl/linux/x11/monitor.rs index 8a696e8a..9ee7d84b 100644 --- a/src/platform_impl/linux/x11/monitor.rs +++ b/src/platform_impl/linux/x11/monitor.rs @@ -1,8 +1,8 @@ use std::os::raw::*; use std::slice; +use std::sync::Mutex; use once_cell::sync::Lazy; -use parking_lot::Mutex; use super::{ ffi::{ @@ -24,7 +24,7 @@ static MONITORS: Lazy>>> = Lazy::new(Mutex::defa pub fn invalidate_cached_monitor_list() -> Option> { // We update this lazily. - (*MONITORS.lock()).take() + (*MONITORS.lock().unwrap()).take() } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -293,7 +293,7 @@ impl XConnection { } pub fn available_monitors(&self) -> Vec { - let mut monitors_lock = MONITORS.lock(); + let mut monitors_lock = MONITORS.lock().unwrap(); (*monitors_lock) .as_ref() .cloned() diff --git a/src/platform_impl/linux/x11/util/atom.rs b/src/platform_impl/linux/x11/util/atom.rs index 5bfa386a..55ebeb60 100644 --- a/src/platform_impl/linux/x11/util/atom.rs +++ b/src/platform_impl/linux/x11/util/atom.rs @@ -3,10 +3,10 @@ use std::{ ffi::{CStr, CString}, fmt::Debug, os::raw::*, + sync::Mutex, }; use once_cell::sync::Lazy; -use parking_lot::Mutex; use super::*; @@ -17,7 +17,7 @@ static ATOM_CACHE: Lazy> = Lazy::new(|| Mutex::new(HashMap::wit impl XConnection { pub fn get_atom + Debug>(&self, name: T) -> ffi::Atom { let name = name.as_ref(); - let mut atom_cache_lock = ATOM_CACHE.lock(); + let mut atom_cache_lock = ATOM_CACHE.lock().unwrap(); let cached_atom = (*atom_cache_lock).get(name).cloned(); if let Some(atom) = cached_atom { atom diff --git a/src/platform_impl/linux/x11/util/cursor.rs b/src/platform_impl/linux/x11/util/cursor.rs index c6551a94..99b25635 100644 --- a/src/platform_impl/linux/x11/util/cursor.rs +++ b/src/platform_impl/linux/x11/util/cursor.rs @@ -7,6 +7,7 @@ impl XConnection { let cursor = *self .cursor_cache .lock() + .unwrap() .entry(cursor) .or_insert_with(|| self.get_cursor(cursor)); diff --git a/src/platform_impl/linux/x11/util/wm.rs b/src/platform_impl/linux/x11/util/wm.rs index 89e9a0b8..368a059c 100644 --- a/src/platform_impl/linux/x11/util/wm.rs +++ b/src/platform_impl/linux/x11/util/wm.rs @@ -1,5 +1,6 @@ +use std::sync::Mutex; + use once_cell::sync::Lazy; -use parking_lot::Mutex; use super::*; @@ -9,11 +10,11 @@ static SUPPORTED_HINTS: Lazy>> = static WM_NAME: Lazy>> = Lazy::new(|| Mutex::new(None)); pub fn hint_is_supported(hint: ffi::Atom) -> bool { - (*SUPPORTED_HINTS.lock()).contains(&hint) + (*SUPPORTED_HINTS.lock().unwrap()).contains(&hint) } pub fn wm_name_is_one_of(names: &[&str]) -> bool { - if let Some(ref name) = *WM_NAME.lock() { + if let Some(ref name) = *WM_NAME.lock().unwrap() { names.contains(&name.as_str()) } else { false @@ -22,8 +23,8 @@ pub fn wm_name_is_one_of(names: &[&str]) -> bool { impl XConnection { pub fn update_cached_wm_info(&self, root: ffi::Window) { - *SUPPORTED_HINTS.lock() = self.get_supported_hints(root); - *WM_NAME.lock() = self.get_wm_name(root); + *SUPPORTED_HINTS.lock().unwrap() = self.get_supported_hints(root); + *WM_NAME.lock().unwrap() = self.get_wm_name(root); } fn get_supported_hints(&self, root: ffi::Window) -> Vec { diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index ab7cddfa..5342f8fb 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -5,11 +5,10 @@ use std::{ os::raw::*, path::Path, ptr, slice, - sync::Arc, + sync::{Arc, Mutex, MutexGuard}, }; use libc; -use parking_lot::Mutex; use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XlibDisplayHandle, XlibWindowHandle}; use x11_dl::xlib::TrueColor; @@ -107,6 +106,7 @@ pub struct UnownedWindow { screen_id: i32, // never changes cursor: Mutex, cursor_grabbed_mode: Mutex, + #[allow(clippy::mutex_atomic)] cursor_visible: Mutex, ime_sender: Mutex, pub shared_state: Mutex, @@ -270,6 +270,7 @@ impl UnownedWindow { ) }; + #[allow(clippy::mutex_atomic)] let mut window = UnownedWindow { xconn: Arc::clone(xconn), xwindow, @@ -374,7 +375,7 @@ impl UnownedWindow { } } - let mut shared_state = window.shared_state.get_mut(); + let mut shared_state = window.shared_state.get_mut().unwrap(); shared_state.min_inner_size = min_inner_size.map(Into::into); shared_state.max_inner_size = max_inner_size.map(Into::into); shared_state.resize_increments = pl_attribs.resize_increments; @@ -480,7 +481,7 @@ impl UnownedWindow { } if let Some(PhysicalPosition { x, y }) = position { - let shared_state = window.shared_state.get_mut(); + let shared_state = window.shared_state.get_mut().unwrap(); shared_state.restore_position = Some((x, y)); } @@ -499,6 +500,10 @@ impl UnownedWindow { .map_err(|x_err| os_error!(OsError::XError(x_err))) } + pub(super) fn shared_state_lock(&self) -> MutexGuard<'_, SharedState> { + self.shared_state.lock().unwrap() + } + fn set_pid(&self) -> Option> { let pid_atom = unsafe { self.xconn.get_atom_unchecked(b"_NET_WM_PID\0") }; let client_machine_atom = unsafe { self.xconn.get_atom_unchecked(b"WM_CLIENT_MACHINE\0") }; @@ -611,7 +616,7 @@ impl UnownedWindow { } fn set_fullscreen_inner(&self, fullscreen: Option) -> Option> { - let mut shared_state_lock = self.shared_state.lock(); + let mut shared_state_lock = self.shared_state_lock(); match shared_state_lock.visibility { // Setting fullscreen on a window that is not visible will generate an error. @@ -665,7 +670,7 @@ impl UnownedWindow { match fullscreen { None => { let flusher = self.set_fullscreen_hint(false); - let mut shared_state_lock = self.shared_state.lock(); + let mut shared_state_lock = self.shared_state_lock(); if let Some(position) = shared_state_lock.restore_position.take() { drop(shared_state_lock); self.set_position_inner(position.0, position.1).queue(); @@ -722,7 +727,7 @@ impl UnownedWindow { } let window_position = self.outer_position_physical(); - self.shared_state.lock().restore_position = Some(window_position); + self.shared_state_lock().restore_position = Some(window_position); let monitor_origin: (i32, i32) = monitor.position().into(); self.set_position_inner(monitor_origin.0, monitor_origin.1) .queue(); @@ -733,7 +738,7 @@ impl UnownedWindow { #[inline] pub fn fullscreen(&self) -> Option { - let shared_state = self.shared_state.lock(); + let shared_state = self.shared_state_lock(); shared_state .desired_fullscreen @@ -753,7 +758,7 @@ impl UnownedWindow { // Called by EventProcessor when a VisibilityNotify event is received pub(crate) fn visibility_notify(&self) { - let mut shared_state = self.shared_state.lock(); + let mut shared_state = self.shared_state_lock(); match shared_state.visibility { Visibility::No => unsafe { @@ -773,7 +778,7 @@ impl UnownedWindow { #[inline] pub fn current_monitor(&self) -> X11MonitorHandle { - self.shared_state.lock().last_monitor.clone() + self.shared_state_lock().last_monitor.clone() } pub fn available_monitors(&self) -> Vec { @@ -888,7 +893,7 @@ impl UnownedWindow { } fn set_decorations_inner(&self, decorations: bool) -> util::Flusher<'_> { - self.shared_state.lock().is_decorated = decorations; + self.shared_state_lock().is_decorated = decorations; let mut hints = self.xconn.get_motif_hints(self.xwindow); hints.set_decorations(decorations); @@ -906,7 +911,7 @@ impl UnownedWindow { #[inline] pub fn is_decorated(&self) -> bool { - self.shared_state.lock().is_decorated + self.shared_state_lock().is_decorated } fn set_maximizable_inner(&self, maximizable: bool) -> util::Flusher<'_> { @@ -965,7 +970,7 @@ impl UnownedWindow { #[inline] pub fn set_visible(&self, visible: bool) { - let mut shared_state = self.shared_state.lock(); + let mut shared_state = self.shared_state_lock(); match (visible, shared_state.visibility) { (true, Visibility::Yes) | (true, Visibility::YesWait) | (false, Visibility::No) => { @@ -995,22 +1000,22 @@ impl UnownedWindow { #[inline] pub fn is_visible(&self) -> Option { - Some(self.shared_state.lock().visibility == Visibility::Yes) + Some(self.shared_state_lock().visibility == Visibility::Yes) } fn update_cached_frame_extents(&self) { let extents = self .xconn .get_frame_extents_heuristic(self.xwindow, self.root); - (*self.shared_state.lock()).frame_extents = Some(extents); + (*self.shared_state_lock()).frame_extents = Some(extents); } pub(crate) fn invalidate_cached_frame_extents(&self) { - (*self.shared_state.lock()).frame_extents.take(); + (*self.shared_state_lock()).frame_extents.take(); } pub(crate) fn outer_position_physical(&self) -> (i32, i32) { - let extents = (*self.shared_state.lock()).frame_extents.clone(); + let extents = (*self.shared_state_lock()).frame_extents.clone(); if let Some(extents) = extents { let (x, y) = self.inner_position_physical(); extents.inner_pos_to_outer(x, y) @@ -1022,7 +1027,7 @@ impl UnownedWindow { #[inline] pub fn outer_position(&self) -> Result, NotSupportedError> { - let extents = (*self.shared_state.lock()).frame_extents.clone(); + let extents = (*self.shared_state_lock()).frame_extents.clone(); if let Some(extents) = extents { let (x, y) = self.inner_position_physical(); Ok(extents.inner_pos_to_outer(x, y).into()) @@ -1050,7 +1055,7 @@ impl UnownedWindow { // There are a few WMs that set client area position rather than window position, so // we'll translate for consistency. if util::wm_name_is_one_of(&["Enlightenment", "FVWM"]) { - let extents = (*self.shared_state.lock()).frame_extents.clone(); + let extents = (*self.shared_state_lock()).frame_extents.clone(); if let Some(extents) = extents { x += extents.frame_extents.left as i32; y += extents.frame_extents.top as i32; @@ -1093,7 +1098,7 @@ impl UnownedWindow { #[inline] pub fn outer_size(&self) -> PhysicalSize { - let extents = self.shared_state.lock().frame_extents.clone(); + let extents = self.shared_state_lock().frame_extents.clone(); if let Some(extents) = extents { let (width, height) = self.inner_size_physical(); extents.inner_size_to_outer(width, height).into() @@ -1120,7 +1125,7 @@ impl UnownedWindow { pub fn set_inner_size(&self, size: Size) { let scale_factor = self.scale_factor(); let size = size.to_physical::(scale_factor).into(); - if !self.shared_state.lock().is_resizable { + if !self.shared_state_lock().is_resizable { self.update_normal_hints(|normal_hints| { normal_hints.set_min_size(Some(size)); normal_hints.set_max_size(Some(size)); @@ -1148,7 +1153,7 @@ impl UnownedWindow { #[inline] pub fn set_min_inner_size(&self, dimensions: Option) { - self.shared_state.lock().min_inner_size = dimensions; + self.shared_state_lock().min_inner_size = dimensions; let physical_dimensions = dimensions.map(|dimensions| dimensions.to_physical::(self.scale_factor()).into()); self.set_min_inner_size_physical(physical_dimensions); @@ -1161,7 +1166,7 @@ impl UnownedWindow { #[inline] pub fn set_max_inner_size(&self, dimensions: Option) { - self.shared_state.lock().max_inner_size = dimensions; + self.shared_state_lock().max_inner_size = dimensions; let physical_dimensions = dimensions.map(|dimensions| dimensions.to_physical::(self.scale_factor()).into()); self.set_max_inner_size_physical(physical_dimensions); @@ -1206,7 +1211,7 @@ impl UnownedWindow { } let (min_size, max_size) = if resizable { - let shared_state_lock = self.shared_state.lock(); + let shared_state_lock = self.shared_state_lock(); ( shared_state_lock.min_inner_size, shared_state_lock.max_inner_size, @@ -1215,7 +1220,7 @@ impl UnownedWindow { let window_size = Some(Size::from(self.inner_size())); (window_size, window_size) }; - self.shared_state.lock().is_resizable = resizable; + self.shared_state_lock().is_resizable = resizable; self.set_maximizable_inner(resizable).queue(); @@ -1235,7 +1240,7 @@ impl UnownedWindow { #[inline] pub fn is_resizable(&self) -> bool { - self.shared_state.lock().is_resizable + self.shared_state_lock().is_resizable } #[inline] @@ -1265,15 +1270,16 @@ impl UnownedWindow { #[inline] pub fn set_cursor_icon(&self, cursor: CursorIcon) { - let old_cursor = replace(&mut *self.cursor.lock(), cursor); - if cursor != old_cursor && *self.cursor_visible.lock() { + let old_cursor = replace(&mut *self.cursor.lock().unwrap(), cursor); + #[allow(clippy::mutex_atomic)] + if cursor != old_cursor && *self.cursor_visible.lock().unwrap() { self.xconn.set_cursor_icon(self.xwindow, Some(cursor)); } } #[inline] pub fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), ExternalError> { - let mut grabbed_lock = self.cursor_grabbed_mode.lock(); + let mut grabbed_lock = self.cursor_grabbed_mode.lock().unwrap(); if mode == *grabbed_lock { return Ok(()); } @@ -1346,12 +1352,13 @@ impl UnownedWindow { #[inline] pub fn set_cursor_visible(&self, visible: bool) { - let mut visible_lock = self.cursor_visible.lock(); + #[allow(clippy::mutex_atomic)] + let mut visible_lock = self.cursor_visible.lock().unwrap(); if visible == *visible_lock { return; } let cursor = if visible { - Some(*self.cursor.lock()) + Some(*self.cursor.lock().unwrap()) } else { None }; @@ -1397,7 +1404,7 @@ impl UnownedWindow { // we can't use `set_cursor_grab(false)` here because it doesn't run `XUngrabPointer` // if the cursor isn't currently grabbed - let mut grabbed_lock = self.cursor_grabbed_mode.lock(); + let mut grabbed_lock = self.cursor_grabbed_mode.lock().unwrap(); unsafe { (self.xconn.xlib.XUngrabPointer)(self.xconn.display, ffi::CurrentTime); } @@ -1431,6 +1438,7 @@ impl UnownedWindow { let _ = self .ime_sender .lock() + .unwrap() .send(ImeRequest::Position(self.xwindow, x, y)); } @@ -1439,6 +1447,7 @@ impl UnownedWindow { let _ = self .ime_sender .lock() + .unwrap() .send(ImeRequest::Allow(self.xwindow, allowed)); } @@ -1454,7 +1463,7 @@ impl UnownedWindow { } else { false }; - let is_visible = match self.shared_state.lock().visibility { + let is_visible = match self.shared_state_lock().visibility { Visibility::Yes => true, Visibility::YesWait | Visibility::No => false, }; diff --git a/src/platform_impl/linux/x11/xdisplay.rs b/src/platform_impl/linux/x11/xdisplay.rs index 25065f04..506e9b82 100644 --- a/src/platform_impl/linux/x11/xdisplay.rs +++ b/src/platform_impl/linux/x11/xdisplay.rs @@ -1,7 +1,4 @@ -use std::{collections::HashMap, error::Error, fmt, os::raw::c_int, ptr}; - -use libc; -use parking_lot::Mutex; +use std::{collections::HashMap, error::Error, fmt, os::raw::c_int, ptr, sync::Mutex}; use crate::window::CursorIcon; @@ -74,7 +71,7 @@ impl XConnection { /// Checks whether an error has been triggered by the previous function calls. #[inline] pub fn check_errors(&self) -> Result<(), XError> { - let error = self.latest_error.lock().take(); + let error = self.latest_error.lock().unwrap().take(); if let Some(error) = error { Err(error) } else { @@ -85,7 +82,7 @@ impl XConnection { /// Ignores any previous error. #[inline] pub fn ignore_error(&self) { - *self.latest_error.lock() = None; + *self.latest_error.lock().unwrap() = None; } } diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 06f885b6..25c6cf61 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -11,14 +11,13 @@ use std::{ rc::Rc, sync::{ mpsc::{self, Receiver, Sender}, - Arc, + Arc, Mutex, MutexGuard, }, thread, time::{Duration, Instant}, }; use once_cell::sync::Lazy; -use parking_lot::Mutex; use raw_window_handle::{RawDisplayHandle, WindowsDisplayHandle}; use windows_sys::Win32::{ @@ -142,6 +141,10 @@ impl WindowData { unsafe fn send_event(&self, event: Event<'_, T>) { self.event_loop_runner.send_event(event); } + + fn window_state_lock(&self) -> MutexGuard<'_, WindowState> { + self.window_state.lock().unwrap() + } } struct ThreadMsgTargetData { @@ -718,7 +721,7 @@ unsafe fn capture_mouse(window: HWND, window_state: &mut WindowState) { /// Release mouse input, stopping windows on this thread from receiving mouse input when the cursor /// is outside the window. -unsafe fn release_mouse(mut window_state: parking_lot::MutexGuard<'_, WindowState>) { +unsafe fn release_mouse(mut window_state: MutexGuard<'_, WindowState>) { window_state.mouse.capture_count = window_state.mouse.capture_count.saturating_sub(1); if window_state.mouse.capture_count == 0 { // ReleaseCapture() causes a WM_CAPTURECHANGED where we lock the window_state. @@ -802,7 +805,7 @@ fn update_modifiers(window: HWND, userdata: &WindowData) { use crate::event::WindowEvent::ModifiersChanged; let modifiers = event::get_key_mods(); - let mut window_state = userdata.window_state.lock(); + let mut window_state = userdata.window_state_lock(); if window_state.modifiers_state != modifiers { window_state.modifiers_state = modifiers; @@ -874,7 +877,7 @@ unsafe fn lose_active_focus(window: HWND, userdata: &WindowData) { }) } - userdata.window_state.lock().modifiers_state = ModifiersState::empty(); + userdata.window_state_lock().modifiers_state = ModifiersState::empty(); userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), event: ModifiersChanged(ModifiersState::empty()), @@ -971,7 +974,7 @@ unsafe fn public_window_callback_inner( // the git blame and history would be preserved. let callback = || match msg { WM_NCCALCSIZE => { - let window_flags = userdata.window_state.lock().window_flags; + let window_flags = userdata.window_state_lock().window_flags; if wparam == 0 || window_flags.contains(WindowFlags::MARKER_DECORATIONS) { return DefWindowProcW(window, msg, wparam, lparam); } @@ -998,16 +1001,14 @@ unsafe fn public_window_callback_inner( WM_ENTERSIZEMOVE => { userdata - .window_state - .lock() + .window_state_lock() .set_window_flags_in_place(|f| f.insert(WindowFlags::MARKER_IN_SIZE_MOVE)); 0 } WM_EXITSIZEMOVE => { userdata - .window_state - .lock() + .window_state_lock() .set_window_flags_in_place(|f| f.remove(WindowFlags::MARKER_IN_SIZE_MOVE)); 0 } @@ -1064,7 +1065,7 @@ unsafe fn public_window_callback_inner( } WM_WINDOWPOSCHANGING => { - let mut window_state = userdata.window_state.lock(); + let mut window_state = userdata.window_state_lock(); if let Some(ref mut fullscreen) = window_state.fullscreen { let window_pos = &mut *(lparam as *mut WINDOWPOS); let new_rect = RECT { @@ -1173,7 +1174,7 @@ unsafe fn public_window_callback_inner( }; { - let mut w = userdata.window_state.lock(); + let mut w = userdata.window_state_lock(); // See WindowFlags::MARKER_RETAIN_STATE_ON_SIZE docs for info on why this `if` check exists. if !w .window_flags() @@ -1195,9 +1196,9 @@ unsafe fn public_window_callback_inner( let is_low_surrogate = (0xDC00..=0xDFFF).contains(&wparam); if is_high_surrogate { - userdata.window_state.lock().high_surrogate = Some(wparam as u16); + userdata.window_state_lock().high_surrogate = Some(wparam as u16); } else if is_low_surrogate { - let high_surrogate = userdata.window_state.lock().high_surrogate.take(); + let high_surrogate = userdata.window_state_lock().high_surrogate.take(); if let Some(high_surrogate) = high_surrogate { let pair = [high_surrogate, wparam as u16]; @@ -1209,7 +1210,7 @@ unsafe fn public_window_callback_inner( } } } else { - userdata.window_state.lock().high_surrogate = None; + userdata.window_state_lock().high_surrogate = None; if let Some(chr) = char::from_u32(wparam as u32) { userdata.send_event(Event::WindowEvent { @@ -1222,9 +1223,9 @@ unsafe fn public_window_callback_inner( } WM_IME_STARTCOMPOSITION => { - let ime_allowed = userdata.window_state.lock().ime_allowed; + let ime_allowed = userdata.window_state_lock().ime_allowed; if ime_allowed { - userdata.window_state.lock().ime_state = ImeState::Enabled; + userdata.window_state_lock().ime_state = ImeState::Enabled; userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), @@ -1237,7 +1238,7 @@ unsafe fn public_window_callback_inner( WM_IME_COMPOSITION => { let ime_allowed_and_composing = { - let w = userdata.window_state.lock(); + let w = userdata.window_state_lock(); w.ime_allowed && w.ime_state != ImeState::Disabled }; // Windows Hangul IME sends WM_IME_COMPOSITION after WM_IME_ENDCOMPOSITION, so @@ -1256,7 +1257,7 @@ unsafe fn public_window_callback_inner( // first, receive composing result if exist. if (lparam as u32 & GCS_RESULTSTR) != 0 { if let Some(text) = ime_context.get_composed_text() { - userdata.window_state.lock().ime_state = ImeState::Enabled; + userdata.window_state_lock().ime_state = ImeState::Enabled; userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), @@ -1268,7 +1269,7 @@ unsafe fn public_window_callback_inner( // Next, receive preedit range for next composing if exist. if (lparam as u32 & GCS_COMPSTR) != 0 { if let Some((text, first, last)) = ime_context.get_composing_text_and_cursor() { - userdata.window_state.lock().ime_state = ImeState::Preedit; + userdata.window_state_lock().ime_state = ImeState::Preedit; let cursor_range = first.map(|f| (f, last.unwrap_or(f))); userdata.send_event(Event::WindowEvent { @@ -1285,11 +1286,11 @@ unsafe fn public_window_callback_inner( WM_IME_ENDCOMPOSITION => { let ime_allowed_or_composing = { - let w = userdata.window_state.lock(); + let w = userdata.window_state_lock(); w.ime_allowed || w.ime_state != ImeState::Disabled }; if ime_allowed_or_composing { - if userdata.window_state.lock().ime_state == ImeState::Preedit { + if userdata.window_state_lock().ime_state == ImeState::Preedit { // Windows Hangul IME sends WM_IME_COMPOSITION after WM_IME_ENDCOMPOSITION, so // trying receiving composing result and commit if exists. let ime_context = ImeContext::current(window); @@ -1301,7 +1302,7 @@ unsafe fn public_window_callback_inner( } } - userdata.window_state.lock().ime_state = ImeState::Disabled; + userdata.window_state_lock().ime_state = ImeState::Disabled; userdata.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), @@ -1322,17 +1323,17 @@ unsafe fn public_window_callback_inner( // this is necessary for us to maintain minimize/restore state WM_SYSCOMMAND => { if wparam == SC_RESTORE as usize { - let mut w = userdata.window_state.lock(); + let mut w = userdata.window_state_lock(); w.set_window_flags_in_place(|f| f.set(WindowFlags::MINIMIZED, false)); } if wparam == SC_MINIMIZE as usize { - let mut w = userdata.window_state.lock(); + let mut w = userdata.window_state_lock(); w.set_window_flags_in_place(|f| f.set(WindowFlags::MINIMIZED, true)); } // Send `WindowEvent::Minimized` here if we decide to implement one if wparam == SC_SCREENSAVE as usize { - let window_state = userdata.window_state.lock(); + let window_state = userdata.window_state_lock(); if window_state.fullscreen.is_some() { return 0; } @@ -1344,7 +1345,7 @@ unsafe fn public_window_callback_inner( WM_MOUSEMOVE => { use crate::event::WindowEvent::{CursorEntered, CursorMoved}; let mouse_was_outside_window = { - let mut w = userdata.window_state.lock(); + let mut w = userdata.window_state_lock(); let was_outside_window = !w.mouse.cursor_flags().contains(CursorFlags::IN_WINDOW); w.mouse @@ -1378,7 +1379,7 @@ unsafe fn public_window_callback_inner( // handle spurious WM_MOUSEMOVE messages // see https://devblogs.microsoft.com/oldnewthing/20031001-00/?p=42343 // and http://debugandconquer.blogspot.com/2015/08/the-cause-of-spurious-mouse-move.html - let mut w = userdata.window_state.lock(); + let mut w = userdata.window_state_lock(); cursor_moved = w.mouse.last_position != Some(position); w.mouse.last_position = Some(position); } @@ -1401,7 +1402,7 @@ unsafe fn public_window_callback_inner( WM_MOUSELEAVE => { use crate::event::WindowEvent::CursorLeft; { - let mut w = userdata.window_state.lock(); + let mut w = userdata.window_state_lock(); w.mouse .set_cursor_flags(window, |f| f.set(CursorFlags::IN_WINDOW, false)) .ok(); @@ -1522,7 +1523,7 @@ unsafe fn public_window_callback_inner( WM_LBUTTONDOWN => { use crate::event::{ElementState::Pressed, MouseButton::Left, WindowEvent::MouseInput}; - capture_mouse(window, &mut *userdata.window_state.lock()); + capture_mouse(window, &mut *userdata.window_state_lock()); update_modifiers(window, userdata); @@ -1543,7 +1544,7 @@ unsafe fn public_window_callback_inner( ElementState::Released, MouseButton::Left, WindowEvent::MouseInput, }; - release_mouse(userdata.window_state.lock()); + release_mouse(userdata.window_state_lock()); update_modifiers(window, userdata); @@ -1564,7 +1565,7 @@ unsafe fn public_window_callback_inner( ElementState::Pressed, MouseButton::Right, WindowEvent::MouseInput, }; - capture_mouse(window, &mut *userdata.window_state.lock()); + capture_mouse(window, &mut *userdata.window_state_lock()); update_modifiers(window, userdata); @@ -1585,7 +1586,7 @@ unsafe fn public_window_callback_inner( ElementState::Released, MouseButton::Right, WindowEvent::MouseInput, }; - release_mouse(userdata.window_state.lock()); + release_mouse(userdata.window_state_lock()); update_modifiers(window, userdata); @@ -1606,7 +1607,7 @@ unsafe fn public_window_callback_inner( ElementState::Pressed, MouseButton::Middle, WindowEvent::MouseInput, }; - capture_mouse(window, &mut *userdata.window_state.lock()); + capture_mouse(window, &mut *userdata.window_state_lock()); update_modifiers(window, userdata); @@ -1627,7 +1628,7 @@ unsafe fn public_window_callback_inner( ElementState::Released, MouseButton::Middle, WindowEvent::MouseInput, }; - release_mouse(userdata.window_state.lock()); + release_mouse(userdata.window_state_lock()); update_modifiers(window, userdata); @@ -1649,7 +1650,7 @@ unsafe fn public_window_callback_inner( }; let xbutton = super::get_xbutton_wparam(wparam as u32); - capture_mouse(window, &mut *userdata.window_state.lock()); + capture_mouse(window, &mut *userdata.window_state_lock()); update_modifiers(window, userdata); @@ -1671,7 +1672,7 @@ unsafe fn public_window_callback_inner( }; let xbutton = super::get_xbutton_wparam(wparam as u32); - release_mouse(userdata.window_state.lock()); + release_mouse(userdata.window_state_lock()); update_modifiers(window, userdata); @@ -1693,7 +1694,7 @@ unsafe fn public_window_callback_inner( // can happen if `SetCapture` is called on our window when it already has the mouse // capture. if lparam != window { - userdata.window_state.lock().mouse.capture_count = 0; + userdata.window_state_lock().mouse.capture_count = 0; } 0 } @@ -1889,7 +1890,7 @@ unsafe fn public_window_callback_inner( WM_NCACTIVATE => { let is_active = wparam == 1; - let active_focus_changed = userdata.window_state.lock().set_active(is_active); + let active_focus_changed = userdata.window_state_lock().set_active(is_active); if active_focus_changed { if is_active { gain_active_focus(window, userdata); @@ -1901,7 +1902,7 @@ unsafe fn public_window_callback_inner( } WM_SETFOCUS => { - let active_focus_changed = userdata.window_state.lock().set_focused(true); + let active_focus_changed = userdata.window_state_lock().set_focused(true); if active_focus_changed { gain_active_focus(window, userdata); } @@ -1909,7 +1910,7 @@ unsafe fn public_window_callback_inner( } WM_KILLFOCUS => { - let active_focus_changed = userdata.window_state.lock().set_focused(false); + let active_focus_changed = userdata.window_state_lock().set_focused(false); if active_focus_changed { lose_active_focus(window, userdata); } @@ -1918,7 +1919,7 @@ unsafe fn public_window_callback_inner( WM_SETCURSOR => { let set_cursor_to = { - let window_state = userdata.window_state.lock(); + let window_state = userdata.window_state_lock(); // The return value for the preceding `WM_NCHITTEST` message is conveniently // provided through the low-order word of lParam. We use that here since // `WM_MOUSEMOVE` seems to come after `WM_SETCURSOR` for a given cursor movement. @@ -1948,7 +1949,7 @@ unsafe fn public_window_callback_inner( WM_GETMINMAXINFO => { let mmi = lparam as *mut MINMAXINFO; - let window_state = userdata.window_state.lock(); + let window_state = userdata.window_state_lock(); let window_flags = window_state.window_flags; if window_state.min_size.is_some() || window_state.max_size.is_some() { @@ -1989,7 +1990,7 @@ unsafe fn public_window_callback_inner( let old_scale_factor: f64; let (allow_resize, window_flags) = { - let mut window_state = userdata.window_state.lock(); + let mut window_state = userdata.window_state_lock(); old_scale_factor = window_state.scale_factor; window_state.scale_factor = new_scale_factor; @@ -2054,7 +2055,7 @@ unsafe fn public_window_callback_inner( let dragging_window: bool; { - let window_state = userdata.window_state.lock(); + let window_state = userdata.window_state_lock(); dragging_window = window_state .window_flags() .contains(WindowFlags::MARKER_IN_SIZE_MOVE); @@ -2183,11 +2184,11 @@ unsafe fn public_window_callback_inner( WM_SETTINGCHANGE => { use crate::event::WindowEvent::ThemeChanged; - let preferred_theme = userdata.window_state.lock().preferred_theme; + let preferred_theme = userdata.window_state_lock().preferred_theme; if preferred_theme == None { let new_theme = try_theme(window, preferred_theme); - let mut window_state = userdata.window_state.lock(); + let mut window_state = userdata.window_state_lock(); if window_state.current_theme != new_theme { window_state.current_theme = new_theme; @@ -2207,13 +2208,13 @@ unsafe fn public_window_callback_inner( DestroyWindow(window); 0 } else if msg == *SET_RETAIN_STATE_ON_SIZE_MSG_ID { - let mut window_state = userdata.window_state.lock(); + let mut window_state = userdata.window_state_lock(); window_state.set_window_flags_in_place(|f| { f.set(WindowFlags::MARKER_RETAIN_STATE_ON_SIZE, wparam != 0) }); 0 } else if msg == *TASKBAR_CREATED { - let window_state = userdata.window_state.lock(); + let window_state = userdata.window_state_lock(); set_skip_taskbar(window, window_state.skip_taskbar); DefWindowProcW(window, msg, wparam, lparam) } else { diff --git a/src/platform_impl/windows/event_loop/runner.rs b/src/platform_impl/windows/event_loop/runner.rs index 945d306f..c4ac1eb3 100644 --- a/src/platform_impl/windows/event_loop/runner.rs +++ b/src/platform_impl/windows/event_loop/runner.rs @@ -441,7 +441,7 @@ impl BufferedEvent { let window_flags = unsafe { let userdata = get_window_long(window_id.0.into(), GWL_USERDATA) as *mut WindowData; - (*userdata).window_state.lock().window_flags + (*userdata).window_state_lock().window_flags }; window_flags.set_size((window_id.0).0, new_inner_size); } diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 63e41dff..31294409 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -1,6 +1,5 @@ #![cfg(target_os = "windows")] -use parking_lot::Mutex; use raw_window_handle::{ RawDisplayHandle, RawWindowHandle, Win32WindowHandle, WindowsDisplayHandle, }; @@ -8,7 +7,7 @@ use std::{ cell::Cell, ffi::c_void, io, mem, panic, ptr, - sync::{mpsc::channel, Arc}, + sync::{mpsc::channel, Arc, Mutex, MutexGuard}, }; use windows_sys::Win32::{ @@ -99,6 +98,10 @@ impl Window { unsafe { init(w_attr, pl_attr, event_loop) } } + fn window_state_lock(&self) -> MutexGuard<'_, WindowState> { + self.window_state.lock().unwrap() + } + pub fn set_title(&self, text: &str) { let wide_text = util::encode_wide(text); unsafe { @@ -112,7 +115,7 @@ impl 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(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::VISIBLE, visible) }); }); @@ -154,7 +157,7 @@ impl Window { let window = self.window.clone(); self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::MAXIMIZED, false) }); }); @@ -207,18 +210,18 @@ impl Window { let window = self.window.clone(); self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::MAXIMIZED, false) }); }); - let window_flags = self.window_state.lock().window_flags; + let window_flags = self.window_state_lock().window_flags; window_flags.set_size(self.hwnd(), physical_size); } #[inline] pub fn set_min_inner_size(&self, size: Option) { - self.window_state.lock().min_size = size; + self.window_state_lock().min_size = size; // Make windows re-check the window size bounds. let size = self.inner_size(); self.set_inner_size(size.into()); @@ -226,7 +229,7 @@ impl Window { #[inline] pub fn set_max_inner_size(&self, size: Option) { - self.window_state.lock().max_size = size; + self.window_state_lock().max_size = size; // Make windows re-check the window size bounds. let size = self.inner_size(); self.set_inner_size(size.into()); @@ -239,7 +242,7 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::RESIZABLE, resizable) }); }); @@ -247,7 +250,7 @@ impl Window { #[inline] pub fn is_resizable(&self) -> bool { - let window_state = self.window_state.lock(); + let window_state = self.window_state_lock(); window_state.window_flags.contains(WindowFlags::RESIZABLE) } @@ -277,7 +280,7 @@ impl Window { #[inline] pub fn set_cursor_icon(&self, cursor: CursorIcon) { - self.window_state.lock().mouse.cursor = cursor; + self.window_state_lock().mouse.cursor = cursor; self.thread_executor.execute_in_thread(move || unsafe { let cursor = LoadCursorW(0, cursor.to_windows_cursor()); SetCursor(cursor); @@ -302,6 +305,7 @@ impl Window { let _ = &window; let result = window_state .lock() + .unwrap() .mouse .set_cursor_flags(window.0, |f| f.set(CursorFlags::GRABBED, confine)) .map_err(|e| ExternalError::Os(os_error!(e))); @@ -320,6 +324,7 @@ impl Window { let _ = &window; let result = window_state .lock() + .unwrap() .mouse .set_cursor_flags(window.0, |f| f.set(CursorFlags::HIDDEN, !visible)) .map_err(|e| e.to_string()); @@ -330,7 +335,7 @@ impl Window { #[inline] pub fn scale_factor(&self) -> f64 { - self.window_state.lock().scale_factor + self.window_state_lock().scale_factor } #[inline] @@ -379,7 +384,7 @@ impl Window { let window = self.window.clone(); let window_state = Arc::clone(&self.window_state); self.thread_executor.execute_in_thread(move || { - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::IGNORE_CURSOR_EVENT, !hittest) }); }); @@ -399,7 +404,7 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::MINIMIZED, minimized) }); }); @@ -412,7 +417,7 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::MAXIMIZED, maximized) }); }); @@ -420,13 +425,13 @@ impl Window { #[inline] pub fn is_maximized(&self) -> bool { - let window_state = self.window_state.lock(); + let window_state = self.window_state_lock(); window_state.window_flags.contains(WindowFlags::MAXIMIZED) } #[inline] pub fn fullscreen(&self) -> Option { - let window_state = self.window_state.lock(); + let window_state = self.window_state_lock(); window_state.fullscreen.clone() } @@ -435,7 +440,7 @@ impl Window { let window = self.window.clone(); let window_state = Arc::clone(&self.window_state); - let mut window_state_lock = window_state.lock(); + let mut window_state_lock = window_state.lock().unwrap(); let old_fullscreen = window_state_lock.fullscreen.clone(); if window_state_lock.fullscreen == fullscreen { return; @@ -502,7 +507,7 @@ impl Window { } // Update window style - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set( WindowFlags::MARKER_EXCLUSIVE_FULLSCREEN, matches!(fullscreen, Some(Fullscreen::Exclusive(_))), @@ -531,7 +536,7 @@ impl Window { placement }; - window_state.lock().saved_window = Some(SavedWindow { placement }); + window_state.lock().unwrap().saved_window = Some(SavedWindow { placement }); let monitor = match &fullscreen { Fullscreen::Exclusive(video_mode) => video_mode.monitor(), @@ -558,7 +563,7 @@ impl Window { } } None => { - let mut window_state_lock = window_state.lock(); + let mut window_state_lock = window_state.lock().unwrap(); if let Some(SavedWindow { placement }) = window_state_lock.saved_window.take() { drop(window_state_lock); unsafe { @@ -578,7 +583,7 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::MARKER_DECORATIONS, decorations) }); }); @@ -586,7 +591,7 @@ impl Window { #[inline] pub fn is_decorated(&self) -> bool { - let window_state = self.window_state.lock(); + let window_state = self.window_state_lock(); window_state .window_flags .contains(WindowFlags::MARKER_DECORATIONS) @@ -599,7 +604,7 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::ALWAYS_ON_TOP, always_on_top) }); }); @@ -621,7 +626,7 @@ impl Window { } else { icon::unset_for_window(self.hwnd(), IconType::Small); } - self.window_state.lock().window_icon = window_icon; + self.window_state_lock().window_icon = window_icon; } #[inline] @@ -638,7 +643,7 @@ impl Window { } else { icon::unset_for_window(self.hwnd(), IconType::Big); } - self.window_state.lock().taskbar_icon = taskbar_icon; + self.window_state_lock().taskbar_icon = taskbar_icon; } #[inline] @@ -650,7 +655,7 @@ impl Window { #[inline] pub fn set_ime_allowed(&self, allowed: bool) { - self.window_state.lock().ime_allowed = allowed; + self.window_state_lock().ime_allowed = allowed; unsafe { ImeContext::set_ime_allowed(self.hwnd(), allowed); } @@ -686,12 +691,12 @@ impl Window { #[inline] pub fn theme(&self) -> Theme { - self.window_state.lock().current_theme + self.window_state_lock().current_theme } #[inline] pub fn set_skip_taskbar(&self, skip: bool) { - self.window_state.lock().skip_taskbar = skip; + self.window_state_lock().skip_taskbar = skip; unsafe { set_skip_taskbar(self.hwnd(), skip) }; } @@ -702,7 +707,7 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock(), window.0, |f| { + WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| { f.set(WindowFlags::MARKER_UNDECORATED_SHADOW, shadow) }); }); @@ -711,7 +716,7 @@ impl Window { #[inline] pub fn focus_window(&self) { let window = self.window.clone(); - let window_flags = self.window_state.lock().window_flags(); + let window_flags = self.window_state_lock().window_flags(); let is_visible = window_flags.contains(WindowFlags::VISIBLE); let is_minimized = window_flags.contains(WindowFlags::MINIMIZED); @@ -783,7 +788,9 @@ impl<'a, T: 'static> InitData<'a, T> { self.pl_attribs.preferred_theme, ); let window_state = Arc::new(Mutex::new(window_state)); - WindowState::set_window_flags(window_state.lock(), window, |f| *f = self.window_flags); + WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { + *f = self.window_flags + }); window_state }; diff --git a/src/platform_impl/windows/window_state.rs b/src/platform_impl/windows/window_state.rs index bf0589c8..a523700a 100644 --- a/src/platform_impl/windows/window_state.rs +++ b/src/platform_impl/windows/window_state.rs @@ -5,8 +5,8 @@ use crate::{ platform_impl::platform::{event_loop, util}, window::{CursorIcon, Fullscreen, Theme, WindowAttributes}, }; -use parking_lot::MutexGuard; use std::io; +use std::sync::MutexGuard; use windows_sys::Win32::{ Foundation::{HWND, RECT}, Graphics::Gdi::InvalidateRgn,