2021-03-10 04:00:51 +00:00
|
|
|
use crate::{
|
2021-04-07 05:50:13 +00:00
|
|
|
json, AfterLayoutContext, DebugContext, Element, ElementBox, Event, EventContext,
|
|
|
|
LayoutContext, PaintContext, SizeConstraint,
|
2021-03-10 04:00:51 +00:00
|
|
|
};
|
2021-04-07 05:50:13 +00:00
|
|
|
use json::ToJson;
|
2021-04-14 22:14:40 +00:00
|
|
|
use pathfinder_geometry::vector::Vector2F;
|
2021-04-07 05:50:13 +00:00
|
|
|
use serde_json::json;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
|
|
pub struct Align {
|
2021-03-22 02:54:23 +00:00
|
|
|
child: ElementBox,
|
2021-03-10 04:00:51 +00:00
|
|
|
alignment: Vector2F,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Align {
|
2021-03-22 02:54:23 +00:00
|
|
|
pub fn new(child: ElementBox) -> Self {
|
2021-03-10 04:00:51 +00:00
|
|
|
Self {
|
|
|
|
child,
|
|
|
|
alignment: Vector2F::zero(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 22:14:40 +00:00
|
|
|
pub fn top(mut self) -> Self {
|
|
|
|
self.alignment.set_y(-1.0);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn right(mut self) -> Self {
|
|
|
|
self.alignment.set_x(1.0);
|
2021-03-10 04:00:51 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Element for Align {
|
2021-03-22 02:54:23 +00:00
|
|
|
type LayoutState = ();
|
|
|
|
type PaintState = ();
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
mut constraint: SizeConstraint,
|
|
|
|
ctx: &mut LayoutContext,
|
2021-03-22 02:54:23 +00:00
|
|
|
) -> (Vector2F, Self::LayoutState) {
|
2021-03-10 04:00:51 +00:00
|
|
|
let mut size = constraint.max;
|
|
|
|
constraint.min = Vector2F::zero();
|
2021-03-22 02:54:23 +00:00
|
|
|
let child_size = self.child.layout(constraint, ctx);
|
2021-03-10 04:00:51 +00:00
|
|
|
if size.x().is_infinite() {
|
|
|
|
size.set_x(child_size.x());
|
|
|
|
}
|
|
|
|
if size.y().is_infinite() {
|
|
|
|
size.set_y(child_size.y());
|
|
|
|
}
|
2021-03-22 02:54:23 +00:00
|
|
|
(size, ())
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn after_layout(
|
|
|
|
&mut self,
|
|
|
|
_: Vector2F,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
ctx: &mut AfterLayoutContext,
|
|
|
|
) {
|
|
|
|
self.child.after_layout(ctx);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn paint(
|
|
|
|
&mut self,
|
|
|
|
bounds: pathfinder_geometry::rect::RectF,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
ctx: &mut PaintContext,
|
|
|
|
) -> Self::PaintState {
|
|
|
|
let my_center = bounds.size() / 2.;
|
|
|
|
let my_target = my_center + my_center * self.alignment;
|
|
|
|
|
|
|
|
let child_center = self.child.size() / 2.;
|
2021-03-10 04:00:51 +00:00
|
|
|
let child_target = child_center + child_center * self.alignment;
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
self.child
|
|
|
|
.paint(bounds.origin() - (child_target - my_target), ctx);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn dispatch_event(
|
|
|
|
&mut self,
|
|
|
|
event: &Event,
|
|
|
|
_: pathfinder_geometry::rect::RectF,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
_: &mut Self::PaintState,
|
|
|
|
ctx: &mut EventContext,
|
|
|
|
) -> bool {
|
|
|
|
self.child.dispatch_event(event, ctx)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2021-04-07 05:50:13 +00:00
|
|
|
|
|
|
|
fn debug(
|
|
|
|
&self,
|
|
|
|
bounds: pathfinder_geometry::rect::RectF,
|
|
|
|
_: &Self::LayoutState,
|
|
|
|
_: &Self::PaintState,
|
|
|
|
ctx: &DebugContext,
|
|
|
|
) -> json::Value {
|
|
|
|
json!({
|
|
|
|
"type": "Align",
|
|
|
|
"bounds": bounds.to_json(),
|
2021-04-08 03:54:05 +00:00
|
|
|
"alignment": self.alignment.to_json(),
|
2021-04-07 05:50:13 +00:00
|
|
|
"child": self.child.debug(ctx),
|
|
|
|
})
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|