feat: detect current active monitor via mouse cursor
This commit is contained in:
Generated
+1
@@ -612,6 +612,7 @@ dependencies = [
|
||||
"slint",
|
||||
"slint-build",
|
||||
"toml 1.1.4+spec-1.1.0",
|
||||
"windows 0.62.2",
|
||||
"winit",
|
||||
"winreg",
|
||||
"winresource",
|
||||
|
||||
@@ -10,6 +10,7 @@ serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.149"
|
||||
slint = { version = "1.16.1", features = ["renderer-skia", "serde"] }
|
||||
toml = "1.1.4"
|
||||
windows = { version = "0.62.2", features = ["Win32_UI_WindowsAndMessaging", "Win32_Foundation"] }
|
||||
winit = "0.30"
|
||||
winreg = "0.56.0"
|
||||
|
||||
|
||||
+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