align.rs

 1use crate::{
 2    AfterLayoutContext, AppContext, Element, Event, EventContext, LayoutContext, MutableAppContext,
 3    PaintContext, SizeConstraint,
 4};
 5use pathfinder_geometry::vector::{vec2f, Vector2F};
 6
 7pub struct Align {
 8    child: Box<dyn Element>,
 9    alignment: Vector2F,
10    size: Option<Vector2F>,
11}
12
13impl Align {
14    pub fn new(child: Box<dyn Element>) -> Self {
15        Self {
16            child,
17            alignment: Vector2F::zero(),
18            size: None,
19        }
20    }
21
22    pub fn top_center(mut self) -> Self {
23        self.alignment = vec2f(0.0, -1.0);
24        self
25    }
26}
27
28impl Element for Align {
29    fn layout(
30        &mut self,
31        mut constraint: SizeConstraint,
32        ctx: &mut LayoutContext,
33        app: &AppContext,
34    ) -> Vector2F {
35        let mut size = constraint.max;
36        constraint.min = Vector2F::zero();
37        let child_size = self.child.layout(constraint, ctx, app);
38        if size.x().is_infinite() {
39            size.set_x(child_size.x());
40        }
41        if size.y().is_infinite() {
42            size.set_y(child_size.y());
43        }
44        self.size = Some(size);
45        size
46    }
47
48    fn after_layout(&mut self, ctx: &mut AfterLayoutContext, app: &mut MutableAppContext) {
49        self.child.after_layout(ctx, app);
50    }
51
52    fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext, app: &AppContext) {
53        let self_center = self.size.unwrap() / 2.0;
54        let self_target = self_center + self_center * self.alignment;
55        let child_center = self.child.size().unwrap() / 2.0;
56        let child_target = child_center + child_center * self.alignment;
57        let origin = origin - (child_target - self_target);
58        self.child.paint(origin, ctx, app);
59    }
60
61    fn dispatch_event(&self, event: &Event, ctx: &mut EventContext, app: &AppContext) -> bool {
62        self.child.dispatch_event(event, ctx, app)
63    }
64
65    fn size(&self) -> Option<Vector2F> {
66        self.size
67    }
68}