Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Introduce `ViewContext::on_blur`
Antonio Scandurra14b41d657dd2
diff --git a/crates/gpui2/src/scene.rs b/crates/gpui2/src/scene.rsindex 918c3c83516..87e89adfa0e 100644--- a/crates/gpui2/src/scene.rs+++ b/crates/gpui2/src/scene.rs@@ -43,10 +43,6 @@ impl Default for SceneBuilder {}impl SceneBuilder {- pub fn new() -> SceneBuilder {- SceneBuilder::default()- }-pub fn build(&mut self) -> Scene {// Map each layer id to a float between 0. and 1., with 1. closer to the viewer.let mut layer_z_values = vec![0.; self.layers_by_order.len()];diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rsindex f9a5d59acd8..38e7c56eebc 100644--- a/crates/gpui2/src/window.rs+++ b/crates/gpui2/src/window.rs@@ -1949,6 +1949,29 @@ impl<'a, V: 'static> ViewContext<'a, V> {)}+ /// Register a listener to be called when the given focus handle loses focus.+ /// Unlike [on_focus_changed], returns a subscription and persists until the subscription+ /// is dropped.+ pub fn on_blur(+ &mut self,+ handle: &FocusHandle,+ mut listener: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,+ ) -> Subscription {+ let view = self.view.downgrade();+ let focus_id = handle.id;+ self.window.focus_listeners.insert(+ (),+ Box::new(move |event, cx| {+ view.update(cx, |view, cx| {+ if event.blurred.as_ref().map(|blurred| blurred.id) == Some(focus_id) {+ listener(view, cx)+ }+ })+ .is_ok()+ }),+ )+ }+/// Register a listener to be called when the given focus handle or one of its descendants loses focus./// Unlike [on_focus_changed], returns a subscription and persists until the subscription/// is dropped.@@ -1966,7 +1989,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {if event.blurred.as_ref()- .map_or(false, |focused| focus_id.contains(focused.id, cx))+ .map_or(false, |blurred| focus_id.contains(blurred.id, cx)){listener(view, cx)}