2017-06-26 21:21:13 +02:00
|
|
|
#![cfg(target_os = "windows")]
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
use parking_lot::Mutex;
|
2019-08-14 07:57:16 -04:00
|
|
|
use raw_window_handle::{windows::WindowsHandle, RawWindowHandle};
|
2019-06-21 11:33:15 -04:00
|
|
|
use std::{
|
|
|
|
|
cell::Cell,
|
|
|
|
|
ffi::OsStr,
|
|
|
|
|
io, mem,
|
|
|
|
|
os::windows::ffi::OsStrExt,
|
|
|
|
|
ptr,
|
|
|
|
|
sync::{mpsc::channel, Arc},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use winapi::{
|
|
|
|
|
ctypes::c_int,
|
|
|
|
|
shared::{
|
2020-01-04 01:29:40 -05:00
|
|
|
minwindef::{HINSTANCE, UINT},
|
2019-06-21 11:33:15 -04:00
|
|
|
windef::{HWND, POINT, RECT},
|
|
|
|
|
},
|
|
|
|
|
um::{
|
2020-12-10 05:16:59 +09:00
|
|
|
combaseapi, dwmapi,
|
|
|
|
|
imm::{CFS_POINT, COMPOSITIONFORM},
|
|
|
|
|
libloaderapi,
|
2019-06-21 11:33:15 -04:00
|
|
|
objbase::COINIT_APARTMENTTHREADED,
|
|
|
|
|
ole2,
|
|
|
|
|
oleidl::LPDROPTARGET,
|
|
|
|
|
shobjidl_core::{CLSID_TaskbarList, ITaskbarList2},
|
2020-01-06 15:28:58 -05:00
|
|
|
winnt::LPCWSTR,
|
2019-06-21 11:33:15 -04:00
|
|
|
winuser,
|
|
|
|
|
},
|
|
|
|
|
};
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::{
|
2019-06-19 16:49:43 -04:00
|
|
|
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
|
2019-06-21 11:33:15 -04:00
|
|
|
error::{ExternalError, NotSupportedError, OsError as RootOsError},
|
2020-03-07 19:42:21 +00:00
|
|
|
icon::Icon,
|
2019-06-21 11:33:15 -04:00
|
|
|
monitor::MonitorHandle as RootMonitorHandle,
|
|
|
|
|
platform_impl::platform::{
|
2020-11-30 19:04:26 +01:00
|
|
|
dark_mode::try_theme,
|
2019-06-21 11:33:15 -04:00
|
|
|
dpi::{dpi_to_scale_factor, hwnd_dpi},
|
|
|
|
|
drop_handler::FileDropHandler,
|
2020-01-04 01:29:40 -05:00
|
|
|
event_loop::{self, EventLoopWindowTarget, DESTROY_MSG_ID},
|
2020-03-07 19:42:21 +00:00
|
|
|
icon::{self, IconType},
|
2019-12-30 14:11:11 -05:00
|
|
|
monitor, util,
|
2019-06-21 11:33:15 -04:00
|
|
|
window_state::{CursorFlags, SavedWindow, WindowFlags, WindowState},
|
|
|
|
|
PlatformSpecificWindowBuilderAttributes, WindowId,
|
|
|
|
|
},
|
2020-11-30 19:04:26 +01:00
|
|
|
window::{CursorIcon, Fullscreen, Theme, UserAttentionType, WindowAttributes},
|
2018-06-14 19:42:18 -04:00
|
|
|
};
|
2018-07-04 10:15:19 +10:00
|
|
|
|
2017-06-26 21:21:13 +02:00
|
|
|
/// The Win32 implementation of the main `Window` object.
|
|
|
|
|
pub struct Window {
|
|
|
|
|
/// Main handle for the window.
|
|
|
|
|
window: WindowWrapper,
|
|
|
|
|
|
|
|
|
|
/// The current window state.
|
2018-07-27 23:34:08 +01:00
|
|
|
window_state: Arc<Mutex<WindowState>>,
|
2018-05-07 17:36:21 -04:00
|
|
|
|
2018-04-13 01:12:15 +08:00
|
|
|
// The events loop proxy.
|
2019-02-05 10:30:33 -05:00
|
|
|
thread_executor: event_loop::EventLoopThreadExecutor,
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Window {
|
2019-02-05 10:30:33 -05:00
|
|
|
pub fn new<T: 'static>(
|
|
|
|
|
event_loop: &EventLoopWindowTarget<T>,
|
2018-05-07 17:36:21 -04:00
|
|
|
w_attr: WindowAttributes,
|
|
|
|
|
pl_attr: PlatformSpecificWindowBuilderAttributes,
|
2019-05-29 21:29:54 -04:00
|
|
|
) -> Result<Window, RootOsError> {
|
2019-02-05 10:30:33 -05:00
|
|
|
// We dispatch an `init` function because of code style.
|
|
|
|
|
// First person to remove the need for cloning here gets a cookie!
|
|
|
|
|
//
|
|
|
|
|
// done. you owe me -- ossi
|
|
|
|
|
unsafe {
|
2020-06-29 01:17:27 +03:00
|
|
|
let drag_and_drop = pl_attr.drag_and_drop;
|
2019-02-05 10:30:33 -05:00
|
|
|
init(w_attr, pl_attr, event_loop).map(|win| {
|
2020-06-29 01:17:27 +03:00
|
|
|
let file_drop_handler = if drag_and_drop {
|
2019-02-05 10:30:33 -05:00
|
|
|
use winapi::shared::winerror::{OLE_E_WRONGCOMPOBJ, RPC_E_CHANGED_MODE, S_OK};
|
|
|
|
|
|
|
|
|
|
let ole_init_result = ole2::OleInitialize(ptr::null_mut());
|
|
|
|
|
// It is ok if the initialize result is `S_FALSE` because it might happen that
|
|
|
|
|
// multiple windows are created on the same thread.
|
|
|
|
|
if ole_init_result == OLE_E_WRONGCOMPOBJ {
|
|
|
|
|
panic!("OleInitialize failed! Result was: `OLE_E_WRONGCOMPOBJ`");
|
|
|
|
|
} else if ole_init_result == RPC_E_CHANGED_MODE {
|
2020-06-29 01:17:27 +03:00
|
|
|
panic!(
|
|
|
|
|
"OleInitialize failed! Result was: `RPC_E_CHANGED_MODE`. \
|
|
|
|
|
Make sure other crates are not using multithreaded COM library \
|
|
|
|
|
on the same thread or disable drag and drop support."
|
|
|
|
|
);
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let file_drop_runner = event_loop.runner_shared.clone();
|
|
|
|
|
let file_drop_handler = FileDropHandler::new(
|
|
|
|
|
win.window.0,
|
2019-06-21 11:33:15 -04:00
|
|
|
Box::new(move |event| {
|
|
|
|
|
if let Ok(e) = event.map_nonuser_event() {
|
|
|
|
|
file_drop_runner.send_event(e)
|
|
|
|
|
}
|
|
|
|
|
}),
|
2019-02-05 10:30:33 -05:00
|
|
|
);
|
2019-06-21 11:33:15 -04:00
|
|
|
let handler_interface_ptr =
|
|
|
|
|
&mut (*file_drop_handler.data).interface as LPDROPTARGET;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2019-06-21 11:33:15 -04:00
|
|
|
assert_eq!(
|
|
|
|
|
ole2::RegisterDragDrop(win.window.0, handler_interface_ptr),
|
|
|
|
|
S_OK
|
|
|
|
|
);
|
2020-06-29 01:17:27 +03:00
|
|
|
Some(file_drop_handler)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2019-02-05 10:30:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let subclass_input = event_loop::SubclassInput {
|
|
|
|
|
window_state: win.window_state.clone(),
|
|
|
|
|
event_loop_runner: event_loop.runner_shared.clone(),
|
|
|
|
|
file_drop_handler,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
event_loop::subclass_window(win.window.0, subclass_input);
|
|
|
|
|
win
|
|
|
|
|
})
|
|
|
|
|
}
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_title(&self, text: &str) {
|
2018-05-07 17:36:21 -04:00
|
|
|
let text = OsStr::new(text)
|
|
|
|
|
.encode_wide()
|
|
|
|
|
.chain(Some(0).into_iter())
|
|
|
|
|
.collect::<Vec<_>>();
|
2017-06-26 21:21:13 +02:00
|
|
|
unsafe {
|
2017-12-24 15:46:47 +02:00
|
|
|
winuser::SetWindowTextW(self.window.0, text.as_ptr() as LPCWSTR);
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn set_visible(&self, visible: bool) {
|
2019-12-29 10:39:15 -05:00
|
|
|
let window = self.window.clone();
|
2019-06-19 16:49:43 -04:00
|
|
|
let window_state = Arc::clone(&self.window_state);
|
2019-12-29 10:39:15 -05:00
|
|
|
self.thread_executor.execute_in_thread(move || {
|
|
|
|
|
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
|
|
|
|
|
f.set(WindowFlags::VISIBLE, visible)
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
#[inline]
|
|
|
|
|
pub fn request_redraw(&self) {
|
|
|
|
|
unsafe {
|
2019-08-26 22:05:42 -04:00
|
|
|
winuser::RedrawWindow(
|
|
|
|
|
self.window.0,
|
|
|
|
|
ptr::null(),
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
winuser::RDW_INTERNALPAINT,
|
|
|
|
|
);
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 16:49:43 -04:00
|
|
|
#[inline]
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
|
2018-06-14 19:42:18 -04:00
|
|
|
util::get_window_rect(self.window.0)
|
2020-01-04 01:33:07 -05:00
|
|
|
.map(|rect| Ok(PhysicalPosition::new(rect.left as i32, rect.top as i32)))
|
2019-06-19 16:49:43 -04:00
|
|
|
.expect("Unexpected GetWindowRect failure; please report this error to https://github.com/rust-windowing/winit")
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-06-14 19:42:18 -04:00
|
|
|
#[inline]
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
|
Windows: Position fixes (#479)
* Remove executable flag from os/macos.rs
This was causing me some grief while working on Windows, and it
doesn't belong here to begin with.
* Windows: get_position returns screen coordinates instead of workspace coordinates
Previously, get_position used GetWindowPlacement. As per the
documentation of WINDOWSTRUCT, the returned coordinates are in
workspace space, meaning they're relative to the taskbar. It's
also explicitly remarked that these coordinates should only be
used in conjunction with SetWindowPlacement, as mixing them with
functions expecting screen coordinates can cause unpleasantness.
Since our set_position (correctly) uses SetWindowPos, this meant
that passing the return of get_position to set_position would
cause the window to move.
We now use GetWindowRect, which returns screen coordinates. This
gives us both better consistency within the Windows backend and
across platforms.
Note that this only makes a difference if the taskbar is visible.
With the taskbar hidden, the values are exactly the same as before.
* Windows: Moved event position values are consistent with get_position
The old Moved values had two problems:
* They were obtained by casting a WORD (u16) straight to an i32.
This meant wrap-around would never be interpreted as negative,
thus negative positions (which are ubiquitous when using multiple
monitors) would result in positions around u16::MAX.
* WM_MOVE supplies client area positions, not window positions.
Switching to handling WM_WINDOWPOSCHANGED solves both of these
problems.
* Better documentation for Moved and Resized
2018-04-26 20:09:33 -04:00
|
|
|
let mut position: POINT = unsafe { mem::zeroed() };
|
|
|
|
|
if unsafe { winuser::ClientToScreen(self.window.0, &mut position) } == 0 {
|
2019-05-29 21:29:54 -04:00
|
|
|
panic!("Unexpected ClientToScreen failure: please report this error to https://github.com/rust-windowing/winit")
|
2018-04-16 21:40:30 -04:00
|
|
|
}
|
2020-01-04 01:33:07 -05:00
|
|
|
Ok(PhysicalPosition::new(position.x as i32, position.y as i32))
|
2018-04-16 21:40:30 -04:00
|
|
|
}
|
|
|
|
|
|
2018-06-14 19:42:18 -04:00
|
|
|
#[inline]
|
2019-06-19 16:49:43 -04:00
|
|
|
pub fn set_outer_position(&self, position: Position) {
|
2020-01-03 14:52:27 -05:00
|
|
|
let (x, y): (i32, i32) = position.to_physical::<i32>(self.scale_factor()).into();
|
2019-06-19 16:49:43 -04:00
|
|
|
|
|
|
|
|
let window_state = Arc::clone(&self.window_state);
|
|
|
|
|
let window = self.window.clone();
|
|
|
|
|
self.thread_executor.execute_in_thread(move || {
|
|
|
|
|
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
|
|
|
|
|
f.set(WindowFlags::MAXIMIZED, false)
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-06-14 19:42:18 -04:00
|
|
|
|
2017-06-26 21:21:13 +02:00
|
|
|
unsafe {
|
2018-06-14 19:42:18 -04:00
|
|
|
winuser::SetWindowPos(
|
|
|
|
|
self.window.0,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
x as c_int,
|
|
|
|
|
y as c_int,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
2019-11-27 04:49:15 +01:00
|
|
|
winuser::SWP_ASYNCWINDOWPOS
|
|
|
|
|
| winuser::SWP_NOZORDER
|
|
|
|
|
| winuser::SWP_NOSIZE
|
|
|
|
|
| winuser::SWP_NOACTIVATE,
|
2018-06-14 19:42:18 -04:00
|
|
|
);
|
2020-03-07 20:04:24 +01:00
|
|
|
winuser::InvalidateRgn(self.window.0, ptr::null_mut(), 0);
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn inner_size(&self) -> PhysicalSize<u32> {
|
2019-06-29 00:07:36 +02:00
|
|
|
let mut rect: RECT = unsafe { mem::zeroed() };
|
2017-12-24 15:46:47 +02:00
|
|
|
if unsafe { winuser::GetClientRect(self.window.0, &mut rect) } == 0 {
|
2019-05-29 21:29:54 -04:00
|
|
|
panic!("Unexpected GetClientRect failure: please report this error to https://github.com/rust-windowing/winit")
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
2019-06-19 16:49:43 -04:00
|
|
|
PhysicalSize::new(
|
2017-06-26 21:21:13 +02:00
|
|
|
(rect.right - rect.left) as u32,
|
2018-06-14 19:42:18 -04:00
|
|
|
(rect.bottom - rect.top) as u32,
|
2019-05-29 21:29:54 -04:00
|
|
|
)
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn outer_size(&self) -> PhysicalSize<u32> {
|
2018-06-14 19:42:18 -04:00
|
|
|
util::get_window_rect(self.window.0)
|
2019-06-21 11:33:15 -04:00
|
|
|
.map(|rect| {
|
2019-06-19 16:49:43 -04:00
|
|
|
PhysicalSize::new(
|
2019-06-21 11:33:15 -04:00
|
|
|
(rect.right - rect.left) as u32,
|
|
|
|
|
(rect.bottom - rect.top) as u32,
|
|
|
|
|
)
|
|
|
|
|
})
|
2019-05-29 21:29:54 -04:00
|
|
|
.unwrap()
|
2018-06-14 19:42:18 -04:00
|
|
|
}
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2018-03-23 05:35:35 -04:00
|
|
|
#[inline]
|
2019-06-19 16:49:43 -04:00
|
|
|
pub fn set_inner_size(&self, size: Size) {
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
let (width, height) = size.to_physical::<u32>(scale_factor).into();
|
2019-08-26 22:07:15 -04:00
|
|
|
|
|
|
|
|
let window_state = Arc::clone(&self.window_state);
|
|
|
|
|
let window = self.window.clone();
|
|
|
|
|
self.thread_executor.execute_in_thread(move || {
|
|
|
|
|
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
|
|
|
|
|
f.set(WindowFlags::MAXIMIZED, false)
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2020-01-06 15:28:58 -05:00
|
|
|
util::set_inner_size_physical(self.window.0, width, height);
|
2018-06-14 19:42:18 -04:00
|
|
|
}
|
2018-03-23 05:35:35 -04:00
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-19 16:49:43 -04:00
|
|
|
pub fn set_min_inner_size(&self, size: Option<Size>) {
|
|
|
|
|
self.window_state.lock().min_size = size;
|
2018-03-23 05:35:35 -04:00
|
|
|
// Make windows re-check the window size bounds.
|
2019-06-19 16:49:43 -04:00
|
|
|
let size = self.inner_size();
|
|
|
|
|
self.set_inner_size(size.into());
|
2018-06-14 19:42:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-19 16:49:43 -04:00
|
|
|
pub fn set_max_inner_size(&self, size: Option<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());
|
2018-03-23 05:35:35 -04:00
|
|
|
}
|
2018-06-12 11:58:18 -04:00
|
|
|
|
2018-06-11 16:47:50 -06:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_resizable(&self, resizable: bool) {
|
2019-02-04 11:52:00 -05:00
|
|
|
let window = self.window.clone();
|
|
|
|
|
let window_state = Arc::clone(&self.window_state);
|
2018-06-14 19:42:18 -04:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
self.thread_executor.execute_in_thread(move || {
|
2019-07-29 21:16:14 +03:00
|
|
|
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
|
2019-06-21 11:33:15 -04:00
|
|
|
f.set(WindowFlags::RESIZABLE, resizable)
|
|
|
|
|
});
|
2019-02-04 11:52:00 -05:00
|
|
|
});
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the `hwnd` of this window.
|
|
|
|
|
#[inline]
|
2017-12-24 15:46:47 +02:00
|
|
|
pub fn hwnd(&self) -> HWND {
|
2017-06-26 21:21:13 +02:00
|
|
|
self.window.0
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-05 20:52:40 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn hinstance(&self) -> HINSTANCE {
|
|
|
|
|
unsafe { winuser::GetWindowLongW(self.hwnd(), winuser::GWL_HINSTANCE) as *mut _ }
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 07:57:16 -04:00
|
|
|
#[inline]
|
|
|
|
|
pub fn raw_window_handle(&self) -> RawWindowHandle {
|
|
|
|
|
let handle = WindowsHandle {
|
|
|
|
|
hwnd: self.window.0 as *mut _,
|
2019-10-05 21:34:27 -04:00
|
|
|
hinstance: self.hinstance() as *mut _,
|
2019-08-14 07:57:16 -04:00
|
|
|
..WindowsHandle::empty()
|
|
|
|
|
};
|
|
|
|
|
RawWindowHandle::Windows(handle)
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 21:21:13 +02:00
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn set_cursor_icon(&self, cursor: CursorIcon) {
|
2019-02-05 10:30:33 -05:00
|
|
|
self.window_state.lock().mouse.cursor = cursor;
|
|
|
|
|
self.thread_executor.execute_in_thread(move || unsafe {
|
2019-06-21 11:33:15 -04:00
|
|
|
let cursor = winuser::LoadCursorW(ptr::null_mut(), cursor.to_windows_cursor());
|
2018-09-22 21:03:38 -04:00
|
|
|
winuser::SetCursor(cursor);
|
|
|
|
|
});
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-06-18 12:32:18 -04:00
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn set_cursor_grab(&self, grab: bool) -> Result<(), ExternalError> {
|
2018-06-18 12:32:18 -04:00
|
|
|
let window = self.window.clone();
|
2018-07-27 23:34:08 +01:00
|
|
|
let window_state = Arc::clone(&self.window_state);
|
2018-06-18 12:32:18 -04:00
|
|
|
let (tx, rx) = channel();
|
2019-02-04 11:52:00 -05:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
self.thread_executor.execute_in_thread(move || {
|
2019-06-21 11:33:15 -04:00
|
|
|
let result = window_state
|
|
|
|
|
.lock()
|
|
|
|
|
.mouse
|
2019-02-04 11:52:00 -05:00
|
|
|
.set_cursor_flags(window.0, |f| f.set(CursorFlags::GRABBED, grab))
|
2019-05-29 21:29:54 -04:00
|
|
|
.map_err(|e| ExternalError::Os(os_error!(e)));
|
2018-06-18 12:32:18 -04:00
|
|
|
let _ = tx.send(result);
|
|
|
|
|
});
|
|
|
|
|
rx.recv().unwrap()
|
|
|
|
|
}
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2018-06-18 12:32:18 -04:00
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn set_cursor_visible(&self, visible: bool) {
|
2019-02-04 11:52:00 -05:00
|
|
|
let window = self.window.clone();
|
2018-07-27 23:34:08 +01:00
|
|
|
let window_state = Arc::clone(&self.window_state);
|
2019-02-04 11:52:00 -05:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
self.thread_executor.execute_in_thread(move || {
|
2019-06-21 11:33:15 -04:00
|
|
|
let result = window_state
|
|
|
|
|
.lock()
|
|
|
|
|
.mouse
|
2019-05-29 21:29:54 -04:00
|
|
|
.set_cursor_flags(window.0, |f| f.set(CursorFlags::HIDDEN, !visible))
|
2019-02-04 11:52:00 -05:00
|
|
|
.map_err(|e| e.to_string());
|
|
|
|
|
let _ = tx.send(result);
|
2018-05-19 12:02:57 -04:00
|
|
|
});
|
2019-02-04 11:52:00 -05:00
|
|
|
rx.recv().unwrap().ok();
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-03 14:52:27 -05:00
|
|
|
pub fn scale_factor(&self) -> f64 {
|
2020-02-13 20:41:41 +01:00
|
|
|
self.window_state.lock().scale_factor
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 16:49:43 -04:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_cursor_position(&self, position: Position) -> Result<(), ExternalError> {
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
let (x, y) = position.to_physical::<i32>(scale_factor).into();
|
2019-06-19 16:49:43 -04:00
|
|
|
|
2018-06-14 19:42:18 -04:00
|
|
|
let mut point = POINT { x, y };
|
2017-06-26 21:21:13 +02:00
|
|
|
unsafe {
|
2017-12-24 15:46:47 +02:00
|
|
|
if winuser::ClientToScreen(self.window.0, &mut point) == 0 {
|
2019-05-29 21:29:54 -04:00
|
|
|
return Err(ExternalError::Os(os_error!(io::Error::last_os_error())));
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
2017-12-24 15:46:47 +02:00
|
|
|
if winuser::SetCursorPos(point.x, point.y) == 0 {
|
2019-05-29 21:29:54 -04:00
|
|
|
return Err(ExternalError::Os(os_error!(io::Error::last_os_error())));
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn id(&self) -> WindowId {
|
|
|
|
|
WindowId(self.window.0)
|
|
|
|
|
}
|
2017-08-28 01:43:34 +01:00
|
|
|
|
2019-12-22 01:04:11 -05:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_minimized(&self, minimized: bool) {
|
|
|
|
|
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| {
|
|
|
|
|
f.set(WindowFlags::MINIMIZED, minimized)
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 01:43:34 +01:00
|
|
|
#[inline]
|
2018-04-13 01:12:15 +08:00
|
|
|
pub fn set_maximized(&self, maximized: bool) {
|
|
|
|
|
let window = self.window.clone();
|
2018-06-18 12:32:18 -04:00
|
|
|
let window_state = Arc::clone(&self.window_state);
|
2018-04-13 01:12:15 +08:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
self.thread_executor.execute_in_thread(move || {
|
2019-07-29 21:16:14 +03:00
|
|
|
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
|
2019-06-21 11:33:15 -04:00
|
|
|
f.set(WindowFlags::MAXIMIZED, maximized)
|
|
|
|
|
});
|
2018-04-13 01:12:15 +08:00
|
|
|
});
|
2017-08-28 01:43:34 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-26 03:09:32 +10:00
|
|
|
#[inline]
|
2019-07-29 21:16:14 +03:00
|
|
|
pub fn fullscreen(&self) -> Option<Fullscreen> {
|
2019-04-26 03:09:32 +10:00
|
|
|
let window_state = self.window_state.lock();
|
|
|
|
|
window_state.fullscreen.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 01:43:34 +01:00
|
|
|
#[inline]
|
2019-07-29 21:16:14 +03:00
|
|
|
pub fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
|
|
|
|
|
let window = self.window.clone();
|
|
|
|
|
let window_state = Arc::clone(&self.window_state);
|
2018-04-13 01:12:15 +08:00
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
let mut window_state_lock = window_state.lock();
|
|
|
|
|
let old_fullscreen = window_state_lock.fullscreen.clone();
|
|
|
|
|
if window_state_lock.fullscreen == fullscreen {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
window_state_lock.fullscreen = fullscreen.clone();
|
|
|
|
|
drop(window_state_lock);
|
|
|
|
|
|
|
|
|
|
self.thread_executor.execute_in_thread(move || {
|
|
|
|
|
let mut window_state_lock = window_state.lock();
|
|
|
|
|
|
|
|
|
|
// Save window bounds before entering fullscreen
|
|
|
|
|
match (&old_fullscreen, &fullscreen) {
|
|
|
|
|
(&None, &Some(_)) => {
|
|
|
|
|
let client_rect = util::get_client_rect(window.0).unwrap();
|
|
|
|
|
window_state_lock.saved_window = Some(SavedWindow {
|
|
|
|
|
client_rect,
|
2020-02-13 20:41:41 +01:00
|
|
|
scale_factor: window_state_lock.scale_factor,
|
2018-04-13 01:12:15 +08:00
|
|
|
});
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-07-29 21:16:14 +03:00
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Change video mode if we're transitioning to or from exclusive
|
|
|
|
|
// fullscreen
|
|
|
|
|
match (&old_fullscreen, &fullscreen) {
|
|
|
|
|
(&None, &Some(Fullscreen::Exclusive(ref video_mode)))
|
|
|
|
|
| (
|
|
|
|
|
&Some(Fullscreen::Borderless(_)),
|
|
|
|
|
&Some(Fullscreen::Exclusive(ref video_mode)),
|
|
|
|
|
)
|
|
|
|
|
| (&Some(Fullscreen::Exclusive(_)), &Some(Fullscreen::Exclusive(ref video_mode))) =>
|
|
|
|
|
{
|
|
|
|
|
let monitor = video_mode.monitor();
|
|
|
|
|
|
|
|
|
|
let mut display_name = OsStr::new(&monitor.inner.native_identifier())
|
|
|
|
|
.encode_wide()
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
// `encode_wide` does not add a null-terminator but
|
|
|
|
|
// `ChangeDisplaySettingsExW` requires a null-terminated
|
|
|
|
|
// string, so add it
|
|
|
|
|
display_name.push(0);
|
|
|
|
|
|
|
|
|
|
let mut native_video_mode = video_mode.video_mode.native_video_mode.clone();
|
|
|
|
|
|
|
|
|
|
let res = unsafe {
|
|
|
|
|
winuser::ChangeDisplaySettingsExW(
|
|
|
|
|
display_name.as_ptr(),
|
|
|
|
|
&mut native_video_mode,
|
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
|
winuser::CDS_FULLSCREEN,
|
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debug_assert!(res != winuser::DISP_CHANGE_BADFLAGS);
|
|
|
|
|
debug_assert!(res != winuser::DISP_CHANGE_BADMODE);
|
|
|
|
|
debug_assert!(res != winuser::DISP_CHANGE_BADPARAM);
|
|
|
|
|
debug_assert!(res != winuser::DISP_CHANGE_FAILED);
|
|
|
|
|
assert_eq!(res, winuser::DISP_CHANGE_SUCCESSFUL);
|
|
|
|
|
}
|
|
|
|
|
(&Some(Fullscreen::Exclusive(_)), &None)
|
|
|
|
|
| (&Some(Fullscreen::Exclusive(_)), &Some(Fullscreen::Borderless(_))) => {
|
|
|
|
|
let res = unsafe {
|
|
|
|
|
winuser::ChangeDisplaySettingsExW(
|
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
|
winuser::CDS_FULLSCREEN,
|
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debug_assert!(res != winuser::DISP_CHANGE_BADFLAGS);
|
|
|
|
|
debug_assert!(res != winuser::DISP_CHANGE_BADMODE);
|
|
|
|
|
debug_assert!(res != winuser::DISP_CHANGE_BADPARAM);
|
|
|
|
|
debug_assert!(res != winuser::DISP_CHANGE_FAILED);
|
|
|
|
|
assert_eq!(res, winuser::DISP_CHANGE_SUCCESSFUL);
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
// There are some scenarios where calling `ChangeDisplaySettingsExW` takes long
|
|
|
|
|
// enough to execute that the DWM thinks our program has frozen and takes over
|
|
|
|
|
// our program's window. When that happens, the `SetWindowPos` call below gets
|
|
|
|
|
// eaten and the window doesn't get set to the proper fullscreen position.
|
|
|
|
|
//
|
|
|
|
|
// Calling `PeekMessageW` here notifies Windows that our process is still running
|
|
|
|
|
// fine, taking control back from the DWM and ensuring that the `SetWindowPos` call
|
|
|
|
|
// below goes through.
|
|
|
|
|
let mut msg = mem::zeroed();
|
|
|
|
|
winuser::PeekMessageW(&mut msg, ptr::null_mut(), 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update window style
|
|
|
|
|
WindowState::set_window_flags(window_state_lock, window.0, |f| {
|
2020-10-19 15:15:23 +01:00
|
|
|
f.set(
|
|
|
|
|
WindowFlags::MARKER_EXCLUSIVE_FULLSCREEN,
|
|
|
|
|
matches!(fullscreen, Some(Fullscreen::Exclusive(_))),
|
|
|
|
|
);
|
|
|
|
|
f.set(
|
|
|
|
|
WindowFlags::MARKER_BORDERLESS_FULLSCREEN,
|
|
|
|
|
matches!(fullscreen, Some(Fullscreen::Borderless(_))),
|
|
|
|
|
);
|
2019-07-29 21:16:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Update window bounds
|
|
|
|
|
match &fullscreen {
|
|
|
|
|
Some(fullscreen) => {
|
2020-09-22 04:54:47 +03:00
|
|
|
let monitor = match &fullscreen {
|
|
|
|
|
Fullscreen::Exclusive(video_mode) => video_mode.monitor(),
|
|
|
|
|
Fullscreen::Borderless(Some(monitor)) => monitor.clone(),
|
|
|
|
|
Fullscreen::Borderless(None) => RootMonitorHandle {
|
|
|
|
|
inner: monitor::current_monitor(window.0),
|
|
|
|
|
},
|
2019-07-29 21:16:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let position: (i32, i32) = monitor.position().into();
|
|
|
|
|
let size: (u32, u32) = monitor.size().into();
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
winuser::SetWindowPos(
|
|
|
|
|
window.0,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
position.0,
|
|
|
|
|
position.1,
|
|
|
|
|
size.0 as i32,
|
|
|
|
|
size.1 as i32,
|
|
|
|
|
winuser::SWP_ASYNCWINDOWPOS | winuser::SWP_NOZORDER,
|
|
|
|
|
);
|
2020-03-07 20:04:24 +01:00
|
|
|
winuser::InvalidateRgn(window.0, ptr::null_mut(), 0);
|
2019-07-29 21:16:14 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
let mut window_state_lock = window_state.lock();
|
|
|
|
|
if let Some(SavedWindow {
|
|
|
|
|
client_rect,
|
2020-02-13 20:41:41 +01:00
|
|
|
scale_factor,
|
2019-07-29 21:16:14 +03:00
|
|
|
}) = window_state_lock.saved_window.take()
|
|
|
|
|
{
|
2020-02-13 20:41:41 +01:00
|
|
|
window_state_lock.scale_factor = scale_factor;
|
2019-07-29 21:16:14 +03:00
|
|
|
drop(window_state_lock);
|
2019-09-19 11:48:20 -04:00
|
|
|
let client_rect = util::adjust_window_rect(window.0, client_rect).unwrap();
|
2019-07-29 21:16:14 +03:00
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
winuser::SetWindowPos(
|
2019-02-04 11:52:00 -05:00
|
|
|
window.0,
|
2019-07-29 21:16:14 +03:00
|
|
|
ptr::null_mut(),
|
|
|
|
|
client_rect.left,
|
|
|
|
|
client_rect.top,
|
|
|
|
|
client_rect.right - client_rect.left,
|
|
|
|
|
client_rect.bottom - client_rect.top,
|
2019-11-27 04:49:15 +01:00
|
|
|
winuser::SWP_ASYNCWINDOWPOS
|
|
|
|
|
| winuser::SWP_NOZORDER
|
|
|
|
|
| winuser::SWP_NOACTIVATE,
|
2019-02-04 11:52:00 -05:00
|
|
|
);
|
2020-03-07 20:04:24 +01:00
|
|
|
winuser::InvalidateRgn(window.0, ptr::null_mut(), 0);
|
2019-02-04 11:52:00 -05:00
|
|
|
}
|
2019-07-29 21:16:14 +03:00
|
|
|
}
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2018-04-13 01:12:15 +08:00
|
|
|
}
|
2019-07-29 21:16:14 +03:00
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
taskbar_mark_fullscreen(window.0, fullscreen.is_some());
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-09-07 09:33:46 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-22 07:50:46 -05:00
|
|
|
#[inline]
|
2018-04-13 01:12:15 +08:00
|
|
|
pub fn set_decorations(&self, decorations: bool) {
|
2019-02-04 11:52:00 -05:00
|
|
|
let window = self.window.clone();
|
|
|
|
|
let window_state = Arc::clone(&self.window_state);
|
2018-06-14 19:42:18 -04:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
self.thread_executor.execute_in_thread(move || {
|
2019-07-29 21:16:14 +03:00
|
|
|
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
|
2019-06-21 11:33:15 -04:00
|
|
|
f.set(WindowFlags::DECORATIONS, decorations)
|
|
|
|
|
});
|
2019-02-04 11:52:00 -05:00
|
|
|
});
|
2018-06-14 19:42:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn set_always_on_top(&self, always_on_top: bool) {
|
2019-02-04 11:52:00 -05:00
|
|
|
let window = self.window.clone();
|
|
|
|
|
let window_state = Arc::clone(&self.window_state);
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
self.thread_executor.execute_in_thread(move || {
|
2019-07-29 21:16:14 +03:00
|
|
|
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
|
2019-06-21 11:33:15 -04:00
|
|
|
f.set(WindowFlags::ALWAYS_ON_TOP, always_on_top)
|
|
|
|
|
});
|
2019-02-04 11:52:00 -05:00
|
|
|
});
|
2018-05-20 10:24:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-09-07 09:33:46 +01:00
|
|
|
#[inline]
|
2020-09-07 20:09:24 +03:00
|
|
|
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
|
|
|
|
|
Some(RootMonitorHandle {
|
2019-05-29 21:29:54 -04:00
|
|
|
inner: monitor::current_monitor(self.window.0),
|
2020-09-07 20:09:24 +03:00
|
|
|
})
|
2017-08-28 01:43:34 +01:00
|
|
|
}
|
2018-05-07 17:36:21 -04:00
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-07 19:42:21 +00:00
|
|
|
pub fn set_window_icon(&self, window_icon: Option<Icon>) {
|
2018-05-07 17:36:21 -04:00
|
|
|
if let Some(ref window_icon) = window_icon {
|
2020-03-07 19:42:21 +00:00
|
|
|
window_icon
|
|
|
|
|
.inner
|
|
|
|
|
.set_for_window(self.window.0, IconType::Small);
|
2018-05-07 17:36:21 -04:00
|
|
|
} else {
|
|
|
|
|
icon::unset_for_window(self.window.0, IconType::Small);
|
|
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
self.window_state.lock().window_icon = window_icon;
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-07 19:42:21 +00:00
|
|
|
pub fn set_taskbar_icon(&self, taskbar_icon: Option<Icon>) {
|
2018-05-07 17:36:21 -04:00
|
|
|
if let Some(ref taskbar_icon) = taskbar_icon {
|
2020-03-07 19:42:21 +00:00
|
|
|
taskbar_icon
|
|
|
|
|
.inner
|
|
|
|
|
.set_for_window(self.window.0, IconType::Big);
|
2018-05-07 17:36:21 -04:00
|
|
|
} else {
|
|
|
|
|
icon::unset_for_window(self.window.0, IconType::Big);
|
|
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
self.window_state.lock().taskbar_icon = taskbar_icon;
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
2018-05-17 21:28:30 -04:00
|
|
|
|
2020-12-10 05:16:59 +09:00
|
|
|
pub(crate) fn set_ime_position_physical(&self, x: i32, y: i32) {
|
|
|
|
|
if unsafe { winuser::GetSystemMetrics(winuser::SM_IMMENABLED) } != 0 {
|
|
|
|
|
let mut composition_form = COMPOSITIONFORM {
|
|
|
|
|
dwStyle: CFS_POINT,
|
|
|
|
|
ptCurrentPos: POINT { x, y },
|
|
|
|
|
rcArea: unsafe { mem::zeroed() },
|
|
|
|
|
};
|
|
|
|
|
unsafe {
|
|
|
|
|
let himc = winapi::um::imm::ImmGetContext(self.window.0);
|
|
|
|
|
winapi::um::imm::ImmSetCompositionWindow(himc, &mut composition_form);
|
|
|
|
|
winapi::um::imm::ImmReleaseContext(self.window.0, himc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-17 21:28:30 -04:00
|
|
|
#[inline]
|
2020-12-10 05:16:59 +09:00
|
|
|
pub fn set_ime_position(&self, spot: Position) {
|
|
|
|
|
let (x, y) = spot.to_physical::<i32>(self.scale_factor()).into();
|
|
|
|
|
self.set_ime_position_physical(x, y);
|
2018-05-17 21:28:30 -04:00
|
|
|
}
|
2019-12-22 19:04:09 +00:00
|
|
|
|
2020-11-27 03:03:08 +01:00
|
|
|
#[inline]
|
|
|
|
|
pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) {
|
|
|
|
|
let window = self.window.clone();
|
|
|
|
|
let active_window_handle = unsafe { winuser::GetActiveWindow() };
|
|
|
|
|
if window.0 == active_window_handle {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.thread_executor.execute_in_thread(move || unsafe {
|
|
|
|
|
let (flags, count) = request_type
|
|
|
|
|
.map(|ty| match ty {
|
|
|
|
|
UserAttentionType::Critical => {
|
|
|
|
|
(winuser::FLASHW_ALL | winuser::FLASHW_TIMERNOFG, u32::MAX)
|
|
|
|
|
}
|
|
|
|
|
UserAttentionType::Informational => {
|
|
|
|
|
(winuser::FLASHW_TRAY | winuser::FLASHW_TIMERNOFG, 0)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or((winuser::FLASHW_STOP, 0));
|
|
|
|
|
|
|
|
|
|
let mut flash_info = winuser::FLASHWINFO {
|
|
|
|
|
cbSize: mem::size_of::<winuser::FLASHWINFO>() as UINT,
|
|
|
|
|
hwnd: window.0,
|
|
|
|
|
dwFlags: flags,
|
|
|
|
|
uCount: count,
|
|
|
|
|
dwTimeout: 0,
|
|
|
|
|
};
|
|
|
|
|
winuser::FlashWindowEx(&mut flash_info);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 19:04:09 +00:00
|
|
|
#[inline]
|
2020-11-30 19:04:26 +01:00
|
|
|
pub fn theme(&self) -> Theme {
|
|
|
|
|
self.window_state.lock().current_theme
|
2019-12-22 19:04:09 +00:00
|
|
|
}
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Window {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
2018-04-24 16:20:40 -04:00
|
|
|
// The window must be destroyed from the same thread that created it, so we send a
|
|
|
|
|
// custom message to be handled by our callback to do the actual work.
|
|
|
|
|
winuser::PostMessageW(self.window.0, *DESTROY_MSG_ID, 0, 0);
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-25 21:30:16 +03:00
|
|
|
/// A simple non-owning wrapper around a window.
|
2017-06-26 21:21:13 +02:00
|
|
|
#[doc(hidden)]
|
2018-04-13 01:12:15 +08:00
|
|
|
#[derive(Clone)]
|
2018-08-02 19:03:15 +02:00
|
|
|
pub struct WindowWrapper(HWND);
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2018-07-27 23:34:08 +01:00
|
|
|
// Send and Sync are not implemented for HWND and HDC, we have to wrap it and implement them manually.
|
2018-04-13 01:12:15 +08:00
|
|
|
// For more info see:
|
|
|
|
|
// https://github.com/retep998/winapi-rs/issues/360
|
|
|
|
|
// https://github.com/retep998/winapi-rs/issues/396
|
2018-07-27 23:34:08 +01:00
|
|
|
unsafe impl Sync for WindowWrapper {}
|
2018-04-13 01:12:15 +08:00
|
|
|
unsafe impl Send for WindowWrapper {}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
unsafe fn init<T: 'static>(
|
2020-03-07 19:42:21 +00:00
|
|
|
attributes: WindowAttributes,
|
2019-05-29 21:29:54 -04:00
|
|
|
pl_attribs: PlatformSpecificWindowBuilderAttributes,
|
2019-02-05 10:30:33 -05:00
|
|
|
event_loop: &EventLoopWindowTarget<T>,
|
2019-05-29 21:29:54 -04:00
|
|
|
) -> Result<Window, RootOsError> {
|
2018-06-14 19:42:18 -04:00
|
|
|
let title = OsStr::new(&attributes.title)
|
2018-05-07 17:36:21 -04:00
|
|
|
.encode_wide()
|
|
|
|
|
.chain(Some(0).into_iter())
|
2017-06-26 21:21:13 +02:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
// registering the window class
|
2020-03-07 19:42:21 +00:00
|
|
|
let class_name = register_window_class(&attributes.window_icon, &pl_attribs.taskbar_icon);
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2019-02-04 11:52:00 -05:00
|
|
|
let mut window_flags = WindowFlags::empty();
|
|
|
|
|
window_flags.set(WindowFlags::DECORATIONS, attributes.decorations);
|
|
|
|
|
window_flags.set(WindowFlags::ALWAYS_ON_TOP, attributes.always_on_top);
|
2019-06-21 11:33:15 -04:00
|
|
|
window_flags.set(
|
|
|
|
|
WindowFlags::NO_BACK_BUFFER,
|
|
|
|
|
pl_attribs.no_redirection_bitmap,
|
|
|
|
|
);
|
2019-02-04 11:52:00 -05:00
|
|
|
window_flags.set(WindowFlags::TRANSPARENT, attributes.transparent);
|
|
|
|
|
// WindowFlags::VISIBLE and MAXIMIZED are set down below after the window has been configured.
|
|
|
|
|
window_flags.set(WindowFlags::RESIZABLE, attributes.resizable);
|
|
|
|
|
window_flags.set(WindowFlags::CHILD, pl_attribs.parent.is_some());
|
|
|
|
|
window_flags.set(WindowFlags::ON_TASKBAR, true);
|
2017-06-26 21:21:13 +02:00
|
|
|
|
|
|
|
|
// creating the real window this time, by using the functions in `extra_functions`
|
|
|
|
|
let real_window = {
|
2019-02-04 11:52:00 -05:00
|
|
|
let (style, ex_style) = window_flags.to_window_styles();
|
|
|
|
|
let handle = winuser::CreateWindowExW(
|
|
|
|
|
ex_style,
|
2017-06-26 21:21:13 +02:00
|
|
|
class_name.as_ptr(),
|
2017-12-24 15:46:47 +02:00
|
|
|
title.as_ptr() as LPCWSTR,
|
2019-02-04 11:52:00 -05:00
|
|
|
style,
|
2019-06-21 11:33:15 -04:00
|
|
|
winuser::CW_USEDEFAULT,
|
|
|
|
|
winuser::CW_USEDEFAULT,
|
|
|
|
|
winuser::CW_USEDEFAULT,
|
|
|
|
|
winuser::CW_USEDEFAULT,
|
2017-06-26 21:21:13 +02:00
|
|
|
pl_attribs.parent.unwrap_or(ptr::null_mut()),
|
2018-06-14 19:42:18 -04:00
|
|
|
ptr::null_mut(),
|
|
|
|
|
libloaderapi::GetModuleHandleW(ptr::null()),
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
);
|
2017-06-26 21:21:13 +02:00
|
|
|
|
|
|
|
|
if handle.is_null() {
|
2019-05-29 21:29:54 -04:00
|
|
|
return Err(os_error!(io::Error::last_os_error()));
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-02 19:03:15 +02:00
|
|
|
WindowWrapper(handle)
|
2017-06-26 21:21:13 +02:00
|
|
|
};
|
|
|
|
|
|
2018-04-05 21:25:37 +02:00
|
|
|
// Register for touch events if applicable
|
|
|
|
|
{
|
2019-06-21 11:33:15 -04:00
|
|
|
let digitizer = winuser::GetSystemMetrics(winuser::SM_DIGITIZER) as u32;
|
2018-04-05 21:25:37 +02:00
|
|
|
if digitizer & winuser::NID_READY != 0 {
|
2019-06-21 11:33:15 -04:00
|
|
|
winuser::RegisterTouchWindow(real_window.0, winuser::TWF_WANTPALM);
|
2018-04-05 21:25:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
Windows: Position fixes (#479)
* Remove executable flag from os/macos.rs
This was causing me some grief while working on Windows, and it
doesn't belong here to begin with.
* Windows: get_position returns screen coordinates instead of workspace coordinates
Previously, get_position used GetWindowPlacement. As per the
documentation of WINDOWSTRUCT, the returned coordinates are in
workspace space, meaning they're relative to the taskbar. It's
also explicitly remarked that these coordinates should only be
used in conjunction with SetWindowPlacement, as mixing them with
functions expecting screen coordinates can cause unpleasantness.
Since our set_position (correctly) uses SetWindowPos, this meant
that passing the return of get_position to set_position would
cause the window to move.
We now use GetWindowRect, which returns screen coordinates. This
gives us both better consistency within the Windows backend and
across platforms.
Note that this only makes a difference if the taskbar is visible.
With the taskbar hidden, the values are exactly the same as before.
* Windows: Moved event position values are consistent with get_position
The old Moved values had two problems:
* They were obtained by casting a WORD (u16) straight to an i32.
This meant wrap-around would never be interpreted as negative,
thus negative positions (which are ubiquitous when using multiple
monitors) would result in positions around u16::MAX.
* WM_MOVE supplies client area positions, not window positions.
Switching to handling WM_WINDOWPOSCHANGED solves both of these
problems.
* Better documentation for Moved and Resized
2018-04-26 20:09:33 -04:00
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
let dpi = hwnd_dpi(real_window.0);
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = dpi_to_scale_factor(dpi);
|
2018-05-07 17:36:21 -04:00
|
|
|
|
2017-06-26 21:21:13 +02:00
|
|
|
// making the window transparent
|
2018-06-22 10:33:29 +09:00
|
|
|
if attributes.transparent && !pl_attribs.no_redirection_bitmap {
|
2017-12-24 15:46:47 +02:00
|
|
|
let bb = dwmapi::DWM_BLURBEHIND {
|
2020-10-14 03:23:34 -07:00
|
|
|
dwFlags: dwmapi::DWM_BB_ENABLE,
|
2017-06-26 21:21:13 +02:00
|
|
|
fEnable: 1,
|
2020-10-14 03:23:34 -07:00
|
|
|
hRgnBlur: ptr::null_mut(),
|
2017-06-26 21:21:13 +02:00
|
|
|
fTransitionOnMaximized: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dwmapi::DwmEnableBlurBehindWindow(real_window.0, &bb);
|
2018-10-17 17:23:59 -07:00
|
|
|
|
|
|
|
|
if attributes.decorations {
|
2020-10-14 03:23:34 -07:00
|
|
|
let opacity = 255;
|
|
|
|
|
|
|
|
|
|
winuser::SetLayeredWindowAttributes(real_window.0, 0, opacity, winuser::LWA_ALPHA);
|
2018-10-17 17:23:59 -07:00
|
|
|
}
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
2018-04-24 16:20:40 -04:00
|
|
|
|
2019-12-22 19:04:09 +00:00
|
|
|
// If the system theme is dark, we need to set the window theme now
|
|
|
|
|
// before we update the window flags (and possibly show the
|
|
|
|
|
// window for the first time).
|
2020-11-30 19:04:26 +01:00
|
|
|
let current_theme = try_theme(real_window.0, pl_attribs.preferred_theme);
|
2019-12-22 19:04:09 +00:00
|
|
|
|
2019-02-04 11:52:00 -05:00
|
|
|
let window_state = {
|
2019-12-22 19:04:09 +00:00
|
|
|
let window_state = WindowState::new(
|
|
|
|
|
&attributes,
|
2020-03-07 19:42:21 +00:00
|
|
|
pl_attribs.taskbar_icon,
|
2020-02-13 20:41:41 +01:00
|
|
|
scale_factor,
|
2020-11-30 19:04:26 +01:00
|
|
|
current_theme,
|
|
|
|
|
pl_attribs.preferred_theme,
|
2019-12-22 19:04:09 +00:00
|
|
|
);
|
2019-02-04 11:52:00 -05:00
|
|
|
let window_state = Arc::new(Mutex::new(window_state));
|
2019-07-29 21:16:14 +03:00
|
|
|
WindowState::set_window_flags(window_state.lock(), real_window.0, |f| *f = window_flags);
|
2019-02-04 11:52:00 -05:00
|
|
|
window_state
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-13 01:12:15 +08:00
|
|
|
let win = Window {
|
|
|
|
|
window: real_window,
|
2018-07-27 23:34:08 +01:00
|
|
|
window_state,
|
2019-02-05 10:30:33 -05:00
|
|
|
thread_executor: event_loop.create_thread_executor(),
|
2018-04-13 01:12:15 +08:00
|
|
|
};
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2019-06-19 16:49:43 -04:00
|
|
|
let dimensions = attributes
|
|
|
|
|
.inner_size
|
|
|
|
|
.unwrap_or_else(|| PhysicalSize::new(1024, 768).into());
|
|
|
|
|
win.set_inner_size(dimensions);
|
2020-04-19 21:52:48 +02:00
|
|
|
if attributes.maximized {
|
|
|
|
|
// Need to set MAXIMIZED after setting `inner_size` as
|
|
|
|
|
// `Window::set_inner_size` changes MAXIMIZED to false.
|
|
|
|
|
win.set_maximized(true);
|
|
|
|
|
}
|
2019-06-19 16:49:43 -04:00
|
|
|
win.set_visible(attributes.visible);
|
|
|
|
|
|
2018-06-14 19:42:18 -04:00
|
|
|
if let Some(_) = attributes.fullscreen {
|
|
|
|
|
win.set_fullscreen(attributes.fullscreen);
|
2018-04-13 01:12:15 +08:00
|
|
|
force_window_active(win.window.0);
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-13 01:12:15 +08:00
|
|
|
Ok(win)
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
unsafe fn register_window_class(
|
2020-03-07 19:42:21 +00:00
|
|
|
window_icon: &Option<Icon>,
|
|
|
|
|
taskbar_icon: &Option<Icon>,
|
2018-05-07 17:36:21 -04:00
|
|
|
) -> Vec<u16> {
|
|
|
|
|
let class_name: Vec<_> = OsStr::new("Window Class")
|
|
|
|
|
.encode_wide()
|
|
|
|
|
.chain(Some(0).into_iter())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let h_icon = taskbar_icon
|
|
|
|
|
.as_ref()
|
2020-03-07 19:42:21 +00:00
|
|
|
.map(|icon| icon.inner.as_raw_handle())
|
2018-05-07 17:36:21 -04:00
|
|
|
.unwrap_or(ptr::null_mut());
|
|
|
|
|
let h_icon_small = window_icon
|
|
|
|
|
.as_ref()
|
2020-03-07 19:42:21 +00:00
|
|
|
.map(|icon| icon.inner.as_raw_handle())
|
2018-05-07 17:36:21 -04:00
|
|
|
.unwrap_or(ptr::null_mut());
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2017-12-24 15:46:47 +02:00
|
|
|
let class = winuser::WNDCLASSEXW {
|
|
|
|
|
cbSize: mem::size_of::<winuser::WNDCLASSEXW>() as UINT,
|
|
|
|
|
style: winuser::CS_HREDRAW | winuser::CS_VREDRAW | winuser::CS_OWNDC,
|
2019-02-05 10:30:33 -05:00
|
|
|
lpfnWndProc: Some(winuser::DefWindowProcW),
|
2017-06-26 21:21:13 +02:00
|
|
|
cbClsExtra: 0,
|
|
|
|
|
cbWndExtra: 0,
|
2017-12-24 15:46:47 +02:00
|
|
|
hInstance: libloaderapi::GetModuleHandleW(ptr::null()),
|
2018-05-07 17:36:21 -04:00
|
|
|
hIcon: h_icon,
|
|
|
|
|
hCursor: ptr::null_mut(), // must be null in order for cursor state to work properly
|
2017-06-26 21:21:13 +02:00
|
|
|
hbrBackground: ptr::null_mut(),
|
|
|
|
|
lpszMenuName: ptr::null(),
|
|
|
|
|
lpszClassName: class_name.as_ptr(),
|
2018-05-07 17:36:21 -04:00
|
|
|
hIconSm: h_icon_small,
|
2017-06-26 21:21:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// We ignore errors because registering the same window class twice would trigger
|
|
|
|
|
// an error, and because errors here are detected during CreateWindowEx anyway.
|
|
|
|
|
// Also since there is no weird element in the struct, there is no reason for this
|
|
|
|
|
// call to fail.
|
2017-12-24 15:46:47 +02:00
|
|
|
winuser::RegisterClassExW(&class);
|
2017-06-26 21:21:13 +02:00
|
|
|
|
|
|
|
|
class_name
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 01:12:15 +08:00
|
|
|
struct ComInitialized(*mut ());
|
|
|
|
|
impl Drop for ComInitialized {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe { combaseapi::CoUninitialize() };
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|
2018-04-13 01:12:15 +08:00
|
|
|
}
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2019-06-21 11:33:15 -04:00
|
|
|
thread_local! {
|
2018-04-13 01:12:15 +08:00
|
|
|
static COM_INITIALIZED: ComInitialized = {
|
|
|
|
|
unsafe {
|
2019-02-05 10:30:33 -05:00
|
|
|
combaseapi::CoInitializeEx(ptr::null_mut(), COINIT_APARTMENTTHREADED);
|
2018-04-13 01:12:15 +08:00
|
|
|
ComInitialized(ptr::null_mut())
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2018-06-12 11:58:18 -04:00
|
|
|
static TASKBAR_LIST: Cell<*mut ITaskbarList2> = Cell::new(ptr::null_mut());
|
2018-04-13 01:12:15 +08:00
|
|
|
}
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2018-04-13 01:12:15 +08:00
|
|
|
pub fn com_initialized() {
|
|
|
|
|
COM_INITIALIZED.with(|_| {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reference Implementation:
|
|
|
|
|
// https://github.com/chromium/chromium/blob/f18e79d901f56154f80eea1e2218544285e62623/ui/views/win/fullscreen_handler.cc
|
|
|
|
|
//
|
|
|
|
|
// As per MSDN marking the window as fullscreen should ensure that the
|
|
|
|
|
// taskbar is moved to the bottom of the Z-order when the fullscreen window
|
|
|
|
|
// is activated. If the window is not fullscreen, the Shell falls back to
|
|
|
|
|
// heuristics to determine how the window should be treated, which means
|
|
|
|
|
// that it could still consider the window as fullscreen. :(
|
2019-07-29 21:16:14 +03:00
|
|
|
unsafe fn taskbar_mark_fullscreen(handle: HWND, fullscreen: bool) {
|
2018-04-13 01:12:15 +08:00
|
|
|
com_initialized();
|
|
|
|
|
|
|
|
|
|
TASKBAR_LIST.with(|task_bar_list_ptr| {
|
|
|
|
|
let mut task_bar_list = task_bar_list_ptr.get();
|
|
|
|
|
|
|
|
|
|
if task_bar_list == ptr::null_mut() {
|
2019-06-21 11:33:15 -04:00
|
|
|
use winapi::{shared::winerror::S_OK, Interface};
|
2018-04-13 01:12:15 +08:00
|
|
|
|
|
|
|
|
let hr = combaseapi::CoCreateInstance(
|
2018-06-12 11:58:18 -04:00
|
|
|
&CLSID_TaskbarList,
|
2018-04-13 01:12:15 +08:00
|
|
|
ptr::null_mut(),
|
|
|
|
|
combaseapi::CLSCTX_ALL,
|
2018-06-12 11:58:18 -04:00
|
|
|
&ITaskbarList2::uuidof(),
|
2018-04-13 01:12:15 +08:00
|
|
|
&mut task_bar_list as *mut _ as *mut _,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if hr != S_OK || (*task_bar_list).HrInit() != S_OK {
|
|
|
|
|
// In some old windows, the taskbar object could not be created, we just ignore it
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
task_bar_list_ptr.set(task_bar_list)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
task_bar_list = task_bar_list_ptr.get();
|
|
|
|
|
(*task_bar_list).MarkFullscreenWindow(handle, if fullscreen { 1 } else { 0 });
|
|
|
|
|
})
|
|
|
|
|
}
|
2017-06-26 21:21:13 +02:00
|
|
|
|
2018-04-13 01:12:15 +08:00
|
|
|
unsafe fn force_window_active(handle: HWND) {
|
|
|
|
|
// In some situation, calling SetForegroundWindow could not bring up the window,
|
|
|
|
|
// This is a little hack which can "steal" the foreground window permission
|
|
|
|
|
// We only call this function in the window creation, so it should be fine.
|
|
|
|
|
// See : https://stackoverflow.com/questions/10740346/setforegroundwindow-only-working-while-visual-studio-is-open
|
|
|
|
|
let alt_sc = winuser::MapVirtualKeyW(winuser::VK_MENU as _, winuser::MAPVK_VK_TO_VSC);
|
|
|
|
|
|
|
|
|
|
let mut inputs: [winuser::INPUT; 2] = mem::zeroed();
|
|
|
|
|
inputs[0].type_ = winuser::INPUT_KEYBOARD;
|
|
|
|
|
inputs[0].u.ki_mut().wVk = winuser::VK_LMENU as _;
|
|
|
|
|
inputs[0].u.ki_mut().wScan = alt_sc as _;
|
|
|
|
|
inputs[0].u.ki_mut().dwFlags = winuser::KEYEVENTF_EXTENDEDKEY;
|
|
|
|
|
|
|
|
|
|
inputs[1].type_ = winuser::INPUT_KEYBOARD;
|
|
|
|
|
inputs[1].u.ki_mut().wVk = winuser::VK_LMENU as _;
|
|
|
|
|
inputs[1].u.ki_mut().wScan = alt_sc as _;
|
|
|
|
|
inputs[1].u.ki_mut().dwFlags = winuser::KEYEVENTF_EXTENDEDKEY | winuser::KEYEVENTF_KEYUP;
|
|
|
|
|
|
|
|
|
|
// Simulate a key press and release
|
|
|
|
|
winuser::SendInput(
|
|
|
|
|
inputs.len() as _,
|
|
|
|
|
inputs.as_mut_ptr(),
|
|
|
|
|
mem::size_of::<winuser::INPUT>() as _,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
winuser::SetForegroundWindow(handle);
|
2017-06-26 21:21:13 +02:00
|
|
|
}
|