mirror of
https://github.com/ivall/xcl.git
synced 2026-11-24 05:14:57 +01:00
XCL window: window_pos setting – center on the primary monitor or a clamped x,y
The window was created with CW_USEDEFAULT for its position, so on a multi-monitor desktop Windows may place it on any monitor (observed: a 4-monitor setup, the window landed on the narrow portrait side monitor). window_pos (XCL_WINDOW_POS overrides): auto keeps the old behavior, center centers on the primary work area, x,y clamps into the nearest monitor work area. Test: window_position_modes covers all modes, the clamp, and the env override.
This commit is contained in:
@@ -56,6 +56,10 @@ pub struct Config {
|
||||
pub raytracing: String,
|
||||
pub window_width: u32,
|
||||
pub window_height: u32,
|
||||
/// The XCL window's position: "auto" (CW_USEDEFAULT – Windows may place it on any monitor),
|
||||
/// "center" (the primary monitor's work area), or "x,y" (top-left of the window, screen
|
||||
/// coordinates; clamped into the nearest monitor's work area). `XCL_WINDOW_POS` overrides.
|
||||
pub window_pos: String,
|
||||
/// Answer `XUserGetTokenAndSignature*` with XCL's local placeholder token (for a backend of
|
||||
/// your own that accepts it). Off: the request fails as on a console that cannot reach Xbox
|
||||
/// Live (0x80072EFD), and titles go their offline way – Xbox Live's and the titles' own
|
||||
@@ -169,6 +173,7 @@ impl Default for Config {
|
||||
raytracing: "auto".into(),
|
||||
window_width: 1280,
|
||||
window_height: 720,
|
||||
window_pos: "auto".into(),
|
||||
xbox_live_tokens: false,
|
||||
keyboard_mouse: true,
|
||||
mouse_sensitivity: 1.0,
|
||||
|
||||
@@ -22,10 +22,11 @@ use std::sync::{Mutex, Once};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use windows::core::{Interface, HSTRING, PCWSTR};
|
||||
use windows::Win32::Foundation::{CloseHandle, HANDLE, HWND, LPARAM, LRESULT, RECT, WPARAM};
|
||||
use windows::Win32::Foundation::{CloseHandle, HANDLE, HWND, LPARAM, LRESULT, POINT, RECT, WPARAM};
|
||||
use windows::Win32::Graphics::Direct3D12::*;
|
||||
use windows::Win32::Graphics::Dxgi::Common::*;
|
||||
use windows::Win32::Graphics::Dxgi::*;
|
||||
use windows::Win32::Graphics::Gdi::{GetMonitorInfoW, MonitorFromPoint, MONITORINFO, MONITOR_DEFAULTTONEAREST, MONITOR_DEFAULTTOPRIMARY, MONITOR_FROM_FLAGS};
|
||||
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
|
||||
use windows::Win32::System::Threading::CreateEventW;
|
||||
use windows::Win32::UI::WindowsAndMessaging::*;
|
||||
@@ -392,15 +393,17 @@ fn window_thread() {
|
||||
let title = window_name();
|
||||
let mut rc = RECT { left: 0, top: 0, right: cfg.window_width as i32, bottom: cfg.window_height as i32 };
|
||||
let _ = AdjustWindowRect(&mut rc, WS_OVERLAPPEDWINDOW, false);
|
||||
let (w, h) = (rc.right - rc.left, rc.bottom - rc.top);
|
||||
let (x, y) = window_position(&cfg.window_pos, w, h);
|
||||
let hwnd = CreateWindowExW(
|
||||
WINDOW_EX_STYLE(0),
|
||||
class,
|
||||
&HSTRING::from(format!("XCL – {title}")),
|
||||
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
|
||||
CW_USEDEFAULT,
|
||||
CW_USEDEFAULT,
|
||||
rc.right - rc.left,
|
||||
rc.bottom - rc.top,
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
None,
|
||||
None,
|
||||
Some(inst.into()),
|
||||
@@ -422,6 +425,45 @@ fn window_thread() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The XCL window's top-left from `window_pos` (`XCL_WINDOW_POS` overrides): "auto" keeps
|
||||
/// CW_USEDEFAULT (Windows may pick any monitor), "center" centers on the primary monitor's
|
||||
/// work area, "x,y" is clamped into the nearest monitor's work area — a multi-monitor desktop
|
||||
/// otherwise gets the window on whichever monitor Windows' cascade picks.
|
||||
unsafe fn window_position(setting: &str, w: i32, h: i32) -> (i32, i32) {
|
||||
let env = std::env::var("XCL_WINDOW_POS").ok();
|
||||
let mode = env.as_deref().unwrap_or(setting).trim();
|
||||
if mode.is_empty() || mode.eq_ignore_ascii_case("auto") {
|
||||
return (CW_USEDEFAULT, CW_USEDEFAULT);
|
||||
}
|
||||
let work = |pt: POINT, flags: u32| -> Option<RECT> {
|
||||
let mon = MonitorFromPoint(pt, MONITOR_FROM_FLAGS(flags));
|
||||
if mon.is_invalid() {
|
||||
return None;
|
||||
}
|
||||
let mut mi = MONITORINFO { cbSize: std::mem::size_of::<MONITORINFO>() as u32, ..Default::default() };
|
||||
GetMonitorInfoW(mon, &mut mi).as_bool().then_some(mi.rcWork)
|
||||
};
|
||||
if mode.eq_ignore_ascii_case("center") {
|
||||
let Some(r) = work(POINT { x: 0, y: 0 }, MONITOR_DEFAULTTOPRIMARY.0) else {
|
||||
return (CW_USEDEFAULT, CW_USEDEFAULT);
|
||||
};
|
||||
return (r.left + ((r.right - r.left) - w).max(0) / 2, r.top + ((r.bottom - r.top) - h).max(0) / 2);
|
||||
}
|
||||
let Some((xs, ys)) = mode.split_once(',') else {
|
||||
crate::xlog_warn!("window_pos: expected \"auto\", \"center\" or \"x,y\", got {mode:?} – using auto");
|
||||
return (CW_USEDEFAULT, CW_USEDEFAULT);
|
||||
};
|
||||
let (Ok(x), Ok(y)) = (xs.trim().parse::<i32>(), ys.trim().parse::<i32>()) else {
|
||||
crate::xlog_warn!("window_pos: expected \"auto\", \"center\" or \"x,y\", got {mode:?} – using auto");
|
||||
return (CW_USEDEFAULT, CW_USEDEFAULT);
|
||||
};
|
||||
// clamp into the work area of the monitor nearest the requested top-left
|
||||
let Some(r) = work(POINT { x, y }, MONITOR_DEFAULTTONEAREST.0) else {
|
||||
return (x, y);
|
||||
};
|
||||
(x.clamp(r.left, (r.right - w).max(r.left)), y.clamp(r.top, (r.bottom - h).max(r.top)))
|
||||
}
|
||||
|
||||
fn ensure_window() -> Option<HWND> {
|
||||
static START: Once = Once::new();
|
||||
START.call_once(|| {
|
||||
@@ -887,3 +929,43 @@ impl Drop for Presenter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// XCL_WINDOW_POS is process-global; one test covers all modes in sequence.
|
||||
#[test]
|
||||
fn window_position_modes() {
|
||||
unsafe {
|
||||
std::env::remove_var("XCL_WINDOW_POS");
|
||||
assert_eq!(window_position("auto", 1280, 720), (CW_USEDEFAULT, CW_USEDEFAULT));
|
||||
assert_eq!(window_position("", 1280, 720), (CW_USEDEFAULT, CW_USEDEFAULT));
|
||||
assert_eq!(window_position("nonsense", 1280, 720), (CW_USEDEFAULT, CW_USEDEFAULT));
|
||||
assert_eq!(window_position("12,abc", 1280, 720), (CW_USEDEFAULT, CW_USEDEFAULT));
|
||||
|
||||
let (cx, cy) = window_position("center", 1280, 720);
|
||||
if cx != CW_USEDEFAULT {
|
||||
// centered on the primary work area: inside it, and not off its left/top
|
||||
let mi = {
|
||||
let mon = MonitorFromPoint(POINT { x: 0, y: 0 }, MONITOR_DEFAULTTOPRIMARY);
|
||||
let mut mi = MONITORINFO { cbSize: std::mem::size_of::<MONITORINFO>() as u32, ..Default::default() };
|
||||
assert!(GetMonitorInfoW(mon, &mut mi).as_bool());
|
||||
mi
|
||||
};
|
||||
assert!(cx >= mi.rcWork.left && cy >= mi.rcWork.top);
|
||||
assert!(cx + 1280 <= mi.rcWork.right || mi.rcWork.right - mi.rcWork.left < 1280);
|
||||
}
|
||||
|
||||
// a position far outside every monitor clamps into the nearest work area
|
||||
let (x, y) = window_position("100000,100000", 1280, 720);
|
||||
let mon = MonitorFromPoint(POINT { x, y }, MONITOR_DEFAULTTONEAREST);
|
||||
assert!(!mon.is_invalid());
|
||||
|
||||
// the env override wins over the setting
|
||||
std::env::set_var("XCL_WINDOW_POS", "center");
|
||||
assert_eq!(window_position("auto", 1280, 720), window_position("center", 1280, 720));
|
||||
std::env::remove_var("XCL_WINDOW_POS");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user