feat: add Minecraft launch transition animation (cover/logo phases)

This commit is contained in:
Xiao-no-love
2026-09-15 22:33:18 +08:00
parent bd09fed0ed
commit 776f621436
8 changed files with 1301 additions and 406 deletions

View File

@ -0,0 +1 @@
<svg height="388" width="2500" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 375.103901073386 57.671"><path d="M57.355.316H16.519C7.396.316 0 7.713 0 16.834V57.671h40.836c9.124 0 16.519-7.396 16.519-16.518V7.38z" fill="#db1f29"/><path d="M13.772 48.104c-4.892 0-4.892-4.892-4.892-4.892V18.746c0-4.893 4.892-4.893 4.892-4.893h9.786c4.894 0 9.786 4.893 9.786 4.893s4.894 0 0 0c0-14.251 4.501 5.285 9.786 0 3.46-3.459 4.893 17.125 4.893 17.125l-4.893-2.444s0-4.894-7.339-9.786c-9.104-6.071-19.572-2.071-19.572 7.338 0 19.196 31.804 12.233 31.804 12.233s0 4.892-4.893 4.892H13.772z" fill="#fff"/><path d="M38.238 13.854c0-1.35 1.095-7.34 2.445-7.34 1.352 0 2.446 5.99 2.446 7.34a2.445 2.445 0 1 1-4.891 0z" fill="#fff"/><path d="M105.714 56.675V21.379L93.342 39.77H87.85L75.479 21.379v35.296h-9.85V.996h7.899l17.109 25.186L107.747.996h7.745v55.679zM149.727 57.671c-7.744 0-14.943-3.021-20.271-8.507-5.375-5.545-8.333-12.763-8.333-20.325 0-7.57 2.959-14.788 8.333-20.325C134.783 3.101 142.169 0 149.727 0c7.636 0 14.858 3.02 20.337 8.502 5.384 5.549 8.343 12.767 8.343 20.336 0 7.562-2.958 14.781-8.332 20.325-5.412 5.489-12.636 8.508-20.348 8.508zm0-47.666c-4.919 0-9.567 1.979-13.088 5.574-3.56 3.484-5.512 8.188-5.512 13.26 0 5.069 1.952 9.77 5.496 13.237 3.531 3.61 8.179 5.59 13.104 5.59 4.947 0 9.774-2.031 13.242-5.573 3.506-3.51 5.433-8.215 5.433-13.254 0-5.042-1.927-9.75-5.426-13.252-3.48-3.55-8.307-5.582-13.249-5.582zM180.215 56.675v-9.396h1.062c16.254 0 19.52-5.682 19.52-14.848V10.236h-20.582V1.071h30.432v31.36c0 16.993-8.783 24.243-29.369 24.243h-1.063zM253.239 56.675l-5.128-13.543h-20.26l-5.204 13.543h-10.633L234.354.312h7.106l22.34 56.363zm-8.762-22.931l-6.491-17.066-6.491 17.066zM301.564 56.675l-24.237-35.068v35.068h-9.853V.996h7.588l24.467 35.376V.996h9.776v55.679zM343.234 57.671c-7.744 0-14.943-3.021-20.271-8.506-5.375-5.546-8.334-12.764-8.334-20.326 0-7.57 2.959-14.788 8.332-20.325C328.29 3.101 335.677 0 343.234 0c7.634 0 14.857 3.02 20.337 8.502a29.986 29.986 0 0 1 4.891 6.671l.842 1.566h-11.782l-.501-.563a12.058 12.058 0 0 0-.539-.591c-3.479-3.549-8.305-5.58-13.247-5.58-4.919 0-9.567 1.979-13.089 5.574-3.559 3.484-5.511 8.188-5.511 13.26 0 5.069 1.952 9.77 5.496 13.237 3.53 3.61 8.179 5.59 13.104 5.59 4.947 0 9.773-2.031 13.24-5.573a18.397 18.397 0 0 0 4.85-8.598h-20.991V23.49h31.095l.14.899c.229 1.469.347 2.965.347 4.45 0 7.562-2.959 14.78-8.331 20.325-5.415 5.488-12.639 8.507-20.351 8.507z"/></svg>

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -373,6 +373,12 @@ let allowWindowClose = false
let unlistenCloseRequested: (() => void) | undefined
let unlistenLightweightModeError: (() => void) | undefined
let unlistenSystemAccentColor: (() => void) | undefined
let unlistenMcTransitionFadeout: (() => void) | undefined
let unlistenMcTransitionFocus: (() => void) | undefined
let unlistenMcTransitionCover: (() => void) | undefined
let unlistenMcTransitionLogoIn: (() => void) | undefined
let unlistenMcTransitionLogoOut: (() => void) | undefined
let unlistenMcTransitionReset: (() => void) | undefined
let maximizedStateTimer: ReturnType<typeof setTimeout> | undefined
let unlistenWindowResize: (() => void) | undefined
const minecraftCrashModal = ref()
@ -491,6 +497,104 @@ onMounted(async () => {
checkUpdates()
void warnIfRunningElevated()
startDirectLinkSync()
unlistenMcTransitionFadeout = await listen('mc-transition-fadeout', async () => {
// 游戏窗口已就位、启动器已缩放到同尺寸。这里做 0.5s CSS 淡出,
// 完成后回调 Rust由后端执行聚焦游戏 + 最小化启动器。
const root = document.getElementById('app')
if (root) {
root.style.transition = 'opacity 500ms ease-out'
root.style.opacity = '0'
}
window.setTimeout(() => {
void invoke('mc_transition_fade_done').catch(() => {})
}, 510)
})
// 窗口恢复可见时,清掉残留的淡出透明度,避免再次打开时全透明。
const resetLauncherOpacity = () => {
const root = document.getElementById('app')
if (root) {
root.style.transition = 'none'
root.style.opacity = ''
void root.offsetHeight
}
}
window.addEventListener('focus', resetLauncherOpacity)
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') resetLauncherOpacity()
})
unlistenMcTransitionFocus = await getCurrentWindow().onFocusChanged(({ payload: focused }) => {
if (focused) resetLauncherOpacity()
})
// ============ 启动过渡遮罩(三阶段) ============
// 阶段1纯色遮罩主题色无图—— 盖住放大抖动
// 阶段2全屏后渐显 SVG logo
// 阶段3缩小前背景变 Mojang 红 + 渐隐 logo
let mcCoverEl = null
let mcCoverImg = null
unlistenMcTransitionCover = await listen('mc-transition-cover-show', () => {
const app = document.getElementById('app')
if (!app) return
if (!mcCoverEl) {
mcCoverEl = document.createElement('div')
mcCoverEl.id = 'mc-transition-cover'
mcCoverEl.style.cssText = [
'position: fixed',
'inset: 0',
'z-index: 999999',
'display: flex',
'align-items: center',
'justify-content: center',
'background: var(--color-brand, #db1f29)',
'transition: background-color 500ms ease, opacity 300ms ease-out',
'opacity: 0',
'pointer-events: none',
].join(';')
mcCoverImg = document.createElement('img')
mcCoverImg.src = '/mojang-logo.svg'
mcCoverImg.alt = ''
mcCoverImg.style.cssText =
'width: 42%; max-width: 640px; height: auto; opacity: 0; transition: opacity 500ms ease-out;'
mcCoverEl.appendChild(mcCoverImg)
app.appendChild(mcCoverEl)
}
requestAnimationFrame(() => {
mcCoverEl.style.opacity = '1'
})
window.setTimeout(() => {
void invoke('mc_transition_cover_ready').catch(() => {})
}, 320)
})
unlistenMcTransitionLogoIn = await listen('mc-transition-logo-in', () => {
if (mcCoverImg) mcCoverImg.style.opacity = '1'
window.setTimeout(() => {
void invoke('mc_transition_logo_in_done').catch(() => {})
}, 520)
})
unlistenMcTransitionLogoOut = await listen('mc-transition-logo-out', () => {
if (mcCoverEl) mcCoverEl.style.backgroundColor = '#db1f29'
if (mcCoverImg) mcCoverImg.style.opacity = '0'
window.setTimeout(() => {
void invoke('mc_transition_logo_out_done').catch(() => {})
}, 520)
})
unlistenMcTransitionReset = await listen('mc-transition-reset-opacity', () => {
const app = document.getElementById('app')
if (app) {
app.style.transition = 'none'
app.style.opacity = ''
}
if (mcCoverEl) {
mcCoverEl.remove()
mcCoverEl = null
mcCoverImg = null
}
})
})
let directLinkSync: (() => Promise<void>) | undefined
@ -567,6 +671,12 @@ onUnmounted(async () => {
window.removeEventListener('keydown', handleGlobalKeydown, true)
unlistenCloseRequested?.()
unlistenLightweightModeError?.()
unlistenMcTransitionFadeout?.()
unlistenMcTransitionFocus?.()
unlistenMcTransitionCover?.()
unlistenMcTransitionLogoIn?.()
unlistenMcTransitionLogoOut?.()
unlistenMcTransitionReset?.()
unlistenSystemAccentColor?.()
document.querySelector('body').removeEventListener('click', handleClick)
document.querySelector('body').removeEventListener('auxclick', handleAuxClick)

View File

@ -92,6 +92,7 @@ features = [
"Foundation",
"UI_ViewManagement",
"Win32_Graphics_Dwm",
"Win32_Graphics_Gdi",
"Win32_System_Com",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",

View File

@ -253,29 +253,22 @@ impl LightweightMode {
if payload.maximize_window {
maximize_minecraft_window(payload.pid).await;
}
let settings = match theseus::settings::get().await {
Ok(settings) => settings,
Err(error) => {
tracing::warn!(
"Failed to read lightweight mode setting: {error}"
);
return;
}
};
if settings.enter_lightweight_mode_on_game_launch {
let state = app.state::<LightweightMode>();
if let Err(error) = state.enter(&app) {
tracing::warn!(
"Failed to enter lightweight mode: {error}"
);
}
} else if settings.hide_on_process_start
&& let Some(window) =
app.get_webview_window(MAIN_WINDOW_LABEL)
&& let Err(error) = window.minimize()
{
// 窗口过渡:把启动器全屏遮罩 → 缩放 → 淡出 → 最小化
crate::mc_transition::run(
&app,
payload.pid,
payload.maximize_window,
)
.await;
// 过渡动画结束后,统一进入轻量模式(隐藏到托盘)。
// 由过渡流程接管,不再各自判断用户的「轻量模式 /
// 启动后隐藏」设置,避免与动画收尾冲突,也顺带修掉
// 「重开时窗口全透明」的问题(轻量模式恢复时是重建
// 全新窗口,不残留旧的 opacity
let state = app.state::<LightweightMode>();
if let Err(error) = state.enter(&app) {
tracing::warn!(
"Failed to minimize launcher after Minecraft started: {error}"
"Failed to enter lightweight mode after transition: {error}"
);
}
});

View File

@ -22,6 +22,7 @@ use theseus::prelude::*;
mod api;
mod error;
mod lightweight_mode;
mod mc_transition;
mod mod_translation;
mod portable;
mod seed_map;
@ -867,6 +868,10 @@ fn main() {
lightweight_mode::lightweight_mode_frontend_ready,
lightweight_mode::lightweight_mode_set_route,
lightweight_mode::lightweight_mode_enter,
mc_transition::mc_transition_fade_done,
mc_transition::mc_transition_cover_ready,
mc_transition::mc_transition_logo_in_done,
mc_transition::mc_transition_logo_out_done,
]);
tracing::info!("Initializing app...");

View File

@ -0,0 +1,470 @@
//! Minecraft 启动时的窗口过渡动画(仅 Windows 生效)。
//!
//! 流程(四阶段):
//! 阶段A启动器放大到全屏1s 动画),全程置顶 + 聚焦;
//! 阶段B等待 MC 窗口出现(最多 60s期间启动器盖住游戏
//! 阶段C获取 MC 窗口位置/大小1s 动画缩放到相同大小;
//! 阶段D大小一致后0.5s 淡出(不与缩放同步);
//! 阶段E聚焦游戏窗口 → 启动器最小化。
//!
//! 过渡结束后返回,由调用方继续执行用户的轻量模式 / 隐藏设置。
//!
//! 注意HWND 是裸指针,非 Send不能跨 await 持有,因此一律用 `usize`
//! 保存窗口句柄,只在同步的 win32 调用里临时转回 HWND。
#![cfg_attr(not(target_os = "windows"), allow(dead_code, unused_imports))]
use tauri::{AppHandle, Emitter, Manager, WebviewWindow};
/// 主窗口 label与 lightweight_mode.rs 保持一致。
pub const MAIN_WINDOW_LABEL: &str = "main";
/// 等待 MC 窗口出现的轮询次数与间隔120 × 500ms = 60s
const FIND_RETRIES: u32 = 120;
const FIND_INTERVAL_MS: u64 = 500;
/// 缩放动画总时长与帧数1000ms / 50 帧 = 20ms 一帧)。
const ANIM_DURATION_MS: u64 = 400;
const ANIM_STEPS: u32 = 50;
/// 等待前端完成 CSS 淡出的超时兜底(毫秒)。淡出 500ms留足余量。
const FADE_TIMEOUT_MS: u64 = 700;
#[cfg(target_os = "windows")]
static FADE_DONE: std::sync::Mutex<
Option<tokio::sync::oneshot::Sender<()>>,
> = std::sync::Mutex::new(None);
#[cfg(target_os = "windows")]
static COVER_DONE: std::sync::Mutex<
Option<tokio::sync::oneshot::Sender<()>>,
> = std::sync::Mutex::new(None);
#[cfg(target_os = "windows")]
static LOGO_IN_DONE: std::sync::Mutex<
Option<tokio::sync::oneshot::Sender<()>>,
> = std::sync::Mutex::new(None);
#[cfg(target_os = "windows")]
static LOGO_OUT_DONE: std::sync::Mutex<
Option<tokio::sync::oneshot::Sender<()>>,
> = std::sync::Mutex::new(None);
/// 调试日志:直接追加到用户目录下固定文件,绕过 tracing 配置。
/// TODO(上线前清理):连同所有 `dbg(...)` 调用一起删除。
fn dbg(msg: &str) {
use std::io::Write;
let base =
std::env::var("USERPROFILE").unwrap_or_else(|_| "C:\\".to_string());
let path = format!("{base}\\mc_transition_debug.log");
if let Ok(mut f) =
std::fs::OpenOptions::new().create(true).append(true).open(path)
{
let _ = writeln!(f, "{msg}");
}
}
/// 前端完成淡出动画后的回调命令。
#[tauri::command]
pub fn mc_transition_fade_done() {
#[cfg(target_os = "windows")]
{
dbg("fade_done callback");
if let Ok(mut guard) = FADE_DONE.lock()
&& let Some(tx) = guard.take()
{
let _ = tx.send(());
}
}
}
/// 前端覆盖层渐显完成后的回调命令。
#[tauri::command]
pub fn mc_transition_cover_ready() {
#[cfg(target_os = "windows")]
{
dbg("cover_ready callback");
if let Ok(mut guard) = COVER_DONE.lock()
&& let Some(tx) = guard.take()
{
let _ = tx.send(());
}
}
}
/// 前端 SVG logo 渐显完成后的回调命令。
#[tauri::command]
pub fn mc_transition_logo_in_done() {
#[cfg(target_os = "windows")]
{
dbg("logo_in_done callback");
if let Ok(mut guard) = LOGO_IN_DONE.lock()
&& let Some(tx) = guard.take()
{
let _ = tx.send(());
}
}
}
/// 前端 SVG logo 渐隐 + 背景变红完成后的回调命令。
#[tauri::command]
pub fn mc_transition_logo_out_done() {
#[cfg(target_os = "windows")]
{
dbg("logo_out_done callback");
if let Ok(mut guard) = LOGO_OUT_DONE.lock()
&& let Some(tx) = guard.take()
{
let _ = tx.send(());
}
}
}
/// 缓入缓出曲线,比纯 ease-out 更柔和自然。
#[cfg(target_os = "windows")]
fn ease_in_out(t: f64) -> f64 {
if t < 0.5 {
4.0 * t * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
}
}
/// 分帧移动 + 缩放启动器窗口。`from`/`to` 为 (x, y, w, h)。
/// 若起止相同则直接返回;结束时补一帧精确坐标,避免累积误差。
#[cfg(target_os = "windows")]
async fn animate_resize(
window: &WebviewWindow,
from: (i32, i32, u32, u32),
to: (i32, i32, u32, u32),
duration_ms: u64,
) {
use tauri::{PhysicalPosition, PhysicalSize};
if from == to {
return;
}
let step_ms = (duration_ms / ANIM_STEPS as u64).max(1);
for step in 1..=ANIM_STEPS {
let t = step as f64 / ANIM_STEPS as f64;
let e = ease_in_out(t);
let x = from.0 as f64 + (to.0 - from.0) as f64 * e;
let y = from.1 as f64 + (to.1 - from.1) as f64 * e;
let w = from.2 as f64 + (to.2 as i64 - from.2 as i64) as f64 * e;
let h = from.3 as f64 + (to.3 as i64 - from.3 as i64) as f64 * e;
let _ =
window.set_position(PhysicalPosition::new(x as i32, y as i32));
let _ = window.set_size(PhysicalSize::new(
w.max(1.0) as u32,
h.max(1.0) as u32,
));
tokio::time::sleep(std::time::Duration::from_millis(step_ms)).await;
}
// 补最后一帧精确值
let _ = window.set_position(PhysicalPosition::new(to.0, to.1));
let _ = window.set_size(PhysicalSize::new(to.2.max(1), to.3.max(1)));
}
pub async fn run(app: &AppHandle, pid: u32, _maximize: bool) {
if pid == 0 {
dbg("run() aborted: pid=0");
return;
}
let Some(window) = app.get_webview_window(MAIN_WINDOW_LABEL) else {
dbg("run() aborted: main window not found");
return;
};
dbg(&format!("run() invoked pid={pid}"));
// 记录启动器当前矩形作为起点
let start = {
let p = window.outer_position().map(|p| (p.x, p.y)).unwrap_or((0, 0));
let s =
window.outer_size().map(|s| (s.width, s.height)).unwrap_or((1280, 800));
(p.0, p.1, s.0, s.1)
};
dbg(&format!("launcher start rect = {start:?}"));
// 全屏目标 = 启动器当前所在显示器的完整矩形
let fullscreen = match window
.current_monitor()
.ok()
.flatten()
.or_else(|| window.primary_monitor().ok().flatten())
{
Some(m) => {
(m.position().x, m.position().y, m.size().width, m.size().height)
}
None => {
dbg("no monitor info, abort");
return;
}
};
dbg(&format!("fullscreen target = {fullscreen:?}"));
// 全程置顶 + 聚焦
let _ = window.set_always_on_top(true);
let _ = window.set_focus();
// ---------- 第①步:显示纯色遮罩(无图,主题色) ----------
dbg("step 1: show plain cover");
let (cover_tx, cover_rx) = tokio::sync::oneshot::channel::<()>();
if let Ok(mut guard) = COVER_DONE.lock() {
*guard = Some(cover_tx);
}
let _ = app.emit("mc-transition-cover-show", ());
let covered = tokio::time::timeout(
std::time::Duration::from_millis(1000),
cover_rx,
)
.await;
dbg(&format!("step 1: cover ready ok={}", covered.is_ok()));
// ---------- 第②步放大到全屏1s ----------
dbg("step 2: expand to fullscreen");
animate_resize(&window, start, fullscreen, ANIM_DURATION_MS).await;
dbg("step 2 done");
// ---------- 第③步:全屏后,渐显 SVG logo ----------
dbg("step 3: fade in logo");
let (lin_tx, lin_rx) = tokio::sync::oneshot::channel::<()>();
if let Ok(mut guard) = LOGO_IN_DONE.lock() {
*guard = Some(lin_tx);
}
let _ = app.emit("mc-transition-logo-in", ());
let logo_in = tokio::time::timeout(
std::time::Duration::from_millis(1500),
lin_rx,
)
.await;
dbg(&format!("step 3: logo in done ok={}", logo_in.is_ok()));
// ---------- 等待 MC 窗口出现(最多 60s ----------
dbg("waiting for MC window");
let mut mc_raw: usize = 0;
for i in 0..FIND_RETRIES {
if let Some(raw) = win_impl::find_window(pid) {
mc_raw = raw;
dbg(&format!("MC window found at try#{i} hwnd={raw:#x}"));
break;
}
if i % 4 == 0 {
dbg(&format!(
"try#{i} pid={pid} candidates: {:?}",
win_impl::dump_pid_windows(pid)
));
}
tokio::time::sleep(std::time::Duration::from_millis(FIND_INTERVAL_MS))
.await;
}
if mc_raw == 0 {
dbg("MC window NOT FOUND -> restore launcher");
let _ = window.set_always_on_top(false);
animate_resize(&window, fullscreen, start, 300).await;
let _ = app.emit("mc-transition-reset-opacity", ());
return;
}
// ---------- 第④步:缩小前,背景变 Mojang 红 + 渐隐 logo ----------
dbg("step 4: turn red + fade out logo");
let (lout_tx, lout_rx) = tokio::sync::oneshot::channel::<()>();
if let Ok(mut guard) = LOGO_OUT_DONE.lock() {
*guard = Some(lout_tx);
}
let _ = app.emit("mc-transition-logo-out", ());
let logo_out = tokio::time::timeout(
std::time::Duration::from_millis(1500),
lout_rx,
)
.await;
dbg(&format!("step 4: logo out done ok={}", logo_out.is_ok()));
// ---------- 第⑤步:缩小到 MC 窗口矩形1s ----------
let mc_rect = win_impl::get_rect(mc_raw).unwrap_or((
fullscreen.0,
fullscreen.1,
fullscreen.2 as i32,
fullscreen.3 as i32,
));
let covers = win_impl::covers_monitor(mc_raw);
dbg(&format!("step 5: MC rect={mc_rect:?} covers_monitor={covers}"));
let target = (
mc_rect.0,
mc_rect.1,
mc_rect.2.max(1) as u32,
mc_rect.3.max(1) as u32,
);
let _ = window.set_always_on_top(true);
let _ = window.set_focus();
animate_resize(&window, fullscreen, target, ANIM_DURATION_MS).await;
dbg("step 5 done");
// ---------- 第⑥步:等待 1s游戏稳定遮罩仍在 ----------
dbg("step 6: settle 1s");
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
dbg("step 6 done");
// ---------- 第⑦步淡出0.5s,含遮罩一起) ----------
dbg("step 7: fade out, waiting frontend");
let (done_tx, done_rx) = tokio::sync::oneshot::channel::<()>();
if let Ok(mut guard) = FADE_DONE.lock() {
*guard = Some(done_tx);
}
let _ = app.emit("mc-transition-fadeout", ());
let faded = tokio::time::timeout(
std::time::Duration::from_millis(FADE_TIMEOUT_MS),
done_rx,
)
.await;
dbg(&format!("step 7: fade wait ok={}", faded.is_ok()));
// ---------- 第⑧步:聚焦游戏 + 最小化启动器 ----------
dbg("step 8: focus game + minimize launcher");
let _ = window.set_always_on_top(false);
win_impl::focus(mc_raw);
tokio::time::sleep(std::time::Duration::from_millis(80)).await;
let _ = window.minimize();
let _ = app.emit("mc-transition-reset-opacity", ());
dbg("step 8 done (launcher minimized)");
}
#[cfg(not(target_os = "windows"))]
pub async fn run(_app: &AppHandle, _pid: u32, _maximize: bool) {}
#[cfg(target_os = "windows")]
mod win_impl {
use std::sync::{
Mutex,
atomic::{AtomicU32, AtomicUsize, Ordering},
};
use windows::Win32::Foundation::{HWND, LPARAM, RECT};
use windows::Win32::Graphics::Gdi::{
GetMonitorInfoW, MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromWindow,
};
use windows::Win32::UI::WindowsAndMessaging::{
EnumWindows, GetWindowRect, GetWindowThreadProcessId, IsWindowVisible,
SetForegroundWindow,
};
use windows::core::BOOL;
/// 目标进程 pid枚举回调通过静态变量传递
static TARGET_PID: AtomicU32 = AtomicU32::new(0);
/// 命中的窗口句柄,以 usize 存储HWND 非 Send/Sync
static FOUND_HWND: AtomicUsize = AtomicUsize::new(0);
/// 串行化枚举,避免并发查找互相覆盖静态变量。
static ENUM_LOCK: Mutex<()> = Mutex::new(());
/// usize → HWND。调用方保证数值来自真实句柄。
fn to_hwnd(raw: usize) -> HWND {
HWND(raw as *mut core::ffi::c_void)
}
unsafe extern "system" fn find_cb(hwnd: HWND, _: LPARAM) -> BOOL {
let mut pid = 0u32;
unsafe { GetWindowThreadProcessId(hwnd, Some(&mut pid)) };
if pid != TARGET_PID.load(Ordering::Relaxed)
|| !unsafe { IsWindowVisible(hwnd).as_bool() }
{
return BOOL(1);
}
let mut rect = RECT::default();
if unsafe { GetWindowRect(hwnd, &mut rect) }.is_ok() {
let w = rect.right - rect.left;
let h = rect.bottom - rect.top;
// 过滤掉工具窗口 / 0 尺寸窗口,只认真正的游戏主窗口
if w > 50 && h > 50 {
FOUND_HWND.store(hwnd.0 as usize, Ordering::Relaxed);
return BOOL(0); // 找到即停止枚举
}
}
BOOL(1)
}
/// 诊断用:列出属于 `pid` 的所有顶层窗口(含不可见/小窗口)。
/// 返回 (hwnd, 宽, 高, 是否可见)。
pub fn dump_pid_windows(pid: u32) -> Vec<(usize, i32, i32, bool)> {
use windows::core::BOOL as B;
static DUMP_PID: AtomicU32 = AtomicU32::new(0);
static DUMP: Mutex<Vec<(usize, i32, i32, bool)>> =
Mutex::new(Vec::new());
unsafe extern "system" fn cb(hwnd: HWND, _: LPARAM) -> B {
let mut p = 0u32;
unsafe { GetWindowThreadProcessId(hwnd, Some(&mut p)) };
if p != DUMP_PID.load(Ordering::Relaxed) {
return B(1);
}
let mut r = RECT::default();
let _ = unsafe { GetWindowRect(hwnd, &mut r) };
let vis = unsafe { IsWindowVisible(hwnd).as_bool() };
if let Ok(mut v) = DUMP.lock() {
v.push((hwnd.0 as usize, r.right - r.left, r.bottom - r.top, vis));
}
B(1)
}
let _g = ENUM_LOCK.lock();
let _g = match _g {
Ok(g) => g,
Err(_) => return Vec::new(),
};
DUMP_PID.store(pid, Ordering::Relaxed);
if let Ok(mut v) = DUMP.lock() {
v.clear();
}
unsafe {
let _ = EnumWindows(Some(cb), LPARAM(0));
}
DUMP.lock().map(|v| v.clone()).unwrap_or_default()
}
/// 查找属于 `pid` 的可见顶层窗口返回句柄原始值0 表示未找到)。
pub fn find_window(pid: u32) -> Option<usize> {
let _guard = ENUM_LOCK.lock().ok()?;
TARGET_PID.store(pid, Ordering::Relaxed);
FOUND_HWND.store(0, Ordering::Relaxed);
unsafe {
let _ = EnumWindows(Some(find_cb), LPARAM(0));
}
let raw = FOUND_HWND.load(Ordering::Relaxed);
(raw != 0).then_some(raw)
}
/// 取窗口矩形,返回 (x, y, width, height)。
pub fn get_rect(raw: usize) -> Option<(i32, i32, i32, i32)> {
let mut r = RECT::default();
unsafe { GetWindowRect(to_hwnd(raw), &mut r).ok()? };
Some((r.left, r.top, r.right - r.left, r.bottom - r.top))
}
/// 取窗口所在显示器的完整矩形,返回 (x, y, width, height)。
pub fn monitor_rect(raw: usize) -> Option<(i32, i32, i32, i32)> {
let mon =
unsafe { MonitorFromWindow(to_hwnd(raw), MONITOR_DEFAULTTONEAREST) };
let mut mi = MONITORINFO {
cbSize: std::mem::size_of::<MONITORINFO>() as u32,
rcMonitor: RECT::default(),
rcWork: RECT::default(),
dwFlags: 0,
};
if !unsafe { GetMonitorInfoW(mon, &mut mi) }.as_bool() {
return None;
}
let r = mi.rcMonitor;
Some((r.left, r.top, r.right - r.left, r.bottom - r.top))
}
/// 判断窗口是否铺满其所在显示器(全屏 / 无边框全屏 / 最大化)。
pub fn covers_monitor(raw: usize) -> bool {
let (Some((_, _, w, h)), Some((_, _, mw, mh))) =
(get_rect(raw), monitor_rect(raw))
else {
return false;
};
w >= mw && h >= mh
}
/// 把前台焦点交给游戏窗口。
pub fn focus(raw: usize) {
unsafe {
let _ = SetForegroundWindow(to_hwnd(raw));
}
}
}