Update display info when window moved to another monitor (#9360)
Release Notes: - N/A
This commit is contained in:
@@ -49,6 +49,7 @@ pub(crate) struct WindowsWindowInner {
|
||||
platform_inner: Rc<WindowsPlatformInner>,
|
||||
pub(crate) handle: AnyWindowHandle,
|
||||
scale_factor: f32,
|
||||
display: RefCell<Rc<WindowsDisplay>>,
|
||||
}
|
||||
|
||||
impl WindowsWindowInner {
|
||||
@@ -57,6 +58,7 @@ impl WindowsWindowInner {
|
||||
cs: &CREATESTRUCTW,
|
||||
platform_inner: Rc<WindowsPlatformInner>,
|
||||
handle: AnyWindowHandle,
|
||||
display: Rc<WindowsDisplay>,
|
||||
) -> Self {
|
||||
let origin = Cell::new(Point::new((cs.x as f64).into(), (cs.y as f64).into()));
|
||||
let size = Cell::new(Size {
|
||||
@@ -101,6 +103,7 @@ impl WindowsWindowInner {
|
||||
};
|
||||
let renderer = RefCell::new(BladeRenderer::new(gpu, extent));
|
||||
let callbacks = RefCell::new(Callbacks::default());
|
||||
let display = RefCell::new(display);
|
||||
Self {
|
||||
hwnd,
|
||||
origin,
|
||||
@@ -112,6 +115,7 @@ impl WindowsWindowInner {
|
||||
platform_inner,
|
||||
handle,
|
||||
scale_factor: 1.0,
|
||||
display,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,6 +255,22 @@ impl WindowsWindowInner {
|
||||
let x = lparam.signed_loword() as f64;
|
||||
let y = lparam.signed_hiword() as f64;
|
||||
self.origin.set(Point::new(x.into(), y.into()));
|
||||
let size = self.size.get();
|
||||
let center_x = x as f32 + size.width.0 / 2.0;
|
||||
let center_y = y as f32 + size.height.0 / 2.0;
|
||||
let monitor_bounds = self.display.borrow().bounds();
|
||||
if center_x < monitor_bounds.left().0
|
||||
|| center_x > monitor_bounds.right().0
|
||||
|| center_y < monitor_bounds.top().0
|
||||
|| center_y > monitor_bounds.bottom().0
|
||||
{
|
||||
// center of the window may have moved to another monitor
|
||||
let monitor = unsafe { MonitorFromWindow(self.hwnd, MONITOR_DEFAULTTONULL) };
|
||||
if !monitor.is_invalid() && self.display.borrow().handle != monitor {
|
||||
// we will get the same monitor if we only have one
|
||||
(*self.display.borrow_mut()) = Rc::new(WindowsDisplay::new_with_handle(monitor));
|
||||
}
|
||||
}
|
||||
let mut callbacks = self.callbacks.borrow_mut();
|
||||
if let Some(callback) = callbacks.moved.as_mut() {
|
||||
callback()
|
||||
@@ -1015,13 +1035,13 @@ struct Callbacks {
|
||||
pub(crate) struct WindowsWindow {
|
||||
inner: Rc<WindowsWindowInner>,
|
||||
drag_drop_handler: IDropTarget,
|
||||
display: Rc<WindowsDisplay>,
|
||||
}
|
||||
|
||||
struct WindowCreateContext {
|
||||
inner: Option<Rc<WindowsWindowInner>>,
|
||||
platform_inner: Rc<WindowsPlatformInner>,
|
||||
handle: AnyWindowHandle,
|
||||
display: Rc<WindowsDisplay>,
|
||||
}
|
||||
|
||||
impl WindowsWindow {
|
||||
@@ -1051,6 +1071,9 @@ impl WindowsWindow {
|
||||
inner: None,
|
||||
platform_inner: platform_inner.clone(),
|
||||
handle,
|
||||
// todo(windows) move window to target monitor
|
||||
// options.display_id
|
||||
display: Rc::new(WindowsDisplay::primary_monitor().unwrap()),
|
||||
};
|
||||
let lpparam = Some(&context as *const _ as *const _);
|
||||
unsafe {
|
||||
@@ -1079,12 +1102,9 @@ impl WindowsWindow {
|
||||
};
|
||||
drag_drop_handler
|
||||
};
|
||||
// todo(windows) move window to target monitor
|
||||
// options.display_id
|
||||
let wnd = Self {
|
||||
inner: context.inner.unwrap(),
|
||||
drag_drop_handler,
|
||||
display: Rc::new(WindowsDisplay::primary_monitor().unwrap()),
|
||||
};
|
||||
platform_inner
|
||||
.raw_window_handles
|
||||
@@ -1161,7 +1181,7 @@ impl PlatformWindow for WindowsWindow {
|
||||
}
|
||||
|
||||
fn display(&self) -> Rc<dyn PlatformDisplay> {
|
||||
self.display.clone()
|
||||
self.inner.display.borrow().clone()
|
||||
}
|
||||
|
||||
fn mouse_position(&self) -> Point<Pixels> {
|
||||
@@ -1504,6 +1524,7 @@ unsafe extern "system" fn wnd_proc(
|
||||
cs,
|
||||
ctx.platform_inner.clone(),
|
||||
ctx.handle,
|
||||
ctx.display.clone(),
|
||||
));
|
||||
let weak = Box::new(Rc::downgrade(&inner));
|
||||
unsafe { set_window_long(hwnd, GWLP_USERDATA, Box::into_raw(weak) as isize) };
|
||||
|
||||
Reference in New Issue
Block a user