2021-04-07 05:50:13 +00:00
|
|
|
use json::ToJson;
|
|
|
|
use serde_json::json;
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
use crate::{
|
2021-04-07 05:50:13 +00:00
|
|
|
geometry::{rect::RectF, vector::Vector2F},
|
|
|
|
json, AfterLayoutContext, DebugContext, Element, ElementBox, Event, EventContext,
|
|
|
|
LayoutContext, PaintContext, SizeConstraint,
|
2021-03-10 04:00:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct ConstrainedBox {
|
2021-03-22 02:54:23 +00:00
|
|
|
child: ElementBox,
|
2021-03-10 04:00:51 +00:00
|
|
|
constraint: SizeConstraint,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConstrainedBox {
|
2021-03-22 02:54:23 +00:00
|
|
|
pub fn new(child: ElementBox) -> Self {
|
2021-03-10 04:00:51 +00:00
|
|
|
Self {
|
|
|
|
child,
|
|
|
|
constraint: SizeConstraint {
|
|
|
|
min: Vector2F::zero(),
|
|
|
|
max: Vector2F::splat(f32::INFINITY),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_max_width(mut self, max_width: f32) -> Self {
|
|
|
|
self.constraint.max.set_x(max_width);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_max_height(mut self, max_height: f32) -> Self {
|
|
|
|
self.constraint.max.set_y(max_height);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_height(mut self, height: f32) -> Self {
|
|
|
|
self.constraint.min.set_y(height);
|
|
|
|
self.constraint.max.set_y(height);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Element for ConstrainedBox {
|
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
|
|
|
constraint.min = constraint.min.max(self.constraint.min);
|
|
|
|
constraint.max = constraint.max.min(self.constraint.max);
|
2021-03-22 02:54:23 +00:00
|
|
|
let size = self.child.layout(constraint, ctx);
|
|
|
|
(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,
|
2021-04-07 05:50:13 +00:00
|
|
|
bounds: RectF,
|
2021-03-22 02:54:23 +00:00
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
ctx: &mut PaintContext,
|
|
|
|
) -> Self::PaintState {
|
|
|
|
self.child.paint(bounds.origin(), ctx);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn dispatch_event(
|
|
|
|
&mut self,
|
|
|
|
event: &Event,
|
2021-04-07 05:50:13 +00:00
|
|
|
_: RectF,
|
2021-03-22 02:54:23 +00:00
|
|
|
_: &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,
|
|
|
|
_: RectF,
|
|
|
|
_: &Self::LayoutState,
|
|
|
|
_: &Self::PaintState,
|
|
|
|
ctx: &DebugContext,
|
|
|
|
) -> json::Value {
|
|
|
|
json!({"type": "ConstrainedBox", "constraint": self.constraint.to_json(), "child": self.child.debug(ctx)})
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|