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::{any::TypeId, fmt};
10
11#[derive(Clone, Debug)]
17pub struct AnyView {
18 entity: AnyEntity,
19 render: fn(&AnyView, &mut Window, &mut App) -> AnyElement,
20}
21
22impl<V: Render> From<Entity<V>> for AnyView {
23 fn from(value: Entity<V>) -> Self {
24 AnyView {
25 entity: value.into_any(),
26 render: any_view::render::<V>,
27 }
28 }
29}
30
31impl AnyView {
32 pub fn cached(self, style: StyleRefinement) -> ViewElement<AnyView> {
38 crate::fast::splice::cached(self.clone(), self, style)
39 }
40
41 pub fn downgrade(&self) -> AnyWeakView {
43 AnyWeakView {
44 entity: self.entity.downgrade(),
45 render: self.render,
46 }
47 }
48
49 pub fn downcast<T: 'static>(self) -> Result<Entity<T>, Self> {
52 match self.entity.downcast() {
53 Ok(entity) => Ok(entity),
54 Err(entity) => Err(Self {
55 entity,
56 render: self.render,
57 }),
58 }
59 }
60
61 pub fn entity_type(&self) -> TypeId {
63 self.entity.entity_type
64 }
65
66 pub fn entity_id(&self) -> EntityId {
68 self.entity.entity_id()
69 }
70}
71
72impl PartialEq for AnyView {
73 fn eq(&self, other: &Self) -> bool {
74 self.entity == other.entity
75 }
76}
77
78impl Eq for AnyView {}
79
80impl View for AnyView {
84 fn entity_id(&self) -> Option<EntityId> {
85 Some(self.entity.entity_id())
86 }
87
88 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
89 (self.render)(&self, window, cx)
90 }
91}
92
93impl<V: 'static + Render> IntoElement for Entity<V> {
94 type Element = ViewElement<Entity<V>>;
95
96 fn into_element(self) -> Self::Element {
97 crate::fast::splice::rebuildable(self.clone(), self.into())
98 }
99
100 #[inline(never)]
101 fn into_any_element(self) -> AnyElement {
102 self.into_element().into_any()
103 }
104}
105
106impl IntoElement for AnyView {
107 type Element = ViewElement<AnyView>;
108
109 fn into_element(self) -> Self::Element {
110 crate::fast::splice::rebuildable(self.clone(), self)
111 }
112}
113
114pub struct AnyWeakView {
116 entity: AnyWeakEntity,
117 render: fn(&AnyView, &mut Window, &mut App) -> AnyElement,
118}
119
120impl AnyWeakView {
121 pub fn upgrade(&self) -> Option<AnyView> {
123 let entity = self.entity.upgrade()?;
124 Some(AnyView {
125 entity,
126 render: self.render,
127 })
128 }
129}
130
131impl<V: 'static + Render> From<WeakEntity<V>> for AnyWeakView {
132 fn from(view: WeakEntity<V>) -> Self {
133 AnyWeakView {
134 entity: view.into(),
135 render: any_view::render::<V>,
136 }
137 }
138}
139
140impl PartialEq for AnyWeakView {
141 fn eq(&self, other: &Self) -> bool {
142 self.entity == other.entity
143 }
144}
145
146impl std::fmt::Debug for AnyWeakView {
147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148 f.debug_struct("AnyWeakView")
149 .field("entity_id", &self.entity.entity_id)
150 .finish_non_exhaustive()
151 }
152}
153
154mod any_view {
155 use crate::{AnyElement, AnyView, App, IntoElement, Render, Window};
156
157 pub(crate) fn render<V: 'static + Render>(
158 view: &AnyView,
159 window: &mut Window,
160 cx: &mut App,
161 ) -> AnyElement {
162 let view = view.clone().downcast::<V>().unwrap();
163 #[cfg(debug_assertions)]
166 window
167 .a11y
168 .view_type_names
169 .insert(view.entity_id(), std::any::type_name::<V>());
170 crate::fast::dependencies::render_next(&mut cx.entities, view.entity_id());
171 view.update(cx, |view, cx| view.render(window, cx).into_any_element())
172 }
173}
174
175pub trait View: 'static + Sized {
187 fn entity_id(&self) -> Option<EntityId>;
196
197 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement;
199}
200
201impl<T: RenderOnce> View for T {
203 fn entity_id(&self) -> Option<EntityId> {
204 None
205 }
206
207 #[inline]
208 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
209 RenderOnce::render(self, window, cx)
210 }
211}
212
213impl<T: Render> View for Entity<T> {
215 fn entity_id(&self) -> Option<EntityId> {
216 Some(Entity::entity_id(self))
217 }
218
219 #[inline]
220 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
221 crate::fast::dependencies::render_next(&mut cx.entities, self.entity_id());
222 self.update(cx, |this, cx| {
223 Render::render(this, window, cx).into_any_element()
224 })
225 }
226}
227
228impl<T: Render> Entity<T> {
229 #[track_caller]
237 pub fn cached(self, style: StyleRefinement) -> ViewElement<Entity<T>> {
238 crate::fast::splice::cached(self.clone(), self.into(), style)
239 }
240}
241
242#[doc(hidden)]
245pub struct ViewElement<V: View> {
246 pub(crate) view: Option<V>,
247 pub(crate) entity_id: Option<EntityId>,
248 pub(crate) cached_style: Option<StyleRefinement>,
249 pub(crate) rebuild: crate::fast::splice::RebuildHandle,
250 #[cfg(debug_assertions)]
251 source: &'static core::panic::Location<'static>,
252}
253
254impl<V: View> ViewElement<V> {
255 #[track_caller]
257 pub fn new(view: V) -> Self {
258 let entity_id = view.entity_id();
259 ViewElement {
260 entity_id,
261 cached_style: None,
262 rebuild: crate::fast::splice::RebuildHandle::default(),
263 view: Some(view),
264 #[cfg(debug_assertions)]
265 source: core::panic::Location::caller(),
266 }
267 }
268
269 pub(crate) fn cached(mut self, style: StyleRefinement) -> Self {
279 self.cached_style = Some(style);
280 self
281 }
282}
283
284impl<V: View> IntoElement for ViewElement<V> {
285 type Element = Self;
286
287 fn into_element(self) -> Self::Element {
288 self
289 }
290}
291
292impl<V: View> Element for ViewElement<V> {
293 type RequestLayoutState = crate::fast::retained::ViewLayoutState;
294 type PrepaintState = crate::fast::retained::ViewPrepaintState;
295
296 fn id(&self) -> Option<ElementId> {
297 self.entity_id.map(ElementId::View)
298 }
299
300 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
301 #[cfg(debug_assertions)]
302 return Some(self.source);
303
304 #[cfg(not(debug_assertions))]
305 return None;
306 }
307
308 fn request_layout(
309 &mut self,
310 _id: Option<&GlobalElementId>,
311 _inspector_id: Option<&InspectorElementId>,
312 window: &mut Window,
313 cx: &mut App,
314 ) -> (LayoutId, Self::RequestLayoutState) {
315 crate::fast::retained::request_view_layout(self, _id, window, cx)
316 }
317
318 fn prepaint(
319 &mut self,
320 global_id: Option<&GlobalElementId>,
321 _inspector_id: Option<&InspectorElementId>,
322 bounds: Bounds<Pixels>,
323 element: &mut Self::RequestLayoutState,
324 window: &mut Window,
325 cx: &mut App,
326 ) -> Self::PrepaintState {
327 crate::fast::retained::prepaint_view(self, global_id, bounds, element, window, cx)
328 }
329
330 fn paint(
331 &mut self,
332 global_id: Option<&GlobalElementId>,
333 _inspector_id: Option<&InspectorElementId>,
334 _bounds: Bounds<Pixels>,
335 _request_layout: &mut Self::RequestLayoutState,
336 element: &mut Self::PrepaintState,
337 window: &mut Window,
338 cx: &mut App,
339 ) {
340 crate::fast::retained::paint_view(self, global_id, element, window, cx)
341 }
342}
343
344pub struct EmptyView;
346
347impl Render for EmptyView {
348 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
349 Empty
350 }
351}