Store AnyViewHandle inside ViewHandle and Deref to it

This commit is contained in:
Nathan Sobo 2023-04-02 14:57:06 -06:00
parent 59fb4b3d29
commit 82a713fd1d
32 changed files with 154 additions and 204 deletions

View file

@ -110,14 +110,14 @@ pub trait Item: View {
fn is_edit_event(_: &Self::Event) -> bool {
false
}
fn act_as_type(
&self,
fn act_as_type<'a>(
&'a self,
type_id: TypeId,
self_handle: &ViewHandle<Self>,
_: &AppContext,
) -> Option<AnyViewHandle> {
self_handle: &'a ViewHandle<Self>,
_: &'a AppContext,
) -> Option<&AnyViewHandle> {
if TypeId::of::<Self>() == type_id {
Some(self_handle.into())
Some(self_handle)
} else {
None
}
@ -187,7 +187,7 @@ pub trait ItemHandle: 'static + fmt::Debug {
fn navigate(&self, data: Box<dyn Any>, cx: &mut MutableAppContext) -> bool;
fn id(&self) -> usize;
fn window_id(&self) -> usize;
fn to_any(&self) -> AnyViewHandle;
fn as_any(&self) -> &AnyViewHandle;
fn is_dirty(&self, cx: &AppContext) -> bool;
fn has_conflict(&self, cx: &AppContext) -> bool;
fn can_save(&self, cx: &AppContext) -> bool;
@ -205,7 +205,7 @@ pub trait ItemHandle: 'static + fmt::Debug {
project: ModelHandle<Project>,
cx: &mut MutableAppContext,
) -> Task<Result<()>>;
fn act_as_type(&self, type_id: TypeId, cx: &AppContext) -> Option<AnyViewHandle>;
fn act_as_type<'a>(&'a self, type_id: TypeId, cx: &'a AppContext) -> Option<&'a AnyViewHandle>;
fn to_followable_item_handle(&self, cx: &AppContext) -> Option<Box<dyn FollowableItemHandle>>;
fn on_release(
&self,
@ -227,12 +227,12 @@ pub trait WeakItemHandle {
impl dyn ItemHandle {
pub fn downcast<T: View>(&self) -> Option<ViewHandle<T>> {
self.to_any().downcast()
self.as_any().clone().downcast()
}
pub fn act_as<T: View>(&self, cx: &AppContext) -> Option<ViewHandle<T>> {
self.act_as_type(TypeId::of::<T>(), cx)
.and_then(|t| t.downcast())
.and_then(|t| t.clone().downcast())
}
}
@ -513,8 +513,8 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
self.window_id()
}
fn to_any(&self) -> AnyViewHandle {
self.into()
fn as_any(&self) -> &AnyViewHandle {
self
}
fn is_dirty(&self, cx: &AppContext) -> bool {
@ -558,14 +558,14 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
self.update(cx, |item, cx| item.git_diff_recalc(project, cx))
}
fn act_as_type(&self, type_id: TypeId, cx: &AppContext) -> Option<AnyViewHandle> {
fn act_as_type<'a>(&'a self, type_id: TypeId, cx: &'a AppContext) -> Option<&'a AnyViewHandle> {
self.read(cx).act_as_type(type_id, self, cx)
}
fn to_followable_item_handle(&self, cx: &AppContext) -> Option<Box<dyn FollowableItemHandle>> {
if cx.has_global::<FollowableItemBuilders>() {
let builders = cx.global::<FollowableItemBuilders>();
let item = self.to_any();
let item = self.as_any();
Some(builders.get(&item.view_type())?.1(item))
} else {
None
@ -603,13 +603,13 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
impl From<Box<dyn ItemHandle>> for AnyViewHandle {
fn from(val: Box<dyn ItemHandle>) -> Self {
val.to_any()
val.as_any().clone()
}
}
impl From<&Box<dyn ItemHandle>> for AnyViewHandle {
fn from(val: &Box<dyn ItemHandle>) -> Self {
val.to_any()
val.as_any().clone()
}
}