mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-24 17:28:40 +00:00
Failed attempt at doing lifetimes via params on Context trait
This commit is contained in:
parent
acaa720c09
commit
1f93ae05eb
7 changed files with 104 additions and 95 deletions
|
@ -1414,30 +1414,18 @@ impl AppContext {
|
|||
pub fn get_name(&self) -> &'static str {
|
||||
self.name.as_ref().unwrap()
|
||||
}
|
||||
|
||||
fn insert_model2<'a, T: 'static>(
|
||||
&'a mut self,
|
||||
reservation: Reservation<T>,
|
||||
build_model: impl FnOnce(&mut ModelContext<'a, 'b, T>) -> T,
|
||||
) -> Model<T> {
|
||||
self.update(|cx| {
|
||||
let slot = reservation.0;
|
||||
let entity = build_model(&mut ModelContext::new(cx, &slot));
|
||||
cx.entities.insert(slot, entity)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Context for AppContext {
|
||||
impl<'a, 'm, 'w> Context<'a, 'm, 'w> for AppContext {
|
||||
type Result<T> = T;
|
||||
type EntityContext<'a, 'b, T: 'static> = ModelContext<'a, 'b, T>;
|
||||
type EntityContext<T: 'static> = ModelContext<'a, 'm, T>;
|
||||
|
||||
/// Build an entity that is owned by the application. The given function will be invoked with
|
||||
/// a `ModelContext` and must return an object representing the entity. A `Model` handle will be returned,
|
||||
/// which can be used to access the entity in a context.
|
||||
fn new_model<T: 'static>(
|
||||
&mut self,
|
||||
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
|
||||
&'m mut self,
|
||||
build_model: impl FnOnce(&mut ModelContext<'a, 'm, T>) -> T,
|
||||
) -> Model<T> {
|
||||
self.update(|cx| {
|
||||
let slot = cx.entities.reserve();
|
||||
|
@ -1466,7 +1454,7 @@ impl Context for AppContext {
|
|||
fn insert_model<T: 'static>(
|
||||
&mut self,
|
||||
reservation: Reservation<T>,
|
||||
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut ModelContext<'a, 'm, T>) -> T,
|
||||
) -> Self::Result<Model<T>> {
|
||||
self.update(|cx| {
|
||||
let slot = reservation.0;
|
||||
|
@ -1480,7 +1468,7 @@ impl Context for AppContext {
|
|||
fn update_model<T: 'static, R>(
|
||||
&mut self,
|
||||
model: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut ModelContext<'a, 'm, T>) -> R,
|
||||
) -> R {
|
||||
self.update(|cx| {
|
||||
let mut entity = cx.entities.lease(model);
|
||||
|
|
|
@ -8,6 +8,8 @@ use derive_more::{Deref, DerefMut};
|
|||
use futures::channel::oneshot;
|
||||
use std::{future::Future, rc::Weak};
|
||||
|
||||
use super::AppRefMut;
|
||||
|
||||
/// An async-friendly version of [AppContext] with a static lifetime so it can be held across `await` points in async code.
|
||||
/// You're provided with an instance when calling [AppContext::spawn], and you can also create one with [AppContext::to_async].
|
||||
/// Internally, this holds a weak reference to an `AppContext`, so its methods are fallible to protect against cases where the [AppContext] is dropped.
|
||||
|
@ -18,19 +20,23 @@ pub struct AsyncAppContext {
|
|||
pub(crate) foreground_executor: ForegroundExecutor,
|
||||
}
|
||||
|
||||
impl Context for AsyncAppContext {
|
||||
impl<'a, 'm, 'w> Context<'a, 'm, 'w> for AsyncAppContext
|
||||
where
|
||||
'a: 'w,
|
||||
'w: 'a,
|
||||
{
|
||||
type Result<T> = Result<T>;
|
||||
type EntityContext<'a, 'b, T: 'static> = ModelContext<'a, 'b, T>;
|
||||
type EntityContext<T: 'static> = ModelContext<'a, 'm, T>;
|
||||
|
||||
fn new_model<T: 'static>(
|
||||
&mut self,
|
||||
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
|
||||
&'m mut self,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Self::Result<Model<T>> {
|
||||
let app = self
|
||||
.app
|
||||
.upgrade()
|
||||
.ok_or_else(|| anyhow!("app was released"))?;
|
||||
let mut app = app.borrow_mut();
|
||||
let mut app: AppRefMut<'m> = app.borrow_mut();
|
||||
Ok(app.new_model(build_model))
|
||||
}
|
||||
|
||||
|
@ -46,7 +52,7 @@ impl Context for AsyncAppContext {
|
|||
fn insert_model<T: 'static>(
|
||||
&mut self,
|
||||
reservation: Reservation<T>,
|
||||
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Result<Model<T>> {
|
||||
let app = self
|
||||
.app
|
||||
|
@ -59,7 +65,7 @@ impl Context for AsyncAppContext {
|
|||
fn update_model<T: 'static, R>(
|
||||
&mut self,
|
||||
handle: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut Self::EntityContext<T>) -> R,
|
||||
) -> Self::Result<R> {
|
||||
let app = self
|
||||
.app
|
||||
|
@ -301,13 +307,17 @@ impl AsyncWindowContext {
|
|||
}
|
||||
}
|
||||
|
||||
impl Context for AsyncWindowContext {
|
||||
impl<'a, 'm, 'w> Context<'a, 'm, 'w> for AsyncWindowContext
|
||||
where
|
||||
'a: 'w,
|
||||
'w: 'm,
|
||||
{
|
||||
type Result<T> = Result<T>;
|
||||
type EntityContext<'a, 'b, T: 'static> = ViewContext<'a, 'b, T>;
|
||||
type EntityContext<T: 'static> = ViewContext<'a, 'm, 'w, T>;
|
||||
|
||||
fn new_model<T>(
|
||||
&mut self,
|
||||
build_model: impl FnOnce(&mut ViewContext<'_, '_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut ViewContext<'a, 'm, 'w, T>) -> T,
|
||||
) -> Result<Model<T>>
|
||||
where
|
||||
T: 'static,
|
||||
|
@ -322,7 +332,7 @@ impl Context for AsyncWindowContext {
|
|||
fn insert_model<T: 'static>(
|
||||
&mut self,
|
||||
reservation: Reservation<T>,
|
||||
build_model: impl FnOnce(&mut ViewContext<'_, '_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut ViewContext<'a, 'm, 'w, T>) -> T,
|
||||
) -> Self::Result<Model<T>> {
|
||||
self.window
|
||||
.update(self, |_, cx| cx.insert_model(reservation, build_model))
|
||||
|
@ -331,7 +341,7 @@ impl Context for AsyncWindowContext {
|
|||
fn update_model<T: 'static, R>(
|
||||
&mut self,
|
||||
handle: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut ViewContext<'_, '_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut ViewContext<'a, 'm, 'w, T>) -> R,
|
||||
) -> Result<R> {
|
||||
self.window
|
||||
.update(self, |_, cx| cx.update_model(handle, update))
|
||||
|
@ -367,10 +377,10 @@ impl Context for AsyncWindowContext {
|
|||
}
|
||||
}
|
||||
|
||||
impl VisualContext for AsyncWindowContext {
|
||||
impl<'a, 'm, 'w> VisualContext<'a, 'm, 'w> for AsyncWindowContext {
|
||||
fn new_view<V>(
|
||||
&mut self,
|
||||
build_view_state: impl FnOnce(&mut ViewContext<'_, '_, V>) -> V,
|
||||
build_view_state: impl FnOnce(&mut ViewContext<'a, 'm, 'w, V>) -> V,
|
||||
) -> Self::Result<Model<V>>
|
||||
where
|
||||
V: 'static + Render,
|
||||
|
@ -382,7 +392,7 @@ impl VisualContext for AsyncWindowContext {
|
|||
fn update_view<V: 'static, R>(
|
||||
&mut self,
|
||||
view: &Model<V>,
|
||||
update: impl FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> R,
|
||||
update: impl FnOnce(&mut V, &mut ViewContext<'a, 'm, 'w, V>) -> R,
|
||||
) -> Self::Result<R> {
|
||||
self.window
|
||||
.update(self, |_, cx| cx.update_view(view, update))
|
||||
|
@ -390,7 +400,7 @@ impl VisualContext for AsyncWindowContext {
|
|||
|
||||
fn replace_root_view<V>(
|
||||
&mut self,
|
||||
build_view: impl FnOnce(&mut ViewContext<'_, '_, V>) -> V,
|
||||
build_view: impl FnOnce(&mut ViewContext<'a, 'm, 'w, V>) -> V,
|
||||
) -> Self::Result<Model<V>>
|
||||
where
|
||||
V: 'static + Render,
|
||||
|
|
|
@ -398,7 +398,7 @@ impl<T: 'static> Model<T> {
|
|||
}
|
||||
|
||||
/// Read the entity referenced by this model with the given function.
|
||||
pub fn read_with<R, C: Context>(
|
||||
pub fn read_with<'a, 'm, 'w, R, C: Context<'a, 'm, 'w>>(
|
||||
&self,
|
||||
cx: &C,
|
||||
f: impl FnOnce(&T, &AppContext) -> R,
|
||||
|
@ -411,13 +411,13 @@ impl<T: 'static> Model<T> {
|
|||
/// The update function receives a context appropriate for its environment.
|
||||
/// When updating in an `AppContext`, it receives a `ModelContext`.
|
||||
/// When updating in a `WindowContext`, it receives a `ViewContext`.
|
||||
pub fn update<C, R>(
|
||||
pub fn update<'a, 'm, 'w, C, R>(
|
||||
&self,
|
||||
cx: &mut C,
|
||||
update: impl FnOnce(&mut T, &mut C::EntityContext<'_, '_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut C::EntityContext<T>) -> R,
|
||||
) -> C::Result<R>
|
||||
where
|
||||
C: Context,
|
||||
C: Context<'a, 'm, 'w>,
|
||||
{
|
||||
cx.update_model(self, update)
|
||||
}
|
||||
|
@ -605,13 +605,13 @@ impl<T: 'static> WeakModel<T> {
|
|||
/// Updates the entity referenced by this model with the given function if
|
||||
/// the referenced entity still exists. Returns an error if the entity has
|
||||
/// been released.
|
||||
pub fn update<C, R>(
|
||||
pub fn update<'a, 'm, 'w, C, R>(
|
||||
&self,
|
||||
cx: &mut C,
|
||||
update: impl FnOnce(&mut T, &mut C::EntityContext<'_, '_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut C::EntityContext<T>) -> R,
|
||||
) -> Result<R>
|
||||
where
|
||||
C: Context,
|
||||
C: Context<'a, 'm, 'w>,
|
||||
Result<C::Result<R>>: crate::Flatten<R>,
|
||||
{
|
||||
crate::Flatten::flatten(
|
||||
|
@ -624,9 +624,13 @@ impl<T: 'static> WeakModel<T> {
|
|||
/// Reads the entity referenced by this model with the given function if
|
||||
/// the referenced entity still exists. Returns an error if the entity has
|
||||
/// been released.
|
||||
pub fn read_with<C, R>(&self, cx: &C, read: impl FnOnce(&T, &AppContext) -> R) -> Result<R>
|
||||
pub fn read_with<'a, 'm, 'w, C, R>(
|
||||
&self,
|
||||
cx: &C,
|
||||
read: impl FnOnce(&T, &AppContext) -> R,
|
||||
) -> Result<R>
|
||||
where
|
||||
C: Context,
|
||||
C: Context<'a, 'm, 'w>,
|
||||
Result<C::Result<R>>: crate::Flatten<R>,
|
||||
{
|
||||
crate::Flatten::flatten(
|
||||
|
|
|
@ -13,15 +13,15 @@ use std::{
|
|||
|
||||
/// The app context, with specialized behavior for the given model.
|
||||
#[derive(Deref, DerefMut)]
|
||||
pub struct ModelContext<'a, 'b, T> {
|
||||
pub struct ModelContext<'a, 'm, T> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
pub(crate) app: &'a mut AppContext,
|
||||
pub(crate) entity: &'b Model<T>,
|
||||
pub(crate) entity: &'m Model<T>,
|
||||
}
|
||||
|
||||
impl<'a, 'b, T: 'static> ModelContext<'a, 'b, T> {
|
||||
pub(crate) fn new(app: &'a mut AppContext, model_state: &'b Model<T>) -> Self {
|
||||
impl<'a, 'm, T: 'static> ModelContext<'a, 'm, T> {
|
||||
pub(crate) fn new(app: &'a mut AppContext, model_state: &'m Model<T>) -> Self {
|
||||
Self {
|
||||
app,
|
||||
entity: model_state,
|
||||
|
@ -210,7 +210,7 @@ impl<'a, 'b, T: 'static> ModelContext<'a, 'b, T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, T> ModelContext<'a, 'b, T> {
|
||||
impl<'a, 'm, T> ModelContext<'a, 'm, T> {
|
||||
/// Emit an event of the specified type, which can be handled by other entities that have subscribed via `subscribe` methods on their respective contexts.
|
||||
pub fn emit<Evt>(&mut self, event: Evt)
|
||||
where
|
||||
|
@ -225,13 +225,13 @@ impl<'a, 'b, T> ModelContext<'a, 'b, T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, T> Context for ModelContext<'a, 'b, T> {
|
||||
impl<'a, 'm, 'w, T> Context<'a, 'm, 'w> for ModelContext<'a, 'm, T> {
|
||||
type Result<U> = U;
|
||||
type EntityContext<'c, 'd, U: 'static> = ModelContext<'c, 'd, U>;
|
||||
type EntityContext<U: 'static> = ModelContext<'a, 'm, U>;
|
||||
|
||||
fn new_model<U: 'static>(
|
||||
&mut self,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<'_, '_, U>) -> U,
|
||||
build_model: impl FnOnce(&mut ModelContext<'a, 'm, U>) -> U,
|
||||
) -> Model<U> {
|
||||
self.app.new_model(build_model)
|
||||
}
|
||||
|
@ -243,7 +243,7 @@ impl<'a, 'b, T> Context for ModelContext<'a, 'b, T> {
|
|||
fn insert_model<U: 'static>(
|
||||
&mut self,
|
||||
reservation: Reservation<U>,
|
||||
build_model: impl FnOnce(&mut ModelContext<'_, '_, U>) -> U,
|
||||
build_model: impl FnOnce(&mut ModelContext<'a, 'm, U>) -> U,
|
||||
) -> Self::Result<Model<U>> {
|
||||
self.app.insert_model(reservation, build_model)
|
||||
}
|
||||
|
@ -251,7 +251,7 @@ impl<'a, 'b, T> Context for ModelContext<'a, 'b, T> {
|
|||
fn update_model<U: 'static, R>(
|
||||
&mut self,
|
||||
handle: &Model<U>,
|
||||
update: impl FnOnce(&mut U, &mut ModelContext<'_, '_, U>) -> R,
|
||||
update: impl FnOnce(&mut U, &mut ModelContext<'a, 'm, U>) -> R,
|
||||
) -> R {
|
||||
self.app.update_model(handle, update)
|
||||
}
|
||||
|
|
|
@ -29,13 +29,13 @@ pub struct TestAppContext {
|
|||
on_quit: Rc<RefCell<Vec<Box<dyn FnOnce() + 'static>>>>,
|
||||
}
|
||||
|
||||
impl Context for TestAppContext {
|
||||
impl<'a, 'm, 'w> Context<'a, 'm, 'w> for TestAppContext {
|
||||
type Result<T> = T;
|
||||
type EntityContext<'a, 'b, T: 'static> = ModelContext<'a, 'b, T>;
|
||||
type EntityContext<T: 'static> = ModelContext<'a, 'm, T>;
|
||||
|
||||
fn new_model<T: 'static>(
|
||||
&mut self,
|
||||
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Self::Result<Model<T>> {
|
||||
let mut app = self.app.borrow_mut();
|
||||
app.new_model(build_model)
|
||||
|
@ -49,7 +49,7 @@ impl Context for TestAppContext {
|
|||
fn insert_model<T: 'static>(
|
||||
&mut self,
|
||||
reservation: crate::Reservation<T>,
|
||||
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Self::Result<Model<T>> {
|
||||
let mut app = self.app.borrow_mut();
|
||||
app.insert_model(reservation, build_model)
|
||||
|
@ -58,7 +58,7 @@ impl Context for TestAppContext {
|
|||
fn update_model<T: 'static, R>(
|
||||
&mut self,
|
||||
handle: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut Self::EntityContext<T>) -> R,
|
||||
) -> Self::Result<R> {
|
||||
let mut app = self.app.borrow_mut();
|
||||
app.update_model(handle, update)
|
||||
|
@ -875,13 +875,17 @@ impl VisualTestContext {
|
|||
}
|
||||
}
|
||||
|
||||
impl Context for VisualTestContext {
|
||||
type Result<T> = <TestAppContext as Context>::Result<T>;
|
||||
type EntityContext<'a, 'b, T: 'static> = ViewContext<'a, 'b, T>;
|
||||
impl<'a, 'm, 'w> Context<'a, 'm, 'w> for VisualTestContext
|
||||
where
|
||||
'a: 'w,
|
||||
'w: 'm,
|
||||
{
|
||||
type Result<T> = <TestAppContext as Context<'a, 'm, 'w>>::Result<T>;
|
||||
type EntityContext<T: 'static> = ViewContext<'a, 'm, 'w, T>;
|
||||
|
||||
fn new_model<T: 'static>(
|
||||
&mut self,
|
||||
build_model: impl FnOnce(&mut ViewContext<'_, '_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Self::Result<Model<T>> {
|
||||
self.cx
|
||||
.update_window(self.window, |_, cx| cx.new_model(build_model))
|
||||
|
@ -895,7 +899,7 @@ impl Context for VisualTestContext {
|
|||
fn insert_model<T: 'static>(
|
||||
&mut self,
|
||||
reservation: crate::Reservation<T>,
|
||||
build_model: impl FnOnce(&mut ViewContext<'_, '_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Self::Result<Model<T>> {
|
||||
self.cx
|
||||
.update_window(self.window, |_, cx| {
|
||||
|
@ -907,7 +911,7 @@ impl Context for VisualTestContext {
|
|||
fn update_model<T, R>(
|
||||
&mut self,
|
||||
handle: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut ViewContext<'_, '_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut Self::EntityContext<T>) -> R,
|
||||
) -> Self::Result<R>
|
||||
where
|
||||
T: 'static,
|
||||
|
@ -947,10 +951,10 @@ impl Context for VisualTestContext {
|
|||
}
|
||||
}
|
||||
|
||||
impl VisualContext for VisualTestContext {
|
||||
impl<'a, 'm, 'w> VisualContext<'a, 'm, 'w> for VisualTestContext {
|
||||
fn new_view<V>(
|
||||
&mut self,
|
||||
build_view: impl FnOnce(&mut ViewContext<'_, '_, V>) -> V,
|
||||
build_view: impl FnOnce(&mut Self::EntityContext<V>) -> V,
|
||||
) -> Self::Result<Model<V>>
|
||||
where
|
||||
V: 'static + Render,
|
||||
|
@ -963,7 +967,7 @@ impl VisualContext for VisualTestContext {
|
|||
fn update_view<V: 'static, R>(
|
||||
&mut self,
|
||||
view: &Model<V>,
|
||||
update: impl FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> R,
|
||||
update: impl FnOnce(&mut V, &mut Self::EntityContext<V>) -> R,
|
||||
) -> Self::Result<R> {
|
||||
self.window
|
||||
.update(&mut self.cx, |_, cx| cx.update_view(view, update))
|
||||
|
@ -972,7 +976,7 @@ impl VisualContext for VisualTestContext {
|
|||
|
||||
fn replace_root_view<V>(
|
||||
&mut self,
|
||||
build_view: impl FnOnce(&mut ViewContext<'_, '_, V>) -> V,
|
||||
build_view: impl FnOnce(&mut Self::EntityContext<V>) -> V,
|
||||
) -> Self::Result<Model<V>>
|
||||
where
|
||||
V: 'static + Render,
|
||||
|
@ -1007,7 +1011,7 @@ impl AnyWindowHandle {
|
|||
pub fn build_view<V: Render + 'static>(
|
||||
&self,
|
||||
cx: &mut TestAppContext,
|
||||
build_view: impl FnOnce(&mut ViewContext<'_, '_, V>) -> V,
|
||||
build_view: impl FnOnce(&mut ViewContext<V>) -> V,
|
||||
) -> Model<V> {
|
||||
self.update(cx, |_, cx| cx.new_view(build_view)).unwrap()
|
||||
}
|
||||
|
|
|
@ -157,7 +157,7 @@ use taffy::TaffyLayoutEngine;
|
|||
|
||||
/// The context trait, allows the different contexts in GPUI to be used
|
||||
/// interchangeably for certain operations.
|
||||
pub trait Context {
|
||||
pub trait Context<'a, 'm, 'w> {
|
||||
/// The result type for this context, used for async contexts that
|
||||
/// can't hold a direct reference to the application context.
|
||||
type Result<T>;
|
||||
|
@ -165,12 +165,12 @@ pub trait Context {
|
|||
/// Defines the context type used when updating an entity within this context.
|
||||
/// This associated type specifies the appropriate context for entity updates,
|
||||
/// allowing for context-specific operations and ensuring type safety.
|
||||
type EntityContext<'a, 'b, 'c, T: 'static>;
|
||||
type EntityContext<T: 'static>;
|
||||
|
||||
/// Create a new model in the app context.
|
||||
fn new_model<T: 'static>(
|
||||
&mut self,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<'_, '_, '_, T>) -> T,
|
||||
&'m mut self,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Self::Result<Model<T>>;
|
||||
|
||||
/// Reserve a slot for a model to be inserted later.
|
||||
|
@ -181,16 +181,16 @@ pub trait Context {
|
|||
///
|
||||
/// [`reserve_model`]: Self::reserve_model
|
||||
fn insert_model<T: 'static>(
|
||||
&mut self,
|
||||
&'m mut self,
|
||||
reservation: Reservation<T>,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<'_, '_, '_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Self::Result<Model<T>>;
|
||||
|
||||
/// Update a model in the app context.
|
||||
fn update_model<T, R>(
|
||||
&mut self,
|
||||
&'m mut self,
|
||||
handle: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, '_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut Self::EntityContext<T>) -> R,
|
||||
) -> Self::Result<R>
|
||||
where
|
||||
T: 'static;
|
||||
|
@ -232,11 +232,11 @@ impl<T: 'static> Reservation<T> {
|
|||
|
||||
/// This trait is used for the different visual contexts in GPUI that
|
||||
/// require a window to be present.
|
||||
pub trait VisualContext: Context {
|
||||
pub trait VisualContext<'a, 'm, 'w>: Context<'a, 'm, 'w> {
|
||||
/// Construct a new view in the window referenced by this context.
|
||||
fn new_view<V>(
|
||||
&mut self,
|
||||
build_view: impl FnOnce(&mut ViewContext<'_, '_, V>) -> V,
|
||||
build_view: impl FnOnce(&mut ViewContext<'a, 'm, 'w, V>) -> V,
|
||||
) -> Self::Result<Model<V>>
|
||||
where
|
||||
V: 'static + Render;
|
||||
|
@ -245,13 +245,13 @@ pub trait VisualContext: Context {
|
|||
fn update_view<V: 'static, R>(
|
||||
&mut self,
|
||||
view: &Model<V>,
|
||||
update: impl FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> R,
|
||||
update: impl FnOnce(&mut V, &mut ViewContext<'a, 'm, 'w, V>) -> R,
|
||||
) -> Self::Result<R>;
|
||||
|
||||
/// Replace the root view of a window with a new view.
|
||||
fn replace_root_view<V>(
|
||||
&mut self,
|
||||
build_view: impl FnOnce(&mut ViewContext<'_, '_, V>) -> V,
|
||||
build_view: impl FnOnce(&mut ViewContext<'a, 'm, 'w, V>) -> V,
|
||||
) -> Self::Result<Model<V>>
|
||||
where
|
||||
V: 'static + Render;
|
||||
|
|
|
@ -6573,13 +6573,16 @@ impl WindowContext<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Context for WindowContext<'_> {
|
||||
impl<'a, 'm, 'w> Context<'a, 'm, 'w> for WindowContext<'_>
|
||||
where
|
||||
'a: 'w,
|
||||
{
|
||||
type Result<T> = T;
|
||||
type EntityContext<'a, 'b, T: 'static> = ViewContext<'a, 'b, T>;
|
||||
type EntityContext<T: 'static> = ViewContext<'a, 'm, 'w, T>;
|
||||
|
||||
fn new_model<T>(
|
||||
&mut self,
|
||||
build_model: impl FnOnce(&mut ViewContext<'_, '_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Model<T>
|
||||
where
|
||||
T: 'static,
|
||||
|
@ -6613,7 +6616,7 @@ impl Context for WindowContext<'_> {
|
|||
fn insert_model<T: 'static>(
|
||||
&mut self,
|
||||
reservation: crate::Reservation<T>,
|
||||
build_model: impl FnOnce(&mut ViewContext<'_, '_, T>) -> T,
|
||||
build_model: impl FnOnce(&mut Self::EntityContext<T>) -> T,
|
||||
) -> Self::Result<Model<T>> {
|
||||
self.app
|
||||
.insert_model(reservation, |cx: &mut ModelContext<'_, T>| {
|
||||
|
@ -6627,7 +6630,7 @@ impl Context for WindowContext<'_> {
|
|||
fn update_model<T: 'static, R>(
|
||||
&mut self,
|
||||
model: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut ViewContext<'_, '_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut Self::EntityContext<T>) -> R,
|
||||
) -> R {
|
||||
let mut entity = self.entities.lease(model);
|
||||
let result = update(
|
||||
|
@ -6685,10 +6688,10 @@ impl Context for WindowContext<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
impl VisualContext for WindowContext<'_> {
|
||||
impl<'a, 'm, 'w> VisualContext<'a, 'm, 'w> for WindowContext<'_> {
|
||||
fn new_view<V>(
|
||||
&mut self,
|
||||
build_view_state: impl FnOnce(&mut ViewContext<'_, '_, '_, V>) -> V,
|
||||
build_view_state: impl FnOnce(&mut Self::EntityContext<V>) -> V,
|
||||
) -> Self::Result<Model<V>>
|
||||
where
|
||||
V: 'static + Render,
|
||||
|
@ -6714,7 +6717,7 @@ impl VisualContext for WindowContext<'_> {
|
|||
fn update_view<T: 'static, R>(
|
||||
&mut self,
|
||||
view: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut ViewContext<'_, '_, T>) -> R,
|
||||
update: impl FnOnce(&mut T, &mut Self::EntityContext<T>) -> R,
|
||||
) -> Self::Result<R> {
|
||||
let mut lease = self.app.entities.lease(view);
|
||||
let mut cx = ViewContext::new(&mut *self.cx, &mut *self.window, view);
|
||||
|
@ -6725,7 +6728,7 @@ impl VisualContext for WindowContext<'_> {
|
|||
|
||||
fn replace_root_view<V>(
|
||||
&mut self,
|
||||
build_view: impl FnOnce(&mut ViewContext<'_, '_, '_, V>) -> V,
|
||||
build_view: impl FnOnce(&mut Self::EntityContext<V>) -> V,
|
||||
) -> Self::Result<Model<V>>
|
||||
where
|
||||
V: 'static + Render,
|
||||
|
@ -6817,9 +6820,9 @@ impl<T> BorrowWindow for T where T: BorrowMut<AppContext> + BorrowMut<Window> {}
|
|||
/// Allows you to interact with focus, emit events, etc.
|
||||
/// ViewContext also derefs to [`WindowContext`], giving you access to all of its methods as well.
|
||||
/// When you call [`View::update`], you're passed a `&mut V` and an `&mut ViewContext<V>`.
|
||||
pub struct ViewContext<'a, 'b, 'c, V> {
|
||||
cx: &'a mut ModelContext<'a, 'b, V>,
|
||||
window: &'c mut Window,
|
||||
pub struct ViewContext<'a, 'm, 'w, V> {
|
||||
cx: &'m mut ModelContext<'a, 'm, V>,
|
||||
window: &'w mut Window,
|
||||
}
|
||||
|
||||
impl<V> Borrow<AppContext> for ViewContext<'_, '_, '_, V> {
|
||||
|
@ -7307,9 +7310,9 @@ impl<'a, 'b, 'c, V: 'static> ViewContext<'a, 'b, 'c, V> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<V> Context for ViewContext<'_, '_, '_, V> {
|
||||
impl<'a, 'm, 'w, V> Context<'a, 'm, 'w> for ViewContext<'_, '_, '_, V> {
|
||||
type Result<U> = U;
|
||||
type EntityContext<'a, 'b, T: 'static> = ViewContext<'a, T>;
|
||||
type EntityContext<T: 'static> = ViewContext<'a, 'm, 'w, T>;
|
||||
|
||||
fn new_model<T: 'static>(
|
||||
&mut self,
|
||||
|
|
Loading…
Reference in a new issue