use crate::Empty; use crate::{ seal::Sealed, AnyElement, AnyModel, AnyWeakModel, AppContext, Bounds, ContentMask, Element, ElementId, Entity, EntityId, Flatten, FocusHandle, FocusableView, GlobalElementId, IntoElement, LayoutId, Model, PaintIndex, Pixels, PrepaintStateIndex, Render, Style, StyleRefinement, TextStyle, ViewContext, VisualContext, WeakModel, WindowContext, }; use anyhow::{Context, Result}; use refineable::Refineable; use std::mem; use std::{ any::{type_name, TypeId}, fmt, hash::{Hash, Hasher}, ops::Range, }; /// A view is a piece of state that can be presented on screen by implementing the [Render] trait. /// Views implement [Element] and can composed with other views, and every window is created with a root view. pub struct View { /// A view is just a [Model] whose type implements `Render`, and the model is accessible via this field. pub model: Model, } impl Sealed for View {} struct AnyViewState { prepaint_range: Range, paint_range: Range, cache_key: ViewCacheKey, } #[derive(Default)] struct ViewCacheKey { bounds: Bounds, content_mask: ContentMask, text_style: TextStyle, } impl Entity for View { type Weak = WeakView; fn entity_id(&self) -> EntityId { self.model.entity_id } fn downgrade(&self) -> Self::Weak { WeakView { model: self.model.downgrade(), } } fn upgrade_from(weak: &Self::Weak) -> Option where Self: Sized, { let model = weak.model.upgrade()?; Some(View { model }) } } impl View { /// Convert this strong view reference into a weak view reference. pub fn downgrade(&self) -> WeakView { Entity::downgrade(self) } /// Updates the view's state with the given function, which is passed a mutable reference and a context. pub fn update( &self, cx: &mut C, f: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R, ) -> C::Result where C: VisualContext, { cx.update_view(self, f) } /// Obtain a read-only reference to this view's state. pub fn read<'a>(&self, cx: &'a AppContext) -> &'a V { self.model.read(cx) } /// Gets a [FocusHandle] for this view when its state implements [FocusableView]. pub fn focus_handle(&self, cx: &AppContext) -> FocusHandle where V: FocusableView, { self.read(cx).focus_handle(cx) } } impl Element for View { type RequestLayoutState = AnyElement; type PrepaintState = (); fn id(&self) -> Option { Some(ElementId::View(self.entity_id())) } fn request_layout( &mut self, _id: Option<&GlobalElementId>, cx: &mut WindowContext, ) -> (LayoutId, Self::RequestLayoutState) { let mut element = self.update(cx, |view, cx| view.render(cx).into_any_element()); let layout_id = element.request_layout(cx); (layout_id, element) } fn prepaint( &mut self, _id: Option<&GlobalElementId>, _: Bounds, element: &mut Self::RequestLayoutState, cx: &mut WindowContext, ) { cx.set_view_id(self.entity_id()); element.prepaint(cx); } fn paint( &mut self, _id: Option<&GlobalElementId>, _: Bounds, element: &mut Self::RequestLayoutState, _: &mut Self::PrepaintState, cx: &mut WindowContext, ) { element.paint(cx); } } impl Clone for View { fn clone(&self) -> Self { Self { model: self.model.clone(), } } } impl std::fmt::Debug for View { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct(&format!("View<{}>", type_name::())) .field("entity_id", &self.model.entity_id) .finish_non_exhaustive() } } impl Hash for View { fn hash(&self, state: &mut H) { self.model.hash(state); } } impl PartialEq for View { fn eq(&self, other: &Self) -> bool { self.model == other.model } } impl Eq for View {} /// A weak variant of [View] which does not prevent the view from being released. pub struct WeakView { pub(crate) model: WeakModel, } impl WeakView { /// Gets the entity id associated with this handle. pub fn entity_id(&self) -> EntityId { self.model.entity_id } /// Obtain a strong handle for the view if it hasn't been released. pub fn upgrade(&self) -> Option> { Entity::upgrade_from(self) } /// Updates this view's state if it hasn't been released. /// Returns an error if this view has been released. pub fn update( &self, cx: &mut C, f: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R, ) -> Result where C: VisualContext, Result>: Flatten, { let view = self.upgrade().context("error upgrading view")?; Ok(view.update(cx, f)).flatten() } /// Assert that the view referenced by this handle has been released. #[cfg(any(test, feature = "test-support"))] pub fn assert_released(&self) { self.model.assert_released() } } impl Clone for WeakView { fn clone(&self) -> Self { Self { model: self.model.clone(), } } } impl Hash for WeakView { fn hash(&self, state: &mut H) { self.model.hash(state); } } impl PartialEq for WeakView { fn eq(&self, other: &Self) -> bool { self.model == other.model } } impl Eq for WeakView {} /// A dynamically-typed handle to a view, which can be downcast to a [View] for a specific type. #[derive(Clone, Debug)] pub struct AnyView { model: AnyModel, render: fn(&AnyView, &mut WindowContext) -> AnyElement, cached_style: Option, } impl AnyView { /// Indicate that this view should be cached when using it as an element. /// When using this method, the view's previous layout and paint will be recycled from the previous frame if [ViewContext::notify] has not been called since it was rendered. /// The one exception is when [WindowContext::refresh] is called, in which case caching is ignored. pub fn cached(mut self, style: StyleRefinement) -> Self { self.cached_style = Some(style); self } /// Convert this to a weak handle. pub fn downgrade(&self) -> AnyWeakView { AnyWeakView { model: self.model.downgrade(), render: self.render, } } /// Convert this to a [View] of a specific type. /// If this handle does not contain a view of the specified type, returns itself in an `Err` variant. pub fn downcast(self) -> Result, Self> { match self.model.downcast() { Ok(model) => Ok(View { model }), Err(model) => Err(Self { model, render: self.render, cached_style: self.cached_style, }), } } /// Gets the [TypeId] of the underlying view. pub fn entity_type(&self) -> TypeId { self.model.entity_type } /// Gets the entity id of this handle. pub fn entity_id(&self) -> EntityId { self.model.entity_id() } } impl From> for AnyView { fn from(value: View) -> Self { AnyView { model: value.model.into_any(), render: any_view::render::, cached_style: None, } } } impl PartialEq for AnyView { fn eq(&self, other: &Self) -> bool { self.model == other.model } } impl Eq for AnyView {} impl Element for AnyView { type RequestLayoutState = Option; type PrepaintState = Option; fn id(&self) -> Option { Some(ElementId::View(self.entity_id())) } fn request_layout( &mut self, _id: Option<&GlobalElementId>, cx: &mut WindowContext, ) -> (LayoutId, Self::RequestLayoutState) { if let Some(style) = self.cached_style.as_ref() { let mut root_style = Style::default(); root_style.refine(style); let layout_id = cx.request_layout(root_style, None); (layout_id, None) } else { let mut element = (self.render)(self, cx); let layout_id = element.request_layout(cx); (layout_id, Some(element)) } } fn prepaint( &mut self, global_id: Option<&GlobalElementId>, bounds: Bounds, element: &mut Self::RequestLayoutState, cx: &mut WindowContext, ) -> Option { cx.set_view_id(self.entity_id()); if self.cached_style.is_some() { cx.with_element_state::(global_id.unwrap(), |element_state, cx| { let content_mask = cx.content_mask(); let text_style = cx.text_style(); if let Some(mut element_state) = element_state { if element_state.cache_key.bounds == bounds && element_state.cache_key.content_mask == content_mask && element_state.cache_key.text_style == text_style && !cx.window.dirty_views.contains(&self.entity_id()) && !cx.window.refreshing { let prepaint_start = cx.prepaint_index(); cx.reuse_prepaint(element_state.prepaint_range.clone()); let prepaint_end = cx.prepaint_index(); element_state.prepaint_range = prepaint_start..prepaint_end; return (None, element_state); } } let refreshing = mem::replace(&mut cx.window.refreshing, true); let prepaint_start = cx.prepaint_index(); let mut element = (self.render)(self, cx); element.layout_as_root(bounds.size.into(), cx); element.prepaint_at(bounds.origin, cx); let prepaint_end = cx.prepaint_index(); cx.window.refreshing = refreshing; ( Some(element), AnyViewState { prepaint_range: prepaint_start..prepaint_end, paint_range: PaintIndex::default()..PaintIndex::default(), cache_key: ViewCacheKey { bounds, content_mask, text_style, }, }, ) }) } else { let mut element = element.take().unwrap(); element.prepaint(cx); Some(element) } } fn paint( &mut self, global_id: Option<&GlobalElementId>, _bounds: Bounds, _: &mut Self::RequestLayoutState, element: &mut Self::PrepaintState, cx: &mut WindowContext, ) { if self.cached_style.is_some() { cx.with_element_state::(global_id.unwrap(), |element_state, cx| { let mut element_state = element_state.unwrap(); let paint_start = cx.paint_index(); if let Some(element) = element { let refreshing = mem::replace(&mut cx.window.refreshing, true); element.paint(cx); cx.window.refreshing = refreshing; } else { cx.reuse_paint(element_state.paint_range.clone()); } let paint_end = cx.paint_index(); element_state.paint_range = paint_start..paint_end; ((), element_state) }) } else { element.as_mut().unwrap().paint(cx); } } } impl IntoElement for View { type Element = View; fn into_element(self) -> Self::Element { self } } impl IntoElement for AnyView { type Element = Self; fn into_element(self) -> Self::Element { self } } /// A weak, dynamically-typed view handle that does not prevent the view from being released. pub struct AnyWeakView { model: AnyWeakModel, render: fn(&AnyView, &mut WindowContext) -> AnyElement, } impl AnyWeakView { /// Convert to a strongly-typed handle if the referenced view has not yet been released. pub fn upgrade(&self) -> Option { let model = self.model.upgrade()?; Some(AnyView { model, render: self.render, cached_style: None, }) } } impl From> for AnyWeakView { fn from(view: WeakView) -> Self { Self { model: view.model.into(), render: any_view::render::, } } } impl PartialEq for AnyWeakView { fn eq(&self, other: &Self) -> bool { self.model == other.model } } impl std::fmt::Debug for AnyWeakView { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("AnyWeakView") .field("entity_id", &self.model.entity_id) .finish_non_exhaustive() } } mod any_view { use crate::{AnyElement, AnyView, IntoElement, Render, WindowContext}; pub(crate) fn render( view: &AnyView, cx: &mut WindowContext, ) -> AnyElement { let view = view.clone().downcast::().unwrap(); view.update(cx, |view, cx| view.render(cx).into_any_element()) } } /// A view that renders nothing pub struct EmptyView; impl Render for EmptyView { fn render(&mut self, _cx: &mut ViewContext) -> impl IntoElement { Empty } }