Allow using the inline assistant in prompt library (#12680)

Release Notes:

- N/A
This commit is contained in:
Antonio Scandurra 2024-06-05 14:46:33 +02:00 committed by GitHub
parent 0289c312c9
commit a96782cc6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 1589 additions and 1470 deletions

View file

@ -1036,6 +1036,37 @@ impl<'a> WindowContext<'a> {
});
}
/// Subscribe to events emitted by a model or view.
/// The entity to which you're subscribing must implement the [`EventEmitter`] trait.
/// The callback will be invoked a handle to the emitting entity (either a [`View`] or [`Model`]), the event, and a window context for the current window.
pub fn observe<E, T>(
&mut self,
entity: &E,
mut on_notify: impl FnMut(E, &mut WindowContext<'_>) + 'static,
) -> Subscription
where
E: Entity<T>,
{
let entity_id = entity.entity_id();
let entity = entity.downgrade();
let window_handle = self.window.handle;
self.app.new_observer(
entity_id,
Box::new(move |cx| {
window_handle
.update(cx, |_, cx| {
if let Some(handle) = E::upgrade_from(&entity) {
on_notify(handle, cx);
true
} else {
false
}
})
.unwrap_or(false)
}),
)
}
/// Subscribe to events emitted by a model or view.
/// The entity to which you're subscribing must implement the [`EventEmitter`] trait.
/// The callback will be invoked a handle to the emitting entity (either a [`View`] or [`Model`]), the event, and a window context for the current window.