From 0aa9c6b61d28e8506fc49ef3e9117d0fc933fb39 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 31 Oct 2023 16:19:46 +0100 Subject: [PATCH] Introduce `AnyWeakView` Co-Authored-By: Nathan Sobo --- crates/gpui2/src/view.rs | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/crates/gpui2/src/view.rs b/crates/gpui2/src/view.rs index e702fcb274..ff5f10e722 100644 --- a/crates/gpui2/src/view.rs +++ b/crates/gpui2/src/view.rs @@ -1,7 +1,7 @@ use crate::{ - private::Sealed, AnyBox, AnyElement, AnyModel, AppContext, AvailableSpace, BorrowWindow, - Bounds, Component, Element, ElementId, Entity, EntityId, LayoutId, Model, Pixels, Size, - ViewContext, VisualContext, WeakModel, WindowContext, + private::Sealed, AnyBox, AnyElement, AnyModel, AnyWeakModel, AppContext, AvailableSpace, + BorrowWindow, Bounds, Component, Element, ElementId, Entity, EntityId, LayoutId, Model, Pixels, + Size, ViewContext, VisualContext, WeakModel, WindowContext, }; use anyhow::{Context, Result}; use std::{any::TypeId, marker::PhantomData}; @@ -262,12 +262,21 @@ where #[derive(Clone, Debug)] pub struct AnyView { model: AnyModel, - initialize: fn(&Self, &mut WindowContext) -> AnyBox, - layout: fn(&Self, &mut AnyBox, &mut WindowContext) -> LayoutId, - paint: fn(&Self, &mut AnyBox, &mut WindowContext), + initialize: fn(&AnyView, &mut WindowContext) -> AnyBox, + layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId, + paint: fn(&AnyView, &mut AnyBox, &mut WindowContext), } impl AnyView { + pub fn downgrade(&self) -> AnyWeakView { + AnyWeakView { + model: self.model.downgrade(), + initialize: self.initialize, + layout: self.layout, + paint: self.paint, + } + } + pub fn downcast(self) -> Result, Self> { match self.model.downcast() { Ok(model) => Ok(View { model }), @@ -366,6 +375,25 @@ impl Element 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 { + let model = self.model.upgrade()?; + Some(AnyView { + model, + initialize: self.initialize, + layout: self.layout, + paint: self.paint, + }) + } +} + impl Render for T where T: 'static + FnMut(&mut WindowContext) -> E,