Extract AnyElement::{measure,draw}

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-11-09 17:30:41 +01:00
parent ad3b0bd227
commit cfee1401ed
2 changed files with 44 additions and 25 deletions

View file

@ -1,4 +1,6 @@
use crate::{BorrowWindow, Bounds, ElementId, LayoutId, Pixels, ViewContext};
use crate::{
AvailableSpace, BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, Size, ViewContext,
};
use derive_more::{Deref, DerefMut};
pub(crate) use smallvec::SmallVec;
use std::{any::Any, mem};
@ -196,6 +198,33 @@ impl<V> AnyElement<V> {
pub fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
self.0.paint(view_state, cx)
}
/// Initializes this element and performs layout within the given available space to determine its size.
pub fn measure(
&mut self,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
) -> Size<Pixels> {
self.initialize(view_state, cx);
let layout_id = self.layout(view_state, cx);
cx.compute_layout(layout_id, available_space);
cx.layout_bounds(layout_id).size
}
/// Initializes this element and performs layout in the available space, then paints it at the given origin.
pub fn draw(
&mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
view_state: &mut V,
cx: &mut ViewContext<V>,
) {
self.initialize(view_state, cx);
let layout_id = self.layout(view_state, cx);
cx.compute_layout(layout_id, available_space);
cx.with_element_offset(Some(origin), |cx| self.paint(view_state, cx))
}
}
pub trait Component<V> {