diff --git a/crates/gpui/src/platform/windows/platform.rs b/crates/gpui/src/platform/windows/platform.rs index bb65163e09..83e56ad3ae 100644 --- a/crates/gpui/src/platform/windows/platform.rs +++ b/crates/gpui/src/platform/windows/platform.rs @@ -295,6 +295,18 @@ impl WindowsPlatform { .log_err() .unwrap_or_default() } + + fn find_current_active_window(&self) -> Option { + let active_window_hwnd = unsafe { GetActiveWindow() }; + if active_window_hwnd.is_invalid() { + return None; + } + self.raw_window_handles + .read() + .iter() + .find(|&&hwnd| hwnd == active_window_hwnd) + .copied() + } } impl Platform for WindowsPlatform { @@ -473,9 +485,10 @@ impl Platform for WindowsPlatform { options: PathPromptOptions, ) -> Receiver>>> { let (tx, rx) = oneshot::channel(); + let window = self.find_current_active_window(); self.foreground_executor() .spawn(async move { - let _ = tx.send(file_open_dialog(options)); + let _ = tx.send(file_open_dialog(options, window)); }) .detach(); @@ -485,9 +498,10 @@ impl Platform for WindowsPlatform { fn prompt_for_new_path(&self, directory: &Path) -> Receiver>> { let directory = directory.to_owned(); let (tx, rx) = oneshot::channel(); + let window = self.find_current_active_window(); self.foreground_executor() .spawn(async move { - let _ = tx.send(file_save_dialog(directory)); + let _ = tx.send(file_save_dialog(directory, window)); }) .detach(); @@ -754,7 +768,10 @@ fn open_target_in_explorer(target: &str) { } } -fn file_open_dialog(options: PathPromptOptions) -> Result>> { +fn file_open_dialog( + options: PathPromptOptions, + window: Option, +) -> Result>> { let folder_dialog: IFileOpenDialog = unsafe { CoCreateInstance(&FileOpenDialog, None, CLSCTX_ALL)? }; @@ -768,7 +785,7 @@ fn file_open_dialog(options: PathPromptOptions) -> Result>> unsafe { folder_dialog.SetOptions(dialog_options)?; - if folder_dialog.Show(None).is_err() { + if folder_dialog.Show(window).is_err() { // User cancelled return Ok(None); } @@ -790,7 +807,7 @@ fn file_open_dialog(options: PathPromptOptions) -> Result>> Ok(Some(paths)) } -fn file_save_dialog(directory: PathBuf) -> Result> { +fn file_save_dialog(directory: PathBuf, window: Option) -> Result> { let dialog: IFileSaveDialog = unsafe { CoCreateInstance(&FileSaveDialog, None, CLSCTX_ALL)? }; if !directory.to_string_lossy().is_empty() { if let Some(full_path) = directory.canonicalize().log_err() { @@ -806,7 +823,7 @@ fn file_save_dialog(directory: PathBuf) -> Result> { pszName: windows::core::w!("All files"), pszSpec: windows::core::w!("*.*"), }])?; - if dialog.Show(None).is_err() { + if dialog.Show(window).is_err() { // User cancelled return Ok(None); }