workspace: Fix captured focus when opening modals (#63512)

# Objective

- This PR fixes the captured focus in modals. 
- Fixes #63480 (and actually this was not specific to the task modal. it
was generic and applied to all modals)

## Solution

- Previously when a modal was opened the `previous_focus_handle` was
stealing the focus because it was captured in `show_modal`. The fix was
to capture it before calling `show_modal` or `reveal_stashed_modal` and
pass it to `show_modal`. That fixes the bug :) (yuhuu).

## Testing

- Tested by running the build and trying to reproduce the bug. I
couldn't reproduce it anymore.
- For testing, a reviewer also needs to test the revelal stashed modal
functionality. I did it with the file finder.

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

---

Release Notes:

- Fixed previous window focus handle not being captured correctly when
opening modals
This commit is contained in:
Matei Oprea authored and GitHub committed 2026-09-02 11:59:16 +00:00
1 parent b1c306668b
commit d1a6db1bc6
1 file changed
+7 -3
+7 -3
View File
@@ -157,15 +157,17 @@ impl ModalLayer {
// Opening a modal explicitly supersedes any reveal that was waiting for the
// layer to become free.
self.reveal_stash_when_free = false;
let mut previous_focus_handle = window.focused(cx);
if let Some(active_modal) = &self.active_modal {
let should_close = active_modal.modal.view().downcast::<V>().is_ok();
previous_focus_handle = active_modal.previous_focus_handle.clone();
let did_close = self.hide_modal(window, cx);
if should_close || !did_close {
return;
}
}
let new_modal = cx.new(|cx| build_view(window, cx));
self.show_modal(Box::new(new_modal), window, cx);
self.show_modal(Box::new(new_modal), previous_focus_handle, window, cx);
cx.emit(ModalOpenedEvent);
}
@@ -174,6 +176,7 @@ impl ModalLayer {
fn show_modal(
&mut self,
modal: Box<dyn ModalViewHandle>,
previous_focus_handle: Option<FocusHandle>,
window: &mut Window,
cx: &mut Context<Self>,
) {
@@ -190,7 +193,7 @@ impl ModalLayer {
}
}),
],
previous_focus_handle: window.focused(cx),
previous_focus_handle,
focus_handle,
});
cx.defer_in(window, move |_, window, cx| {
@@ -214,7 +217,8 @@ impl ModalLayer {
let Some(modal) = self.stashed_modal.take() else {
return false;
};
self.show_modal(modal, window, cx);
let previous_focus_handle = window.focused(cx);
self.show_modal(modal, previous_focus_handle, window, cx);
cx.emit(ModalOpenedEvent);
true
}