Eliminate PaintContext
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::{paint_context::PaintContext, ViewContext};
|
||||
use crate::ViewContext;
|
||||
use gpui::{geometry::rect::RectF, LayoutEngine, LayoutId};
|
||||
use util::ResultExt;
|
||||
|
||||
@@ -40,13 +40,13 @@ impl<V: 'static> gpui::Element<V> for AdapterElement<V> {
|
||||
_visible_bounds: RectF,
|
||||
layout_data: &mut Option<(LayoutEngine, LayoutId)>,
|
||||
view: &mut V,
|
||||
legacy_cx: &mut gpui::PaintContext<V>,
|
||||
cx: &mut gpui::ViewContext<V>,
|
||||
) -> Self::PaintState {
|
||||
let (layout_engine, layout_id) = layout_data.take().unwrap();
|
||||
legacy_cx.push_layout_engine(layout_engine);
|
||||
let mut cx = PaintContext::new(legacy_cx);
|
||||
self.0.paint(view, bounds.origin(), &mut cx);
|
||||
*layout_data = legacy_cx.pop_layout_engine().zip(Some(layout_id));
|
||||
cx.push_layout_engine(layout_engine);
|
||||
self.0
|
||||
.paint(view, bounds.origin(), &mut ViewContext::new(cx));
|
||||
*layout_data = cx.pop_layout_engine().zip(Some(layout_id));
|
||||
debug_assert!(layout_data.is_some());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
pub use crate::paint_context::PaintContext;
|
||||
pub use crate::ViewContext;
|
||||
use anyhow::Result;
|
||||
use gpui::geometry::vector::Vector2F;
|
||||
@@ -22,7 +21,7 @@ pub trait Element<V: 'static>: 'static + IntoElement<V> {
|
||||
parent_origin: Vector2F,
|
||||
layout: &Layout,
|
||||
state: &mut Self::PaintState,
|
||||
cx: &mut PaintContext<V>,
|
||||
cx: &mut ViewContext<V>,
|
||||
) where
|
||||
Self: Sized;
|
||||
|
||||
@@ -40,7 +39,7 @@ pub trait Element<V: 'static>: 'static + IntoElement<V> {
|
||||
/// Used to make ElementState<V, E> into a trait object, so we can wrap it in AnyElement<V>.
|
||||
trait AnyStatefulElement<V> {
|
||||
fn layout(&mut self, view: &mut V, cx: &mut ViewContext<V>) -> Result<LayoutId>;
|
||||
fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext<V>);
|
||||
fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext<V>);
|
||||
}
|
||||
|
||||
/// A wrapper around an element that stores its layout state.
|
||||
@@ -105,7 +104,7 @@ impl<V, E: Element<V>> AnyStatefulElement<V> for StatefulElement<V, E> {
|
||||
result
|
||||
}
|
||||
|
||||
fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext<V>) {
|
||||
fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext<V>) {
|
||||
self.phase = match std::mem::take(&mut self.phase) {
|
||||
ElementPhase::PostLayout {
|
||||
layout_id,
|
||||
@@ -149,7 +148,7 @@ impl<V> AnyElement<V> {
|
||||
self.0.layout(view, cx)
|
||||
}
|
||||
|
||||
pub fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext<V>) {
|
||||
pub fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext<V>) {
|
||||
self.0.paint(view, parent_origin, cx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ use std::{cell::Cell, rc::Rc};
|
||||
use crate::{
|
||||
element::{AnyElement, Element, IntoElement, Layout, ParentElement},
|
||||
hsla,
|
||||
paint_context::PaintContext,
|
||||
style::{CornerRadii, Overflow, Style, StyleHelpers, Styleable},
|
||||
InteractionHandlers, Interactive, ViewContext,
|
||||
};
|
||||
@@ -69,7 +68,7 @@ impl<V: 'static> Element<V> for Div<V> {
|
||||
parent_origin: Vector2F,
|
||||
layout: &Layout,
|
||||
child_layouts: &mut Vec<LayoutId>,
|
||||
cx: &mut PaintContext<V>,
|
||||
cx: &mut ViewContext<V>,
|
||||
) where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -167,7 +166,7 @@ impl<V: 'static> Div<V> {
|
||||
bounds: RectF,
|
||||
overflow: Point<Overflow>,
|
||||
child_layout_ids: &[LayoutId],
|
||||
cx: &mut PaintContext<V>,
|
||||
cx: &mut ViewContext<V>,
|
||||
) {
|
||||
if overflow.y == Overflow::Scroll || overflow.x == Overflow::Scroll {
|
||||
let mut scroll_max = Vector2F::zero();
|
||||
@@ -214,7 +213,7 @@ impl<V: 'static> Div<V> {
|
||||
}
|
||||
}
|
||||
|
||||
fn paint_inspector(&self, parent_origin: Vector2F, layout: &Layout, cx: &mut PaintContext<V>) {
|
||||
fn paint_inspector(&self, parent_origin: Vector2F, layout: &Layout, cx: &mut ViewContext<V>) {
|
||||
let style = self.styles.merged();
|
||||
let bounds = layout.bounds + parent_origin;
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::{
|
||||
element::{AnyElement, Element, IntoElement, Layout, ParentElement},
|
||||
interactive::{InteractionHandlers, Interactive},
|
||||
paint_context::PaintContext,
|
||||
style::{Style, StyleHelpers, Styleable},
|
||||
ViewContext,
|
||||
};
|
||||
@@ -59,7 +58,7 @@ impl<V: 'static, E: Element<V> + Styleable> Element<V> for Hoverable<E> {
|
||||
parent_origin: Vector2F,
|
||||
layout: &Layout,
|
||||
paint_state: &mut Self::PaintState,
|
||||
cx: &mut PaintContext<V>,
|
||||
cx: &mut ViewContext<V>,
|
||||
) where
|
||||
Self: Sized,
|
||||
{
|
||||
|
||||
@@ -53,7 +53,7 @@ impl<V: 'static> Element<V> for Img {
|
||||
parent_origin: Vector2F,
|
||||
layout: &gpui::Layout,
|
||||
_: &mut Self::PaintState,
|
||||
cx: &mut crate::paint_context::PaintContext<V>,
|
||||
cx: &mut crate::ViewContext<V>,
|
||||
) where
|
||||
Self: Sized,
|
||||
{
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::{
|
||||
element::{AnyElement, Element, IntoElement, Layout, ParentElement},
|
||||
interactive::{InteractionHandlers, Interactive},
|
||||
paint_context::PaintContext,
|
||||
style::{Style, StyleHelpers, Styleable},
|
||||
ViewContext,
|
||||
};
|
||||
@@ -59,7 +58,7 @@ impl<V: 'static, E: Element<V> + Styleable> Element<V> for Pressable<E> {
|
||||
parent_origin: Vector2F,
|
||||
layout: &Layout,
|
||||
paint_state: &mut Self::PaintState,
|
||||
cx: &mut PaintContext<V>,
|
||||
cx: &mut ViewContext<V>,
|
||||
) where
|
||||
Self: Sized,
|
||||
{
|
||||
|
||||
@@ -49,7 +49,7 @@ impl<V: 'static> Element<V> for Svg {
|
||||
parent_origin: Vector2F,
|
||||
layout: &Layout,
|
||||
_: &mut Self::PaintState,
|
||||
cx: &mut crate::paint_context::PaintContext<V>,
|
||||
cx: &mut crate::ViewContext<V>,
|
||||
) where
|
||||
Self: Sized,
|
||||
{
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::{
|
||||
element::{Element, IntoElement, Layout},
|
||||
paint_context::PaintContext,
|
||||
ViewContext,
|
||||
};
|
||||
use anyhow::Result;
|
||||
@@ -71,7 +70,7 @@ impl<V: 'static> Element<V> for Text {
|
||||
parent_origin: Vector2F,
|
||||
layout: &Layout,
|
||||
paint_state: &mut Self::PaintState,
|
||||
cx: &mut PaintContext<V>,
|
||||
cx: &mut ViewContext<V>,
|
||||
) {
|
||||
let bounds = layout.bounds + parent_origin;
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ pub mod color;
|
||||
pub mod element;
|
||||
pub mod elements;
|
||||
pub mod interactive;
|
||||
pub mod paint_context;
|
||||
pub mod style;
|
||||
pub mod view;
|
||||
pub mod view_context;
|
||||
|
||||
@@ -6,7 +6,7 @@ use gpui::{
|
||||
use smallvec::SmallVec;
|
||||
use std::{cell::Cell, rc::Rc};
|
||||
|
||||
use crate::element::PaintContext;
|
||||
use crate::ViewContext;
|
||||
|
||||
pub trait Interactive<V: 'static> {
|
||||
fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V>;
|
||||
@@ -121,7 +121,7 @@ pub struct InteractionHandlers<V: 'static> {
|
||||
}
|
||||
|
||||
impl<V: 'static> InteractionHandlers<V> {
|
||||
pub fn paint(&self, order: u32, bounds: RectF, cx: &mut PaintContext<V>) {
|
||||
pub fn paint(&self, order: u32, bounds: RectF, cx: &mut ViewContext<V>) {
|
||||
for handler in self.mouse_down.iter().cloned() {
|
||||
cx.on_event(order, move |view, event: &MouseButtonEvent, cx| {
|
||||
if event.is_down && bounds.contains_point(event.position) {
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
pub use gpui::taffy::tree::NodeId;
|
||||
use gpui::{
|
||||
scene::EventHandler, EventContext, Layout, LayoutId, PaintContext as LegacyPaintContext,
|
||||
};
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
|
||||
#[derive(Deref, DerefMut)]
|
||||
pub struct PaintContext<'a, 'b, 'c, 'd, V> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
pub(crate) legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, V>,
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c, 'd, V: 'static> PaintContext<'a, 'b, 'c, 'd, V> {
|
||||
pub fn new(legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, V>) -> Self {
|
||||
Self { legacy_cx }
|
||||
}
|
||||
|
||||
pub fn on_event<E: 'static>(
|
||||
&mut self,
|
||||
order: u32,
|
||||
handler: impl Fn(&mut V, &E, &mut EventContext<V>) + 'static,
|
||||
) {
|
||||
let view = self.weak_handle();
|
||||
|
||||
self.scene().event_handlers.push(EventHandler {
|
||||
order,
|
||||
handler: Rc::new(move |event, window_cx| {
|
||||
if let Some(view) = view.upgrade(window_cx) {
|
||||
view.update(window_cx, |view, view_cx| {
|
||||
let mut event_cx = EventContext::new(view_cx);
|
||||
handler(view, event.downcast_ref().unwrap(), &mut event_cx);
|
||||
event_cx.bubble
|
||||
})
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}),
|
||||
event_type: TypeId::of::<E>(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn computed_layout(&mut self, layout_id: LayoutId) -> Result<Layout> {
|
||||
self.layout_engine()
|
||||
.ok_or_else(|| anyhow!("no layout engine present"))?
|
||||
.computed_layout(layout_id)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ use crate::{
|
||||
color::Hsla,
|
||||
elements::hoverable::{hoverable, Hoverable},
|
||||
elements::pressable::{pressable, Pressable},
|
||||
paint_context::PaintContext,
|
||||
ViewContext,
|
||||
};
|
||||
pub use fonts::Style as FontStyle;
|
||||
pub use fonts::Weight as FontWeight;
|
||||
@@ -164,7 +164,7 @@ impl Style {
|
||||
}
|
||||
|
||||
/// Paints the background of an element styled with this style.
|
||||
pub fn paint_background<V: 'static>(&self, bounds: RectF, cx: &mut PaintContext<V>) {
|
||||
pub fn paint_background<V: 'static>(&self, bounds: RectF, cx: &mut ViewContext<V>) {
|
||||
let rem_size = cx.rem_size();
|
||||
if let Some(color) = self.fill.as_ref().and_then(Fill::color) {
|
||||
cx.scene().push_quad(gpui::Quad {
|
||||
@@ -177,7 +177,7 @@ impl Style {
|
||||
}
|
||||
|
||||
/// Paints the foreground of an element styled with this style.
|
||||
pub fn paint_foreground<V: 'static>(&self, bounds: RectF, cx: &mut PaintContext<V>) {
|
||||
pub fn paint_foreground<V: 'static>(&self, bounds: RectF, cx: &mut ViewContext<V>) {
|
||||
let rem_size = cx.rem_size();
|
||||
|
||||
if let Some(color) = self.border_color {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
|
||||
use crate::{element::LayoutId, style::Style};
|
||||
use anyhow::{anyhow, Result};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use gpui::{geometry::Size, MeasureParams};
|
||||
use gpui::{geometry::Size, scene::EventHandler, EventContext, Layout, MeasureParams};
|
||||
pub use gpui::{taffy::tree::NodeId, ViewContext as LegacyViewContext};
|
||||
|
||||
#[derive(Deref, DerefMut)]
|
||||
@@ -44,4 +46,34 @@ impl<'a, 'b, 'c, V: 'static> ViewContext<'a, 'b, 'c, V> {
|
||||
|
||||
Ok(layout_id)
|
||||
}
|
||||
|
||||
pub fn on_event<E: 'static>(
|
||||
&mut self,
|
||||
order: u32,
|
||||
handler: impl Fn(&mut V, &E, &mut EventContext<V>) + 'static,
|
||||
) {
|
||||
let view = self.weak_handle();
|
||||
|
||||
self.scene().event_handlers.push(EventHandler {
|
||||
order,
|
||||
handler: Rc::new(move |event, window_cx| {
|
||||
if let Some(view) = view.upgrade(window_cx) {
|
||||
view.update(window_cx, |view, view_cx| {
|
||||
let mut event_cx = EventContext::new(view_cx);
|
||||
handler(view, event.downcast_ref().unwrap(), &mut event_cx);
|
||||
event_cx.bubble
|
||||
})
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}),
|
||||
event_type: TypeId::of::<E>(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn computed_layout(&mut self, layout_id: LayoutId) -> Result<Layout> {
|
||||
self.layout_engine()
|
||||
.ok_or_else(|| anyhow!("no layout engine present"))?
|
||||
.computed_layout(layout_id)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user