// Port of gpui/src/app/context.rs: the context passed to entity callbacks. // // GPUI's `Context` derefs to `App`; here the app is the public `app` // field (`cx.app.set_global(...)`). Window-related methods (`listener`, // `observe_in`, focus) come with the Window (M6), `spawn` with the executors // (M4). ///| /// The context of an update to entity `T` (GPUI: `Context<'_, T>`). pub struct Context[T] { app : App priv weak : WeakEntity[T] } ///| pub fn[T] Context::entity_id(self : Context[T]) -> EntityId { self.weak.entity_id() } ///| /// The entity being updated. pub fn[T] Context::entity(self : Context[T]) -> Entity[T] { match self.weak.upgrade() { Some(entity) => entity None => abort("the entity must be alive if we have an entity context") } } ///| pub fn[T] Context::weak_entity(self : Context[T]) -> WeakEntity[T] { self.weak } ///| /// Marks this entity as changed (GPUI: `cx.notify()`). pub fn[T] Context::notify(self : Context[T]) -> Unit { self.app.notify(self.weak.entity_id()) } ///| /// Emits `event` to subscribers of this entity's `key` events /// (GPUI: `cx.emit(event)`). pub fn[T, E] Context::emit( self : Context[T], key : EventKey[E], event : E, ) -> Unit { self.app.emit_event(self.weak.entity_id(), key, event) } ///| /// Queues `f` to run after the current effects. pub fn[T] Context::defer_call(self : Context[T], f : (App) -> Unit) -> Unit { self.app.defer_call(f) } ///| /// Runs `f` on this entity's state, if it is still alive. fn[T] invoke_on( weak : WeakEntity[T], app : App, f : (T, Context[T]) -> Unit, ) -> Bool { match weak.upgrade() { Some(entity) => { app.update_entity(entity, f) true } None => false } } ///| /// Calls `on_notify` on this entity whenever `entity` is notified. Stops when /// either entity is released (GPUI: `cx.observe`). pub fn[T, W] Context::observe( self : Context[T], entity : Entity[W], on_notify : (T, Entity[W], Context[T]) -> Unit, ) -> Subscription { let observer = self.weak self.app.observe_internal(entity, fn(entity, app) { invoke_on(observer, app, fn(this, cx) { on_notify(this, entity, cx) }) }) } ///| /// Calls `on_notify` whenever this entity is notified. pub fn[T] Context::observe_self( self : Context[T], on_notify : (T, Context[T]) -> Unit, ) -> Subscription { let observer = self.weak self.app.observe_internal(self.entity(), fn(_, app) { invoke_on(observer, app, on_notify) }) } ///| /// Calls `on_event` on this entity for every `key` event `entity` emits. /// Stops when either entity is released (GPUI: `cx.subscribe`). pub fn[T, W, E] Context::subscribe( self : Context[T], entity : Entity[W], key : EventKey[E], on_event : (T, Entity[W], E, Context[T]) -> Unit, ) -> Subscription { let subscriber = self.weak self.app.subscribe_internal(entity, key, fn(entity, event, app) { invoke_on(subscriber, app, fn(this, cx) { on_event(this, entity, event, cx) }) }) } ///| /// Calls `on_event` for every `key` event this entity emits. pub fn[T, E] Context::subscribe_self( self : Context[T], key : EventKey[E], on_event : (T, E, Context[T]) -> Unit, ) -> Subscription { let subscriber = self.weak self.app.subscribe_internal(self.entity(), key, fn(_, event, app) { invoke_on(subscriber, app, fn(this, cx) { on_event(this, event, cx) }) }) } ///| /// Calls `on_release` with this entity's state when it is released /// (GPUI: `cx.on_release`). pub fn[T] Context::on_release( self : Context[T], on_release : (T, App) -> Unit, ) -> Subscription { self.app.observe_release({ cell: self.weak.cell, }, on_release) } ///| /// Calls `on_release` on this entity when `entity` is released /// (GPUI: `cx.observe_release`). pub fn[T, W] Context::observe_release( self : Context[T], entity : Entity[W], on_release : (T, W, Context[T]) -> Unit, ) -> Subscription { let observer = self.weak self.app.observe_release(entity, fn(released, app) { ignore( invoke_on(observer, app, fn(this, cx) { on_release(this, released, cx) }), ) }) } ///| /// Calls `f` on this entity whenever the global for `key` changes. Stops when /// this entity is released (GPUI: `cx.observe_global`). pub fn[T, G] Context::observe_global( self : Context[T], key : GlobalKey[G], f : (T, Context[T]) -> Unit, ) -> Subscription { let observer = self.weak let (subscription, activate) = self.app.global_observers.insert_pending( key.id, fn(app) { invoke_on(observer, app, f) }, ) self.app.defer_call(fn(_) { activate() }) subscription } ///| /// Updates this entity's state from a weak handle, if it is still alive. pub fn[T, R] WeakEntity::update( self : WeakEntity[T], app : App, f : (T, Context[T]) -> R, ) -> R? { match self.upgrade() { Some(entity) => Some(app.update_entity(entity, f)) None => None } } ///| /// `app.update_entity(self, f)`. pub fn[T, R] Entity::update( self : Entity[T], app : App, f : (T, Context[T]) -> R, ) -> R { app.update_entity(self, f) } ///| /// `app.read_entity(self)`. pub fn[T] Entity::read(self : Entity[T], app : App) -> T { app.read_entity(self) }