windows: Implement `restart` action (#10234)

张小白 created

Release Notes:

- N/A

Change summary

crates/gpui/src/platform/windows/platform.rs | 35 +++++++++++++++++++--
1 file changed, 31 insertions(+), 4 deletions(-)

Detailed changes

crates/gpui/src/platform/windows/platform.rs 🔗

@@ -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 {