feat: detect current active monitor via mouse cursor
This commit is contained in:
+26
-1
@@ -2,11 +2,36 @@ use i_slint_backend_winit::WinitWindowAccessor;
|
||||
use i_slint_backend_winit::winit::dpi::PhysicalPosition;
|
||||
use i_slint_backend_winit::winit::monitor::MonitorHandle;
|
||||
use i_slint_backend_winit::winit::window::Window;
|
||||
use windows::Win32::Foundation::POINT;
|
||||
use windows::Win32::UI::WindowsAndMessaging::GetCursorPos;
|
||||
|
||||
fn get_cursor_monitor(window: &Window) -> Option<MonitorHandle> {
|
||||
let mut point = POINT { x: 0, y: 0 };
|
||||
let ok = unsafe { GetCursorPos(&mut point) };
|
||||
if ok.is_err() {
|
||||
return None;
|
||||
}
|
||||
|
||||
for monitor in window.available_monitors() {
|
||||
let pos = monitor.position();
|
||||
let size = monitor.size();
|
||||
if point.x >= pos.x
|
||||
&& point.x < pos.x + size.width as i32
|
||||
&& point.y >= pos.y
|
||||
&& point.y < pos.y + size.height as i32
|
||||
{
|
||||
return Some(monitor);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn center_window(window: &slint::Window) {
|
||||
if window.has_winit_window() {
|
||||
window.with_winit_window(|window: &Window| {
|
||||
if let Some(monitor) = window.current_monitor() {
|
||||
let monitor = get_cursor_monitor(window)
|
||||
.or_else(|| window.current_monitor());
|
||||
if let Some(monitor) = monitor {
|
||||
set_centered(window, &monitor);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user