2021-09-03 10:18:31 +00:00
|
|
|
use crate::{
|
|
|
|
geometry::{rect::RectF, vector::Vector2F},
|
|
|
|
json::json,
|
|
|
|
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, PaintContext,
|
|
|
|
SizeConstraint,
|
|
|
|
};
|
|
|
|
|
2021-09-03 15:55:39 +00:00
|
|
|
pub struct Hook {
|
2021-09-03 10:18:31 +00:00
|
|
|
child: ElementBox,
|
2021-09-03 15:19:57 +00:00
|
|
|
after_layout: Option<Box<dyn FnMut(Vector2F, &mut LayoutContext)>>,
|
2021-09-03 10:18:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 15:55:39 +00:00
|
|
|
impl Hook {
|
2021-09-03 10:18:31 +00:00
|
|
|
pub fn new(child: ElementBox) -> Self {
|
|
|
|
Self {
|
|
|
|
child,
|
2021-09-03 15:19:57 +00:00
|
|
|
after_layout: None,
|
2021-09-03 10:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 15:19:57 +00:00
|
|
|
pub fn on_after_layout(
|
2021-09-03 10:18:31 +00:00
|
|
|
mut self,
|
2021-09-03 15:19:57 +00:00
|
|
|
f: impl 'static + FnMut(Vector2F, &mut LayoutContext),
|
2021-09-03 10:18:31 +00:00
|
|
|
) -> Self {
|
2021-09-03 15:19:57 +00:00
|
|
|
self.after_layout = Some(Box::new(f));
|
2021-09-03 10:18:31 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 15:55:39 +00:00
|
|
|
impl Element for Hook {
|
2021-09-03 10:18:31 +00:00
|
|
|
type LayoutState = ();
|
|
|
|
type PaintState = ();
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
constraint: SizeConstraint,
|
|
|
|
cx: &mut LayoutContext,
|
|
|
|
) -> (Vector2F, Self::LayoutState) {
|
|
|
|
let size = self.child.layout(constraint, cx);
|
2021-09-03 15:19:57 +00:00
|
|
|
if let Some(handler) = self.after_layout.as_mut() {
|
|
|
|
handler(size, cx);
|
|
|
|
}
|
2021-09-03 10:18:31 +00:00
|
|
|
(size, ())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
|
|
|
bounds: RectF,
|
|
|
|
visible_bounds: RectF,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
cx: &mut PaintContext,
|
|
|
|
) {
|
|
|
|
self.child.paint(bounds.origin(), visible_bounds, cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dispatch_event(
|
|
|
|
&mut self,
|
|
|
|
event: &Event,
|
|
|
|
_: RectF,
|
2022-04-07 13:10:09 +00:00
|
|
|
_: RectF,
|
2021-09-03 10:18:31 +00:00
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
_: &mut Self::PaintState,
|
|
|
|
cx: &mut EventContext,
|
|
|
|
) -> bool {
|
|
|
|
self.child.dispatch_event(event, cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn debug(
|
|
|
|
&self,
|
|
|
|
_: RectF,
|
|
|
|
_: &Self::LayoutState,
|
|
|
|
_: &Self::PaintState,
|
|
|
|
cx: &DebugContext,
|
|
|
|
) -> serde_json::Value {
|
|
|
|
json!({
|
|
|
|
"type": "Hooks",
|
|
|
|
"child": self.child.debug(cx),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|