Rename context parameters to cx in gpui

This commit is contained in:
Max Brunsfeld 2021-05-28 15:25:15 -07:00
parent 173f99748d
commit 6ef447866a
20 changed files with 643 additions and 733 deletions

View file

@ -10,9 +10,9 @@ use simplelog::SimpleLogger;
fn main() { fn main() {
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
gpui::App::new(()).unwrap().run(|ctx| { gpui::App::new(()).unwrap().run(|cx| {
ctx.platform().activate(true); cx.platform().activate(true);
ctx.add_window(|_| TextView); cx.add_window(|_| TextView);
}); });
} }
@ -58,15 +58,15 @@ impl gpui::Element for TextElement {
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut gpui::PaintContext, cx: &mut gpui::PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
let font_size = 12.; let font_size = 12.;
let family = ctx.font_cache.load_family(&["SF Pro Display"]).unwrap(); let family = cx.font_cache.load_family(&["SF Pro Display"]).unwrap();
let normal = ctx let normal = cx
.font_cache .font_cache
.select_font(family, &Default::default()) .select_font(family, &Default::default())
.unwrap(); .unwrap();
let bold = ctx let bold = cx
.font_cache .font_cache
.select_font( .select_font(
family, family,
@ -78,7 +78,7 @@ impl gpui::Element for TextElement {
.unwrap(); .unwrap();
let text = "Hello world!"; let text = "Hello world!";
let line = ctx.text_layout_cache.layout_str( let line = cx.text_layout_cache.layout_str(
text, text,
font_size, font_size,
&[ &[
@ -90,12 +90,12 @@ impl gpui::Element for TextElement {
], ],
); );
ctx.scene.push_quad(Quad { cx.scene.push_quad(Quad {
bounds: bounds, bounds: bounds,
background: Some(ColorU::white()), background: Some(ColorU::white()),
..Default::default() ..Default::default()
}); });
line.paint(bounds.origin(), bounds, ctx); line.paint(bounds.origin(), bounds, cx);
} }
fn dispatch_event( fn dispatch_event(

File diff suppressed because it is too large Load diff

View file

@ -38,11 +38,11 @@ use replace_with::replace_with_or_abort;
use std::{any::Any, borrow::Cow}; use std::{any::Any, borrow::Cow};
trait AnyElement { trait AnyElement {
fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F; fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F;
fn after_layout(&mut self, _: &mut AfterLayoutContext) {} fn after_layout(&mut self, _: &mut AfterLayoutContext) {}
fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext); fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext);
fn dispatch_event(&mut self, event: &Event, ctx: &mut EventContext) -> bool; fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool;
fn debug(&self, ctx: &DebugContext) -> serde_json::Value; fn debug(&self, cx: &DebugContext) -> serde_json::Value;
fn size(&self) -> Vector2F; fn size(&self) -> Vector2F;
fn metadata(&self) -> Option<&dyn Any>; fn metadata(&self) -> Option<&dyn Any>;
@ -55,21 +55,21 @@ pub trait Element {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState); ) -> (Vector2F, Self::LayoutState);
fn after_layout( fn after_layout(
&mut self, &mut self,
size: Vector2F, size: Vector2F,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
); );
fn paint( fn paint(
&mut self, &mut self,
bounds: RectF, bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState; ) -> Self::PaintState;
fn dispatch_event( fn dispatch_event(
@ -78,7 +78,7 @@ pub trait Element {
bounds: RectF, bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
paint: &mut Self::PaintState, paint: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool; ) -> bool;
fn metadata(&self) -> Option<&dyn Any> { fn metadata(&self) -> Option<&dyn Any> {
@ -90,7 +90,7 @@ pub trait Element {
bounds: RectF, bounds: RectF,
layout: &Self::LayoutState, layout: &Self::LayoutState,
paint: &Self::PaintState, paint: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> serde_json::Value; ) -> serde_json::Value;
fn boxed(self) -> ElementBox fn boxed(self) -> ElementBox
@ -138,13 +138,13 @@ pub struct ElementBox {
} }
impl<T: Element> AnyElement for Lifecycle<T> { impl<T: Element> AnyElement for Lifecycle<T> {
fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F { fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F {
let mut result = None; let mut result = None;
replace_with_or_abort(self, |me| match me { replace_with_or_abort(self, |me| match me {
Lifecycle::Init { mut element } Lifecycle::Init { mut element }
| Lifecycle::PostLayout { mut element, .. } | Lifecycle::PostLayout { mut element, .. }
| Lifecycle::PostPaint { mut element, .. } => { | Lifecycle::PostPaint { mut element, .. } => {
let (size, layout) = element.layout(constraint, ctx); let (size, layout) = element.layout(constraint, cx);
debug_assert!(size.x().is_finite()); debug_assert!(size.x().is_finite());
debug_assert!(size.y().is_finite()); debug_assert!(size.y().is_finite());
@ -160,7 +160,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
result.unwrap() result.unwrap()
} }
fn after_layout(&mut self, ctx: &mut AfterLayoutContext) { fn after_layout(&mut self, cx: &mut AfterLayoutContext) {
if let Lifecycle::PostLayout { if let Lifecycle::PostLayout {
element, element,
size, size,
@ -168,13 +168,13 @@ impl<T: Element> AnyElement for Lifecycle<T> {
.. ..
} = self } = self
{ {
element.after_layout(*size, layout, ctx); element.after_layout(*size, layout, cx);
} else { } else {
panic!("invalid element lifecycle state"); panic!("invalid element lifecycle state");
} }
} }
fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext) { fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) {
replace_with_or_abort(self, |me| { replace_with_or_abort(self, |me| {
if let Lifecycle::PostLayout { if let Lifecycle::PostLayout {
mut element, mut element,
@ -184,7 +184,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
} = me } = me
{ {
let bounds = RectF::new(origin, size); let bounds = RectF::new(origin, size);
let paint = element.paint(bounds, &mut layout, ctx); let paint = element.paint(bounds, &mut layout, cx);
Lifecycle::PostPaint { Lifecycle::PostPaint {
element, element,
constraint, constraint,
@ -198,7 +198,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
}); });
} }
fn dispatch_event(&mut self, event: &Event, ctx: &mut EventContext) -> bool { fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool {
if let Lifecycle::PostPaint { if let Lifecycle::PostPaint {
element, element,
bounds, bounds,
@ -207,7 +207,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
.. ..
} = self } = self
{ {
element.dispatch_event(event, *bounds, layout, paint, ctx) element.dispatch_event(event, *bounds, layout, paint, cx)
} else { } else {
panic!("invalid element lifecycle state"); panic!("invalid element lifecycle state");
} }
@ -229,7 +229,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
} }
} }
fn debug(&self, ctx: &DebugContext) -> serde_json::Value { fn debug(&self, cx: &DebugContext) -> serde_json::Value {
match self { match self {
Lifecycle::PostPaint { Lifecycle::PostPaint {
element, element,
@ -238,7 +238,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
layout, layout,
paint, paint,
} => { } => {
let mut value = element.debug(*bounds, layout, paint, ctx); let mut value = element.debug(*bounds, layout, paint, cx);
if let json::Value::Object(map) = &mut value { if let json::Value::Object(map) = &mut value {
let mut new_map: crate::json::Map<String, serde_json::Value> = let mut new_map: crate::json::Map<String, serde_json::Value> =
Default::default(); Default::default();
@ -258,20 +258,20 @@ impl<T: Element> AnyElement for Lifecycle<T> {
} }
impl ElementBox { impl ElementBox {
pub fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F { pub fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F {
self.element.layout(constraint, ctx) self.element.layout(constraint, cx)
} }
pub fn after_layout(&mut self, ctx: &mut AfterLayoutContext) { pub fn after_layout(&mut self, cx: &mut AfterLayoutContext) {
self.element.after_layout(ctx); self.element.after_layout(cx);
} }
pub fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext) { pub fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) {
self.element.paint(origin, ctx); self.element.paint(origin, cx);
} }
pub fn dispatch_event(&mut self, event: &Event, ctx: &mut EventContext) -> bool { pub fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool {
self.element.dispatch_event(event, ctx) self.element.dispatch_event(event, cx)
} }
pub fn size(&self) -> Vector2F { pub fn size(&self) -> Vector2F {
@ -282,8 +282,8 @@ impl ElementBox {
self.element.metadata() self.element.metadata()
} }
pub fn debug(&self, ctx: &DebugContext) -> json::Value { pub fn debug(&self, cx: &DebugContext) -> json::Value {
let mut value = self.element.debug(ctx); let mut value = self.element.debug(cx);
if let Some(name) = &self.name { if let Some(name) = &self.name {
if let json::Value::Object(map) = &mut value { if let json::Value::Object(map) = &mut value {

View file

@ -37,11 +37,11 @@ impl Element for Align {
fn layout( fn layout(
&mut self, &mut self,
mut constraint: SizeConstraint, mut constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let mut size = constraint.max; let mut size = constraint.max;
constraint.min = Vector2F::zero(); constraint.min = Vector2F::zero();
let child_size = self.child.layout(constraint, ctx); let child_size = self.child.layout(constraint, cx);
if size.x().is_infinite() { if size.x().is_infinite() {
size.set_x(child_size.x()); size.set_x(child_size.x());
} }
@ -55,16 +55,16 @@ impl Element for Align {
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
self.child.after_layout(ctx); self.child.after_layout(cx);
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: pathfinder_geometry::rect::RectF, bounds: pathfinder_geometry::rect::RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> 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;
@ -73,7 +73,7 @@ impl Element for Align {
let child_target = child_center + child_center * self.alignment; let child_target = child_center + child_center * self.alignment;
self.child self.child
.paint(bounds.origin() - (child_target - my_target), ctx); .paint(bounds.origin() - (child_target - my_target), cx);
} }
fn dispatch_event( fn dispatch_event(
@ -82,9 +82,9 @@ impl Element for Align {
_: pathfinder_geometry::rect::RectF, _: pathfinder_geometry::rect::RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
self.child.dispatch_event(event, ctx) self.child.dispatch_event(event, cx)
} }
fn debug( fn debug(
@ -92,13 +92,13 @@ impl Element for Align {
bounds: pathfinder_geometry::rect::RectF, bounds: pathfinder_geometry::rect::RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> json::Value { ) -> json::Value {
json!({ json!({
"type": "Align", "type": "Align",
"bounds": bounds.to_json(), "bounds": bounds.to_json(),
"alignment": self.alignment.to_json(), "alignment": self.alignment.to_json(),
"child": self.child.debug(ctx), "child": self.child.debug(cx),
}) })
} }
} }

View file

@ -51,9 +51,9 @@ where
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
self.0(bounds, ctx) self.0(bounds, cx)
} }
fn after_layout( fn after_layout(

View file

@ -58,12 +58,12 @@ impl Element for ConstrainedBox {
fn layout( fn layout(
&mut self, &mut self,
mut constraint: SizeConstraint, mut constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
constraint.min = constraint.min.max(self.constraint.min); constraint.min = constraint.min.max(self.constraint.min);
constraint.max = constraint.max.min(self.constraint.max); constraint.max = constraint.max.min(self.constraint.max);
constraint.max = constraint.max.max(constraint.min); constraint.max = constraint.max.max(constraint.min);
let size = self.child.layout(constraint, ctx); let size = self.child.layout(constraint, cx);
(size, ()) (size, ())
} }
@ -71,18 +71,18 @@ impl Element for ConstrainedBox {
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
self.child.after_layout(ctx); self.child.after_layout(cx);
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
self.child.paint(bounds.origin(), ctx); self.child.paint(bounds.origin(), cx);
} }
fn dispatch_event( fn dispatch_event(
@ -91,9 +91,9 @@ impl Element for ConstrainedBox {
_: RectF, _: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
self.child.dispatch_event(event, ctx) self.child.dispatch_event(event, cx)
} }
fn debug( fn debug(
@ -101,8 +101,8 @@ impl Element for ConstrainedBox {
_: RectF, _: RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> json::Value { ) -> json::Value {
json!({"type": "ConstrainedBox", "set_constraint": self.constraint.to_json(), "child": self.child.debug(ctx)}) json!({"type": "ConstrainedBox", "set_constraint": self.constraint.to_json(), "child": self.child.debug(cx)})
} }
} }

View file

@ -141,14 +141,14 @@ impl Element for Container {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let size_buffer = self.margin_size() + self.padding_size() + self.border_size(); let size_buffer = self.margin_size() + self.padding_size() + self.border_size();
let child_constraint = SizeConstraint { let child_constraint = SizeConstraint {
min: (constraint.min - size_buffer).max(Vector2F::zero()), min: (constraint.min - size_buffer).max(Vector2F::zero()),
max: (constraint.max - size_buffer).max(Vector2F::zero()), max: (constraint.max - size_buffer).max(Vector2F::zero()),
}; };
let child_size = self.child.layout(child_constraint, ctx); let child_size = self.child.layout(child_constraint, cx);
(child_size + size_buffer, ()) (child_size + size_buffer, ())
} }
@ -156,16 +156,16 @@ impl Element for Container {
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
self.child.after_layout(ctx); self.child.after_layout(cx);
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
let quad_bounds = RectF::from_points( let quad_bounds = RectF::from_points(
bounds.origin() + vec2f(self.margin.left, self.margin.top), bounds.origin() + vec2f(self.margin.left, self.margin.top),
@ -173,14 +173,14 @@ impl Element for Container {
); );
if let Some(shadow) = self.shadow.as_ref() { if let Some(shadow) = self.shadow.as_ref() {
ctx.scene.push_shadow(scene::Shadow { cx.scene.push_shadow(scene::Shadow {
bounds: quad_bounds + shadow.offset, bounds: quad_bounds + shadow.offset,
corner_radius: self.corner_radius, corner_radius: self.corner_radius,
sigma: shadow.blur, sigma: shadow.blur,
color: shadow.color, color: shadow.color,
}); });
} }
ctx.scene.push_quad(Quad { cx.scene.push_quad(Quad {
bounds: quad_bounds, bounds: quad_bounds,
background: self.background_color, background: self.background_color,
border: self.border, border: self.border,
@ -190,7 +190,7 @@ impl Element for Container {
let child_origin = quad_bounds.origin() let child_origin = quad_bounds.origin()
+ vec2f(self.padding.left, self.padding.top) + vec2f(self.padding.left, self.padding.top)
+ vec2f(self.border.left_width(), self.border.top_width()); + vec2f(self.border.left_width(), self.border.top_width());
self.child.paint(child_origin, ctx); self.child.paint(child_origin, cx);
} }
fn dispatch_event( fn dispatch_event(
@ -199,9 +199,9 @@ impl Element for Container {
_: RectF, _: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
self.child.dispatch_event(event, ctx) self.child.dispatch_event(event, cx)
} }
fn debug( fn debug(
@ -209,7 +209,7 @@ impl Element for Container {
bounds: RectF, bounds: RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &crate::DebugContext, cx: &crate::DebugContext,
) -> serde_json::Value { ) -> serde_json::Value {
json!({ json!({
"type": "Container", "type": "Container",
@ -222,7 +222,7 @@ impl Element for Container {
"corner_radius": self.corner_radius, "corner_radius": self.corner_radius,
"shadow": self.shadow.to_json(), "shadow": self.shadow.to_json(),
}, },
"child": self.child.debug(ctx), "child": self.child.debug(cx),
}) })
} }
} }

View file

@ -35,9 +35,9 @@ impl Element for EventHandler {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let size = self.child.layout(constraint, ctx); let size = self.child.layout(constraint, cx);
(size, ()) (size, ())
} }
@ -45,18 +45,18 @@ impl Element for EventHandler {
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
self.child.after_layout(ctx); self.child.after_layout(cx);
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
self.child.paint(bounds.origin(), ctx); self.child.paint(bounds.origin(), cx);
} }
fn dispatch_event( fn dispatch_event(
@ -65,16 +65,16 @@ impl Element for EventHandler {
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
if self.child.dispatch_event(event, ctx) { if self.child.dispatch_event(event, cx) {
true true
} else { } else {
match event { match event {
Event::LeftMouseDown { position, .. } => { Event::LeftMouseDown { position, .. } => {
if let Some(callback) = self.mouse_down.as_mut() { if let Some(callback) = self.mouse_down.as_mut() {
if bounds.contains_point(*position) { if bounds.contains_point(*position) {
return callback(ctx); return callback(cx);
} }
} }
false false
@ -89,11 +89,11 @@ impl Element for EventHandler {
_: RectF, _: RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> serde_json::Value { ) -> serde_json::Value {
json!({ json!({
"type": "EventHandler", "type": "EventHandler",
"child": self.child.debug(ctx), "child": self.child.debug(cx),
}) })
} }
} }

View file

@ -53,7 +53,7 @@ impl Element for Flex {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let mut total_flex = 0.0; let mut total_flex = 0.0;
let mut fixed_space = 0.0; let mut fixed_space = 0.0;
@ -74,7 +74,7 @@ impl Element for Flex {
vec2f(constraint.max.x(), INFINITY), vec2f(constraint.max.x(), INFINITY),
), ),
}; };
let size = child.layout(child_constraint, ctx); let size = child.layout(child_constraint, cx);
fixed_space += size.along(self.axis); fixed_space += size.along(self.axis);
cross_axis_max = cross_axis_max.max(size.along(cross_axis)); cross_axis_max = cross_axis_max.max(size.along(cross_axis));
} }
@ -105,7 +105,7 @@ impl Element for Flex {
vec2f(constraint.max.x(), child_max), vec2f(constraint.max.x(), child_max),
), ),
}; };
let child_size = child.layout(child_constraint, ctx); let child_size = child.layout(child_constraint, cx);
remaining_space -= child_size.along(self.axis); remaining_space -= child_size.along(self.axis);
remaining_flex -= flex; remaining_flex -= flex;
cross_axis_max = cross_axis_max.max(child_size.along(cross_axis)); cross_axis_max = cross_axis_max.max(child_size.along(cross_axis));
@ -138,10 +138,10 @@ impl Element for Flex {
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
for child in &mut self.children { for child in &mut self.children {
child.after_layout(ctx); child.after_layout(cx);
} }
} }
@ -149,11 +149,11 @@ impl Element for Flex {
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
let mut child_origin = bounds.origin(); let mut child_origin = bounds.origin();
for child in &mut self.children { for child in &mut self.children {
child.paint(child_origin, ctx); child.paint(child_origin, cx);
match self.axis { match self.axis {
Axis::Horizontal => child_origin += vec2f(child.size().x(), 0.0), Axis::Horizontal => child_origin += vec2f(child.size().x(), 0.0),
Axis::Vertical => child_origin += vec2f(0.0, child.size().y()), Axis::Vertical => child_origin += vec2f(0.0, child.size().y()),
@ -167,11 +167,11 @@ impl Element for Flex {
_: RectF, _: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
let mut handled = false; let mut handled = false;
for child in &mut self.children { for child in &mut self.children {
handled = child.dispatch_event(event, ctx) || handled; handled = child.dispatch_event(event, cx) || handled;
} }
handled handled
} }
@ -181,13 +181,13 @@ impl Element for Flex {
bounds: RectF, bounds: RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> json::Value { ) -> json::Value {
json!({ json!({
"type": "Flex", "type": "Flex",
"bounds": bounds.to_json(), "bounds": bounds.to_json(),
"axis": self.axis.to_json(), "axis": self.axis.to_json(),
"children": self.children.iter().map(|child| child.debug(ctx)).collect::<Vec<json::Value>>() "children": self.children.iter().map(|child| child.debug(cx)).collect::<Vec<json::Value>>()
}) })
} }
} }
@ -217,9 +217,9 @@ impl Element for Expanded {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let size = self.child.layout(constraint, ctx); let size = self.child.layout(constraint, cx);
(size, ()) (size, ())
} }
@ -227,18 +227,18 @@ impl Element for Expanded {
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
self.child.after_layout(ctx); self.child.after_layout(cx);
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
self.child.paint(bounds.origin(), ctx) self.child.paint(bounds.origin(), cx)
} }
fn dispatch_event( fn dispatch_event(
@ -247,9 +247,9 @@ impl Element for Expanded {
_: RectF, _: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
self.child.dispatch_event(event, ctx) self.child.dispatch_event(event, cx)
} }
fn metadata(&self) -> Option<&dyn Any> { fn metadata(&self) -> Option<&dyn Any> {
@ -261,12 +261,12 @@ impl Element for Expanded {
_: RectF, _: RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> Value { ) -> Value {
json!({ json!({
"type": "Expanded", "type": "Expanded",
"flex": self.metadata.flex, "flex": self.metadata.flex,
"child": self.child.debug(ctx) "child": self.child.debug(cx)
}) })
} }
} }

View file

@ -109,20 +109,20 @@ impl Element for Label {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let font_id = ctx let font_id = cx
.font_cache .font_cache
.select_font(self.family_id, &self.font_properties) .select_font(self.family_id, &self.font_properties)
.unwrap(); .unwrap();
let runs = self.compute_runs(&ctx.font_cache, font_id); let runs = self.compute_runs(&cx.font_cache, font_id);
let line = let line =
ctx.text_layout_cache cx.text_layout_cache
.layout_str(self.text.as_str(), self.font_size, runs.as_slice()); .layout_str(self.text.as_str(), self.font_size, runs.as_slice());
let size = vec2f( let size = vec2f(
line.width().max(constraint.min.x()).min(constraint.max.x()), line.width().max(constraint.min.x()).min(constraint.max.x()),
ctx.font_cache.line_height(font_id, self.font_size).ceil(), cx.font_cache.line_height(font_id, self.font_size).ceil(),
); );
(size, line) (size, line)
@ -135,12 +135,12 @@ impl Element for Label {
&mut self, &mut self,
bounds: RectF, bounds: RectF,
line: &mut Self::LayoutState, line: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
line.paint( line.paint(
bounds.origin(), bounds.origin(),
RectF::new(vec2f(0., 0.), bounds.size()), RectF::new(vec2f(0., 0.), bounds.size()),
ctx, cx,
) )
} }
@ -160,12 +160,12 @@ impl Element for Label {
bounds: RectF, bounds: RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> Value { ) -> Value {
json!({ json!({
"type": "Label", "type": "Label",
"bounds": bounds.to_json(), "bounds": bounds.to_json(),
"font_family": ctx.font_cache.family_name(self.family_id).unwrap(), "font_family": cx.font_cache.family_name(self.family_id).unwrap(),
"font_size": self.font_size, "font_size": self.font_size,
"font_properties": self.font_properties.to_json(), "font_properties": self.font_properties.to_json(),
"text": &self.text, "text": &self.text,
@ -191,13 +191,13 @@ mod tests {
use super::*; use super::*;
#[crate::test(self)] #[crate::test(self)]
fn test_layout_label_with_highlights(app: &mut crate::MutableAppContext) { fn test_layout_label_with_highlights(cx: &mut crate::MutableAppContext) {
let menlo = app.font_cache().load_family(&["Menlo"]).unwrap(); let menlo = cx.font_cache().load_family(&["Menlo"]).unwrap();
let menlo_regular = app let menlo_regular = cx
.font_cache() .font_cache()
.select_font(menlo, &Properties::new()) .select_font(menlo, &Properties::new())
.unwrap(); .unwrap();
let menlo_bold = app let menlo_bold = cx
.font_cache() .font_cache()
.select_font(menlo, Properties::new().weight(Weight::BOLD)) .select_font(menlo, Properties::new().weight(Weight::BOLD))
.unwrap(); .unwrap();
@ -216,7 +216,7 @@ mod tests {
], ],
); );
let runs = label.compute_runs(app.font_cache().as_ref(), menlo_regular); let runs = label.compute_runs(cx.font_cache().as_ref(), menlo_regular);
assert_eq!( assert_eq!(
runs.as_slice(), runs.as_slice(),
&[ &[

View file

@ -35,20 +35,20 @@ impl Element for LineBox {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
match ctx match cx
.font_cache .font_cache
.select_font(self.family_id, &self.font_properties) .select_font(self.family_id, &self.font_properties)
{ {
Ok(font_id) => { Ok(font_id) => {
let line_height = ctx.font_cache.line_height(font_id, self.font_size); let line_height = cx.font_cache.line_height(font_id, self.font_size);
let character_height = ctx.font_cache.ascent(font_id, self.font_size) let character_height = cx.font_cache.ascent(font_id, self.font_size)
+ ctx.font_cache.descent(font_id, self.font_size); + cx.font_cache.descent(font_id, self.font_size);
let child_max = vec2f(constraint.max.x(), character_height); let child_max = vec2f(constraint.max.x(), character_height);
let child_size = self.child.layout( let child_size = self.child.layout(
SizeConstraint::new(constraint.min.min(child_max), child_max), SizeConstraint::new(constraint.min.min(child_max), child_max),
ctx, cx,
); );
let size = vec2f(child_size.x(), line_height); let size = vec2f(child_size.x(), line_height);
(size, (line_height - character_height) / 2.) (size, (line_height - character_height) / 2.)
@ -64,19 +64,19 @@ impl Element for LineBox {
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
self.child.after_layout(ctx); self.child.after_layout(cx);
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: pathfinder_geometry::rect::RectF, bounds: pathfinder_geometry::rect::RectF,
padding_top: &mut f32, padding_top: &mut f32,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
self.child self.child
.paint(bounds.origin() + vec2f(0., *padding_top), ctx); .paint(bounds.origin() + vec2f(0., *padding_top), cx);
} }
fn dispatch_event( fn dispatch_event(
@ -85,9 +85,9 @@ impl Element for LineBox {
_: pathfinder_geometry::rect::RectF, _: pathfinder_geometry::rect::RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
self.child.dispatch_event(event, ctx) self.child.dispatch_event(event, cx)
} }
fn debug( fn debug(
@ -95,14 +95,14 @@ impl Element for LineBox {
bounds: RectF, bounds: RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> serde_json::Value { ) -> serde_json::Value {
json!({ json!({
"bounds": bounds.to_json(), "bounds": bounds.to_json(),
"font_family": ctx.font_cache.family_name(self.family_id).unwrap(), "font_family": cx.font_cache.family_name(self.family_id).unwrap(),
"font_size": self.font_size, "font_size": self.font_size,
"font_properties": self.font_properties.to_json(), "font_properties": self.font_properties.to_json(),
"child": self.child.debug(ctx), "child": self.child.debug(cx),
}) })
} }
} }

View file

@ -18,13 +18,13 @@ pub struct MouseState {
} }
impl MouseEventHandler { impl MouseEventHandler {
pub fn new<Tag, F>(id: usize, ctx: &AppContext, render_child: F) -> Self pub fn new<Tag, F>(id: usize, cx: &AppContext, render_child: F) -> Self
where where
Tag: 'static, Tag: 'static,
F: FnOnce(MouseState) -> ElementBox, F: FnOnce(MouseState) -> ElementBox,
{ {
let state_handle = ctx.value::<Tag, _>(id); let state_handle = cx.value::<Tag, _>(id);
let state = state_handle.read(ctx, |state| *state); let state = state_handle.read(cx, |state| *state);
let child = render_child(state); let child = render_child(state);
Self { Self {
state: state_handle, state: state_handle,
@ -46,27 +46,27 @@ impl Element for MouseEventHandler {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
(self.child.layout(constraint, ctx), ()) (self.child.layout(constraint, cx), ())
} }
fn after_layout( fn after_layout(
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
self.child.after_layout(ctx); self.child.after_layout(cx);
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
self.child.paint(bounds.origin(), ctx); self.child.paint(bounds.origin(), cx);
} }
fn dispatch_event( fn dispatch_event(
@ -75,18 +75,18 @@ impl Element for MouseEventHandler {
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
let click_handler = self.click_handler.as_mut(); let click_handler = self.click_handler.as_mut();
let handled_in_child = self.child.dispatch_event(event, ctx); let handled_in_child = self.child.dispatch_event(event, cx);
self.state.update(ctx.app, |state| match event { self.state.update(cx.app, |state| match event {
Event::MouseMoved { position } => { Event::MouseMoved { position } => {
let mouse_in = bounds.contains_point(*position); let mouse_in = bounds.contains_point(*position);
if state.hovered != mouse_in { if state.hovered != mouse_in {
state.hovered = mouse_in; state.hovered = mouse_in;
ctx.notify(); cx.notify();
true true
} else { } else {
handled_in_child handled_in_child
@ -95,7 +95,7 @@ impl Element for MouseEventHandler {
Event::LeftMouseDown { position, .. } => { Event::LeftMouseDown { position, .. } => {
if !handled_in_child && bounds.contains_point(*position) { if !handled_in_child && bounds.contains_point(*position) {
state.clicked = true; state.clicked = true;
ctx.notify(); cx.notify();
true true
} else { } else {
handled_in_child handled_in_child
@ -104,10 +104,10 @@ impl Element for MouseEventHandler {
Event::LeftMouseUp { position, .. } => { Event::LeftMouseUp { position, .. } => {
if !handled_in_child && state.clicked { if !handled_in_child && state.clicked {
state.clicked = false; state.clicked = false;
ctx.notify(); cx.notify();
if let Some(handler) = click_handler { if let Some(handler) = click_handler {
if bounds.contains_point(*position) { if bounds.contains_point(*position) {
handler(ctx); handler(cx);
} }
} }
true true
@ -124,11 +124,11 @@ impl Element for MouseEventHandler {
_: RectF, _: RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> serde_json::Value { ) -> serde_json::Value {
json!({ json!({
"type": "MouseEventHandler", "type": "MouseEventHandler",
"child": self.child.debug(ctx), "child": self.child.debug(cx),
}) })
} }
} }

View file

@ -24,11 +24,11 @@ impl Element for Stack {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let mut size = constraint.min; let mut size = constraint.min;
for child in &mut self.children { for child in &mut self.children {
size = size.max(child.layout(constraint, ctx)); size = size.max(child.layout(constraint, cx));
} }
(size, ()) (size, ())
} }
@ -37,10 +37,10 @@ impl Element for Stack {
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
for child in &mut self.children { for child in &mut self.children {
child.after_layout(ctx); child.after_layout(cx);
} }
} }
@ -48,12 +48,12 @@ impl Element for Stack {
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
for child in &mut self.children { for child in &mut self.children {
ctx.scene.push_layer(None); cx.scene.push_layer(None);
child.paint(bounds.origin(), ctx); child.paint(bounds.origin(), cx);
ctx.scene.pop_layer(); cx.scene.pop_layer();
} }
} }
@ -63,10 +63,10 @@ impl Element for Stack {
_: RectF, _: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
for child in self.children.iter_mut().rev() { for child in self.children.iter_mut().rev() {
if child.dispatch_event(event, ctx) { if child.dispatch_event(event, cx) {
return true; return true;
} }
} }
@ -78,12 +78,12 @@ impl Element for Stack {
bounds: RectF, bounds: RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> json::Value { ) -> json::Value {
json!({ json!({
"type": "Stack", "type": "Stack",
"bounds": bounds.to_json(), "bounds": bounds.to_json(),
"children": self.children.iter().map(|child| child.debug(ctx)).collect::<Vec<json::Value>>() "children": self.children.iter().map(|child| child.debug(cx)).collect::<Vec<json::Value>>()
}) })
} }
} }

View file

@ -38,9 +38,9 @@ impl Element for Svg {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
match ctx.asset_cache.svg(&self.path) { match cx.asset_cache.svg(&self.path) {
Ok(tree) => { Ok(tree) => {
let size = if constraint.max.x().is_infinite() && constraint.max.y().is_infinite() { let size = if constraint.max.x().is_infinite() && constraint.max.y().is_infinite() {
let rect = from_usvg_rect(tree.svg_node().view_box.rect); let rect = from_usvg_rect(tree.svg_node().view_box.rect);
@ -69,9 +69,9 @@ impl Element for Svg {
fn after_layout(&mut self, _: Vector2F, _: &mut Self::LayoutState, _: &mut AfterLayoutContext) { fn after_layout(&mut self, _: Vector2F, _: &mut Self::LayoutState, _: &mut AfterLayoutContext) {
} }
fn paint(&mut self, bounds: RectF, svg: &mut Self::LayoutState, ctx: &mut PaintContext) { fn paint(&mut self, bounds: RectF, svg: &mut Self::LayoutState, cx: &mut PaintContext) {
if let Some(svg) = svg.clone() { if let Some(svg) = svg.clone() {
ctx.scene.push_icon(scene::Icon { cx.scene.push_icon(scene::Icon {
bounds, bounds,
svg, svg,
path: self.path.clone(), path: self.path.clone(),

View file

@ -72,7 +72,7 @@ where
delta: Vector2F, delta: Vector2F,
precise: bool, precise: bool,
scroll_max: f32, scroll_max: f32,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
if !precise { if !precise {
todo!("still need to handle non-precise scroll events from a mouse wheel"); todo!("still need to handle non-precise scroll events from a mouse wheel");
@ -80,7 +80,7 @@ where
let mut state = self.state.0.lock(); let mut state = self.state.0.lock();
state.scroll_top = (state.scroll_top - delta.y()).max(0.0).min(scroll_max); state.scroll_top = (state.scroll_top - delta.y()).max(0.0).min(scroll_max);
ctx.dispatch_action("uniform_list:scroll", state.scroll_top); cx.dispatch_action("uniform_list:scroll", state.scroll_top);
true true
} }
@ -119,7 +119,7 @@ where
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
if constraint.max.y().is_infinite() { if constraint.max.y().is_infinite() {
unimplemented!( unimplemented!(
@ -133,9 +133,9 @@ where
let mut scroll_max = 0.; let mut scroll_max = 0.;
let mut items = Vec::new(); let mut items = Vec::new();
(self.append_items)(0..1, &mut items, ctx.app); (self.append_items)(0..1, &mut items, cx.app);
if let Some(first_item) = items.first_mut() { if let Some(first_item) = items.first_mut() {
let mut item_size = first_item.layout(item_constraint, ctx); let mut item_size = first_item.layout(item_constraint, cx);
item_size.set_x(size.x()); item_size.set_x(size.x());
item_constraint.min = item_size; item_constraint.min = item_size;
item_constraint.max = item_size; item_constraint.max = item_size;
@ -155,9 +155,9 @@ where
self.item_count, self.item_count,
start + (size.y() / item_height).ceil() as usize + 1, start + (size.y() / item_height).ceil() as usize + 1,
); );
(self.append_items)(start..end, &mut items, ctx.app); (self.append_items)(start..end, &mut items, cx.app);
for item in &mut items { for item in &mut items {
item.layout(item_constraint, ctx); item.layout(item_constraint, cx);
} }
} }
@ -175,10 +175,10 @@ where
&mut self, &mut self,
_: Vector2F, _: Vector2F,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
for item in &mut layout.items { for item in &mut layout.items {
item.after_layout(ctx); item.after_layout(cx);
} }
} }
@ -186,19 +186,19 @@ where
&mut self, &mut self,
bounds: RectF, bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
ctx.scene.push_layer(Some(bounds)); cx.scene.push_layer(Some(bounds));
let mut item_origin = let mut item_origin =
bounds.origin() - vec2f(0.0, self.state.scroll_top() % layout.item_height); bounds.origin() - vec2f(0.0, self.state.scroll_top() % layout.item_height);
for item in &mut layout.items { for item in &mut layout.items {
item.paint(item_origin, ctx); item.paint(item_origin, cx);
item_origin += vec2f(0.0, layout.item_height); item_origin += vec2f(0.0, layout.item_height);
} }
ctx.scene.pop_layer(); cx.scene.pop_layer();
} }
fn dispatch_event( fn dispatch_event(
@ -207,11 +207,11 @@ where
bounds: RectF, bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
let mut handled = false; let mut handled = false;
for item in &mut layout.items { for item in &mut layout.items {
handled = item.dispatch_event(event, ctx) || handled; handled = item.dispatch_event(event, cx) || handled;
} }
match event { match event {
@ -221,7 +221,7 @@ where
precise, precise,
} => { } => {
if bounds.contains_point(*position) { if bounds.contains_point(*position) {
if self.scroll(*position, *delta, *precise, layout.scroll_max, ctx) { if self.scroll(*position, *delta, *precise, layout.scroll_max, cx) {
handled = true; handled = true;
} }
} }
@ -237,14 +237,14 @@ where
bounds: RectF, bounds: RectF,
layout: &Self::LayoutState, layout: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &crate::DebugContext, cx: &crate::DebugContext,
) -> json::Value { ) -> json::Value {
json!({ json!({
"type": "UniformList", "type": "UniformList",
"bounds": bounds.to_json(), "bounds": bounds.to_json(),
"scroll_max": layout.scroll_max, "scroll_max": layout.scroll_max,
"item_height": layout.item_height, "item_height": layout.item_height,
"items": layout.items.iter().map(|item| item.debug(ctx)).collect::<Vec<json::Value>>() "items": layout.items.iter().map(|item| item.debug(cx)).collect::<Vec<json::Value>>()
}) })
} }

View file

@ -98,12 +98,12 @@ impl Matcher {
&mut self, &mut self,
keystroke: Keystroke, keystroke: Keystroke,
view_id: usize, view_id: usize,
ctx: &Context, cx: &Context,
) -> MatchResult { ) -> MatchResult {
let pending = self.pending.entry(view_id).or_default(); let pending = self.pending.entry(view_id).or_default();
if let Some(pending_ctx) = pending.context.as_ref() { if let Some(pending_ctx) = pending.context.as_ref() {
if pending_ctx != ctx { if pending_ctx != cx {
pending.keystrokes.clear(); pending.keystrokes.clear();
} }
} }
@ -113,11 +113,7 @@ impl Matcher {
let mut retain_pending = false; let mut retain_pending = false;
for binding in self.keymap.0.iter().rev() { for binding in self.keymap.0.iter().rev() {
if binding.keystrokes.starts_with(&pending.keystrokes) if binding.keystrokes.starts_with(&pending.keystrokes)
&& binding && binding.context.as_ref().map(|c| c.eval(cx)).unwrap_or(true)
.context
.as_ref()
.map(|c| c.eval(ctx))
.unwrap_or(true)
{ {
if binding.keystrokes.len() == pending.keystrokes.len() { if binding.keystrokes.len() == pending.keystrokes.len() {
self.pending.remove(&view_id); self.pending.remove(&view_id);
@ -127,7 +123,7 @@ impl Matcher {
}; };
} else { } else {
retain_pending = true; retain_pending = true;
pending.context = Some(ctx.clone()); pending.context = Some(cx.clone());
} }
} }
} }
@ -312,22 +308,20 @@ impl ContextPredicate {
} }
} }
fn eval(&self, ctx: &Context) -> bool { fn eval(&self, cx: &Context) -> bool {
match self { match self {
Self::Identifier(name) => ctx.set.contains(name.as_str()), Self::Identifier(name) => cx.set.contains(name.as_str()),
Self::Equal(left, right) => ctx Self::Equal(left, right) => cx
.map .map
.get(left) .get(left)
.map(|value| value == right) .map(|value| value == right)
.unwrap_or(false), .unwrap_or(false),
Self::NotEqual(left, right) => ctx Self::NotEqual(left, right) => {
.map cx.map.get(left).map(|value| value != right).unwrap_or(true)
.get(left) }
.map(|value| value != right) Self::Not(pred) => !pred.eval(cx),
.unwrap_or(true), Self::And(left, right) => left.eval(cx) && right.eval(cx),
Self::Not(pred) => !pred.eval(ctx), Self::Or(left, right) => left.eval(cx) || right.eval(cx),
Self::And(left, right) => left.eval(ctx) && right.eval(ctx),
Self::Or(left, right) => left.eval(ctx) || right.eval(ctx),
} }
} }
} }
@ -488,10 +482,10 @@ mod tests {
&mut self, &mut self,
keystroke: &str, keystroke: &str,
view_id: usize, view_id: usize,
ctx: &Context, cx: &Context,
) -> Option<(String, Option<A>)> { ) -> Option<(String, Option<A>)> {
if let MatchResult::Action { name, arg } = if let MatchResult::Action { name, arg } =
self.push_keystroke(Keystroke::parse(keystroke).unwrap(), view_id, ctx) self.push_keystroke(Keystroke::parse(keystroke).unwrap(), view_id, cx)
{ {
Some((name, arg.and_then(|arg| arg.downcast_ref::<A>().cloned()))) Some((name, arg.and_then(|arg| arg.downcast_ref::<A>().cloned())))
} else { } else {

View file

@ -145,7 +145,7 @@ impl FontSystemState {
// Make room for subpixel variants. // Make room for subpixel variants.
let bounds = RectI::new(bounds.origin(), bounds.size() + vec2i(1, 1)); let bounds = RectI::new(bounds.origin(), bounds.size() + vec2i(1, 1));
let mut pixels = vec![0; bounds.width() as usize * bounds.height() as usize]; let mut pixels = vec![0; bounds.width() as usize * bounds.height() as usize];
let ctx = CGContext::create_bitmap_context( let cx = CGContext::create_bitmap_context(
Some(pixels.as_mut_ptr() as *mut _), Some(pixels.as_mut_ptr() as *mut _),
bounds.width() as usize, bounds.width() as usize,
bounds.height() as usize, bounds.height() as usize,
@ -157,9 +157,9 @@ impl FontSystemState {
// Move the origin to bottom left and account for scaling, this // Move the origin to bottom left and account for scaling, this
// makes drawing text consistent with the font-kit's raster_bounds. // makes drawing text consistent with the font-kit's raster_bounds.
ctx.translate(0.0, bounds.height() as CGFloat); cx.translate(0.0, bounds.height() as CGFloat);
let transform = scale.translate(-bounds.origin().to_f32()); let transform = scale.translate(-bounds.origin().to_f32());
ctx.set_text_matrix(&CGAffineTransform { cx.set_text_matrix(&CGAffineTransform {
a: transform.matrix.m11() as CGFloat, a: transform.matrix.m11() as CGFloat,
b: -transform.matrix.m21() as CGFloat, b: -transform.matrix.m21() as CGFloat,
c: -transform.matrix.m12() as CGFloat, c: -transform.matrix.m12() as CGFloat,
@ -168,9 +168,9 @@ impl FontSystemState {
ty: -transform.vector.y() as CGFloat, ty: -transform.vector.y() as CGFloat,
}); });
ctx.set_font(&font.native_font().copy_to_CGFont()); cx.set_font(&font.native_font().copy_to_CGFont());
ctx.set_font_size(font_size as CGFloat); cx.set_font_size(font_size as CGFloat);
ctx.show_glyphs_at_positions( cx.show_glyphs_at_positions(
&[glyph_id as CGGlyph], &[glyph_id as CGGlyph],
&[CGPoint::new( &[CGPoint::new(
(subpixel_shift.x() / scale_factor) as CGFloat, (subpixel_shift.x() / scale_factor) as CGFloat,

View file

@ -31,11 +31,11 @@ impl Presenter {
font_cache: Arc<FontCache>, font_cache: Arc<FontCache>,
text_layout_cache: TextLayoutCache, text_layout_cache: TextLayoutCache,
asset_cache: Arc<AssetCache>, asset_cache: Arc<AssetCache>,
app: &MutableAppContext, cx: &MutableAppContext,
) -> Self { ) -> Self {
Self { Self {
window_id, window_id,
rendered_views: app.render_views(window_id), rendered_views: cx.render_views(window_id),
parents: HashMap::new(), parents: HashMap::new(),
font_cache, font_cache,
text_layout_cache, text_layout_cache,
@ -55,7 +55,7 @@ impl Presenter {
path path
} }
pub fn invalidate(&mut self, mut invalidation: WindowInvalidation, app: &AppContext) { pub fn invalidate(&mut self, mut invalidation: WindowInvalidation, cx: &AppContext) {
for view_id in invalidation.removed { for view_id in invalidation.removed {
invalidation.updated.remove(&view_id); invalidation.updated.remove(&view_id);
self.rendered_views.remove(&view_id); self.rendered_views.remove(&view_id);
@ -63,7 +63,7 @@ impl Presenter {
} }
for view_id in invalidation.updated { for view_id in invalidation.updated {
self.rendered_views self.rendered_views
.insert(view_id, app.render_view(self.window_id, view_id).unwrap()); .insert(view_id, cx.render_view(self.window_id, view_id).unwrap());
} }
} }
@ -71,25 +71,25 @@ impl Presenter {
&mut self, &mut self,
window_size: Vector2F, window_size: Vector2F,
scale_factor: f32, scale_factor: f32,
app: &mut MutableAppContext, cx: &mut MutableAppContext,
) -> Scene { ) -> Scene {
let mut scene = Scene::new(scale_factor); let mut scene = Scene::new(scale_factor);
if let Some(root_view_id) = app.root_view_id(self.window_id) { if let Some(root_view_id) = cx.root_view_id(self.window_id) {
self.layout(window_size, app.as_ref()); self.layout(window_size, cx.as_ref());
self.after_layout(app); self.after_layout(cx);
let mut ctx = PaintContext { let mut paint_cx = PaintContext {
scene: &mut scene, scene: &mut scene,
font_cache: &self.font_cache, font_cache: &self.font_cache,
text_layout_cache: &self.text_layout_cache, text_layout_cache: &self.text_layout_cache,
rendered_views: &mut self.rendered_views, rendered_views: &mut self.rendered_views,
app: app.as_ref(), app: cx.as_ref(),
}; };
ctx.paint(root_view_id, Vector2F::zero()); paint_cx.paint(root_view_id, Vector2F::zero());
self.text_layout_cache.finish_frame(); self.text_layout_cache.finish_frame();
if let Some(event) = self.last_mouse_moved_event.clone() { if let Some(event) = self.last_mouse_moved_event.clone() {
self.dispatch_event(event, app) self.dispatch_event(event, cx)
} }
} else { } else {
log::error!("could not find root_view_id for window {}", self.window_id); log::error!("could not find root_view_id for window {}", self.window_id);
@ -98,8 +98,8 @@ impl Presenter {
scene scene
} }
fn layout(&mut self, size: Vector2F, app: &AppContext) { fn layout(&mut self, size: Vector2F, cx: &AppContext) {
if let Some(root_view_id) = app.root_view_id(self.window_id) { if let Some(root_view_id) = cx.root_view_id(self.window_id) {
let mut layout_ctx = LayoutContext { let mut layout_ctx = LayoutContext {
rendered_views: &mut self.rendered_views, rendered_views: &mut self.rendered_views,
parents: &mut self.parents, parents: &mut self.parents,
@ -107,49 +107,49 @@ impl Presenter {
text_layout_cache: &self.text_layout_cache, text_layout_cache: &self.text_layout_cache,
asset_cache: &self.asset_cache, asset_cache: &self.asset_cache,
view_stack: Vec::new(), view_stack: Vec::new(),
app, app: cx,
}; };
layout_ctx.layout(root_view_id, SizeConstraint::strict(size)); layout_ctx.layout(root_view_id, SizeConstraint::strict(size));
} }
} }
fn after_layout(&mut self, app: &mut MutableAppContext) { fn after_layout(&mut self, cx: &mut MutableAppContext) {
if let Some(root_view_id) = app.root_view_id(self.window_id) { if let Some(root_view_id) = cx.root_view_id(self.window_id) {
let mut ctx = AfterLayoutContext { let mut layout_cx = AfterLayoutContext {
rendered_views: &mut self.rendered_views, rendered_views: &mut self.rendered_views,
font_cache: &self.font_cache, font_cache: &self.font_cache,
text_layout_cache: &self.text_layout_cache, text_layout_cache: &self.text_layout_cache,
app, app: cx,
}; };
ctx.after_layout(root_view_id); layout_cx.after_layout(root_view_id);
} }
} }
pub fn dispatch_event(&mut self, event: Event, app: &mut MutableAppContext) { pub fn dispatch_event(&mut self, event: Event, cx: &mut MutableAppContext) {
if let Some(root_view_id) = app.root_view_id(self.window_id) { if let Some(root_view_id) = cx.root_view_id(self.window_id) {
if matches!(event, Event::MouseMoved { .. }) { if matches!(event, Event::MouseMoved { .. }) {
self.last_mouse_moved_event = Some(event.clone()); self.last_mouse_moved_event = Some(event.clone());
} }
let mut ctx = EventContext { let mut event_cx = EventContext {
rendered_views: &mut self.rendered_views, rendered_views: &mut self.rendered_views,
actions: Default::default(), actions: Default::default(),
font_cache: &self.font_cache, font_cache: &self.font_cache,
text_layout_cache: &self.text_layout_cache, text_layout_cache: &self.text_layout_cache,
view_stack: Default::default(), view_stack: Default::default(),
invalidated_views: Default::default(), invalidated_views: Default::default(),
app: app.as_ref(), app: cx.as_ref(),
}; };
ctx.dispatch_event(root_view_id, &event); event_cx.dispatch_event(root_view_id, &event);
let invalidated_views = ctx.invalidated_views; let invalidated_views = event_cx.invalidated_views;
let actions = ctx.actions; let actions = event_cx.actions;
for view_id in invalidated_views { for view_id in invalidated_views {
app.notify_view(self.window_id, view_id); cx.notify_view(self.window_id, view_id);
} }
for action in actions { for action in actions {
app.dispatch_action_any( cx.dispatch_action_any(
self.window_id, self.window_id,
&action.path, &action.path,
action.name, action.name,
@ -159,14 +159,14 @@ impl Presenter {
} }
} }
pub fn debug_elements(&self, ctx: &AppContext) -> Option<json::Value> { pub fn debug_elements(&self, cx: &AppContext) -> Option<json::Value> {
ctx.root_view_id(self.window_id) cx.root_view_id(self.window_id)
.and_then(|root_view_id| self.rendered_views.get(&root_view_id)) .and_then(|root_view_id| self.rendered_views.get(&root_view_id))
.map(|root_element| { .map(|root_element| {
root_element.debug(&DebugContext { root_element.debug(&DebugContext {
rendered_views: &self.rendered_views, rendered_views: &self.rendered_views,
font_cache: &self.font_cache, font_cache: &self.font_cache,
app: ctx, app: cx,
}) })
}) })
} }
@ -380,9 +380,9 @@ impl Element for ChildView {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
ctx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let size = ctx.layout(self.view_id, constraint); let size = cx.layout(self.view_id, constraint);
(size, ()) (size, ())
} }
@ -390,18 +390,18 @@ impl Element for ChildView {
&mut self, &mut self,
_: Vector2F, _: Vector2F,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut AfterLayoutContext, cx: &mut AfterLayoutContext,
) { ) {
ctx.after_layout(self.view_id); cx.after_layout(self.view_id);
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: pathfinder_geometry::rect::RectF, bounds: pathfinder_geometry::rect::RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
ctx.paint(self.view_id, bounds.origin()); cx.paint(self.view_id, bounds.origin());
} }
fn dispatch_event( fn dispatch_event(
@ -410,9 +410,9 @@ impl Element for ChildView {
_: pathfinder_geometry::rect::RectF, _: pathfinder_geometry::rect::RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
ctx.dispatch_event(self.view_id, event) cx.dispatch_event(self.view_id, event)
} }
fn debug( fn debug(
@ -420,14 +420,14 @@ impl Element for ChildView {
bounds: pathfinder_geometry::rect::RectF, bounds: pathfinder_geometry::rect::RectF,
_: &Self::LayoutState, _: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, cx: &DebugContext,
) -> serde_json::Value { ) -> serde_json::Value {
json!({ json!({
"type": "ChildView", "type": "ChildView",
"view_id": self.view_id, "view_id": self.view_id,
"bounds": bounds.to_json(), "bounds": bounds.to_json(),
"child": if let Some(view) = ctx.rendered_views.get(&self.view_id) { "child": if let Some(view) = cx.rendered_views.get(&self.view_id) {
view.debug(ctx) view.debug(cx)
} else { } else {
json!(null) json!(null)
} }
@ -441,9 +441,9 @@ mod tests {
// fn test_responder_chain() { // fn test_responder_chain() {
// let settings = settings_rx(None); // let settings = settings_rx(None);
// let mut app = App::new().unwrap(); // let mut app = App::new().unwrap();
// let workspace = app.add_model(|ctx| Workspace::new(Vec::new(), ctx)); // let workspace = app.add_model(|cx| Workspace::new(Vec::new(), cx));
// let (window_id, workspace_view) = // let (window_id, workspace_view) =
// app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx)); // app.add_window(|cx| WorkspaceView::new(workspace.clone(), settings, cx));
// let invalidations = Rc::new(RefCell::new(Vec::new())); // let invalidations = Rc::new(RefCell::new(Vec::new()));
// let invalidations_ = invalidations.clone(); // let invalidations_ = invalidations.clone();
@ -451,8 +451,8 @@ mod tests {
// invalidations_.borrow_mut().push(invalidation) // invalidations_.borrow_mut().push(invalidation)
// }); // });
// let active_pane_id = workspace_view.update(&mut app, |view, ctx| { // let active_pane_id = workspace_view.update(&mut app, |view, cx| {
// ctx.focus(view.active_pane()); // cx.focus(view.active_pane());
// view.active_pane().id() // view.active_pane().id()
// }); // });
@ -468,7 +468,7 @@ mod tests {
// } // }
// assert_eq!( // assert_eq!(
// presenter.responder_chain(app.ctx()).unwrap(), // presenter.responder_chain(app.cx()).unwrap(),
// vec![workspace_view.id(), active_pane_id] // vec![workspace_view.id(), active_pane_id]
// ); // );
// }); // });

View file

@ -200,7 +200,7 @@ impl Line {
} }
} }
pub fn paint(&self, origin: Vector2F, visible_bounds: RectF, ctx: &mut PaintContext) { pub fn paint(&self, origin: Vector2F, visible_bounds: RectF, cx: &mut PaintContext) {
let padding_top = (visible_bounds.height() - self.layout.ascent - self.layout.descent) / 2.; let padding_top = (visible_bounds.height() - self.layout.ascent - self.layout.descent) / 2.;
let baseline_origin = vec2f(0., padding_top + self.layout.ascent); let baseline_origin = vec2f(0., padding_top + self.layout.ascent);
@ -209,7 +209,7 @@ impl Line {
let mut color = ColorU::black(); let mut color = ColorU::black();
for run in &self.layout.runs { for run in &self.layout.runs {
let max_glyph_width = ctx let max_glyph_width = cx
.font_cache .font_cache
.bounding_box(run.font_id, self.layout.font_size) .bounding_box(run.font_id, self.layout.font_size)
.x(); .x();
@ -234,7 +234,7 @@ impl Line {
} }
} }
ctx.scene.push_glyph(scene::Glyph { cx.scene.push_glyph(scene::Glyph {
font_id: run.font_id, font_id: run.font_id,
font_size: self.layout.font_size, font_size: self.layout.font_size,
id: glyph.id, id: glyph.id,

View file

@ -34,8 +34,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
fn #outer_fn_name() { fn #outer_fn_name() {
#inner_fn #inner_fn
#namespace::App::test_async((), move |ctx| async { #namespace::App::test_async((), move |cx| async {
#inner_fn_name(ctx).await; #inner_fn_name(cx).await;
}); });
} }
} }
@ -45,8 +45,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
fn #outer_fn_name() { fn #outer_fn_name() {
#inner_fn #inner_fn
#namespace::App::test((), |ctx| { #namespace::App::test((), |cx| {
#inner_fn_name(ctx); #inner_fn_name(cx);
}); });
} }
} }