zed/crates/gpui3/src/element.rs

643 lines
20 KiB
Rust
Raw Normal View History

2023-10-19 19:48:23 +00:00
use crate::{
2023-10-19 21:21:26 +00:00
AppContext, BorrowWindow, Bounds, DispatchPhase, ElementId, FocusHandle, FocusListeners,
KeyDownEvent, KeyListener, KeyMatch, LayoutId, MouseClickEvent, MouseClickListener,
MouseDownEvent, MouseDownListener, MouseMoveEvent, MouseMoveListener, MouseUpEvent,
MouseUpListener, Pixels, Point, ScrollWheelEvent, ScrollWheelListener, SharedString, Style,
StyleRefinement, ViewContext, WindowContext,
2023-10-19 19:48:23 +00:00
};
2023-10-19 21:21:26 +00:00
use collections::HashMap;
2023-10-10 18:41:28 +00:00
use derive_more::{Deref, DerefMut};
2023-10-19 21:00:19 +00:00
use parking_lot::Mutex;
2023-10-19 20:04:56 +00:00
use refineable::Refineable;
2023-09-21 19:46:31 +00:00
pub(crate) use smallvec::SmallVec;
2023-10-19 21:00:19 +00:00
use std::{any::TypeId, mem, sync::Arc};
2023-09-19 19:19:22 +00:00
2023-10-12 17:30:00 +00:00
pub trait Element: 'static + Send + Sync + IntoAnyElement<Self::ViewState> {
2023-10-11 04:14:47 +00:00
type ViewState: 'static + Send + Sync;
type ElementState: 'static + Send + Sync;
2023-09-19 19:19:22 +00:00
2023-10-17 09:08:48 +00:00
fn id(&self) -> Option<ElementId>;
2023-10-10 18:41:28 +00:00
2023-10-18 12:12:50 +00:00
fn initialize(
2023-09-19 19:19:22 +00:00
&mut self,
2023-10-18 12:12:50 +00:00
view_state: &mut Self::ViewState,
2023-10-11 04:14:47 +00:00
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
2023-10-18 12:12:50 +00:00
) -> Self::ElementState;
fn layout(
&mut self,
view_state: &mut Self::ViewState,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) -> LayoutId;
2023-09-19 19:19:22 +00:00
fn paint(
2023-09-19 19:19:22 +00:00
&mut self,
2023-10-06 03:02:26 +00:00
bounds: Bounds<Pixels>,
2023-10-18 12:12:50 +00:00
view_state: &mut Self::ViewState,
2023-10-11 04:14:47 +00:00
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
);
2023-09-19 19:19:22 +00:00
}
2023-10-11 04:14:47 +00:00
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
2023-10-19 17:43:28 +00:00
pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
2023-10-10 18:41:28 +00:00
2023-10-19 21:54:17 +00:00
pub trait ElementInteraction<V: 'static + Send + Sync>: 'static + Send + Sync {
fn as_stateless(&self) -> &StatelessInteraction<V>;
fn as_stateless_mut(&mut self) -> &mut StatelessInteraction<V>;
2023-10-19 20:28:42 +00:00
fn as_stateful(&self) -> Option<&StatefulInteractivity<V>>;
2023-10-19 21:00:19 +00:00
fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity<V>>;
2023-10-19 21:21:26 +00:00
fn initialize<R>(
&mut self,
cx: &mut ViewContext<V>,
f: impl FnOnce(&mut ViewContext<V>) -> R,
) -> R {
if let Some(stateful) = self.as_stateful_mut() {
cx.with_element_id(stateful.id.clone(), |global_id, cx| {
stateful.key_listeners.push((
TypeId::of::<KeyDownEvent>(),
Arc::new(move |_, key_down, context, phase, cx| {
if phase == DispatchPhase::Bubble {
let key_down = key_down.downcast_ref::<KeyDownEvent>().unwrap();
if let KeyMatch::Some(action) =
cx.match_keystroke(&global_id, &key_down.keystroke, context)
{
return Some(action);
}
}
None
}),
));
let result = stateful.stateless.initialize(cx, f);
stateful.key_listeners.pop();
result
})
} else {
cx.with_key_listeners(&self.as_stateless().key_listeners, f)
}
}
2023-10-19 21:30:14 +00:00
fn refine_style(
&self,
style: &mut Style,
bounds: Bounds<Pixels>,
2023-10-19 21:35:09 +00:00
element_state: &InteractiveElementState,
2023-10-19 21:30:14 +00:00
cx: &mut ViewContext<V>,
) {
2023-10-19 21:21:26 +00:00
let mouse_position = cx.mouse_position();
let stateless = self.as_stateless();
2023-10-19 21:30:14 +00:00
if let Some(group_hover) = stateless.group_hover_style.as_ref() {
if let Some(group_bounds) = GroupBounds::get(&group_hover.group, cx) {
2023-10-19 21:21:26 +00:00
if group_bounds.contains_point(&mouse_position) {
style.refine(&group_hover.style);
}
}
}
if bounds.contains_point(&mouse_position) {
style.refine(&stateless.hover_style);
}
2023-10-19 21:30:14 +00:00
if let Some(stateful) = self.as_stateful() {
2023-10-19 21:35:09 +00:00
let active_state = element_state.active_state.lock();
2023-10-19 21:30:14 +00:00
if active_state.group {
if let Some(group_style) = stateful.group_active_style.as_ref() {
style.refine(&group_style.style);
}
}
if active_state.element {
style.refine(&stateful.active_style);
}
}
2023-10-19 21:21:26 +00:00
}
2023-10-19 21:00:19 +00:00
fn paint(
&mut self,
bounds: Bounds<Pixels>,
2023-10-19 21:35:09 +00:00
element_state: &InteractiveElementState,
2023-10-19 21:00:19 +00:00
cx: &mut ViewContext<V>,
) {
let stateless = self.as_stateless();
for listener in stateless.mouse_down_listeners.iter().cloned() {
cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| {
listener(state, event, &bounds, phase, cx);
})
}
for listener in stateless.mouse_up_listeners.iter().cloned() {
cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| {
listener(state, event, &bounds, phase, cx);
})
}
for listener in stateless.mouse_move_listeners.iter().cloned() {
cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| {
listener(state, event, &bounds, phase, cx);
})
}
for listener in stateless.scroll_wheel_listeners.iter().cloned() {
cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| {
listener(state, event, &bounds, phase, cx);
})
}
2023-10-19 21:21:26 +00:00
let hover_group_bounds = stateless
2023-10-19 21:30:14 +00:00
.group_hover_style
2023-10-19 21:21:26 +00:00
.as_ref()
.and_then(|group_hover| GroupBounds::get(&group_hover.group, cx));
if let Some(group_bounds) = hover_group_bounds {
paint_hover_listener(group_bounds, cx);
}
if stateless.hover_style.is_some() {
paint_hover_listener(bounds, cx);
}
2023-10-19 21:00:19 +00:00
if let Some(stateful) = self.as_stateful() {
let click_listeners = stateful.mouse_click_listeners.clone();
2023-10-19 21:35:09 +00:00
let pending_click = element_state.pending_click.clone();
2023-10-19 21:00:19 +00:00
let mouse_down = pending_click.lock().clone();
if let Some(mouse_down) = mouse_down {
cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
let mouse_click = MouseClickEvent {
down: mouse_down.clone(),
up: event.clone(),
};
for listener in &click_listeners {
listener(state, &mouse_click, cx);
}
}
*pending_click.lock() = None;
});
} else {
cx.on_mouse_event(move |_state, event: &MouseDownEvent, phase, _cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
*pending_click.lock() = Some(event.clone());
}
});
2023-10-19 21:30:14 +00:00
}
2023-10-19 21:35:09 +00:00
let active_state = element_state.active_state.clone();
if active_state.lock().is_none() {
2023-10-19 21:30:14 +00:00
let active_group_bounds = stateful
.group_active_style
.as_ref()
.and_then(|group_active| GroupBounds::get(&group_active.group, cx));
cx.on_mouse_event(move |_view, down: &MouseDownEvent, phase, cx| {
if phase == DispatchPhase::Bubble {
let group = active_group_bounds
.map_or(false, |bounds| bounds.contains_point(&down.position));
let element = bounds.contains_point(&down.position);
if group || element {
2023-10-19 21:35:09 +00:00
*active_state.lock() = ActiveState { group, element };
2023-10-19 21:30:14 +00:00
cx.notify();
}
}
});
} else {
cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
if phase == DispatchPhase::Capture {
2023-10-19 21:35:09 +00:00
*active_state.lock() = ActiveState::default();
2023-10-19 21:30:14 +00:00
cx.notify();
}
});
}
2023-10-19 21:00:19 +00:00
}
}
2023-10-19 21:21:26 +00:00
}
2023-10-19 20:28:42 +00:00
2023-10-19 21:21:26 +00:00
fn paint_hover_listener<V>(bounds: Bounds<Pixels>, cx: &mut ViewContext<V>)
where
V: 'static + Send + Sync,
{
let hovered = bounds.contains_point(&cx.mouse_position());
cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
if phase == DispatchPhase::Capture {
if bounds.contains_point(&event.position) != hovered {
cx.notify();
}
2023-10-19 20:28:42 +00:00
}
2023-10-19 21:21:26 +00:00
});
2023-10-19 20:28:42 +00:00
}
#[derive(Deref, DerefMut)]
pub struct StatefulInteractivity<V: 'static + Send + Sync> {
pub id: ElementId,
#[deref]
#[deref_mut]
2023-10-19 21:54:17 +00:00
stateless: StatelessInteraction<V>,
2023-10-19 21:00:19 +00:00
pub mouse_click_listeners: SmallVec<[MouseClickListener<V>; 2]>,
2023-10-19 21:30:14 +00:00
pub active_style: StyleRefinement,
pub group_active_style: Option<GroupStyle>,
2023-10-17 19:11:52 +00:00
}
2023-10-19 21:54:17 +00:00
impl<V> ElementInteraction<V> for StatefulInteractivity<V>
2023-10-19 20:28:42 +00:00
where
V: 'static + Send + Sync,
{
fn as_stateful(&self) -> Option<&StatefulInteractivity<V>> {
Some(self)
}
2023-10-19 21:00:19 +00:00
fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity<V>> {
Some(self)
}
2023-10-19 21:54:17 +00:00
fn as_stateless(&self) -> &StatelessInteraction<V> {
2023-10-19 21:00:19 +00:00
&self.stateless
}
2023-10-19 21:54:17 +00:00
fn as_stateless_mut(&mut self) -> &mut StatelessInteraction<V> {
2023-10-19 21:00:19 +00:00
&mut self.stateless
}
2023-10-19 20:28:42 +00:00
}
2023-10-17 19:11:52 +00:00
2023-10-19 20:28:42 +00:00
impl<V> From<ElementId> for StatefulInteractivity<V>
where
V: 'static + Send + Sync,
{
fn from(id: ElementId) -> Self {
Self {
id,
2023-10-19 21:54:17 +00:00
stateless: StatelessInteraction::default(),
2023-10-19 21:00:19 +00:00
mouse_click_listeners: SmallVec::new(),
2023-10-19 21:30:14 +00:00
active_style: StyleRefinement::default(),
group_active_style: None,
2023-10-19 20:28:42 +00:00
}
2023-10-17 19:11:52 +00:00
}
}
2023-10-19 21:54:17 +00:00
pub struct StatelessInteraction<V> {
2023-10-19 21:00:19 +00:00
pub mouse_down_listeners: SmallVec<[MouseDownListener<V>; 2]>,
pub mouse_up_listeners: SmallVec<[MouseUpListener<V>; 2]>,
pub mouse_move_listeners: SmallVec<[MouseMoveListener<V>; 2]>,
pub scroll_wheel_listeners: SmallVec<[ScrollWheelListener<V>; 2]>,
pub key_listeners: SmallVec<[(TypeId, KeyListener<V>); 32]>,
2023-10-19 21:21:26 +00:00
pub hover_style: StyleRefinement,
2023-10-19 21:30:14 +00:00
pub group_hover_style: Option<GroupStyle>,
2023-10-19 21:21:26 +00:00
}
pub struct GroupStyle {
pub group: SharedString,
pub style: StyleRefinement,
}
#[derive(Default)]
pub struct GroupBounds(HashMap<SharedString, SmallVec<[Bounds<Pixels>; 1]>>);
impl GroupBounds {
pub fn get(name: &SharedString, cx: &mut AppContext) -> Option<Bounds<Pixels>> {
cx.default_global::<Self>()
.0
.get(name)
.and_then(|bounds_stack| bounds_stack.last())
.cloned()
}
pub fn push(name: SharedString, bounds: Bounds<Pixels>, cx: &mut AppContext) {
cx.default_global::<Self>()
.0
.entry(name)
.or_default()
.push(bounds);
}
pub fn pop(name: &SharedString, cx: &mut AppContext) {
cx.default_global::<GroupBounds>()
.0
.get_mut(name)
.unwrap()
.pop();
}
}
2023-10-19 21:30:14 +00:00
#[derive(Copy, Clone, Default, Eq, PartialEq)]
2023-10-19 21:35:09 +00:00
struct ActiveState {
2023-10-19 21:30:14 +00:00
pub group: bool,
pub element: bool,
}
2023-10-19 21:35:09 +00:00
impl ActiveState {
2023-10-19 21:30:14 +00:00
pub fn is_none(&self) -> bool {
!self.group && !self.element
}
2023-10-19 21:00:19 +00:00
}
2023-10-19 20:28:42 +00:00
2023-10-19 21:35:09 +00:00
#[derive(Default)]
pub struct InteractiveElementState {
active_state: Arc<Mutex<ActiveState>>,
pending_click: Arc<Mutex<Option<MouseDownEvent>>>,
}
2023-10-19 21:54:17 +00:00
impl<V> Default for StatelessInteraction<V> {
2023-10-19 20:28:42 +00:00
fn default() -> Self {
2023-10-19 21:00:19 +00:00
Self {
mouse_down_listeners: SmallVec::new(),
mouse_up_listeners: SmallVec::new(),
mouse_move_listeners: SmallVec::new(),
scroll_wheel_listeners: SmallVec::new(),
key_listeners: SmallVec::new(),
2023-10-19 21:21:26 +00:00
hover_style: StyleRefinement::default(),
2023-10-19 21:30:14 +00:00
group_hover_style: None,
2023-10-19 21:00:19 +00:00
}
2023-10-19 20:28:42 +00:00
}
}
2023-10-18 11:36:26 +00:00
2023-10-19 21:54:17 +00:00
impl<V> ElementInteraction<V> for StatelessInteraction<V>
2023-10-19 20:28:42 +00:00
where
V: 'static + Send + Sync,
{
fn as_stateful(&self) -> Option<&StatefulInteractivity<V>> {
2023-10-17 19:11:52 +00:00
None
}
2023-10-19 21:00:19 +00:00
fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity<V>> {
None
}
2023-10-19 21:54:17 +00:00
fn as_stateless(&self) -> &StatelessInteraction<V> {
2023-10-19 21:00:19 +00:00
self
}
2023-10-19 21:54:17 +00:00
fn as_stateless_mut(&mut self) -> &mut StatelessInteraction<V> {
2023-10-19 21:00:19 +00:00
self
}
2023-10-17 19:11:52 +00:00
}
2023-10-19 21:54:17 +00:00
pub trait ElementFocus<V: 'static + Send + Sync>: 'static + Send + Sync {
fn as_focusable(&self) -> Option<&FocusEnabled<V>>;
2023-10-19 19:48:23 +00:00
fn initialize<R>(
&self,
cx: &mut ViewContext<V>,
f: impl FnOnce(&mut ViewContext<V>) -> R,
) -> R {
2023-10-19 19:57:09 +00:00
if let Some(focusable) = self.as_focusable() {
for listener in focusable.focus_listeners.iter().cloned() {
2023-10-19 19:48:23 +00:00
cx.on_focus_changed(move |view, event, cx| listener(view, event, cx));
}
2023-10-19 19:57:09 +00:00
cx.with_focus(focusable.focus_handle.clone(), |cx| f(cx))
2023-10-19 19:48:23 +00:00
} else {
f(cx)
}
}
2023-10-19 20:04:56 +00:00
fn refine_style(&self, style: &mut Style, cx: &WindowContext) {
if let Some(focusable) = self.as_focusable() {
if focusable.focus_handle.contains_focused(cx) {
style.refine(&focusable.focus_in_style);
}
if focusable.focus_handle.within_focused(cx) {
style.refine(&focusable.in_focus_style);
}
if focusable.focus_handle.is_focused(cx) {
style.refine(&focusable.focus_style);
}
}
}
fn paint(&self, bounds: Bounds<Pixels>, cx: &mut WindowContext) {
if let Some(focusable) = self.as_focusable() {
let focus_handle = focusable.focus_handle.clone();
cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if !cx.default_prevented() {
cx.focus(&focus_handle);
cx.prevent_default();
}
}
})
}
}
2023-10-18 11:36:26 +00:00
}
2023-10-19 21:54:17 +00:00
pub struct FocusEnabled<V: 'static + Send + Sync> {
2023-10-19 19:48:23 +00:00
pub focus_handle: FocusHandle,
pub focus_listeners: FocusListeners<V>,
2023-10-19 19:57:09 +00:00
pub focus_style: StyleRefinement,
pub focus_in_style: StyleRefinement,
pub in_focus_style: StyleRefinement,
2023-10-18 14:13:36 +00:00
}
2023-10-19 21:54:17 +00:00
impl<V> ElementFocus<V> for FocusEnabled<V>
2023-10-19 19:48:23 +00:00
where
V: 'static + Send + Sync,
{
2023-10-19 21:54:17 +00:00
fn as_focusable(&self) -> Option<&FocusEnabled<V>> {
2023-10-19 19:57:09 +00:00
Some(self)
2023-10-18 11:36:26 +00:00
}
}
2023-10-19 21:54:17 +00:00
impl<V> From<FocusHandle> for FocusEnabled<V>
2023-10-19 19:48:23 +00:00
where
V: 'static + Send + Sync,
{
2023-10-18 11:36:26 +00:00
fn from(value: FocusHandle) -> Self {
2023-10-19 19:48:23 +00:00
Self {
focus_handle: value,
2023-10-19 19:57:09 +00:00
focus_listeners: FocusListeners::default(),
focus_style: StyleRefinement::default(),
focus_in_style: StyleRefinement::default(),
in_focus_style: StyleRefinement::default(),
2023-10-19 19:48:23 +00:00
}
2023-10-18 11:36:26 +00:00
}
}
2023-10-19 21:54:17 +00:00
pub struct FocusDisabled;
2023-10-18 11:36:26 +00:00
2023-10-19 21:54:17 +00:00
impl<V> ElementFocus<V> for FocusDisabled
2023-10-19 19:48:23 +00:00
where
V: 'static + Send + Sync,
{
2023-10-19 21:54:17 +00:00
fn as_focusable(&self) -> Option<&FocusEnabled<V>> {
2023-10-19 19:48:23 +00:00
None
}
2023-10-18 11:36:26 +00:00
}
2023-10-17 11:32:49 +00:00
pub trait ParentElement: Element {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]>;
2023-10-10 14:47:09 +00:00
2023-10-17 11:32:49 +00:00
fn child(mut self, child: impl IntoAnyElement<Self::ViewState>) -> Self
2023-09-20 03:55:49 +00:00
where
Self: Sized,
{
self.children_mut().push(child.into_any());
self
}
2023-10-17 11:32:49 +00:00
fn children(
mut self,
iter: impl IntoIterator<Item = impl IntoAnyElement<Self::ViewState>>,
) -> Self
where
Self: Sized,
{
self.children_mut()
.extend(iter.into_iter().map(|item| item.into_any()));
self
}
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
trait ElementObject<V>: 'static + Send + Sync {
fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>);
fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId;
fn paint(&mut self, view_state: &mut V, offset: Option<Point<Pixels>>, cx: &mut ViewContext<V>);
2023-09-19 19:19:22 +00:00
}
struct RenderedElement<E: Element> {
element: E,
2023-10-11 04:14:47 +00:00
phase: ElementRenderPhase<E::ElementState>,
2023-09-19 19:19:22 +00:00
}
#[derive(Default)]
2023-10-18 12:12:50 +00:00
enum ElementRenderPhase<V> {
2023-09-19 19:19:22 +00:00
#[default]
2023-10-18 12:12:50 +00:00
Start,
Initialized {
frame_state: Option<V>,
},
2023-09-19 19:19:22 +00:00
LayoutRequested {
layout_id: LayoutId,
2023-10-18 12:12:50 +00:00
frame_state: Option<V>,
2023-09-19 19:19:22 +00:00
},
2023-10-18 12:12:50 +00:00
Painted,
2023-09-19 19:19:22 +00:00
}
2023-10-11 04:14:47 +00:00
/// Internal struct that wraps an element to store Layout and ElementState after the element is rendered.
2023-09-19 19:19:22 +00:00
/// It's allocated as a trait object to erase the element type and wrapped in AnyElement<E::State> for
/// improved usability.
impl<E: Element> RenderedElement<E> {
fn new(element: E) -> Self {
RenderedElement {
element,
2023-10-18 12:12:50 +00:00
phase: ElementRenderPhase::Start,
2023-09-19 19:19:22 +00:00
}
}
2023-10-18 12:12:50 +00:00
}
2023-10-11 04:14:47 +00:00
2023-10-18 12:12:50 +00:00
impl<E> ElementObject<E::ViewState> for RenderedElement<E>
where
E: Element,
{
fn initialize(&mut self, view_state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) {
let frame_state = if let Some(id) = self.element.id() {
2023-10-11 04:14:47 +00:00
cx.with_element_state(id, |element_state, cx| {
2023-10-18 12:12:50 +00:00
let element_state = self.element.initialize(view_state, element_state, cx);
2023-10-11 04:14:47 +00:00
((), element_state)
});
2023-10-18 12:12:50 +00:00
None
2023-10-11 04:14:47 +00:00
} else {
2023-10-18 12:12:50 +00:00
let frame_state = self.element.initialize(view_state, None, cx);
Some(frame_state)
};
self.phase = ElementRenderPhase::Initialized { frame_state };
2023-10-11 04:14:47 +00:00
}
2023-09-19 19:19:22 +00:00
2023-10-11 04:14:47 +00:00
fn layout(&mut self, state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) -> LayoutId {
2023-10-18 12:12:50 +00:00
let layout_id;
let mut frame_state;
match mem::take(&mut self.phase) {
ElementRenderPhase::Initialized {
frame_state: initial_frame_state,
} => {
frame_state = initial_frame_state;
if let Some(id) = self.element.id() {
layout_id = cx.with_element_state(id, |element_state, cx| {
let mut element_state = element_state.unwrap();
let layout_id = self.element.layout(state, &mut element_state, cx);
(layout_id, element_state)
});
} else {
layout_id = self
.element
.layout(state, frame_state.as_mut().unwrap(), cx);
}
}
_ => panic!("must call initialize before layout"),
2023-10-11 04:14:47 +00:00
};
2023-09-19 19:19:22 +00:00
self.phase = ElementRenderPhase::LayoutRequested {
layout_id,
frame_state,
};
2023-10-11 04:14:47 +00:00
layout_id
2023-09-19 19:19:22 +00:00
}
fn paint(
&mut self,
2023-10-11 04:14:47 +00:00
view_state: &mut E::ViewState,
2023-09-21 18:18:09 +00:00
offset: Option<Point<Pixels>>,
2023-10-11 04:14:47 +00:00
cx: &mut ViewContext<E::ViewState>,
) {
2023-10-18 12:12:50 +00:00
self.phase = match mem::take(&mut self.phase) {
2023-09-19 19:19:22 +00:00
ElementRenderPhase::LayoutRequested {
layout_id,
mut frame_state,
} => {
2023-10-11 04:14:47 +00:00
let mut bounds = cx.layout_bounds(layout_id);
2023-10-06 03:02:26 +00:00
offset.map(|offset| bounds.origin += offset);
2023-10-18 12:12:50 +00:00
if let Some(id) = self.element.id() {
cx.with_element_state(id, |element_state, cx| {
let mut element_state = element_state.unwrap();
self.element
.paint(bounds, view_state, &mut element_state, cx);
((), element_state)
});
} else {
self.element
.paint(bounds, view_state, frame_state.as_mut().unwrap(), cx);
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
ElementRenderPhase::Painted
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
_ => panic!("must call layout before paint"),
2023-09-19 19:19:22 +00:00
};
}
}
2023-10-18 12:12:50 +00:00
pub struct AnyElement<V>(Box<dyn ElementObject<V>>);
2023-09-19 19:19:22 +00:00
2023-10-18 12:12:50 +00:00
impl<V: 'static + Send + Sync> AnyElement<V> {
pub fn new<E: Element<ViewState = V>>(element: E) -> Self {
2023-10-12 17:30:00 +00:00
AnyElement(Box::new(RenderedElement::new(element)))
}
2023-10-18 12:12:50 +00:00
pub fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
self.0.initialize(view_state, cx);
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
pub fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
self.0.layout(view_state, cx)
}
pub fn paint(
&mut self,
view_state: &mut V,
offset: Option<Point<Pixels>>,
cx: &mut ViewContext<V>,
) {
self.0.paint(view_state, offset, cx)
2023-09-19 19:19:22 +00:00
}
}
2023-10-18 12:12:50 +00:00
pub trait IntoAnyElement<V> {
fn into_any(self) -> AnyElement<V>;
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
impl<V> IntoAnyElement<V> for AnyElement<V> {
fn into_any(self) -> AnyElement<V> {
2023-09-19 19:19:22 +00:00
self
}
}