mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-24 17:28:40 +00:00
Rename context parameters to cx
in gpui
This commit is contained in:
parent
173f99748d
commit
6ef447866a
20 changed files with 643 additions and 733 deletions
|
@ -10,9 +10,9 @@ use simplelog::SimpleLogger;
|
|||
fn main() {
|
||||
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
|
||||
|
||||
gpui::App::new(()).unwrap().run(|ctx| {
|
||||
ctx.platform().activate(true);
|
||||
ctx.add_window(|_| TextView);
|
||||
gpui::App::new(()).unwrap().run(|cx| {
|
||||
cx.platform().activate(true);
|
||||
cx.add_window(|_| TextView);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -58,15 +58,15 @@ impl gpui::Element for TextElement {
|
|||
&mut self,
|
||||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut gpui::PaintContext,
|
||||
cx: &mut gpui::PaintContext,
|
||||
) -> Self::PaintState {
|
||||
let font_size = 12.;
|
||||
let family = ctx.font_cache.load_family(&["SF Pro Display"]).unwrap();
|
||||
let normal = ctx
|
||||
let family = cx.font_cache.load_family(&["SF Pro Display"]).unwrap();
|
||||
let normal = cx
|
||||
.font_cache
|
||||
.select_font(family, &Default::default())
|
||||
.unwrap();
|
||||
let bold = ctx
|
||||
let bold = cx
|
||||
.font_cache
|
||||
.select_font(
|
||||
family,
|
||||
|
@ -78,7 +78,7 @@ impl gpui::Element for TextElement {
|
|||
.unwrap();
|
||||
|
||||
let text = "Hello world!";
|
||||
let line = ctx.text_layout_cache.layout_str(
|
||||
let line = cx.text_layout_cache.layout_str(
|
||||
text,
|
||||
font_size,
|
||||
&[
|
||||
|
@ -90,12 +90,12 @@ impl gpui::Element for TextElement {
|
|||
],
|
||||
);
|
||||
|
||||
ctx.scene.push_quad(Quad {
|
||||
cx.scene.push_quad(Quad {
|
||||
bounds: bounds,
|
||||
background: Some(ColorU::white()),
|
||||
..Default::default()
|
||||
});
|
||||
line.paint(bounds.origin(), bounds, ctx);
|
||||
line.paint(bounds.origin(), bounds, cx);
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
|
846
gpui/src/app.rs
846
gpui/src/app.rs
File diff suppressed because it is too large
Load diff
|
@ -38,11 +38,11 @@ use replace_with::replace_with_or_abort;
|
|||
use std::{any::Any, borrow::Cow};
|
||||
|
||||
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 paint(&mut self, origin: Vector2F, ctx: &mut PaintContext);
|
||||
fn dispatch_event(&mut self, event: &Event, ctx: &mut EventContext) -> bool;
|
||||
fn debug(&self, ctx: &DebugContext) -> serde_json::Value;
|
||||
fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext);
|
||||
fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool;
|
||||
fn debug(&self, cx: &DebugContext) -> serde_json::Value;
|
||||
|
||||
fn size(&self) -> Vector2F;
|
||||
fn metadata(&self) -> Option<&dyn Any>;
|
||||
|
@ -55,21 +55,21 @@ pub trait Element {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState);
|
||||
|
||||
fn after_layout(
|
||||
&mut self,
|
||||
size: Vector2F,
|
||||
layout: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
);
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: RectF,
|
||||
layout: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState;
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -78,7 +78,7 @@ pub trait Element {
|
|||
bounds: RectF,
|
||||
layout: &mut Self::LayoutState,
|
||||
paint: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool;
|
||||
|
||||
fn metadata(&self) -> Option<&dyn Any> {
|
||||
|
@ -90,7 +90,7 @@ pub trait Element {
|
|||
bounds: RectF,
|
||||
layout: &Self::LayoutState,
|
||||
paint: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> serde_json::Value;
|
||||
|
||||
fn boxed(self) -> ElementBox
|
||||
|
@ -138,13 +138,13 @@ pub struct ElementBox {
|
|||
}
|
||||
|
||||
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;
|
||||
replace_with_or_abort(self, |me| match me {
|
||||
Lifecycle::Init { mut element }
|
||||
| Lifecycle::PostLayout { 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.y().is_finite());
|
||||
|
||||
|
@ -160,7 +160,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
|||
result.unwrap()
|
||||
}
|
||||
|
||||
fn after_layout(&mut self, ctx: &mut AfterLayoutContext) {
|
||||
fn after_layout(&mut self, cx: &mut AfterLayoutContext) {
|
||||
if let Lifecycle::PostLayout {
|
||||
element,
|
||||
size,
|
||||
|
@ -168,13 +168,13 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
|||
..
|
||||
} = self
|
||||
{
|
||||
element.after_layout(*size, layout, ctx);
|
||||
element.after_layout(*size, layout, cx);
|
||||
} else {
|
||||
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| {
|
||||
if let Lifecycle::PostLayout {
|
||||
mut element,
|
||||
|
@ -184,7 +184,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
|||
} = me
|
||||
{
|
||||
let bounds = RectF::new(origin, size);
|
||||
let paint = element.paint(bounds, &mut layout, ctx);
|
||||
let paint = element.paint(bounds, &mut layout, cx);
|
||||
Lifecycle::PostPaint {
|
||||
element,
|
||||
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 {
|
||||
element,
|
||||
bounds,
|
||||
|
@ -207,7 +207,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
|||
..
|
||||
} = self
|
||||
{
|
||||
element.dispatch_event(event, *bounds, layout, paint, ctx)
|
||||
element.dispatch_event(event, *bounds, layout, paint, cx)
|
||||
} else {
|
||||
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 {
|
||||
Lifecycle::PostPaint {
|
||||
element,
|
||||
|
@ -238,7 +238,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
|||
layout,
|
||||
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 {
|
||||
let mut new_map: crate::json::Map<String, serde_json::Value> =
|
||||
Default::default();
|
||||
|
@ -258,20 +258,20 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
|||
}
|
||||
|
||||
impl ElementBox {
|
||||
pub fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F {
|
||||
self.element.layout(constraint, ctx)
|
||||
pub fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F {
|
||||
self.element.layout(constraint, cx)
|
||||
}
|
||||
|
||||
pub fn after_layout(&mut self, ctx: &mut AfterLayoutContext) {
|
||||
self.element.after_layout(ctx);
|
||||
pub fn after_layout(&mut self, cx: &mut AfterLayoutContext) {
|
||||
self.element.after_layout(cx);
|
||||
}
|
||||
|
||||
pub fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext) {
|
||||
self.element.paint(origin, ctx);
|
||||
pub fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) {
|
||||
self.element.paint(origin, cx);
|
||||
}
|
||||
|
||||
pub fn dispatch_event(&mut self, event: &Event, ctx: &mut EventContext) -> bool {
|
||||
self.element.dispatch_event(event, ctx)
|
||||
pub fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool {
|
||||
self.element.dispatch_event(event, cx)
|
||||
}
|
||||
|
||||
pub fn size(&self) -> Vector2F {
|
||||
|
@ -282,8 +282,8 @@ impl ElementBox {
|
|||
self.element.metadata()
|
||||
}
|
||||
|
||||
pub fn debug(&self, ctx: &DebugContext) -> json::Value {
|
||||
let mut value = self.element.debug(ctx);
|
||||
pub fn debug(&self, cx: &DebugContext) -> json::Value {
|
||||
let mut value = self.element.debug(cx);
|
||||
|
||||
if let Some(name) = &self.name {
|
||||
if let json::Value::Object(map) = &mut value {
|
||||
|
|
|
@ -37,11 +37,11 @@ impl Element for Align {
|
|||
fn layout(
|
||||
&mut self,
|
||||
mut constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let mut size = constraint.max;
|
||||
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() {
|
||||
size.set_x(child_size.x());
|
||||
}
|
||||
|
@ -55,16 +55,16 @@ impl Element for Align {
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
self.child.after_layout(ctx);
|
||||
self.child.after_layout(cx);
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: pathfinder_geometry::rect::RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
let my_center = bounds.size() / 2.;
|
||||
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;
|
||||
|
||||
self.child
|
||||
.paint(bounds.origin() - (child_target - my_target), ctx);
|
||||
.paint(bounds.origin() - (child_target - my_target), cx);
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -82,9 +82,9 @@ impl Element for Align {
|
|||
_: pathfinder_geometry::rect::RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
self.child.dispatch_event(event, ctx)
|
||||
self.child.dispatch_event(event, cx)
|
||||
}
|
||||
|
||||
fn debug(
|
||||
|
@ -92,13 +92,13 @@ impl Element for Align {
|
|||
bounds: pathfinder_geometry::rect::RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> json::Value {
|
||||
json!({
|
||||
"type": "Align",
|
||||
"bounds": bounds.to_json(),
|
||||
"alignment": self.alignment.to_json(),
|
||||
"child": self.child.debug(ctx),
|
||||
"child": self.child.debug(cx),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,9 +51,9 @@ where
|
|||
&mut self,
|
||||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
self.0(bounds, ctx)
|
||||
self.0(bounds, cx)
|
||||
}
|
||||
|
||||
fn after_layout(
|
||||
|
|
|
@ -58,12 +58,12 @@ impl Element for ConstrainedBox {
|
|||
fn layout(
|
||||
&mut self,
|
||||
mut constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
constraint.min = constraint.min.max(self.constraint.min);
|
||||
constraint.max = constraint.max.min(self.constraint.max);
|
||||
constraint.max = constraint.max.max(constraint.min);
|
||||
let size = self.child.layout(constraint, ctx);
|
||||
let size = self.child.layout(constraint, cx);
|
||||
(size, ())
|
||||
}
|
||||
|
||||
|
@ -71,18 +71,18 @@ impl Element for ConstrainedBox {
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
self.child.after_layout(ctx);
|
||||
self.child.after_layout(cx);
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
self.child.paint(bounds.origin(), ctx);
|
||||
self.child.paint(bounds.origin(), cx);
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -91,9 +91,9 @@ impl Element for ConstrainedBox {
|
|||
_: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
self.child.dispatch_event(event, ctx)
|
||||
self.child.dispatch_event(event, cx)
|
||||
}
|
||||
|
||||
fn debug(
|
||||
|
@ -101,8 +101,8 @@ impl Element for ConstrainedBox {
|
|||
_: RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> 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)})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -141,14 +141,14 @@ impl Element for Container {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let size_buffer = self.margin_size() + self.padding_size() + self.border_size();
|
||||
let child_constraint = SizeConstraint {
|
||||
min: (constraint.min - 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, ())
|
||||
}
|
||||
|
||||
|
@ -156,16 +156,16 @@ impl Element for Container {
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
self.child.after_layout(ctx);
|
||||
self.child.after_layout(cx);
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
let quad_bounds = RectF::from_points(
|
||||
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() {
|
||||
ctx.scene.push_shadow(scene::Shadow {
|
||||
cx.scene.push_shadow(scene::Shadow {
|
||||
bounds: quad_bounds + shadow.offset,
|
||||
corner_radius: self.corner_radius,
|
||||
sigma: shadow.blur,
|
||||
color: shadow.color,
|
||||
});
|
||||
}
|
||||
ctx.scene.push_quad(Quad {
|
||||
cx.scene.push_quad(Quad {
|
||||
bounds: quad_bounds,
|
||||
background: self.background_color,
|
||||
border: self.border,
|
||||
|
@ -190,7 +190,7 @@ impl Element for Container {
|
|||
let child_origin = quad_bounds.origin()
|
||||
+ vec2f(self.padding.left, self.padding.top)
|
||||
+ vec2f(self.border.left_width(), self.border.top_width());
|
||||
self.child.paint(child_origin, ctx);
|
||||
self.child.paint(child_origin, cx);
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -199,9 +199,9 @@ impl Element for Container {
|
|||
_: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
self.child.dispatch_event(event, ctx)
|
||||
self.child.dispatch_event(event, cx)
|
||||
}
|
||||
|
||||
fn debug(
|
||||
|
@ -209,7 +209,7 @@ impl Element for Container {
|
|||
bounds: RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &crate::DebugContext,
|
||||
cx: &crate::DebugContext,
|
||||
) -> serde_json::Value {
|
||||
json!({
|
||||
"type": "Container",
|
||||
|
@ -222,7 +222,7 @@ impl Element for Container {
|
|||
"corner_radius": self.corner_radius,
|
||||
"shadow": self.shadow.to_json(),
|
||||
},
|
||||
"child": self.child.debug(ctx),
|
||||
"child": self.child.debug(cx),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,9 +35,9 @@ impl Element for EventHandler {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let size = self.child.layout(constraint, ctx);
|
||||
let size = self.child.layout(constraint, cx);
|
||||
(size, ())
|
||||
}
|
||||
|
||||
|
@ -45,18 +45,18 @@ impl Element for EventHandler {
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
self.child.after_layout(ctx);
|
||||
self.child.after_layout(cx);
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
self.child.paint(bounds.origin(), ctx);
|
||||
self.child.paint(bounds.origin(), cx);
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -65,16 +65,16 @@ impl Element for EventHandler {
|
|||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
if self.child.dispatch_event(event, ctx) {
|
||||
if self.child.dispatch_event(event, cx) {
|
||||
true
|
||||
} else {
|
||||
match event {
|
||||
Event::LeftMouseDown { position, .. } => {
|
||||
if let Some(callback) = self.mouse_down.as_mut() {
|
||||
if bounds.contains_point(*position) {
|
||||
return callback(ctx);
|
||||
return callback(cx);
|
||||
}
|
||||
}
|
||||
false
|
||||
|
@ -89,11 +89,11 @@ impl Element for EventHandler {
|
|||
_: RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> serde_json::Value {
|
||||
json!({
|
||||
"type": "EventHandler",
|
||||
"child": self.child.debug(ctx),
|
||||
"child": self.child.debug(cx),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ impl Element for Flex {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let mut total_flex = 0.0;
|
||||
let mut fixed_space = 0.0;
|
||||
|
@ -74,7 +74,7 @@ impl Element for Flex {
|
|||
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);
|
||||
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),
|
||||
),
|
||||
};
|
||||
let child_size = child.layout(child_constraint, ctx);
|
||||
let child_size = child.layout(child_constraint, cx);
|
||||
remaining_space -= child_size.along(self.axis);
|
||||
remaining_flex -= flex;
|
||||
cross_axis_max = cross_axis_max.max(child_size.along(cross_axis));
|
||||
|
@ -138,10 +138,10 @@ impl Element for Flex {
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
for child in &mut self.children {
|
||||
child.after_layout(ctx);
|
||||
child.after_layout(cx);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -149,11 +149,11 @@ impl Element for Flex {
|
|||
&mut self,
|
||||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
let mut child_origin = bounds.origin();
|
||||
for child in &mut self.children {
|
||||
child.paint(child_origin, ctx);
|
||||
child.paint(child_origin, cx);
|
||||
match self.axis {
|
||||
Axis::Horizontal => child_origin += vec2f(child.size().x(), 0.0),
|
||||
Axis::Vertical => child_origin += vec2f(0.0, child.size().y()),
|
||||
|
@ -167,11 +167,11 @@ impl Element for Flex {
|
|||
_: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
let mut handled = false;
|
||||
for child in &mut self.children {
|
||||
handled = child.dispatch_event(event, ctx) || handled;
|
||||
handled = child.dispatch_event(event, cx) || handled;
|
||||
}
|
||||
handled
|
||||
}
|
||||
|
@ -181,13 +181,13 @@ impl Element for Flex {
|
|||
bounds: RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> json::Value {
|
||||
json!({
|
||||
"type": "Flex",
|
||||
"bounds": bounds.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(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let size = self.child.layout(constraint, ctx);
|
||||
let size = self.child.layout(constraint, cx);
|
||||
(size, ())
|
||||
}
|
||||
|
||||
|
@ -227,18 +227,18 @@ impl Element for Expanded {
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
self.child.after_layout(ctx);
|
||||
self.child.after_layout(cx);
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
self.child.paint(bounds.origin(), ctx)
|
||||
self.child.paint(bounds.origin(), cx)
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -247,9 +247,9 @@ impl Element for Expanded {
|
|||
_: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
self.child.dispatch_event(event, ctx)
|
||||
self.child.dispatch_event(event, cx)
|
||||
}
|
||||
|
||||
fn metadata(&self) -> Option<&dyn Any> {
|
||||
|
@ -261,12 +261,12 @@ impl Element for Expanded {
|
|||
_: RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> Value {
|
||||
json!({
|
||||
"type": "Expanded",
|
||||
"flex": self.metadata.flex,
|
||||
"child": self.child.debug(ctx)
|
||||
"child": self.child.debug(cx)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -109,20 +109,20 @@ impl Element for Label {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let font_id = ctx
|
||||
let font_id = cx
|
||||
.font_cache
|
||||
.select_font(self.family_id, &self.font_properties)
|
||||
.unwrap();
|
||||
let runs = self.compute_runs(&ctx.font_cache, font_id);
|
||||
let runs = self.compute_runs(&cx.font_cache, font_id);
|
||||
let line =
|
||||
ctx.text_layout_cache
|
||||
cx.text_layout_cache
|
||||
.layout_str(self.text.as_str(), self.font_size, runs.as_slice());
|
||||
|
||||
let size = vec2f(
|
||||
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)
|
||||
|
@ -135,12 +135,12 @@ impl Element for Label {
|
|||
&mut self,
|
||||
bounds: RectF,
|
||||
line: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
line.paint(
|
||||
bounds.origin(),
|
||||
RectF::new(vec2f(0., 0.), bounds.size()),
|
||||
ctx,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -160,12 +160,12 @@ impl Element for Label {
|
|||
bounds: RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> Value {
|
||||
json!({
|
||||
"type": "Label",
|
||||
"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_properties": self.font_properties.to_json(),
|
||||
"text": &self.text,
|
||||
|
@ -191,13 +191,13 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
#[crate::test(self)]
|
||||
fn test_layout_label_with_highlights(app: &mut crate::MutableAppContext) {
|
||||
let menlo = app.font_cache().load_family(&["Menlo"]).unwrap();
|
||||
let menlo_regular = app
|
||||
fn test_layout_label_with_highlights(cx: &mut crate::MutableAppContext) {
|
||||
let menlo = cx.font_cache().load_family(&["Menlo"]).unwrap();
|
||||
let menlo_regular = cx
|
||||
.font_cache()
|
||||
.select_font(menlo, &Properties::new())
|
||||
.unwrap();
|
||||
let menlo_bold = app
|
||||
let menlo_bold = cx
|
||||
.font_cache()
|
||||
.select_font(menlo, Properties::new().weight(Weight::BOLD))
|
||||
.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!(
|
||||
runs.as_slice(),
|
||||
&[
|
||||
|
|
|
@ -35,20 +35,20 @@ impl Element for LineBox {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
match ctx
|
||||
match cx
|
||||
.font_cache
|
||||
.select_font(self.family_id, &self.font_properties)
|
||||
{
|
||||
Ok(font_id) => {
|
||||
let line_height = ctx.font_cache.line_height(font_id, self.font_size);
|
||||
let character_height = ctx.font_cache.ascent(font_id, self.font_size)
|
||||
+ ctx.font_cache.descent(font_id, self.font_size);
|
||||
let line_height = cx.font_cache.line_height(font_id, self.font_size);
|
||||
let character_height = cx.font_cache.ascent(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_size = self.child.layout(
|
||||
SizeConstraint::new(constraint.min.min(child_max), child_max),
|
||||
ctx,
|
||||
cx,
|
||||
);
|
||||
let size = vec2f(child_size.x(), line_height);
|
||||
(size, (line_height - character_height) / 2.)
|
||||
|
@ -64,19 +64,19 @@ impl Element for LineBox {
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
self.child.after_layout(ctx);
|
||||
self.child.after_layout(cx);
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: pathfinder_geometry::rect::RectF,
|
||||
padding_top: &mut f32,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
self.child
|
||||
.paint(bounds.origin() + vec2f(0., *padding_top), ctx);
|
||||
.paint(bounds.origin() + vec2f(0., *padding_top), cx);
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -85,9 +85,9 @@ impl Element for LineBox {
|
|||
_: pathfinder_geometry::rect::RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
self.child.dispatch_event(event, ctx)
|
||||
self.child.dispatch_event(event, cx)
|
||||
}
|
||||
|
||||
fn debug(
|
||||
|
@ -95,14 +95,14 @@ impl Element for LineBox {
|
|||
bounds: RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> serde_json::Value {
|
||||
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_properties": self.font_properties.to_json(),
|
||||
"child": self.child.debug(ctx),
|
||||
"child": self.child.debug(cx),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,13 +18,13 @@ pub struct MouseState {
|
|||
}
|
||||
|
||||
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
|
||||
Tag: 'static,
|
||||
F: FnOnce(MouseState) -> ElementBox,
|
||||
{
|
||||
let state_handle = ctx.value::<Tag, _>(id);
|
||||
let state = state_handle.read(ctx, |state| *state);
|
||||
let state_handle = cx.value::<Tag, _>(id);
|
||||
let state = state_handle.read(cx, |state| *state);
|
||||
let child = render_child(state);
|
||||
Self {
|
||||
state: state_handle,
|
||||
|
@ -46,27 +46,27 @@ impl Element for MouseEventHandler {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
(self.child.layout(constraint, ctx), ())
|
||||
(self.child.layout(constraint, cx), ())
|
||||
}
|
||||
|
||||
fn after_layout(
|
||||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
self.child.after_layout(ctx);
|
||||
self.child.after_layout(cx);
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
self.child.paint(bounds.origin(), ctx);
|
||||
self.child.paint(bounds.origin(), cx);
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -75,18 +75,18 @@ impl Element for MouseEventHandler {
|
|||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
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 } => {
|
||||
let mouse_in = bounds.contains_point(*position);
|
||||
if state.hovered != mouse_in {
|
||||
state.hovered = mouse_in;
|
||||
ctx.notify();
|
||||
cx.notify();
|
||||
true
|
||||
} else {
|
||||
handled_in_child
|
||||
|
@ -95,7 +95,7 @@ impl Element for MouseEventHandler {
|
|||
Event::LeftMouseDown { position, .. } => {
|
||||
if !handled_in_child && bounds.contains_point(*position) {
|
||||
state.clicked = true;
|
||||
ctx.notify();
|
||||
cx.notify();
|
||||
true
|
||||
} else {
|
||||
handled_in_child
|
||||
|
@ -104,10 +104,10 @@ impl Element for MouseEventHandler {
|
|||
Event::LeftMouseUp { position, .. } => {
|
||||
if !handled_in_child && state.clicked {
|
||||
state.clicked = false;
|
||||
ctx.notify();
|
||||
cx.notify();
|
||||
if let Some(handler) = click_handler {
|
||||
if bounds.contains_point(*position) {
|
||||
handler(ctx);
|
||||
handler(cx);
|
||||
}
|
||||
}
|
||||
true
|
||||
|
@ -124,11 +124,11 @@ impl Element for MouseEventHandler {
|
|||
_: RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> serde_json::Value {
|
||||
json!({
|
||||
"type": "MouseEventHandler",
|
||||
"child": self.child.debug(ctx),
|
||||
"child": self.child.debug(cx),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,11 +24,11 @@ impl Element for Stack {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let mut size = constraint.min;
|
||||
for child in &mut self.children {
|
||||
size = size.max(child.layout(constraint, ctx));
|
||||
size = size.max(child.layout(constraint, cx));
|
||||
}
|
||||
(size, ())
|
||||
}
|
||||
|
@ -37,10 +37,10 @@ impl Element for Stack {
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
for child in &mut self.children {
|
||||
child.after_layout(ctx);
|
||||
child.after_layout(cx);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,12 +48,12 @@ impl Element for Stack {
|
|||
&mut self,
|
||||
bounds: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
for child in &mut self.children {
|
||||
ctx.scene.push_layer(None);
|
||||
child.paint(bounds.origin(), ctx);
|
||||
ctx.scene.pop_layer();
|
||||
cx.scene.push_layer(None);
|
||||
child.paint(bounds.origin(), cx);
|
||||
cx.scene.pop_layer();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,10 +63,10 @@ impl Element for Stack {
|
|||
_: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
for child in self.children.iter_mut().rev() {
|
||||
if child.dispatch_event(event, ctx) {
|
||||
if child.dispatch_event(event, cx) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -78,12 +78,12 @@ impl Element for Stack {
|
|||
bounds: RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> json::Value {
|
||||
json!({
|
||||
"type": "Stack",
|
||||
"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>>()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,9 +38,9 @@ impl Element for Svg {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
match ctx.asset_cache.svg(&self.path) {
|
||||
match cx.asset_cache.svg(&self.path) {
|
||||
Ok(tree) => {
|
||||
let size = if constraint.max.x().is_infinite() && constraint.max.y().is_infinite() {
|
||||
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 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() {
|
||||
ctx.scene.push_icon(scene::Icon {
|
||||
cx.scene.push_icon(scene::Icon {
|
||||
bounds,
|
||||
svg,
|
||||
path: self.path.clone(),
|
||||
|
|
|
@ -72,7 +72,7 @@ where
|
|||
delta: Vector2F,
|
||||
precise: bool,
|
||||
scroll_max: f32,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
if !precise {
|
||||
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();
|
||||
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
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ where
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
if constraint.max.y().is_infinite() {
|
||||
unimplemented!(
|
||||
|
@ -133,9 +133,9 @@ where
|
|||
let mut scroll_max = 0.;
|
||||
|
||||
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() {
|
||||
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_constraint.min = item_size;
|
||||
item_constraint.max = item_size;
|
||||
|
@ -155,9 +155,9 @@ where
|
|||
self.item_count,
|
||||
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 {
|
||||
item.layout(item_constraint, ctx);
|
||||
item.layout(item_constraint, cx);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -175,10 +175,10 @@ where
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
layout: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
for item in &mut layout.items {
|
||||
item.after_layout(ctx);
|
||||
item.after_layout(cx);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -186,19 +186,19 @@ where
|
|||
&mut self,
|
||||
bounds: RectF,
|
||||
layout: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
ctx.scene.push_layer(Some(bounds));
|
||||
cx.scene.push_layer(Some(bounds));
|
||||
|
||||
let mut item_origin =
|
||||
bounds.origin() - vec2f(0.0, self.state.scroll_top() % layout.item_height);
|
||||
|
||||
for item in &mut layout.items {
|
||||
item.paint(item_origin, ctx);
|
||||
item.paint(item_origin, cx);
|
||||
item_origin += vec2f(0.0, layout.item_height);
|
||||
}
|
||||
|
||||
ctx.scene.pop_layer();
|
||||
cx.scene.pop_layer();
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -207,11 +207,11 @@ where
|
|||
bounds: RectF,
|
||||
layout: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
let mut handled = false;
|
||||
for item in &mut layout.items {
|
||||
handled = item.dispatch_event(event, ctx) || handled;
|
||||
handled = item.dispatch_event(event, cx) || handled;
|
||||
}
|
||||
|
||||
match event {
|
||||
|
@ -221,7 +221,7 @@ where
|
|||
precise,
|
||||
} => {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -237,14 +237,14 @@ where
|
|||
bounds: RectF,
|
||||
layout: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &crate::DebugContext,
|
||||
cx: &crate::DebugContext,
|
||||
) -> json::Value {
|
||||
json!({
|
||||
"type": "UniformList",
|
||||
"bounds": bounds.to_json(),
|
||||
"scroll_max": layout.scroll_max,
|
||||
"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>>()
|
||||
|
||||
})
|
||||
}
|
||||
|
|
|
@ -98,12 +98,12 @@ impl Matcher {
|
|||
&mut self,
|
||||
keystroke: Keystroke,
|
||||
view_id: usize,
|
||||
ctx: &Context,
|
||||
cx: &Context,
|
||||
) -> MatchResult {
|
||||
let pending = self.pending.entry(view_id).or_default();
|
||||
|
||||
if let Some(pending_ctx) = pending.context.as_ref() {
|
||||
if pending_ctx != ctx {
|
||||
if pending_ctx != cx {
|
||||
pending.keystrokes.clear();
|
||||
}
|
||||
}
|
||||
|
@ -113,11 +113,7 @@ impl Matcher {
|
|||
let mut retain_pending = false;
|
||||
for binding in self.keymap.0.iter().rev() {
|
||||
if binding.keystrokes.starts_with(&pending.keystrokes)
|
||||
&& binding
|
||||
.context
|
||||
.as_ref()
|
||||
.map(|c| c.eval(ctx))
|
||||
.unwrap_or(true)
|
||||
&& binding.context.as_ref().map(|c| c.eval(cx)).unwrap_or(true)
|
||||
{
|
||||
if binding.keystrokes.len() == pending.keystrokes.len() {
|
||||
self.pending.remove(&view_id);
|
||||
|
@ -127,7 +123,7 @@ impl Matcher {
|
|||
};
|
||||
} else {
|
||||
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 {
|
||||
Self::Identifier(name) => ctx.set.contains(name.as_str()),
|
||||
Self::Equal(left, right) => ctx
|
||||
Self::Identifier(name) => cx.set.contains(name.as_str()),
|
||||
Self::Equal(left, right) => cx
|
||||
.map
|
||||
.get(left)
|
||||
.map(|value| value == right)
|
||||
.unwrap_or(false),
|
||||
Self::NotEqual(left, right) => ctx
|
||||
.map
|
||||
.get(left)
|
||||
.map(|value| value != right)
|
||||
.unwrap_or(true),
|
||||
Self::Not(pred) => !pred.eval(ctx),
|
||||
Self::And(left, right) => left.eval(ctx) && right.eval(ctx),
|
||||
Self::Or(left, right) => left.eval(ctx) || right.eval(ctx),
|
||||
Self::NotEqual(left, right) => {
|
||||
cx.map.get(left).map(|value| value != right).unwrap_or(true)
|
||||
}
|
||||
Self::Not(pred) => !pred.eval(cx),
|
||||
Self::And(left, right) => left.eval(cx) && right.eval(cx),
|
||||
Self::Or(left, right) => left.eval(cx) || right.eval(cx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -488,10 +482,10 @@ mod tests {
|
|||
&mut self,
|
||||
keystroke: &str,
|
||||
view_id: usize,
|
||||
ctx: &Context,
|
||||
cx: &Context,
|
||||
) -> Option<(String, Option<A>)> {
|
||||
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())))
|
||||
} else {
|
||||
|
|
|
@ -145,7 +145,7 @@ impl FontSystemState {
|
|||
// Make room for subpixel variants.
|
||||
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 ctx = CGContext::create_bitmap_context(
|
||||
let cx = CGContext::create_bitmap_context(
|
||||
Some(pixels.as_mut_ptr() as *mut _),
|
||||
bounds.width() as usize,
|
||||
bounds.height() as usize,
|
||||
|
@ -157,9 +157,9 @@ impl FontSystemState {
|
|||
|
||||
// Move the origin to bottom left and account for scaling, this
|
||||
// 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());
|
||||
ctx.set_text_matrix(&CGAffineTransform {
|
||||
cx.set_text_matrix(&CGAffineTransform {
|
||||
a: transform.matrix.m11() as CGFloat,
|
||||
b: -transform.matrix.m21() as CGFloat,
|
||||
c: -transform.matrix.m12() as CGFloat,
|
||||
|
@ -168,9 +168,9 @@ impl FontSystemState {
|
|||
ty: -transform.vector.y() as CGFloat,
|
||||
});
|
||||
|
||||
ctx.set_font(&font.native_font().copy_to_CGFont());
|
||||
ctx.set_font_size(font_size as CGFloat);
|
||||
ctx.show_glyphs_at_positions(
|
||||
cx.set_font(&font.native_font().copy_to_CGFont());
|
||||
cx.set_font_size(font_size as CGFloat);
|
||||
cx.show_glyphs_at_positions(
|
||||
&[glyph_id as CGGlyph],
|
||||
&[CGPoint::new(
|
||||
(subpixel_shift.x() / scale_factor) as CGFloat,
|
||||
|
|
|
@ -31,11 +31,11 @@ impl Presenter {
|
|||
font_cache: Arc<FontCache>,
|
||||
text_layout_cache: TextLayoutCache,
|
||||
asset_cache: Arc<AssetCache>,
|
||||
app: &MutableAppContext,
|
||||
cx: &MutableAppContext,
|
||||
) -> Self {
|
||||
Self {
|
||||
window_id,
|
||||
rendered_views: app.render_views(window_id),
|
||||
rendered_views: cx.render_views(window_id),
|
||||
parents: HashMap::new(),
|
||||
font_cache,
|
||||
text_layout_cache,
|
||||
|
@ -55,7 +55,7 @@ impl Presenter {
|
|||
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 {
|
||||
invalidation.updated.remove(&view_id);
|
||||
self.rendered_views.remove(&view_id);
|
||||
|
@ -63,7 +63,7 @@ impl Presenter {
|
|||
}
|
||||
for view_id in invalidation.updated {
|
||||
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,
|
||||
window_size: Vector2F,
|
||||
scale_factor: f32,
|
||||
app: &mut MutableAppContext,
|
||||
cx: &mut MutableAppContext,
|
||||
) -> Scene {
|
||||
let mut scene = Scene::new(scale_factor);
|
||||
|
||||
if let Some(root_view_id) = app.root_view_id(self.window_id) {
|
||||
self.layout(window_size, app.as_ref());
|
||||
self.after_layout(app);
|
||||
let mut ctx = PaintContext {
|
||||
if let Some(root_view_id) = cx.root_view_id(self.window_id) {
|
||||
self.layout(window_size, cx.as_ref());
|
||||
self.after_layout(cx);
|
||||
let mut paint_cx = PaintContext {
|
||||
scene: &mut scene,
|
||||
font_cache: &self.font_cache,
|
||||
text_layout_cache: &self.text_layout_cache,
|
||||
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();
|
||||
|
||||
if let Some(event) = self.last_mouse_moved_event.clone() {
|
||||
self.dispatch_event(event, app)
|
||||
self.dispatch_event(event, cx)
|
||||
}
|
||||
} else {
|
||||
log::error!("could not find root_view_id for window {}", self.window_id);
|
||||
|
@ -98,8 +98,8 @@ impl Presenter {
|
|||
scene
|
||||
}
|
||||
|
||||
fn layout(&mut self, size: Vector2F, app: &AppContext) {
|
||||
if let Some(root_view_id) = app.root_view_id(self.window_id) {
|
||||
fn layout(&mut self, size: Vector2F, cx: &AppContext) {
|
||||
if let Some(root_view_id) = cx.root_view_id(self.window_id) {
|
||||
let mut layout_ctx = LayoutContext {
|
||||
rendered_views: &mut self.rendered_views,
|
||||
parents: &mut self.parents,
|
||||
|
@ -107,49 +107,49 @@ impl Presenter {
|
|||
text_layout_cache: &self.text_layout_cache,
|
||||
asset_cache: &self.asset_cache,
|
||||
view_stack: Vec::new(),
|
||||
app,
|
||||
app: cx,
|
||||
};
|
||||
layout_ctx.layout(root_view_id, SizeConstraint::strict(size));
|
||||
}
|
||||
}
|
||||
|
||||
fn after_layout(&mut self, app: &mut MutableAppContext) {
|
||||
if let Some(root_view_id) = app.root_view_id(self.window_id) {
|
||||
let mut ctx = AfterLayoutContext {
|
||||
fn after_layout(&mut self, cx: &mut MutableAppContext) {
|
||||
if let Some(root_view_id) = cx.root_view_id(self.window_id) {
|
||||
let mut layout_cx = AfterLayoutContext {
|
||||
rendered_views: &mut self.rendered_views,
|
||||
font_cache: &self.font_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) {
|
||||
if let Some(root_view_id) = app.root_view_id(self.window_id) {
|
||||
pub fn dispatch_event(&mut self, event: Event, cx: &mut MutableAppContext) {
|
||||
if let Some(root_view_id) = cx.root_view_id(self.window_id) {
|
||||
if matches!(event, Event::MouseMoved { .. }) {
|
||||
self.last_mouse_moved_event = Some(event.clone());
|
||||
}
|
||||
|
||||
let mut ctx = EventContext {
|
||||
let mut event_cx = EventContext {
|
||||
rendered_views: &mut self.rendered_views,
|
||||
actions: Default::default(),
|
||||
font_cache: &self.font_cache,
|
||||
text_layout_cache: &self.text_layout_cache,
|
||||
view_stack: 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 actions = ctx.actions;
|
||||
let invalidated_views = event_cx.invalidated_views;
|
||||
let actions = event_cx.actions;
|
||||
|
||||
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 {
|
||||
app.dispatch_action_any(
|
||||
cx.dispatch_action_any(
|
||||
self.window_id,
|
||||
&action.path,
|
||||
action.name,
|
||||
|
@ -159,14 +159,14 @@ impl Presenter {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn debug_elements(&self, ctx: &AppContext) -> Option<json::Value> {
|
||||
ctx.root_view_id(self.window_id)
|
||||
pub fn debug_elements(&self, cx: &AppContext) -> Option<json::Value> {
|
||||
cx.root_view_id(self.window_id)
|
||||
.and_then(|root_view_id| self.rendered_views.get(&root_view_id))
|
||||
.map(|root_element| {
|
||||
root_element.debug(&DebugContext {
|
||||
rendered_views: &self.rendered_views,
|
||||
font_cache: &self.font_cache,
|
||||
app: ctx,
|
||||
app: cx,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -380,9 +380,9 @@ impl Element for ChildView {
|
|||
fn layout(
|
||||
&mut self,
|
||||
constraint: SizeConstraint,
|
||||
ctx: &mut LayoutContext,
|
||||
cx: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let size = ctx.layout(self.view_id, constraint);
|
||||
let size = cx.layout(self.view_id, constraint);
|
||||
(size, ())
|
||||
}
|
||||
|
||||
|
@ -390,18 +390,18 @@ impl Element for ChildView {
|
|||
&mut self,
|
||||
_: Vector2F,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut AfterLayoutContext,
|
||||
cx: &mut AfterLayoutContext,
|
||||
) {
|
||||
ctx.after_layout(self.view_id);
|
||||
cx.after_layout(self.view_id);
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: pathfinder_geometry::rect::RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
ctx: &mut PaintContext,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
ctx.paint(self.view_id, bounds.origin());
|
||||
cx.paint(self.view_id, bounds.origin());
|
||||
}
|
||||
|
||||
fn dispatch_event(
|
||||
|
@ -410,9 +410,9 @@ impl Element for ChildView {
|
|||
_: pathfinder_geometry::rect::RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
ctx: &mut EventContext,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
ctx.dispatch_event(self.view_id, event)
|
||||
cx.dispatch_event(self.view_id, event)
|
||||
}
|
||||
|
||||
fn debug(
|
||||
|
@ -420,14 +420,14 @@ impl Element for ChildView {
|
|||
bounds: pathfinder_geometry::rect::RectF,
|
||||
_: &Self::LayoutState,
|
||||
_: &Self::PaintState,
|
||||
ctx: &DebugContext,
|
||||
cx: &DebugContext,
|
||||
) -> serde_json::Value {
|
||||
json!({
|
||||
"type": "ChildView",
|
||||
"view_id": self.view_id,
|
||||
"bounds": bounds.to_json(),
|
||||
"child": if let Some(view) = ctx.rendered_views.get(&self.view_id) {
|
||||
view.debug(ctx)
|
||||
"child": if let Some(view) = cx.rendered_views.get(&self.view_id) {
|
||||
view.debug(cx)
|
||||
} else {
|
||||
json!(null)
|
||||
}
|
||||
|
@ -441,9 +441,9 @@ mod tests {
|
|||
// fn test_responder_chain() {
|
||||
// let settings = settings_rx(None);
|
||||
// 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) =
|
||||
// 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_ = invalidations.clone();
|
||||
|
@ -451,8 +451,8 @@ mod tests {
|
|||
// invalidations_.borrow_mut().push(invalidation)
|
||||
// });
|
||||
|
||||
// let active_pane_id = workspace_view.update(&mut app, |view, ctx| {
|
||||
// ctx.focus(view.active_pane());
|
||||
// let active_pane_id = workspace_view.update(&mut app, |view, cx| {
|
||||
// cx.focus(view.active_pane());
|
||||
// view.active_pane().id()
|
||||
// });
|
||||
|
||||
|
@ -468,7 +468,7 @@ mod tests {
|
|||
// }
|
||||
|
||||
// assert_eq!(
|
||||
// presenter.responder_chain(app.ctx()).unwrap(),
|
||||
// presenter.responder_chain(app.cx()).unwrap(),
|
||||
// vec![workspace_view.id(), active_pane_id]
|
||||
// );
|
||||
// });
|
||||
|
|
|
@ -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 baseline_origin = vec2f(0., padding_top + self.layout.ascent);
|
||||
|
||||
|
@ -209,7 +209,7 @@ impl Line {
|
|||
let mut color = ColorU::black();
|
||||
|
||||
for run in &self.layout.runs {
|
||||
let max_glyph_width = ctx
|
||||
let max_glyph_width = cx
|
||||
.font_cache
|
||||
.bounding_box(run.font_id, self.layout.font_size)
|
||||
.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_size: self.layout.font_size,
|
||||
id: glyph.id,
|
||||
|
|
|
@ -34,8 +34,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
fn #outer_fn_name() {
|
||||
#inner_fn
|
||||
|
||||
#namespace::App::test_async((), move |ctx| async {
|
||||
#inner_fn_name(ctx).await;
|
||||
#namespace::App::test_async((), move |cx| async {
|
||||
#inner_fn_name(cx).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -45,8 +45,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
fn #outer_fn_name() {
|
||||
#inner_fn
|
||||
|
||||
#namespace::App::test((), |ctx| {
|
||||
#inner_fn_name(ctx);
|
||||
#namespace::App::test((), |cx| {
|
||||
#inner_fn_name(cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue