ori_core/views/
build_handler.rsuse crate::{
context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
event::Event,
layout::{Size, Space},
view::View,
};
pub fn on_build<T, V>(
content: V,
after: impl FnMut(&mut BuildCx, &mut T) + 'static,
) -> BuildHandler<T, V> {
BuildHandler::new(content).after(after)
}
pub struct BuildHandler<T, V> {
pub content: V,
#[allow(clippy::type_complexity)]
pub after: Option<Box<dyn FnMut(&mut BuildCx, &mut T) + 'static>>,
}
impl<T, V> BuildHandler<T, V> {
pub fn new(content: V) -> Self {
Self {
content,
after: None,
}
}
pub fn after(mut self, after: impl FnMut(&mut BuildCx, &mut T) + 'static) -> Self {
self.after = Some(Box::new(after));
self
}
}
impl<T, V: View<T>> View<T> for BuildHandler<T, V> {
type State = V::State;
fn build(&mut self, cx: &mut BuildCx, data: &mut T) -> Self::State {
let state = self.content.build(cx, data);
if let Some(after) = &mut self.after {
after(cx, data);
}
state
}
fn rebuild(&mut self, state: &mut Self::State, cx: &mut RebuildCx, data: &mut T, old: &Self) {
self.content.rebuild(state, cx, data, &old.content);
}
fn event(
&mut self,
state: &mut Self::State,
cx: &mut EventCx,
data: &mut T,
event: &Event,
) -> bool {
self.content.event(state, cx, data, event)
}
fn layout(
&mut self,
state: &mut Self::State,
cx: &mut LayoutCx,
data: &mut T,
space: Space,
) -> Size {
self.content.layout(state, cx, data, space)
}
fn draw(&mut self, state: &mut Self::State, cx: &mut DrawCx, data: &mut T) {
self.content.draw(state, cx, data);
}
}