Prevent ChildView from retaining an otherwise dropped view

This commit is contained in:
Antonio Scandurra 2022-10-13 14:53:58 +02:00
parent 26b03afa60
commit 06dfb74663
2 changed files with 17 additions and 7 deletions

View file

@ -4732,6 +4732,10 @@ pub struct AnyWeakViewHandle {
} }
impl AnyWeakViewHandle { impl AnyWeakViewHandle {
pub fn id(&self) -> usize {
self.view_id
}
pub fn upgrade(&self, cx: &impl UpgradeViewHandle) -> Option<AnyViewHandle> { pub fn upgrade(&self, cx: &impl UpgradeViewHandle) -> Option<AnyViewHandle> {
cx.upgrade_any_view_handle(self) cx.upgrade_any_view_handle(self)
} }

View file

@ -12,10 +12,10 @@ use crate::{
UpOutRegionEvent, UpRegionEvent, UpOutRegionEvent, UpRegionEvent,
}, },
text_layout::TextLayoutCache, text_layout::TextLayoutCache,
Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, Appearance, AssetCache, ElementBox, Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle, Appearance,
Entity, FontSystem, ModelHandle, MouseButton, MouseMovedEvent, MouseRegion, MouseRegionId, AssetCache, ElementBox, Entity, FontSystem, ModelHandle, MouseButton, MouseMovedEvent,
ParentId, ReadModel, ReadView, RenderContext, RenderParams, Scene, UpgradeModelHandle, MouseRegion, MouseRegionId, ParentId, ReadModel, ReadView, RenderContext, RenderParams, Scene,
UpgradeViewHandle, View, ViewHandle, WeakModelHandle, WeakViewHandle, UpgradeModelHandle, UpgradeViewHandle, View, ViewHandle, WeakModelHandle, WeakViewHandle,
}; };
use collections::{HashMap, HashSet}; use collections::{HashMap, HashSet};
use pathfinder_geometry::vector::{vec2f, Vector2F}; use pathfinder_geometry::vector::{vec2f, Vector2F};
@ -972,12 +972,14 @@ impl ToJson for SizeConstraint {
} }
pub struct ChildView { pub struct ChildView {
view: AnyViewHandle, view: AnyWeakViewHandle,
} }
impl ChildView { impl ChildView {
pub fn new(view: impl Into<AnyViewHandle>) -> Self { pub fn new(view: impl Into<AnyViewHandle>) -> Self {
Self { view: view.into() } Self {
view: view.into().downgrade(),
}
} }
} }
@ -1039,7 +1041,11 @@ impl Element for ChildView {
"type": "ChildView", "type": "ChildView",
"view_id": self.view.id(), "view_id": self.view.id(),
"bounds": bounds.to_json(), "bounds": bounds.to_json(),
"view": self.view.debug_json(cx.app), "view": if let Some(view) = self.view.upgrade(cx.app) {
view.debug_json(cx.app)
} else {
json!(null)
},
"child": if let Some(view) = cx.rendered_views.get(&self.view.id()) { "child": if let Some(view) = cx.rendered_views.get(&self.view.id()) {
view.debug(cx) view.debug(cx)
} else { } else {