use super::{AdaptConfigCx, AdaptEventCx};
#[allow(unused)] use kas::Events;
use kas::event::{ConfigCx, Event, EventCx, IsUsed};
use kas::{Id, Widget};
use kas::{autoimpl, impl_self};
use std::fmt::Debug;
#[impl_self]
mod AdaptEvents {
#[autoimpl(Deref, DerefMut using self.inner)]
#[autoimpl(Viewport using self.inner where W: trait)]
#[derive_widget]
pub struct AdaptEvents<W: Widget> {
#[widget]
pub inner: W,
on_configure: Option<Box<dyn Fn(&mut AdaptConfigCx, &mut W)>>,
on_update: Option<Box<dyn Fn(&mut AdaptConfigCx, &mut W, &W::Data)>>,
message_handlers: Vec<Box<dyn Fn(&mut AdaptEventCx, &mut W, &W::Data)>>,
}
impl<W: Widget> AdaptEvents<W> {
#[inline]
pub fn new(inner: W) -> Self {
AdaptEvents {
inner,
on_configure: None,
on_update: None,
message_handlers: vec![],
}
}
#[must_use]
pub fn on_configure<F>(mut self, f: F) -> Self
where
F: Fn(&mut AdaptConfigCx, &mut W) + 'static,
{
self.on_configure = Some(Box::new(f));
self
}
#[must_use]
pub fn on_update<F>(mut self, f: F) -> Self
where
F: Fn(&mut AdaptConfigCx, &mut W, &W::Data) + 'static,
{
self.on_update = Some(Box::new(f));
self
}
#[must_use]
pub fn on_message<M, H>(self, handler: H) -> Self
where
M: Debug + 'static,
H: Fn(&mut AdaptEventCx, &mut W, M) + 'static,
{
self.on_messages(move |cx, w, _data| {
if let Some(m) = cx.try_pop() {
handler(cx, w, m);
}
})
}
pub fn map_message<M, N, H>(self, handler: H) -> Self
where
M: Debug + 'static,
N: Debug + 'static,
H: Fn(usize, M) -> N + 'static,
{
self.on_messages(move |cx, _, _| {
if let Some(index) = cx.last_child() {
if let Some(m) = cx.try_pop() {
cx.push(handler(index, m));
}
}
})
}
#[must_use]
pub fn on_messages<H>(mut self, handler: H) -> Self
where
H: Fn(&mut AdaptEventCx, &mut W, &W::Data) + 'static,
{
self.message_handlers.push(Box::new(handler));
self
}
}
impl<W: Widget> Widget for AdaptEvents<W> {
type Data = W::Data;
fn _configure(&mut self, cx: &mut ConfigCx, data: &Self::Data, id: Id) {
self.inner._configure(cx, data, id);
if let Some(ref f) = self.on_configure {
let mut cx = AdaptConfigCx::new(cx, self.inner.id());
f(&mut cx, &mut self.inner);
}
if let Some(ref f) = self.on_update {
let mut cx = AdaptConfigCx::new(cx, self.inner.id());
f(&mut cx, &mut self.inner, data);
}
}
fn _update(&mut self, cx: &mut ConfigCx, data: &Self::Data) {
self.inner._update(cx, data);
if let Some(ref f) = self.on_update {
let mut cx = AdaptConfigCx::new(cx, self.inner.id());
f(&mut cx, &mut self.inner, data);
}
}
fn _send(&mut self, cx: &mut EventCx, data: &Self::Data, id: Id, event: Event) -> IsUsed {
let is_used = self.inner._send(cx, data, id, event);
if cx.has_msg() {
let mut cx = AdaptEventCx::new(cx, self.inner.id());
for handler in self.message_handlers.iter() {
handler(&mut cx, &mut self.inner, data);
}
}
is_used
}
fn _replay(&mut self, cx: &mut EventCx, data: &Self::Data, id: Id) {
self.inner._replay(cx, data, id);
if cx.has_msg() {
let mut cx = AdaptEventCx::new(cx, self.inner.id());
for handler in self.message_handlers.iter() {
handler(&mut cx, &mut self.inner, data);
}
}
}
}
}