feat: center window when out of monitor

This commit is contained in:
GyDi 2023-07-24 20:55:26 +08:00
parent 1443ddfe6c
commit af704681d9
No known key found for this signature in database
GPG Key ID: B28E790B95193457

View File

@ -94,24 +94,42 @@ pub fn create_window(app_handle: &AppHandle) {
.visible(false) .visible(false)
.build() .build()
{ {
Ok(_) => { Ok(win) => {
let app_handle = app_handle.clone(); let center = (|| -> Result<bool> {
let mut center = false;
let monitor = win.current_monitor()?.ok_or(anyhow::anyhow!(""))?;
let size = monitor.size();
let pos = win.outer_position()?;
if let Some(window) = app_handle.get_window("main") { if pos.x < -400
let _ = set_shadow(&window, true); || pos.x > (size.width - 200).try_into()?
|| pos.y < -200
|| pos.y > (size.height - 200).try_into()?
{
center = true;
}
Ok(center)
})();
if center.unwrap_or(true) {
let _ = win.center();
} }
let app_handle = app_handle.clone();
// 加点延迟避免界面闪一下
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
sleep(Duration::from_secs(1)).await; sleep(Duration::from_millis(888)).await;
if let Some(window) = app_handle.get_window("main") { if let Some(window) = app_handle.get_window("main") {
let _ = set_shadow(&window, true);
let _ = window.show(); let _ = window.show();
let _ = window.unminimize(); let _ = window.unminimize();
let _ = window.set_focus(); let _ = window.set_focus();
} }
}); });
} }
Err(err) => log::error!(target: "app", "{err}"), Err(err) => log::error!(target: "app", "create window, {err}"),
} }
} }