From 2542e1154a83b2fe325610058cef930a679de226 Mon Sep 17 00:00:00 2001 From: Dustin Yost Date: Tue, 28 Jul 2026 02:16:59 -0400 Subject: [PATCH] add App::emit such that events can be emitted without acquiring a Context (#94) --- crates/gpui/src/app.rs | 18 ++++++++++++++++++ crates/gpui/src/app/context.rs | 12 ++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index ae34b3617a..236885728b 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -961,6 +961,24 @@ impl App { self.pending_updates -= 1; } + /// Emit an event of the specified type, which can be handled by other entities that have subscribed via `subscribe` methods on their respective contexts. + /// A globally-callable equivalent to `Context::emit` without requiring an entity update. + pub fn emit(&mut self, entity: &Entity, event: EventType) + where + EntityType: EventEmitter, + EventType: 'static, + { + let event = self + .event_arena + .alloc(|| event) + .map(|it| it as &mut dyn Any); + self.pending_effects.push_back(Effect::Emit { + emitter: entity.entity_id(), + event_type: TypeId::of::(), + event, + }); + } + /// Arrange a callback to be invoked when the given entity calls `notify` on its respective context. pub fn observe( &mut self, diff --git a/crates/gpui/src/app/context.rs b/crates/gpui/src/app/context.rs index 0d1ee47ce4..30675a1733 100644 --- a/crates/gpui/src/app/context.rs +++ b/crates/gpui/src/app/context.rs @@ -1,5 +1,5 @@ use crate::{ - AnyView, AnyWindowHandle, AppContext, AsyncApp, DispatchPhase, Effect, EntityId, EventEmitter, + AnyView, AnyWindowHandle, AppContext, AsyncApp, DispatchPhase, EntityId, EventEmitter, FocusHandle, FocusOutEvent, Focusable, Global, KeystrokeObserver, Priority, Reservation, SubscriberSet, Subscription, Task, WeakEntity, WeakFocusHandle, Window, WindowHandle, }; @@ -767,15 +767,7 @@ impl Context<'_, T> { T: EventEmitter, Evt: 'static, { - let event = self - .event_arena - .alloc(|| event) - .map(|it| it as &mut dyn Any); - self.app.pending_effects.push_back(Effect::Emit { - emitter: self.entity_state.entity_id, - event_type: TypeId::of::(), - event, - }); + self.app.emit(&self.entity(), event); } }