Skip to content

Commit d96c440

Browse files
committed
feat(windows): centering
Now when we open the app via the shortcut it centers it and also make it appear on the right monitor (where mouse cursor is).
1 parent 03d683a commit d96c440

3 files changed

Lines changed: 61 additions & 8 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2024"
66

77
[target.'cfg(target_os = "windows")'.dependencies]
88
winreg = "0.52"
9-
windows = { version = "0.58", features = ["Win32_UI_WindowsAndMessaging", "Win32_Foundation"] }
9+
windows = { version = "0.58", features = ["Win32_UI_WindowsAndMessaging", "Win32_Foundation", "Win32_Graphics_Gdi"] }
1010

1111
[target.'cfg(target_os = "macos")'.dependencies]
1212
objc2 = "0.6.3"

src/app.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,29 @@ impl Tile {
329329
if hk_id == self.open_hotkey_id {
330330
self.visible = !self.visible;
331331
if self.visible {
332-
Task::chain(
333-
window::open(default_settings())
334-
.1
335-
.map(|_| Message::OpenWindow),
336-
operation::focus("query"),
337-
)
332+
#[cfg(target_os = "windows")]
333+
{
334+
// get normal settings and modify position
335+
use crate::utils::open_on_focused_monitor;
336+
use iced::window::Position::Specific;
337+
let pos = open_on_focused_monitor();
338+
let mut settings = default_settings();
339+
settings.position = Specific(pos);
340+
Task::chain(
341+
window::open(settings).1.map(|_| Message::OpenWindow),
342+
operation::focus("query"),
343+
)
344+
}
345+
346+
#[cfg(target_os = "macos")]
347+
{
348+
Task::chain(
349+
window::open(default_settings())
350+
.1
351+
.map(|_| Message::OpenWindow),
352+
operation::focus("query"),
353+
)
354+
}
338355
} else {
339356
let to_close = window::latest().map(|x| x.unwrap());
340357
Task::batch([

src/utils.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ use std::{
66
};
77

88
use global_hotkey::hotkey::Code;
9-
use iced::widget::image::Handle;
9+
use iced::{futures::io::Window, widget::image::Handle};
1010
use icns::IconFamily;
1111
use image::RgbaImage;
1212
use rayon::iter::{IntoParallelIterator, ParallelIterator};
13+
use windows::Win32::{
14+
Graphics::Gdi::MonitorFromPoint,
15+
UI::WindowsAndMessaging::{GetCursor, GetCursorPos},
16+
};
1317

1418
use crate::{app::App, commands::Function};
1519

@@ -373,3 +377,35 @@ pub fn open_application(path: &String) {
373377
Command::new("xdg-open").arg(path).status().ok();
374378
}
375379
}
380+
381+
struct WindowPos {
382+
x: f32,
383+
y: f32,
384+
}
385+
386+
use crate::app::{DEFAULT_WINDOW_HEIGHT, WINDOW_WIDTH};
387+
pub fn open_on_focused_monitor() -> iced::Point {
388+
use windows::Win32::Foundation::POINT;
389+
use windows::Win32::Graphics::Gdi::{
390+
GetMonitorInfoW, MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromPoint,
391+
};
392+
let mut point = POINT { x: 0, y: 0 };
393+
let mut monitor_info = MONITORINFO {
394+
cbSize: std::mem::size_of::<MONITORINFO>() as u32,
395+
..Default::default()
396+
};
397+
398+
let cursor = unsafe { GetCursorPos(&mut point) };
399+
let monitor = unsafe { MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST) };
400+
let monitor_infos = unsafe { GetMonitorInfoW(monitor, &mut monitor_info) };
401+
402+
let monitor_width = monitor_info.rcMonitor.right - monitor_info.rcMonitor.left;
403+
let monitor_height = monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top;
404+
let window_width = WINDOW_WIDTH;
405+
let window_height = DEFAULT_WINDOW_HEIGHT;
406+
407+
let x = monitor_info.rcMonitor.left as f32 + (monitor_width as f32 - window_width) / 2.0;
408+
let y = monitor_info.rcMonitor.top as f32 + (monitor_height as f32 - window_height) / 2.0;
409+
410+
return iced::Point { x, y };
411+
}

0 commit comments

Comments
 (0)