Eliminate PaintContext

This commit is contained in:
Nathan Sobo 2023-09-11 09:00:59 -06:00
parent a24d94cfda
commit e0fdc7d0aa
49 changed files with 156 additions and 269 deletions

View File

@ -13,8 +13,8 @@ use gpui::{
geometry::{rect::RectF, vector::vec2f, PathBuilder}, geometry::{rect::RectF, vector::vec2f, PathBuilder},
json::{self, ToJson}, json::{self, ToJson},
platform::{CursorStyle, MouseButton}, platform::{CursorStyle, MouseButton},
AppContext, Entity, ImageData, ModelHandle, PaintContext, Subscription, View, ViewContext, AppContext, Entity, ImageData, ModelHandle, Subscription, View, ViewContext, ViewHandle,
ViewHandle, WeakViewHandle, WeakViewHandle,
}; };
use picker::PickerEvent; use picker::PickerEvent;
use project::{Project, RepositoryEntry}; use project::{Project, RepositoryEntry};
@ -1176,7 +1176,7 @@ impl Element<CollabTitlebarItem> for AvatarRibbon {
_: RectF, _: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut CollabTitlebarItem, _: &mut CollabTitlebarItem,
cx: &mut PaintContext<CollabTitlebarItem>, cx: &mut ViewContext<CollabTitlebarItem>,
) -> Self::PaintState { ) -> Self::PaintState {
let mut path = PathBuilder::new(); let mut path = PathBuilder::new();
path.reset(bounds.lower_left()); path.reset(bounds.lower_left());

View File

@ -7,7 +7,7 @@ use gpui::{
}, },
json::ToJson, json::ToJson,
serde_json::{self, json}, serde_json::{self, json},
AnyElement, Axis, Element, PaintContext, View, ViewContext, AnyElement, Axis, Element, View, ViewContext,
}; };
pub(crate) struct FacePile<V: View> { pub(crate) struct FacePile<V: View> {
@ -57,7 +57,7 @@ impl<V: View> Element<V> for FacePile<V> {
visible_bounds: RectF, visible_bounds: RectF,
_layout: &mut Self::LayoutState, _layout: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();

View File

@ -32,8 +32,8 @@ use gpui::{
json::{self, ToJson}, json::{self, ToJson},
platform::{CursorStyle, Modifiers, MouseButton, MouseButtonEvent, MouseMovedEvent}, platform::{CursorStyle, Modifiers, MouseButton, MouseButtonEvent, MouseMovedEvent},
text_layout::{self, Line, RunStyle, TextLayoutCache}, text_layout::{self, Line, RunStyle, TextLayoutCache},
AnyElement, Axis, CursorRegion, Element, EventContext, FontCache, MouseRegion, PaintContext, AnyElement, Axis, CursorRegion, Element, EventContext, FontCache, MouseRegion, Quad,
Quad, SizeConstraint, ViewContext, WindowContext, SizeConstraint, ViewContext, WindowContext,
}; };
use itertools::Itertools; use itertools::Itertools;
use json::json; use json::json;
@ -635,7 +635,7 @@ impl EditorElement {
visible_bounds: RectF, visible_bounds: RectF,
layout: &mut LayoutState, layout: &mut LayoutState,
editor: &mut Editor, editor: &mut Editor,
cx: &mut PaintContext<Editor>, cx: &mut ViewContext<Editor>,
) { ) {
let line_height = layout.position_map.line_height; let line_height = layout.position_map.line_height;
@ -778,7 +778,7 @@ impl EditorElement {
visible_bounds: RectF, visible_bounds: RectF,
layout: &mut LayoutState, layout: &mut LayoutState,
editor: &mut Editor, editor: &mut Editor,
cx: &mut PaintContext<Editor>, cx: &mut ViewContext<Editor>,
) { ) {
let style = &self.style; let style = &self.style;
let scroll_position = layout.position_map.snapshot.scroll_position(); let scroll_position = layout.position_map.snapshot.scroll_position();
@ -1351,7 +1351,7 @@ impl EditorElement {
visible_bounds: RectF, visible_bounds: RectF,
layout: &mut LayoutState, layout: &mut LayoutState,
editor: &mut Editor, editor: &mut Editor,
cx: &mut PaintContext<Editor>, cx: &mut ViewContext<Editor>,
) { ) {
let scroll_position = layout.position_map.snapshot.scroll_position(); let scroll_position = layout.position_map.snapshot.scroll_position();
let scroll_left = scroll_position.x() * layout.position_map.em_width; let scroll_left = scroll_position.x() * layout.position_map.em_width;
@ -2570,7 +2570,7 @@ impl Element<Editor> for EditorElement {
visible_bounds: RectF, visible_bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
editor: &mut Editor, editor: &mut Editor,
cx: &mut PaintContext<Editor>, cx: &mut ViewContext<Editor>,
) -> Self::PaintState { ) -> Self::PaintState {
let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();
cx.scene().push_layer(Some(visible_bounds)); cx.scene().push_layer(Some(visible_bounds));
@ -3340,13 +3340,7 @@ mod tests {
// Don't panic. // Don't panic.
let bounds = RectF::new(Default::default(), size); let bounds = RectF::new(Default::default(), size);
editor.update(cx, |editor, cx| { editor.update(cx, |editor, cx| {
element.paint( element.paint(bounds, bounds, &mut state, editor, cx);
bounds,
bounds,
&mut state,
editor,
&mut PaintContext::new(cx),
);
}); });
} }

View File

@ -53,7 +53,7 @@ impl<V: View> gpui::Element<V> for CornersElement {
_: pathfinder_geometry::rect::RectF, _: pathfinder_geometry::rect::RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut V, _: &mut V,
cx: &mut gpui::PaintContext<V>, cx: &mut gpui::ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
cx.scene().push_quad(Quad { cx.scene().push_quad(Quad {
bounds, bounds,

View File

@ -3404,6 +3404,16 @@ impl<'a, 'b, V: 'static> ViewContext<'a, 'b, V> {
.or_default() .or_default()
.push(self_view_id); .push(self_view_id);
} }
pub fn paint_layer<F, R>(&mut self, clip_bounds: Option<RectF>, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
self.scene().push_layer(clip_bounds);
let result = f(self);
self.scene().pop_layer();
result
}
} }
impl<V: View> ViewContext<'_, '_, V> { impl<V: View> ViewContext<'_, '_, V> {
@ -3495,82 +3505,6 @@ impl<V> BorrowWindowContext for ViewContext<'_, '_, V> {
} }
} }
pub struct PaintContext<'a, 'b, 'c, V> {
pub view_context: &'c mut ViewContext<'a, 'b, V>,
}
impl<'a, 'b, 'c, V> PaintContext<'a, 'b, 'c, V> {
pub fn new(view_context: &'c mut ViewContext<'a, 'b, V>) -> Self {
Self { view_context }
}
pub fn paint_layer<F, R>(&mut self, clip_bounds: Option<RectF>, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
self.scene().push_layer(clip_bounds);
let result = f(self);
self.scene().pop_layer();
result
}
}
impl<'a, 'b, 'c, V> Deref for PaintContext<'a, 'b, 'c, V> {
type Target = ViewContext<'a, 'b, V>;
fn deref(&self) -> &Self::Target {
&self.view_context
}
}
impl<V> DerefMut for PaintContext<'_, '_, '_, V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.view_context
}
}
impl<V> BorrowAppContext for PaintContext<'_, '_, '_, V> {
fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
BorrowAppContext::read_with(&*self.view_context, f)
}
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
BorrowAppContext::update(&mut *self.view_context, f)
}
}
impl<V> BorrowWindowContext for PaintContext<'_, '_, '_, V> {
type Result<T> = T;
fn read_window<T, F>(&self, window: AnyWindowHandle, f: F) -> Self::Result<T>
where
F: FnOnce(&WindowContext) -> T,
{
BorrowWindowContext::read_window(self.view_context, window, f)
}
fn read_window_optional<T, F>(&self, window: AnyWindowHandle, f: F) -> Option<T>
where
F: FnOnce(&WindowContext) -> Option<T>,
{
BorrowWindowContext::read_window_optional(self.view_context, window, f)
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Self::Result<T>
where
F: FnOnce(&mut WindowContext) -> T,
{
BorrowWindowContext::update_window(self.view_context, window, f)
}
fn update_window_optional<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Option<T>
where
F: FnOnce(&mut WindowContext) -> Option<T>,
{
BorrowWindowContext::update_window_optional(self.view_context, window, f)
}
}
pub struct EventContext<'a, 'b, 'c, V> { pub struct EventContext<'a, 'b, 'c, V> {
view_context: &'c mut ViewContext<'a, 'b, V>, view_context: &'c mut ViewContext<'a, 'b, V>,
pub(crate) handled: bool, pub(crate) handled: bool,

View File

@ -16,8 +16,8 @@ use crate::{
text_layout::TextLayoutCache, text_layout::TextLayoutCache,
util::post_inc, util::post_inc,
Action, AnyView, AnyViewHandle, AnyWindowHandle, AppContext, BorrowAppContext, Action, AnyView, AnyViewHandle, AnyWindowHandle, AppContext, BorrowAppContext,
BorrowWindowContext, Effect, Element, Entity, Handle, MouseRegion, MouseRegionId, PaintContext, BorrowWindowContext, Effect, Element, Entity, Handle, MouseRegion, MouseRegionId, SceneBuilder,
SceneBuilder, Subscription, View, ViewContext, ViewHandle, WindowInvalidation, Subscription, View, ViewContext, ViewHandle, WindowInvalidation,
}; };
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use collections::{HashMap, HashSet}; use collections::{HashMap, HashSet};
@ -1703,7 +1703,7 @@ impl<V: 'static> Element<V> for ChildView {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut V, _: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
if let Some(mut rendered_view) = cx.window.rendered_views.remove(&self.view_id) { if let Some(mut rendered_view) = cx.window.rendered_views.remove(&self.view_id) {
rendered_view rendered_view

View File

@ -34,7 +34,7 @@ use crate::{
rect::RectF, rect::RectF,
vector::{vec2f, Vector2F}, vector::{vec2f, Vector2F},
}, },
json, Action, Entity, PaintContext, SizeConstraint, TypeTag, View, ViewContext, WeakViewHandle, json, Action, Entity, SizeConstraint, TypeTag, View, ViewContext, WeakViewHandle,
WindowContext, WindowContext,
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
@ -68,7 +68,7 @@ pub trait Element<V: 'static>: 'static {
visible_bounds: RectF, visible_bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState; ) -> Self::PaintState;
fn rect_for_text_range( fn rect_for_text_range(
@ -267,7 +267,7 @@ trait AnyElementState<V> {
origin: Vector2F, origin: Vector2F,
visible_bounds: RectF, visible_bounds: RectF,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
); );
fn rect_for_text_range( fn rect_for_text_range(
@ -347,7 +347,7 @@ impl<V, E: Element<V>> AnyElementState<V> for ElementState<V, E> {
origin: Vector2F, origin: Vector2F,
visible_bounds: RectF, visible_bounds: RectF,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
*self = match mem::take(self) { *self = match mem::take(self) {
ElementState::PostLayout { ElementState::PostLayout {
@ -357,13 +357,7 @@ impl<V, E: Element<V>> AnyElementState<V> for ElementState<V, E> {
mut layout, mut layout,
} => { } => {
let bounds = RectF::new(origin, size); let bounds = RectF::new(origin, size);
let paint = element.paint( let paint = element.paint(bounds, visible_bounds, &mut layout, view, cx);
bounds,
visible_bounds,
&mut layout,
view,
&mut PaintContext::new(cx),
);
ElementState::PostPaint { ElementState::PostPaint {
element, element,
constraint, constraint,
@ -381,13 +375,7 @@ impl<V, E: Element<V>> AnyElementState<V> for ElementState<V, E> {
.. ..
} => { } => {
let bounds = RectF::new(origin, bounds.size()); let bounds = RectF::new(origin, bounds.size());
let paint = element.paint( let paint = element.paint(bounds, visible_bounds, &mut layout, view, cx);
bounds,
visible_bounds,
&mut layout,
view,
&mut PaintContext::new(cx),
);
ElementState::PostPaint { ElementState::PostPaint {
element, element,
constraint, constraint,
@ -520,7 +508,7 @@ impl<V> AnyElement<V> {
origin: Vector2F, origin: Vector2F,
visible_bounds: RectF, visible_bounds: RectF,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
self.state.paint(origin, visible_bounds, view, cx); self.state.paint(origin, visible_bounds, view, cx);
} }
@ -582,7 +570,7 @@ impl<V: 'static> Element<V> for AnyElement<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
self.paint(bounds.origin(), visible_bounds, view, cx); self.paint(bounds.origin(), visible_bounds, view, cx);
} }
@ -674,8 +662,7 @@ impl<V: View> AnyRootElement for RootElement<V> {
.ok_or_else(|| anyhow!("paint called on a root element for a dropped view"))?; .ok_or_else(|| anyhow!("paint called on a root element for a dropped view"))?;
view.update(cx, |view, cx| { view.update(cx, |view, cx| {
let mut cx = PaintContext::new(cx); self.element.paint(origin, visible_bounds, view, cx);
self.element.paint(origin, visible_bounds, view, &mut cx);
Ok(()) Ok(())
}) })
} }

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json, AnyElement, Element, PaintContext, SizeConstraint, ViewContext, json, AnyElement, Element, SizeConstraint, ViewContext,
}; };
use json::ToJson; use json::ToJson;
@ -68,7 +68,7 @@ impl<V: 'static> Element<V> for Align<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
let my_center = bounds.size() / 2.; let my_center = bounds.size() / 2.;
let my_target = my_center + my_center * self.alignment; let my_target = my_center + my_center * self.alignment;

View File

@ -3,7 +3,7 @@ use std::marker::PhantomData;
use super::Element; use super::Element;
use crate::{ use crate::{
json::{self, json}, json::{self, json},
PaintContext, ViewContext, ViewContext,
}; };
use json::ToJson; use json::ToJson;
use pathfinder_geometry::{ use pathfinder_geometry::{
@ -15,7 +15,7 @@ pub struct Canvas<V, F>(F, PhantomData<V>);
impl<V, F> Canvas<V, F> impl<V, F> Canvas<V, F>
where where
F: FnMut(RectF, RectF, &mut V, &mut PaintContext<V>), F: FnMut(RectF, RectF, &mut V, &mut ViewContext<V>),
{ {
pub fn new(f: F) -> Self { pub fn new(f: F) -> Self {
Self(f, PhantomData) Self(f, PhantomData)
@ -24,7 +24,7 @@ where
impl<V: 'static, F> Element<V> for Canvas<V, F> impl<V: 'static, F> Element<V> for Canvas<V, F>
where where
F: 'static + FnMut(RectF, RectF, &mut V, &mut PaintContext<V>), F: 'static + FnMut(RectF, RectF, &mut V, &mut ViewContext<V>),
{ {
type LayoutState = (); type LayoutState = ();
type PaintState = (); type PaintState = ();
@ -54,7 +54,7 @@ where
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
self.0(bounds, visible_bounds, view, cx) self.0(bounds, visible_bounds, view, cx)
} }

View File

@ -3,7 +3,7 @@ use std::ops::Range;
use pathfinder_geometry::{rect::RectF, vector::Vector2F}; use pathfinder_geometry::{rect::RectF, vector::Vector2F};
use serde_json::json; use serde_json::json;
use crate::{json, AnyElement, Element, PaintContext, SizeConstraint, ViewContext}; use crate::{json, AnyElement, Element, SizeConstraint, ViewContext};
pub struct Clipped<V> { pub struct Clipped<V> {
child: AnyElement<V>, child: AnyElement<V>,
@ -34,7 +34,7 @@ impl<V: 'static> Element<V> for Clipped<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
cx.scene().push_layer(Some(bounds)); cx.scene().push_layer(Some(bounds));
let state = self.child.paint(bounds.origin(), visible_bounds, view, cx); let state = self.child.paint(bounds.origin(), visible_bounds, view, cx);

View File

@ -2,7 +2,7 @@ use std::{any::Any, marker::PhantomData};
use pathfinder_geometry::{rect::RectF, vector::Vector2F}; use pathfinder_geometry::{rect::RectF, vector::Vector2F};
use crate::{AnyElement, Element, PaintContext, SizeConstraint, ViewContext}; use crate::{AnyElement, Element, SizeConstraint, ViewContext};
use super::Empty; use super::Empty;
@ -302,7 +302,7 @@ impl<V: 'static, C: StatefulComponent<V> + 'static> Element<V> for ComponentAdap
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
self.element self.element
.as_mut() .as_mut()

View File

@ -5,7 +5,7 @@ use serde_json::json;
use crate::{ use crate::{
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json, AnyElement, Element, PaintContext, SizeConstraint, ViewContext, json, AnyElement, Element, SizeConstraint, ViewContext,
}; };
pub struct ConstrainedBox<V> { pub struct ConstrainedBox<V> {
@ -154,7 +154,7 @@ impl<V: 'static> Element<V> for ConstrainedBox<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
cx.scene().push_layer(Some(visible_bounds)); cx.scene().push_layer(Some(visible_bounds));
self.child.paint(bounds.origin(), visible_bounds, view, cx); self.child.paint(bounds.origin(), visible_bounds, view, cx);

View File

@ -10,7 +10,7 @@ use crate::{
json::ToJson, json::ToJson,
platform::CursorStyle, platform::CursorStyle,
scene::{self, CornerRadii, CursorRegion, Quad}, scene::{self, CornerRadii, CursorRegion, Quad},
AnyElement, Element, PaintContext, SizeConstraint, ViewContext, AnyElement, Element, SizeConstraint, ViewContext,
}; };
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Deserialize; use serde::Deserialize;
@ -391,7 +391,7 @@ impl<V: 'static> Element<V> for Container<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
let quad_bounds = RectF::from_points( let quad_bounds = RectF::from_points(
bounds.origin() + vec2f(self.style.margin.left, self.style.margin.top), bounds.origin() + vec2f(self.style.margin.left, self.style.margin.top),

View File

@ -6,7 +6,7 @@ use crate::{
vector::{vec2f, Vector2F}, vector::{vec2f, Vector2F},
}, },
json::{json, ToJson}, json::{json, ToJson},
PaintContext, ViewContext, ViewContext,
}; };
use crate::{Element, SizeConstraint}; use crate::{Element, SizeConstraint};
@ -56,7 +56,7 @@ impl<V: 'static> Element<V> for Empty {
_: RectF, _: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut V, _: &mut V,
_: &mut PaintContext<V>, _: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
} }

View File

@ -2,7 +2,7 @@ use std::ops::Range;
use crate::{ use crate::{
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json, AnyElement, Element, PaintContext, SizeConstraint, ViewContext, json, AnyElement, Element, SizeConstraint, ViewContext,
}; };
use serde_json::json; use serde_json::json;
@ -60,7 +60,7 @@ impl<V: 'static> Element<V> for Expanded<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
self.child.paint(bounds.origin(), visible_bounds, view, cx); self.child.paint(bounds.origin(), visible_bounds, view, cx);
} }

View File

@ -2,8 +2,7 @@ use std::{any::Any, cell::Cell, f32::INFINITY, ops::Range, rc::Rc};
use crate::{ use crate::{
json::{self, ToJson, Value}, json::{self, ToJson, Value},
AnyElement, Axis, Element, ElementStateHandle, PaintContext, SizeConstraint, Vector2FExt, AnyElement, Axis, Element, ElementStateHandle, SizeConstraint, Vector2FExt, ViewContext,
ViewContext,
}; };
use pathfinder_geometry::{ use pathfinder_geometry::{
rect::RectF, rect::RectF,
@ -264,7 +263,7 @@ impl<V: 'static> Element<V> for Flex<V> {
visible_bounds: RectF, visible_bounds: RectF,
remaining_space: &mut Self::LayoutState, remaining_space: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();
@ -454,7 +453,7 @@ impl<V: 'static> Element<V> for FlexItem<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
self.child.paint(bounds.origin(), visible_bounds, view, cx) self.child.paint(bounds.origin(), visible_bounds, view, cx)
} }

View File

@ -3,7 +3,7 @@ use std::ops::Range;
use crate::{ use crate::{
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json::json, json::json,
AnyElement, Element, PaintContext, SizeConstraint, ViewContext, AnyElement, Element, SizeConstraint, ViewContext,
}; };
pub struct Hook<V> { pub struct Hook<V> {
@ -51,7 +51,7 @@ impl<V: 'static> Element<V> for Hook<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
self.child.paint(bounds.origin(), visible_bounds, view, cx); self.child.paint(bounds.origin(), visible_bounds, view, cx);
} }

View File

@ -5,7 +5,7 @@ use crate::{
vector::{vec2f, Vector2F}, vector::{vec2f, Vector2F},
}, },
json::{json, ToJson}, json::{json, ToJson},
scene, Element, ImageData, PaintContext, SizeConstraint, ViewContext, scene, Element, ImageData, SizeConstraint, ViewContext,
}; };
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Deserialize; use serde::Deserialize;
@ -95,7 +95,7 @@ impl<V: 'static> Element<V> for Image {
_: RectF, _: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
_: &mut V, _: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
if let Some(data) = layout { if let Some(data) = layout {
cx.scene().push_image(scene::Image { cx.scene().push_image(scene::Image {

View File

@ -65,7 +65,7 @@ impl<V: 'static> Element<V> for KeystrokeLabel {
visible_bounds: RectF, visible_bounds: RectF,
element: &mut AnyElement<V>, element: &mut AnyElement<V>,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
element.paint(bounds.origin(), visible_bounds, view, cx); element.paint(bounds.origin(), visible_bounds, view, cx);
} }

View File

@ -8,7 +8,7 @@ use crate::{
}, },
json::{ToJson, Value}, json::{ToJson, Value},
text_layout::{Line, RunStyle}, text_layout::{Line, RunStyle},
Element, PaintContext, SizeConstraint, ViewContext, Element, SizeConstraint, ViewContext,
}; };
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Deserialize; use serde::Deserialize;
@ -162,7 +162,7 @@ impl<V: 'static> Element<V> for Label {
visible_bounds: RectF, visible_bounds: RectF,
line: &mut Self::LayoutState, line: &mut Self::LayoutState,
_: &mut V, _: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();
line.paint(bounds.origin(), visible_bounds, bounds.size().y(), cx) line.paint(bounds.origin(), visible_bounds, bounds.size().y(), cx)

View File

@ -4,7 +4,7 @@ use crate::{
vector::{vec2f, Vector2F}, vector::{vec2f, Vector2F},
}, },
json::json, json::json,
AnyElement, Element, MouseRegion, PaintContext, SizeConstraint, ViewContext, AnyElement, Element, MouseRegion, SizeConstraint, ViewContext,
}; };
use std::{cell::RefCell, collections::VecDeque, fmt::Debug, ops::Range, rc::Rc}; use std::{cell::RefCell, collections::VecDeque, fmt::Debug, ops::Range, rc::Rc};
use sum_tree::{Bias, SumTree}; use sum_tree::{Bias, SumTree};
@ -253,7 +253,7 @@ impl<V: 'static> Element<V> for List<V> {
visible_bounds: RectF, visible_bounds: RectF,
scroll_top: &mut ListOffset, scroll_top: &mut ListOffset,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default(); let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default();
cx.scene().push_layer(Some(visible_bounds)); cx.scene().push_layer(Some(visible_bounds));
@ -643,7 +643,7 @@ impl<'a> sum_tree::SeekTarget<'a, ListItemSummary, ListItemSummary> for Height {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{elements::Empty, geometry::vector::vec2f, Entity, PaintContext}; use crate::{elements::Empty, geometry::vector::vec2f, Entity};
use rand::prelude::*; use rand::prelude::*;
use std::env; use std::env;
@ -949,7 +949,7 @@ mod tests {
(self.size, ()) (self.size, ())
} }
fn paint(&mut self, _: RectF, _: RectF, _: &mut (), _: &mut V, _: &mut PaintContext<V>) { fn paint(&mut self, _: RectF, _: RectF, _: &mut (), _: &mut V, _: &mut ViewContext<V>) {
unimplemented!() unimplemented!()
} }

View File

@ -10,8 +10,8 @@ use crate::{
CursorRegion, HandlerSet, MouseClick, MouseClickOut, MouseDown, MouseDownOut, MouseDrag, CursorRegion, HandlerSet, MouseClick, MouseClickOut, MouseDown, MouseDownOut, MouseDrag,
MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut,
}, },
AnyElement, Element, EventContext, MouseRegion, MouseState, PaintContext, SizeConstraint, AnyElement, Element, EventContext, MouseRegion, MouseState, SizeConstraint, TypeTag,
TypeTag, ViewContext, ViewContext,
}; };
use serde_json::json; use serde_json::json;
use std::ops::Range; use std::ops::Range;
@ -281,7 +281,7 @@ impl<V: 'static> Element<V> for MouseEventHandler<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
if self.above { if self.above {
self.child.paint(bounds.origin(), visible_bounds, view, cx); self.child.paint(bounds.origin(), visible_bounds, view, cx);

View File

@ -3,7 +3,7 @@ use std::ops::Range;
use crate::{ use crate::{
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json::ToJson, json::ToJson,
AnyElement, Axis, Element, MouseRegion, PaintContext, SizeConstraint, ViewContext, AnyElement, Axis, Element, MouseRegion, SizeConstraint, ViewContext,
}; };
use serde_json::json; use serde_json::json;
@ -141,7 +141,7 @@ impl<V: 'static> Element<V> for Overlay<V> {
_: RectF, _: RectF,
size: &mut Self::LayoutState, size: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
let (anchor_position, mut bounds) = match self.position_mode { let (anchor_position, mut bounds) = match self.position_mode {
OverlayPositionMode::Window => { OverlayPositionMode::Window => {

View File

@ -7,8 +7,7 @@ use serde_json::json;
use crate::{ use crate::{
geometry::rect::RectF, geometry::rect::RectF,
platform::{CursorStyle, MouseButton}, platform::{CursorStyle, MouseButton},
AnyElement, AppContext, Axis, Element, MouseRegion, PaintContext, SizeConstraint, TypeTag, AnyElement, AppContext, Axis, Element, MouseRegion, SizeConstraint, TypeTag, View, ViewContext,
View, ViewContext,
}; };
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@ -116,7 +115,7 @@ impl<V: 'static> Element<V> for Resizable<V> {
visible_bounds: pathfinder_geometry::rect::RectF, visible_bounds: pathfinder_geometry::rect::RectF,
constraint: &mut SizeConstraint, constraint: &mut SizeConstraint,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
cx.scene().push_stacking_context(None, None); cx.scene().push_stacking_context(None, None);
@ -252,7 +251,7 @@ impl<V: View, P: 'static> Element<V> for BoundsProvider<V, P> {
visible_bounds: pathfinder_geometry::rect::RectF, visible_bounds: pathfinder_geometry::rect::RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut crate::PaintContext<V>, cx: &mut crate::ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
cx.update_default_global::<ProviderMap, _, _>(|map, _| { cx.update_default_global::<ProviderMap, _, _>(|map, _| {
map.0.insert(TypeTag::new::<P>(), (bounds, visible_bounds)); map.0.insert(TypeTag::new::<P>(), (bounds, visible_bounds));

View File

@ -3,7 +3,7 @@ use std::ops::Range;
use crate::{ use crate::{
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json::{self, json, ToJson}, json::{self, json, ToJson},
AnyElement, Element, PaintContext, SizeConstraint, ViewContext, AnyElement, Element, SizeConstraint, ViewContext,
}; };
/// Element which renders it's children in a stack on top of each other. /// Element which renders it's children in a stack on top of each other.
@ -56,7 +56,7 @@ impl<V: 'static> Element<V> for Stack<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
for child in &mut self.children { for child in &mut self.children {
cx.scene().push_layer(None); cx.scene().push_layer(None);

View File

@ -1,6 +1,5 @@
use super::constrain_size_preserving_aspect_ratio; use super::constrain_size_preserving_aspect_ratio;
use crate::json::ToJson; use crate::json::ToJson;
use crate::PaintContext;
use crate::{ use crate::{
color::Color, color::Color,
geometry::{ geometry::{
@ -73,7 +72,7 @@ impl<V: 'static> Element<V> for Svg {
_visible_bounds: RectF, _visible_bounds: RectF,
svg: &mut Self::LayoutState, svg: &mut Self::LayoutState,
_: &mut V, _: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
if let Some(svg) = svg.clone() { if let Some(svg) = svg.clone() {
cx.scene().push_icon(scene::Icon { cx.scene().push_icon(scene::Icon {

View File

@ -7,7 +7,7 @@ use crate::{
}, },
json::{ToJson, Value}, json::{ToJson, Value},
text_layout::{Line, RunStyle, ShapedBoundary}, text_layout::{Line, RunStyle, ShapedBoundary},
Element, FontCache, PaintContext, SizeConstraint, TextLayoutCache, ViewContext, WindowContext, Element, FontCache, SizeConstraint, TextLayoutCache, ViewContext, WindowContext,
}; };
use log::warn; use log::warn;
use serde_json::json; use serde_json::json;
@ -169,7 +169,7 @@ impl<V: 'static> Element<V> for Text {
visible_bounds: RectF, visible_bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
_: &mut V, _: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
let mut origin = bounds.origin(); let mut origin = bounds.origin();
let empty = Vec::new(); let empty = Vec::new();

View File

@ -6,7 +6,7 @@ use crate::{
fonts::TextStyle, fonts::TextStyle,
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json::json, json::json,
Action, Axis, ElementStateHandle, PaintContext, SizeConstraint, Task, TypeTag, ViewContext, Action, Axis, ElementStateHandle, SizeConstraint, Task, TypeTag, ViewContext,
}; };
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Deserialize; use serde::Deserialize;
@ -207,7 +207,7 @@ impl<V: 'static> Element<V> for Tooltip<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
self.child.paint(bounds.origin(), visible_bounds, view, cx); self.child.paint(bounds.origin(), visible_bounds, view, cx);
if let Some(tooltip) = self.tooltip.as_mut() { if let Some(tooltip) = self.tooltip.as_mut() {

View File

@ -6,7 +6,7 @@ use crate::{
}, },
json::{self, json}, json::{self, json},
platform::ScrollWheelEvent, platform::ScrollWheelEvent,
AnyElement, MouseRegion, PaintContext, ViewContext, AnyElement, MouseRegion, ViewContext,
}; };
use json::ToJson; use json::ToJson;
use std::{cell::RefCell, cmp, ops::Range, rc::Rc}; use std::{cell::RefCell, cmp, ops::Range, rc::Rc};
@ -276,7 +276,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
visible_bounds: RectF, visible_bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default(); let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default();

View File

@ -1,4 +1,4 @@
use crate::{paint_context::PaintContext, ViewContext}; use crate::ViewContext;
use gpui::{geometry::rect::RectF, LayoutEngine, LayoutId}; use gpui::{geometry::rect::RectF, LayoutEngine, LayoutId};
use util::ResultExt; use util::ResultExt;
@ -40,13 +40,13 @@ impl<V: 'static> gpui::Element<V> for AdapterElement<V> {
_visible_bounds: RectF, _visible_bounds: RectF,
layout_data: &mut Option<(LayoutEngine, LayoutId)>, layout_data: &mut Option<(LayoutEngine, LayoutId)>,
view: &mut V, view: &mut V,
legacy_cx: &mut gpui::PaintContext<V>, cx: &mut gpui::ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
let (layout_engine, layout_id) = layout_data.take().unwrap(); let (layout_engine, layout_id) = layout_data.take().unwrap();
legacy_cx.push_layout_engine(layout_engine); cx.push_layout_engine(layout_engine);
let mut cx = PaintContext::new(legacy_cx); self.0
self.0.paint(view, bounds.origin(), &mut cx); .paint(view, bounds.origin(), &mut ViewContext::new(cx));
*layout_data = legacy_cx.pop_layout_engine().zip(Some(layout_id)); *layout_data = cx.pop_layout_engine().zip(Some(layout_id));
debug_assert!(layout_data.is_some()); debug_assert!(layout_data.is_some());
} }

View File

@ -1,4 +1,3 @@
pub use crate::paint_context::PaintContext;
pub use crate::ViewContext; pub use crate::ViewContext;
use anyhow::Result; use anyhow::Result;
use gpui::geometry::vector::Vector2F; use gpui::geometry::vector::Vector2F;
@ -22,7 +21,7 @@ pub trait Element<V: 'static>: 'static + IntoElement<V> {
parent_origin: Vector2F, parent_origin: Vector2F,
layout: &Layout, layout: &Layout,
state: &mut Self::PaintState, state: &mut Self::PaintState,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) where ) where
Self: Sized; 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>. /// Used to make ElementState<V, E> into a trait object, so we can wrap it in AnyElement<V>.
trait AnyStatefulElement<V> { trait AnyStatefulElement<V> {
fn layout(&mut self, view: &mut V, cx: &mut ViewContext<V>) -> Result<LayoutId>; 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. /// 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 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) { self.phase = match std::mem::take(&mut self.phase) {
ElementPhase::PostLayout { ElementPhase::PostLayout {
layout_id, layout_id,
@ -149,7 +148,7 @@ impl<V> AnyElement<V> {
self.0.layout(view, cx) 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) self.0.paint(view, parent_origin, cx)
} }
} }

View File

@ -3,7 +3,6 @@ use std::{cell::Cell, rc::Rc};
use crate::{ use crate::{
element::{AnyElement, Element, IntoElement, Layout, ParentElement}, element::{AnyElement, Element, IntoElement, Layout, ParentElement},
hsla, hsla,
paint_context::PaintContext,
style::{CornerRadii, Overflow, Style, StyleHelpers, Styleable}, style::{CornerRadii, Overflow, Style, StyleHelpers, Styleable},
InteractionHandlers, Interactive, ViewContext, InteractionHandlers, Interactive, ViewContext,
}; };
@ -69,7 +68,7 @@ impl<V: 'static> Element<V> for Div<V> {
parent_origin: Vector2F, parent_origin: Vector2F,
layout: &Layout, layout: &Layout,
child_layouts: &mut Vec<LayoutId>, child_layouts: &mut Vec<LayoutId>,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) where ) where
Self: Sized, Self: Sized,
{ {
@ -167,7 +166,7 @@ impl<V: 'static> Div<V> {
bounds: RectF, bounds: RectF,
overflow: Point<Overflow>, overflow: Point<Overflow>,
child_layout_ids: &[LayoutId], child_layout_ids: &[LayoutId],
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
if overflow.y == Overflow::Scroll || overflow.x == Overflow::Scroll { if overflow.y == Overflow::Scroll || overflow.x == Overflow::Scroll {
let mut scroll_max = Vector2F::zero(); 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 style = self.styles.merged();
let bounds = layout.bounds + parent_origin; let bounds = layout.bounds + parent_origin;

View File

@ -1,7 +1,6 @@
use crate::{ use crate::{
element::{AnyElement, Element, IntoElement, Layout, ParentElement}, element::{AnyElement, Element, IntoElement, Layout, ParentElement},
interactive::{InteractionHandlers, Interactive}, interactive::{InteractionHandlers, Interactive},
paint_context::PaintContext,
style::{Style, StyleHelpers, Styleable}, style::{Style, StyleHelpers, Styleable},
ViewContext, ViewContext,
}; };
@ -59,7 +58,7 @@ impl<V: 'static, E: Element<V> + Styleable> Element<V> for Hoverable<E> {
parent_origin: Vector2F, parent_origin: Vector2F,
layout: &Layout, layout: &Layout,
paint_state: &mut Self::PaintState, paint_state: &mut Self::PaintState,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) where ) where
Self: Sized, Self: Sized,
{ {

View File

@ -53,7 +53,7 @@ impl<V: 'static> Element<V> for Img {
parent_origin: Vector2F, parent_origin: Vector2F,
layout: &gpui::Layout, layout: &gpui::Layout,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut crate::paint_context::PaintContext<V>, cx: &mut crate::ViewContext<V>,
) where ) where
Self: Sized, Self: Sized,
{ {

View File

@ -1,7 +1,6 @@
use crate::{ use crate::{
element::{AnyElement, Element, IntoElement, Layout, ParentElement}, element::{AnyElement, Element, IntoElement, Layout, ParentElement},
interactive::{InteractionHandlers, Interactive}, interactive::{InteractionHandlers, Interactive},
paint_context::PaintContext,
style::{Style, StyleHelpers, Styleable}, style::{Style, StyleHelpers, Styleable},
ViewContext, ViewContext,
}; };
@ -59,7 +58,7 @@ impl<V: 'static, E: Element<V> + Styleable> Element<V> for Pressable<E> {
parent_origin: Vector2F, parent_origin: Vector2F,
layout: &Layout, layout: &Layout,
paint_state: &mut Self::PaintState, paint_state: &mut Self::PaintState,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) where ) where
Self: Sized, Self: Sized,
{ {

View File

@ -49,7 +49,7 @@ impl<V: 'static> Element<V> for Svg {
parent_origin: Vector2F, parent_origin: Vector2F,
layout: &Layout, layout: &Layout,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut crate::paint_context::PaintContext<V>, cx: &mut crate::ViewContext<V>,
) where ) where
Self: Sized, Self: Sized,
{ {

View File

@ -1,6 +1,5 @@
use crate::{ use crate::{
element::{Element, IntoElement, Layout}, element::{Element, IntoElement, Layout},
paint_context::PaintContext,
ViewContext, ViewContext,
}; };
use anyhow::Result; use anyhow::Result;
@ -71,7 +70,7 @@ impl<V: 'static> Element<V> for Text {
parent_origin: Vector2F, parent_origin: Vector2F,
layout: &Layout, layout: &Layout,
paint_state: &mut Self::PaintState, paint_state: &mut Self::PaintState,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) { ) {
let bounds = layout.bounds + parent_origin; let bounds = layout.bounds + parent_origin;

View File

@ -3,7 +3,6 @@ pub mod color;
pub mod element; pub mod element;
pub mod elements; pub mod elements;
pub mod interactive; pub mod interactive;
pub mod paint_context;
pub mod style; pub mod style;
pub mod view; pub mod view;
pub mod view_context; pub mod view_context;

View File

@ -6,7 +6,7 @@ use gpui::{
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{cell::Cell, rc::Rc}; use std::{cell::Cell, rc::Rc};
use crate::element::PaintContext; use crate::ViewContext;
pub trait Interactive<V: 'static> { pub trait Interactive<V: 'static> {
fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V>; fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V>;
@ -121,7 +121,7 @@ pub struct InteractionHandlers<V: 'static> {
} }
impl<V: 'static> InteractionHandlers<V> { 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() { for handler in self.mouse_down.iter().cloned() {
cx.on_event(order, move |view, event: &MouseButtonEvent, cx| { cx.on_event(order, move |view, event: &MouseButtonEvent, cx| {
if event.is_down && bounds.contains_point(event.position) { if event.is_down && bounds.contains_point(event.position) {

View File

@ -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)
}
}

View File

@ -2,7 +2,7 @@ use crate::{
color::Hsla, color::Hsla,
elements::hoverable::{hoverable, Hoverable}, elements::hoverable::{hoverable, Hoverable},
elements::pressable::{pressable, Pressable}, elements::pressable::{pressable, Pressable},
paint_context::PaintContext, ViewContext,
}; };
pub use fonts::Style as FontStyle; pub use fonts::Style as FontStyle;
pub use fonts::Weight as FontWeight; pub use fonts::Weight as FontWeight;
@ -164,7 +164,7 @@ impl Style {
} }
/// Paints the background of an element styled with this 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(); let rem_size = cx.rem_size();
if let Some(color) = self.fill.as_ref().and_then(Fill::color) { if let Some(color) = self.fill.as_ref().and_then(Fill::color) {
cx.scene().push_quad(gpui::Quad { cx.scene().push_quad(gpui::Quad {
@ -177,7 +177,7 @@ impl Style {
} }
/// Paints the foreground of an element styled with this 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(); let rem_size = cx.rem_size();
if let Some(color) = self.border_color { if let Some(color) = self.border_color {

View File

@ -1,7 +1,9 @@
use std::{any::TypeId, rc::Rc};
use crate::{element::LayoutId, style::Style}; use crate::{element::LayoutId, style::Style};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use derive_more::{Deref, DerefMut}; 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}; pub use gpui::{taffy::tree::NodeId, ViewContext as LegacyViewContext};
#[derive(Deref, DerefMut)] #[derive(Deref, DerefMut)]
@ -44,4 +46,34 @@ impl<'a, 'b, 'c, V: 'static> ViewContext<'a, 'b, 'c, V> {
Ok(layout_id) 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)
}
} }

View File

@ -80,7 +80,7 @@ pub fn derive_element(input: TokenStream) -> TokenStream {
parent_origin: gpui2::Vector2F, parent_origin: gpui2::Vector2F,
_: &gpui2::element::Layout, _: &gpui2::element::Layout,
rendered_element: &mut Self::PaintState, rendered_element: &mut Self::PaintState,
cx: &mut gpui2::element::PaintContext<V>, cx: &mut gpui2::ViewContext<V>,
) { ) {
rendered_element.paint(view, parent_origin, cx); rendered_element.paint(view, parent_origin, cx);
} }

View File

@ -342,7 +342,7 @@ pub fn element_derive(input: TokenStream) -> TokenStream {
visible_bounds: gpui::geometry::rect::RectF, visible_bounds: gpui::geometry::rect::RectF,
element: &mut gpui::elements::AnyElement<V>, element: &mut gpui::elements::AnyElement<V>,
view: &mut V, view: &mut V,
cx: &mut gpui::PaintContext<V>, cx: &mut gpui::ViewContext<V>,
) { ) {
element.paint(bounds.origin(), visible_bounds, view, cx); element.paint(bounds.origin(), visible_bounds, view, cx);
} }

View File

@ -1,7 +1,6 @@
use gpui2::{ use gpui2::{
color::Hsla, color::Hsla, element::Element, serde_json, AppContext, IntoElement, Vector2F, ViewContext,
element::{Element, PaintContext}, WindowContext,
serde_json, AppContext, IntoElement, Vector2F, ViewContext, WindowContext,
}; };
use serde::{de::Visitor, Deserialize, Deserializer}; use serde::{de::Visitor, Deserialize, Deserializer};
use std::{collections::HashMap, fmt, marker::PhantomData}; use std::{collections::HashMap, fmt, marker::PhantomData};
@ -162,7 +161,7 @@ impl<V: 'static, E: Element<V>> Element<V> for Themed<V, E> {
parent_origin: Vector2F, parent_origin: Vector2F,
layout: &gpui2::Layout, layout: &gpui2::Layout,
state: &mut Self::PaintState, state: &mut Self::PaintState,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) where ) where
Self: Sized, Self: Sized,
{ {

View File

@ -10,8 +10,8 @@ use gpui::{
platform::{CursorStyle, MouseButton}, platform::{CursorStyle, MouseButton},
serde_json::json, serde_json::json,
text_layout::{Line, RunStyle}, text_layout::{Line, RunStyle},
AnyElement, Element, EventContext, FontCache, ModelContext, MouseRegion, PaintContext, Quad, AnyElement, Element, EventContext, FontCache, ModelContext, MouseRegion, Quad, SizeConstraint,
SizeConstraint, TextLayoutCache, ViewContext, WeakModelHandle, WindowContext, TextLayoutCache, ViewContext, WeakModelHandle, WindowContext,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::CursorShape; use language::CursorShape;
@ -733,7 +733,7 @@ impl Element<TerminalView> for TerminalElement {
visible_bounds: RectF, visible_bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
view: &mut TerminalView, view: &mut TerminalView,
cx: &mut PaintContext<TerminalView>, cx: &mut ViewContext<TerminalView>,
) -> Self::PaintState { ) -> Self::PaintState {
let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();

View File

@ -25,8 +25,8 @@ use gpui::{
keymap_matcher::KeymapContext, keymap_matcher::KeymapContext,
platform::{CursorStyle, MouseButton, NavigationDirection, PromptLevel}, platform::{CursorStyle, MouseButton, NavigationDirection, PromptLevel},
Action, AnyViewHandle, AnyWeakViewHandle, AppContext, AsyncAppContext, Entity, EventContext, Action, AnyViewHandle, AnyWeakViewHandle, AppContext, AsyncAppContext, Entity, EventContext,
ModelHandle, MouseRegion, PaintContext, Quad, Task, View, ViewContext, ViewHandle, ModelHandle, MouseRegion, Quad, Task, View, ViewContext, ViewHandle, WeakViewHandle,
WeakViewHandle, WindowContext, WindowContext,
}; };
use project::{Project, ProjectEntryId, ProjectPath}; use project::{Project, ProjectEntryId, ProjectPath};
use serde::Deserialize; use serde::Deserialize;
@ -2011,7 +2011,7 @@ impl<V: 'static> Element<V> for PaneBackdrop<V> {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut V, view: &mut V,
cx: &mut PaintContext<V>, cx: &mut ViewContext<V>,
) -> Self::PaintState { ) -> Self::PaintState {
let background = theme::current(cx).editor.background; let background = theme::current(cx).editor.background;

View File

@ -594,7 +594,7 @@ mod element {
json::{self, ToJson}, json::{self, ToJson},
platform::{CursorStyle, MouseButton}, platform::{CursorStyle, MouseButton},
scene::MouseDrag, scene::MouseDrag,
AnyElement, Axis, CursorRegion, Element, EventContext, MouseRegion, PaintContext, RectFExt, AnyElement, Axis, CursorRegion, Element, EventContext, MouseRegion, RectFExt,
SizeConstraint, Vector2FExt, ViewContext, SizeConstraint, Vector2FExt, ViewContext,
}; };
@ -855,7 +855,7 @@ mod element {
visible_bounds: RectF, visible_bounds: RectF,
remaining_space: &mut Self::LayoutState, remaining_space: &mut Self::LayoutState,
view: &mut Workspace, view: &mut Workspace,
cx: &mut PaintContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Self::PaintState { ) -> Self::PaintState {
let can_resize = settings::get::<WorkspaceSettings>(cx).active_pane_magnification == 1.; let can_resize = settings::get::<WorkspaceSettings>(cx).active_pane_magnification == 1.;
let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();

View File

@ -8,8 +8,8 @@ use gpui::{
vector::{vec2f, Vector2F}, vector::{vec2f, Vector2F},
}, },
json::{json, ToJson}, json::{json, ToJson},
AnyElement, AnyViewHandle, Entity, PaintContext, SizeConstraint, Subscription, View, AnyElement, AnyViewHandle, Entity, SizeConstraint, Subscription, View, ViewContext, ViewHandle,
ViewContext, ViewHandle, WindowContext, WindowContext,
}; };
pub trait StatusItemView: View { pub trait StatusItemView: View {
@ -230,7 +230,7 @@ impl Element<StatusBar> for StatusBarElement {
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
view: &mut StatusBar, view: &mut StatusBar,
cx: &mut PaintContext<StatusBar>, cx: &mut ViewContext<StatusBar>,
) -> Self::PaintState { ) -> Self::PaintState {
let origin_y = bounds.upper_right().y(); let origin_y = bounds.upper_right().y();
let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();