Don't hide inline assist when editor loses focus (#12990)

Release Notes:

- Now when an editor loses focus (e.g. from switching tabs) and then
gains focus again, it doesn't close the inline assist. Instead, it only
closes when you move the cursor outside of it, e.g. by clicking
somewhere else in its parent editor.

---------

Co-authored-by: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Richard Feldman 2024-06-17 03:43:52 -04:00 committed by GitHub
parent 15d3e54ae3
commit 4855da53df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 90 additions and 81 deletions

View file

@ -85,13 +85,20 @@ impl DispatchPhase {
type AnyObserver = Box<dyn FnMut(&mut WindowContext) -> bool + 'static>;
type AnyWindowFocusListener = Box<dyn FnMut(&FocusEvent, &mut WindowContext) -> bool + 'static>;
type AnyWindowFocusListener =
Box<dyn FnMut(&WindowFocusEvent, &mut WindowContext) -> bool + 'static>;
struct FocusEvent {
struct WindowFocusEvent {
previous_focus_path: SmallVec<[FocusId; 8]>,
current_focus_path: SmallVec<[FocusId; 8]>,
}
/// This is provided when subscribing for `ViewContext::on_focus_out` events.
pub struct FocusOutEvent {
/// A weak focus handle representing what was blurred.
pub blurred: WeakFocusHandle,
}
slotmap::new_key_type! {
/// A globally unique identifier for a focusable element.
pub struct FocusId;
@ -1397,7 +1404,7 @@ impl<'a> WindowContext<'a> {
.retain(&(), |listener| listener(self));
}
let event = FocusEvent {
let event = WindowFocusEvent {
previous_focus_path: if previous_window_active {
previous_focus_path
} else {
@ -4055,6 +4062,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
}
/// Register a listener to be called when the given focus handle or one of its descendants receives focus.
/// This does not fire if the given focus handle - or one of its descendants - was previously focused.
/// Returns a subscription and persists until the subscription is dropped.
pub fn on_focus_in(
&mut self,
@ -4124,17 +4132,25 @@ impl<'a, V: 'static> ViewContext<'a, V> {
pub fn on_focus_out(
&mut self,
handle: &FocusHandle,
mut listener: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
mut listener: impl FnMut(&mut V, FocusOutEvent, &mut ViewContext<V>) + 'static,
) -> Subscription {
let view = self.view.downgrade();
let focus_id = handle.id;
let (subscription, activate) =
self.window.new_focus_listener(Box::new(move |event, cx| {
view.update(cx, |view, cx| {
if event.previous_focus_path.contains(&focus_id)
&& !event.current_focus_path.contains(&focus_id)
{
listener(view, cx)
if let Some(blurred_id) = event.previous_focus_path.last().copied() {
if event.previous_focus_path.contains(&focus_id)
&& !event.current_focus_path.contains(&focus_id)
{
let event = FocusOutEvent {
blurred: WeakFocusHandle {
id: blurred_id,
handles: Arc::downgrade(&cx.window.focus_handles),
},
};
listener(view, event, cx)
}
}
})
.is_ok()
@ -4193,7 +4209,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
});
}
/// Emit an event to be handled any other views that have subscribed via [ViewContext::subscribe].
/// Emit an event to be handled by any other views that have subscribed via [ViewContext::subscribe].
pub fn emit<Evt>(&mut self, event: Evt)
where
Evt: 'static,