context.rs

  1use crate::{
  2    AnyView, AnyWindowHandle, AppContext, AsyncApp, DispatchPhase, Effect, EntityId, EventEmitter,
  3    FocusHandle, FocusOutEvent, Focusable, Global, KeystrokeObserver, Reservation, SubscriberSet,
  4    Subscription, Task, WeakEntity, WeakFocusHandle, Window, WindowHandle,
  5};
  6use anyhow::Result;
  7use derive_more::{Deref, DerefMut};
  8use futures::FutureExt;
  9use std::{
 10    any::{Any, TypeId},
 11    borrow::{Borrow, BorrowMut},
 12    future::Future,
 13    sync::Arc,
 14};
 15
 16use super::{App, AsyncWindowContext, Entity, KeystrokeEvent};
 17
 18/// The app context, with specialized behavior for the given entity.
 19#[derive(Deref, DerefMut)]
 20pub struct Context<'a, T> {
 21    #[deref]
 22    #[deref_mut]
 23    app: &'a mut App,
 24    entity_state: WeakEntity<T>,
 25}
 26
 27impl<'a, T: 'static> Context<'a, T> {
 28    pub(crate) fn new_context(app: &'a mut App, entity_state: WeakEntity<T>) -> Self {
 29        Self { app, entity_state }
 30    }
 31
 32    /// The entity id of the entity backing this context.
 33    pub fn entity_id(&self) -> EntityId {
 34        self.entity_state.entity_id
 35    }
 36
 37    /// Returns a handle to the entity belonging to this context.
 38    pub fn entity(&self) -> Entity<T> {
 39        self.weak_entity()
 40            .upgrade()
 41            .expect("The entity must be alive if we have a entity context")
 42    }
 43
 44    /// Returns a weak handle to the entity belonging to this context.
 45    pub fn weak_entity(&self) -> WeakEntity<T> {
 46        self.entity_state.clone()
 47    }
 48
 49    /// Arranges for the given function to be called whenever [`Context::notify`] is
 50    /// called with the given entity.
 51    pub fn observe<W>(
 52        &mut self,
 53        entity: &Entity<W>,
 54        mut on_notify: impl FnMut(&mut T, Entity<W>, &mut Context<'_, T>) + 'static,
 55    ) -> Subscription
 56    where
 57        T: 'static,
 58        W: 'static,
 59    {
 60        let this = self.weak_entity();
 61        self.app.observe_internal(entity, move |e, cx| {
 62            if let Some(this) = this.upgrade() {
 63                this.update(cx, |this, cx| on_notify(this, e, cx));
 64                true
 65            } else {
 66                false
 67            }
 68        })
 69    }
 70
 71    /// Subscribe to an event type from another entity
 72    pub fn subscribe<T2, Evt>(
 73        &mut self,
 74        entity: &Entity<T2>,
 75        mut on_event: impl FnMut(&mut T, Entity<T2>, &Evt, &mut Context<'_, T>) + 'static,
 76    ) -> Subscription
 77    where
 78        T: 'static,
 79        T2: 'static + EventEmitter<Evt>,
 80        Evt: 'static,
 81    {
 82        let this = self.weak_entity();
 83        self.app.subscribe_internal(entity, move |e, event, cx| {
 84            if let Some(this) = this.upgrade() {
 85                this.update(cx, |this, cx| on_event(this, e, event, cx));
 86                true
 87            } else {
 88                false
 89            }
 90        })
 91    }
 92
 93    /// Register a callback to be invoked when GPUI releases this entity.
 94    pub fn on_release(&self, on_release: impl FnOnce(&mut T, &mut App) + 'static) -> Subscription
 95    where
 96        T: 'static,
 97    {
 98        let (subscription, activate) = self.app.release_listeners.insert(
 99            self.entity_state.entity_id,
100            Box::new(move |this, cx| {
101                let this = this.downcast_mut().expect("invalid entity type");
102                on_release(this, cx);
103            }),
104        );
105        activate();
106        subscription
107    }
108
109    /// Register a callback to be run on the release of another entity
110    pub fn observe_release<T2>(
111        &self,
112        entity: &Entity<T2>,
113        on_release: impl FnOnce(&mut T, &mut T2, &mut Context<'_, T>) + 'static,
114    ) -> Subscription
115    where
116        T: Any,
117        T2: 'static,
118    {
119        let entity_id = entity.entity_id();
120        let this = self.weak_entity();
121        let (subscription, activate) = self.app.release_listeners.insert(
122            entity_id,
123            Box::new(move |entity, cx| {
124                let entity = entity.downcast_mut().expect("invalid entity type");
125                if let Some(this) = this.upgrade() {
126                    this.update(cx, |this, cx| on_release(this, entity, cx));
127                }
128            }),
129        );
130        activate();
131        subscription
132    }
133
134    /// Register a callback to for updates to the given global
135    pub fn observe_global<G: 'static>(
136        &mut self,
137        mut f: impl FnMut(&mut T, &mut Context<'_, T>) + 'static,
138    ) -> Subscription
139    where
140        T: 'static,
141    {
142        let handle = self.weak_entity();
143        let (subscription, activate) = self.global_observers.insert(
144            TypeId::of::<G>(),
145            Box::new(move |cx| handle.update(cx, |view, cx| f(view, cx)).is_ok()),
146        );
147        self.defer(move |_| activate());
148        subscription
149    }
150
151    /// Arrange for the given function to be invoked whenever the application is quit.
152    /// The future returned from this callback will be polled for up to [crate::SHUTDOWN_TIMEOUT] until the app fully quits.
153    pub fn on_app_quit<Fut>(
154        &self,
155        mut on_quit: impl FnMut(&mut T, &mut Context<T>) -> Fut + 'static,
156    ) -> Subscription
157    where
158        Fut: 'static + Future<Output = ()>,
159        T: 'static,
160    {
161        let handle = self.weak_entity();
162        let (subscription, activate) = self.app.quit_observers.insert(
163            (),
164            Box::new(move |cx| {
165                let future = handle.update(cx, |entity, cx| on_quit(entity, cx)).ok();
166                async move {
167                    if let Some(future) = future {
168                        future.await;
169                    }
170                }
171                .boxed_local()
172            }),
173        );
174        activate();
175        subscription
176    }
177
178    /// Tell GPUI that this entity has changed and observers of it should be notified.
179    pub fn notify(&mut self) {
180        self.app.notify(self.entity_state.entity_id);
181    }
182
183    /// Spawn the future returned by the given function.
184    /// The function is provided a weak handle to the entity owned by this context and a context that can be held across await points.
185    /// The returned task must be held or detached.
186    pub fn spawn<Fut, R>(&self, f: impl FnOnce(WeakEntity<T>, AsyncApp) -> Fut) -> Task<R>
187    where
188        T: 'static,
189        Fut: Future<Output = R> + 'static,
190        R: 'static,
191    {
192        let this = self.weak_entity();
193        self.app.spawn(|cx| f(this, cx))
194    }
195
196    /// Convenience method for accessing view state in an event callback.
197    ///
198    /// Many GPUI callbacks take the form of `Fn(&E, &mut Window, &mut AppContext)`,
199    /// but it's often useful to be able to access view state in these
200    /// callbacks. This method provides a convenient way to do so.
201    pub fn listener<E: ?Sized>(
202        &self,
203        f: impl Fn(&mut T, &E, &mut Window, &mut Context<T>) + 'static,
204    ) -> impl Fn(&E, &mut Window, &mut App) + 'static {
205        let view = self.entity().downgrade();
206        move |e: &E, window: &mut Window, cx: &mut App| {
207            view.update(cx, |view, cx| f(view, e, window, cx)).ok();
208        }
209    }
210
211    /// Focus the given view in the given window. View type is required to implement Focusable.
212    pub fn focus_view<W: Focusable>(&mut self, view: &Entity<W>, window: &mut Window) {
213        window.focus(&view.focus_handle(self));
214    }
215
216    /// Sets a given callback to be run on the next frame.
217    pub fn on_next_frame(
218        &self,
219        window: &mut Window,
220        f: impl FnOnce(&mut T, &mut Window, &mut Context<T>) + 'static,
221    ) where
222        T: 'static,
223    {
224        let view = self.entity();
225        window.on_next_frame(move |window, cx| view.update(cx, |view, cx| f(view, window, cx)));
226    }
227
228    /// Schedules the given function to be run at the end of the current effect cycle, allowing entities
229    /// that are currently on the stack to be returned to the app.
230    pub fn defer_in(
231        &mut self,
232        window: &Window,
233        f: impl FnOnce(&mut T, &mut Window, &mut Context<T>) + 'static,
234    ) {
235        let view = self.entity();
236        window.defer(self, move |window, cx| {
237            view.update(cx, |view, cx| f(view, window, cx))
238        });
239    }
240
241    /// Observe another entity for changes to its state, as tracked by [`Context::notify`].
242    pub fn observe_in<V2>(
243        &mut self,
244        observed: &Entity<V2>,
245        window: &mut Window,
246        mut on_notify: impl FnMut(&mut T, Entity<V2>, &mut Window, &mut Context<'_, T>) + 'static,
247    ) -> Subscription
248    where
249        V2: 'static,
250        T: 'static,
251    {
252        let observed_id = observed.entity_id();
253        let observed = observed.downgrade();
254        let window_handle = window.handle;
255        let observer = self.weak_entity();
256        self.new_observer(
257            observed_id,
258            Box::new(move |cx| {
259                window_handle
260                    .update(cx, |_, window, cx| {
261                        if let Some((observer, observed)) =
262                            observer.upgrade().zip(observed.upgrade())
263                        {
264                            observer.update(cx, |observer, cx| {
265                                on_notify(observer, observed, window, cx);
266                            });
267                            true
268                        } else {
269                            false
270                        }
271                    })
272                    .unwrap_or(false)
273            }),
274        )
275    }
276
277    /// Subscribe to events emitted by another entity.
278    /// The entity to which you're subscribing must implement the [`EventEmitter`] trait.
279    /// The callback will be invoked with a reference to the current view, a handle to the emitting `Entity`, the event, a mutable reference to the `Window`, and the context for the entity.
280    pub fn subscribe_in<Emitter, Evt>(
281        &mut self,
282        emitter: &Entity<Emitter>,
283        window: &Window,
284        mut on_event: impl FnMut(&mut T, &Entity<Emitter>, &Evt, &mut Window, &mut Context<'_, T>)
285            + 'static,
286    ) -> Subscription
287    where
288        Emitter: EventEmitter<Evt>,
289        Evt: 'static,
290    {
291        let emitter = emitter.downgrade();
292        let window_handle = window.handle;
293        let subscriber = self.weak_entity();
294        self.new_subscription(
295            emitter.entity_id(),
296            (
297                TypeId::of::<Evt>(),
298                Box::new(move |event, cx| {
299                    window_handle
300                        .update(cx, |_, window, cx| {
301                            if let Some((subscriber, emitter)) =
302                                subscriber.upgrade().zip(emitter.upgrade())
303                            {
304                                let event = event.downcast_ref().expect("invalid event type");
305                                subscriber.update(cx, |subscriber, cx| {
306                                    on_event(subscriber, &emitter, event, window, cx);
307                                });
308                                true
309                            } else {
310                                false
311                            }
312                        })
313                        .unwrap_or(false)
314                }),
315            ),
316        )
317    }
318
319    /// Register a callback to be invoked when the view is released.
320    ///
321    /// The callback receives a handle to the view's window. This handle may be
322    /// invalid, if the window was closed before the view was released.
323    pub fn on_release_in(
324        &mut self,
325        window: &Window,
326        on_release: impl FnOnce(&mut T, &mut Window, &mut App) + 'static,
327    ) -> Subscription {
328        let entity = self.entity();
329        self.app.observe_release_in(&entity, window, on_release)
330    }
331
332    /// Register a callback to be invoked when the given Entity is released.
333    pub fn observe_release_in<T2>(
334        &self,
335        observed: &Entity<T2>,
336        window: &Window,
337        mut on_release: impl FnMut(&mut T, &mut T2, &mut Window, &mut Context<'_, T>) + 'static,
338    ) -> Subscription
339    where
340        T: 'static,
341        T2: 'static,
342    {
343        let observer = self.weak_entity();
344        self.app
345            .observe_release_in(observed, window, move |observed, window, cx| {
346                observer
347                    .update(cx, |observer, cx| {
348                        on_release(observer, observed, window, cx)
349                    })
350                    .ok();
351            })
352    }
353
354    /// Register a callback to be invoked when the window is resized.
355    pub fn observe_window_bounds(
356        &self,
357        window: &mut Window,
358        mut callback: impl FnMut(&mut T, &mut Window, &mut Context<T>) + 'static,
359    ) -> Subscription {
360        let view = self.weak_entity();
361        let (subscription, activate) = window.bounds_observers.insert(
362            (),
363            Box::new(move |window, cx| {
364                view.update(cx, |view, cx| callback(view, window, cx))
365                    .is_ok()
366            }),
367        );
368        activate();
369        subscription
370    }
371
372    /// Register a callback to be invoked when the window is activated or deactivated.
373    pub fn observe_window_activation(
374        &self,
375        window: &mut Window,
376        mut callback: impl FnMut(&mut T, &mut Window, &mut Context<T>) + 'static,
377    ) -> Subscription {
378        let view = self.weak_entity();
379        let (subscription, activate) = window.activation_observers.insert(
380            (),
381            Box::new(move |window, cx| {
382                view.update(cx, |view, cx| callback(view, window, cx))
383                    .is_ok()
384            }),
385        );
386        activate();
387        subscription
388    }
389
390    /// Registers a callback to be invoked when the window appearance changes.
391    pub fn observe_window_appearance(
392        &self,
393        window: &mut Window,
394        mut callback: impl FnMut(&mut T, &mut Window, &mut Context<T>) + 'static,
395    ) -> Subscription {
396        let view = self.weak_entity();
397        let (subscription, activate) = window.appearance_observers.insert(
398            (),
399            Box::new(move |window, cx| {
400                view.update(cx, |view, cx| callback(view, window, cx))
401                    .is_ok()
402            }),
403        );
404        activate();
405        subscription
406    }
407
408    /// Register a callback to be invoked when a keystroke is received by the application
409    /// in any window. Note that this fires after all other action and event mechanisms have resolved
410    /// and that this API will not be invoked if the event's propagation is stopped.
411    pub fn observe_keystrokes(
412        &mut self,
413        mut f: impl FnMut(&mut T, &KeystrokeEvent, &mut Window, &mut Context<T>) + 'static,
414    ) -> Subscription {
415        fn inner(
416            keystroke_observers: &SubscriberSet<(), KeystrokeObserver>,
417            handler: KeystrokeObserver,
418        ) -> Subscription {
419            let (subscription, activate) = keystroke_observers.insert((), handler);
420            activate();
421            subscription
422        }
423
424        let view = self.weak_entity();
425        inner(
426            &mut self.keystroke_observers,
427            Box::new(move |event, window, cx| {
428                if let Some(view) = view.upgrade() {
429                    view.update(cx, |view, cx| f(view, event, window, cx));
430                    true
431                } else {
432                    false
433                }
434            }),
435        )
436    }
437
438    /// Register a callback to be invoked when the window's pending input changes.
439    pub fn observe_pending_input(
440        &self,
441        window: &mut Window,
442        mut callback: impl FnMut(&mut T, &mut Window, &mut Context<T>) + 'static,
443    ) -> Subscription {
444        let view = self.weak_entity();
445        let (subscription, activate) = window.pending_input_observers.insert(
446            (),
447            Box::new(move |window, cx| {
448                view.update(cx, |view, cx| callback(view, window, cx))
449                    .is_ok()
450            }),
451        );
452        activate();
453        subscription
454    }
455
456    /// Register a listener to be called when the given focus handle receives focus.
457    /// Returns a subscription and persists until the subscription is dropped.
458    pub fn on_focus(
459        &mut self,
460        handle: &FocusHandle,
461        window: &mut Window,
462        mut listener: impl FnMut(&mut T, &mut Window, &mut Context<T>) + 'static,
463    ) -> Subscription {
464        let view = self.weak_entity();
465        let focus_id = handle.id;
466        let (subscription, activate) =
467            window.new_focus_listener(Box::new(move |event, window, cx| {
468                view.update(cx, |view, cx| {
469                    if event.previous_focus_path.last() != Some(&focus_id)
470                        && event.current_focus_path.last() == Some(&focus_id)
471                    {
472                        listener(view, window, cx)
473                    }
474                })
475                .is_ok()
476            }));
477        self.defer(|_| activate());
478        subscription
479    }
480
481    /// Register a listener to be called when the given focus handle or one of its descendants receives focus.
482    /// This does not fire if the given focus handle - or one of its descendants - was previously focused.
483    /// Returns a subscription and persists until the subscription is dropped.
484    pub fn on_focus_in(
485        &mut self,
486        handle: &FocusHandle,
487        window: &mut Window,
488        mut listener: impl FnMut(&mut T, &mut Window, &mut Context<T>) + 'static,
489    ) -> Subscription {
490        let view = self.weak_entity();
491        let focus_id = handle.id;
492        let (subscription, activate) =
493            window.new_focus_listener(Box::new(move |event, window, cx| {
494                view.update(cx, |view, cx| {
495                    if event.is_focus_in(focus_id) {
496                        listener(view, window, cx)
497                    }
498                })
499                .is_ok()
500            }));
501        self.defer(|_| activate());
502        subscription
503    }
504
505    /// Register a listener to be called when the given focus handle loses focus.
506    /// Returns a subscription and persists until the subscription is dropped.
507    pub fn on_blur(
508        &mut self,
509        handle: &FocusHandle,
510        window: &mut Window,
511        mut listener: impl FnMut(&mut T, &mut Window, &mut Context<T>) + 'static,
512    ) -> Subscription {
513        let view = self.weak_entity();
514        let focus_id = handle.id;
515        let (subscription, activate) =
516            window.new_focus_listener(Box::new(move |event, window, cx| {
517                view.update(cx, |view, cx| {
518                    if event.previous_focus_path.last() == Some(&focus_id)
519                        && event.current_focus_path.last() != Some(&focus_id)
520                    {
521                        listener(view, window, cx)
522                    }
523                })
524                .is_ok()
525            }));
526        self.defer(|_| activate());
527        subscription
528    }
529
530    /// Register a listener to be called when nothing in the window has focus.
531    /// This typically happens when the node that was focused is removed from the tree,
532    /// and this callback lets you chose a default place to restore the users focus.
533    /// Returns a subscription and persists until the subscription is dropped.
534    pub fn on_focus_lost(
535        &mut self,
536        window: &mut Window,
537        mut listener: impl FnMut(&mut T, &mut Window, &mut Context<T>) + 'static,
538    ) -> Subscription {
539        let view = self.weak_entity();
540        let (subscription, activate) = window.focus_lost_listeners.insert(
541            (),
542            Box::new(move |window, cx| {
543                view.update(cx, |view, cx| listener(view, window, cx))
544                    .is_ok()
545            }),
546        );
547        self.defer(|_| activate());
548        subscription
549    }
550
551    /// Register a listener to be called when the given focus handle or one of its descendants loses focus.
552    /// Returns a subscription and persists until the subscription is dropped.
553    pub fn on_focus_out(
554        &mut self,
555        handle: &FocusHandle,
556        window: &mut Window,
557        mut listener: impl FnMut(&mut T, FocusOutEvent, &mut Window, &mut Context<T>) + 'static,
558    ) -> Subscription {
559        let view = self.weak_entity();
560        let focus_id = handle.id;
561        let (subscription, activate) =
562            window.new_focus_listener(Box::new(move |event, window, cx| {
563                view.update(cx, |view, cx| {
564                    if let Some(blurred_id) = event.previous_focus_path.last().copied() {
565                        if event.is_focus_out(focus_id) {
566                            let event = FocusOutEvent {
567                                blurred: WeakFocusHandle {
568                                    id: blurred_id,
569                                    handles: Arc::downgrade(&cx.focus_handles),
570                                },
571                            };
572                            listener(view, event, window, cx)
573                        }
574                    }
575                })
576                .is_ok()
577            }));
578        self.defer(|_| activate());
579        subscription
580    }
581
582    /// Schedule a future to be run asynchronously.
583    /// The given callback is invoked with a [`WeakEntity<V>`] to avoid leaking the view for a long-running process.
584    /// It's also given an [`AsyncWindowContext`], which can be used to access the state of the view across await points.
585    /// The returned future will be polled on the main thread.
586    pub fn spawn_in<Fut, R>(
587        &self,
588        window: &Window,
589        f: impl FnOnce(WeakEntity<T>, AsyncWindowContext) -> Fut,
590    ) -> Task<R>
591    where
592        R: 'static,
593        Fut: Future<Output = R> + 'static,
594    {
595        let view = self.weak_entity();
596        window.spawn(self, |mut cx| f(view, cx))
597    }
598
599    /// Register a callback to be invoked when the given global state changes.
600    pub fn observe_global_in<G: Global>(
601        &mut self,
602        window: &Window,
603        mut f: impl FnMut(&mut T, &mut Window, &mut Context<'_, T>) + 'static,
604    ) -> Subscription {
605        let window_handle = window.handle;
606        let view = self.weak_entity();
607        let (subscription, activate) = self.global_observers.insert(
608            TypeId::of::<G>(),
609            Box::new(move |cx| {
610                window_handle
611                    .update(cx, |_, window, cx| {
612                        view.update(cx, |view, cx| f(view, window, cx)).is_ok()
613                    })
614                    .unwrap_or(false)
615            }),
616        );
617        self.defer(move |_| activate());
618        subscription
619    }
620
621    /// Register a callback to be invoked when the given Action type is dispatched to the window.
622    pub fn on_action(
623        &mut self,
624        action_type: TypeId,
625        window: &mut Window,
626        listener: impl Fn(&mut T, &dyn Any, DispatchPhase, &mut Window, &mut Context<T>) + 'static,
627    ) {
628        let handle = self.weak_entity();
629        window.on_action(action_type, move |action, phase, window, cx| {
630            handle
631                .update(cx, |view, cx| {
632                    listener(view, action, phase, window, cx);
633                })
634                .ok();
635        });
636    }
637
638    /// Move focus to the current view, assuming it implements [`Focusable`].
639    pub fn focus_self(&mut self, window: &mut Window)
640    where
641        T: Focusable,
642    {
643        let view = self.entity();
644        window.defer(self, move |window, cx| {
645            view.read(cx).focus_handle(cx).focus(window)
646        })
647    }
648}
649
650impl<'a, T> Context<'a, T> {
651    /// Emit an event of the specified type, which can be handled by other entities that have subscribed via `subscribe` methods on their respective contexts.
652    pub fn emit<Evt>(&mut self, event: Evt)
653    where
654        T: EventEmitter<Evt>,
655        Evt: 'static,
656    {
657        self.app.pending_effects.push_back(Effect::Emit {
658            emitter: self.entity_state.entity_id,
659            event_type: TypeId::of::<Evt>(),
660            event: Box::new(event),
661        });
662    }
663}
664
665impl<'a, T> AppContext for Context<'a, T> {
666    type Result<U> = U;
667
668    fn new<U: 'static>(
669        &mut self,
670        build_entity: impl FnOnce(&mut Context<'_, U>) -> U,
671    ) -> Entity<U> {
672        self.app.new(build_entity)
673    }
674
675    fn reserve_entity<U: 'static>(&mut self) -> Reservation<U> {
676        self.app.reserve_entity()
677    }
678
679    fn insert_entity<U: 'static>(
680        &mut self,
681        reservation: Reservation<U>,
682        build_entity: impl FnOnce(&mut Context<'_, U>) -> U,
683    ) -> Self::Result<Entity<U>> {
684        self.app.insert_entity(reservation, build_entity)
685    }
686
687    fn update_entity<U: 'static, R>(
688        &mut self,
689        handle: &Entity<U>,
690        update: impl FnOnce(&mut U, &mut Context<'_, U>) -> R,
691    ) -> R {
692        self.app.update_entity(handle, update)
693    }
694
695    fn read_entity<U, R>(
696        &self,
697        handle: &Entity<U>,
698        read: impl FnOnce(&U, &App) -> R,
699    ) -> Self::Result<R>
700    where
701        U: 'static,
702    {
703        self.app.read_entity(handle, read)
704    }
705
706    fn update_window<R, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<R>
707    where
708        F: FnOnce(AnyView, &mut Window, &mut App) -> R,
709    {
710        self.app.update_window(window, update)
711    }
712
713    fn read_window<U, R>(
714        &self,
715        window: &WindowHandle<U>,
716        read: impl FnOnce(Entity<U>, &App) -> R,
717    ) -> Result<R>
718    where
719        U: 'static,
720    {
721        self.app.read_window(window, read)
722    }
723
724    fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
725    where
726        R: Send + 'static,
727    {
728        self.app.background_executor.spawn(future)
729    }
730
731    fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> Self::Result<R>
732    where
733        G: Global,
734    {
735        self.app.read_global(callback)
736    }
737}
738
739impl<T> Borrow<App> for Context<'_, T> {
740    fn borrow(&self) -> &App {
741        self.app
742    }
743}
744
745impl<T> BorrowMut<App> for Context<'_, T> {
746    fn borrow_mut(&mut self) -> &mut App {
747        self.app
748    }
749}