feat: detect current active monitor via mouse cursor
This commit is contained in:
Generated
+1
@@ -612,6 +612,7 @@ dependencies = [
|
|||||||
"slint",
|
"slint",
|
||||||
"slint-build",
|
"slint-build",
|
||||||
"toml 1.1.4+spec-1.1.0",
|
"toml 1.1.4+spec-1.1.0",
|
||||||
|
"windows 0.62.2",
|
||||||
"winit",
|
"winit",
|
||||||
"winreg",
|
"winreg",
|
||||||
"winresource",
|
"winresource",
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ serde = { version = "1.0.228", features = ["derive"] }
|
|||||||
serde_json = "1.0.149"
|
serde_json = "1.0.149"
|
||||||
slint = { version = "1.16.1", features = ["renderer-skia", "serde"] }
|
slint = { version = "1.16.1", features = ["renderer-skia", "serde"] }
|
||||||
toml = "1.1.4"
|
toml = "1.1.4"
|
||||||
|
windows = { version = "0.62.2", features = ["Win32_UI_WindowsAndMessaging", "Win32_Foundation"] }
|
||||||
winit = "0.30"
|
winit = "0.30"
|
||||||
winreg = "0.56.0"
|
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::dpi::PhysicalPosition;
|
||||||
use i_slint_backend_winit::winit::monitor::MonitorHandle;
|
use i_slint_backend_winit::winit::monitor::MonitorHandle;
|
||||||
use i_slint_backend_winit::winit::window::Window;
|
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) {
|
pub fn center_window(window: &slint::Window) {
|
||||||
if window.has_winit_window() {
|
if window.has_winit_window() {
|
||||||
window.with_winit_window(|window: &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);
|
set_centered(window, &monitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user