Introduce AnyWeakView
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
7b6514b178
commit
0aa9c6b61d
1 changed files with 34 additions and 6 deletions
|
@ -1,7 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
private::Sealed, AnyBox, AnyElement, AnyModel, AppContext, AvailableSpace, BorrowWindow,
|
private::Sealed, AnyBox, AnyElement, AnyModel, AnyWeakModel, AppContext, AvailableSpace,
|
||||||
Bounds, Component, Element, ElementId, Entity, EntityId, LayoutId, Model, Pixels, Size,
|
BorrowWindow, Bounds, Component, Element, ElementId, Entity, EntityId, LayoutId, Model, Pixels,
|
||||||
ViewContext, VisualContext, WeakModel, WindowContext,
|
Size, ViewContext, VisualContext, WeakModel, WindowContext,
|
||||||
};
|
};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use std::{any::TypeId, marker::PhantomData};
|
use std::{any::TypeId, marker::PhantomData};
|
||||||
|
@ -262,12 +262,21 @@ where
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct AnyView {
|
pub struct AnyView {
|
||||||
model: AnyModel,
|
model: AnyModel,
|
||||||
initialize: fn(&Self, &mut WindowContext) -> AnyBox,
|
initialize: fn(&AnyView, &mut WindowContext) -> AnyBox,
|
||||||
layout: fn(&Self, &mut AnyBox, &mut WindowContext) -> LayoutId,
|
layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId,
|
||||||
paint: fn(&Self, &mut AnyBox, &mut WindowContext),
|
paint: fn(&AnyView, &mut AnyBox, &mut WindowContext),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AnyView {
|
impl AnyView {
|
||||||
|
pub fn downgrade(&self) -> AnyWeakView {
|
||||||
|
AnyWeakView {
|
||||||
|
model: self.model.downgrade(),
|
||||||
|
initialize: self.initialize,
|
||||||
|
layout: self.layout,
|
||||||
|
paint: self.paint,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn downcast<T: 'static>(self) -> Result<View<T>, Self> {
|
pub fn downcast<T: 'static>(self) -> Result<View<T>, Self> {
|
||||||
match self.model.downcast() {
|
match self.model.downcast() {
|
||||||
Ok(model) => Ok(View { model }),
|
Ok(model) => Ok(View { model }),
|
||||||
|
@ -366,6 +375,25 @@ impl<ParentViewState: 'static> Element<ParentViewState> for AnyView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct AnyWeakView {
|
||||||
|
model: AnyWeakModel,
|
||||||
|
initialize: fn(&AnyView, &mut WindowContext) -> AnyBox,
|
||||||
|
layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId,
|
||||||
|
paint: fn(&AnyView, &mut AnyBox, &mut WindowContext),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AnyWeakView {
|
||||||
|
pub fn upgrade(&self) -> Option<AnyView> {
|
||||||
|
let model = self.model.upgrade()?;
|
||||||
|
Some(AnyView {
|
||||||
|
model,
|
||||||
|
initialize: self.initialize,
|
||||||
|
layout: self.layout,
|
||||||
|
paint: self.paint,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T, E> Render for T
|
impl<T, E> Render for T
|
||||||
where
|
where
|
||||||
T: 'static + FnMut(&mut WindowContext) -> E,
|
T: 'static + FnMut(&mut WindowContext) -> E,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue