1use crate::{
3 AnyElement, AnyEntity, AnyWeakEntity, App, Bounds, Context, Element, ElementId, Entity,
4 EntityId, GlobalElementId, InspectorElementId, IntoElement, LayoutId, Pixels, Render,
5 RenderOnce, StyleRefinement, WeakEntity,
6};
7use crate::{Empty, Window};
8use anyhow::Result;
9use std::{
10 any::{TypeId, type_name},
11 fmt,
12};
13
14#[derive(Clone, Debug)]
20pub struct AnyView {
21 entity: AnyEntity,
22 render: fn(&AnyView, &mut Window, &mut App) -> AnyElement,
23 pub(crate) view_type_name: &'static str,
24}
25
26impl<V: Render> From<Entity<V>> for AnyView {
27 fn from(value: Entity<V>) -> Self {
28 AnyView {
29 entity: value.into_any(),
30 render: any_view::render::<V>,
31 view_type_name: type_name::<V>(),
32 }
33 }
34}
35
36impl AnyView {
37 pub fn cached(self, style: StyleRefinement) -> ViewElement<AnyView> {
43 self.into_element().cached(style)
44 }
45
46 pub fn downgrade(&self) -> AnyWeakView {
48 AnyWeakView {
49 entity: self.entity.downgrade(),
50 render: self.render,
51 view_type_name: self.view_type_name,
52 }
53 }
54
55 pub fn downcast<T: 'static>(self) -> Result<Entity<T>, Self> {
58 match self.entity.downcast() {
59 Ok(entity) => Ok(entity),
60 Err(entity) => Err(Self {
61 entity,
62 render: self.render,
63 view_type_name: self.view_type_name,
64 }),
65 }
66 }
67
68 pub fn entity_type(&self) -> TypeId {
70 self.entity.entity_type
71 }
72
73 pub fn entity_id(&self) -> EntityId {
75 self.entity.entity_id()
76 }
77}
78
79impl PartialEq for AnyView {
80 fn eq(&self, other: &Self) -> bool {
81 self.entity == other.entity
82 }
83}
84
85impl Eq for AnyView {}
86
87impl View for AnyView {
91 fn entity_id(&self) -> Option<EntityId> {
92 Some(self.entity.entity_id())
93 }
94
95 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
96 (self.render)(&self, window, cx)
97 }
98}
99
100impl<V: 'static + Render> IntoElement for Entity<V> {
101 type Element = ViewElement<Entity<V>>;
102
103 fn into_element(self) -> Self::Element {
104 crate::fast::splice::rebuildable(self.clone(), self.into())
105 }
106
107 #[inline(never)]
108 fn into_any_element(self) -> AnyElement {
109 self.into_element().into_any()
110 }
111}
112
113impl IntoElement for AnyView {
114 type Element = ViewElement<AnyView>;
115
116 fn into_element(self) -> Self::Element {
117 crate::fast::splice::rebuildable(self.clone(), self)
118 }
119}
120
121pub struct AnyWeakView {
123 entity: AnyWeakEntity,
124 render: fn(&AnyView, &mut Window, &mut App) -> AnyElement,
125 view_type_name: &'static str,
126}
127
128impl AnyWeakView {
129 pub fn upgrade(&self) -> Option<AnyView> {
131 let entity = self.entity.upgrade()?;
132 Some(AnyView {
133 entity,
134 render: self.render,
135 view_type_name: self.view_type_name,
136 })
137 }
138}
139
140impl<V: 'static + Render> From<WeakEntity<V>> for AnyWeakView {
141 fn from(view: WeakEntity<V>) -> Self {
142 AnyWeakView {
143 entity: view.into(),
144 render: any_view::render::<V>,
145 view_type_name: type_name::<V>(),
146 }
147 }
148}
149
150impl PartialEq for AnyWeakView {
151 fn eq(&self, other: &Self) -> bool {
152 self.entity == other.entity
153 }
154}
155
156impl std::fmt::Debug for AnyWeakView {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 f.debug_struct("AnyWeakView")
159 .field("entity_id", &self.entity.entity_id)
160 .finish_non_exhaustive()
161 }
162}
163
164mod any_view {
165 use crate::{AnyElement, AnyView, App, IntoElement, Render, Window};
166
167 pub(crate) fn render<V: 'static + Render>(
168 view: &AnyView,
169 window: &mut Window,
170 cx: &mut App,
171 ) -> AnyElement {
172 let view = view.clone().downcast::<V>().unwrap();
173 #[cfg(debug_assertions)]
176 window
177 .a11y
178 .view_type_names
179 .insert(view.entity_id(), std::any::type_name::<V>());
180 crate::fast::dependencies::render_next(&mut cx.entities, view.entity_id());
181 view.update(cx, |view, cx| view.render(window, cx).into_any_element())
182 }
183}
184
185pub trait View: 'static + Sized {
197 fn entity_id(&self) -> Option<EntityId>;
206
207 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement;
209}
210
211impl<T: RenderOnce> View for T {
213 fn entity_id(&self) -> Option<EntityId> {
214 None
215 }
216
217 #[inline]
218 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
219 RenderOnce::render(self, window, cx)
220 }
221}
222
223impl<T: Render> View for Entity<T> {
225 fn entity_id(&self) -> Option<EntityId> {
226 Some(Entity::entity_id(self))
227 }
228
229 #[inline]
230 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
231 crate::fast::dependencies::render_next(&mut cx.entities, self.entity_id());
232 self.update(cx, |this, cx| {
233 Render::render(this, window, cx).into_any_element()
234 })
235 }
236}
237
238impl<T: Render> Entity<T> {
239 #[track_caller]
247 pub fn cached(self, style: StyleRefinement) -> ViewElement<Entity<T>> {
248 crate::fast::splice::cached(self.clone(), self.into(), style)
249 }
250}
251
252#[doc(hidden)]
255pub struct ViewElement<V: View> {
256 pub(crate) view: Option<V>,
257 pub(crate) entity_id: Option<EntityId>,
258 pub(crate) cached_style: Option<StyleRefinement>,
259 pub(crate) rebuild: crate::fast::splice::RebuildHandle,
260 #[cfg(feature = "profiler")]
261 pub(crate) view_type_name: &'static str,
262 #[cfg(debug_assertions)]
263 source: &'static core::panic::Location<'static>,
264}
265
266impl<V: View> ViewElement<V> {
267 #[track_caller]
269 pub fn new(view: V) -> Self {
270 Self::with_type_name(view, type_name::<V>())
271 }
272
273 #[track_caller]
276 #[cfg_attr(not(feature = "profiler"), expect(unused_variables))]
277 pub(crate) fn with_type_name(view: V, view_type_name: &'static str) -> Self {
278 let entity_id = view.entity_id();
279 ViewElement {
280 entity_id,
281 cached_style: None,
282 rebuild: crate::fast::splice::RebuildHandle::default(),
283 #[cfg(feature = "profiler")]
284 view_type_name,
285 view: Some(view),
286 #[cfg(debug_assertions)]
287 source: core::panic::Location::caller(),
288 }
289 }
290
291 pub(crate) fn cached(mut self, style: StyleRefinement) -> Self {
301 self.cached_style = Some(style);
302 self
303 }
304}
305
306impl<V: View> IntoElement for ViewElement<V> {
307 type Element = Self;
308
309 fn into_element(self) -> Self::Element {
310 self
311 }
312}
313
314impl<V: View> Element for ViewElement<V> {
315 type RequestLayoutState = crate::fast::retained::ViewLayoutState;
316 type PrepaintState = crate::fast::retained::ViewPrepaintState;
317
318 fn id(&self) -> Option<ElementId> {
319 self.entity_id.map(ElementId::View)
320 }
321
322 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
323 #[cfg(debug_assertions)]
324 return Some(self.source);
325
326 #[cfg(not(debug_assertions))]
327 return None;
328 }
329
330 fn request_layout(
331 &mut self,
332 _id: Option<&GlobalElementId>,
333 _inspector_id: Option<&InspectorElementId>,
334 window: &mut Window,
335 cx: &mut App,
336 ) -> (LayoutId, Self::RequestLayoutState) {
337 crate::fast::retained::request_view_layout(self, _id, window, cx)
338 }
339
340 fn prepaint(
341 &mut self,
342 global_id: Option<&GlobalElementId>,
343 _inspector_id: Option<&InspectorElementId>,
344 bounds: Bounds<Pixels>,
345 element: &mut Self::RequestLayoutState,
346 window: &mut Window,
347 cx: &mut App,
348 ) -> Self::PrepaintState {
349 crate::fast::retained::prepaint_view(self, global_id, bounds, element, window, cx)
350 }
351
352 fn paint(
353 &mut self,
354 global_id: Option<&GlobalElementId>,
355 _inspector_id: Option<&InspectorElementId>,
356 _bounds: Bounds<Pixels>,
357 _request_layout: &mut Self::RequestLayoutState,
358 element: &mut Self::PrepaintState,
359 window: &mut Window,
360 cx: &mut App,
361 ) {
362 crate::fast::retained::paint_view(self, global_id, element, window, cx)
363 }
364}
365
366pub struct EmptyView;
368
369impl Render for EmptyView {
370 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
371 Empty
372 }
373}