Rename context parameters to cx in gpui

This commit is contained in:
Max Brunsfeld
2021-05-28 15:32:35 -07:00
parent 173f99748d
commit 6ef447866a
20 changed files with 643 additions and 733 deletions
+10 -10
View File
@@ -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),
})
}
}
+2 -2
View File
@@ -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(
+10 -10
View File
@@ -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)})
}
}
+12 -12
View File
@@ -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),
})
}
}
+11 -11
View File
@@ -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),
})
}
}
+21 -21
View File
@@ -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)
})
}
}
+14 -14
View File
@@ -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(),
&[
+15 -15
View File
@@ -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 -18
View File
@@ -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),
})
}
}
+12 -12
View File
@@ -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>>()
})
}
}
+4 -4
View File
@@ -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(),
+18 -18
View File
@@ -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>>()
})
}