windows: Implement restart action (#10234)

Release Notes:

- N/A
This commit is contained in:
张小白 2024-04-10 09:35:10 +08:00 committed by GitHub
parent bfd9bb8a7c
commit c763c8c64b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -275,9 +275,37 @@ impl Platform for WindowsPlatform {
.detach();
}
// todo(windows)
fn restart(&self) {
unimplemented!()
let pid = std::process::id();
let Some(app_path) = self.app_path().log_err() else {
return;
};
let script = format!(
r#"
$pidToWaitFor = {}
$exePath = "{}"
while ($true) {{
$process = Get-Process -Id $pidToWaitFor -ErrorAction SilentlyContinue
if (-not $process) {{
Start-Process -FilePath $exePath
break
}}
Start-Sleep -Seconds 0.1
}}
"#,
pid,
app_path.display(),
);
let restart_process = std::process::Command::new("powershell.exe")
.arg("-command")
.arg(script)
.spawn();
match restart_process {
Ok(_) => self.quit(),
Err(e) => log::error!("failed to spawn restart script: {:?}", e),
}
}
// todo(windows)
@ -621,9 +649,8 @@ impl Platform for WindowsPlatform {
}
}
// todo(windows)
fn app_path(&self) -> Result<PathBuf> {
Err(anyhow!("not yet implemented"))
Ok(std::env::current_exe()?)
}
fn local_timezone(&self) -> UtcOffset {