Re-focus window on workspace on corresponding window blur

Co-authored-by: Antonio <antonio@zed.dev>
This commit is contained in:
Kirill Bulatov 2023-12-12 15:36:20 +02:00
parent 717b2885f8
commit 2cde1a2e15
3 changed files with 85 additions and 61 deletions

View file

@ -569,6 +569,7 @@ impl AppContext {
/// such as notifying observers, emitting events, etc. Effects can themselves /// such as notifying observers, emitting events, etc. Effects can themselves
/// cause effects, so we continue looping until all effects are processed. /// cause effects, so we continue looping until all effects are processed.
fn flush_effects(&mut self) { fn flush_effects(&mut self) {
while !self.pending_effects.is_empty() {
loop { loop {
self.release_dropped_entities(); self.release_dropped_entities();
self.release_dropped_focus_handles(); self.release_dropped_focus_handles();
@ -624,6 +625,7 @@ impl AppContext {
self.update_window(window, |_, cx| cx.draw()).unwrap(); self.update_window(window, |_, cx| cx.draw()).unwrap();
} }
} }
}
/// Repeatedly called during `flush_effects` to release any entities whose /// Repeatedly called during `flush_effects` to release any entities whose
/// reference count has become zero. We invoke any release observers before dropping /// reference count has become zero. We invoke any release observers before dropping

View file

@ -229,6 +229,7 @@ pub struct Window {
pub(crate) next_frame: Frame, pub(crate) next_frame: Frame,
pub(crate) focus_handles: Arc<RwLock<SlotMap<FocusId, AtomicUsize>>>, pub(crate) focus_handles: Arc<RwLock<SlotMap<FocusId, AtomicUsize>>>,
pub(crate) focus_listeners: SubscriberSet<(), AnyWindowFocusListener>, pub(crate) focus_listeners: SubscriberSet<(), AnyWindowFocusListener>,
pub(crate) blur_listeners: SubscriberSet<(), AnyObserver>,
default_prevented: bool, default_prevented: bool,
mouse_position: Point<Pixels>, mouse_position: Point<Pixels>,
requested_cursor_style: Option<CursorStyle>, requested_cursor_style: Option<CursorStyle>,
@ -362,6 +363,7 @@ impl Window {
next_frame: Frame::new(DispatchTree::new(cx.keymap.clone(), cx.actions.clone())), next_frame: Frame::new(DispatchTree::new(cx.keymap.clone(), cx.actions.clone())),
focus_handles: Arc::new(RwLock::new(SlotMap::with_key())), focus_handles: Arc::new(RwLock::new(SlotMap::with_key())),
focus_listeners: SubscriberSet::new(), focus_listeners: SubscriberSet::new(),
blur_listeners: SubscriberSet::new(),
default_prevented: true, default_prevented: true,
mouse_position, mouse_position,
requested_cursor_style: None, requested_cursor_style: None,
@ -1235,6 +1237,16 @@ impl<'a> WindowContext<'a> {
/// Draw pixels to the display for this window based on the contents of its scene. /// Draw pixels to the display for this window based on the contents of its scene.
pub(crate) fn draw(&mut self) -> Scene { pub(crate) fn draw(&mut self) -> Scene {
let window_was_focused = self
.window
.focus
.and_then(|focus_id| {
self.window
.rendered_frame
.dispatch_tree
.focusable_node_id(focus_id)
})
.is_some();
self.text_system().start_frame(); self.text_system().start_frame();
self.window.platform_window.clear_input_handler(); self.window.platform_window.clear_input_handler();
self.window.layout_engine.as_mut().unwrap().clear(); self.window.layout_engine.as_mut().unwrap().clear();
@ -1273,6 +1285,23 @@ impl<'a> WindowContext<'a> {
}); });
} }
let window_is_focused = self
.window
.focus
.and_then(|focus_id| {
self.window
.next_frame
.dispatch_tree
.focusable_node_id(focus_id)
})
.is_some();
if window_was_focused && !window_is_focused {
self.window
.blur_listeners
.clone()
.retain(&(), |listener| listener(self));
}
self.window self.window
.next_frame .next_frame
.dispatch_tree .dispatch_tree
@ -2423,23 +2452,16 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// Register a listener to be called when the window loses focus. /// Register a listener to be called when the window loses focus.
/// Unlike [on_focus_changed], returns a subscription and persists until the subscription /// Unlike [on_focus_changed], returns a subscription and persists until the subscription
/// is dropped. /// is dropped.
pub fn on_window_focus_lost( pub fn on_blur_window(
&mut self, &mut self,
mut listener: impl FnMut(&mut V, &mut ViewContext<V>) + 'static, mut listener: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
) -> Subscription { ) -> Subscription {
let view = self.view.downgrade(); let view = self.view.downgrade();
let (subscription, activate) = self.window.focus_listeners.insert( let (subscription, activate) = self.window.blur_listeners.insert(
(), (),
Box::new(move |event, cx| { Box::new(move |cx| view.update(cx, |view, cx| listener(view, cx)).is_ok()),
view.update(cx, |view, cx| {
if event.blurred.is_none() && event.focused.is_none() {
listener(view, cx)
}
})
.is_ok()
}),
); );
self.app.defer(move |_| activate()); activate();
subscription subscription
} }

View file

@ -532,7 +532,7 @@ impl Workspace {
cx.notify() cx.notify()
}) })
.detach(); .detach();
cx.on_window_focus_lost(|this, cx| { cx.on_blur_window(|this, cx| {
let focus_handle = this.focus_handle(cx); let focus_handle = this.focus_handle(cx);
cx.focus(&focus_handle); cx.focus(&focus_handle);
}) })