Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod registry;
pub mod shortcuts;
pub mod utils;
pub mod win32;
pub mod window_placement;

/// Progress callback shared by the installer and uninstaller UI/worker paths.
pub type ProgressFn = std::sync::Arc<dyn Fn(u64, u64, &str) + Send + Sync>;
37 changes: 8 additions & 29 deletions common/src/win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! controls and again on `WM_DPICHANGED`.

use crate::utils::wide;
use crate::window_placement::center;
use windows::Win32::Foundation::{HINSTANCE, HWND, LPARAM, RECT, WPARAM};
use windows::Win32::Graphics::Gdi::{
CLEARTYPE_QUALITY, CLIP_DEFAULT_PRECIS, CreateFontW, DEFAULT_CHARSET, DEFAULT_PITCH,
Expand All @@ -20,12 +21,11 @@ use windows::Win32::UI::Controls::{
use windows::Win32::UI::HiDpi::{AdjustWindowRectExForDpi, GetDpiForWindow};
use windows::Win32::UI::Shell::ExtractIconW;
use windows::Win32::UI::WindowsAndMessaging::{
CW_USEDEFAULT, CreateWindowExW, DispatchMessageW, GetDlgItem, GetMessageW, GetSystemMetrics,
GetWindowRect, GetWindowTextLengthW, GetWindowTextW, HICON, HMENU, IDC_ARROW, LoadCursorW, MSG,
MoveWindow, PostMessageW, RegisterClassExW, SM_CXSCREEN, SM_CYSCREEN, SWP_NOACTIVATE,
SWP_NOMOVE, SWP_NOSIZE, SWP_NOZORDER, SendMessageW, SetWindowPos, SetWindowTextW,
TranslateMessage, WINDOW_EX_STYLE, WINDOW_STYLE, WM_SETFONT, WM_SETICON, WNDCLASSEXW, WNDPROC,
WS_CHILD, WS_TABSTOP,
CW_USEDEFAULT, CreateWindowExW, DispatchMessageW, GetDlgItem, GetMessageW,
GetWindowTextLengthW, GetWindowTextW, HICON, HMENU, IDC_ARROW, LoadCursorW, MSG, MoveWindow,
PostMessageW, RegisterClassExW, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOZORDER, SendMessageW,
SetWindowPos, SetWindowTextW, TranslateMessage, WINDOW_EX_STYLE, WINDOW_STYLE, WM_SETFONT,
WM_SETICON, WNDCLASSEXW, WNDPROC, WS_CHILD, WS_TABSTOP,
};
use windows::core::PCWSTR;

Expand Down Expand Up @@ -113,6 +113,8 @@ pub unsafe fn create_main_window(
/// `client_w` × `client_h` in 96-dpi units, and center it. Returns that DPI.
pub fn fit_to_monitor(hwnd: HWND, client_w: i32, client_h: i32, style: WINDOW_STYLE) -> i32 {
unsafe {
// Move to the launch monitor before querying its DPI.
center(hwnd);
let dpi = dpi_for(hwnd);
let (w, h) = window_size_for_client(
scale(client_w, dpi),
Expand Down Expand Up @@ -301,29 +303,6 @@ fn window_size_for_client(
(r.right - r.left, r.bottom - r.top)
}

/// Center a top-level window on the primary monitor.
fn center(hwnd: HWND) {
let mut rect = RECT::default();
unsafe {
let _ = GetWindowRect(hwnd, &mut rect);
};
let w = rect.right - rect.left;
let h = rect.bottom - rect.top;
let sw = unsafe { GetSystemMetrics(SM_CXSCREEN) };
let sh = unsafe { GetSystemMetrics(SM_CYSCREEN) };
unsafe {
let _ = SetWindowPos(
hwnd,
None,
(sw - w) / 2,
(sh - h) / 2,
0,
0,
SWP_NOSIZE | SWP_NOZORDER,
);
}
}

/// Set the font of a child control by dialog id.
pub fn set_font(parent: HWND, id: usize, font: HFONT) {
unsafe {
Expand Down
141 changes: 141 additions & 0 deletions common/src/window_placement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Gaëtan Dezeiraud, Louis Pinaud

//! Initial placement shared by the installer, uninstaller and both UI backends.

use std::{fmt, str::FromStr, sync::OnceLock};
use windows::Win32::Foundation::{HWND, POINT, RECT};
use windows::Win32::Graphics::Gdi::{
GetMonitorInfoW, MONITOR_DEFAULTTOPRIMARY, MONITORINFO, MonitorFromPoint, MonitorFromWindow,
};
use windows::Win32::UI::WindowsAndMessaging::{
GetCursorPos, GetForegroundWindow, GetWindowRect, SWP_NOACTIVATE, SWP_NOSIZE, SWP_NOZORDER,
SetWindowPos,
};

static LAUNCH_MONITOR: OnceLock<LaunchMonitor> = OnceLock::new();

/// A point inside the launch monitor, transferable across process boundaries.
/// Monitor handles cannot be used as persistent launch coordinates.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct LaunchMonitor {
x: i32,
y: i32,
}

impl LaunchMonitor {
/// Capture before opening UI or doing work that may change the foreground.
pub fn capture() -> Self {
unsafe {
let foreground = GetForegroundWindow();
let monitor = if foreground.is_invalid() {
let mut point = POINT::default();
let _ = GetCursorPos(&mut point);
MonitorFromPoint(point, MONITOR_DEFAULTTOPRIMARY)
} else {
MonitorFromWindow(foreground, MONITOR_DEFAULTTOPRIMARY)
};
let mut info = monitor_info();
if GetMonitorInfoW(monitor, &mut info).as_bool() {
let rc = info.rcMonitor;
Self {
x: rc.left + (rc.right - rc.left) / 2,
y: rc.top + (rc.bottom - rc.top) / 2,
}
} else {
Self::default()
}
}
}
}

impl fmt::Display for LaunchMonitor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{},{}", self.x, self.y)
}
}

impl FromStr for LaunchMonitor {
type Err = &'static str;

fn from_str(value: &str) -> Result<Self, Self::Err> {
let (x, y) = value.split_once(',').ok_or("expected monitor point x,y")?;
Ok(Self {
x: x.parse().map_err(|_| "invalid monitor x coordinate")?,
y: y.parse().map_err(|_| "invalid monitor y coordinate")?,
})
}
}

pub fn capture_launch_monitor() {
remember_launch_monitor(LaunchMonitor::capture());
}

/// Initialize once, using the original launch point when this is a child process.
pub fn remember_launch_monitor(monitor: LaunchMonitor) {
let _ = LAUNCH_MONITOR.set(monitor);
}

pub fn launch_monitor() -> LaunchMonitor {
LAUNCH_MONITOR.get().copied().unwrap_or_default()
}

fn monitor_info() -> MONITORINFO {
MONITORINFO {
cbSize: size_of::<MONITORINFO>() as u32,
..Default::default()
}
}

fn centered_origin(work: RECT, width: i32, height: i32) -> (i32, i32) {
(
work.left + ((work.right - work.left - width) / 2).max(0),
work.top + ((work.bottom - work.top - height) / 2).max(0),
)
}

/// Center on the remembered monitor's work area; use primary if it disappeared.
pub fn center(hwnd: HWND) {
center_on(hwnd, launch_monitor());
}

fn center_on(hwnd: HWND, point: LaunchMonitor) {
unsafe {
let monitor = MonitorFromPoint(
POINT {
x: point.x,
y: point.y,
},
MONITOR_DEFAULTTOPRIMARY,
);
let mut info = monitor_info();
if !GetMonitorInfoW(monitor, &mut info).as_bool() {
return;
}
// A cross-monitor move can synchronously resize for DPI. Re-read the
// final size and center again, without activating or repeatedly pinning UI.
for _ in 0..2 {
let mut rect = RECT::default();
if GetWindowRect(hwnd, &mut rect).is_err() {
return;
}
let (x, y) =
centered_origin(info.rcWork, rect.right - rect.left, rect.bottom - rect.top);
if rect.left == x && rect.top == y {
break;
}
let _ = SetWindowPos(
hwnd,
None,
x,
y,
0,
0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE,
);
}
}
}

#[cfg(test)]
mod tests;
Loading
Loading